diff mbox series

[v4,1/3] memory: implement memory_block_advise/probe_max_size

Message ID 20241029202041.25334-2-gourry@gourry.net (mailing list archive)
State New
Headers show
Series memory,x86,acpi: hotplug memory alignment advisement | expand

Commit Message

Gregory Price Oct. 29, 2024, 8:20 p.m. UTC
Hotplug memory sources may have opinions on what the memblock size
should be - usually for alignment purposes.  For example, CXL memory
extents can be 256MB with a matching alignment. If this size/alignment
is smaller than the block size, it can result in stranded capacity.

Implement memory_block_advise_max_size for use prior to allocator init,
for software to advise the system on the max block size.

Implement memory_block_probe_max_size for use by arch init code to
calculate the best block size. Use of advice is architecture defined.

The probe value can never change after first probe. Calls to advise
after probe will return -EBUSY to aid debugging.

On systems without hotplug, always return -ENODEV and 0 respectively.

Suggested-by: Ira Weiny <ira.weiny@intel.com>
Signed-off-by: Gregory Price <gourry@gourry.net>
---
 drivers/base/memory.c  | 48 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/memory.h | 10 +++++++++
 2 files changed, 58 insertions(+)

Comments

David Hildenbrand Oct. 30, 2024, 10:25 a.m. UTC | #1
On 29.10.24 21:20, Gregory Price wrote:
> Hotplug memory sources may have opinions on what the memblock size
> should be - usually for alignment purposes.  For example, CXL memory
> extents can be 256MB with a matching alignment. If this size/alignment
> is smaller than the block size, it can result in stranded capacity.
> 
> Implement memory_block_advise_max_size for use prior to allocator init,
> for software to advise the system on the max block size.
> 
> Implement memory_block_probe_max_size for use by arch init code to
> calculate the best block size. Use of advice is architecture defined.
> 
> The probe value can never change after first probe. Calls to advise
> after probe will return -EBUSY to aid debugging.
> 
> On systems without hotplug, always return -ENODEV and 0 respectively.
> 
> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> Signed-off-by: Gregory Price <gourry@gourry.net>
> ---
>   drivers/base/memory.c  | 48 ++++++++++++++++++++++++++++++++++++++++++
>   include/linux/memory.h | 10 +++++++++
>   2 files changed, 58 insertions(+)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index 67858eeb92ed..099a972c52dc 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -110,6 +110,54 @@ static void memory_block_release(struct device *dev)
>   	kfree(mem);
>   }
>   
> +/**
> + * memory_block_advise_max_size() - advise memory hotplug on the max suggested
> + *				    block size, usually for alignment.
> + * @size: suggestion for maximum block size. must be aligned on power of 2.
> + *
> + * Early boot software (pre-allocator init) may advise archs on the max block
> + * size. This value can only decrease after initialization, as the intent is
> + * to identify the largest supported alignment for all sources.
> + *
> + * Use of this value is arch-defined, as is min/max block size.
> + *
> + * Return: 0 on success
> + *	   -EINVAL if size is 0 or not pow2 aligned
> + *	   -EBUSY if value has already been probed
> + */
> +static size_t memory_block_advised_sz;

Nit: if everything is called "size", call this "size" as well.

> +static bool memory_block_advised_size_queried;
> +int memory_block_advise_max_size(size_t size)

Not that memory_block_size_bytes() uses "unsigned long". I don't think 
it matters here. Or could it on 32bit? (I assume that code will not 
really matter on 32bit)

> +{
> +	if (!size || !is_power_of_2(size))
> +		return -EINVAL;
> +
> +	if (memory_block_advised_size_queried)
> +		return -EBUSY;
> +
> +	if (memory_block_advised_sz)
> +		memory_block_advised_sz = min(size, memory_block_advised_sz);
> +	else
> +		memory_block_advised_sz = size;
> +
> +	return 0;
> +}
> +
> +/**
> + * memory_block_advised_max_size() - query advised max hotplug block size.
> + *
> + * After the first call, the value can never change. Callers looking for the
> + * actual block size should use memory_block_size_bytes. This interface is
> + * intended for use by arch-init when initializing the hotplug block size.
> + *
> + * Return: advised size in bytes, or 0 if never set.
> + */
> +size_t memory_block_advised_max_size(void)
> +{
> +	memory_block_advised_size_queried = true;
 > +	return memory_block_advised_sz;> +}
