Skip to contentSkip to conversation

powerpc/numa: Introduce logical numa id

RFC PATCH Superseded Aneesh Kumar K.V 4 participants 14 messages Jul 31, 2020 – Aug 13, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 powerpc/numa: Introduce logical numa id

We use ibm,associativity and ibm,associativity-lookup-arrays to derive the numa node numbers. These device tree properties are firmware indicated grouping of resources based on their hierarchy in the platform. These numbers (group id) are not sequential and hypervisor/firmware can follow different numbering schemes. For ex: on powernv platforms, we group them in the below order.

 *     - CCM node ID
 *     - HW card ID
 *     - HW module ID
 *     - Chip ID
 *     - Core ID

Based on ibm,associativity-reference-points we use one of the above group ids as Linux NUMA node id. (On PowerNV platform Chip ID is used). This results in Linux reporting non-linear NUMA node id and which also results in Linux reporting empty node 0 NUMA nodes.

This can be resolved by mapping the firmware provided group id to a logical Linux NUMA id. In this patch, we do this only for pseries platforms considering the firmware group id is a virtualized entity and users would not have drawn any conclusion based on the Linux Numa Node id.

On PowerNV platform since we have historically mapped Chip ID as Linux NUMA node id, we keep the existing Linux NUMA node id numbering.

Before Fix:

 # numactl -H
available: 2 nodes (0-1)
node 0 cpus:
node 0 size: 0 MB
node 0 free: 0 MB
node 1 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
node 1 size: 50912 MB
node 1 free: 45248 MB
node distances:
node   0   1
  0:  10  40
  1:  40  10

after fix

 # numactl  -H
available: 1 nodes (0)
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
node 0 size: 50912 MB
node 0 free: 49724 MB
node distances:
node   0
  0:  10
---
 arch/powerpc/include/asm/topology.h |  1 +
 arch/powerpc/mm/numa.c              | 49 ++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 11 deletions(-)
arch/powerpc/include/asm/topology.h +1 -0
index f0b6300e7dd3..15b0424a27a8 100644
@@ -118,5 +118,6 @@ int get_physical_package_id(int cpu);
 #endif
 #endif
 
+int firmware_group_id_to_nid(int firmware_gid);
 #endif /* __KERNEL__ */
 #endif	/* _ASM_POWERPC_TOPOLOGY_H */
arch/powerpc/mm/numa.c +38 -11

87 lines not on this page · raw message

Signature · 1 line
2.26.2
#1 lore raw

RFC PATCH 2/2 powerpc/powernv/cpufreq: Don't assume chip id is same as Linux node id

On PowerNV platforms we always have 1:1 mapping between chip ID and firmware group id. Use the helper to convert firmware group id to node id instead of directly using chip ID as Linux node id.

NOTE: This doesn't have any functional change. On PowerNV platforms we continue to have 1:1 mapping between firmware group id and Linux node id.

---
 drivers/cpufreq/powernv-cpufreq.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/cpufreq/powernv-cpufreq.c +8 -1
index 8646eb197cd9..ca82b6ac0989 100644
@@ -27,6 +27,7 @@
 #include <asm/reg.h>
 #include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
 #include <asm/opal.h>
+#include <asm/topology.h>
 #include <linux/timer.h>
 
 #define POWERNV_MAX_PSTATES_ORDER  8
