Skip to content

Commit a1e16b6

Browse files
committed
dax/bus: Factor out dev dax resize logic
Dynamic Capacity regions must limit dev dax resources to those areas which have extents backing real memory. Such DAX regions are dubbed 'sparse' regions. In order to manage where memory is available four alternatives were considered: 1) Create a single region resource child on region creation which reserves the entire region. Then as extents are added punch holes in this reservation. This requires new resource manipulation to punch the holes and still requires an additional iteration over the extent areas which may already have existing dev dax resources used. 2) Maintain an ordered xarray of extents which can be queried while processing the resize logic. The issue is that existing region->res children may artificially limit the allocation size sent to alloc_dev_dax_range(). IE the resource children can't be directly used in the resize logic to find where space in the region is. This also poses a problem of managing the available size in 2 places. 3) Maintain a separate resource tree with extents. This option is the same as 2) but with the different data structure. Most ideally there should be a unified representation of the resource tree not two places to look for space. 4) Create region resource children for each extent. Manage the dax dev resize logic in the same way as before but use a region child (extent) resource as the parents to find space within each extent. Option 4 can leverage the existing resize algorithm to find space within the extents. It manages the available space in a singular resource tree which is less complicated for finding space. In preparation for this change, factor out the dev_dax_resize logic. For static regions use dax_region->res as the parent to find space for the dax ranges. Future patches will use the same algorithm with individual extent resources as the parent. Reviewed-by: Jonathan Cameron <[email protected]> Reviewed-by: Dave Jiang <[email protected]> Signed-off-by: Ira Weiny <[email protected]>
1 parent 9e53046 commit a1e16b6

File tree

1 file changed

+80
-50
lines changed

1 file changed

+80
-50
lines changed

drivers/dax/bus.c

Lines changed: 80 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -844,11 +844,9 @@ static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
844844
return 0;
845845
}
846846

847-
static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
848-
resource_size_t size)
847+
static int alloc_dev_dax_range(struct resource *parent, struct dev_dax *dev_dax,
848+
u64 start, resource_size_t size)
849849
{
850-
struct dax_region *dax_region = dev_dax->region;
851-
struct resource *res = &dax_region->res;
852850
struct device *dev = &dev_dax->dev;
853851
struct dev_dax_range *ranges;
854852
unsigned long pgoff = 0;
@@ -866,14 +864,14 @@ static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
866864
return 0;
867865
}
868866

869-
alloc = __request_region(res, start, size, dev_name(dev), 0);
867+
alloc = __request_region(parent, start, size, dev_name(dev), 0);
870868
if (!alloc)
871869
return -ENOMEM;
872870

873871
ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
874872
* (dev_dax->nr_range + 1), GFP_KERNEL);
875873
if (!ranges) {
876-
__release_region(res, alloc->start, resource_size(alloc));
874+
__release_region(parent, alloc->start, resource_size(alloc));
877875
return -ENOMEM;
878876
}
879877

@@ -1026,50 +1024,45 @@ static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
10261024
return true;
10271025
}
10281026

1029-
static ssize_t dev_dax_resize(struct dax_region *dax_region,
1030-
struct dev_dax *dev_dax, resource_size_t size)
1027+
/**
1028+
* dev_dax_resize_static - Expand the device into the unused portion of the
1029+
* region. This may involve adjusting the end of an existing resource, or
1030+
* allocating a new resource.
1031+
*
1032+
* @parent: parent resource to allocate this range in
1033+
* @dev_dax: DAX device to be expanded
1034+
* @to_alloc: amount of space to alloc; must be <= space available in @parent
1035+
*
1036+
* Return the amount of space allocated or -ERRNO on failure
1037+
*/
1038+
static ssize_t dev_dax_resize_static(struct resource *parent,
1039+
struct dev_dax *dev_dax,
1040+
resource_size_t to_alloc)
10311041
{
1032-
resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
1033-
resource_size_t dev_size = dev_dax_size(dev_dax);
1034-
struct resource *region_res = &dax_region->res;
1035-
struct device *dev = &dev_dax->dev;
10361042
struct resource *res, *first;
1037-
resource_size_t alloc = 0;
10381043
int rc;
10391044

1040-
if (dev->driver)
1041-
return -EBUSY;
1042-
if (size == dev_size)
1043-
return 0;
1044-
if (size > dev_size && size - dev_size > avail)
1045-
return -ENOSPC;
1046-
if (size < dev_size)
1047-
return dev_dax_shrink(dev_dax, size);
1048-
1049-
to_alloc = size - dev_size;
1050-
if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
1051-
"resize of %pa misaligned\n", &to_alloc))
1052-
return -ENXIO;
1053-
1054-
/*
1055-
* Expand the device into the unused portion of the region. This
1056-
* may involve adjusting the end of an existing resource, or
1057-
* allocating a new resource.
1058-
*/
1059-
retry:
1060-
first = region_res->child;
1061-
if (!first)
1062-
return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
1045+
first = parent->child;
1046+
if (!first) {
1047+
rc = alloc_dev_dax_range(parent, dev_dax,
1048+
parent->start, to_alloc);
1049+
if (rc)
1050+
return rc;
1051+
return to_alloc;
1052+
}
10631053

