A DC dax device's size is determined by the extents that back it, not by
the user.  DCD extents are all-or-nothing, so partial shrink is just as
illegal as growing.  Enforce that on the size and creation paths:

  * size_store: any non-zero resize on a DC region returns -EOPNOTSUPP.
    The sole exception is size=0, which daxctl destroy-device writes to
    return every claimed extent to the region's available pool before
    the device's name is written to the region's 'delete' attribute.
  * __devm_create_dev_dax: a DC dax device must be created at size 0.
    Non-zero data->size on a DC region returns -EINVAL with a clear
    message.

The resize machinery (dev_dax_shrink, adjust_ok, dev_dax_resize_static,
dev_dax_resize) learns to walk the right parent — dax_region->res for
static regions, the dax_resource->res for DC regions claimed via
uuid_store — so shrink-to-0 correctly releases each extent's child
resource rather than the region's.

Based on an original patch by Navneet Singh.

Signed-off-by: Ira Weiny <[email protected]>
Signed-off-by: Anisa Su <[email protected]>

---
Changes:
[anisa: split out from the original "Surface dc_extents" commit;
 DC-aware resize policy only.]
---
 drivers/dax/bus.c | 46 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 11 deletions(-)

diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
index 1d6f82920be6..c030eb103ad0 100644
--- a/drivers/dax/bus.c
+++ b/drivers/dax/bus.c
@@ -1136,7 +1136,8 @@ static int dev_dax_shrink(struct dev_dax *dev_dax, 
resource_size_t size)
        int i;
 
        for (i = dev_dax->nr_range - 1; i >= 0; i--) {
-               struct range *range = &dev_dax->ranges[i].range;
+               struct dev_dax_range *dev_range = &dev_dax->ranges[i];
+               struct range *range = &dev_range->range;
                struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
                struct resource *adjust = NULL, *res;
                resource_size_t shrink;
@@ -1152,6 +1153,10 @@ static int dev_dax_shrink(struct dev_dax *dev_dax, 
resource_size_t size)
                        continue;
                }
 
+               /*
+                * Partial shrink: forbidden on DC regions, so dev_range
+                * here must belong to a static device.
+                */
                for_each_dax_region_resource(dax_region, res)
                        if (strcmp(res->name, dev_name(dev)) == 0
                                        && res->start == range->start) {
@@ -1195,19 +1200,21 @@ static bool adjust_ok(struct dev_dax *dev_dax, struct 
resource *res)
 }
 
 /**
- * dev_dax_resize_static - Expand the device into the unused portion of the
- * region. This may involve adjusting the end of an existing resource, or
- * allocating a new resource.
+ * __dev_dax_resize - Expand the device into the unused portion of the region.
+ * This may involve adjusting the end of an existing resource, or allocating a
+ * new resource.
  *
  * @parent: parent resource to allocate this range in
  * @dev_dax: DAX device to be expanded
  * @to_alloc: amount of space to alloc; must be <= space available in @parent
+ * @dax_resource: if dc; the parent resource
  *
  * Return the amount of space allocated or -ERRNO on failure
  */
-static ssize_t dev_dax_resize_static(struct resource *parent,
-                                    struct dev_dax *dev_dax,
-                                    resource_size_t to_alloc)
+static ssize_t __dev_dax_resize(struct resource *parent,
+                               struct dev_dax *dev_dax,
+                               resource_size_t to_alloc,
+                               struct dax_resource *dax_resource)
 {
        struct resource *res, *first;
        int rc;
@@ -1215,7 +1222,8 @@ static ssize_t dev_dax_resize_static(struct resource 
*parent,
        first = parent->child;
        if (!first) {
                rc = alloc_dev_dax_range(parent, dev_dax,
-                                          parent->start, to_alloc, NULL);
+                                          parent->start, to_alloc,
+                                          dax_resource);
                if (rc)
                        return rc;
                return to_alloc;
@@ -1229,7 +1237,8 @@ static ssize_t dev_dax_resize_static(struct resource 
*parent,
                if (res == first && res->start > parent->start) {
                        alloc = min(res->start - parent->start, to_alloc);
                        rc = alloc_dev_dax_range(parent, dev_dax,
-                                                parent->start, alloc, NULL);
+                                                parent->start, alloc,
+                                                dax_resource);
                        if (rc)
                                return rc;
                        return alloc;
@@ -1253,7 +1262,8 @@ static ssize_t dev_dax_resize_static(struct resource 
*parent,
                                return rc;
                        return alloc;
                }
-               rc = alloc_dev_dax_range(parent, dev_dax, res->end + 1, alloc, 
NULL);
+               rc = alloc_dev_dax_range(parent, dev_dax, res->end + 1, alloc,
+                                        dax_resource);
                if (rc)
                        return rc;
                return alloc;
@@ -1264,6 +1274,13 @@ static ssize_t dev_dax_resize_static(struct resource 
*parent,
        return 0;
 }
 
+static ssize_t dev_dax_resize_static(struct dax_region *dax_region,
+                                    struct dev_dax *dev_dax,
+                                    resource_size_t to_alloc)
+{
+       return __dev_dax_resize(&dax_region->res, dev_dax, to_alloc, NULL);
+}
+
 static ssize_t dev_dax_resize(struct dax_region *dax_region,
                struct dev_dax *dev_dax, resource_size_t size)
 {
@@ -1277,6 +1294,8 @@ static ssize_t dev_dax_resize(struct dax_region 
*dax_region,
                return -EBUSY;
        if (size == dev_size)
                return 0;
+       if (size != 0 && is_dynamic(dax_region))
+               return -EOPNOTSUPP;
        if (size > dev_size && size - dev_size > avail)
                return -ENOSPC;
        if (size < dev_size)
@@ -1288,7 +1307,7 @@ static ssize_t dev_dax_resize(struct dax_region 
*dax_region,
                return -ENXIO;
 
 retry:
-       alloc = dev_dax_resize_static(&dax_region->res, dev_dax, to_alloc);
+       alloc = dev_dax_resize_static(dax_region, dev_dax, to_alloc);
        if (alloc <= 0)
                return alloc;
        to_alloc -= alloc;
@@ -1674,6 +1693,11 @@ static struct dev_dax *__devm_create_dev_dax(struct 
dev_dax_data *data)
        struct device *dev;
        int rc;
 
+       if (is_dynamic(dax_region) && data->size) {
+               dev_err(parent, "DC DAX region devices must be created 
initially with 0 size");
+               return ERR_PTR(-EINVAL);
+       }
+
        dev_dax = kzalloc_obj(*dev_dax);
        if (!dev_dax)
                return ERR_PTR(-ENOMEM);
-- 
2.43.0


Reply via email to