Skip to contentSkip to conversation

percpu: fix race on alloc failed warning limit

PATCH Superseded Vlad Dumitrescu 3 participants 5 messages Aug 22, 2025 – Sep 2, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 percpu: fix race on alloc failed warning limit

The 'allocation failed, ...' warning messages can cause unlimited log spam, contrary to the implementation's intent.

The warn_limit variable is accessed without synchronization. If more than <warn_limit> threads enter the warning path at the same time, the variable will get decremented past 0. Once it becomes negative, the non-zero check will always return true leading to unlimited log spam.

Use atomic operations to access warn_limit and change the check to test for positive (> 0) as it can still become negative.

While the change cited in Fixes is only adjacent, the warning limit implementation was correct before it. Only non-atomic allocations were considered for warnings, and those happened to hold pcpu_alloc_mutex while accessing warn_limit.

Fixes f7d77dfc91f7 ("mm/percpu.c: print error message too if atomic alloc failed")
---
 mm/percpu.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
mm/percpu.c +12 -8
index a56f35dcc417..c1a4089eb4c3 100644
@@ -1734,7 +1734,7 @@ void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved,
 	bool is_atomic;
 	bool do_warn;
 	struct obj_cgroup *objcg = NULL;
-	static int warn_limit = 10;
+	static atomic_t warn_limit = ATOMIC_INIT(10);
 	struct pcpu_chunk *chunk, *next;
 	const char *err;
 	int slot, off, cpu, ret;
@@ -1904,13 +1904,17 @@ void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved,
 fail:
 	trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align);
 
-	if (do_warn && warn_limit) {
-		pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
-			size, align, is_atomic, err);
-		if (!is_atomic)
-			dump_stack();
-		if (!--warn_limit)
-			pr_info("limit reached, disable warning\n");
+	if (do_warn && atomic_read(&warn_limit) > 0) {
+		int remaining = atomic_dec_return(&warn_limit);
+
+		if (remaining >= 0) {
+			pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
+				size, align, is_atomic, err);
+			if (!is_atomic)
+				dump_stack();
+			if (remaining == 0)
+				pr_info("limit reached, disable warning\n");
+		}
 	}
 
 	if (is_atomic) {
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 47 lines · Vlad Dumitrescu

The 'allocation failed, ...' warning messages can cause unlimited log spam, contrary to the implementation's intent.

The warn_limit variable is accessed without synchronization. If more than <warn_limit> threads enter the warning path at the same time, the variable will get decremented past 0. Once it becomes negative, the non-zero check will always return true leading to unlimited log spam.

Use atomic operations to access warn_limit and change the check to test for positive (> 0) as it can still become negative.

While the change cited in Fixes is only adjacent, the warning limit implementation was correct before it. Only non-atomic allocations were considered for warnings, and those happened to hold pcpu_alloc_mutex while accessing warn_limit.

Fixes f7d77dfc91f7 ("mm/percpu.c: print error message too if atomic alloc failed")
---
 mm/percpu.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
mm/percpu.c +2 -8
index a56f35dcc417..c1a4089eb4c3 100644
@@ -1734,7 +1734,7 @@ void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved,
 	bool is_atomic;
 	bool do_warn;
 	struct obj_cgroup *objcg = NULL;
-	static int warn_limit = 10;
+	static atomic_t warn_limit = ATOMIC_INIT(10);
 	struct pcpu_chunk *chunk, *next;
 	const char *err;
 	int slot, off, cpu, ret;
@@ -1904,13 +1904,17 @@ void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved,
 fail:
 	trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align);
 
-	if (do_warn && warn_limit) {
-		pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
-			size, align, is_atomic, err);
-		if (!is_atomic)
-			dump_stack();
-		if (!--warn_limit)
-			pr_info("limit reached, disable warning\n");
+	if (do_warn && atomic_read(&warn_limit) > 0) {

mm/percpu.c void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved, · line 1917

Quoted 10 lines
mm/percpu.c +10 -0
+		int remaining = atomic_dec_return(&warn_limit);
+
+		if (remaining >= 0) {
+			pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
+				size, align, is_atomic, err);
+			if (!is_atomic)
+				dump_stack();
+			if (remaining == 0)
+				pr_info("limit reached, disable warning\n");
+		}

A good catch, and the new code logic makes code more robust, thanks for the fix.

Quoted 6 lines
mm/percpu.c +0 -0
 	}
 
 	if (is_atomic) {
Signature · 1 line
2.49.0
#2 lore raw
In reply to Vlad Dumitrescu

mm/percpu.c void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved, · line 1908

Quoted 2 lines · Vlad Dumitrescu
mm/percpu.c +2 -0
+	if (do_warn && atomic_read(&warn_limit) > 0) {
+		int remaining = atomic_dec_return(&warn_limit);

The code creates a race condition since another atomic_dec_return() can happen on another cpu between these two lines. warn_limit can go negative.

Use a single atomic operation instead?

#3 lore raw
Quoted 8 lines · Christoph Lameter (Ampere)

mm/percpu.c void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved, · line 1908

Quoted 2 lines · Vlad Dumitrescu
mm/percpu.c +2 -0
+	if (do_warn && atomic_read(&warn_limit) > 0) {
+		int remaining = atomic_dec_return(&warn_limit);

The code creates a race condition since another atomic_dec_return() can happen on another cpu between these two lines. warn_limit can go negative.

Yes, which is why I mentioned it in the description. But compared to before, it should be benign.

Quoted 1 line

Use a single atomic operation instead?

Did you have something like this in mind?

- if (do_warn && atomic_read(&warn_limit) > 0) {
- int remaining = atomic_dec_return(&warn_limit);
+ if (do_warn) {
+ int remaining = atomic_dec_if_positive(&warn_limit);

Should end up with the same visible result, but w/o going negative.

Would you like me to send v2?Vlad

#4 lore raw
Quoted 17 lines · Vlad Dumitrescu
Quoted 8 lines · Christoph Lameter (Ampere)

mm/percpu.c void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved, · line 1908

Quoted 2 lines · Vlad Dumitrescu
mm/percpu.c +2 -0
+	if (do_warn && atomic_read(&warn_limit) > 0) {
+		int remaining = atomic_dec_return(&warn_limit);

The code creates a race condition since another atomic_dec_return() can happen on another cpu between these two lines. warn_limit can go negative.

Yes, which is why I mentioned it in the description. But compared to before, it should be benign.

Quoted 1 line

Use a single atomic operation instead?

Did you have something like this in mind?

Review

Quoted 4 lines
(quoted diff) +2 -2
-	if (do_warn && atomic_read(&warn_limit) > 0) {
-		int remaining = atomic_dec_return(&warn_limit);
+	if (do_warn) {
+		int remaining = atomic_dec_if_positive(&warn_limit);

Something like it... Maybe

if (do_warn && (atomic_dec_if_positive(&warn_limit)) ) {
pr_warn ...
}