@@ -1069,8 +1070,14 @@ static int init_chip_info(void)
 	}
 
 	for (i = 0; i < nr_chips; i++) {
+		unsigned int nid;
+
 		chips[i].id = chip[i];
-		cpumask_copy(&chips[i].mask, cpumask_of_node(chip[i]));
+		/*
+		 * On powervn platforms firmware group id is same as chipd id.
+		 */
+		nid = firmware_group_id_to_nid(chip[i]);
+		cpumask_copy(&chips[i].mask, cpumask_of_node(nid));
 		INIT_WORK(&chips[i].throttle, powernv_cpufreq_work_fn);
 		for_each_cpu(cpu, &chips[i].mask)
 			per_cpu(chip_info, cpu) =  &chips[i];
Signature · 1 line
2.26.2
Reviewed-by on v1 2/2
#2 lore raw

Re: [RFC PATCH 2/2] powerpc/powernv/cpufreq: Don't assume chip id is same as Linux node id

Quoted 26 lines · Aneesh Kumar K.V

On PowerNV platforms we always have 1:1 mapping between chip ID and firmware group id. Use the helper to convert firmware group id to node id instead of directly using chip ID as Linux node id.

NOTE: This doesn't have any functional change. On PowerNV platforms we continue to have 1:1 mapping between firmware group id and Linux node id.

---
 drivers/cpufreq/powernv-cpufreq.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/cpufreq/powernv-cpufreq.c +1 -0
index 8646eb197cd9..ca82b6ac0989 100644
@@ -27,6 +27,7 @@
 #include <asm/reg.h>
 #include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
 #include <asm/opal.h>
+#include <asm/topology.h>
 #include <linux/timer.h>
 
 #define POWERNV_MAX_PSTATES_ORDER  8

drivers/cpufreq/powernv-cpufreq.c static int init_chip_info(void) · line 1077

Quoted 9 lines
drivers/cpufreq/powernv-cpufreq.c +4 -1
@@ -1069,8 +1070,14 @@ static int init_chip_info(void)
 	}
 
 	for (i = 0; i < nr_chips; i++) {
+		unsigned int nid;
+
 		chips[i].id = chip[i];
-		cpumask_copy(&chips[i].mask, cpumask_of_node(chip[i]));
+		/*
+		 * On powervn platforms firmware group id is same as chipd id.

But doesn't hurt to be safe :-)

Quoted 9 lines
drivers/cpufreq/powernv-cpufreq.c +3 -0
+		 */
+		nid = firmware_group_id_to_nid(chip[i]);
+		cpumask_copy(&chips[i].mask, cpumask_of_node(nid));
 		INIT_WORK(&chips[i].throttle, powernv_cpufreq_work_fn);
 		for_each_cpu(cpu, &chips[i].mask)
 			per_cpu(chip_info, cpu) =  &chips[i];
Signature · 1 line
2.26.2
#3 lore raw
In reply to Aneesh Kumar K.V
Quoted 12 lines · Aneesh Kumar K.V

We use ibm,associativity and ibm,associativity-lookup-arrays to derive the numa node numbers. These device tree properties are firmware indicated grouping of resources based on their hierarchy in the platform. These numbers (group id) are not sequential and hypervisor/firmware can follow different numbering schemes. For ex: on powernv platforms, we group them in the below order.

 *     - CCM node ID
 *     - HW card ID
 *     - HW module ID
 *     - Chip ID
 *     - Core ID
Quoted 5 lines

Based on ibm,associativity-reference-points we use one of the above group ids as Linux NUMA node id. (On PowerNV platform Chip ID is used). This results in Linux reporting non-linear NUMA node id and which also results in Linux reporting empty node 0 NUMA nodes.

If its just to eliminate node 0, then we have 2 other probably better solutions.
1. Dont mark node 0 as spl (currently still in mm-tree and a result in linux-next)
2. powerpc specific: explicitly clear node 0 during numa bringup.

Quoted 2 lines

This can be resolved by mapping the firmware provided group id to a logical Linux NUMA id. In this patch, we do this only for pseries platforms considering the

On PowerVM, as you would know the nid is already a logical or a flattened chip-id and not the actual hardware chip-id.

Quoted 64 lines

firmware group id is a virtualized entity and users would not have drawn any conclusion based on the Linux Numa Node id.

On PowerNV platform since we have historically mapped Chip ID as Linux NUMA node id, we keep the existing Linux NUMA node id numbering.

Before Fix:

 # numactl -H
available: 2 nodes (0-1)
node 0 cpus:
node 0 size: 0 MB
node 0 free: 0 MB
node 1 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
node 1 size: 50912 MB
node 1 free: 45248 MB
node distances:
node   0   1
  0:  10  40
  1:  40  10

after fix

 # numactl  -H
available: 1 nodes (0)
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
node 0 size: 50912 MB
node 0 free: 49724 MB
node distances:
node   0
  0:  10
---
 arch/powerpc/include/asm/topology.h |  1 +
 arch/powerpc/mm/numa.c              | 49 ++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 11 deletions(-)
arch/powerpc/include/asm/topology.h +1 -0
index f0b6300e7dd3..15b0424a27a8 100644
@@ -118,5 +118,6 @@ int get_physical_package_id(int cpu);
 #endif
 #endif
 
+int firmware_group_id_to_nid(int firmware_gid);
 #endif /* __KERNEL__ */
 #endif	/* _ASM_POWERPC_TOPOLOGY_H */
arch/powerpc/mm/numa.c +9 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
 	}
 }
 
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};
+
+int firmware_group_id_to_nid(int firmware_gid)
+{
+	static int last_nid = 0;
+
+	/*
+	 * For PowerNV we don't change the node id. This helps to avoid
+	 * confusion w.r.t the expected node ids. On pseries, node numbers

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 242

Quoted 10 lines
arch/powerpc/mm/numa.c +10 -0
+	 * are virtualized. Hence do logical node id for pseries.
+	 */
+	if (!firmware_has_feature(FW_FEATURE_LPAR))
+		return firmware_gid;
+
+	if (firmware_gid ==  -1)
+		return NUMA_NO_NODE;
+
+	if (nid_map[firmware_gid] == NUMA_NO_NODE)
+		nid_map[firmware_gid] = last_nid++;

How do we ensure 2 simultaneous firmware_group_id_to_nid() calls dont end up at this place in parallel?

Quoted 14 lines
arch/powerpc/mm/numa.c +5 -0
+
+	return nid_map[firmware_gid];
+}
+
 /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
  * info is found.
  */
 static int associativity_to_nid(const __be32 *associativity)
 {
 	int nid = NUMA_NO_NODE;
+	int firmware_gid = -1;
 
 	if (!numa_enabled)
 		goto out;

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 263

Quoted 10 lines
arch/powerpc/mm/numa.c +3 -3
 
 	if (of_read_number(associativity, 1) >= min_common_depth)
-		nid = of_read_number(&associativity[min_common_depth], 1);
+		firmware_gid = of_read_number(&associativity[min_common_depth], 1);
 
 	/* POWER4 LPAR uses 0xffff as invalid node */
-	if (nid == 0xffff || nid >= MAX_NUMNODES)
-		nid = NUMA_NO_NODE;
+	if (firmware_gid == 0xffff || firmware_gid >= MAX_NUMNODES)
+		firmware_gid = -1;

Lets assume two or more invocations of associativity_to_nid for the same associativity, end up with -1, In each case aren't giving different nids?

Quoted 16 lines
arch/powerpc/mm/numa.c +4 -3
+
+	nid = firmware_group_id_to_nid(firmware_gid);
 
 	if (nid > 0 &&
-		of_read_number(associativity, 1) >= distance_ref_points_depth) {
+	    of_read_number(associativity, 1) >= distance_ref_points_depth) {
 		/*
 		 * Skip the length field and send start of associativity array
 		 */
@@ -432,24 +458,25 @@ static int of_get_assoc_arrays(struct assoc_arrays *aa)
 static int of_drconf_to_nid_single(struct drmem_lmb *lmb)
 {
 	struct assoc_arrays aa = { .arrays = NULL };
-	int default_nid = NUMA_NO_NODE;
-	int nid = default_nid;
+	int nid = NUMA_NO_NODE, firmware_gid;

arch/powerpc/mm/numa.c static int of_get_assoc_arrays(struct assoc_arrays *aa) · line 469

Quoted 10 lines
arch/powerpc/mm/numa.c +2 -2
 	int rc, index;
 
 	if ((min_common_depth < 0) || !numa_enabled)
-		return default_nid;
+		return NUMA_NO_NODE;
 
 	rc = of_get_assoc_arrays(&aa);
 	if (rc)
-		return default_nid;
+		return NUMA_NO_NODE;

https://lore.kernel.org/linuxppc-dev/[email protected]/t/#u

arch/powerpc/mm/numa.c static int of_get_assoc_arrays(struct assoc_arrays *aa) · line 477

Quoted 11 lines
arch/powerpc/mm/numa.c +3 -3
 	if (min_common_depth <= aa.array_sz &&
 	    !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
 		index = lmb->aa_index * aa.array_sz + min_common_depth - 1;
-		nid = of_read_number(&aa.arrays[index], 1);
+		firmware_gid = of_read_number(&aa.arrays[index], 1);
 
-		if (nid == 0xffff || nid >= MAX_NUMNODES)
-			nid = default_nid;
+		if (firmware_gid == 0xffff || firmware_gid >= MAX_NUMNODES)
+			firmware_gid = -1;

Same case as above, How do we ensure that we return unique nid for a similar assoc_array?

Quoted 8 lines
arch/powerpc/mm/numa.c +2 -0
+
+		nid = firmware_group_id_to_nid(firmware_gid);
 
 		if (nid > 0) {
 			index = lmb->aa_index * aa.array_sz;
Signature · 1 line
2.26.2
Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
#4 lore raw
Quoted 20 lines · Srikar Dronamraju
Quoted 12 lines · Aneesh Kumar K.V

We use ibm,associativity and ibm,associativity-lookup-arrays to derive the numa node numbers. These device tree properties are firmware indicated grouping of resources based on their hierarchy in the platform. These numbers (group id) are not sequential and hypervisor/firmware can follow different numbering schemes. For ex: on powernv platforms, we group them in the below order.

 *     - CCM node ID
 *     - HW card ID
 *     - HW module ID
 *     - Chip ID
 *     - Core ID
Quoted 5 lines

Based on ibm,associativity-reference-points we use one of the above group ids as Linux NUMA node id. (On PowerNV platform Chip ID is used). This results in Linux reporting non-linear NUMA node id and which also results in Linux reporting empty node 0 NUMA nodes.

Quoted 6 lines

If its just to eliminate node 0, then we have 2 other probably better solutions.
1. Dont mark node 0 as spl (currently still in mm-tree and a result in linux-next)
2. powerpc specific: explicitly clear node 0 during numa bringup.

I am not sure I consider them better. But yes, those patches are good and also resolves the node 0 initialization when the firmware didn't indicate the presence of such a node.

This patch in addition make sure that we get the same topolgy report across reboot on a virtualized partitions as longs as the cpu/memory ratio per powervm domains remain the same. This should also help to avoid confusion after an LPM migration once we start applying topology updates.

Quoted 5 lines
Quoted 2 lines

This can be resolved by mapping the firmware provided group id to a logical Linux NUMA id. In this patch, we do this only for pseries platforms considering the

On PowerVM, as you would know the nid is already a logical or a flattened chip-id and not the actual hardware chip-id.

Yes. But then they are derived based on PowerVM resources AKA domains. Now based on the available resource on a system, we could end up with different node numbers with same toplogy across reboots. Making it logical at OS level prevent that.

Quoted 70 lines
Quoted 70 lines

firmware group id is a virtualized entity and users would not have drawn any conclusion based on the Linux Numa Node id.

On PowerNV platform since we have historically mapped Chip ID as Linux NUMA node id, we keep the existing Linux NUMA node id numbering.

Before Fix:

 # numactl -H
available: 2 nodes (0-1)
node 0 cpus:
node 0 size: 0 MB
node 0 free: 0 MB
node 1 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
node 1 size: 50912 MB
node 1 free: 45248 MB
node distances:
node   0   1
  0:  10  40
  1:  40  10

after fix

 # numactl  -H
available: 1 nodes (0)
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
node 0 size: 50912 MB
node 0 free: 49724 MB
node distances:
node   0
  0:  10
---
 arch/powerpc/include/asm/topology.h |  1 +
 arch/powerpc/mm/numa.c              | 49 ++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 11 deletions(-)
arch/powerpc/include/asm/topology.h +1 -0
index f0b6300e7dd3..15b0424a27a8 100644
@@ -118,5 +118,6 @@ int get_physical_package_id(int cpu);
 #endif
 #endif
 
+int firmware_group_id_to_nid(int firmware_gid);
 #endif /* __KERNEL__ */
 #endif	/* _ASM_POWERPC_TOPOLOGY_H */
arch/powerpc/mm/numa.c +14 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
 	}
 }
 
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};
+
+int firmware_group_id_to_nid(int firmware_gid)
+{
+	static int last_nid = 0;
+
+	/*
+	 * For PowerNV we don't change the node id. This helps to avoid
+	 * confusion w.r.t the expected node ids. On pseries, node numbers
+	 * are virtualized. Hence do logical node id for pseries.
+	 */
+	if (!firmware_has_feature(FW_FEATURE_LPAR))
+		return firmware_gid;
+
Quoted 8 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 242

Quoted 5 lines
arch/powerpc/mm/numa.c +5 -0
+	if (firmware_gid ==  -1)
+		return NUMA_NO_NODE;
+
+	if (nid_map[firmware_gid] == NUMA_NO_NODE)
+		nid_map[firmware_gid] = last_nid++;

How do we ensure 2 simultaneous firmware_group_id_to_nid() calls dont end up at this place in parallel?

Do we have a code path where we do that? All the node id init should happen early and there should not be two cpus doing node init at the same time. I might be mistaken. Can you point to the code path where you expect this to be called in parallel?

Quoted 20 lines
Quoted 20 lines
arch/powerpc/mm/numa.c +6 -1
+
+	return nid_map[firmware_gid];
+}
+
 /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
  * info is found.
  */
 static int associativity_to_nid(const __be32 *associativity)
 {
 	int nid = NUMA_NO_NODE;
+	int firmware_gid = -1;
 
 	if (!numa_enabled)
 		goto out;
 
 	if (of_read_number(associativity, 1) >= min_common_depth)
-		nid = of_read_number(&associativity[min_common_depth], 1);
+		firmware_gid = of_read_number(&associativity[min_common_depth], 1);
 
Quoted 9 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 263

Quoted 5 lines
arch/powerpc/mm/numa.c +2 -2
 	/* POWER4 LPAR uses 0xffff as invalid node */
-	if (nid == 0xffff || nid >= MAX_NUMNODES)
-		nid = NUMA_NO_NODE;
+	if (firmware_gid == 0xffff || firmware_gid >= MAX_NUMNODES)
+		firmware_gid = -1;

Lets assume two or more invocations of associativity_to_nid for the same associativity, end up with -1, In each case aren't giving different nids?

I didn't quiet get the comment here. But I assume you are indicating the same one you mentioned above?

Quoted 24 lines
Quoted 24 lines
arch/powerpc/mm/numa.c +5 -4
+
+	nid = firmware_group_id_to_nid(firmware_gid);
 
 	if (nid > 0 &&
-		of_read_number(associativity, 1) >= distance_ref_points_depth) {
+	    of_read_number(associativity, 1) >= distance_ref_points_depth) {
 		/*
 		 * Skip the length field and send start of associativity array
 		 */
@@ -432,24 +458,25 @@ static int of_get_assoc_arrays(struct assoc_arrays *aa)
 static int of_drconf_to_nid_single(struct drmem_lmb *lmb)
 {
 	struct assoc_arrays aa = { .arrays = NULL };
-	int default_nid = NUMA_NO_NODE;
-	int nid = default_nid;
+	int nid = NUMA_NO_NODE, firmware_gid;
 	int rc, index;
 
 	if ((min_common_depth < 0) || !numa_enabled)
-		return default_nid;
+		return NUMA_NO_NODE;
 
Quoted 6 lines

arch/powerpc/mm/numa.c static int of_get_assoc_arrays(struct assoc_arrays *aa) · line 469

Quoted 4 lines
arch/powerpc/mm/numa.c +1 -1
 	rc = of_get_assoc_arrays(&aa);
 	if (rc)
-		return default_nid;
+		return NUMA_NO_NODE;

https://lore.kernel.org/linuxppc-dev/[email protected]/t/#u

Not sure what I should conclude on that. I am changing the function here and would like to make NUMA_NO_NODE as the error return.

Quoted 8 lines
Quoted 8 lines
arch/powerpc/mm/numa.c +1 -1
 	if (min_common_depth <= aa.array_sz &&
 	    !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
 		index = lmb->aa_index * aa.array_sz + min_common_depth - 1;
-		nid = of_read_number(&aa.arrays[index], 1);
+		firmware_gid = of_read_number(&aa.arrays[index], 1);
 
Quoted 7 lines

arch/powerpc/mm/numa.c static int of_get_assoc_arrays(struct assoc_arrays *aa) · line 477

Quoted 4 lines
arch/powerpc/mm/numa.c +2 -2
-		if (nid == 0xffff || nid >= MAX_NUMNODES)
-			nid = default_nid;
+		if (firmware_gid == 0xffff || firmware_gid >= MAX_NUMNODES)
+			firmware_gid = -1;

Same case as above, How do we ensure that we return unique nid for a similar assoc_array?

Can you ellaborate this?

Quoted 9 lines
Quoted 9 lines
arch/powerpc/mm/numa.c +2 -0
+
+		nid = firmware_group_id_to_nid(firmware_gid);
 
 		if (nid > 0) {
 			index = lmb->aa_index * aa.array_sz;
Signature · 1 line
2.26.2

-aneesh

#5 lore raw
Quoted 16 lines · Aneesh Kumar K.V
Quoted 9 lines · Srikar Dronamraju

* Aneesh Kumar K.V <[email protected]> [2020-07-31 16:49:14]:

If its just to eliminate node 0, then we have 2 other probably better solutions.
1. Dont mark node 0 as spl (currently still in mm-tree and a result in linux-next)
2. powerpc specific: explicitly clear node 0 during numa bringup.

I am not sure I consider them better. But yes, those patches are good and also resolves the node 0 initialization when the firmware didn't indicate the presence of such a node.

Quoted 6 lines

This patch in addition make sure that we get the same topolgy report across reboot on a virtualized partitions as longs as the cpu/memory ratio per powervm domains remain the same. This should also help to avoid confusion after an LPM migration once we start applying topology updates.

What do we mean by cpu/memory ratio. The topology across reboot would have changed only if PowerVM would have allocated resources differently by scrambling/unscrambling. We are no more processing topology updates at runtime. As far as I know, after LPM, the source topology is maintained.

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

This can be resolved by mapping the firmware provided group id to a logical Linux NUMA id. In this patch, we do this only for pseries platforms considering the

On PowerVM, as you would know the nid is already a logical or a flattened chip-id and not the actual hardware chip-id.

Yes. But then they are derived based on PowerVM resources AKA domains. Now based on the available resource on a system, we could end up with different node numbers with same toplogy across reboots. Making it logical at OS level prevent that.

The above statement kind of gives an impression, that topology changes across every reboot. We only end up with different node numbers if and only if the underlying topology has changed and that case is very rare. Or am I missing something?

Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
arch/powerpc/mm/numa.c +14 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
 	}
 }
 
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};
+
+int firmware_group_id_to_nid(int firmware_gid)
+{
+	static int last_nid = 0;
+
+	/*
+	 * For PowerNV we don't change the node id. This helps to avoid
+	 * confusion w.r.t the expected node ids. On pseries, node numbers
+	 * are virtualized. Hence do logical node id for pseries.
+	 */
+	if (!firmware_has_feature(FW_FEATURE_LPAR))
+		return firmware_gid;
+
Quoted 8 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 242

Quoted 5 lines
arch/powerpc/mm/numa.c +5 -0
+	if (firmware_gid ==  -1)
+		return NUMA_NO_NODE;
+
+	if (nid_map[firmware_gid] == NUMA_NO_NODE)
+		nid_map[firmware_gid] = last_nid++;

How do we ensure 2 simultaneous firmware_group_id_to_nid() calls dont end up at this place in parallel?

Quoted 5 lines

Do we have a code path where we do that? All the node id init should happen early and there should not be two cpus doing node init at the same time. I might be mistaken. Can you point to the code path where you expect this to be called in parallel?

associativity_to_nid gets called the first time a cpu is being made present from offline. So it need not be in boot path. We may to verify if cpu hotplug, dlpar, operations are synchronized. For example a memory hotadd and cpu hotplug are they synchronized? I am not sure if they are synchronized at this time.

Quoted 31 lines
Quoted 20 lines
Quoted 20 lines
arch/powerpc/mm/numa.c +6 -1
+
+	return nid_map[firmware_gid];
+}
+
 /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
  * info is found.
  */
 static int associativity_to_nid(const __be32 *associativity)
 {
 	int nid = NUMA_NO_NODE;
+	int firmware_gid = -1;
 
 	if (!numa_enabled)
 		goto out;
 
 	if (of_read_number(associativity, 1) >= min_common_depth)
-		nid = of_read_number(&associativity[min_common_depth], 1);
+		firmware_gid = of_read_number(&associativity[min_common_depth], 1);
 
Quoted 9 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 263

Quoted 5 lines
arch/powerpc/mm/numa.c +2 -2
 	/* POWER4 LPAR uses 0xffff as invalid node */
-	if (nid == 0xffff || nid >= MAX_NUMNODES)
-		nid = NUMA_NO_NODE;
+	if (firmware_gid == 0xffff || firmware_gid >= MAX_NUMNODES)
+		firmware_gid = -1;

Lets assume two or more invocations of associativity_to_nid for the same associativity, end up with -1, In each case aren't giving different nids?

Quoted 3 lines

I didn't quiet get the comment here. But I assume you are indicating the same one you mentioned above?

No its not related to the above comment.
We are incrementing the nid_map table for every unique firmware_gid or for every -1 (aka invalid associativities). If there are sufficiently large number of associativities that end up returning invalid associativities, then don't we quickly overflow the nid_map table? Not only about the overflow but a 8 node machine may soon look like a 80 node machine.

Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
#6 lore raw
Quoted 24 lines · Srikar Dronamraju
Quoted 16 lines · Aneesh Kumar K.V
Quoted 9 lines · Srikar Dronamraju

* Aneesh Kumar K.V <[email protected]> [2020-07-31 16:49:14]:

If its just to eliminate node 0, then we have 2 other probably better solutions.
1. Dont mark node 0 as spl (currently still in mm-tree and a result in linux-next)
2. powerpc specific: explicitly clear node 0 during numa bringup.

I am not sure I consider them better. But yes, those patches are good and also resolves the node 0 initialization when the firmware didn't indicate the presence of such a node.

Quoted 6 lines

This patch in addition make sure that we get the same topolgy report across reboot on a virtualized partitions as longs as the cpu/memory ratio per powervm domains remain the same. This should also help to avoid confusion after an LPM migration once we start applying topology updates.

Quoted 4 lines

What do we mean by cpu/memory ratio. The topology across reboot would have changed only if PowerVM would have allocated resources differently by scrambling/unscrambling. We are no more processing topology updates at runtime. As far as I know, after LPM, the source topology is maintained.

A LPAR running with one numa node and 10GB of memory on PowerVM domain 10 will report node 10 and 10GB memory in the current scheme. After LPM migration or a CEC shutdown/reboot if the domain from which the resource allocated becomes 11, then the LPAR will report node 11 and 10GB memory.

Having a logical node number means in the both the above cases we report node 0, 10GB memory.

Quoted 12 lines
Quoted 11 lines
Quoted 6 lines
Quoted 3 lines

This can be resolved by mapping the firmware provided group id to a logical Linux NUMA id. In this patch, we do this only for pseries platforms considering the

On PowerVM, as you would know the nid is already a logical or a flattened chip-id and not the actual hardware chip-id.

Yes. But then they are derived based on PowerVM resources AKA domains. Now based on the available resource on a system, we could end up with different node numbers with same toplogy across reboots. Making it logical at OS level prevent that.

Quoted 4 lines

The above statement kind of gives an impression, that topology changes across every reboot. We only end up with different node numbers if and only if the underlying topology has changed and that case is very rare. Or am I missing something?

IIUC it also depends on availability of resources within the domain at the time of LPAR start.

Quoted 39 lines
Quoted 33 lines
Quoted 24 lines
Quoted 24 lines
arch/powerpc/mm/numa.c +14 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
 	}
 }
 
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};
+
+int firmware_group_id_to_nid(int firmware_gid)
+{
+	static int last_nid = 0;
+
+	/*
+	 * For PowerNV we don't change the node id. This helps to avoid
+	 * confusion w.r.t the expected node ids. On pseries, node numbers
+	 * are virtualized. Hence do logical node id for pseries.
+	 */
+	if (!firmware_has_feature(FW_FEATURE_LPAR))
+		return firmware_gid;
+
Quoted 8 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 242

Quoted 5 lines
arch/powerpc/mm/numa.c +5 -0
+	if (firmware_gid ==  -1)
+		return NUMA_NO_NODE;
+
+	if (nid_map[firmware_gid] == NUMA_NO_NODE)
+		nid_map[firmware_gid] = last_nid++;

How do we ensure 2 simultaneous firmware_group_id_to_nid() calls dont end up at this place in parallel?

Quoted 5 lines

Do we have a code path where we do that? All the node id init should happen early and there should not be two cpus doing node init at the same time. I might be mistaken. Can you point to the code path where you expect this to be called in parallel?

Quoted 5 lines

associativity_to_nid gets called the first time a cpu is being made present from offline. So it need not be in boot path. We may to verify if cpu hotplug, dlpar, operations are synchronized. For example a memory hotadd and cpu hotplug are they synchronized? I am not sure if they are synchronized at this time.

But you don't online cpu or memory to a non existent node post boot right?. If the node is existent we have already initialized the nid_map.

However i am not sure whether we do a parallel initialization of devices. ie, of_device_add getting called in parallel. if it can then we need the below?

@@ -226,6 +226,7 @@ static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE};
int firmware_group_id_to_nid(int firmware_gid)

 {
        static int last_nid = 0;

+ static DEFINE_SPINLOCK(node_id_lock);

/*
* For PowerNV we don't change the node id. This helps to avoid
@@ -238,8 +239,13 @@ int firmware_group_id_to_nid(int firmware_gid)

        if (firmware_gid ==  -1)
                return NUMA_NO_NODE;

- if (nid_map[firmware_gid] == NUMA_NO_NODE)
- nid_map[firmware_gid] = last_nid++;
+ if (nid_map[firmware_gid] == NUMA_NO_NODE) {

+               spin_lock(&node_id_lock);
+               /*  recheck with lock held */
+               if (nid_map[firmware_gid] == NUMA_NO_NODE)

+ nid_map[firmware_gid] = last_nid++;
+ spin_unlock(&node_id_lock);
+ }

        return nid_map[firmware_gid];

}

Quoted 36 lines
Quoted 32 lines
Quoted 21 lines
Quoted 21 lines
arch/powerpc/mm/numa.c +6 -1
+
+	return nid_map[firmware_gid];
+}
+
 /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
  * info is found.
  */
 static int associativity_to_nid(const __be32 *associativity)
 {
 	int nid = NUMA_NO_NODE;
+	int firmware_gid = -1;
 
 	if (!numa_enabled)
 		goto out;
 
 	if (of_read_number(associativity, 1) >= min_common_depth)
-		nid = of_read_number(&associativity[min_common_depth], 1);
+		firmware_gid = of_read_number(&associativity[min_common_depth], 1);
 
Quoted 9 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 263

Quoted 5 lines
arch/powerpc/mm/numa.c +2 -2
 	/* POWER4 LPAR uses 0xffff as invalid node */
-	if (nid == 0xffff || nid >= MAX_NUMNODES)
-		nid = NUMA_NO_NODE;
+	if (firmware_gid == 0xffff || firmware_gid >= MAX_NUMNODES)
+		firmware_gid = -1;

Lets assume two or more invocations of associativity_to_nid for the same associativity, end up with -1, In each case aren't giving different nids?

Quoted 3 lines

I didn't quiet get the comment here. But I assume you are indicating the same one you mentioned above?

Quoted 6 lines

No its not related to the above comment.
We are incrementing the nid_map table for every unique firmware_gid or for every -1 (aka invalid associativities). If there are sufficiently large number of associativities that end up returning invalid associativities, then don't we quickly overflow the nid_map table? Not only about the overflow but a 8 node machine may soon look like a 80 node machine.

Not sure I follow. What does a large number of associativies imply? Are you looking at ibm,associativity-lookup-arrays that got entries which are invalid? Even there we are not parsing the full array, we lookup only a specific firmware_gid (in case of lookup-arrays we use aa_index value from drmem_lmb).

I will also add a las_nid > MAX_NUMNODES check in firmware_group_id_to_nid() to handle the case where we find more numa nodes than MAX_NUMANODES in device tree.

-aneesh

#7 lore raw
Quoted 10 lines · Aneesh Kumar K.V
Quoted 6 lines

associativity_to_nid gets called the first time a cpu is being made present from offline. So it need not be in boot path. We may to verify if cpu hotplug, dlpar, operations are synchronized. For example a memory hotadd and cpu hotplug are they synchronized? I am not sure if they are synchronized at this time.

But you don't online cpu or memory to a non existent node post boot right?. If the node is existent we have already initialized the nid_map.

Not sure what you mean by existent and non-existent. Are you referring to online / offline?

Quoted 19 lines

However i am not sure whether we do a parallel initialization of devices. ie, of_device_add getting called in parallel. if it can then we need the below?

(quoted diff) +1 -2
@@ -226,6 +226,7 @@ static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};
 int firmware_group_id_to_nid(int firmware_gid)
 {
        static int last_nid = 0;
+       static DEFINE_SPINLOCK(node_id_lock);
 
        /*
         * For PowerNV we don't change the node id. This helps to avoid
@@ -238,8 +239,13 @@ int firmware_group_id_to_nid(int firmware_gid)
        if (firmware_gid ==  -1)
                return NUMA_NO_NODE;
 
-       if (nid_map[firmware_gid] == NUMA_NO_NODE)
-               nid_map[firmware_gid] = last_nid++;

Review int firmware_group_id_to_nid(int firmware_gid) · line 251

Quoted 10 lines
(quoted diff) +7 -0
+       if (nid_map[firmware_gid] == NUMA_NO_NODE) {
+               spin_lock(&node_id_lock);
+               /*  recheck with lock held */
+               if (nid_map[firmware_gid] == NUMA_NO_NODE)
+                       nid_map[firmware_gid] = last_nid++;
+               spin_unlock(&node_id_lock);
+       }
 
        return nid_map[firmware_gid];
 }

