Skip to content

Commit 6d82120

Browse files
djbwtorvalds
authored andcommitted
device-dax: add an 'align' attribute
Introduce a device align attribute. While doing so, rename the region align attribute to be more explicitly named as so, but keep it named as @align to retain the API for tools like daxctl. Changes on align may not always be valid, when say certain mappings were created with 2M and then we switch to 1G. So, we validate all ranges against the new value being attempted, post resizing. Signed-off-by: Joao Martins <joao.m.martins@oracle.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Ben Skeggs <bskeggs@redhat.com> Cc: Bjorn Helgaas <bhelgaas@google.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Cc: Brice Goglin <Brice.Goglin@inria.fr> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Daniel Vetter <daniel@ffwll.ch> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Dave Jiang <dave.jiang@intel.com> Cc: David Airlie <airlied@linux.ie> Cc: David Hildenbrand <david@redhat.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Hulk Robot <hulkci@huawei.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Ira Weiny <ira.weiny@intel.com> Cc: Jason Gunthorpe <jgg@mellanox.com> Cc: Jason Yan <yanaijie@huawei.com> Cc: Jeff Moyer <jmoyer@redhat.com> Cc: "Jérôme Glisse" <jglisse@redhat.com> Cc: Jia He <justin.he@arm.com> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com> Cc: Juergen Gross <jgross@suse.com> Cc: kernel test robot <lkp@intel.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Mike Rapoport <rppt@linux.ibm.com> Cc: Paul Mackerras <paulus@ozlabs.org> Cc: Pavel Tatashin <pasha.tatashin@soleen.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com> Cc: Randy Dunlap <rdunlap@infradead.org> Cc: Stefano Stabellini <sstabellini@kernel.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Tom Lendacky <thomas.lendacky@amd.com> Cc: Vishal Verma <vishal.l.verma@intel.com> Cc: Vivek Goyal <vgoyal@redhat.com> Cc: Wei Yang <richard.weiyang@linux.alibaba.com> Cc: Will Deacon <will@kernel.org> Link: https://lkml.kernel.org/r/159643105944.4062302.3131761052969132784.stgit@dwillia2-desk3.amr.corp.intel.com Link: https://lore.kernel.org/r/20200716172913.19658-3-joao.m.martins@oracle.com Link: https://lkml.kernel.org/r/160106118486.30709.13012322227204800596.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 33cf94d commit 6d82120

2 files changed

Lines changed: 101 additions & 10 deletions

File tree

‎drivers/dax/bus.c‎

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,15 @@ static ssize_t region_size_show(struct device *dev,
230230
static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
231231
region_size_show, NULL);
232232

