Skip to contentSkip to conversation

mm: Coccinelle-driven cleanups across memory management code

PATCH Superseded Sahil Chandna 6 participants 12 messages Nov 28, 2025 – Jan 30

Subsystems (5)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover mm: Coccinelle-driven cleanups across memory management code

This patch series contains three minor cleanups identified by Coccinelle scripts under scripts/coccinelle/misc/:

* Use ALIGN() to compute the next hugepage boundary instead of the open-coded expression and use min() macro to improve readability.
* Use the %pe printk format specifier for PTR_ERR() reporting in vmscan.c and zswap.c
Found using:
make coccicheck MODE=report M=mm/

change since v3:
- Update the commit message to highlight change in computation of boundary using ALIGN().
changes since v1 and v2:
- Patch 1/3: update return value as per review comments.
- patch 2/3 and 3/3: Add Acked-by and Reviewed-by tags.

link to v1: https://lore.kernel.org/all/[email protected]/
link to v2: https://lore.kernel.org/all/[email protected]/
link to v3: https://lore.kernel.org/all/[email protected]/

Sahil Chandna (3):
mm: pagewalk: simplify hugepage boundary calculation mm/vmscan: use %pe to print error pointers mm/zswap: use %pe to print error pointers

 mm/pagewalk.c | 3 +--
 mm/vmscan.c   | 4 ++--
 mm/zswap.c    | 4 ++--
 3 files changed, 5 insertions(+), 6 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v4 1/3 mm: pagewalk: simplify hugepage boundary calculation

Replace hugepage boundary computation with ALIGN() helper instead of an open coded expression. This helps to improves code readability.

This was flagged by Coccinelle (misc/minmax.cocci) as an opportunity to use min(), after which the boundary computation was updated following review suggestions.

Found by: make coccicheck MODE=report M=mm/
No functional change intended.

---
 mm/pagewalk.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/pagewalk.c +1 -2
index 9f91cf85a5be..9fd59d517f37 100644
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
 static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
 				       unsigned long end)
 {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
 }
 
 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
Signature · 1 line
2.50.1
reviewer
#2 lore raw

Re: [PATCH v4 1/3] mm: pagewalk: simplify hugepage boundary calculation

Hi Andrew,

Quoted 24 lines · Sahil Chandna

Replace hugepage boundary computation with ALIGN() helper instead of an open coded expression. This helps to improves code readability.

This was flagged by Coccinelle (misc/minmax.cocci) as an opportunity to use min(), after which the boundary computation was updated following review suggestions.

Found by: make coccicheck MODE=report M=mm/
No functional change intended.

---
  mm/pagewalk.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
mm/pagewalk.c +0 -0
index 9f91cf85a5be..9fd59d517f37 100644

mm/pagewalk.c static int walk_pgd_range(unsigned long addr, unsigned long end, · line 316

Quoted 7 lines
mm/pagewalk.c +1 -2
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
  static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
  				       unsigned long end)
  {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
  }

Please drop this patch from the mm-new branch, as it causes 'run_vmtests.sh' to hang. Specifically, it leads to the system hanging when executing hugepage-vmemmap test, because the program falls into an infinite loop in walk_hugetlb_range() and cannot break out.

This patch does introduce functional changes and makes an incorrect assumption that the 'end' must be aligned to the hugepage size. However, this is not necessarily the case. For example, see how pagemap_read() calculates the 'end':

"
end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT); "

Revert this patch, mm selftests work well.

#3 lore raw

Re: [PATCH v4 1/3] mm: pagewalk: simplify hugepage boundary

From: Lance Yang <[email protected]>

Quoted 31 lines · Baolin Wang

Hi Andrew,

Quoted 28 lines · Sahil Chandna

Replace hugepage boundary computation with ALIGN() helper instead of an open coded expression. This helps to improves code readability.

This was flagged by Coccinelle (misc/minmax.cocci) as an opportunity to use min(), after which the boundary computation was updated following review suggestions.

