From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from va-1-113.ptr.blmpb.com (va-1-113.ptr.blmpb.com [209.127.230.113]) (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 691F640DFBE for ; Wed, 13 May 2026 12:47:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.113 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778676439; cv=none; b=IHRVWPkGRYK54vrFtm1cetsbknZ3Cje+UwVyD+pmWkcOSA5bXX8WUPgpA37Ri6Kt+pZXU8ulgyJQs2iebj19GP+m9VErdbScSebTawKZEilK0b1BbdBGoyZazWf1UJmlx1NAMNhiiShpU5Lu6Sb1QtYdzLB8aUZSOgbJ/nlWN6U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778676439; c=relaxed/simple; bh=Yg6+ysqNpKUAvKlsN+fcz5tkal+wkG1yuZtlq9dSwmc=; h=Mime-Version:Content-Type:Cc:References:From:Subject:Date:To: Message-Id:In-Reply-To; b=CxJP3q/MHPwnUWiDHX+ikaCjQxD2SRv6vHSqbSReLoqRAOS3ns7OPdxK18EjGTmRVchx4YNXtOCOCZyX9ka3PHkszFS6huE89B0KFQQbzCc/3l2Kw69SH13Pm9zAta0o10eY0FcF7z4M+f3V+8dbfAqEqXyg3Bd8/Wq94+V+Ml0= 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=WEKeWTNN; arc=none smtp.client-ip=209.127.230.113 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="WEKeWTNN" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1778676432; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=BXpZPH4dp2a8rB0CKaTWsQkpCpkV/kf2CjnJVZI7f/8=; b=WEKeWTNNwHLFG736CjkskUHeMtXX5UhfLBfEqYvBZ9w0D4XvoEPoSRoU2Q3Z6z7H8L+wv6 a3NHAIV+BnlUXk7ZyZeAgHjj1isM25z/0s725L2+jc4MpgJtsO3YafY9ahLHoBnHxT12RG +Ovifdz20lPZfUrsaTUxhIIgVI+NWOg8QRHLEAKH3WGzQrvpUt0jQyQihQTb11l98SO775 NwhtYf68KvP2T2McaxnyCMbJHkiESAxpbcy2kMgvfOrP1mn5NaKX/Dz29faI+x7kCUsHoa MD4G1nDjSyf2K4h1hTUcad/auaqbQ3kxKjdlLb5r67iCg/ziK9mfYH8n0Wp5nQ== 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=UTF-8 Cc: , "Chuyi Zhou" Content-Transfer-Encoding: 7bit X-Original-From: Chuyi Zhou References: <20260513124524.2569867-1-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 From: "Chuyi Zhou" Subject: [RESEND PATCH v5 06/12] smp: Enable preemption early in smp_call_function_many_cond Date: Wed, 13 May 2026 20:45:18 +0800 To: , , , , , , , , , , , , Message-Id: <20260513124524.2569867-7-zhouchuyi@bytedance.com> In-Reply-To: <20260513124524.2569867-1-zhouchuyi@bytedance.com> X-Lms-Return-Path: Disabling preemption entirely during smp_call_function_many_cond() was primarily for the following reasons: - To prevent the remote online CPU from going offline. Specifically, we want to ensure that no new csds are queued after smpcfd_dying_cpu() has finished. Therefore, preemption must be disabled until all necessary IPIs are sent. - To prevent current CPU from going offline. Being migrated to another CPU and calling csd_lock_wait() may cause UAF due to smpcfd_dead_cpu() during the current CPU offline process. - To protect the per-cpu cfd_data from concurrent modification by other tasks on the current CPU. cfd_data contains cpumasks and per-cpu csds. Before enqueueing a csd, we block on the csd_lock() to ensure the previous async csd->func() has completed, and then initialize csd->func and csd->info. After sending the IPI, we spin-wait for the remote CPU to call csd_unlock(). Actually the csd_lock mechanism already guarantees csd serialization. If preemption occurs during csd_lock_wait, other concurrent smp_call_function_many_cond calls will simply block until the previous csd->func() completes: task A task B sd->func = fun_a send ipis preempted by B ---------------> csd_lock(csd); // block until last // fun_a finished csd->func = func_b; csd->info = info; ... send ipis switch back to A <--------------- csd_lock_wait(csd); // block until remote finish func_* Previous patches replaced the per-cpu cfd->cpumask with task-local cpumask, and the percpu csd is allocated only once and is never freed to ensure we can safely access csd. Now we can enable preemption before csd_lock_wait() which makes the potentially unpredictable csd_lock_wait() preemptible and migratable. Signed-off-by: Chuyi Zhou --- kernel/smp.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/kernel/smp.c b/kernel/smp.c index ab22a0697db2..a55919f39cf4 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -853,7 +853,7 @@ static void smp_call_function_many_cond(const struct cpumask *mask, unsigned int scf_flags, smp_cond_func_t cond_func) { - int cpu, last_cpu, this_cpu = smp_processor_id(); + int cpu, last_cpu, this_cpu; struct call_function_data *cfd; bool wait = scf_flags & SCF_WAIT; struct cpumask *cpumask, *task_mask; @@ -861,10 +861,10 @@ static void smp_call_function_many_cond(const struct cpumask *mask, int nr_cpus = 0; bool run_remote = false; - lockdep_assert_preemption_disabled(); - task_mask = smp_task_ipi_mask(current); - preemptible_wait = task_mask; + preemptible_wait = task_mask && preemptible(); + + this_cpu = get_cpu(); cfd = this_cpu_ptr(&cfd_data); cpumask = preemptible_wait ? task_mask : cfd->cpumask; @@ -946,6 +946,19 @@ static void smp_call_function_many_cond(const struct cpumask *mask, local_irq_restore(flags); } + /* + * We may block in csd_lock_wait() for a significant amount of time, + * especially when interrupts are disabled or with a large number of + * remote CPUs. Try to enable preemption before csd_lock_wait(). + * + * Use the task_mask instead of cfd->cpumask to avoid concurrency + * modification from tasks on the same cpu. If preemption occurs during + * csd_lock_wait, other concurrent smp_call_function_many_cond() calls + * will simply block until the previous csd->func() completes. + */ + if (preemptible_wait) + put_cpu(); + if (run_remote && wait) { for_each_cpu(cpu, cpumask) { call_single_data_t *csd; @@ -954,6 +967,9 @@ static void smp_call_function_many_cond(const struct cpumask *mask, csd_lock_wait(csd); } } + + if (!preemptible_wait) + put_cpu(); } /** @@ -965,8 +981,7 @@ static void smp_call_function_many_cond(const struct cpumask *mask, * on other CPUs. * * You must not call this function with disabled interrupts or from a - * hardware interrupt handler or from a bottom half handler. Preemption - * must be disabled when calling this function. + * hardware interrupt handler or from a bottom half handler. * * @func is not called on the local CPU even if @mask contains it. Consider * using on_each_cpu_cond_mask() instead if this is not desirable. -- 2.20.1