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 E09153B841D for ; Tue, 16 Jun 2026 11:14:05 +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=1781608447; cv=none; b=uMJWRPIxzrljMJuLMq6C6uKVazeTVMm+7WYwiUS/UgmMjdDL2+yJu53iXR+zatgqIqqo1/j5fMZO0pVAkB/6wmDW+4UrfxGxh/qdTxE3z6jVY56BOM4vRqBsVg1GcVz4u+AoEFNMaHFod+0j5KQtrcL2spnfdHtqQNR61UcfpvA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781608447; c=relaxed/simple; bh=uRWkY2wQs8YgnNqUQ36nNxK+pjSBYXc+FXNwn9jsCVA=; h=Date:In-Reply-To:References:From:Cc:To:Content-Type:Message-Id: Mime-Version:Subject; b=MwuVwQHXN5n/NiztdFYlfrqk4h4FLiziwd86siBNLN/2MBXrl3xeumnGkrw3f1zTHhIu5WqnGWUdUQy0i2wV0HKOVBDqSetSHJ2cabi6YURYAUimW6qrhBxXaFjmwH7BE005Q2sF5CQu3uvKhNX2JexwbyJtdoA3umuD6UMMQe4= 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=VMOcyfSa; 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="VMOcyfSa" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1781608441; 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=VMOcyfSafrBUJsUnlCjviVkaBAWtodXc0F598YE2e8PM1u2SpDuHA6JahJaBFko7dhistr n9f7jIyfUggY0k2W9y8kcQ2CisIcWDnwxygNs+zYuilWoLxz9NjGuMFY39MPMGsfHDnV24 i/TIO/K8cvIbdyRpZP+dICWQgh5ZaLxjn5zUK/sYYqLEvqi2NIr9+DMDdspFLHc5HO61Yt NViIdljGznLyH2Ls42qCe0Sfs05tjBYHyrxNYMOzDP7/FMi4OekLBBoKExMgWC/y3xneOW lMWB/duyNCFUmcO49K1oLzWzvz9nQdJ9RHbXy8K7ZQPElBaFKbNdE/xd5XlLdA== Date: Tue, 16 Jun 2026 19:11:19 +0800 In-Reply-To: <20260616111127.966468-1-zhouchuyi@bytedance.com> References: <20260616111127.966468-1-zhouchuyi@bytedance.com> From: "Chuyi Zhou" Cc: , "Chuyi Zhou" X-Original-From: Chuyi Zhou X-Mailer: git-send-email 2.20.1 To: , , , , , , , , , , , , , Content-Type: text/plain; charset=UTF-8 Message-Id: <20260616111127.966468-7-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-Lms-Return-Path: Subject: [PATCH v8 06/14] smp: Enable preemption early in smp_call_function_many_cond() 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