Re: [PATCH v7 01/10] mm/memory: add memory_block_aligned_range() helper

[email protected] Sun, 12 Jul 2026 15:52:21 +0000
Newsgroups dev.linux.lists.nvdimm,org.kernel.vger.linux-cxl
Message-ID <[email protected]>
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.

> diff --git a/include/linux/memory.h b/include/linux/memory.h
> index 463dc02f6cff0..1783299073e47 100644
> --- a/include/linux/memory.h
> +++ b/include/linux/memory.h
[ ... ]
> @@ -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;

[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?

> +	else
> +		aligned.end -= 1;
> +
> +	return aligned;
> +}
> +
>  struct memory_notify {
>  	unsigned long start_pfn;
>  	unsigned long nr_pages;

-- 
Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=1