From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCF2C30C14E for ; Fri, 12 Jun 2026 01:10:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781226658; cv=none; b=UGBKB/vV982urlBPu7YDgLEB65NmtWkfUzpS1ooF+gdNS//d9UpdgivOyjcqOA3g21JYdAAa8e6H668EGWlb+duIkxBtthGxJb369wb1RRXxJ8TZ3gEMJRS73EhOVlWQiGLAS2trxvcHqAdIzidlZWU53Hkaz59gnSDFOPoJrJg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781226658; c=relaxed/simple; bh=GUJZNkLejrvJOc2zT1qp5uXYFdFTrJLUzo+uss+M154=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I0hKDyiE1bk7R7VOnm2ZU6HGrIlBA3PGK63ClvO/FQ3OTbQXHGgdgmZFgtJQ4J6zM2XJIwx9AEiwn0P0CTB/cW4b7nqKYcbs/wC/5JyZaOqvmVvg1kWvLEAp7oSy1gTfTIC1qQ9EqhLLLf6JEHoER8ndZ8nALlB9pO/uE8HjWD8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=K/ebhH3u; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="K/ebhH3u" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 029811F000E9; Fri, 12 Jun 2026 01:10:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1781226657; bh=8Nt7zTy7OwkHO+yy23DmbB2qHh++8NKVSmgy/TvQ9cw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=K/ebhH3uC6E2Exq+61ZEWztMIPk4YRRJLlxdF0tImKaO7Pulx8fUcCw5EAjS5dWwh CrWmAh9m7eAliMCSb4PmrSoykALflrAEc2CohDRTdeG3Jjf5Zt+moL1/4i8BYeFtty eIk9wHnCOLR1AB/7SyBbjq0X92u3+y/mD2alOLNCVNlLcvMiE3iT5BLE/6JMdcGan+ DcoRRlqbxoefSGjIZsUbdbfSBmhqVo6Zwm7nplyHcjle6GLcRhKobLZM4NSrCNNZzh PyzIRxxql7Jcs89uniKkOKVvXaLWyBH4+WkE/fra00bR9W6HQQT4JXoTZsAUkUnM1/ WgVpovFv6TNew== From: SeongJae Park To: Breno Leitao Cc: SeongJae Park , Catalin Marinas , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: Re: [PATCH RFC] mm/kmemleak: avoid soft lockup when scanning task stacks Date: Thu, 11 Jun 2026 18:10:48 -0700 Message-ID: <20260612011049.84146-1-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260611-kmemleak-stack-resched-v1-1-d6248ade5f4a@debian.org> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Thu, 11 Jun 2026 05:45:00 -0700 Breno Leitao wrote: > kmemleak_scan() walks every thread and scans its kernel stack under a > single rcu_read_lock() with no reschedule point. On a host with very > many threads -- amplified by KASAN/lockdep in debug builds -- this loop > can hog a CPU long enough to trip the soft lockup watchdog: > > watchdog: BUG: soft lockup - CPU#35 stuck for 22s! [kmemleak:537] > scan_block > kmemleak_scan > kmemleak_scan_thread > kthread > > A cond_resched() cannot be added directly: the loop runs inside an RCU > read-side critical section. > > Split the scan in two parts: > > 1) get the list of tasks (with RCU read lock) in an array > 2) run scan_block() for the tasks (with cond_reschd()). > > Is it a sane approach? > > Signed-off-by: Breno Leitao > --- > mm/kmemleak.c | 26 ++++++++++++++++++++++---- > 1 file changed, 22 insertions(+), 4 deletions(-) > > diff --git a/mm/kmemleak.c b/mm/kmemleak.c > index 7c7ba17ce7af0..9f8a35ecbb50c 100644 > --- a/mm/kmemleak.c > +++ b/mm/kmemleak.c > @@ -62,6 +62,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -1885,17 +1886,34 @@ static void kmemleak_scan(void) > * Scanning the task stacks (may introduce false negatives). > */ > if (kmemleak_stack_scan) { > - struct task_struct *p, *g; > + struct task_struct **tasks, *p, *g; > + unsigned int nr = 0, max, i; > > + max = nr_threads + 64; > + tasks = kvmalloc_array(max, sizeof(*tasks), GFP_KERNEL); > + > + /* Snapshot the threads under RCU */ > rcu_read_lock(); > for_each_process_thread(g, p) { > - void *stack = try_get_task_stack(p); > + if (!tasks || nr >= max) > + break; Why don't you check !tasks right after the allocation? > + get_task_struct(p); > + tasks[nr++] = p; > + } > + rcu_read_unlock(); > + > + /* now scan_block for the tasks above with cond_resched() */ > + for (i = 0; i < nr; i++) { > + void *stack = try_get_task_stack(tasks[i]); > + > if (stack) { > scan_block(stack, stack + THREAD_SIZE, NULL); > - put_task_stack(p); > + put_task_stack(tasks[i]); > } > + put_task_struct(tasks[i]); > + cond_resched(); > } > - rcu_read_unlock(); > + kvfree(tasks); > } Thanks, SJ [...]