Found by: make coccicheck MODE=report M=mm/
No functional change intended.

---
  mm/pagewalk.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
mm/pagewalk.c +0 -1
index 9f91cf85a5be..9fd59d517f37 100644
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
  static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
  				       unsigned long end)
  {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
Quoted 8 lines

mm/pagewalk.c static int walk_pgd_range(unsigned long addr, unsigned long end, · line 316

Quoted 3 lines
mm/pagewalk.c +1 -1
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
  }

Please drop this patch from the mm-new branch, as it causes 'run_vmtests.sh' to hang. Specifically, it leads to the system hanging when executing hugepage-vmemmap test, because the program falls into an infinite loop in walk_hugetlb_range() and cannot break out.

Good catch! The problem is that ALIGN() returns addr itself when already aligned, causing the infinite loop ...

Quoted 3 lines

This patch does introduce functional changes and makes an incorrect assumption that the 'end' must be aligned to the hugepage size. However,

Yep. This patch is not equivalent to the original code when addr is already aligned :)

Quoted 8 lines

this is not necessarily the case. For example, see how pagemap_read() calculates the 'end':

"
end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT); "

Revert this patch, mm selftests work well.

#4 lore raw

Re: [PATCH v4 1/3] mm: pagewalk: simplify hugepage boundary

Quoted 48 lines · Lance Yang

From: Lance Yang <[email protected]>

Quoted 31 lines · Baolin Wang

Hi Andrew,

Quoted 28 lines · Sahil Chandna

Replace hugepage boundary computation with ALIGN() helper instead of an open coded expression. This helps to improves code readability.

This was flagged by Coccinelle (misc/minmax.cocci) as an opportunity to use min(), after which the boundary computation was updated following review suggestions.

Found by: make coccicheck MODE=report M=mm/
No functional change intended.

---
  mm/pagewalk.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
mm/pagewalk.c +0 -1
index 9f91cf85a5be..9fd59d517f37 100644
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
  static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
  				       unsigned long end)
  {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
Quoted 8 lines

mm/pagewalk.c static int walk_pgd_range(unsigned long addr, unsigned long end, · line 316

Quoted 3 lines
mm/pagewalk.c +1 -1
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
  }

Please drop this patch from the mm-new branch, as it causes 'run_vmtests.sh' to hang. Specifically, it leads to the system hanging when executing hugepage-vmemmap test, because the program falls into an infinite loop in walk_hugetlb_range() and cannot break out.

Good catch! The problem is that ALIGN() returns addr itself when already aligned, causing the infinite loop ...

Quoted 1 line
Quoted 14 lines
Quoted 2 lines

This patch does introduce functional changes and makes an incorrect assumption that the 'end' must be aligned to the hugepage size. However,

Yep. This patch is not equivalent to the original code when addr is already aligned :)

Quoted 8 lines

this is not necessarily the case. For example, see how pagemap_read() calculates the 'end':

"
end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT); "

Revert this patch, mm selftests work well.

Hi Baolin, Lance, Andrew,
Thanks for catching this, I understand why ALIGN() caused an infinite loop.
Please drop this patch, or shall I submit the revert ? Apologies for this, I am setting up environment for running the selftest and will send out corrected patch once it successfully pass mm selftests.

Regards,
Sahil

#5 lore raw

Re: [PATCH v4 1/3] mm: pagewalk: simplify hugepage boundary

In reply to Lance Yang
Quoted 44 lines · On Wed, 24 Dec 2025 17:23:32 +0800 Lance Yang <[email protected]> wrote:

From: Lance Yang <[email protected]>

Quoted 31 lines · Baolin Wang

Hi Andrew,

Quoted 28 lines · Sahil Chandna

Replace hugepage boundary computation with ALIGN() helper instead of an open coded expression. This helps to improves code readability.

This was flagged by Coccinelle (misc/minmax.cocci) as an opportunity to use min(), after which the boundary computation was updated following review suggestions.

Found by: make coccicheck MODE=report M=mm/
No functional change intended.