> +

I wonder if both should.could be "__init" ? So they could only be called 
from __init ... which sounds like the tight thing to do?

Acked-by: David Hildenbrand <david@redhat.com>
Gregory Price Oct. 30, 2024, 2:59 p.m. UTC | #2
On Wed, Oct 30, 2024 at 11:25:33AM +0100, David Hildenbrand wrote:
> On 29.10.24 21:20, Gregory Price wrote:
> > Hotplug memory sources may have opinions on what the memblock size
> > should be - usually for alignment purposes.  For example, CXL memory
> > extents can be 256MB with a matching alignment. If this size/alignment
> > is smaller than the block size, it can result in stranded capacity.
> > 
> > Implement memory_block_advise_max_size for use prior to allocator init,
> > for software to advise the system on the max block size.
> > 
> > Implement memory_block_probe_max_size for use by arch init code to
> > calculate the best block size. Use of advice is architecture defined.
> > 
> > The probe value can never change after first probe. Calls to advise
> > after probe will return -EBUSY to aid debugging.
> > 
> > On systems without hotplug, always return -ENODEV and 0 respectively.
> > 
> > Suggested-by: Ira Weiny <ira.weiny@intel.com>
> > Signed-off-by: Gregory Price <gourry@gourry.net>
> > ---
> >   drivers/base/memory.c  | 48 ++++++++++++++++++++++++++++++++++++++++++
> >   include/linux/memory.h | 10 +++++++++
> >   2 files changed, 58 insertions(+)
> > 
> > diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> > index 67858eeb92ed..099a972c52dc 100644
> > --- a/drivers/base/memory.c
> > +++ b/drivers/base/memory.c
> > @@ -110,6 +110,54 @@ static void memory_block_release(struct device *dev)
> >   	kfree(mem);
> >   }
> > +/**
> > + * memory_block_advise_max_size() - advise memory hotplug on the max suggested
> > + *				    block size, usually for alignment.
> > + * @size: suggestion for maximum block size. must be aligned on power of 2.
> > + *
> > + * Early boot software (pre-allocator init) may advise archs on the max block
> > + * size. This value can only decrease after initialization, as the intent is
> > + * to identify the largest supported alignment for all sources.
> > + *
> > + * Use of this value is arch-defined, as is min/max block size.
> > + *
> > + * Return: 0 on success
> > + *	   -EINVAL if size is 0 or not pow2 aligned
> > + *	   -EBUSY if value has already been probed
> > + */
> > +static size_t memory_block_advised_sz;
> 
> Nit: if everything is called "size", call this "size" as well.
> 

Mostly shortened here because

	if (memory_block_advised_sz)
		memory_block_advised_size = min(size, memory_block_advised_size);

is over 80 characters lol.  Happy to change if you have strong feelings.


> > +static bool memory_block_advised_size_queried;
> > +int memory_block_advise_max_size(size_t size)
> 
> Not that memory_block_size_bytes() uses "unsigned long". I don't think it
> matters here. Or could it on 32bit? (I assume that code will not really
> matter on 32bit)
> 

ack

