mm: Convert memory block states (MEM_*) macros to
Subsystems (5)
v2 cover mm: Convert memory block states (MEM_*) macros to
The MEM_* constants indicating the state of a memory block are currently defined as macros, meaning their definitions will be omitted from the debuginfo on most kernel builds. This makes it harder for debuggers to correctly map the block state at runtime, which can be quite useful when analysing errors related to memory hot plugging and unplugging with tools such as drgn.
Converting the constants to an enum would ensure the correct information is emitted by the compiler and available for the debugger, without needing to hard-code them into the debugger and track their changes.
This patch series aims to replace the current macros with a newly created enum named memory_block_state, while also taking advantage of the compile time guarantees that we get when using enums.
The first patch does the conversion of the macros to an enum, while the 2nd and 3rd patches use this enum to clean up some type declarations and make sure that only valid values are used.
---
v1 -> v2
- Rename the enum to make it more descriptive.
- Let the enum auto-generate the values, as the (1<<X) pattern could be misleading and they're not exposed to userspace.
- Change the type signature from unsigned long to enum memory_block_state where suitable.
Thanks to everyone who took their time to review the first version.
This patch series applies to commit: f30d294530d9 (mm-new)
Israel Batista (3):
mm: convert memory block states (MEM_*) macros to enum
mm: change type of state in struct memory_block
mm: change type of parameter for memory_notify
drivers/base/memory.c | 6 +++--- include/linux/memory.h | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-)
Signature · 1 line
2.51.0
PATCH v2 1/3 mm: convert memory block states (MEM_*) macros to enum
Converting the MEM_* constants from macros to an enum ensures that their values will be correctly emitted in the debug symbols, making it easier to trace the meaning of each value when debugging with tools such as drgn, without the need to hard-code the values.
Since the values are mutually exclusive and they are not exposed directly to userspace, I also dropped the misleading pattern (1<<X) that made it look like they were combinable flags.
--- include/linux/memory.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
include/linux/memory.h +12 -10
@@ -64,6 +64,18 @@ struct memory_group { }; }; +enum memory_block_state { +/* These states are exposed to userspace as text strings in sysfs */ +MEM_ONLINE ,/* exposed to userspace */ +MEM_GOING_OFFLINE ,/* exposed to userspace */ +MEM_OFFLINE ,/* exposed to userspace */ +MEM_GOING_ONLINE , +MEM_CANCEL_ONLINE , +MEM_CANCEL_OFFLINE , +MEM_PREPARE_ONLINE , +MEM_FINISH_OFFLINE , +}; +struct memory_block {unsigned long start_section_nr;unsigned long state;/* serialized by the dev->lock */
@@ -89,16 +101,6 @@ 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); -/* These states are exposed to userspace as text strings in sysfs */ -#define MEM_ONLINE (1<<0)/* exposed to userspace */ -#define MEM_GOING_OFFLINE (1<<1)/* exposed to userspace */ -#define MEM_OFFLINE (1<<2)/* exposed to userspace */ -#define MEM_GOING_ONLINE (1<<3) -#define MEM_CANCEL_ONLINE (1<<4) -#define MEM_CANCEL_OFFLINE (1<<5) -#define MEM_PREPARE_ONLINE (1<<6) -#define MEM_FINISH_OFFLINE (1<<7) -struct memory_notify {/* * The altmap_start_pfn and altmap_nr_pages fields are designated for
Signature · 1 line
2.51.0
Re: [PATCH v2 1/3] mm: convert memory block states (MEM_*) macros to enum
Quoted 10 lines · Israel Batista
Converting the MEM_* constants from macros to an enum ensures that their values will be correctly emitted in the debug symbols, making it easier to trace the meaning of each value when debugging with tools such as drgn, without the need to hard-code the values.
Since the values are mutually exclusive and they are not exposed directly to userspace, I also dropped the misleading pattern (1<<X) that made it look like they were combinable flags.
Signed-off-by Israel Batista <[email protected]>
LGTM, so:
Quoted 47 lines
--- include/linux/memory.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)include/linux/memory.h +12
-11@@ -64,6 +64,18 @@ struct memory_group { }; }; +enum memory_block_state { +/* These states are exposed to userspace as text strings in sysfs */ +MEM_ONLINE ,/* exposed to userspace */ +MEM_GOING_OFFLINE ,/* exposed to userspace */ +MEM_OFFLINE ,/* exposed to userspace */ +MEM_GOING_ONLINE , +MEM_CANCEL_ONLINE , +MEM_CANCEL_OFFLINE , +MEM_PREPARE_ONLINE , +MEM_FINISH_OFFLINE , +}; +struct memory_block {unsigned long start_section_nr;unsigned long state;/* serialized by the dev->lock */ @@ -89,16 +101,6 @@ 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); -/* These states are exposed to userspace as text strings in sysfs */ -#define MEM_ONLINE (1<<0)/* exposed to userspace */ -#define MEM_GOING_OFFLINE (1<<1)/* exposed to userspace */ -#define MEM_OFFLINE (1<<2)/* exposed to userspace */ -#define MEM_GOING_ONLINE (1<<3) -#define MEM_CANCEL_ONLINE (1<<4) -#define MEM_CANCEL_OFFLINE (1<<5) -#define MEM_PREPARE_ONLINE (1<<6) -#define MEM_FINISH_OFFLINE (1<<7) -struct memory_notify {/* * The altmap_start_pfn and altmap_nr_pages fields are designated for -- 2.51.0
Re: [PATCH v2 1/3] mm: convert memory block states (MEM_*) macros to enum
In reply to Israel BatistaQuoted 11 lines · Israel Batista
Converting the MEM_* constants from macros to an enum ensures that their values will be correctly emitted in the debug symbols, making it easier to trace the meaning of each value when debugging with tools such as drgn, without the need to hard-code the values.
Since the values are mutually exclusive and they are not exposed directly to userspace, I also dropped the misleading pattern (1<<X) that made it look like they were combinable flags.
Signed-off-by Israel Batista <[email protected]>---
Signature · 3 lines
Cheers David / dhildenb
PATCH v2 2/3 mm: change type of state in struct memory_block
In reply to Israel BatistaThe state of a memory block should be restricted to values specified in the documentation of the memory hotplug API. However, since the state field in the memory_block struct was defined as an unsigned long, this restriction was not enforced at compile time.
With the introduction of the enum memory_block_state, it is now possible to incorporate the desired semantics in the field declaration and enforce these restrictions at compile time.
--- drivers/base/memory.c | 2 +- include/linux/memory.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/base/memory.c +1 -1
@@ -198,7 +198,7 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,break ;default :WARN_ON (1 ); -return sysfs_emit (buf,"ERROR-UNKNOWN-%ld\n" , mem->state); +return sysfs_emit (buf,"ERROR-UNKNOWN-%d\n" , mem->state); }return sysfs_emit (buf,"%s\n" , output);
include/linux/memory.h +1 -1
@@ -78,7 +78,7 @@ enum memory_block_state {struct memory_block {unsigned long start_section_nr; -unsigned long state;/* serialized by the dev->lock */ +enum memory_block_state state;/* serialized by the dev->lock */ int online_type;/* for passing data to online routine */ int nid;/* NID for this memory block */ /*
Signature · 1 line
2.51.0
Re: [PATCH v2 2/3] mm: change type of state in struct memory_block
Quoted 33 lines · Israel Batista
The state of a memory block should be restricted to values specified in the documentation of the memory hotplug API. However, since the state field in the
memory_blockstruct was defined as an unsigned long, this restriction was not enforced at compile time.With the introduction of the enum
memory_block_state, it is now possible to incorporate the desired semantics in the field declaration and enforce these restrictions at compile time.Signed-off-by Israel Batista <[email protected]>--- drivers/base/memory.c | 2 +- include/linux/memory.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/base/memory.c +1
-1@@ -198,7 +198,7 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,break ;default :WARN_ON (1 ); -return sysfs_emit (buf,"ERROR-UNKNOWN-%ld\n" , mem->state); +return sysfs_emit (buf,"ERROR-UNKNOWN-%d\n" , mem->state); }return sysfs_emit (buf,"%s\n" , output);include/linux/memory.h +0
-0
include/linux/memory.h enum memory_block_state { · line 81
Quoted 5 lines
include/linux/memory.h +1
-1@@ -78,7 +78,7 @@ enum memory_block_state {struct memory_block {unsigned long start_section_nr; -unsigned long state;/* serialized by the dev->lock */ +enum memory_block_state state;/* serialized by the dev->lock */
Use a tab after the ';' please, instead of a space, to align the comments.
Quoted 3 lines
include/linux/memory.h +0
-0int online_type;/* for passing data to online routine */ int nid;/* NID for this memory block */ /*
Signature · 1 line
~Randy
Re: [PATCH v2 2/3] mm: change type of state in struct memory_block
In reply to Israel BatistaQuoted 8 lines · Israel Batista
The state of a memory block should be restricted to values specified in the documentation of the memory hotplug API. However, since the state field in the
memory_blockstruct was defined as an unsigned long, this restriction was not enforced at compile time.With the introduction of the enum
memory_block_state, it is now possible to incorporate the desired semantics in the field declaration and enforce these restrictions at compile time.
Well I'm not sure how much is enforced at compile time, the compiler treats the enum very loosely as if it were simply an int. BUt it does make it clear what you expect this field to contain!
Quoted 2 lines
Signed-off-by Israel Batista <[email protected]>
This LGTM as a reasonable change though, so:
On basis that you fixup the formatting issue raised by Randy.
Quoted 23 lines
--- drivers/base/memory.c | 2 +- include/linux/memory.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/base/memory.c +1
-1@@ -198,7 +198,7 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,break ;default :WARN_ON (1 ); -return sysfs_emit (buf,"ERROR-UNKNOWN-%ld\n" , mem->state); +return sysfs_emit (buf,"ERROR-UNKNOWN-%d\n" , mem->state); }return sysfs_emit (buf,"%s\n" , output);include/linux/memory.h +0
-0
include/linux/memory.h enum memory_block_state { · line 81
Quoted 5 lines
include/linux/memory.h +1
-1@@ -78,7 +78,7 @@ enum memory_block_state {struct memory_block {unsigned long start_section_nr; -unsigned long state;/* serialized by the dev->lock */ +enum memory_block_state state;/* serialized by the dev->lock */
I doubt it will be an isuse, but this obviously changes the layout of the struct.
However since this will be an int and you have a bunch of ints here it should all pad reasonably.
Quoted 6 lines
include/linux/memory.h +0
-1int online_type;/* for passing data to online routine */ int nid;/* NID for this memory block */ /* -- 2.51.0
Re: [PATCH v2 2/3] mm: change type of state in struct memory_block
In reply to Israel BatistaQuoted 33 lines · Israel Batista
The state of a memory block should be restricted to values specified in the documentation of the memory hotplug API. However, since the state field in the
memory_blockstruct was defined as an unsigned long, this restriction was not enforced at compile time.With the introduction of the enum
memory_block_state, it is now possible to incorporate the desired semantics in the field declaration and enforce these restrictions at compile time.Signed-off-by Israel Batista <[email protected]>--- drivers/base/memory.c | 2 +- include/linux/memory.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/base/memory.c +1
-1@@ -198,7 +198,7 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,break ;default :WARN_ON (1 ); -return sysfs_emit (buf,"ERROR-UNKNOWN-%ld\n" , mem->state); +return sysfs_emit (buf,"ERROR-UNKNOWN-%d\n" , mem->state); }return sysfs_emit (buf,"%s\n" , output);include/linux/memory.h +0
-0
include/linux/memory.h enum memory_block_state { · line 84
Quoted 8 lines
include/linux/memory.h +1
-1@@ -78,7 +78,7 @@ enum memory_block_state {struct memory_block {unsigned long start_section_nr; -unsigned long state;/* serialized by the dev->lock */ +enum memory_block_state state;/* serialized by the dev->lock */ int online_type;/* for passing data to online routine */ int nid;/* NID for this memory block */ /*
With comment alignment fixed
Signature · 3 lines
Cheers David / dhildenb
PATCH v2 3/3 mm: change type of parameter for memory_notify
In reply to Israel BatistaThe memory_notify function is responsible for sending events related to memory hotplugging to a notification queue. Since all the events must match one of the values from the enum memory_block_state, it is appropriate to change the function parameter type to make this condition explicit at compile time.
--- drivers/base/memory.c | 4 ++-- include/linux/memory.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/base/memory.c +2 -2
@@ -204,9 +204,9 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,return sysfs_emit (buf,"%s\n" , output); } -int memory_notify (unsigned long val,void *v) +int memory_notify (enum memory_block_state state,void *v) { -return blocking_notifier_call_chain (&memory_chain, val, v); +return blocking_notifier_call_chain (&memory_chain, state, v); }#if defined(CONFIG_MEMORY_FAILURE ) && defined(CONFIG_MEMORY_HOTPLUG )
include/linux/memory.h +2 -2
@@ -141,7 +141,7 @@ static inline int register_memory_notifier(struct notifier_block *nb)static inline void unregister_memory_notifier (struct notifier_block *nb) { } -static inline int memory_notify (unsigned long val,void *v) +static inline int memory_notify (enum memory_block_state state,void *v) {return 0 ; }
@@ -165,7 +165,7 @@ int create_memory_block_devices(unsigned long start, unsigned long size, struct memory_group *group);void remove_memory_block_devices (unsigned long start,unsigned long size);extern void memory_dev_init (void ); -extern int memory_notify (unsigned long val,void *v); +extern int memory_notify (enum memory_block_state state,void *v);extern struct memory_block *find_memory_block (unsigned long section_nr);typedef int (*walk_memory_blocks_func_t )(struct memory_block *,void *); extern int walk_memory_blocks(unsigned long start, unsigned long size,
Signature · 1 line
2.51.0
Re: [PATCH v2 3/3] mm: change type of parameter for memory_notify
Quoted 7 lines · Israel Batista
The
memory_notifyfunction is responsible for sending events related to memory hotplugging to a notification queue. Since all the events must match one of the values from the enummemory_block_state, it is appropriate to change the function parameter type to make this condition explicit at compile time.Signed-off-by Israel Batista <[email protected]>
This seems fine, but I can see a whole bunch of others like:
kcore_callback()mm_compute_batch_notifier()page_ext_callback()reserve_mem_notiifer()
etc.
So I think worth chasing all of these down?
Some of the switches which don't cover all cases will need to be adjusted to insert a no-op default:
Quoted 35 lines
--- drivers/base/memory.c | 4 ++-- include/linux/memory.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)drivers/base/memory.c +2
-2@@ -204,9 +204,9 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,return sysfs_emit (buf,"%s\n" , output); } -int memory_notify (unsigned long val,void *v) +int memory_notify (enum memory_block_state state,void *v) { -return blocking_notifier_call_chain (&memory_chain, val, v); +return blocking_notifier_call_chain (&memory_chain, state, v); }#if defined(CONFIG_MEMORY_FAILURE ) && defined(CONFIG_MEMORY_HOTPLUG )include/linux/memory.h +1
-1@@ -141,7 +141,7 @@ static inline int register_memory_notifier(struct notifier_block *nb)static inline void unregister_memory_notifier (struct notifier_block *nb) { } -static inline int memory_notify (unsigned long val,void *v) +static inline int memory_notify (enum memory_block_state state,void *v) {return 0 ; }
include/linux/memory.h int create_memory_block_devices(unsigned long start, unsigned long size, · line 168
Quoted 5 lines
include/linux/memory.h +1
-1@@ -165,7 +165,7 @@ int create_memory_block_devices(unsigned long start, unsigned long size, struct memory_group *group);void remove_memory_block_devices (unsigned long start,unsigned long size);extern void memory_dev_init (void ); -extern int memory_notify (unsigned long val,void *v); +extern int memory_notify (enum memory_block_state state,void *v);
No reason you'd know, but we have a pattern of removing extraneous extern's like this when we otherwise change a line.
Quoted 6 lines
include/linux/memory.h +0
-1extern struct memory_block *find_memory_block (unsigned long section_nr);typedef int (*walk_memory_blocks_func_t )(struct memory_block *,void *); extern int walk_memory_blocks(unsigned long start, unsigned long size, --2.51.0
Re: [PATCH v2 3/3] mm: change type of parameter for memory_notify
Quoted 10 lines · Lorenzo Stoakes
This seems fine, but I can see a whole bunch of others like:
kcore_callback()mm_compute_batch_notifier()page_ext_callback()reserve_mem_notiifer()
etc.So I think worth chasing all of these down?
Yeah, I figured there were other cases and was originally planning to include them in the patch series. The problem is they are notifier callbacks and changing the type from unsingned long to enum memory_block_state would break compatibility with the type notifier_fn_t found in include/linux/notifier.h:
typedef int (*notifier_fn_t)(struct notifier_block *nb,
unsigned long action, void *data);
So I think it's not worth the trouble for now.
Quoted 4 lines
Some of the switches which don't cover all cases will need to be adjusted to insert a no-op default:
Good point.
Quoted 6 lines
include/linux/memory.h int create_memory_block_devices(unsigned long start, unsigned long size, · line 168
Quoted 2 lines
include/linux/memory.h +1
-1-extern int memory_notify (unsigned long val,void *v); +extern int memory_notify (enum memory_block_state state,void *v);No reason you'd know, but we have a pattern of removing extraneous extern's like this when we otherwise change a line.
Oh, that's good to know, I'll update it and keep that in mind for my next patches.
Re: [PATCH v2 3/3] mm: change type of parameter for memory_notify
Quoted 20 lines · Israel Batista
Quoted 10 lines · Lorenzo Stoakes
This seems fine, but I can see a whole bunch of others like:
kcore_callback()mm_compute_batch_notifier()page_ext_callback()reserve_mem_notiifer()
etc.So I think worth chasing all of these down?
Yeah, I figured there were other cases and was originally planning to include them in the patch series. The problem is they are notifier callbacks and changing the type from unsingned long to enum
memory_block_statewould break compatibility with the typenotifier_fn_tfound ininclude/linux/notifier.h:
Quoted 4 lines
typedef int (*notifier_fn_t)(struct
notifier_block*nb,unsigned long action, void *data);So I think it's not worth the trouble for now.
We could just cat from unsigned long -> type at the beginning of all these notifiers to have us then work with the actual type.
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v2 3/3] mm: change type of parameter for memory_notify
Quoted 21 lines · David Hildenbrand
Quoted 20 lines · Israel Batista
Quoted 10 lines · Lorenzo Stoakes
This seems fine, but I can see a whole bunch of others like:
kcore_callback()mm_compute_batch_notifier()page_ext_callback()reserve_mem_notiifer()
etc.So I think worth chasing all of these down?
Yeah, I figured there were other cases and was originally planning to include them in the patch series. The problem is they are notifier callbacks and changing the type from unsingned long to enum
memory_block_statewould break compatibility with the typenotifier_fn_tfound ininclude/linux/notifier.h:
Quoted 7 lines
Quoted 4 lines
typedef int (*notifier_fn_t)(struct
notifier_block*nb,unsigned long action, void *data);So I think it's not worth the trouble for now.
We could just cat from unsigned long -> type at the beginning of all these notifiers to have us then work with the actual type.
Right, that works.
Quoted 34 lines · Israel Batista
The MEM_* constants indicating the state of a memory block are currently defined as macros, meaning their definitions will be omitted from the debuginfo on most kernel builds. This makes it harder for debuggers to correctly map the block state at runtime, which can be quite useful when analysing errors related to memory hot plugging and unplugging with tools such as drgn.
Converting the constants to an enum would ensure the correct information is emitted by the compiler and available for the debugger, without needing to hard-code them into the debugger and track their changes.
This patch series aims to replace the current macros with a newly created enum named
memory_block_state, while also taking advantage of the compile time guarantees that we get when using enums.The first patch does the conversion of the macros to an enum, while the 2nd and 3rd patches use this enum to clean up some type declarations and make sure that only valid values are used.
---
v1 -> v2
- Rename the enum to make it more descriptive.
- Let the enum auto-generate the values, as the (1<<X) pattern could be misleading and they're not exposed to userspace.
- Change the type signature from unsigned long to enummemory_block_statewhere suitable.Thanks to everyone who took their time to review the first version.
This patch series applies to commit:
f30d294530d9(mm-new)
Quoted 8 lines
Israel Batista (3):
mm: convert memory block states (MEM_*) macros to enum
mm: change type of state in structmemory_block
mm: change type of parameter formemory_notifydrivers/base/memory.c | 6 +++--- include/linux/memory.h | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-)
I wonder if we need three patches for this, but regardless
Quoted 3 lines
Signature · 1 line
2.51.0
Signature · 2 lines
Sincerely yours, Mike.