---
  mm/pagewalk.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
mm/pagewalk.c +0 -1
index 9f91cf85a5be..9fd59d517f37 100644
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
  static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
  				       unsigned long end)
  {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
Quoted 8 lines

mm/pagewalk.c static int walk_pgd_range(unsigned long addr, unsigned long end, · line 316

Quoted 3 lines
mm/pagewalk.c +1 -1
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
  }  

Please drop this patch from the mm-new branch, as it causes 'run_vmtests.sh' to hang. Specifically, it leads to the system hanging when executing hugepage-vmemmap test, because the program falls into an infinite loop in walk_hugetlb_range() and cannot break out.

Quoted 2 lines

Good catch! The problem is that ALIGN() returns addr itself when already aligned, causing the infinite loop ...

Using ALIGN(addr + 1, huge_page_size(h)) would work. Although it could be (addr + 1) & ~huge_page_mask(h) which is probably the easiest to understand.
Some of the 'helper' macros don't really make the code easier to read. (And that includes a lot of uses of min().)

David

Quoted 16 lines
Quoted 4 lines

This patch does introduce functional changes and makes an incorrect assumption that the 'end' must be aligned to the hugepage size. However,

Yep. This patch is not equivalent to the original code when addr is already aligned :)

Quoted 8 lines

this is not necessarily the case. For example, see how pagemap_read() calculates the 'end':

"
end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT); "

Revert this patch, mm selftests work well.

#6 lore raw

Re: [PATCH v4 1/3] mm: pagewalk: simplify hugepage boundary

Quoted 15 lines · David Laight
Quoted 15 lines
Quoted 14 lines

mm/pagewalk.c static int walk_pgd_range(unsigned long addr, unsigned long end, · line 316

Quoted 9 lines

+++ b/mm/pagewalk.c

mm/pagewalk.c +1 -2
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
  static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
  				       unsigned long end)
  {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
  }  

Please drop this patch from the mm-new branch, as it causes 'run_vmtests.sh' to hang. Specifically, it leads to the system hanging when executing hugepage-vmemmap test, because the program falls into an infinite loop in walk_hugetlb_range() and cannot break out.

Quoted 8 lines
Quoted 2 lines

Good catch! The problem is that ALIGN() returns addr itself when already aligned, causing the infinite loop ...

Using ALIGN(addr + 1, huge_page_size(h)) would work. Although it could be (addr + 1) & ~huge_page_mask(h) which is probably the easiest to understand.
Some of the 'helper' macros don't really make the code easier to read. (And that includes a lot of uses of min().)

Or we could go back to my original suggestion.

https://lore.kernel.org/linux-mm/[email protected]/

which was in v2:

https://lore.kernel.org/linux-mm/f802959f58865371ba1b10081bced98e3784c5e4.1763796152.git.chandna.sahil@gmail.com/

#7 lore raw

Re: [PATCH v4 1/3] mm: pagewalk: simplify hugepage boundary

Quoted 25 lines · Matthew Wilcox
Quoted 15 lines · David Laight
Quoted 15 lines
Quoted 14 lines

mm/pagewalk.c static int walk_pgd_range(unsigned long addr, unsigned long end, · line 316

Quoted 9 lines

+++ b/mm/pagewalk.c

mm/pagewalk.c +1 -2
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
   static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
   				       unsigned long end)
   {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
   }

Please drop this patch from the mm-new branch, as it causes 'run_vmtests.sh' to hang. Specifically, it leads to the system hanging when executing hugepage-vmemmap test, because the program falls into an infinite loop in walk_hugetlb_range() and cannot break out.

Quoted 8 lines
Quoted 2 lines

Good catch! The problem is that ALIGN() returns addr itself when already aligned, causing the infinite loop ...

Using ALIGN(addr + 1, huge_page_size(h)) would work. Although it could be (addr + 1) & ~huge_page_mask(h) which is probably the easiest to understand.
Some of the 'helper' macros don't really make the code easier to read. (And that includes a lot of uses of min().)

