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 0E533421F17 for ; Wed, 13 May 2026 12:46:50 +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=1778676412; cv=none; b=XP411rqSnl4km8+yzAt2NlrXseqslRCLRkvsbkX7TyyicJGy0ZHlbFR2bKNLZat8jYV0nrnHzXZvrEjJke7bHkS7+R5Dy8qu44bYso9cqaKm4R2J/Ci7MVqeBcDhYEGPw3qvdIAhb/BmUUMjIl4GoPOf1KROSyZcTooj1q63mJU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778676412; c=relaxed/simple; bh=u79xGmflwucI+YEOnG0UZY0wDGbdoqD3LQdBnD8y8jc=; h=From:Message-Id:References:In-Reply-To:Content-Type:To:Subject: Date:Cc:Mime-Version; b=WN2OqXuFEHzqntKLbbv2yBI49Ih5/j6/YRo7PNnbRvb4XIgSEx03T3WTo1ijfo2GBxzIFRmzfshZX7ArqFDe7XxhpisrV865RI0bNDTyvlynK2AJHbERt6G3BQOkI8VqQQIVvVrFFCXmmRfQ5zxNoaYyUe104b9RRHwHo/6+4UI= 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=nXXen8xS; 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="nXXen8xS" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1778676406; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=OglEIZT1ufBPtbBBcdcHmPqq6F3370jITSD7W3xD394=; b=nXXen8xSUSnV8wUAAkTaOi3QY6FKj5YCSDyH+77Y7d7Jnd6L7AAhJyZFVNJh1fQLVuTrWh yc9oO5RmPU4ZziL+expAatxxJt+bk5VEVy0UVh54yoMHNK1QGABKxXPX3L9vR4ebghbnJo Bsbd/NJZdEhhjjZhOuSyKCv6sVSaOxcGbmnO7TvEKTS8v0CO5glkvHqVcf/J/uj8QtZImm Kk0pR7J9EdvlVte/BG15+8rsPo9e4CxrkdHCncZMIclpCM6z8sVZtWVBF3pK1iJQrC+iYv xP22kVLFusvDEV6TpundN1BSaxpOjfzM0kpbW+oMFvlb+yerPf/EnnIiuEWGHg== From: "Chuyi Zhou" Message-Id: <20260513124524.2569867-5-zhouchuyi@bytedance.com> X-Lms-Return-Path: X-Mailer: git-send-email 2.20.1 Content-Transfer-Encoding: 7bit References: <20260513124524.2569867-1-zhouchuyi@bytedance.com> In-Reply-To: <20260513124524.2569867-1-zhouchuyi@bytedance.com> X-Original-From: Chuyi Zhou Content-Type: text/plain; charset=UTF-8 To: , , , , , , , , , , , , Subject: [RESEND PATCH v5 04/12] smp: Use task-local IPI cpumask in smp_call_function_many_cond() Date: Wed, 13 May 2026 20:45:16 +0800 Cc: , "Chuyi Zhou" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 This patch prepares the task-local IPI cpumask during thread creation, and uses the local cpumask to replace the percpu cfd cpumask in smp_call_function_many_cond(). We will enable preemption during csd_lock_wait() later, and this can prevent concurrent access to the cfd->cpumask from other tasks on the current CPU. For cases where cpumask_size() is smaller than or equal to the pointer size, it tries to stash the cpumask in the pointer itself to avoid extra memory allocations. Signed-off-by: Chuyi Zhou --- include/linux/sched.h | 6 +++++ include/linux/smp.h | 20 +++++++++++++++ kernel/fork.c | 9 ++++++- kernel/smp.c | 59 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 368c7b4d7cb5..bb2c53279412 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1348,6 +1348,12 @@ struct task_struct { struct list_head perf_event_list; struct perf_ctx_data __rcu *perf_ctx_data; #endif +#if defined(CONFIG_SMP) && defined(CONFIG_PREEMPTION) + union { + cpumask_t *ipi_mask_ptr; + unsigned long ipi_mask_val; + }; +#endif #ifdef CONFIG_DEBUG_PREEMPT unsigned long preempt_disable_ip; #endif diff --git a/include/linux/smp.h b/include/linux/smp.h index 6925d15ccaa7..b2ecb9a5edd0 100644 --- a/include/linux/smp.h +++ b/include/linux/smp.h @@ -167,6 +167,12 @@ void smp_call_function_many(const struct cpumask *mask, int smp_call_function_any(const struct cpumask *mask, smp_call_func_t func, void *info, int wait); +#ifdef CONFIG_PREEMPTION +int smp_task_ipi_mask_alloc(struct task_struct *task); +void smp_task_ipi_mask_free(struct task_struct *task); +cpumask_t *smp_task_ipi_mask(struct task_struct *cur); +#endif + void kick_all_cpus_sync(void); void wake_up_all_idle_cpus(void); bool cpus_peek_for_pending_ipi(const struct cpumask *mask); @@ -310,4 +316,18 @@ bool csd_lock_is_stuck(void); static inline bool csd_lock_is_stuck(void) { return false; } #endif +#if !defined(CONFIG_SMP) || !defined(CONFIG_PREEMPTION) +static inline int smp_task_ipi_mask_alloc(struct task_struct *task) +{ + return 0; +} +static inline void smp_task_ipi_mask_free(struct task_struct *task) +{ +} +static inline cpumask_t *smp_task_ipi_mask(struct task_struct *cur) +{ + return NULL; +} +#endif + #endif /* __LINUX_SMP_H */ diff --git a/kernel/fork.c b/kernel/fork.c index 5f3fdfdb14c7..bf485c51c447 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -535,6 +535,7 @@ void free_task(struct task_struct *tsk) #endif release_user_cpus_ptr(tsk); scs_release(tsk); + smp_task_ipi_mask_free(tsk); #ifndef CONFIG_THREAD_INFO_IN_TASK /* @@ -932,10 +933,14 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) #endif account_kernel_stack(tsk, 1); - err = scs_prepare(tsk, node); + err = smp_task_ipi_mask_alloc(tsk); if (err) goto free_stack; + err = scs_prepare(tsk, node); + if (err) + goto free_ipi_mask; + #ifdef CONFIG_SECCOMP /* * We must handle setting up seccomp filters once we're under @@ -1006,6 +1011,8 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) #endif return tsk; +free_ipi_mask: + smp_task_ipi_mask_free(tsk); free_stack: exit_task_stack_account(tsk); free_thread_stack(tsk); diff --git a/kernel/smp.c b/kernel/smp.c index 9e9dab3b0d51..630c8e5a635d 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -794,6 +794,44 @@ int smp_call_function_any(const struct cpumask *mask, } EXPORT_SYMBOL_GPL(smp_call_function_any); +static DEFINE_STATIC_KEY_FALSE(ipi_mask_inlined); + +#ifdef CONFIG_PREEMPTION + +int smp_task_ipi_mask_alloc(struct task_struct *task) +{ + if (static_branch_unlikely(&ipi_mask_inlined)) + return 0; + + task->ipi_mask_ptr = kmalloc(cpumask_size(), GFP_KERNEL); + if (!task->ipi_mask_ptr) + return -ENOMEM; + + return 0; +} + +void smp_task_ipi_mask_free(struct task_struct *task) +{ + if (static_branch_unlikely(&ipi_mask_inlined)) + return; + + kfree(task->ipi_mask_ptr); +} + +cpumask_t *smp_task_ipi_mask(struct task_struct *cur) +{ + /* + * If cpumask_size() is smaller than or equal to the pointer + * size, it stashes the cpumask in the pointer itself to + * avoid extra memory allocations. + */ + if (static_branch_unlikely(&ipi_mask_inlined)) + return (cpumask_t *)&cur->ipi_mask_val; + + return cur->ipi_mask_ptr; +} +#endif + /* * Flags to be used as scf_flags argument of smp_call_function_many_cond(). * @@ -811,11 +849,18 @@ static void smp_call_function_many_cond(const struct cpumask *mask, int cpu, last_cpu, this_cpu = smp_processor_id(); struct call_function_data *cfd; bool wait = scf_flags & SCF_WAIT; + struct cpumask *cpumask, *task_mask; + bool preemptible_wait; int nr_cpus = 0; bool run_remote = false; lockdep_assert_preemption_disabled(); + task_mask = smp_task_ipi_mask(current); + preemptible_wait = task_mask; + cfd = this_cpu_ptr(&cfd_data); + cpumask = preemptible_wait ? task_mask : cfd->cpumask; + /* * Can deadlock when called with interrupts disabled. * We allow cpu's that are not yet online though, as no one else can @@ -836,16 +881,15 @@ static void smp_call_function_many_cond(const struct cpumask *mask, /* Check if we need remote execution, i.e., any CPU excluding this one. */ if (cpumask_any_and_but(mask, cpu_online_mask, this_cpu) < nr_cpu_ids) { - cfd = this_cpu_ptr(&cfd_data); - cpumask_and(cfd->cpumask, mask, cpu_online_mask); - __cpumask_clear_cpu(this_cpu, cfd->cpumask); + cpumask_and(cpumask, mask, cpu_online_mask); + __cpumask_clear_cpu(this_cpu, cpumask); cpumask_clear(cfd->cpumask_ipi); - for_each_cpu(cpu, cfd->cpumask) { + for_each_cpu(cpu, cpumask) { call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu); if (cond_func && !cond_func(cpu, info)) { - __cpumask_clear_cpu(cpu, cfd->cpumask); + __cpumask_clear_cpu(cpu, cpumask); continue; } @@ -896,7 +940,7 @@ static void smp_call_function_many_cond(const struct cpumask *mask, } if (run_remote && wait) { - for_each_cpu(cpu, cfd->cpumask) { + for_each_cpu(cpu, cpumask) { call_single_data_t *csd; csd = per_cpu_ptr(cfd->csd, cpu); @@ -1010,6 +1054,9 @@ EXPORT_SYMBOL(nr_cpu_ids); void __init setup_nr_cpu_ids(void) { set_nr_cpu_ids(find_last_bit(cpumask_bits(cpu_possible_mask), NR_CPUS) + 1); + + if (IS_ENABLED(CONFIG_PREEMPTION) && cpumask_size() <= sizeof(unsigned long)) + static_branch_enable(&ipi_mask_inlined); } /* Called by boot processor to activate the rest. */ -- 2.20.1