From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00D583C2798 for ; Fri, 29 May 2026 09:34:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780047302; cv=none; b=j0KvnkPbSXoIvo/nxdLDl574KV6gC09i8FmF/e/nUKx5mFY7Z6tDeSSfLyUkZ74ESXUK+mJmC/YYLVFe5llB9H8gP1HKi0PRt9/53PTO+/WH+XB3VKsq86PE9w5wuyTOLjOBDkQ/At57RSQ+VufigpXg29UER8lEqVmpJgikNTE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780047302; c=relaxed/simple; bh=bZ4cg4WTAXds4oL0tlcsSB99H7Z+PCPUuO18u+bS+pQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oLkPyFVC9cc7wIHaQW+w4vyIrt3zY8UHJFxDaoWaET8I9MsHcIAk2p3a2w/w5WWU6qqA5b/i5nkeQhS/HjEYNgKnzuhVHC5DCXmlVDvivF5QzvRTOMaXzA+bD16+ID/a8G4VYZYY0FJUtCDAxu2e1+oYD/ypmj9/trdX04ANGwc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=tp66M1ej; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=sC9PUTFM; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=tp66M1ej; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=sC9PUTFM; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="tp66M1ej"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="sC9PUTFM"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="tp66M1ej"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="sC9PUTFM" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 0A5856AEC4; Fri, 29 May 2026 09:34:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1780047298; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JiXphdER9KPjAvZvInQHVfSv8bEELBky6G1eMTBRIPE=; b=tp66M1ejfBahNYohlZSRIIZSkHilMajtUUE6NTETbZX/GCNgRZub+nI8bE7a+DgWv9y89J /9itPxBNWyUcE14e1w4fXdov8fdHgx51vfOIahubFB87UQ/AzNXJ5K67cOW6HYk04wMaOW an+4y5qnDBb5Fx8wSfvpei4QtBTc/XQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1780047298; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JiXphdER9KPjAvZvInQHVfSv8bEELBky6G1eMTBRIPE=; b=sC9PUTFMhjZWjrmDObD57Iz6YBKPuGz6KVXxDZbotPdJ7rAuLbzXs0F0cs/lTHCH9085iy utmV7d/YCcOysaDQ== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1780047298; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JiXphdER9KPjAvZvInQHVfSv8bEELBky6G1eMTBRIPE=; b=tp66M1ejfBahNYohlZSRIIZSkHilMajtUUE6NTETbZX/GCNgRZub+nI8bE7a+DgWv9y89J /9itPxBNWyUcE14e1w4fXdov8fdHgx51vfOIahubFB87UQ/AzNXJ5K67cOW6HYk04wMaOW an+4y5qnDBb5Fx8wSfvpei4QtBTc/XQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1780047298; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JiXphdER9KPjAvZvInQHVfSv8bEELBky6G1eMTBRIPE=; b=sC9PUTFMhjZWjrmDObD57Iz6YBKPuGz6KVXxDZbotPdJ7rAuLbzXs0F0cs/lTHCH9085iy utmV7d/YCcOysaDQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 41005779A7; Fri, 29 May 2026 09:34:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id KeR0DMFdGWq/WQAAD6G6ig (envelope-from ); Fri, 29 May 2026 09:34:57 +0000 Date: Fri, 29 May 2026 10:34:55 +0100 From: Pedro Falcato To: Kaitao Cheng Cc: dennis@kernel.org, tj@kernel.org, cl@gentwo.org, akpm@linux-foundation.org, mhocko@suse.com, vbabka@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, muchun.song@linux.dev, Kaitao Cheng Subject: Re: [PATCH 2/2] mm/percpu: Avoid pcpu_alloc_mutex recursion from reclaim Message-ID: References: <20260528132917.81123-1-kaitao.cheng@linux.dev> <20260528132917.81123-3-kaitao.cheng@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260528132917.81123-3-kaitao.cheng@linux.dev> X-Spam-Flag: NO X-Spam-Score: -4.30 X-Spam-Level: X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MISSING_XM_UA(0.00)[]; MIME_TRACE(0.00)[0:+]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCPT_COUNT_SEVEN(0.00)[11]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[pedro-suse.lan:mid,imap1.dmz-prg2.suse.org:helo] On Thu, May 28, 2026 at 09:29:17PM +0800, Kaitao Cheng wrote: > From: Kaitao Cheng > > pcpu_alloc_noprof() takes pcpu_alloc_mutex for sleepable allocations > so that it can create chunks and populate backing pages. If reclaim is > entered while that mutex is already held, and reclaim reaches a path > which allocates percpu memory, the nested allocation can try to take > pcpu_alloc_mutex again. > > That creates a reclaim recursion dependency: > > pcpu_alloc_noprof(GFP_KERNEL) > mutex_lock(&pcpu_alloc_mutex) > reclaim > pcpu_alloc_noprof(GFP_NOIO/GFP_NOFS) > mutex_lock(&pcpu_alloc_mutex) > > Avoid this by treating percpu allocations from reclaim context as atomic. > Such allocations may still be served from already available and populated > areas, but they must not enter the mutex-protected slow path or create new > chunks. If no space is available, fail the allocation and let the normal > balance work handle replenishment outside reclaim. > > Update the function comment to describe that reclaim context allocations > are atomic regardless of whether the supplied GFP mask would otherwise > allow blocking. > > This patch is a preventive fix. There may not currently be any path that > calls pcpu_alloc_noprof(GFP_NOIO/GFP_NOFS) from direct reclaim context. I don't like this. The proper way of fixing this would probably be to release pcpu_alloc_mutex (or not have it in the first place!) while you're allocating memory. > > Fixes: 9a5b183941b5 ("mm, percpu: do not consider sleepable allocations atomic") > Signed-off-by: Kaitao Cheng > --- > mm/percpu.c | 13 +++++++++---- > 1 file changed, 9 insertions(+), 4 deletions(-) > > diff --git a/mm/percpu.c b/mm/percpu.c > index 1bb38467390b..9c30e5897813 100644 > --- a/mm/percpu.c > +++ b/mm/percpu.c > @@ -1803,9 +1803,9 @@ static void pcpu_memalloc_scope_restore(gfp_t gfp, unsigned int flags) > * @gfp: allocation flags > * > * Allocate percpu area of @size bytes aligned at @align. If @gfp doesn't > - * contain %GFP_KERNEL, the allocation is atomic. If @gfp has __GFP_NOWARN > - * then no warning will be triggered on invalid or failed allocation > - * requests. > + * allow blocking, or if allocation is requested from reclaim context, the > + * allocation is atomic. If @gfp has __GFP_NOWARN then no warning will be > + * triggered on invalid or failed allocation requests. > * > * RETURNS: > * Percpu pointer to the allocated area on success, NULL on failure. > @@ -1828,7 +1828,12 @@ void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved, > gfp = current_gfp_context(gfp); > /* whitelisted flags that can be passed to the backing allocators */ > pcpu_gfp = gfp & (GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); > - is_atomic = !gfpflags_allow_blocking(gfp); > + /* > + * Reclaim can be entered while pcpu_alloc_mutex is already held by > + * another percpu allocation. Avoid recursing back into the mutex from > + * reclaim; best-effort allocations from already populated areas are OK. > + */ since this is an entirely theoretical issue: /* Reclaim paths should not be hitting the percpu allocator, for now */ if (WARN_ON_ONCE(current->reclaim_state)) return NULL; But that's just my 2c. > + is_atomic = !gfpflags_allow_blocking(gfp) || current->reclaim_state; > do_warn = !(gfp & __GFP_NOWARN); > > /* > -- > 2.50.1 (Apple Git-155) > -- Pedro