1064-
rc = -ENOSPC;
10651054
for (res = first; res; res = res->sibling) {
10661055
struct resource *next = res->sibling;
1056+
resource_size_t alloc;
10671057

10681058
/* space at the beginning of the region */
1069-
if (res == first && res->start > dax_region->res.start) {
1070-
alloc = min(res->start - dax_region->res.start, to_alloc);
1071-
rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
1072-
break;
1059+
if (res == first && res->start > parent->start) {
1060+
alloc = min(res->start - parent->start, to_alloc);
1061+
rc = alloc_dev_dax_range(parent, dev_dax,
1062+
parent->start, alloc);
1063+
if (rc)
1064+
return rc;
1065+
return alloc;
10731066
}
10741067

10751068
alloc = 0;
@@ -1078,21 +1071,56 @@ static ssize_t dev_dax_resize(struct dax_region *dax_region,
10781071
alloc = min(next->start - (res->end + 1), to_alloc);
10791072

10801073
/* space at the end of the region */
1081-
if (!alloc && !next && res->end < region_res->end)
1082-
alloc = min(region_res->end - res->end, to_alloc);
1074+
if (!alloc && !next && res->end < parent->end)
1075+
alloc = min(parent->end - res->end, to_alloc);
10831076

10841077
if (!alloc)
10851078
continue;
10861079

10871080
if (adjust_ok(dev_dax, res)) {
10881081
rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
1089-
break;
1082+
if (rc)
1083+
return rc;
1084+
return alloc;
10901085
}
1091-
rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
1092-
break;
1086+
rc = alloc_dev_dax_range(parent, dev_dax, res->end + 1, alloc);
1087+
if (rc)
1088+
return rc;
1089+
return alloc;
10931090
}
1094-
if (rc)
1095-
return rc;
1091+
1092+
/* available was already calculated and should never be an issue */
1093+
dev_WARN_ONCE(&dev_dax->dev, 1, "space not found?");
1094+
return 0;
1095+
}
1096+
1097+
static ssize_t dev_dax_resize(struct dax_region *dax_region,
1098+
struct dev_dax *dev_dax, resource_size_t size)
1099+
{
1100+
resource_size_t avail = dax_region_avail_size(dax_region);
1101+
resource_size_t dev_size = dev_dax_size(dev_dax);
1102+
struct device *dev = &dev_dax->dev;
1103+
resource_size_t to_alloc;
1104+
resource_size_t alloc;
1105+
1106+
if (dev->driver)
1107+
return -EBUSY;
1108+
if (size == dev_size)
1109+
return 0;
1110+
if (size > dev_size && size - dev_size > avail)
1111+
return -ENOSPC;
1112+
if (size < dev_size)
1113+
return dev_dax_shrink(dev_dax, size);
1114+
1115+
to_alloc = size - dev_size;
1116+
if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
1117+
"resize of %pa misaligned\n", &to_alloc))
1118+
return -ENXIO;
1119+
1120+
retry:
1121+
alloc = dev_dax_resize_static(&dax_region->res, dev_dax, to_alloc);
1122+
if (alloc <= 0)
1123+
return alloc;
10961124
to_alloc -= alloc;
10971125
if (to_alloc)
10981126
goto retry;
@@ -1198,7 +1226,8 @@ static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
11981226

11991227
to_alloc = range_len(&r);
12001228
if (alloc_is_aligned(dev_dax, to_alloc))
1201-
rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc);
1229+
rc = alloc_dev_dax_range(&dax_region->res, dev_dax, r.start,
1230+
to_alloc);
12021231
up_write(&dax_dev_rwsem);
12031232
up_write(&dax_region_rwsem);
12041233

@@ -1466,7 +1495,8 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
14661495
device_initialize(dev);
14671496
dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
14681497

1469-
rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
1498+
rc = alloc_dev_dax_range(&dax_region->res, dev_dax, dax_region->res.start,
1499+
data->size);
14701500
if (rc)
14711501
goto err_range;
14721502

0 commit comments

Comments
 (0)