This should help.

Quoted 5 lines

I will also add a las_nid > MAX_NUMNODES check in firmware_group_id_to_nid() to handle the case where we find more numa nodes than MAX_NUMANODES in device tree.

Okay,

Whats your plan to handle the node distances?
Currently the node distances we compute from the device tree properties are based on distance from node 0. If you rename a different node as node 0, how do you plan to remap the node distances?

Quoted 1 line

-aneesh

Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
#8 lore raw
In reply to Aneesh Kumar K.V

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 224

Quoted 9 lines · Aneesh Kumar K.V
arch/powerpc/mm/numa.c +1 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
 	}
 }
 
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};

It's odd to me to use MAX_NUMNODES for this array when it's going to be indexed not by Linux's logical node IDs but by the platform-provided domain number, which has no relation to MAX_NUMNODES.

Quoted 8 lines
arch/powerpc/mm/numa.c +8 -0
+
+int firmware_group_id_to_nid(int firmware_gid)
+{
+	static int last_nid = 0;
+
+	/*
+	 * For PowerNV we don't change the node id. This helps to avoid
+	 * confusion w.r.t the expected node ids. On pseries, node numbers

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 242

Quoted 10 lines
arch/powerpc/mm/numa.c +10 -0
+	 * are virtualized. Hence do logical node id for pseries.
+	 */
+	if (!firmware_has_feature(FW_FEATURE_LPAR))
+		return firmware_gid;
+
+	if (firmware_gid ==  -1)
+		return NUMA_NO_NODE;
+
+	if (nid_map[firmware_gid] == NUMA_NO_NODE)
+		nid_map[firmware_gid] = last_nid++;

This should at least be bounds-checked in case of domain numbering in excess of MAX_NUMNODES. Or a different data structure should be used? Not sure.

I'd prefer Linux's logical node type not be easily interchangeable with the firmware node/group id type. The firmware type could be something like:

struct affinity_domain {
	u32 val;
};

typedef struct affinity_domain affinity_domain_t;

with appropriate accessors/APIs.

This can prevent a whole class of errors that is currently possible with CPUs, since the logical and "hardware" ID types are both simple integers.

#9 lore raw
Quoted 9 lines · Nathan Lynch
Quoted 8 lines · Aneesh Kumar K.V
arch/powerpc/mm/numa.c +0 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
  	}
  }
 
