Skip to contentSkip to conversation

mm: replace custom ratelimiting logic

PATCH Applied Pedro Falcato 8 participants 15 messages Aug 11 – Aug 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover mm: replace custom ratelimiting logic

The kernel has a perfectly cromulent and mostly-equivalent variant in lib/ratelimit.c that can be used.

To: Andrew Morton <[email protected]>
To: David Hildenbrand <[email protected]>
Cc: Lorenzo Stoakes <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Brendan Jackman <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: [email protected]
Cc: [email protected]

Pedro Falcato (2):
mm: replace custom bad page map ratelimiting logic mm/page_alloc: replace custom bad page ratelimiting logic

 mm/memory.c     | 30 +++---------------------------
 mm/page_alloc.c | 28 +++++-----------------------
 2 files changed, 8 insertions(+), 50 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 mm: replace custom bad page map ratelimiting logic

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

KERN_WARNING "print_bad_page_map: %d callbacks suppressed\n", ...

But that should be fine enough.

---
 mm/memory.c | 30 +++---------------------------
 1 file changed, 3 insertions(+), 27 deletions(-)
mm/memory.c +3 -27
index 4134ac607ee0..b4be57b590ce 100644
@@ -492,32 +492,8 @@ static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
 			add_mm_counter(mm, i, rss[i]);
 }
 
-static bool is_bad_page_map_ratelimited(void)
-{
-	static unsigned long resume;
-	static unsigned long nr_shown;
-	static unsigned long nr_unshown;
-
-	/*
-	 * Allow a burst of 60 reports, then keep quiet for that minute;
-	 * or allow a steady drip of one report per second.
-	 */
-	if (nr_shown == 60) {
-		if (time_before(jiffies, resume)) {
-			nr_unshown++;
-			return true;
-		}
-		if (nr_unshown) {
-			pr_alert("BUG: Bad page map: %lu messages suppressed\n",
-				 nr_unshown);
-			nr_unshown = 0;
-		}
-		nr_shown = 0;
-	}
-	if (nr_shown++ == 0)
-		resume = jiffies + 60 * HZ;
-	return false;
-}
+/* Allow a burst of 60 bad page map reports per minute. */
+static DEFINE_RATELIMIT_STATE(bad_page_map_ratelimit, 60 * HZ, 60);
 
 static void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_t entry_size)
 {
@@ -633,7 +609,7 @@ static void print_bad_page_map(struct vm_area_struct *vma,
 	char entry_str[PTVAL_STR_MAX];
 	pgoff_t index, anon_index;
 
-	if (is_bad_page_map_ratelimited())
+	if (!__ratelimit(&bad_page_map_ratelimit))
 		return;
 
 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
Signature · 1 line
2.55.0
reviewer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm: replace custom bad page map ratelimiting logic

Quoted 11 lines · Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

KERN_WARNING "print_bad_page_map: %d callbacks suppressed\n", ...

But that should be fine enough.

reviewer Acked-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] mm: replace custom bad page map ratelimiting logic

In reply to Pedro Falcato
Quoted 6 lines · Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

Quoted 9 lines

KERN_WARNING "print_bad_page_map: %d callbacks suppressed\n", ...

But that should be fine enough.

---
 mm/memory.c | 30 +++---------------------------
 1 file changed, 3 insertions(+), 27 deletions(-)

Great!

Signature · 2 lines
Best Regards,
Yan, Zi
Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] mm: replace custom bad page map ratelimiting logic

In reply to Pedro Falcato
Quoted 9 lines · On Tue, 11 Aug 2026 18:21:55 +0100 Pedro Falcato <[email protected]> wrote:

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

KERN_WARNING "print_bad_page_map: %d callbacks suppressed\n", ...

But that should be fine enough.

Nice cleanup, thank you! I also agree the change on the log message should be fine.

Quoted 2 lines

Thanks,
SJ

[...]

maintainer Acked-by on v1 1/2
#5 lore raw

Re: [PATCH 1/2] mm: replace custom bad page map ratelimiting logic

In reply to Pedro Falcato
Quoted 6 lines · Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

Quoted 6 lines

KERN_WARNING "print_bad_page_map: %d callbacks suppressed\n", ...

But that should be fine enough.

---

Well that's nice!

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v1 1/2
#6 lore raw

Re: [PATCH 1/2] mm: replace custom bad page map ratelimiting logic

In reply to Pedro Falcato
Quoted 11 lines · Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

KERN_WARNING "print_bad_page_map: %d callbacks suppressed\n", ...

But that should be fine enough.

Oh I love this :) seems __ratelimited() uses a try-lock on a spinlock internally but I don't think that's an issue, a spurious not-rate-limted output on any contention isn't really a big problem.

