Skip to content

Commit fcffb6a

Browse files
djbwtorvalds
authored andcommitted
device-dax: add resize support
Make the device-dax 'size' attribute writable to allow capacity to be split between multiple instances in a region. The intended consumers of this capability are users that want to split a scarce memory resource between device-dax and System-RAM access, or users that want to have multiple security domains for a large region. By default the hmem instance provider allocates an entire region to the first instance. The process of creating a new instance (assuming a region-id of 0) is find the region and trigger the 'create' attribute which yields an empty instance to configure. For example: cd /sys/bus/dax/devices echo dax0.0 > dax0.0/driver/unbind echo $new_size > dax0.0/size echo 1 > $(readlink -f dax0.0)../dax_region/create seed=$(cat $(readlink -f dax0.0)../dax_region/seed) echo $new_size > $seed/size echo dax0.0 > ../drivers/{device_dax,kmem}/bind echo dax0.1 > ../drivers/{device_dax,kmem}/bind Instances can be destroyed by: echo $device > $(readlink -f $device)../dax_region/delete Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Vishal Verma <vishal.l.verma@intel.com> Cc: Brice Goglin <Brice.Goglin@inria.fr> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Dave Jiang <dave.jiang@intel.com> Cc: David Hildenbrand <david@redhat.com> Cc: Ira Weiny <ira.weiny@intel.com> Cc: Jia He <justin.he@arm.com> Cc: Joao Martins <joao.m.martins@oracle.com> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com> 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: Catalin Marinas <catalin.marinas@arm.com> Cc: Daniel Vetter <daniel@ffwll.ch> Cc: David Airlie <airlied@linux.ie> 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: 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: 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: 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/159643102625.4062302.7431838945566033852.stgit@dwillia2-desk3.amr.corp.intel.com Link: https://lkml.kernel.org/r/160106115239.30709.9850106928133493138.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c77f520 commit fcffb6a

File tree

1 file changed

+152
-9
lines changed

1 file changed

+152
-9
lines changed

drivers/dax/bus.c

+152-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/list.h>
77
#include <linux/slab.h>
88
#include <linux/dax.h>
9+
#include <linux/io.h>
910
#include "dax-private.h"
1011
#include "bus.h"
1112

@@ -562,7 +563,8 @@ struct dax_region *alloc_dax_region(struct device *parent, int region_id,
562563
}
563564
EXPORT_SYMBOL_GPL(alloc_dax_region);
564565

565-
static int alloc_dev_dax_range(struct dev_dax *dev_dax, resource_size_t size)
566+
static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
567+
resource_size_t size)
566568
{
567569
struct dax_region *dax_region = dev_dax->region;
568570
struct resource *res = &dax_region->res;
@@ -580,12 +582,7 @@ static int alloc_dev_dax_range(struct dev_dax *dev_dax, resource_size_t size)
580582
return 0;
581583
}
582584

583-
/* TODO: handle multiple allocations per region */
584-
if (res->child)
585-
return -ENOMEM;
586-
587-
alloc = __request_region(res, res->start, size, dev_name(dev), 0);
588-
585+
alloc = __request_region(res, start, size, dev_name(dev), 0);
589586
if (!alloc)
590587
return -ENOMEM;
591588

@@ -597,6 +594,29 @@ static int alloc_dev_dax_range(struct dev_dax *dev_dax, resource_size_t size)
597594
return 0;
598595
}
599596