Quoted 5 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 224

Quoted 1 line
arch/powerpc/mm/numa.c +1 -0
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};

It's odd to me to use MAX_NUMNODES for this array when it's going to be indexed not by Linux's logical node IDs but by the platform-provided domain number, which has no relation to MAX_NUMNODES.

I didn't want to dynamically allocate this. We could fetch "ibm,max-associativity-domains" to find the size for that. The current code do assume firmware group id to not exceed MAX_NUMNODES. Hence kept the array size to be MAX_NUMNODEs. I do agree that it is confusing. May be we can do #define MAX_AFFINITY_DOMAIN MAX_NUMNODES?

Quoted 28 lines
Quoted 9 lines
arch/powerpc/mm/numa.c +8 -0
+
+int firmware_group_id_to_nid(int firmware_gid)
+{
+	static int last_nid = 0;
+
+	/*
+	 * For PowerNV we don't change the node id. This helps to avoid
+	 * confusion w.r.t the expected node ids. On pseries, node numbers

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 242

Quoted 10 lines
arch/powerpc/mm/numa.c +10 -0
+	 * are virtualized. Hence do logical node id for pseries.
+	 */
+	if (!firmware_has_feature(FW_FEATURE_LPAR))
+		return firmware_gid;
+
+	if (firmware_gid ==  -1)
+		return NUMA_NO_NODE;
+
+	if (nid_map[firmware_gid] == NUMA_NO_NODE)
+		nid_map[firmware_gid] = last_nid++;

This should at least be bounds-checked in case of domain numbering in excess of MAX_NUMNODES. Or a different data structure should be used? Not sure.

I'd prefer Linux's logical node type not be easily interchangeable with the firmware node/group id type. The firmware type could be something like:

Quoted 7 lines
struct affinity_domain {
	u32 val;
};

typedef struct affinity_domain affinity_domain_t;

with appropriate accessors/APIs.

That is a good idea. Will use this.

-aneesh

#10 lore raw
Quoted 17 lines · Aneesh Kumar K.V
Quoted 9 lines · Nathan Lynch
Quoted 8 lines · Aneesh Kumar K.V
arch/powerpc/mm/numa.c +0 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
  	}
  }
 
