Skip to content

Commit 7989fdc

Browse files
vladumakpm00
authored andcommitted
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 operation to access warn_limit and change condition to test for non-negative (>= 0) - atomic_dec_if_positive will return -1 once warn_limit becomes 0. Continue to print disable message alongside the last warning. 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. [[email protected]: prevent warn_limit from going negative, per Christoph Lameter] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: f7d77df ("mm/percpu.c: print error message too if atomic alloc failed") Signed-off-by: Vlad Dumitrescu <[email protected]> Reviewed-by: Baoquan He <[email protected]> Cc: Christoph Lameter (Ampere) <[email protected]> Cc: Dennis Zhou <[email protected]> Cc: Tejun Heo <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3be306c commit 7989fdc

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

mm/percpu.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved,
17341734
bool is_atomic;
17351735
bool do_warn;
17361736
struct obj_cgroup *objcg = NULL;
1737-
static int warn_limit = 10;
1737+
static atomic_t warn_limit = ATOMIC_INIT(10);
17381738
struct pcpu_chunk *chunk, *next;
17391739
const char *err;
17401740
int slot, off, cpu, ret;
@@ -1904,13 +1904,17 @@ void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved,
19041904
fail:
19051905
trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align);
19061906

1907-
if (do_warn && warn_limit) {
1908-
pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
1909-
size, align, is_atomic, err);
1910-
if (!is_atomic)
1911-
dump_stack();
1912-
if (!--warn_limit)
1913-
pr_info("limit reached, disable warning\n");
1907+
if (do_warn) {
1908+
int remaining = atomic_dec_if_positive(&warn_limit);
1909+
1910+
if (remaining >= 0) {
1911+
pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
1912+
size, align, is_atomic, err);
1913+
if (!is_atomic)
1914+
dump_stack();
1915+
if (remaining == 0)
1916+
pr_info("limit reached, disable warning\n");
1917+
}
19141918
}
19151919

19161920
if (is_atomic) {

0 commit comments

Comments
 (0)