dax/kmem: atomic whole-device hotplug via sysfs
Subsystems (7)
v7 cover dax/kmem: atomic whole-device hotplug via sysfs
The dax kmem driver onlines memory during probe using the system default policy, with no atomic control for the state of an entire region at runtime - only by toggling individual memory blocks.
Offlining and removing a whole region therefore races with other userland controllers that interfere between the two steps.
This series adds a sysfs "state" attribute for atomic whole-device hotplug control, plus the mm and dax plumbing to support it.
Transitions are atomic across every range of the device. The state names mirror the per-block memoryX/state ABI with one modification:
- "unplugged": memory blocks are not present - "online": online as system RAM, zone chosen by the kernel - "online_kernel": online in ZONE_NORMAL
- "online_movable": online in ZONE_MOVABLE
"offline" (blocks present but offline) is reportable for backward compatibility but is not writable because it entices the race condition we are trying to solve (separate atomic steps for offline and unplug).
'unplugged' (atomic offline+remove of the whole device) is the new capability provided by the new kmem sysfs attribute.
dax/kmem probe still creates the memory blocks by default when the default policy is "offline", to preserve backwards compatibility.
mm preparation:
1. mm/memory: add memory_block_aligned_range() helper.
2. mm/memory_hotplug: add mhp_online_type_to_str() and export the online-type string helpers.
3. mm/memory_hotplug: pass online_type to online_memory_block().
4. mm/memory_hotplug: export mhp_get_default_online_type().
5. mm/memory_hotplug: add __add_memory_driver_managed() so a driver can select the online policy. The override is restricted to in-tree modules via EXPORT_SYMBOL_FOR_MODULES().
6. mm/memory_hotplug: add offline_and_remove_memory_ranges() for atomic, all-or-nothing offline+remove of several ranges under a single lock_device_hotplug().
dax/kmem:
7. Resolve mhp default online type at probe time
8. Extract hotplug/hotremove into helper functions (refactor).
9. Add the "state" sysfs attribute.
10. selftests/dax: regression test for the attribute.
---
v7 changes
- picked up tags
- dropped !data checks (Dan)
- re-defined 'DAX_KMEM_UNPLUGGED (-1)' (Dan)
- converted state to static ATTRIBUTE_GROUPS (Dan)
- dropped DEFAULT until later commit with per-driver settings (Dan)
- state_store now frees reservations on total hotplug failure (sashiko)
- kmalloc_array -> kmalloc_objs (Dan)
- unbind reworked to just remove_memory() (still never offlines, no deadlock)
- added memoryN/state and daxX.Y/state desync test
- gated destructive unbind tests behind DAX_KMEM_TEST_UNBIND
- added DAX_KMEM_TEST_DEV environment variable for target dax device
- added comment in offline_and_remove_memory_ranges for clarity
- minor spellcheck, whitespace, doc fixups
Gregory Price (10):
mm/memory: add memory_block_aligned_range() helper mm/memory_hotplug: add mhp_online_type_to_str() and export string
helpers
mm/memory_hotplug: pass online_type to online_memory_block() via arg mm/memory_hotplug: export mhp_get_default_online_type mm/memory_hotplug: add __add_memory_driver_managed() with online_type
arg
mm/memory_hotplug: add offline_and_remove_memory_ranges() dax/kmem: resolve default online type at probe time dax/kmem: extract hotplug/hotremove helper functions dax/kmem: add sysfs interface for atomic whole-device hotplug selftests/dax: add dax/kmem hotplug sysfs regression test
Documentation/ABI/testing/sysfs-bus-dax | 24 + drivers/base/memory.c | 9 + drivers/dax/bus.h | 2 + drivers/dax/kmem.c | 500 ++++++++++++++---- include/linux/memory.h | 27 + include/linux/memory_hotplug.h | 14 + mm/memory_hotplug.c | 162 ++++-- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/dax/Makefile | 6 + tools/testing/selftests/dax/config | 4 + .../testing/selftests/dax/dax-kmem-hotplug.sh | 317 +++++++++++ tools/testing/selftests/dax/settings | 1 + 12 files changed, 928 insertions(+), 139 deletions(-) create mode 100644 tools/testing/selftests/dax/Makefile create mode 100644 tools/testing/selftests/dax/config create mode 100755 tools/testing/selftests/dax/dax-kmem-hotplug.sh create mode 100644 tools/testing/selftests/dax/settings
Signature · 1 line
2.53.0-Meta
PATCH v7 1/10 mm/memory: add memory_block_aligned_range() helper
Memory hotplug operations require ranges aligned to memory block boundaries. This is a generic operation for hotplug.
Add memory_block_aligned_range() as a common helper in <linux/memory.h> that aligns the start address up and end address down to memory block boundaries. Guard against end underflow when the range falls below the first memory block boundary, returning an empty range instead.
Update dax/kmem to use this helper.
--- drivers/dax/kmem.c | 4 +--- include/linux/memory.h | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-)
drivers/dax/kmem.c +1 -3
@@ -33,9 +33,7 @@ static int dax_kmem_range(struct dev_dax *dev_dax, int i, struct range *r)struct dev_dax_range *dax_range = &dev_dax->ranges[i];struct range *range = &dax_range->range; -/* memory-block align the hotplug range */ - r->start =ALIGN (range->start,memory_block_size_bytes ()); - r->end =ALIGN_DOWN (range->end +1 ,memory_block_size_bytes ()) -1 ; + *r =memory_block_aligned_range (range);if (r->start >= r->end) { r->start = range->start; r->end = range->end;
include/linux/memory.h +27 -0
@@ -20,6 +20,7 @@#include <linux/compiler.h> #include <linux/mutex.h> #include <linux/memory_hotplug.h> +#include <linux/range.h> #define MIN_MEMORY_BLOCK_SIZE (1UL << SECTION_SIZE_BITS)
@@ -100,6 +101,32 @@ int arch_get_memory_phys_device(unsigned long start_pfn);unsigned long memory_block_size_bytes (void );int set_memory_block_size_order (unsigned int order); +/** +* memory_block_aligned_range - align a physical address range to memory blocks +* @range: the input range to align +* +* Aligns the start address up and the end address down to memory block +* boundaries. This is required for memory hotplug operations which must +* operate on memory-block aligned ranges. +* +* Returns the aligned range. Callers should check that the returned +* range is valid (aligned.start < aligned.end) before using it. +*/ +static inline struct range memory_block_aligned_range (const struct range *range) +{ +struct range aligned; + + aligned.start =ALIGN (range->start,memory_block_size_bytes ()); + aligned.end =ALIGN_DOWN (range->end +1 ,memory_block_size_bytes ()); +/* No whole block fits (e.g. range below the first boundary): empty. */ +if (aligned.end <= aligned.start) + aligned.start = aligned.end; +else + aligned.end -=1 ; + +return aligned; +} +struct memory_notify {unsigned long start_pfn;unsigned long nr_pages;
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v7 01/10] mm/memory: add memory_block_aligned_range() helper
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Integer overflow in `memory_block_aligned_range` when `range->end == U64_MAX` incorrectly…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Integer overflow in memory_block_aligned_range when range->end == U64_MAX incorrectly truncates valid memory ranges. --
--- Patch [1]: [PATCH v7 01/10] mm/memory: add memory_block_aligned_range() helper --- commit 1d936debbb2e5198011a220a96cf6a3dc9882748
Author: Gregory Price <[email protected]>
mm/memory: add memory_block_aligned_range() helper
This commit introduces memory_block_aligned_range() in <linux/memory.h> to align ranges to memory block boundaries for memory hotplug operations. It also updates the dax/kmem driver to use this new generic helper.
Quoted 4 lines
include/linux/memory.h +0
-0
[ ... ]
Quoted 14 lines
include/linux/memory.h +10
-0@@ -100,6 +101,32 @@ int arch_get_memory_phys_device(unsigned long start_pfn);unsigned long memory_block_size_bytes (void );int set_memory_block_size_order (unsigned int order); +/** +* memory_block_aligned_range - align a physical address range to memory blocks +* @range: the input range to align +* +* Aligns the start address up and the end address down to memory block +* boundaries. This is required for memory hotplug operations which must +* operate on memory-block aligned ranges. +* +* Returns the aligned range. Callers should check that the returned +* range is valid (aligned.start < aligned.end) before using it.
include/linux/memory.h int arch_get_memory_phys_device(unsigned long start_pfn); · line 123
Quoted 10 lines
include/linux/memory.h +10
-0+*/ +static inline struct range memory_block_aligned_range (const struct range *range) +{ +struct range aligned; + + aligned.start =ALIGN (range->start,memory_block_size_bytes ()); + aligned.end =ALIGN_DOWN (range->end +1 ,memory_block_size_bytes ()); +/* No whole block fits (e.g. range below the first boundary): empty. */ +if (aligned.end <= aligned.start) + aligned.start = aligned.end;
[Severity: Medium]
Does this overflow if range->end is U64_MAX?
If range->end is U64_MAX, range->end + 1 will wrap around to 0. This makes ALIGN_DOWN(0, ...) return 0.
Since aligned.end is now 0, the condition aligned.end <= aligned.start evaluates to true. This would incorrectly report a valid range spanning to the end of the 64-bit address space as an empty range.
Is there a way to handle the U64_MAX upper bound safely to avoid truncating the range?
Quoted 9 lines
include/linux/memory.h +6
-0+else + aligned.end -=1 ; + +return aligned; +} +struct memory_notify {unsigned long start_pfn;unsigned long nr_pages;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v7 2/10 mm/memory_hotplug: add mhp_online_type_to_str() and export string helpers
In reply to Gregory PriceAdd mhp_online_type_to_str() as the inverse of mhp_online_type_from_str(), and export both so a driver can render and parse the memory online type through its own sysfs interface.
--- drivers/base/memory.c | 9 +++++++++ include/linux/memory_hotplug.h | 1 + 2 files changed, 10 insertions(+)
drivers/base/memory.c +9 -0
@@ -46,6 +46,15 @@ int mhp_online_type_from_str(const char *str) }return -EINVAL ; } +EXPORT_SYMBOL_GPL (mhp_online_type_from_str); + +const char *mhp_online_type_to_str (int online_type) +{ +if (online_type <0 || online_type >= (int )ARRAY_SIZE (online_type_to_str)) +return NULL ; +return online_type_to_str[online_type]; +} +EXPORT_SYMBOL_GPL (mhp_online_type_to_str);#define to_memory_block (dev) container_of(dev, struct memory_block, dev)
include/linux/memory_hotplug.h +1 -0
@@ -127,6 +127,7 @@ extern int arch_add_memory(int nid, u64 start, u64 size,extern u64 max_mem_size;extern int mhp_online_type_from_str (const char *str); +const char *mhp_online_type_to_str (int online_type);/* If movable_node boot option specified */ extern bool movable_node_enabled;
Signature · 1 line
2.53.0-Meta
PATCH v7 3/10 mm/memory_hotplug: pass online_type to online_memory_block() via arg
In reply to Gregory PriceModify online_memory_block() to accept the online type through its arg parameter rather than calling mhp_get_default_online_type() internally.
This prepares for allowing callers to specify explicit online types.
Update the caller in add_memory_resource() to pass the default online type via a local variable.
No functional change.
--- mm/memory_hotplug.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
mm/memory_hotplug.c +6 -2
@@ -1338,7 +1338,9 @@ static int check_hotplug_memory_range(u64 start, u64 size)static int online_memory_block (struct memory_block *mem,void *arg) { - mem->online_type =mhp_get_default_online_type (); +enum mmop *online_type = arg; + + mem->online_type = *online_type;return device_online (&mem->dev); }
@@ -1495,6 +1497,7 @@ static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,int add_memory_resource (int nid,struct resource *res,mhp_t mhp_flags) {struct mhp_params params = { .pgprot =pgprot_mhp (PAGE_KERNEL ) }; +enum mmop online_type =mhp_get_default_online_type ();enum memblock_flags memblock_flags =MEMBLOCK_NONE ;struct memory_group *group =NULL ;u64 start, size;
@@ -1583,7 +1586,8 @@ int add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)/* online pages if requested */ if (mhp_get_default_online_type () !=MMOP_OFFLINE ) -walk_memory_blocks (start, size,NULL , online_memory_block); +walk_memory_blocks (start, size, &online_type, + online_memory_block);return ret; error:
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v7 03/10] mm/memory_hotplug: pass online_type to online_memory_block() via arg
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Incomplete refactoring introduces a TOCTOU race condition where `add_memory_resource` uses a stale…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Incomplete refactoring introduces a TOCTOU race condition where add_memory_resource uses a stale snapshot of online_type but evaluates the condition using the live mhp_get_default_online_type(). --
--- Patch [3]: [PATCH v7 03/10] mm/memory_hotplug: pass online_type to online_memory_block() via arg --- commit 72fc58e4b65f508d5057e9fe2ec0ff1c08b34e61
Author: Gregory Price <[email protected]>
mm/memory_hotplug: pass online_type to online_memory_block() via arg
Modify online_memory_block() to accept the online type through its arg parameter rather than calling mhp_get_default_online_type() internally.
Quoted 12 lines
mm/memory_hotplug.c +1
-0@@ -1494,6 +1496,7 @@ static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,int add_memory_resource (int nid,struct resource *res,mhp_t mhp_flags) {struct mhp_params params = { .pgprot =pgprot_mhp (PAGE_KERNEL ) }; +enum mmop online_type =mhp_get_default_online_type ();enum memblock_flags memblock_flags =MEMBLOCK_NONE ;struct memory_group *group =NULL ;u64 start, size;
[ ... ]
mm/memory_hotplug.c int add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) · line 1589
Quoted 7 lines
mm/memory_hotplug.c +2
-1@@ -1582,7 +1585,8 @@ int add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)/* online pages if requested */ if (mhp_get_default_online_type () !=MMOP_OFFLINE ) -walk_memory_blocks (start, size,NULL , online_memory_block); +walk_memory_blocks (start, size, &online_type, + online_memory_block);
[Severity: High]
Does checking the live state here instead of the cached online_type introduce a race condition?
If a concurrent write to the lockless sysfs attribute /sys/devices/system/memory/auto_online_blocks changes the global policy from MMOP_OFFLINE to MMOP_ONLINE_MOVABLE while add_memory_resource() is executing, this condition could evaluate to true while walk_memory_blocks() uses the cached MMOP_OFFLINE value.
Because the condition uses the live value, but walk_memory_blocks() uses the cached online_type, could memory_subsys_online() treat MMOP_OFFLINE as an unconfigured request and forcefully overwrite it to MMOP_ONLINE?
This would result in memory being onlined into the un-removable ZONE_NORMAL kernel zone instead of being left offline or onlined into ZONE_MOVABLE. This pins the memory permanently and prevents memory hot-unplug, leading to resource stranding in virtualized environments.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v7 4/10 mm/memory_hotplug: export mhp_get_default_online_type
In reply to Gregory PriceDrivers which may pass hotplug policy down to DAX need MMOP_ symbols and the mhp_get_default_online_type function for hotplug use cases.
Some drivers (cxl) co-mingle their hotplug and devdax use-cases into the same driver code, and chose the dax_kmem path as the default driver path - making it difficult to require hotplug as a predicate to building the overall driver (it may break other non-hotplug use-cases).
Export mhp_get_default_online_type function to allow these drivers to build when hotplug is disabled and still use the DAX use case.
In the built-out case we simply return MMOP_OFFLINE as it's non-destructive. The internal function can never return -1 either, so we choose this to allow for defining the function with 'enum mmop'.
--- include/linux/memory_hotplug.h | 2 ++ mm/memory_hotplug.c | 1 + 2 files changed, 3 insertions(+)
include/linux/memory_hotplug.h +2 -0
@@ -317,6 +317,8 @@ extern struct zone *zone_for_pfn_range(enum mmop online_type,extern int arch_create_linear_mapping (int nid,u64 start,u64 size,struct mhp_params *params);void arch_remove_linear_mapping (u64 start,u64 size); +#else +static inline enum mmop mhp_get_default_online_type (void ) {return MMOP_OFFLINE ; }#endif /* CONFIG_MEMORY_HOTPLUG */ #endif /* __LINUX_MEMORY_HOTPLUG_H */
mm/memory_hotplug.c +1 -0
@@ -239,6 +239,7 @@ enum mmop mhp_get_default_online_type(void)return mhp_default_online_type; } +EXPORT_SYMBOL_GPL (mhp_get_default_online_type);void mhp_set_default_online_type (enum mmop online_type) {
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v7 04/10] mm/memory_hotplug: export mhp_get_default_online_type
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] add_memory_resource() caches mhp_get_default_online_type() at the start but re-reads it for the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] add_memory_resource() caches mhp_get_default_online_type() at the start but re-reads it for the final condition check, potentially ignoring concurrent sysfs policy changes. --
--- Patch [4]: [PATCH v7 04/10] mm/memory_hotplug: export mhp_get_default_online_type --- commit d037ee754b46e2d238e974dbf3f6ebb2ab7882bc
Author: Gregory Price <[email protected]>
mm/memory_hotplug: export mhp_get_default_online_type
Drivers which may pass hotplug policy down to DAX need MMOP_ symbols and the mhp_get_default_online_type function for hotplug use cases. This commit exports mhp_get_default_online_type to allow these drivers to build when hotplug is disabled and still use the DAX use case.
[ ... ]
mm/memory_hotplug.c enum mmop mhp_get_default_online_type(void) · line 242
Quoted 9 lines
mm/memory_hotplug.c +1
-0@@ -239,6 +239,7 @@ enum mmop mhp_get_default_online_type(void)return mhp_default_online_type; } +EXPORT_SYMBOL_GPL (mhp_get_default_online_type);
[Severity: Medium]
In add_memory_resource(), mhp_get_default_online_type() is cached at the start of the function, but then re-read for the final condition check:
mm/memory_hotplug.c:add_memory_resource() {
enum mmop online_type = mhp_get_default_online_type();
...
/* online pages if requested */
if (mhp_get_default_online_type() != MMOP_OFFLINE)
walk_memory_blocks(start, size, &online_type,
online_memory_block);
}If a system administrator modifies the auto-online sysfs policy concurrently while a memory hot-add operation is in progress, could this re-read cause the operation to silently ignore the user's explicit online policy (e.g., online_movable), onlining the memory into the default zone instead?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v7 5/10 mm/memory_hotplug: add __add_memory_driver_managed() with online_type arg
In reply to Gregory PriceExisting callers of add_memory_driver_managed cannot select the preferred online type (ZONE_NORMAL vs ZONE_MOVABLE), requiring it to hot-add memory as offline blocks, and then follow up by onlining each memory block individually.
Most drivers prefer the system default, but the CXL driver wants to plumb a preferred policy through the dax kmem driver.
Refactor APIs to add a new interface which allows the dax kmem module to select a preferred policy.
Overriding the configured auto-online policy is only safe for known in-tree modules, where we know the override reflects a different, user-requested policy. We do not want arbitrary out-of-tree drivers silently overriding the system-wide onlining policy, so restrict the new interface to the kmem module using EXPORT_SYMBOL_FOR_MODULES() rather than a plain EXPORT_SYMBOL_GPL(). Other in-tree modules (e.g. cxl_core) can be added to the allowed list as the need arises.
Refactor add_memory_driver_managed, extract __add_memory_driver_managed
- Add proper kernel-doc for add_memory_driver_managed while refactoring
- New helper accepts an explicit online_type.
- New helper validates online_type is between OFFLINE and ONLINE_MOVABLE
Refactor: add_memory_resource, extract __add_memory_resource
- new helper accepts an explicit online_type
Original APIs now explicitly pass the system-default to new helpers.
No functional change for existing users.
--- include/linux/memory_hotplug.h | 3 ++ mm/memory_hotplug.c | 61 +++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 8 deletions(-)
include/linux/memory_hotplug.h +3 -0
@@ -295,6 +295,9 @@ extern int __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags);extern int add_memory (int nid,u64 start,u64 size,mhp_t mhp_flags);extern int add_memory_resource (int nid,struct resource *resource,mhp_t mhp_flags); +int __add_memory_driver_managed (int nid,u64 start,u64 size, +const char *resource_name,mhp_t mhp_flags, +enum mmop online_type);extern int add_memory_driver_managed (int nid,u64 start,u64 size,const char *resource_name,mhp_t mhp_flags);
mm/memory_hotplug.c +53 -8
118 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH v7 6/10 mm/memory_hotplug: add offline_and_remove_memory_ranges()
In reply to Gregory Priceoffline_and_remove_memory() handles a single contiguous range.
Callers that manage a device composed of several ranges (dax/kmem) currently have to call it in a loop, which gives up atomicity.
In addition to pushing rollback logic into the driver, the lack of atomicity creates a race condition between system daemons trying to manage the same resource:
- Manager 1: Offlines memory blocks. Removes device.
^^^^
- Manager 2: Detects offline memory blocks, re-onlines them.
Add offline_and_remove_memory_ranges(), which takes an array of ranges and processes them as one operation under a single lock_device_hotplug():
- Phase 1 offlines every block of every range.
- Phase 2 removes the ranges only if all ranges are offline.
- If any offline fails, the whole operation is reverted.
This gives callers all-or-nothing semantics for the offline step, so a failed or interrupted unplug leaves the device in a consistent state.
This also resolves the battling managers race - the second manager's operation simply fails when the block is destroyed / cannot be onlined.
offline_and_remove_memory() becomes a thin wrapper that passes its single range to the new helper, so the offline/rollback logic lives in one place.
--- include/linux/memory_hotplug.h | 8 +++ mm/memory_hotplug.c | 94 ++++++++++++++++++++++++---------- 2 files changed, 75 insertions(+), 27 deletions(-)
include/linux/memory_hotplug.h +8 -0
@@ -268,6 +268,8 @@ extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages,extern int remove_memory (u64 start,u64 size);extern void __remove_memory (u64 start,u64 size);extern int offline_and_remove_memory (u64 start,u64 size); +int offline_and_remove_memory_ranges (const struct range *ranges, +unsigned int nr_ranges);#else static inline void try_offline_node (int nid) {}
@@ -284,6 +286,12 @@ static inline int remove_memory(u64 start, u64 size) }static inline void __remove_memory (u64 start,u64 size) {} + +static inline int offline_and_remove_memory_ranges (const struct range *ranges, +unsigned int nr_ranges) +{ +return -EBUSY ; +}#endif /* CONFIG_MEMORY_HOTREMOVE */ #ifdef CONFIG_MEMORY_HOTPLUG
mm/memory_hotplug.c +67 -27
125 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v7 06/10] mm/memory_hotplug: add offline_and_remove_memory_ranges()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] offline_and_remove_memory_ranges() silently discards errors from try_remove_memory(), falsely…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] offline_and_remove_memory_ranges() silently discards errors from try_remove_memory(), falsely returning success and breaking all-or-nothing rollback semantics. --
--- Patch [6]: [PATCH v7 06/10] mm/memory_hotplug: add offline_and_remove_memory_ranges() --- commit 5dfae7d4a23ca17c8437f8cb92a4cc3f409e0c37
Author: Gregory Price <[email protected]>
mm/memory_hotplug: add offline_and_remove_memory_ranges()
This commit introduces offline_and_remove_memory_ranges() to handle the offlining and removal of multiple memory ranges as a single atomic operation under the device hotplug lock. It offlines all blocks first, reverting on failure, before attempting to remove them.
Quoted 4 lines
mm/memory_hotplug.c +0
-0
[ ... ]
Quoted 3 lines
mm/memory_hotplug.c +3
-0+int offline_and_remove_memory_ranges (const struct range *ranges, +unsigned int nr_ranges) +{
[ ... ]
mm/memory_hotplug.c static int try_reonline_memory_block(struct memory_block *mem, void *arg) · line 2518
Quoted 4 lines
mm/memory_hotplug.c +4
-0+/* Phase 2: Remove. This should never fail holding the hotplug lock */ +for (i =0 ; i < nr_ranges; i++) +WARN_ON_ONCE (try_remove_memory (ranges[i].start, +range_len (&ranges[i])));
[Severity: High]
If try_remove_memory() does happen to fail during Phase 2, does wrapping it in WARN_ON_ONCE() silently discard the error code?
Since rc is not updated here, offline_and_remove_memory_ranges() would return 0 (success) to the caller even if the removal failed.
Could this mislead the calling driver into freeing its device structures, leaving the kernel's memory subsystem with stale references to unremoved memory blocks?
Quoted 8 lines
mm/memory_hotplug.c +3
-0+ +out_unlock:unlock_device_hotplug ();kfree (online_types);return rc; } +EXPORT_SYMBOL_GPL (offline_and_remove_memory_ranges);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v7 7/10 dax/kmem: resolve default online type at probe time
In reply to Gregory PriceResolve the default online type in kmem at probe time explicitly instead of implicitly inheriting the system default by calling add_memory_driver_managed().
No behavioral change at this point (still system default).
--- drivers/dax/kmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/dax/kmem.c +4 -2
@@ -73,6 +73,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)mhp_t mhp_flags;int numa_node;int adist =MEMTIER_DEFAULT_DAX_ADISTANCE ; +int online_type =mhp_get_default_online_type ();/* * Ensure good NUMA information for the persistent memory.
@@ -172,8 +173,9 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)* Ensure that future kexec'd kernels will not treat * this as RAM automatically. */ - rc =add_memory_driver_managed (data->mgid, range.start, -range_len (&range), kmem_name, mhp_flags); + rc =__add_memory_driver_managed (data->mgid, range.start, +range_len (&range), kmem_name, mhp_flags, + online_type);if (rc) { dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n",
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v7 07/10] dax/kmem: resolve default online type at probe time
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Memory leak during device removal if any DAX ranges are too small after alignment. --
--- Patch [7]: [PATCH v7 07/10] dax/kmem: resolve default online type at probe time --- commit 4d3b2bc139f20f3d91088883d6dbe39863e79701
Author: Gregory Price <[email protected]>
dax/kmem: resolve default online type at probe time
Resolve the default online type in kmem at probe time explicitly instead of implicitly inheriting the system default by calling add_memory_driver_managed().
drivers/dax/kmem.c
Quoted 4 lines
drivers/dax/kmem.c +0
-0
[ ... ]
While reviewing this, I noticed a potential resource leak in another part of the file:
Quoted 9 lines
rc = dax_kmem_range(dev_dax, i, &range);
if (rc)
continue;
/* range was never added during probe */
if (!data->res[i]) {
success++;
continue;
}[Severity: High]
This is a pre-existing issue, but does this logic skip the success counter increment when a memory range is too small?
If dax_kmem_range() returns -ENOSPC during probe for a small range, the same check here in dev_dax_kmem_remove() will execute continue early.
This bypasses the success++ operation, which means the success counter will remain strictly less than dev_dax->nr_range.
Quoted 8 lines
if (success >= dev_dax->nr_range) {
memory_group_unregister(data->mgid);
kfree(data->res_name);
kfree(data);
dev_set_drvdata(dev, NULL);
/* ... */
clear_node_memory_type(node, NULL);
}Could this bypass the cleanup block entirely, permanently leaking the memory group and driver data structures every time a device with unaligned or small memory ranges is unbound?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v7 8/10 dax/kmem: extract hotplug/hotremove helper functions
In reply to Gregory PriceRefactor kmem _probe() _remove() by extracting init, cleanup, hotplug, and hot-remove logic into separate helper functions:
- dax_kmem_init_resources: inits IO_RESOURCE w/ request_mem_region
- dax_kmem_cleanup_resources: cleans up initialized IO_RESOURCE
- dax_kmem_do_hotplug: handles memory region reservation and adding
- dax_kmem_do_hotremove: handles memory removal and resource cleanup
This is a pure refactoring with no functional change. The helpers will enable future extensions to support more granular control over memory hotplug operations.
We need to split hotplug/hotunplug and init/cleanup in order to have the resources available for hot-add. Otherwise, when probe occurs, the dax devices are never added to sysfs because the resources are never registered.
Detatching hotunplug/cleanup allows us to re-use the hotunplug code without destroying the underlying resources.
--- drivers/dax/kmem.c | 323 +++++++++++++++++++++++++++++++-------------- 1 file changed, 223 insertions(+), 100 deletions(-)
drivers/dax/kmem.c +223 -100
@@ -63,14 +63,206 @@ static void kmem_put_memory_types(void)mt_put_memory_types (&kmem_memory_types); } +/** +* dax_kmem_do_hotplug - hotplug memory for dax kmem device +* @dev_dax: the dev_dax instance +* @data: the dax_kmem_data structure with resource tracking +* +* Hotplugs all ranges in the dev_dax region as system memory. +* +* Returns the number of successfully mapped ranges, or negative error. +*/ +static int dax_kmem_do_hotplug (struct dev_dax *dev_dax, +struct dax_kmem_data *data, +int online_type) +{ +struct device *dev = &dev_dax->dev; +int i, rc, onlined =0 ; +mhp_t mhp_flags; + +for (i =0 ; i < dev_dax->nr_range; i++) { +struct range range; + + rc =dax_kmem_range (dev_dax, i, &range); +if (rc) +continue ; + +/* +* init_resources() is best-effort: if a reservation conflict +* occurs it keeps the range but leaves res[i]=NULL. For hotplug +* on probe systems, this means kmem will partially online. +* +* We have to keep this behavior not to break those systems. +* For those systems - atomicity only applies to valid ranges. +*/ +if (!data->res[i]) +continue ; + + mhp_flags =MHP_NID_IS_MGID ; +if (dev_dax->memmap_on_memory) + mhp_flags |=MHP_MEMMAP_ON_MEMORY ; + +/* +* Ensure that future kexec'd kernels will not treat +* this as RAM automatically. +*/ + rc =__add_memory_driver_managed (data->mgid, range.start, +range_len (&range), kmem_name, mhp_flags, + online_type); + +if (rc) { +dev_warn (dev,"mapping%d: %#llx-%#llx memory add failed\n" , + i, range.start, range.end); +/* +* Release the reservation for the range that failed to +* add so a later hotremove does not try to remove memory +* that was never added. +*/ +if (data->res[i]) { +remove_resource (data->res[i]); +kfree (data->res[i]); + data->res[i] =NULL ; + } +if (onlined) +continue ; +return rc; + } + onlined++; + } + +return onlined; +} + +/** +* dax_kmem_init_resources - create memory regions for dax kmem +* @dev_dax: the dev_dax instance +* @data: the dax_kmem_data structure with resource tracking +* +* Initializes all the resources for the DAX +* +* Returns the number of successfully mapped ranges, or negative error. +*/ +static int dax_kmem_init_resources (struct dev_dax *dev_dax, +struct dax_kmem_data *data) +{ +struct device *dev = &dev_dax->dev; +int i, rc, mapped =0 ; + +for (i =0 ; i < dev_dax->nr_range; i++) { +struct resource *res; +struct range range; + + rc =dax_kmem_range (dev_dax, i, &range); +if (rc) +continue ; + +/* Skip ranges already added */ +if (data->res[i]) +continue ; + +/* Region is permanently reserved if hotremove fails. */ + res =request_mem_region (range.start,range_len (&range), + data->res_name); +if (!res) { +dev_warn (dev,"mapping%d: %#llx-%#llx could not reserve region\n" , + i, range.start, range.end); +/* +* Once some memory has been onlined we can't +* assume that it can be un-onlined safely. +*/ +if (mapped) +continue ; +return -EBUSY ; + } + data->res[i] = res; +/* +* Set flags appropriate for System RAM. Leave ..._BUSY clear +* so that add_memory() can add a child resource. Do not +* inherit flags from the parent since it may set new flags +* unknown to us that will break add_memory() later. +*/ + res->flags =IORESOURCE_SYSTEM_RAM ; + mapped++; + } +return mapped; +} + +#ifdef CONFIG_MEMORY_HOTREMOVE +/** +* dax_kmem_do_hotremove - hot-remove memory for dax kmem device +* @dev_dax: the dev_dax instance +* @data: the dax_kmem_data structure with resource tracking +* +* Removes all ranges in the dev_dax region. +* +* Returns the number of successfully removed ranges. +*/ +static int dax_kmem_do_hotremove (struct dev_dax *dev_dax, +struct dax_kmem_data *data) +{ +struct device *dev = &dev_dax->dev; +int i, success =0 ; + +for (i =0 ; i < dev_dax->nr_range; i++) { +struct range range; +int rc; + + rc =dax_kmem_range (dev_dax, i, &range); +if (rc) +continue ; + +/* range was never added during probe, count as removed */ +if (!data->res[i]) { + success++; +continue ; + } + + rc =remove_memory (range.start,range_len (&range)); +if (rc ==0 ) { +/* Release the resource for the successfully removed range */ +remove_resource (data->res[i]); +kfree (data->res[i]); + data->res[i] =NULL ; + success++; +continue ; + } + any_hotremove_failed = true; +dev_err (dev,"mapping%d: %#llx-%#llx hotremove failed\n" , + i, range.start, range.end); + } + +return success; +} +#endif /* CONFIG_MEMORY_HOTREMOVE */ + +/** +* dax_kmem_cleanup_resources - remove the dax memory resources +* @dev_dax: the dev_dax instance +* @data: the dax_kmem_data structure with resource tracking +* +* Removes all resources in the dev_dax region. +*/ +static void dax_kmem_cleanup_resources (struct dev_dax *dev_dax, +struct dax_kmem_data *data) +{ +int i; + +for (i =0 ; i < dev_dax->nr_range; i++) { +if (!data->res[i]) +continue ; +remove_resource (data->res[i]); +kfree (data->res[i]); + data->res[i] =NULL ; + } +} +static int dev_dax_kmem_probe (struct dev_dax *dev_dax) {struct device *dev = &dev_dax->dev;unsigned long total_len =0 , orig_len =0 ;struct dax_kmem_data *data;struct memory_dev_type *mtype; -int i, rc, mapped =0 ; -mhp_t mhp_flags; +int i, rc;int numa_node;int adist =MEMTIER_DEFAULT_DAX_ADISTANCE ;int online_type =mhp_get_default_online_type ();
@@ -133,68 +325,22 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) goto err_reg_mgid; data->mgid = rc; -for (i =0 ; i < dev_dax->nr_range; i++) { -struct resource *res; -struct range range; - - rc =dax_kmem_range (dev_dax, i, &range); -if (rc) -continue ; - -/* Region is permanently reserved if hotremove fails. */ - res =request_mem_region (range.start,range_len (&range), data->res_name); -if (!res) { -dev_warn (dev,"mapping%d: %#llx-%#llx could not reserve region\n" , - i, range.start, range.end); -/* -* Once some memory has been onlined we can't -* assume that it can be un-onlined safely. -*/ -if (mapped) -continue ; - rc = -EBUSY ; - goto err_request_mem; - } - data->res[i] = res; - -/* -* Set flags appropriate for System RAM. Leave ..._BUSY clear -* so that add_memory() can add a child resource. Do not -* inherit flags from the parent since it may set new flags -* unknown to us that will break add_memory() below. -*/ - res->flags =IORESOURCE_SYSTEM_RAM ; - - mhp_flags =MHP_NID_IS_MGID ; -if (dev_dax->memmap_on_memory) - mhp_flags |=MHP_MEMMAP_ON_MEMORY ; - -/* -* Ensure that future kexec'd kernels will not treat -* this as RAM automatically. -*/ - rc =__add_memory_driver_managed (data->mgid, range.start, -range_len (&range), kmem_name, mhp_flags, - online_type); +dev_set_drvdata (dev, data); -if (rc) { -dev_warn (dev,"mapping%d: %#llx-%#llx memory add failed\n" , - i, range.start, range.end); -remove_resource (res); -kfree (res); - data->res[i] =NULL ; -if (mapped) -continue ; - goto err_request_mem; - } - mapped++; - } + rc =dax_kmem_init_resources (dev_dax, data); +if (rc <0 ) + goto err_resources; -dev_set_drvdata (dev, data); + rc =dax_kmem_do_hotplug (dev_dax, data, online_type); +if (rc <0 ) + goto err_hotplug;return 0 ; -err_request_mem: +err_hotplug: +dax_kmem_cleanup_resources (dev_dax, data); +err_resources: +dev_set_drvdata (dev,NULL );memory_group_unregister (data->mgid); err_reg_mgid:kfree (data->res_name);
@@ -208,7 +354,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)#ifdef CONFIG_MEMORY_HOTREMOVE static void dev_dax_kmem_remove (struct dev_dax *dev_dax) { -int i, success =0 ; +int success;int node = dev_dax->target_node;struct device *dev = &dev_dax->dev;struct dax_kmem_data *data =dev_get_drvdata (dev);
@@ -219,48 +365,25 @@ static void dev_dax_kmem_remove(struct dev_dax *dev_dax)* there is no way to hotremove this memory until reboot because device * unbind will succeed even if we return failure. */ -for (i =0 ; i < dev_dax->nr_range; i++) { -struct range range; -int rc; - - rc =dax_kmem_range (dev_dax, i, &range); -if (rc) -continue ; - -/* range was never added during probe */ -if (!data->res[i]) { - success++; -continue ; - } - - rc =remove_memory (range.start,range_len (&range)); -if (rc ==0 ) { -remove_resource (data->res[i]); -kfree (data->res[i]); - data->res[i] =NULL ; - success++; -continue ; - } - any_hotremove_failed = true; -dev_err (dev, -"mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n" , - i, range.start, range.end); + success =dax_kmem_do_hotremove (dev_dax, data); +if (success < dev_dax->nr_range) { +dev_err (dev,"Hotplug regions stuck online until reboot\n" ); +return ; } -if (success >= dev_dax->nr_range) { -memory_group_unregister (data->mgid); -kfree (data->res_name); -kfree (data); -dev_set_drvdata (dev,NULL ); -/* -* Clear the memtype association on successful unplug. -* If not, we have memory blocks left which can be -* offlined/onlined later. We need to keep memory_dev_type -* for that. This implies this reference will be around -* till next reboot. -*/ -clear_node_memory_type (node,NULL ); - } +dax_kmem_cleanup_resources (dev_dax, data); +memory_group_unregister (data->mgid); +kfree (data->res_name); +kfree (data); +dev_set_drvdata (dev,NULL ); +/* +* Clear the memtype association on successful unplug. +* If not, we have memory blocks left which can be +* offlined/onlined later. We need to keep memory_dev_type +* for that. This implies this reference will be around +* till next reboot. +*/ + clear_node_memory_type(node, NULL); }#else static void dev_dax_kmem_remove (struct dev_dax *dev_dax)
Signature · 1 line
2.53.0-Meta
Re: [PATCH v7 08/10] dax/kmem: extract hotplug/hotremove helper functions
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] If a DAX device has multiple ranges and at least one is too small to be aligned, removing the device will unconditionally leak driver data and memory group registrations. --
--- Patch [8]: [PATCH v7 08/10] dax/kmem: extract hotplug/hotremove helper functions --- commit 44ad562618b0448a09ed81ab105fa4c8551b6718
Author: Gregory Price <[email protected]>
dax/kmem: extract hotplug/hotremove helper functions
Refactor kmem _probe() _remove() by extracting init, cleanup, hotplug, and hot-remove logic into separate helper functions. This splits the hotplug and init phases to have resources available for hot-add, while enabling future granular hotplug operation control.
Quoted 4 lines
drivers/dax/kmem.c +0
-0
[ ... ]
Quoted 36 lines
drivers/dax/kmem.c +36
-0+static int dax_kmem_do_hotremove (struct dev_dax *dev_dax, +struct dax_kmem_data *data) +{ +struct device *dev = &dev_dax->dev; +int i, success =0 ; + +for (i =0 ; i < dev_dax->nr_range; i++) { +struct range range; +int rc; + + rc =dax_kmem_range (dev_dax, i, &range); +if (rc) +continue ; + +/* range was never added during probe, count as removed */ +if (!data->res[i]) { + success++; +continue ; + } + + rc =remove_memory (range.start,range_len (&range)); +if (rc ==0 ) { +/* Release the resource for the successfully removed range */ +remove_resource (data->res[i]); +kfree (data->res[i]); + data->res[i] =NULL ; + success++; +continue ; + } + any_hotremove_failed = true; +dev_err (dev,"mapping%d: %#llx-%#llx hotremove failed\n" , + i, range.start, range.end); + } + +return success; +}
[ ... ]
Quoted 34 lines
drivers/dax/kmem.c +1
-22@@ -208,7 +354,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)#ifdef CONFIG_MEMORY_HOTREMOVE static void dev_dax_kmem_remove (struct dev_dax *dev_dax) { -int i, success =0 ; +int success;int node = dev_dax->target_node;struct device *dev = &dev_dax->dev;struct dax_kmem_data *data =dev_get_drvdata (dev);@@ -219,48 +365,25 @@ static void dev_dax_kmem_remove(struct dev_dax *dev_dax)* there is no way to hotremove this memory until reboot because device * unbind will succeed even if we return failure. */ -for (i =0 ; i < dev_dax->nr_range; i++) { -struct range range; -int rc; - - rc =dax_kmem_range (dev_dax, i, &range); -if (rc) -continue ; - -/* range was never added during probe */ -if (!data->res[i]) { - success++; -continue ; - } - - rc =remove_memory (range.start,range_len (&range)); -if (rc ==0 ) { -remove_resource (data->res[i]); -kfree (data->res[i]); - data->res[i] =NULL ; - success++; -continue ;
drivers/dax/kmem.c static void dev_dax_kmem_remove(struct dev_dax *dev_dax) · line 372
Quoted 10 lines
drivers/dax/kmem.c +4
-5- } - any_hotremove_failed = true; -dev_err (dev, -"mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n" , - i, range.start, range.end); + success =dax_kmem_do_hotremove (dev_dax, data); +if (success < dev_dax->nr_range) { +dev_err (dev,"Hotplug regions stuck online until reboot\n" ); +return ; }
[Severity: High]
This is a pre-existing issue, but does this logic handle unaligned ranges correctly during device unbind?
If a DAX device is created with a memory range smaller than the block alignment, dax_kmem_range() returns an error (like -ENOSPC) during probe, but the device can still probe successfully.
During removal, dax_kmem_do_hotremove() will also get an error from dax_kmem_range() for that small range and skip incrementing the success counter.
This causes success to evaluate as strictly less than dev_dax->nr_range, resulting in an early return here.
Since device unbind can be triggered from userspace, does this early return unconditionally leak the memory group registrations, device name string, and driver data on every unbind for devices with unaligned ranges?
Quoted 6 lines
drivers/dax/kmem.c +0
-5-if (success >= dev_dax->nr_range) { -memory_group_unregister (data->mgid); -kfree (data->res_name); -kfree (data); -dev_set_drvdata (dev,NULL );
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
PATCH v7 9/10 dax/kmem: add sysfs interface for atomic whole-device hotplug
In reply to Gregory PriceThere is no atomic mechanism to offline and remove an entire multi-block DAX kmem device. This is presently done in two steps:
1. offline all
2. remove all
This creates a race condition where another entity operates directly on the memory blocks and can cause hot-unplug to fail / unbind to deadlock.
Add a new 'state' sysfs attribute that enables an atomic whole-device hotplug operation across its entire memory region.
daxX.Y/state mirrors the per-block memoryX/state ABI:
- [offline, online, online_kernel, online_movable]
- "unplugged" - is added specifically for dax0.0/state
The valid writable states include:
- "unplugged": memory blocks are not present - "online": memory is online, zone chosen by the kernel - "online_kernel": memory is online in ZONE_NORMAL
- "online_movable": memory is online in ZONE_MOVABLE
Valid transitions:
- unplugged -> online[_kernel|_movable]
- online[_kernel|_movable] -> unplugged
- offline -> unplugged
A device can only be onlined from "unplugged", so it must be returned there before being onlined into a different state.
For backwards compatibility the memory blocks are always created at probe - existing tools expect them to be present after kmem binds.
"offline" is therefore a reportable state but is not writable: it only arises from the legacy auto_online_blocks=offline policy. Onlining such a device through this attribute requires unplugging it first in an effort to get drivers creating DAX devices to set a default.
Unplug is atomic across the whole device: dax_kmem_do_hotremove() collects every added range and offlines/removes them in one operation. Either the operation succeeds or is entirely rolled back.
Unbind Note:
An offline dax device memory is removed on unbind as before.
If online at unbind, the resources are leaked (as before), but now we prevent deadlock if a memory region is impossible to hotremove.
--- Documentation/ABI/testing/sysfs-bus-dax | 24 +++ drivers/dax/bus.h | 2 + drivers/dax/kmem.c | 243 ++++++++++++++++++++---- 3 files changed, 233 insertions(+), 36 deletions(-)
Documentation/ABI/testing/sysfs-bus-dax +24 -0
@@ -151,3 +151,27 @@ Description: memmap_on_memory parameter for memory_hotplug. This is typically set on the kernel command line - memory_hotplug.memmap_on_memory set to 'true' or 'force'." + +What: /sys/bus/dax/devices/daxX.Y/state +Contact: [email protected] +Description: + (RW) Controls the state of the memory region. + Applies to all memory blocks associated with the device. + Only applies to dax_kmem devices. + + Reading returns the current state; the writable states mirror + the per-block /sys/devices/system/memory/memoryX/state ABI:: + + "unplugged": memory blocks are not present + "online": memory is online, zone chosen by the kernel + "online_kernel": memory is online in ZONE_NORMAL + "online_movable": memory is online in ZONE_MOVABLE + + "offline" (memory blocks are present but offline) may also be + reported - this happens when the device is bound while the + auto_online_blocks policy is "offline". It cannot be written, + as it's not useful and creates device destruction races. + + A device can only be onlined from the "unplugged" state, so a + device must be returned to "unplugged" before it can be onlined + into a different state.
drivers/dax/bus.h +2 -0
@@ -16,6 +16,8 @@ struct dax_region;#define IORESOURCE_DAX_STATIC BIT(0)#define IORESOURCE_DAX_KMEM BIT(1) +#define DAX_KMEM_UNPLUGGED (-1)/* Do not create memory blocks */ +struct dax_region *alloc_dax_region (struct device *parent,int region_id,struct range *range,int target_node,unsigned int align,unsigned long flags);
drivers/dax/kmem.c +207 -36
353 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH v7 10/10 selftests/dax: add dax/kmem hotplug sysfs regression test
In reply to Gregory PriceAdd a kselftest for the dax/kmem whole-device "state" sysfs attribute (/sys/bus/dax/devices/daxX.Y/state), which transitions a kmem-backed dax device between "unplugged", "online" and "online_movable".
The kselftest also includes a test to demonstrate the force-unbind does not deadlock - but this is destructive (the dax device can never be rebound), so it only runs when DAX_KMEM_TEST_UNBIND=1 is set.
Provisioning a devdax device and binding it to kmem needs daxctl/ndctl out of scope for an in-tree selftest. As the test mutates a device's memory, the operator opts in by naming it in DAX_KMEM_TEST_DEV (or "auto" to pick the first kmem-bound device); it SKIPs when unset, when no device is present, or when the memory cannot be freed to a baseline.
When a device is available it validates the interface contract:
- online / online_movable actually add memory (MemTotal grows),
- online is idempotent,
- switching between online types without unplug is rejected,
- unplug removes memory and the reported state is "unplugged"
- invalid input is rejected,
- unplug and unbind tolerate blocks toggled out-of-band through the per-block memoryX/state interface.
One specific regression test:
online -> unplug -> online_movable -> unplug
Re-online must re-reserve per-range resources so subsequent unplug actually offlines and removes instead of silently reporting success while the memory stays online.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/dax/Makefile | 6 + tools/testing/selftests/dax/config | 4 + .../testing/selftests/dax/dax-kmem-hotplug.sh | 317 ++++++++++++++++++ tools/testing/selftests/dax/settings | 1 + 5 files changed, 329 insertions(+) create mode 100644 tools/testing/selftests/dax/Makefile create mode 100644 tools/testing/selftests/dax/config create mode 100755 tools/testing/selftests/dax/dax-kmem-hotplug.sh create mode 100644 tools/testing/selftests/dax/settings
tools/testing/selftests/Makefile +1 -0
@@ -14,6 +14,7 @@ TARGETS += core TARGETS += cpufreq TARGETS += cpu-hotplug TARGETS += damon +TARGETS += dax TARGETS += devices/error_logs TARGETS += devices/probe TARGETS += dmabuf-heaps
tools/testing/selftests/dax/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +all: + +TEST_PROGS := dax-kmem-hotplug.sh + +include ../lib.mk
tools/testing/selftests/dax/config new file +4 -0
@@ -0,0 +1,4 @@ +CONFIG_DEV_DAX=m +CONFIG_DEV_DAX_KMEM=m +CONFIG_MEMORY_HOTPLUG=y +CONFIG_MEMORY_HOTREMOVE=y
tools/testing/selftests/dax/dax-kmem-hotplug.sh new file +317 -0
317 lines not on this page · raw message
tools/testing/selftests/dax/settings new file +1 -0
@@ -0,0 +1 @@ +timeout=90
Signature · 1 line
2.53.0-Meta
Quoted 4 lines · Gregory Price
The dax kmem driver onlines memory during probe using the system default policy, with no atomic control for the state of an entire region at runtime - only by toggling individual memory blocks.
Took a look at Sashiko reviews.
All are either false-positive, pre-existing issues on isolated patches that are actually resolved by later patches, or something we previously agreed on (WARN/pass in phase 2 of offline_and_remove_memory_ranges).
So series is clean as-is.
~Gregory