Quoted 5 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 224

Quoted 1 line
arch/powerpc/mm/numa.c +1 -0
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};

It's odd to me to use MAX_NUMNODES for this array when it's going to be indexed not by Linux's logical node IDs but by the platform-provided domain number, which has no relation to MAX_NUMNODES.

Quoted 5 lines

I didn't want to dynamically allocate this. We could fetch "ibm,max-associativity-domains" to find the size for that. The current code do assume firmware group id to not exceed MAX_NUMNODES. Hence kept the array size to be MAX_NUMNODEs. I do agree that it is confusing. May be we can do #define MAX_AFFINITY_DOMAIN MAX_NUMNODES?

Well, consider:

- ibm,max-associativity-domains can change at runtime with LPM. This doesn't happen in practice yet, but we should probably start thinking about how to support that.
- The domain numbering isn't clearly specified to have any particular properties such as beginning at zero or a contiguous range.

While the current code likely contains assumptions contrary to these points, a change such as this is an opportunity to think about whether those assumptions can be reduced or removed. In particular I think it would be good to gracefully degrade when the number of NUMA affinity domains can exceed MAX_NUMNODES. Using the platform-supplied domain numbers to directly index Linux data structures will make that impossible.

So, maybe genradix or even xarray wouldn't actually be overengineering here.