Quoted 7 lines

Or we could go back to my original suggestion.

https://lore.kernel.org/linux-mm/[email protected]/

which was in v2:

https://lore.kernel.org/linux-mm/f802959f58865371ba1b10081bced98e3784c5e4.1763796152.git.chandna.sahil@gmail.com/

I'm starting to wonder whether we should just leave that code alone :)

Signature · 3 lines
Cheers

David
#8 lore raw

Re: [PATCH v4 1/3] mm: pagewalk: simplify hugepage boundary

Quoted 28 lines · On Thu, 25 Dec 2025 10:32:46 +0100 "David Hildenbrand (Red Hat)"…
Quoted 27 lines · Matthew Wilcox
Quoted 15 lines · David Laight
Quoted 15 lines
Quoted 14 lines

mm/pagewalk.c static int walk_pgd_range(unsigned long addr, unsigned long end, · line 316

Quoted 9 lines

+++ b/mm/pagewalk.c

mm/pagewalk.c +1 -2
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
   static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
   				       unsigned long end)
   {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
   }  

Please drop this patch from the mm-new branch, as it causes 'run_vmtests.sh' to hang. Specifically, it leads to the system hanging when executing hugepage-vmemmap test, because the program falls into an infinite loop in walk_hugetlb_range() and cannot break out.

Quoted 8 lines
Quoted 2 lines

Good catch! The problem is that ALIGN() returns addr itself when already aligned, causing the infinite loop ...

Using ALIGN(addr + 1, huge_page_size(h)) would work. Although it could be (addr + 1) & ~huge_page_mask(h) which is probably the easiest to understand.
Some of the 'helper' macros don't really make the code easier to read. (And that includes a lot of uses of min().)

Or we could go back to my original suggestion.

Quoted 8 lines
Quoted 5 lines

https://lore.kernel.org/linux-mm/[email protected]/

which was in v2:

https://lore.kernel.org/linux-mm/f802959f58865371ba1b10081bced98e3784c5e4.1763796152.git.chandna.sahil@gmail.com/

I'm starting to wonder whether we should just leave that code alone :)

Maybe 'we' should stop checkpatch (etc) suggesting min() in trivial cases. It doesn't really make the code better.

David

#9 lore raw

Re: [PATCH v4 1/3] mm: pagewalk: simplify hugepage boundary

Quoted 37 lines · David Laight
Quoted 34 lines · On Thu, 25 Dec 2025 10:32:46 +0100 "David Hildenbrand (Red Hat)"…
Quoted 32 lines · Matthew Wilcox
Quoted 15 lines · David Laight
Quoted 15 lines
Quoted 14 lines

Review static int walk_pgd_range(unsigned long addr, unsigned long end, · line 316

Quoted 9 lines

+++ b/mm/pagewalk.c

(quoted diff) +1 -2
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
   static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
   				       unsigned long end)
   {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
-	return boundary < end ? boundary : end;
+	return min(ALIGN(addr, huge_page_size(h)), end);
   }

Please drop this patch from the mm-new branch, as it causes 'run_vmtests.sh' to hang. Specifically, it leads to the system hanging when executing hugepage-vmemmap test, because the program falls into an infinite loop in walk_hugetlb_range() and cannot break out.

Quoted 8 lines
Quoted 2 lines

Good catch! The problem is that ALIGN() returns addr itself when already aligned, causing the infinite loop ...

Using ALIGN(addr + 1, huge_page_size(h)) would work. Although it could be (addr + 1) & ~huge_page_mask(h) which is probably the easiest to understand.
Some of the 'helper' macros don't really make the code easier to read. (And that includes a lot of uses of min().)

Or we could go back to my original suggestion.

https://lore.kernel.org/linux-mm/[email protected]/

which was in v2:

https://lore.kernel.org/linux-mm/f802959f58865371ba1b10081bced98e3784c5e4.1763796152.git.chandna.sahil@gmail.com/

