Skip to contentSkip to conversation

mm/sparse: clarify pgdat_to_phys

PATCH RESEND Superseded Miles Chen 2 participants 3 messages Jul 23, 2021

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Miles Chen

author
#0 lore raw

PATCH RESEND v2 1/1 mm/sparse: clarify pgdat_to_phys

Clarify pgdat_to_phys() by testing if
pgdat == &contig_page_data when CONFIG_NUMA=n.

contig_page_data is only available when CONFIG_NUMA=n so we have to use #ifndef here.

No functional change intended.

Comment from Mark [1]:
"
... and I reckon it'd be clearer and more robust to define pgdat_to_phys() in the same ifdefs as contig_page_data so that these, stay in-sync. e.g. have:

| #ifdef CONFIG_NUMA
| #define pgdat_to_phys(x)	virt_to_phys(x)
| #else /* CONFIG_NUMA */
|
| extern struct pglist_data contig_page_data;
| ...
| #define pgdat_to_phys(x)	__pa_symbol(&contig_page_data)
|
| #endif /* CONIFIG_NUMA */

"

Comment from Mike [2]:
"
I'm not sure a macro is better than a static inline.

Maybe we'd want to warn if pgdat passed to pgtat_to_phys() is not &contig_page_data, e.g something like

static inline phys_addr_t pgdat_to_phys(struct pglist_data *pgdat)

{
	if (!IS_ENABLED(CONFIG_NUMA)) {
		if (pgdat == &contig_page_data)
			return __pa_symbol(&contig_page_data);
		else
			pr_warn("Unexpected pglist_data pointer!\n");
	}

	return __pa(pgdat);
}

"

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/patchwork/patch/1452903/#1650759

Cc Mike Rapoport <[email protected]>
Cc Mark Rutland <[email protected]>

---

Change since v1:
Thanks for Mike's comment, check if pgdat == &contig_page_data, so it is clearer that we only expect contig_page_data when CONFIG_NUMA=n.

---
 mm/sparse.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/sparse.c +4 -3
index 6326cdf36c4f..f73ff3c124c5 100644
@@ -348,10 +348,11 @@ size_t mem_section_usage_size(void)
 static inline phys_addr_t pgdat_to_phys(struct pglist_data *pgdat)
 {
 #ifndef CONFIG_NUMA
-	return __pa_symbol(pgdat);
-#else
+	if (pgdat == &contig_page_data)
+		return __pa_symbol(&contig_page_data);
+	pr_warn("Unexpected pglist_data pointer!\n");
+#endif /* !CONFIG_NUMA */
 	return __pa(pgdat);
-#endif
 }
 
 #ifdef CONFIG_MEMORY_HOTREMOVE
Signature · 1 line
2.18.0
#1 lore raw
Quoted 67 lines · Miles Chen

Clarify pgdat_to_phys() by testing if
pgdat == &contig_page_data when CONFIG_NUMA=n.

contig_page_data is only available when CONFIG_NUMA=n so we have to use #ifndef here.

No functional change intended.

Comment from Mark [1]:
"
... and I reckon it'd be clearer and more robust to define pgdat_to_phys() in the same ifdefs as contig_page_data so that these, stay in-sync. e.g. have:

| #ifdef CONFIG_NUMA
| #define pgdat_to_phys(x)	virt_to_phys(x)
| #else /* CONFIG_NUMA */
|
| extern struct pglist_data contig_page_data;
| ...
| #define pgdat_to_phys(x)	__pa_symbol(&contig_page_data)
|
| #endif /* CONIFIG_NUMA */

"

Comment from Mike [2]:
"
I'm not sure a macro is better than a static inline.

Maybe we'd want to warn if pgdat passed to pgtat_to_phys() is not &contig_page_data, e.g something like

static inline phys_addr_t pgdat_to_phys(struct pglist_data *pgdat)

{
	if (!IS_ENABLED(CONFIG_NUMA)) {
		if (pgdat == &contig_page_data)
			return __pa_symbol(&contig_page_data);
		else
			pr_warn("Unexpected pglist_data pointer!\n");
	}

	return __pa(pgdat);
}

"

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/patchwork/patch/1452903/#1650759

Cc Mike Rapoport <[email protected]>
Cc Mark Rutland <[email protected]>

---