233-
static ssize_t align_show(struct device *dev,
233+
static ssize_t region_align_show(struct device *dev,
234234
struct device_attribute *attr, char *buf)
235235
{
236236
struct dax_region *dax_region = dev_get_drvdata(dev);
237237

238238
return sprintf(buf, "%u\n", dax_region->align);
239239
}
240-
static DEVICE_ATTR_RO(align);
240+
static struct device_attribute dev_attr_region_align =
241+
__ATTR(align, 0400, region_align_show, NULL);
241242

242243
#define for_each_dax_region_resource(dax_region, res) \
243244
for (res = (dax_region)->res.child; res; res = res->sibling)
@@ -488,7 +489,7 @@ static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
488489
static struct attribute *dax_region_attributes[] = {
489490
&dev_attr_available_size.attr,
490491
&dev_attr_region_size.attr,
491-
&dev_attr_align.attr,
492+
&dev_attr_region_align.attr,
492493
&dev_attr_create.attr,
493494
&dev_attr_seed.attr,
494495
&dev_attr_delete.attr,
@@ -858,15 +859,13 @@ static ssize_t size_show(struct device *dev,
858859
return sprintf(buf, "%llu\n", size);
859860
}
860861

861-
static bool alloc_is_aligned(struct dax_region *dax_region,
862-
resource_size_t size)
862+
static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
863863
{
864864
/*
865865
* The minimum mapping granularity for a device instance is a
866866
* single subsection, unless the arch says otherwise.
867867
*/
868-
return IS_ALIGNED(size, max_t(unsigned long, dax_region->align,
869-
memremap_compat_align()));
868+
return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
870869
}
871870

872871
static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
@@ -961,7 +960,7 @@ static ssize_t dev_dax_resize(struct dax_region *dax_region,
961960
return dev_dax_shrink(dev_dax, size);
962961

963962
to_alloc = size - dev_size;
964-
if (dev_WARN_ONCE(dev, !alloc_is_aligned(dax_region, to_alloc),
963+
if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
965964
"resize of %pa misaligned\n", &to_alloc))
966965
return -ENXIO;
967966

@@ -1025,7 +1024,7 @@ static ssize_t size_store(struct device *dev, struct device_attribute *attr,
10251024
if (rc)
10261025
return rc;
10271026

1028-
if (!alloc_is_aligned(dax_region, val)) {
1027+
if (!alloc_is_aligned(dev_dax, val)) {
10291028
dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
10301029
return -EINVAL;
10311030
}
@@ -1044,6 +1043,78 @@ static ssize_t size_store(struct device *dev, struct device_attribute *attr,
10441043
}
10451044
static DEVICE_ATTR_RW(size);
10461045

1046+
static ssize_t align_show(struct device *dev,
1047+
struct device_attribute *attr, char *buf)
1048+
{
1049+
struct dev_dax *dev_dax = to_dev_dax(dev);
1050+
1051+
return sprintf(buf, "%d\n", dev_dax->align);
1052+
}
1053+
1054+
static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
1055+
{
1056+
resource_size_t dev_size = dev_dax_size(dev_dax);
1057+
struct device *dev = &dev_dax->dev;
1058+
int i;
1059+
1060+
if (dev_size > 0 && !alloc_is_aligned(dev_dax, dev_size)) {
1061+
dev_dbg(dev, "%s: align %u invalid for size %pa\n",
1062+
__func__, dev_dax->align, &dev_size);
1063+
return -EINVAL;
1064+
}
1065+
1066+
for (i = 0; i < dev_dax->nr_range; i++) {
1067+
size_t len = range_len(&dev_dax->ranges[i].range);
1068+
1069+
if (!alloc_is_aligned(dev_dax, len)) {
1070+
dev_dbg(dev, "%s: align %u invalid for range %d\n",
1071+
__func__, dev_dax->align, i);
1072+
return -EINVAL;
1073+
}
1074+
}
1075+
1076+
return 0;
1077+
}
1078+
1079+
static ssize_t align_store(struct device *dev, struct device_attribute *attr,
1080+
const char *buf, size_t len)
1081+
{
1082+
struct dev_dax *dev_dax = to_dev_dax(dev);
1083+
struct dax_region *dax_region = dev_dax->region;
1084+
unsigned long val, align_save;
1085+
ssize_t rc;
1086+
1087+
rc = kstrtoul(buf, 0, &val);
1088+
if (rc)
1089+
return -ENXIO;
1090+
1091+
if (!dax_align_valid(val))
1092+
return -EINVAL;
1093+
1094+
device_lock(dax_region->dev);
1095+
if (!dax_region->dev->driver) {
1096+
device_unlock(dax_region->dev);
1097+
return -ENXIO;
1098+
}
1099+
1100+
device_lock(dev);
1101+
if (dev->driver) {
1102+
rc = -EBUSY;
1103+
goto out_unlock;
1104+
}
1105+
1106+
align_save = dev_dax->align;
1107+
dev_dax->align = val;
1108+
rc = dev_dax_validate_align(dev_dax);
1109+
if (rc)
1110+
dev_dax->align = align_save;
1111+
out_unlock:
1112+
device_unlock(dev);
1113+
device_unlock(dax_region->dev);
1114+
return rc == 0 ? len : rc;
1115+
}
1116+
static DEVICE_ATTR_RW(align);
1117+
10471118
static int dev_dax_target_node(struct dev_dax *dev_dax)
10481119
{
10491120
struct dax_region *dax_region = dev_dax->region;
@@ -1104,7 +1175,8 @@ static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
11041175
return 0;
11051176
if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
11061177
return 0;
1107-
if (a == &dev_attr_size.attr && is_static(dax_region))
1178+
if ((a == &dev_attr_align.attr ||
1179+
a == &dev_attr_size.attr) && is_static(dax_region))
11081180
return 0444;
11091181
return a->mode;
11101182
}
@@ -1113,6 +1185,7 @@ static struct attribute *dev_dax_attributes[] = {
11131185
&dev_attr_modalias.attr,
11141186
&dev_attr_size.attr,
11151187
&dev_attr_target_node.attr,
1188+
&dev_attr_align.attr,
11161189
&dev_attr_resource.attr,
11171190
&dev_attr_numa_node.attr,
11181191
NULL,

‎drivers/dax/dax-private.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,22 @@ static inline struct dax_mapping *to_dax_mapping(struct device *dev)
8787
}
8888

8989
phys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff, unsigned long size);
90+
91+
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
92+
static inline bool dax_align_valid(unsigned long align)
93+
{
94+
if (align == PUD_SIZE && IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
95+
return true;
96+
if (align == PMD_SIZE && has_transparent_hugepage())
97+
return true;
98+
if (align == PAGE_SIZE)
99+
return true;
100+
return false;
101+
}
102+
#else
103+
static inline bool dax_align_valid(unsigned long align)
104+
{
105+
return align == PAGE_SIZE;
106+
}
107+
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
90108
#endif

0 commit comments

Comments
 (0)