From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from va-1-112.ptr.blmpb.com (va-1-112.ptr.blmpb.com [209.127.230.112]) (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 1345E3DFC9B for ; Tue, 9 Jun 2026 09:19:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.112 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780996760; cv=none; b=k30Xqgd5U73yLD1NjiG7paMSt3OhMpSzOWUiHPzmi3bKDc5sDBNktT70OwAahkoJg51ebslU6784kYKQNVt1TShzNdC/BKcPKMsbv8/3mZgEsPV8yol4oxdW0zJU/1Ht/zVd5StLVq3k45h8KcYmEpJN8TCzdau1j6jCIc8OrKc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780996760; c=relaxed/simple; bh=uRWkY2wQs8YgnNqUQ36nNxK+pjSBYXc+FXNwn9jsCVA=; h=To:Date:Mime-Version:Cc:From:Message-Id:Subject:In-Reply-To: Content-Type:References; b=U1UkOjwC62z/HPTPafYvmkRYIbquG403Msy0Rspa3Pa3WZKe93uJyaROF1OWuBdAoUarkhtxo7M+sZOpa1OMh5KRN2jvz2UF9/G1TEv74o/oEwES94oW7/AOT9wtRn2HwcwRj1K13mfjy0ziLElfTgBJLcL6hG++Oac3F/K/G2c= 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=R5uFUhQE; arc=none smtp.client-ip=209.127.230.112 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="R5uFUhQE" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1780996753; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=CtCQ19MYHoDYIqTakdxf2422YXxfy5+A3fzTiz/hZlo=; b=R5uFUhQEgp/K6VJlS+A5LrPCYjq3R3ZBC/cLJioIaHdleegERSaCwpJJCTtFsI9f+0sFOb h3Rdj5Lr2rDJDFNhRvQK7Pi3v85X4UJqU3OJlD/7OV594rcZUZVanzwZZ/xYPzgARIGjBS DSy47BB3ysqN/AhWT+jyJrMmMy+1VTyJE0Z2JyMRAgb3QqAILaQwirY1EyhDkPOC6eji97 Z7L764WG4iVxgtzWjWJ3C30r21DnGJzgNaq40u0yMrCN5DLagA+Ftl2XilVB3PO0N+t1ON V+SaXAowNiqYtP7DH/TijkuMXp5LjzaN+VUq8TUYMYQpUU2c8KUG3PYZCAMqwQ== To: , , , , , , , , , , , , , Date: Tue, 9 Jun 2026 17:17:02 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Cc: , "Chuyi Zhou" From: "Chuyi Zhou" Message-Id: <20260609091710.404083-7-zhouchuyi@bytedance.com> Subject: [PATCH v7 06/14] smp: Enable preemption early in smp_call_function_many_cond() X-Original-From: Chuyi Zhou In-Reply-To: <20260609091710.404083-1-zhouchuyi@bytedance.com> Content-Type: text/plain; charset=UTF-8 X-Lms-Return-Path: References: <20260609091710.404083-1-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 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 Reviewed-by: Sebastian Andrzej Siewior Tested-by: Paul E. McKenney --- kernel/smp.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/kernel/smp.c b/kernel/smp.c index 9ef136bacda0..390e6526574c 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -859,15 +859,14 @@ 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; int nr_cpus = 0; bool run_remote = false; - lockdep_assert_preemption_disabled(); - + this_cpu = get_cpu(); task_mask = smp_task_ipi_mask(current); cfd = this_cpu_ptr(&cfd_data); if (task_mask) @@ -953,6 +952,17 @@ static void smp_call_function_many_cond(const struct cpumask *mask, local_irq_restore(flags); } + /* + * Waiting for completion can take time, especially with many CPUs. + * On a PREEMPT kernel a per-task cpumask is used to track CPUs with + * pending IPI requests. This allows preemption to be enabled before + * waiting. On a !PREEMPT kernel the cpumask is shared and the call + * must block until completion to avoid modifications by another caller + * on this CPU. + */ + if (task_mask) + put_cpu(); + if (run_remote && wait) { for_each_cpu(cpu, cpumask) { call_single_data_t *csd; @@ -961,6 +971,9 @@ static void smp_call_function_many_cond(const struct cpumask *mask, csd_lock_wait(csd); } } + + if (!task_mask) + put_cpu(); } /** @@ -972,8 +985,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