Change since v1:
Thanks for Mike's comment, check if pgdat == &contig_page_data, so it is clearer that we only expect contig_page_data when CONFIG_NUMA=n.

---
  mm/sparse.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
mm/sparse.c +0 -0
index 6326cdf36c4f..f73ff3c124c5 100644

mm/sparse.c size_t mem_section_usage_size(void) · line 353

Quoted 8 lines
mm/sparse.c +3 -2
@@ -348,10 +348,11 @@ size_t mem_section_usage_size(void)
  static inline phys_addr_t pgdat_to_phys(struct pglist_data *pgdat)
  {
  #ifndef CONFIG_NUMA
-	return __pa_symbol(pgdat);
-#else
+	if (pgdat == &contig_page_data)
+		return __pa_symbol(&contig_page_data);
+	pr_warn("Unexpected pglist_data pointer!\n");

Shouldn't this rather be a VM_BUG_ON()?

Because it looks like something that should barely happen and we might not want to perform runtime checks on each and every system?

Signature · 3 lines
Thanks,

David / dhildenb
author
#2 lore raw
Quoted 79 lines · David Hildenbrand
Quoted 67 lines · Miles Chen

Clarify pgdat_to_phys() by testing if
pgdat == &contig_page_data when CONFIG_NUMA=n.

contig_page_data is only available when CONFIG_NUMA=n so we have to use #ifndef here.

No functional change intended.

Comment from Mark [1]:
"
... and I reckon it'd be clearer and more robust to define pgdat_to_phys() in the same ifdefs as contig_page_data so that these, stay in-sync. e.g. have:

| #ifdef CONFIG_NUMA
| #define pgdat_to_phys(x)	virt_to_phys(x)
| #else /* CONFIG_NUMA */
|
| extern struct pglist_data contig_page_data;
| ...
| #define pgdat_to_phys(x)	__pa_symbol(&contig_page_data)
|
| #endif /* CONIFIG_NUMA */

"

Comment from Mike [2]:
"
I'm not sure a macro is better than a static inline.

Maybe we'd want to warn if pgdat passed to pgtat_to_phys() is not &contig_page_data, e.g something like

static inline phys_addr_t pgdat_to_phys(struct pglist_data *pgdat)

{
	if (!IS_ENABLED(CONFIG_NUMA)) {
		if (pgdat == &contig_page_data)
			return __pa_symbol(&contig_page_data);
		else
			pr_warn("Unexpected pglist_data pointer!\n");
	}

	return __pa(pgdat);
}

"

[1] https://urldefense.com/v3/__https://lore.kernel.org/linux-arm-kernel/[email protected]/__;!!CTRNKA9wMg0ARbw!1bJTnH2gJ6QvsBKTXH8xOiKfHF-xq_1dok3cnETD4e4qjryntFm8K0XjtEmkHAxG6g$
[2] https://urldefense.com/v3/__https://lore.kernel.org/patchwork/patch/1452903/*1650759__;Iw!!CTRNKA9wMg0ARbw!1bJTnH2gJ6QvsBKTXH8xOiKfHF-xq_1dok3cnETD4e4qjryntFm8K0XjtEmbGXEkVg$

Cc Mike Rapoport <[email protected]>
Cc Mark Rutland <[email protected]>

---

Change since v1:
Thanks for Mike's comment, check if pgdat == &contig_page_data, so it is clearer that we only expect contig_page_data when CONFIG_NUMA=n.

---
  mm/sparse.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
mm/sparse.c +0 -0
index 6326cdf36c4f..f73ff3c124c5 100644

mm/sparse.c size_t mem_section_usage_size(void) · line 353

Quoted 8 lines
mm/sparse.c +3 -2
@@ -348,10 +348,11 @@ size_t mem_section_usage_size(void)
  static inline phys_addr_t pgdat_to_phys(struct pglist_data *pgdat)
  {
  #ifndef CONFIG_NUMA
-	return __pa_symbol(pgdat);
-#else
+	if (pgdat == &contig_page_data)
+		return __pa_symbol(&contig_page_data);
+	pr_warn("Unexpected pglist_data pointer!\n");

Shouldn't this rather be a VM_BUG_ON()?

Quoted 2 lines

Because it looks like something that should barely happen and we might not want to perform runtime checks on each and every system?

thanks for your comment, I will use VM_BUG_ON() to check if pgdat == contig_page_data to avoid the runtime check.

Quoted 2 lines