From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from va-1-114.ptr.blmpb.com (va-1-114.ptr.blmpb.com [209.127.230.114]) (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 CBEF53F44C3 for ; Tue, 9 Jun 2026 09:19:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.114 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780996745; cv=none; b=jN7XuN2BloPj9kLzTVGh8IoepKEtDBkIh8+7LhLIp+RWETNlUm2cG4PnvWyMx7oBFacqVd9KwHEP4HW3rKZLsdox5gMjjuTjSYiO1oTZn6L2LOFfQoP8wr/KYaSmSCOaPEiJj5BqOkKXZur7pbgLK5vFIdse/mYy62HB2hyGAIM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780996745; c=relaxed/simple; bh=T2xs9v4QNzc+YxYskBzD2NeNfY0oZrIUShTHyOtgYn0=; h=In-Reply-To:Cc:Mime-Version:Date:Message-Id:From:Subject: References:Content-Type:To; b=i9whcEmFfQl7tg7ls+fw4RMW5ShfJfgznOefmyZqRTiccYLzN2iPOzhO61jWL/Tt/OZJbTPa/1gxFTvyJagogDlpacLq2mqe8fxMRTMotFR7d8IyYjpzUoSVD1tblVrjOYxpdLq1JFGQtiIJRBlR+tpK5UM1o8Vq+jYD0VoyWaI= 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=hFkxZGq+; arc=none smtp.client-ip=209.127.230.114 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="hFkxZGq+" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1780996739; 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=hFkxZGq+lsSsoT10yE2AqkSA4I9wx1bD/i3cprVsJi36YpsSGvaaAhsm3ngrF7y4S4cy/Z cNg87FoDjzPQSuv8B+bzzxerwsEJ1XMYHPhXTbw0ogqTAzdN1/NVzr19rot7TwluS4jtsy SZQBduuhUJuzQBS20R1i6hkLOo01mEJ1e95QX8qwbfv4XrHEFn9G9JgRKpUqGX8A0pzYll iiCAnYCZwwbM+fVzUvG7Z8XOMTTaDJSa0L7dQU4Fe2oJyIPJFuVkxjXiu2XBcI+UlELGt9 Mg07j8NIUwcTGrhaapDEAigKwYHPHlMW6N7KR5BljuD277iUTJt2T8hqjkgqpQ== X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260609091710.404083-1-zhouchuyi@bytedance.com> Cc: , "Chuyi Zhou" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Date: Tue, 9 Jun 2026 17:17:01 +0800 Message-Id: <20260609091710.404083-6-zhouchuyi@bytedance.com> From: "Chuyi Zhou" Subject: [PATCH v7 05/14] smp: Alloc percpu csd data in smpcfd_prepare_cpu() only once References: <20260609091710.404083-1-zhouchuyi@bytedance.com> Content-Transfer-Encoding: 7bit X-Original-From: Chuyi Zhou Content-Type: text/plain; charset=UTF-8 To: , , , , , , , , , , , , , X-Lms-Return-Path: 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