Re: [PATCH v2 1/3] x86/numa: Fix SRAT lookup of CFMWS ranges with numa_fill_memblks()

From: Robert Richter
Date: Thu Mar 21 2024 - 18:17:45 EST


Hi Alison,

On 21.03.24 11:39:17, Alison Schofield wrote:
> On Thu, Mar 21, 2024 at 05:55:57PM +0100, Robert Richter wrote:
> > Alison,
> >
> > On 20.03.24 10:46:07, Alison Schofield wrote:
> > > On Tue, Mar 19, 2024 at 01:00:23PM +0100, Robert Richter wrote:
> > > > For configurations that have the kconfig option NUMA_KEEP_MEMINFO
> > > > disabled, the SRAT lookup done with numa_fill_memblks() fails
> > > > returning NUMA_NO_MEMBLK (-1). An existing SRAT memory range cannot be
> > > > found for a CFMWS address range. This causes the addition of a
> > > > duplicate numa_memblk with a different node id and a subsequent page
> > > > fault and kernel crash during boot.
> > > >
> > > > numa_fill_memblks() is implemented and used in the init section only.
> > > > The option NUMA_KEEP_MEMINFO is only for the case when NUMA data will
> > > > be used outside of init. So fix the SRAT lookup by moving
> > > > numa_fill_memblks() out of the NUMA_KEEP_MEMINFO block to make it
> > > > always available in the init section.
> > > >
> > > > Note that the issue was initially introduced with [1]. But since
> > > > phys_to_target_node() was originally used that returned the valid node
> > > > 0, an additional numa_memblk was not added. Though, the node id was
> > > > wrong too.
> > >
> > > Hi Richard,
> > >
> > > I recall a bit of wrangling w #defines to make ARM64 and LOONGARCH build.
> > > I'm seeing an x86 build error today:
> > >
> > > >> arch/x86/mm/numa.c:957:12: error: redefinition of 'numa_fill_memblks'
> > > 957 | int __init numa_fill_memblks(u64 start, u64 end)
> > >
> > > include/linux/numa.h:40:26: note: previous definition of 'numa_fill_memblks' with type
> > > +'int(u64, u64)' {aka 'int(long long unsigned int, long long unsigned int)'}
> > > 40 | static inline int __init numa_fill_memblks(u64 start, u64 end)
> > > | ^~~~~~~~~~~~~~~~~
> > >
> > > In addition to what you suggest, would something like this diff below be
> > > a useful safety measure to distinguish num_fill_memblks() success (rc:0)
> > > and possible non-existence (rc:-1). I don't think it hurts to take a
> > > second look using phys_to_target_node() (totall untested)
> > >
> > > diff --git a/drivers/acpi/numa/srat.c b/drivers/acpi/numa/srat.c
> > > index 070a52e4daa8..0c48fe32ced4 100644
> > > --- a/drivers/acpi/numa/srat.c
> > > +++ b/drivers/acpi/numa/srat.c
> > > @@ -437,9 +437,16 @@ static int __init acpi_parse_cfmws(union acpi_subtable_headers *header,
> > > * found for any portion of the window to cover the entire
> > > * window.
> > > */
> > > - if (!numa_fill_memblks(start, end))
> > > + rc = numa_fill_memblks(start, end);
> > > + if (!rc)
> > > return 0;
> > >
> > > + if (rc == NUMA_NO_MEMBLK) {
> > > + node = phys_to_target_node(start);
> > > + if (node != NUMA_NO_NODE)
> > > + return 0;
> > > + }
> > > +
> >
> > for non-x86 the numa_add_memblk() function looks good in a way that it
> > is able to handle presumable overlapping regions. numa_fill_memblks()
> > would just fail then and numa_add_memblk() being called. For x86 we
> > need numa_fill_memblks() since x86 specific numa_add_memblk() cannot
> > handle the overlapping case.
> >
> > That said, we do not need the 2nd check. It looks to me that it
> > actually breaks non-x86 as the whole block may not be registered (if
> > it is larger than anything existing).
> >
> > For x86 the 2nd check may never happen if numa_fill_memblks() is
> > always enabled (which is this patch for).
>
> Hi Robert, (<-- got it right this time ;))

no worries. :-)

>
> I wasn't thinking of x86, but rather archs that may not support
> numa_fill_memblks() and return NUMA_NO_MEMBLK (-1) per the
> #ifndef numa_fill_memblks in include/linux/numa.h
>
> In those cases, take a second look at phys_to_targe_node() before
> blindly adding another memblk. Is that the failure signature you
> reported here?

No, I am seeing this on x86 with NUMA_KEEP_MEMINFO disabled.
numa_fill_memblks() is not implemented then and returns
NUMA_NO_MEMBLK. numa_add_memblk() (the x86 variant) is called for an
existing range and it adds a duplicate memblock for the same range but
with a different nid, which causes a page fault.

For the non-x86 generic variant of numa_add_memblk() it looks like it
can handle already existing mem blocks within the range and thus does
not need numa_fill_memblks() or the phys_to_target_node() check. Using
phys_to_target_node() would be actually a bug since this always
returns node 0 no matter if there is already a memblock or not. A mem
block for a CFMWS range would never be initialized by calling
numa_add_memblk(), no matter if the range does not exist at all or if
it is partially (at the end) missing.

> I can wait and see your final patch and how the different archs
> will handle it. I'm worried that NUMA_NO_MEMBLK is overloaded and
> we need to diffentiate between archs that don't even look for a
> node, versus archs that look but don't find a node.

This only happens to archs with ACPI_NUMA enabled which is arm64 and
loongarch. As said, numa_add_memblk() handles overlapping ranges so it
is ok to just call it multiple times for the whole or a partional
range.

See below for the full diff of this patch that I will send with a v3
(need to take care at the other review comments yet before sending
it). It just changes sparsemem.h too.

-Robert


diff --git a/arch/x86/include/asm/sparsemem.h b/arch/x86/include/asm/sparsemem.h
index 1be13b2dfe8b..1aaa447ef24b 100644
--- a/arch/x86/include/asm/sparsemem.h
+++ b/arch/x86/include/asm/sparsemem.h
@@ -37,9 +37,9 @@ extern int phys_to_target_node(phys_addr_t start);
#define phys_to_target_node phys_to_target_node
extern int memory_add_physaddr_to_nid(u64 start);
#define memory_add_physaddr_to_nid memory_add_physaddr_to_nid
+#endif
extern int numa_fill_memblks(u64 start, u64 end);
#define numa_fill_memblks numa_fill_memblks
-#endif
#endif /* __ASSEMBLY__ */

#endif /* _ASM_X86_SPARSEMEM_H */
diff --git a/arch/x86/mm/numa.c b/arch/x86/mm/numa.c
index 65e9a6e391c0..ce84ba86e69e 100644
--- a/arch/x86/mm/numa.c
+++ b/arch/x86/mm/numa.c
@@ -929,6 +929,8 @@ int memory_add_physaddr_to_nid(u64 start)
}
EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);

+#endif
+
static int __init cmp_memblk(const void *a, const void *b)
{
const struct numa_memblk *ma = *(const struct numa_memblk **)a;
@@ -1001,5 +1003,3 @@ int __init numa_fill_memblks(u64 start, u64 end)
}
return 0;
}
-
-#endif