Quoted 7 lines
Quoted 2 lines

I'm starting to wonder whether we should just leave that code alone :)

Maybe 'we' should stop checkpatch (etc) suggesting min() in trivial cases. It doesn't really make the code better.

David

Thank you for feedback, I dropped this patch this has been resubmitted [1].
This next two patches in this series were also dropped due to error in this
patch. Requesting feedback, if I can re-submit the other 2 patches which use the "%pe" printk format specifier ? Sharing reference to patches [2] and [3] below.
Thanks,
Sahil

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/2c842a64fddeb0fe0cac087783aaedd97edc3191.1764312627.git.chandna.sahil@gmail.com/
[3] https://lore.kernel.org/all/6d729a60eb71baade3670e5bb609a068683af3eb.1764312627.git.chandna.sahil@gmail.com/

#10 lore raw

PATCH v4 2/3 mm/vmscan: use %pe to print error pointers

In reply to Sahil Chandna

Use the %pe printk format specifier to report error pointers directly instead of printing PTR_ERR() as a long value. This improves clarity, produces more readable error messages.

This instance was flagged by the Coccinelle script (misc/ptr_err_to_pe.cocci) as an opportunity to adopt %pe.

Found by: make coccicheck MODE=report M=mm/
No functional change intended

---
changes since v2:
- Add Acked-by tag
link to v2: https://lore.kernel.org/all/64e15c6b2ce86d7824f54e9e001b6f5b44af48b8.1763796152.git.chandna.sahil@gmail.com/
link to v3: https://lore.kernel.org/all/db049e564cafcb0913bee930d6a577ea43044f5b.1764177933.git.chandna.sahil@gmail.com/

---
 mm/vmscan.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/vmscan.c +2 -2
index b2fc8b626d3d..29ed0b304b5c 100644
@@ -7500,8 +7500,8 @@ void __meminit kswapd_run(int nid)
 		pgdat->kswapd = kthread_create_on_node(kswapd, pgdat, nid, "kswapd%d", nid);
 		if (IS_ERR(pgdat->kswapd)) {
 			/* failure at boot is fatal */
-			pr_err("Failed to start kswapd on node %d,ret=%ld\n",
-				   nid, PTR_ERR(pgdat->kswapd));
+			pr_err("Failed to start kswapd on node %d, ret=%pe\n",
+				   nid, pgdat->kswapd);
 			BUG_ON(system_state < SYSTEM_RUNNING);
 			pgdat->kswapd = NULL;
 		} else {
Signature · 1 line
2.50.1
#11 lore raw

PATCH v4 3/3 mm/zswap: use %pe to print error pointers

In reply to Sahil Chandna

Use the %pe printk format specifier to report error pointers directly instead of printing PTR_ERR() as a long value. This improves clarity, produces more readable error messages.

This instance was flagged by the Coccinelle script (misc/ptr_err_to_pe.cocci) as an opportunity to adopt %pe.

Found by: make coccicheck MODE=report M=mm/
No functional change intended.

---
Changes since v2:
- Add Acked-by tag.
link to v2: https://lore.kernel.org/all/0c00ceedce6e0d9aed35cee5faf15fd9126a1f70.1763796152.git.chandna.sahil@gmail.com/
link to v3: https://lore.kernel.org/all/57b7205813aa87c8f5c8bf765e5a8d88daeba68f.1764177933.git.chandna.sahil@gmail.com/

---
 mm/zswap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/zswap.c +2 -2
index c1af782e54ec..c35604db32ad 100644
@@ -749,8 +749,8 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 
 	acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
 	if (IS_ERR(acomp)) {
-		pr_err("could not alloc crypto acomp %s : %ld\n",
-				pool->tfm_name, PTR_ERR(acomp));
+		pr_err("could not alloc crypto acomp %s : %pe\n",
+				pool->tfm_name, acomp);
 		ret = PTR_ERR(acomp);
 		goto fail;
 	}
Signature · 1 line
2.50.1