From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from va-1-115.ptr.blmpb.com (va-1-115.ptr.blmpb.com [209.127.230.115]) (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 DA89D43C056 for ; Tue, 16 Jun 2026 11:13:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.115 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781608434; cv=none; b=X17efmvs8oYWUt1Z946wtUAbQh1+GsWA/x4L6Mrkn69jSH9NettiYU4+s568ZcFKLfHRVNnam4UgN9pqp/kf4KZnzai+1vp6NHs9OwE90bebAUVtcpJ44zJp9MB9I+GwnyECnZvWvgQzYwMJGtDSPerAk8JQGAYJWcGoy4oblss= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781608434; c=relaxed/simple; bh=T2xs9v4QNzc+YxYskBzD2NeNfY0oZrIUShTHyOtgYn0=; h=Date:Content-Type:Subject:Message-Id:Mime-Version:To:Cc:From: References:In-Reply-To; b=swhUUQ8YfRSe1eRk/Vzv2mBAZnLt+h2epLQOkBcjGRnd9GIp0068pzy1XZTT4qAZBeIVsGmZgL92hJok5Jg/hWTPjAycDojCvUR7PMmZRy2tN4qRBg2CeBvmMvVspc5qfqCmUYsXTx+GenditpMSIXseUwJDGxy2HCJoqbRU5BQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=edSiK075; arc=none smtp.client-ip=209.127.230.115 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="edSiK075" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1781608422; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=pO46IL8QWb9ZB8R4f6WGAlcAMqyemLMdaVa3NlmJ4ec=; b=edSiK075K9KfQtYM86akK9fPP0CVSOcDbdXhVbAOuUwl6oBUIKlgPlSxE2xkxatT4xvJzc bt80/AM8LcHxHustoDsHD476L9TgvdVAODxmgXRj8hOiobvaQd4TUyeCc4RpiSu7WwcWmv TyoyCrP+sBOVzuxGvtSRaO4T0t6jhpxMBnf8XwsZ6V+89eFn7foZGsLyUjjmC9T90x3wA/ d9BNqxicb4+N1qWIf/kKsCrAFBy6GMMyX+H8eA4V7lU7arMr3ZNGDLagOJk9EsRTFHp1oT TWFf2mIr/c8wkCnkK5RyGi1DxZOWmVopo3rWQijDsMYtFLIo4mlaym7xdnR0yw== Date: Tue, 16 Jun 2026 19:11:18 +0800 X-Lms-Return-Path: X-Mailer: git-send-email 2.20.1 Content-Type: text/plain; charset=UTF-8 Subject: [PATCH v8 05/14] smp: Alloc percpu csd data in smpcfd_prepare_cpu() only once Message-Id: <20260616111127.966468-6-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Original-From: Chuyi Zhou To: , , , , , , , , , , , , , Cc: , "Chuyi Zhou" From: "Chuyi Zhou" References: <20260616111127.966468-1-zhouchuyi@bytedance.com> In-Reply-To: <20260616111127.966468-1-zhouchuyi@bytedance.com> Content-Transfer-Encoding: 7bit Later patch would enable preemption during csd_lock_wait() in smp_call_function_many_cond(), which may cause access cfd->csd data that has already been freed in smpcfd_dead_cpu(). One way to fix the above issue is to use the RCU mechanism to protect the csd data and wait for all read critical sections to exit before freeing the memory in smpcfd_dead_cpu(), but this could delay CPU shutdown. This patch chooses a simpler approach: allocate the percpu csd on the UP side only once and skip freeing the csd memory in smpcfd_dead_cpu(). Suggested-by: Sebastian Andrzej Siewior Signed-off-by: Chuyi Zhou Acked-by: Muchun Song Reviewed-by: Sebastian Andrzej Siewior Tested-by: Paul E. McKenney --- kernel/smp.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kernel/smp.c b/kernel/smp.c index 8f8a9ee2ad11..9ef136bacda0 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -64,7 +64,15 @@ int smpcfd_prepare_cpu(unsigned int cpu) free_cpumask_var(cfd->cpumask); return -ENOMEM; } - cfd->csd = alloc_percpu(call_single_data_t); + + /* + * The percpu csd is allocated only once and never freed. + * This ensures that smp_call_function_many_cond() can safely + * access the csd of an offlined CPU if it gets preempted + * during csd_lock_wait(). + */ + if (!cfd->csd) + cfd->csd = alloc_percpu(call_single_data_t); if (!cfd->csd) { free_cpumask_var(cfd->cpumask); free_cpumask_var(cfd->cpumask_ipi); @@ -80,7 +88,6 @@ int smpcfd_dead_cpu(unsigned int cpu) free_cpumask_var(cfd->cpumask); free_cpumask_var(cfd->cpumask_ipi); - free_percpu(cfd->csd); return 0; } -- 2.20.1