> > +{
> > +	if (!size || !is_power_of_2(size))
> > +		return -EINVAL;
> > +
> > +	if (memory_block_advised_size_queried)
> > +		return -EBUSY;
> > +
> > +	if (memory_block_advised_sz)
> > +		memory_block_advised_sz = min(size, memory_block_advised_sz);
> > +	else
> > +		memory_block_advised_sz = size;
> > +
> > +	return 0;
> > +}
> > +
> > +/**
> > + * memory_block_advised_max_size() - query advised max hotplug block size.
> > + *
> > + * After the first call, the value can never change. Callers looking for the
> > + * actual block size should use memory_block_size_bytes. This interface is
> > + * intended for use by arch-init when initializing the hotplug block size.
> > + *
> > + * Return: advised size in bytes, or 0 if never set.
> > + */
> > +size_t memory_block_advised_max_size(void)
> > +{
> > +	memory_block_advised_size_queried = true;
> > +	return memory_block_advised_sz;> +}
> > +
> 
> I wonder if both should.could be "__init" ? So they could only be called
> from __init ... which sounds like the tight thing to do?
> 

Was thinking the same thing in another thread, will go ahead and change it.

> Acked-by: David Hildenbrand <david@redhat.com>
> 
> -- 
> Cheers,
> 
> David / dhildenb
>
David Hildenbrand Oct. 30, 2024, 4:35 p.m. UTC | #3
On 30.10.24 15:59, Gregory Price wrote:
> On Wed, Oct 30, 2024 at 11:25:33AM +0100, David Hildenbrand wrote:
>> On 29.10.24 21:20, Gregory Price wrote:
>>> Hotplug memory sources may have opinions on what the memblock size
>>> should be - usually for alignment purposes.  For example, CXL memory
>>> extents can be 256MB with a matching alignment. If this size/alignment
>>> is smaller than the block size, it can result in stranded capacity.
>>>
>>> Implement memory_block_advise_max_size for use prior to allocator init,
>>> for software to advise the system on the max block size.
>>>
>>> Implement memory_block_probe_max_size for use by arch init code to
>>> calculate the best block size. Use of advice is architecture defined.
>>>
>>> The probe value can never change after first probe. Calls to advise
>>> after probe will return -EBUSY to aid debugging.
>>>
>>> On systems without hotplug, always return -ENODEV and 0 respectively.
>>>
>>> Suggested-by: Ira Weiny <ira.weiny@intel.com>
>>> Signed-off-by: Gregory Price <gourry@gourry.net>
>>> ---
>>>    drivers/base/memory.c  | 48 ++++++++++++++++++++++++++++++++++++++++++
>>>    include/linux/memory.h | 10 +++++++++
>>>    2 files changed, 58 insertions(+)
>>>
>>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>>> index 67858eeb92ed..099a972c52dc 100644
>>> --- a/drivers/base/memory.c
>>> +++ b/drivers/base/memory.c
>>> @@ -110,6 +110,54 @@ static void memory_block_release(struct device *dev)
>>>    	kfree(mem);
>>>    }
>>> +/**
>>> + * memory_block_advise_max_size() - advise memory hotplug on the max suggested
>>> + *				    block size, usually for alignment.
>>> + * @size: suggestion for maximum block size. must be aligned on power of 2.
>>> + *
>>> + * Early boot software (pre-allocator init) may advise archs on the max block
>>> + * size. This value can only decrease after initialization, as the intent is
>>> + * to identify the largest supported alignment for all sources.
>>> + *
>>> + * Use of this value is arch-defined, as is min/max block size.
>>> + *
>>> + * Return: 0 on success
>>> + *	   -EINVAL if size is 0 or not pow2 aligned
>>> + *	   -EBUSY if value has already been probed
>>> + */
>>> +static size_t memory_block_advised_sz;
>>
>> Nit: if everything is called "size", call this "size" as well.
>>
> 
> Mostly shortened here because
> 
> 	if (memory_block_advised_sz)
> 		memory_block_advised_size = min(size, memory_block_advised_size);
> 
> is over 80 characters lol.  Happy to change if you have strong feelings.

Feel free to exceed 80 chars if there is good reason to -- like in this 
case. checkpatch.pl nowadays complains if you exceed 100 chars.