#11 lore raw
Quoted 40 lines · Nathan Lynch
Quoted 17 lines · Aneesh Kumar K.V
Quoted 9 lines · Nathan Lynch
Quoted 8 lines · Aneesh Kumar K.V
arch/powerpc/mm/numa.c +0 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
   	}
   }
 
Quoted 5 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 224

Quoted 1 line
arch/powerpc/mm/numa.c +1 -0
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};

It's odd to me to use MAX_NUMNODES for this array when it's going to be indexed not by Linux's logical node IDs but by the platform-provided domain number, which has no relation to MAX_NUMNODES.

Quoted 5 lines

I didn't want to dynamically allocate this. We could fetch "ibm,max-associativity-domains" to find the size for that. The current code do assume firmware group id to not exceed MAX_NUMNODES. Hence kept the array size to be MAX_NUMNODEs. I do agree that it is confusing. May be we can do #define MAX_AFFINITY_DOMAIN MAX_NUMNODES?

Well, consider:

- ibm,max-associativity-domains can change at runtime with LPM. This doesn't happen in practice yet, but we should probably start thinking about how to support that.
- The domain numbering isn't clearly specified to have any particular properties such as beginning at zero or a contiguous range.

While the current code likely contains assumptions contrary to these points, a change such as this is an opportunity to think about whether those assumptions can be reduced or removed. In particular I think it would be good to gracefully degrade when the number of NUMA affinity domains can exceed MAX_NUMNODES. Using the platform-supplied domain numbers to directly index Linux data structures will make that impossible.

