Message ID | 20250603221949.53272-7-Smita.KoralahalliChannabasappa@amd.com |
---|---|
State | New |
Headers | show |
Series | Add managed SOFT RESERVE resource handling | expand |
On 6/3/25 3:19 PM, Smita Koralahalli wrote: > From: Nathan Fontenot <nathan.fontenot@amd.com> > > To enable registration of HMEM devices for SOFT RESERVED regions after > the DAX HMEM device is initialized, this patch saves a reference to the > DAX HMEM platform device. > > This saved pointer will be used in a follow-up patch to allow late > registration of SOFT RESERVED memory ranges. It also enables > simplification of the walk_hmem_resources() by removing the need to > pass a struct device argument. > > There are no functional changes. > > Co-developed-by: Nathan Fontenot <Nathan.Fontenot@amd.com> > Signed-off-by: Nathan Fontenot <Nathan.Fontenot@amd.com> > Co-developed-by: Terry Bowman <terry.bowman@amd.com> > Signed-off-by: Terry Bowman <terry.bowman@amd.com> > Signed-off-by: Smita Koralahalli <Smita.KoralahalliChannabasappa@amd.com> > --- > drivers/dax/hmem/device.c | 4 ++-- > drivers/dax/hmem/hmem.c | 9 ++++++--- > include/linux/dax.h | 5 ++--- > 3 files changed, 10 insertions(+), 8 deletions(-) > > diff --git a/drivers/dax/hmem/device.c b/drivers/dax/hmem/device.c > index f9e1a76a04a9..59ad44761191 100644 > --- a/drivers/dax/hmem/device.c > +++ b/drivers/dax/hmem/device.c > @@ -17,14 +17,14 @@ static struct resource hmem_active = { > .flags = IORESOURCE_MEM, > }; > > -int walk_hmem_resources(struct device *host, walk_hmem_fn fn) > +int walk_hmem_resources(walk_hmem_fn fn) > { > struct resource *res; > int rc = 0; > > mutex_lock(&hmem_resource_lock); > for (res = hmem_active.child; res; res = res->sibling) { > - rc = fn(host, (int) res->desc, res); > + rc = fn((int) res->desc, res); > if (rc) > break; > } > diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c > index 5e7c53f18491..3aedef5f1be1 100644 > --- a/drivers/dax/hmem/hmem.c > +++ b/drivers/dax/hmem/hmem.c > @@ -9,6 +9,8 @@ > static bool region_idle; > module_param_named(region_idle, region_idle, bool, 0644); > > +static struct platform_device *dax_hmem_pdev; > + > static int dax_hmem_probe(struct platform_device *pdev) > { > unsigned long flags = IORESOURCE_DAX_KMEM; > @@ -59,9 +61,9 @@ static void release_hmem(void *pdev) > platform_device_unregister(pdev); > } > > -static int hmem_register_device(struct device *host, int target_nid, > - const struct resource *res) > +static int hmem_register_device(int target_nid, const struct resource *res) > { > + struct device *host = &dax_hmem_pdev->dev; > struct platform_device *pdev; > struct memregion_info info; > long id; > @@ -125,7 +127,8 @@ static int hmem_register_device(struct device *host, int target_nid, > > static int dax_hmem_platform_probe(struct platform_device *pdev) > { > - return walk_hmem_resources(&pdev->dev, hmem_register_device); > + dax_hmem_pdev = pdev; Is there never more than 1 DAX HMEM platform device that can show up? The global pointer makes me nervous. DJ > + return walk_hmem_resources(hmem_register_device); > } > > static struct platform_driver dax_hmem_platform_driver = { > diff --git a/include/linux/dax.h b/include/linux/dax.h > index dcc9fcdf14e4..a4ad3708ea35 100644 > --- a/include/linux/dax.h > +++ b/include/linux/dax.h > @@ -305,7 +305,6 @@ static inline void hmem_register_resource(int target_nid, struct resource *r) > } > #endif > > -typedef int (*walk_hmem_fn)(struct device *dev, int target_nid, > - const struct resource *res); > -int walk_hmem_resources(struct device *dev, walk_hmem_fn fn); > +typedef int (*walk_hmem_fn)(int target_nid, const struct resource *res); > +int walk_hmem_resources(walk_hmem_fn fn); > #endif
On 06/06/2025 00:54, Dave Jiang wrote: >> -static int hmem_register_device(struct device *host, int target_nid, >> - const struct resource *res) >> +static int hmem_register_device(int target_nid, const struct resource *res) >> { >> + struct device *host = &dax_hmem_pdev->dev; >> struct platform_device *pdev; >> struct memregion_info info; >> long id; >> @@ -125,7 +127,8 @@ static int hmem_register_device(struct device *host, int target_nid, >> >> static int dax_hmem_platform_probe(struct platform_device *pdev) >> { >> - return walk_hmem_resources(&pdev->dev, hmem_register_device); >> + dax_hmem_pdev = pdev; > Is there never more than 1 DAX HMEM platform device that can show up? The global pointer makes me nervous. IIUC, Referring to the device creation logic in `__hmem_register_resource()` (shown below), only one `hmem_platform` instance can ever be registered. This ensures the change is safe. However, I agree that using a global pointer in a function that may be called multiple times does raise valid concerns. To strengthen this, how about: 1. Add a comment clarifying single-instance enforcement 2. Add a warn_on/bug_on for it: `WARN_ON(dax_hmem_pdev && dax_hmem_pdev != pdev)` static void __hmem_register_resource(int target_nid, struct resource *res) { struct platform_device *pdev; struct resource *new; int rc; new = __request_region(&hmem_active, res->start, resource_size(res), "", 0); if (!new) { pr_debug("hmem range %pr already active\n", res); return; } new->desc = target_nid; if (platform_initialized) return; pdev = platform_device_alloc("hmem_platform", 0); if (!pdev) { pr_err_once("failed to register device-dax hmem_platform device\n"); return; } rc = platform_device_add(pdev); if (rc) platform_device_put(pdev); else platform_initialized = true; } Thanks Zhijian > > DJ
On 6/6/2025 3:11 AM, Zhijian Li (Fujitsu) wrote: > > > On 06/06/2025 00:54, Dave Jiang wrote: >>> -static int hmem_register_device(struct device *host, int target_nid, >>> - const struct resource *res) >>> +static int hmem_register_device(int target_nid, const struct resource *res) >>> { >>> + struct device *host = &dax_hmem_pdev->dev; >>> struct platform_device *pdev; >>> struct memregion_info info; >>> long id; >>> @@ -125,7 +127,8 @@ static int hmem_register_device(struct device *host, int target_nid, >>> >>> static int dax_hmem_platform_probe(struct platform_device *pdev) >>> { >>> - return walk_hmem_resources(&pdev->dev, hmem_register_device); >>> + dax_hmem_pdev = pdev; > >> Is there never more than 1 DAX HMEM platform device that can show up? The global pointer makes me nervous. > > IIUC, > > Referring to the device creation logic in `__hmem_register_resource()` (shown below), > only one `hmem_platform` instance can ever be registered. This ensures the change is safe. > > > However, I agree that using a global pointer in a function that may be called multiple times > does raise valid concerns. Note: The creation of the platform device is moved in patch 7/7. I think the placed it was moved to may not be correct. The creation of the platform device should be in hmem_init so that it is always create one instance. -Nathan > > To strengthen this, how about: > 1. Add a comment clarifying single-instance enforcement > 2. Add a warn_on/bug_on for it: `WARN_ON(dax_hmem_pdev && dax_hmem_pdev != pdev)` > > > static void __hmem_register_resource(int target_nid, struct resource *res) > { > struct platform_device *pdev; > struct resource *new; > int rc; > > new = __request_region(&hmem_active, res->start, resource_size(res), "", > 0); > if (!new) { > pr_debug("hmem range %pr already active\n", res); > return; > } > > new->desc = target_nid; > > if (platform_initialized) > return; > > pdev = platform_device_alloc("hmem_platform", 0); > if (!pdev) { > pr_err_once("failed to register device-dax hmem_platform device\n"); > return; > } > > rc = platform_device_add(pdev); > if (rc) > platform_device_put(pdev); > else > platform_initialized = true; > } > > Thanks > Zhijian > > > > > >> >> DJ
diff --git a/drivers/dax/hmem/device.c b/drivers/dax/hmem/device.c index f9e1a76a04a9..59ad44761191 100644 --- a/drivers/dax/hmem/device.c +++ b/drivers/dax/hmem/device.c @@ -17,14 +17,14 @@ static struct resource hmem_active = { .flags = IORESOURCE_MEM, }; -int walk_hmem_resources(struct device *host, walk_hmem_fn fn) +int walk_hmem_resources(walk_hmem_fn fn) { struct resource *res; int rc = 0; mutex_lock(&hmem_resource_lock); for (res = hmem_active.child; res; res = res->sibling) { - rc = fn(host, (int) res->desc, res); + rc = fn((int) res->desc, res); if (rc) break; } diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c index 5e7c53f18491..3aedef5f1be1 100644 --- a/drivers/dax/hmem/hmem.c +++ b/drivers/dax/hmem/hmem.c @@ -9,6 +9,8 @@ static bool region_idle; module_param_named(region_idle, region_idle, bool, 0644); +static struct platform_device *dax_hmem_pdev; + static int dax_hmem_probe(struct platform_device *pdev) { unsigned long flags = IORESOURCE_DAX_KMEM; @@ -59,9 +61,9 @@ static void release_hmem(void *pdev) platform_device_unregister(pdev); } -static int hmem_register_device(struct device *host, int target_nid, - const struct resource *res) +static int hmem_register_device(int target_nid, const struct resource *res) { + struct device *host = &dax_hmem_pdev->dev; struct platform_device *pdev; struct memregion_info info; long id; @@ -125,7 +127,8 @@ static int hmem_register_device(struct device *host, int target_nid, static int dax_hmem_platform_probe(struct platform_device *pdev) { - return walk_hmem_resources(&pdev->dev, hmem_register_device); + dax_hmem_pdev = pdev; + return walk_hmem_resources(hmem_register_device); } static struct platform_driver dax_hmem_platform_driver = { diff --git a/include/linux/dax.h b/include/linux/dax.h index dcc9fcdf14e4..a4ad3708ea35 100644 --- a/include/linux/dax.h +++ b/include/linux/dax.h @@ -305,7 +305,6 @@ static inline void hmem_register_resource(int target_nid, struct resource *r) } #endif -typedef int (*walk_hmem_fn)(struct device *dev, int target_nid, - const struct resource *res); -int walk_hmem_resources(struct device *dev, walk_hmem_fn fn); +typedef int (*walk_hmem_fn)(int target_nid, const struct resource *res); +int walk_hmem_resources(walk_hmem_fn fn); #endif