No strong feelings, making it consistent in some way would be "nice" ;)
Mike Rapoport Oct. 31, 2024, 2:31 p.m. UTC | #4
On Tue, Oct 29, 2024 at 04:20:39PM -0400, Gregory Price wrote:
> Hotplug memory sources may have opinions on what the memblock size
> should be - usually for alignment purposes.  For example, CXL memory
> extents can be 256MB with a matching alignment. If this size/alignment
> is smaller than the block size, it can result in stranded capacity.
> 
> Implement memory_block_advise_max_size for use prior to allocator init,
> for software to advise the system on the max block size.
> 
> Implement memory_block_probe_max_size for use by arch init code to
> calculate the best block size. Use of advice is architecture defined.
> 
> The probe value can never change after first probe. Calls to advise
> after probe will return -EBUSY to aid debugging.
> 
> On systems without hotplug, always return -ENODEV and 0 respectively.
> 
> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> Signed-off-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/base/memory.c  | 48 ++++++++++++++++++++++++++++++++++++++++++
>  include/linux/memory.h | 10 +++++++++
>  2 files changed, 58 insertions(+)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index 67858eeb92ed..099a972c52dc 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -110,6 +110,54 @@ static void memory_block_release(struct device *dev)
>  	kfree(mem);
>  }
>  
> +/**
> + * memory_block_advise_max_size() - advise memory hotplug on the max suggested
> + *				    block size, usually for alignment.
> + * @size: suggestion for maximum block size. must be aligned on power of 2.
> + *
> + * Early boot software (pre-allocator init) may advise archs on the max block
> + * size. This value can only decrease after initialization, as the intent is
> + * to identify the largest supported alignment for all sources.
> + *
> + * Use of this value is arch-defined, as is min/max block size.
> + *
> + * Return: 0 on success
> + *	   -EINVAL if size is 0 or not pow2 aligned
> + *	   -EBUSY if value has already been probed
> + */
> +static size_t memory_block_advised_sz;
> +static bool memory_block_advised_size_queried;

kernel-doc will be unhappy about variable declarations between the doc
block and the function it describes

> +int memory_block_advise_max_size(size_t size)
> +{
> +	if (!size || !is_power_of_2(size))
> +		return -EINVAL;
> +
> +	if (memory_block_advised_size_queried)
> +		return -EBUSY;
> +
> +	if (memory_block_advised_sz)
> +		memory_block_advised_sz = min(size, memory_block_advised_sz);
> +	else
> +		memory_block_advised_sz = size;
> +
> +	return 0;
> +}
> +
> +/**
> + * memory_block_advised_max_size() - query advised max hotplug block size.
> + *
> + * After the first call, the value can never change. Callers looking for the
> + * actual block size should use memory_block_size_bytes. This interface is
> + * intended for use by arch-init when initializing the hotplug block size.
> + *
> + * Return: advised size in bytes, or 0 if never set.
> + */
> +size_t memory_block_advised_max_size(void)
> +{
> +	memory_block_advised_size_queried = true;
> +	return memory_block_advised_sz;
> +}
> +
>  unsigned long __weak memory_block_size_bytes(void)
>  {
>  	return MIN_MEMORY_BLOCK_SIZE;
> diff --git a/include/linux/memory.h b/include/linux/memory.h
> index c0afee5d126e..07e20a77b717 100644
> --- a/include/linux/memory.h
> +++ b/include/linux/memory.h
> @@ -149,6 +149,14 @@ static inline int hotplug_memory_notifier(notifier_fn_t fn, int pri)
>  {
>  	return 0;
>  }
> +static inline int memory_block_advise_max_size(size_t size)
> +{
> +	return -ENODEV;
> +}
> +static inline size_t memory_block_advised_max_size(void)
> +{
> +	return 0;
> +}
>  #else /* CONFIG_MEMORY_HOTPLUG */
>  extern int register_memory_notifier(struct notifier_block *nb);
>  extern void unregister_memory_notifier(struct notifier_block *nb);
> @@ -181,6 +189,8 @@ int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,
>  void memory_block_add_nid(struct memory_block *mem, int nid,
>  			  enum meminit_context context);
>  #endif /* CONFIG_NUMA */
> +int memory_block_advise_max_size(size_t size);
> +size_t memory_block_advised_max_size(void);
>  #endif	/* CONFIG_MEMORY_HOTPLUG */
>  
>  /*
> -- 
> 2.43.0
>
Gregory Price Oct. 31, 2024, 4:23 p.m. UTC | #5
On Thu, Oct 31, 2024 at 04:31:03PM +0200, Mike Rapoport wrote:
> On Tue, Oct 29, 2024 at 04:20:39PM -0400, Gregory Price wrote:
> > + * Return: 0 on success
> > + *	   -EINVAL if size is 0 or not pow2 aligned
> > + *	   -EBUSY if value has already been probed
> > + */
> > +static size_t memory_block_advised_sz;
> > +static bool memory_block_advised_size_queried;
> 
> kernel-doc will be unhappy about variable declarations between the doc
> block and the function it describes
> 

Yup, that was the warning I was waiting to clear KLP.

Learning new things n.n;; - new version shortly

~Gregory
diff mbox series

Patch

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 67858eeb92ed..099a972c52dc 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -110,6 +110,54 @@  static void memory_block_release(struct device *dev)
 	kfree(mem);
 }
 