So LGTM and:

Quoted 53 lines
---
 mm/memory.c | 30 +++---------------------------
 1 file changed, 3 insertions(+), 27 deletions(-)
mm/memory.c +3 -28
index 4134ac607ee0..b4be57b590ce 100644
@@ -492,32 +492,8 @@ static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
 			add_mm_counter(mm, i, rss[i]);
 }
 
-static bool is_bad_page_map_ratelimited(void)
-{
-	static unsigned long resume;
-	static unsigned long nr_shown;
-	static unsigned long nr_unshown;
-
-	/*
-	 * Allow a burst of 60 reports, then keep quiet for that minute;
-	 * or allow a steady drip of one report per second.
-	 */
-	if (nr_shown == 60) {
-		if (time_before(jiffies, resume)) {
-			nr_unshown++;
-			return true;
-		}
-		if (nr_unshown) {
-			pr_alert("BUG: Bad page map: %lu messages suppressed\n",
-				 nr_unshown);
-			nr_unshown = 0;
-		}
-		nr_shown = 0;
-	}
-	if (nr_shown++ == 0)
-		resume = jiffies + 60 * HZ;
-	return false;
-}
+/* Allow a burst of 60 bad page map reports per minute. */
+static DEFINE_RATELIMIT_STATE(bad_page_map_ratelimit, 60 * HZ, 60);
 
 static void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_t entry_size)
 {
@@ -633,7 +609,7 @@ static void print_bad_page_map(struct vm_area_struct *vma,
 	char entry_str[PTVAL_STR_MAX];
 	pgoff_t index, anon_index;
 
-	if (is_bad_page_map_ratelimited())
+	if (!__ratelimit(&bad_page_map_ratelimit))
 		return;
 
 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
--
Quoted 2 lines

2.55.0

--
Cheers, Lorenzo

#7 lore raw

PATCH 2/2 mm/page_alloc: replace custom bad page ratelimiting logic

In reply to Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

KERN_WARNING "bad_page: %d callbacks suppressed\n", ...

But that should be fine enough.

---
 mm/page_alloc.c | 28 +++++-----------------------
 1 file changed, 5 insertions(+), 23 deletions(-)
mm/page_alloc.c +5 -23
index 12fac9084c48..9ccfd87bc273 100644
@@ -613,31 +613,13 @@ static inline bool __maybe_unused bad_range(struct zone *zone, struct page *page
 }
 #endif
 
+/* Allow a burst of 60 reports per minute */
+static DEFINE_RATELIMIT_STATE(bad_page_ratelimit, 60 * HZ, 60);
+
 static void bad_page(struct page *page, const char *reason)
 {
-	static unsigned long resume;
-	static unsigned long nr_shown;
-	static unsigned long nr_unshown;
-
-	/*
-	 * Allow a burst of 60 reports, then keep quiet for that minute;
-	 * or allow a steady drip of one report per second.
-	 */
-	if (nr_shown == 60) {
-		if (time_before(jiffies, resume)) {
-			nr_unshown++;
-			goto out;
-		}
-		if (nr_unshown) {
-			pr_alert(
-			      "BUG: Bad page state: %lu messages suppressed\n",
-				nr_unshown);
-			nr_unshown = 0;
-		}
-		nr_shown = 0;
-	}
-	if (nr_shown++ == 0)
-		resume = jiffies + 60 * HZ;
+	if (!__ratelimit(&bad_page_ratelimit))
+		goto out;
 
 	pr_alert("BUG: Bad page state in process %s  pfn:%05lx\n",
 		current->comm, page_to_pfn(page));
Signature · 1 line
2.55.0
reviewer Acked-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] mm/page_alloc: replace custom bad page ratelimiting logic

Quoted 11 lines · Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

KERN_WARNING "bad_page: %d callbacks suppressed\n", ...

But that should be fine enough.

reviewer Acked-by on v1 2/2
#9 lore raw

Re: [PATCH 2/2] mm/page_alloc: replace custom bad page ratelimiting logic

In reply to Pedro Falcato
Quoted 6 lines · Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

Quoted 9 lines

KERN_WARNING "bad_page: %d callbacks suppressed\n", ...

But that should be fine enough.

---
 mm/page_alloc.c | 28 +++++-----------------------
 1 file changed, 5 insertions(+), 23 deletions(-)

LGTM.

Signature · 2 lines
Best Regards,
Yan, Zi
Reviewed-by on v1 2/2
#10 lore raw

Re: [PATCH 2/2] mm/page_alloc: replace custom bad page ratelimiting logic

In reply to Pedro Falcato
Quoted 9 lines · On Tue, 11 Aug 2026 18:21:56 +0100 Pedro Falcato <[email protected]> wrote:

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

KERN_WARNING "bad_page: %d callbacks suppressed\n", ...

But that should be fine enough.

I agree that should be fine. This looks good cleanup to me.

Quoted 2 lines

Thanks,
SJ

[...]

maintainer Acked-by on v1 2/2
#11 lore raw

Re: [PATCH 2/2] mm/page_alloc: replace custom bad page ratelimiting logic

In reply to Pedro Falcato
Quoted 6 lines · Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

Quoted 6 lines

KERN_WARNING "bad_page: %d callbacks suppressed\n", ...

But that should be fine enough.

---

Nice :)

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v1 2/2
#12 lore raw

Re: [PATCH 2/2] mm/page_alloc: replace custom bad page ratelimiting logic

In reply to Pedro Falcato
Quoted 11 lines · Pedro Falcato

The current logic (allow up to $BURST prints per minute) can be entirely replaced by the generic version in lib/ratelimit.c, used around the kernel.

Do so. The only functional difference should be that the new logs will read something like:

KERN_WARNING "bad_page: %d callbacks suppressed\n", ...

But that should be fine enough.

LGTM so:

Quoted 46 lines
---
 mm/page_alloc.c | 28 +++++-----------------------
 1 file changed, 5 insertions(+), 23 deletions(-)
mm/page_alloc.c +5 -24
index 12fac9084c48..9ccfd87bc273 100644
@@ -613,31 +613,13 @@ static inline bool __maybe_unused bad_range(struct zone *zone, struct page *page
 }
 #endif
 
+/* Allow a burst of 60 reports per minute */
+static DEFINE_RATELIMIT_STATE(bad_page_ratelimit, 60 * HZ, 60);
+
 static void bad_page(struct page *page, const char *reason)
 {
-	static unsigned long resume;
-	static unsigned long nr_shown;
-	static unsigned long nr_unshown;
-
-	/*
-	 * Allow a burst of 60 reports, then keep quiet for that minute;
-	 * or allow a steady drip of one report per second.
-	 */
-	if (nr_shown == 60) {
-		if (time_before(jiffies, resume)) {
-			nr_unshown++;
-			goto out;
-		}
-		if (nr_unshown) {
-			pr_alert(
-			      "BUG: Bad page state: %lu messages suppressed\n",
-				nr_unshown);
-			nr_unshown = 0;
-		}
-		nr_shown = 0;
-	}
-	if (nr_shown++ == 0)
-		resume = jiffies + 60 * HZ;
+	if (!__ratelimit(&bad_page_ratelimit))
+		goto out;
 
 	pr_alert("BUG: Bad page state in process %s  pfn:%05lx\n",
 		current->comm, page_to_pfn(page));
--
Quoted 2 lines

2.55.0

--
Cheers, Lorenzo

reviewer Acked-by on the v1 series
#13 lore raw
In reply to Pedro Falcato
Quoted 17 lines · Pedro Falcato

The kernel has a perfectly cromulent and mostly-equivalent variant in lib/ratelimit.c that can be used.

To: Andrew Morton <[email protected]>
To: David Hildenbrand <[email protected]>
Cc: Lorenzo Stoakes <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Brendan Jackman <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: [email protected]
Cc: [email protected]

Quoted 3 lines

Pedro Falcato (2):
mm: replace custom bad page map ratelimiting logic mm/page_alloc: replace custom bad page ratelimiting logic

For the series:

Quoted 7 lines
 mm/memory.c     | 30 +++---------------------------
 mm/page_alloc.c | 28 +++++-----------------------
 2 files changed, 8 insertions(+), 50 deletions(-)
Signature · 1 line
2.55.0
Signature · 2 lines
Sincerely yours,
Mike.
maintainer Acked-by on the v1 series
#14 lore raw
In reply to Pedro Falcato
Quoted 17 lines · Pedro Falcato

The kernel has a perfectly cromulent and mostly-equivalent variant in lib/ratelimit.c that can be used.

To: Andrew Morton <[email protected]>
To: David Hildenbrand <[email protected]>
Cc: Lorenzo Stoakes <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Brendan Jackman <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: [email protected]
Cc: [email protected]

Quoted 3 lines

Pedro Falcato (2):
mm: replace custom bad page map ratelimiting logic mm/page_alloc: replace custom bad page ratelimiting logic

Cool!

Quoted 5 lines
 mm/memory.c     | 30 +++---------------------------
 mm/page_alloc.c | 28 +++++-----------------------
 2 files changed, 8 insertions(+), 50 deletions(-)