Skip to content

Commit 8f401ec

Browse files
committed
cxl/region: Recycle region ids
At region creation time the next region-id is atomically cached so that there is predictability of region device names. If that region is destroyed and then a new one is created the region id increments. That ends up looking like a memory leak, or is otherwise surprising that identifiers roll forward even after destroying all previously created regions. Try to reuse rather than free old region ids at region release time. While this fixes a cosmetic issue, the needlessly advancing memory region-id gives the appearance of a memory leak, hence the "Fixes" tag, but no "Cc: stable" tag. Cc: Ben Widawsky <[email protected]> Cc: Jonathan Cameron <[email protected]> Fixes: 779dd20 ("cxl/region: Add region creation support") Reviewed-by: Dave Jiang <[email protected]> Reviewed-by: Vishal Verma <[email protected]> Link: https://lore.kernel.org/r/166752186062.947915.13200195701224993317.stgit@dwillia2-xfh.jf.intel.com Signed-off-by: Dan Williams <[email protected]>
1 parent e4f6dfa commit 8f401ec

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

drivers/cxl/core/region.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,9 +1534,24 @@ static const struct attribute_group *region_groups[] = {
15341534

15351535
static void cxl_region_release(struct device *dev)
15361536
{
1537+
struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
15371538
struct cxl_region *cxlr = to_cxl_region(dev);
1539+
int id = atomic_read(&cxlrd->region_id);
1540+
1541+
/*
1542+
* Try to reuse the recently idled id rather than the cached
1543+
* next id to prevent the region id space from increasing
1544+
* unnecessarily.
1545+
*/
1546+
if (cxlr->id < id)
1547+
if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
1548+
memregion_free(id);
1549+
goto out;
1550+
}
15381551

15391552
memregion_free(cxlr->id);
1553+
out:
1554+
put_device(dev->parent);
15401555
kfree(cxlr);
15411556
}
15421557

@@ -1598,6 +1613,11 @@ static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int i
15981613
device_initialize(dev);
15991614
lockdep_set_class(&dev->mutex, &cxl_region_key);
16001615
dev->parent = &cxlrd->cxlsd.cxld.dev;
1616+
/*
1617+
* Keep root decoder pinned through cxl_region_release to fixup
1618+
* region id allocations
1619+
*/
1620+
get_device(dev->parent);
16011621
device_set_pm_not_required(dev);
16021622
dev->bus = &cxl_bus_type;
16031623
dev->type = &cxl_region_type;

0 commit comments

Comments
 (0)