Quoted 3 lines

So, maybe genradix or even xarray wouldn't actually be overengineering here.

One of the challenges with such a data structure is that we initialize the nid_map before the slab is available. This means a memblock based allocation and we would end up implementing such a sparse data structure ourselves here.

As you mentioned above, since we know that hypervisor as of now limits the max affinity domain id below ibm,max-associativity-domains we are good with an array-like nid_map we have here. This keeps the code simpler.

This will also allow us to switch to a more sparse data structure as you requested here in the future because the main change that is pushed in this series is the usage of firmare_group_id_to_nid(). The details of the data structure we use to keep track of that mapping are pretty much internal to that function.

-aneesh

#12 lore raw
Quoted 54 lines · Aneesh Kumar K.V
Quoted 40 lines · Nathan Lynch
Quoted 17 lines · Aneesh Kumar K.V
Quoted 9 lines · Nathan Lynch
Quoted 8 lines · Aneesh Kumar K.V
arch/powerpc/mm/numa.c +0 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
   	}
   }
 
Quoted 5 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 224

Quoted 1 line
arch/powerpc/mm/numa.c +1 -0
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};

It's odd to me to use MAX_NUMNODES for this array when it's going to be indexed not by Linux's logical node IDs but by the platform-provided domain number, which has no relation to MAX_NUMNODES.

Quoted 5 lines

I didn't want to dynamically allocate this. We could fetch "ibm,max-associativity-domains" to find the size for that. The current code do assume firmware group id to not exceed MAX_NUMNODES. Hence kept the array size to be MAX_NUMNODEs. I do agree that it is confusing. May be we can do #define MAX_AFFINITY_DOMAIN MAX_NUMNODES?

Well, consider:

- ibm,max-associativity-domains can change at runtime with LPM. This doesn't happen in practice yet, but we should probably start thinking about how to support that.
- The domain numbering isn't clearly specified to have any particular properties such as beginning at zero or a contiguous range.

While the current code likely contains assumptions contrary to these points, a change such as this is an opportunity to think about whether those assumptions can be reduced or removed. In particular I think it would be good to gracefully degrade when the number of NUMA affinity domains can exceed MAX_NUMNODES. Using the platform-supplied domain numbers to directly index Linux data structures will make that impossible.

Quoted 3 lines

So, maybe genradix or even xarray wouldn't actually be overengineering here.

One of the challenges with such a data structure is that we initialize the nid_map before the slab is available. This means a memblock based allocation and we would end up implementing such a sparse data structure ourselves here.

As you mentioned above, since we know that hypervisor as of now limits the max affinity domain id below ibm,max-associativity-domains we are good with an array-like nid_map we have here. This keeps the code simpler.

Quoted 5 lines

This will also allow us to switch to a more sparse data structure as you requested here in the future because the main change that is pushed in this series is the usage of firmare_group_id_to_nid(). The details of the data structure we use to keep track of that mapping are pretty much internal to that function.

How about this? This makes it not a direct index. But it do limit the search to max numa node on the system.

static int domain_id_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] = -1 };