597+
static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
598+
{
599+
struct dax_region *dax_region = dev_dax->region;
600+
struct range *range = &dev_dax->range;
601+
int rc = 0;
602+
603+
device_lock_assert(dax_region->dev);
604+
605+
if (size)
606+
rc = adjust_resource(res, range->start, size);
607+
else
608+
__release_region(&dax_region->res, range->start, range_len(range));
609+
if (rc)
610+
return rc;
611+
612+
dev_dax->range = (struct range) {
613+
.start = range->start,
614+
.end = range->start + size - 1,
615+
};
616+
617+
return 0;
618+
}
619+
600620
static ssize_t size_show(struct device *dev,
601621
struct device_attribute *attr, char *buf)
602622
{
@@ -605,7 +625,127 @@ static ssize_t size_show(struct device *dev,
605625

606626
return sprintf(buf, "%llu\n", size);
607627
}
608-
static DEVICE_ATTR_RO(size);
628+
629+
static bool alloc_is_aligned(struct dax_region *dax_region,
630+
resource_size_t size)
631+
{
632+
/*
633+
* The minimum mapping granularity for a device instance is a
634+
* single subsection, unless the arch says otherwise.
635+
*/
636+
return IS_ALIGNED(size, max_t(unsigned long, dax_region->align,
637+
memremap_compat_align()));
638+
}
639+
640+
static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
641+
{
642+
struct dax_region *dax_region = dev_dax->region;
643+
struct range *range = &dev_dax->range;
644+
struct resource *res, *adjust = NULL;
645+
struct device *dev = &dev_dax->dev;
646+
647+
for_each_dax_region_resource(dax_region, res)
648+
if (strcmp(res->name, dev_name(dev)) == 0
649+
&& res->start == range->start) {
650+
adjust = res;
651+
break;
652+
}
653+
654+
if (dev_WARN_ONCE(dev, !adjust, "failed to find matching resource\n"))
655+
return -ENXIO;
656+
return adjust_dev_dax_range(dev_dax, adjust, size);
657+
}
658+
659+
static ssize_t dev_dax_resize(struct dax_region *dax_region,
660+
struct dev_dax *dev_dax, resource_size_t size)
661+
{
662+
resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
663+
resource_size_t dev_size = range_len(&dev_dax->range);
664+
struct resource *region_res = &dax_region->res;
665+
struct device *dev = &dev_dax->dev;
666+
const char *name = dev_name(dev);
667+
struct resource *res, *first;
668+
669+
if (dev->driver)
670+
return -EBUSY;
671+
if (size == dev_size)
672+
return 0;
673+
if (size > dev_size && size - dev_size > avail)
674+
return -ENOSPC;
675+
if (size < dev_size)
676+
return dev_dax_shrink(dev_dax, size);
677+
678+
to_alloc = size - dev_size;
679+
if (dev_WARN_ONCE(dev, !alloc_is_aligned(dax_region, to_alloc),
680+
"resize of %pa misaligned\n", &to_alloc))
681+
return -ENXIO;
682+
683+
/*
684+
* Expand the device into the unused portion of the region. This
685+
* may involve adjusting the end of an existing resource, or
686+
* allocating a new resource.
687+
*/
688+
first = region_res->child;
689+
if (!first)
690+
return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
691+
for (res = first; to_alloc && res; res = res->sibling) {
692+
struct resource *next = res->sibling;
693+
resource_size_t free;
694+
695+
/* space at the beginning of the region */
696+
free = 0;
697+
if (res == first && res->start > dax_region->res.start)
698+
free = res->start - dax_region->res.start;
699+
if (free >= to_alloc && dev_size == 0)
700+
return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
701+
702+
free = 0;
703+
/* space between allocations */
704+
if (next && next->start > res->end + 1)
705+
free = next->start - res->end + 1;
706+
707+
/* space at the end of the region */
708+
if (free < to_alloc && !next && res->end < region_res->end)
709+
free = region_res->end - res->end;
710+
711+
if (free >= to_alloc && strcmp(name, res->name) == 0)
712+
return adjust_dev_dax_range(dev_dax, res, resource_size(res) + to_alloc);
713+
else if (free >= to_alloc && dev_size == 0)
714+
return alloc_dev_dax_range(dev_dax, res->end + 1, to_alloc);
715+
}
716+
return -ENOSPC;
717+
}
718+
719+
static ssize_t size_store(struct device *dev, struct device_attribute *attr,
720+
const char *buf, size_t len)
721+
{
722+
ssize_t rc;
723+
unsigned long long val;
724+
struct dev_dax *dev_dax = to_dev_dax(dev);
725+
struct dax_region *dax_region = dev_dax->region;
726+
727+
rc = kstrtoull(buf, 0, &val);
728+
if (rc)
729+
return rc;
730+
731+
if (!alloc_is_aligned(dax_region, val)) {
732+
dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
733+
return -EINVAL;
734+
}
735+
736+
device_lock(dax_region->dev);
737+
if (!dax_region->dev->driver) {
738+
device_unlock(dax_region->dev);
739+
return -ENXIO;
740+
}
741+
device_lock(dev);
742+
rc = dev_dax_resize(dax_region, dev_dax, val);
743+
device_unlock(dev);
744+
device_unlock(dax_region->dev);
745+
746+
return rc == 0 ? len : rc;
747+
}
748+
static DEVICE_ATTR_RW(size);
609749

610750
static int dev_dax_target_node(struct dev_dax *dev_dax)
611751
{
@@ -654,11 +794,14 @@ static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
654794
{
655795
struct device *dev = container_of(kobj, struct device, kobj);
656796
struct dev_dax *dev_dax = to_dev_dax(dev);
797+
struct dax_region *dax_region = dev_dax->region;
657798

658799
if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
659800
return 0;
660801
if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
661802
return 0;
803+
if (a == &dev_attr_size.attr && is_static(dax_region))
804+
return 0444;
662805
return a->mode;
663806
}
664807

@@ -739,7 +882,7 @@ struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
739882
device_initialize(dev);
740883
dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
741884

742-
rc = alloc_dev_dax_range(dev_dax, data->size);
885+
rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
743886
if (rc)
744887
goto err_range;
745888

0 commit comments

Comments
 (0)