+/**
+ * memory_block_advise_max_size() - advise memory hotplug on the max suggested
+ *				    block size, usually for alignment.
+ * @size: suggestion for maximum block size. must be aligned on power of 2.
+ *
+ * Early boot software (pre-allocator init) may advise archs on the max block
+ * size. This value can only decrease after initialization, as the intent is
+ * to identify the largest supported alignment for all sources.
+ *
+ * Use of this value is arch-defined, as is min/max block size.
+ *
+ * Return: 0 on success
+ *	   -EINVAL if size is 0 or not pow2 aligned
+ *	   -EBUSY if value has already been probed
+ */
+static size_t memory_block_advised_sz;
+static bool memory_block_advised_size_queried;
+int memory_block_advise_max_size(size_t size)
+{
+	if (!size || !is_power_of_2(size))
+		return -EINVAL;
+
+	if (memory_block_advised_size_queried)
+		return -EBUSY;
+
+	if (memory_block_advised_sz)
+		memory_block_advised_sz = min(size, memory_block_advised_sz);
+	else
+		memory_block_advised_sz = size;
+
+	return 0;
+}
+
+/**
+ * memory_block_advised_max_size() - query advised max hotplug block size.
+ *
+ * After the first call, the value can never change. Callers looking for the
+ * actual block size should use memory_block_size_bytes. This interface is
+ * intended for use by arch-init when initializing the hotplug block size.
+ *
+ * Return: advised size in bytes, or 0 if never set.
+ */
+size_t memory_block_advised_max_size(void)
+{
+	memory_block_advised_size_queried = true;
+	return memory_block_advised_sz;
+}
+
 unsigned long __weak memory_block_size_bytes(void)
 {
 	return MIN_MEMORY_BLOCK_SIZE;
diff --git a/include/linux/memory.h b/include/linux/memory.h
index c0afee5d126e..07e20a77b717 100644
--- a/include/linux/memory.h
+++ b/include/linux/memory.h
@@ -149,6 +149,14 @@  static inline int hotplug_memory_notifier(notifier_fn_t fn, int pri)
 {
 	return 0;
 }
+static inline int memory_block_advise_max_size(size_t size)
+{
+	return -ENODEV;
+}
+static inline size_t memory_block_advised_max_size(void)
+{
+	return 0;
+}
 #else /* CONFIG_MEMORY_HOTPLUG */
 extern int register_memory_notifier(struct notifier_block *nb);
 extern void unregister_memory_notifier(struct notifier_block *nb);
@@ -181,6 +189,8 @@  int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,
 void memory_block_add_nid(struct memory_block *mem, int nid,
 			  enum meminit_context context);
 #endif /* CONFIG_NUMA */
+int memory_block_advise_max_size(size_t size);
+size_t memory_block_advised_max_size(void);
 #endif	/* CONFIG_MEMORY_HOTPLUG */
 
 /*