static int __affinity_domain_to_nid(int domain_id, int max_nid)

{
	int i;

	for (i = 0; i < max_nid; i++) {
		if (domain_id_map[i] == domain_id)
			return i;
	}
	return NUMA_NO_NODE;
}

int affinity_domain_to_nid(struct affinity_domain *domain)

{
	int nid, domain_id;
	static int last_nid = 0;
	static DEFINE_SPINLOCK(node_id_lock);

	domain_id = domain->id;
	/*
	 * For PowerNV we don't change the node id. This helps to avoid
	 * confusion w.r.t the expected node ids. On pseries, node numbers
	 * are virtualized. Hence do logical node id for pseries.
	 */
	if (!firmware_has_feature(FW_FEATURE_LPAR))
		return domain_id;

	if (domain_id ==  -1 || last_nid == MAX_NUMNODES)
		return NUMA_NO_NODE;

	nid = __affinity_domain_to_nid(domain_id, last_nid);

	if (nid == NUMA_NO_NODE) {
		spin_lock(&node_id_lock);
		/*  recheck with lock held */
		nid = __affinity_domain_to_nid(domain_id, last_nid);
		if (nid == NUMA_NO_NODE) {
			nid = last_nid++;
			domain_id_map[nid] = domain_id;
		}
		spin_unlock(&node_id_lock);
	}

	return nid;
}
#13 lore raw

Hi Aneesh,

Quoted 42 lines · Aneesh Kumar K.V
Quoted 41 lines · Aneesh Kumar K.V
Quoted 40 lines · Nathan Lynch
Quoted 17 lines · Aneesh Kumar K.V
Quoted 9 lines · Nathan Lynch
Quoted 8 lines · Aneesh Kumar K.V
arch/powerpc/mm/numa.c +0 -0
index e437a9ac4956..6c659aada55b 100644
@@ -221,25 +221,51 @@ static void initialize_distance_lookup_table(int nid,
   	}
   }
 
Quoted 5 lines

arch/powerpc/mm/numa.c static void initialize_distance_lookup_table(int nid, · line 224

Quoted 1 line
arch/powerpc/mm/numa.c +1 -0
+static u32 nid_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] =  NUMA_NO_NODE};

It's odd to me to use MAX_NUMNODES for this array when it's going to be indexed not by Linux's logical node IDs but by the platform-provided domain number, which has no relation to MAX_NUMNODES.

Quoted 5 lines

I didn't want to dynamically allocate this. We could fetch "ibm,max-associativity-domains" to find the size for that. The current code do assume firmware group id to not exceed MAX_NUMNODES. Hence kept the array size to be MAX_NUMNODEs. I do agree that it is confusing. May be we can do #define MAX_AFFINITY_DOMAIN MAX_NUMNODES?

Well, consider:

- ibm,max-associativity-domains can change at runtime with LPM. This doesn't happen in practice yet, but we should probably start thinking about how to support that.
- The domain numbering isn't clearly specified to have any particular properties such as beginning at zero or a contiguous range.

While the current code likely contains assumptions contrary to these points, a change such as this is an opportunity to think about whether those assumptions can be reduced or removed. In particular I think it would be good to gracefully degrade when the number of NUMA affinity domains can exceed MAX_NUMNODES. Using the platform-supplied domain numbers to directly index Linux data structures will make that impossible.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

So, maybe genradix or even xarray wouldn't actually be overengineering here.

One of the challenges with such a data structure is that we initialize the nid_map before the slab is available. This means a memblock based allocation and we would end up implementing such a sparse data structure ourselves here.

Yes, good point.

Quoted 16 lines
Quoted 9 lines

As you mentioned above, since we know that hypervisor as of now limits the max affinity domain id below ibm,max-associativity-domains we are good with an array-like nid_map we have here. This keeps the code simpler.

This will also allow us to switch to a more sparse data structure as you requested here in the future because the main change that is pushed in this series is the usage of firmare_group_id_to_nid(). The details of the data structure we use to keep track of that mapping are pretty much internal to that function.

How about this? This makes it not a direct index. But it do limit the search to max numa node on the system.

static int domain_id_map[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] = -1 };

static int __affinity_domain_to_nid(int domain_id, int max_nid)

Quoted 9 lines
{
	int i;

	for (i = 0; i < max_nid; i++) {
		if (domain_id_map[i] == domain_id)
			return i;
	}
	return NUMA_NO_NODE;
}

OK, this indexes the array by Linux's node id, good. I was wondering if I could persuade you do flip it around like this :-)

Walking through the code below:

Quoted 11 lines

int affinity_domain_to_nid(struct affinity_domain *domain)

{
	int nid, domain_id;
	static int last_nid = 0;
	static DEFINE_SPINLOCK(node_id_lock);

	domain_id = domain->id;
	/*
	 * For PowerNV we don't change the node id. This helps to avoid
	 * confusion w.r.t the expected node ids. On pseries, node numbers
	 * are virtualized. Hence do logical node id for pseries.
Quoted 8 lines
	 */
	if (!firmware_has_feature(FW_FEATURE_LPAR))
		return domain_id;

	if (domain_id ==  -1 || last_nid == MAX_NUMNODES)
		return NUMA_NO_NODE;

	nid = __affinity_domain_to_nid(domain_id, last_nid);

So this is pseries fast path. Attempt to look up the Linux node for the given domain, where last_nid is the highest-numbered node in use so far. If the result is in [0..last_nid] we're done.

Quoted 3 lines
	if (nid == NUMA_NO_NODE) {
		spin_lock(&node_id_lock);

If the lookup fails enter the critical section. As we discussed offline, this is a precaution for potentially parallel device probing.

Quoted 2 lines
		/*  recheck with lock held */
		nid = __affinity_domain_to_nid(domain_id, last_nid);

Attempt the same lookup again. If the result is in [0..last_nid], another thread has just initialized the mapping for this domain and we're done.

Quoted 4 lines
		if (nid == NUMA_NO_NODE) {
			nid = last_nid++;
			domain_id_map[nid] = domain_id;
		}

If the lookup fails again, "allocate" the next unused Linux node number. Otherwise use the result returned by the second call to __affinity_domain_to_nid().

Quoted 5 lines
		spin_unlock(&node_id_lock);
	}

	return nid;
}

Generally I agree with this approach. I don't quite get the locking. I understand there could be a need for a lockless fast path, but as written I don't think last_nid is appropriately protected - two slow-path threads could cause an increment to be "lost" since last_nid is loaded before taking the lock.