Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1499376pxb; Wed, 2 Feb 2022 06:23:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJzLrinp3LBa8bJH9/BiiF2DWdnm8HgUX7W6EVEX/shuCWL88g/jibsgyVklyYsKSXBwTzgK X-Received: by 2002:a17:903:41c6:: with SMTP id u6mr25036684ple.6.1643811831260; Wed, 02 Feb 2022 06:23:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643811831; cv=none; d=google.com; s=arc-20160816; b=J5qshWRpKb5+2a77jPALIX5tTdkcsJI5KnVhimg+cLF0yFQQg7T/804gQZsv5FCteM jw/4BDG7+ooRv6c+DY/uX5ehRmNjPHqMrUyxBVsGXR9mOXviOhTwYvn8Z3ls3orp3+4Y Ln43d37/D43h014GduNm1aHxjS5vIyyLaZ7F2JZY5RsQfBywyJUYa+O2WKvDVVqSFrkS FacPdJO2OnhHD8JW2WkbS/fHLtGtN2gZrzCIrjmEXodBXWsOIfWuoWGjnxWdhJQnikc3 efmsKQWOBBvMsE0aRsPlPsQyDFrmO7pNo9cF4+JbOLAyGgttg5PfngNiG9Yp3ynbKBPk HFFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=9E3Ighgjw9m12iC/P0bbBT1+B/3Uw1GcX37hKU7hT1E=; b=QIOuDDQdr9h+iNjG1apXju2sY9nGTngKtTHhuTXgHSypVCCbNyo+c8rMrNj+F0BuIa QPsCJ/+yDtwiC4B3KWzxKIePGq2leAHG6AQWeigfhuP2TjHbQDD6n4L0tzuNdJA5cPkd UItwxislfoXAq4kf7C4RVFEcwZtZ3bKJfXcIPNx+Jv5Arv6dmcl7ra4xuMtnYmi8x7RH vNWs2Ew2oeu79OeysuGe1rdTyKYNTcpYB4qx4Hr2SYgVGBjPAoaLvq8Z2AWtJA1s7P1c XMB3YYSjcPzb+UJbXCc6YG3HvG4IhT6BsZVu+4qbHXWDKvt1kH3pgeiebYO2qYHBF4ve rBpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=SnSnJ7PL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b6si4836031pjw.154.2022.02.02.06.23.38; Wed, 02 Feb 2022 06:23:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=SnSnJ7PL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242392AbiBAT0Z (ORCPT + 99 others); Tue, 1 Feb 2022 14:26:25 -0500 Received: from mail.efficios.com ([167.114.26.124]:53264 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239721AbiBAT0Y (ORCPT ); Tue, 1 Feb 2022 14:26:24 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id DF04933F866; Tue, 1 Feb 2022 14:26:23 -0500 (EST) Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id zKDloQarj2oE; Tue, 1 Feb 2022 14:26:23 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id DBB9033F8F9; Tue, 1 Feb 2022 14:26:22 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com DBB9033F8F9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1643743582; bh=9E3Ighgjw9m12iC/P0bbBT1+B/3Uw1GcX37hKU7hT1E=; h=From:To:Date:Message-Id; b=SnSnJ7PLCjW4p/E1891s+36iyUFJrNU/OIwXrZZDfr39U0iTBEEpxH+DGOPR9R5GV DG+4u2YZNwN+RkiepmvRH4RqtyPF0Aijx4LTDquIbS90ErBme87YicjlCqgv3afLGV vc74L/VUveHGcofZH1HCeaA/QZnj0qHFXLogtATCcvCqnmyBNWu9G7mFW5xQWCb91D HBVrGRIBQgl+yE2xPuFIwpF3uNb4sf0wVGDRMGeytKuouWm6WzSXNzogoMweItC/Pw DFLEHpgnwSvMFmdA+ybJDWltaOb7e17fT/laVbvZ3HFo7AiAMioPFAeXOIvZBW5ENh 6twK1NJIHaJVQ== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id X-Sc6Y3AlOlp; Tue, 1 Feb 2022 14:26:22 -0500 (EST) Received: from localhost.localdomain (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) by mail.efficios.com (Postfix) with ESMTPSA id 5D4CF33FBA1; Tue, 1 Feb 2022 14:26:22 -0500 (EST) From: Mathieu Desnoyers To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Thomas Gleixner , "Paul E . McKenney" , Boqun Feng , "H . Peter Anvin" , Paul Turner , linux-api@vger.kernel.org, Christian Brauner , Florian Weimer , David.Laight@ACULAB.COM, carlos@redhat.com, Peter Oskolkov , Mathieu Desnoyers Subject: [RFC PATCH 1/3] Introduce per thread group current virtual cpu id Date: Tue, 1 Feb 2022 14:25:38 -0500 Message-Id: <20220201192540.10439-1-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This feature allows the scheduler to expose a current virtual cpu id to user-space. This virtual cpu id is within the possible cpus range, and is temporarily (and uniquely) assigned while threads are actively running within a thread group. If a thread group has fewer threads than cores, or is limited to run on few cores concurrently through sched affinity or cgroup cpusets, the virtual cpu ids will be values close to 0, thus allowing efficient use of user-space memory for per-cpu data structures. This feature is meant to be exposed by a new rseq thread area field. Signed-off-by: Mathieu Desnoyers --- fs/exec.c | 4 +++ include/linux/sched.h | 4 +++ include/linux/sched/signal.h | 49 ++++++++++++++++++++++++++++++++++++ init/Kconfig | 14 +++++++++++ kernel/sched/core.c | 2 ++ 5 files changed, 73 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index 79f2c9483302..bc9a8c5f17f4 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1153,6 +1153,10 @@ static int de_thread(struct task_struct *tsk) sig->group_exec_task = NULL; sig->notify_count = 0; + /* Release possibly high vcpu id, get vcpu id 0. */ + tg_vcpu_put(tsk); + tg_vcpu_get(tsk); + no_thread_group: /* we have changed execution domain */ tsk->exit_signal = SIGCHLD; diff --git a/include/linux/sched.h b/include/linux/sched.h index 838c9e0b4cae..0f199daed26a 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1300,6 +1300,10 @@ struct task_struct { unsigned long rseq_event_mask; #endif +#ifdef CONFIG_SCHED_THREAD_GROUP_VCPU + int tg_vcpu; /* Current vcpu in thread group */ +#endif + struct tlbflush_unmap_batch tlb_ubc; union { diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index b6ecb9fc4cd2..c87e7ad5a1ea 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -244,6 +244,12 @@ struct signal_struct { * and may have inconsistent * permissions. */ +#ifdef CONFIG_SCHED_THREAD_GROUP_VCPU + /* + * Mask of allocated vcpu ids within the thread group. + */ + cpumask_t vcpu_mask; +#endif } __randomize_layout; /* @@ -742,4 +748,47 @@ static inline unsigned long rlimit_max(unsigned int limit) return task_rlimit_max(current, limit); } +#ifdef CONFIG_SCHED_THREAD_GROUP_VCPU +static inline void tg_vcpu_get(struct task_struct *t) +{ + struct cpumask *cpumask = &t->signal->vcpu_mask; + unsigned int vcpu; + + if (t->flags & PF_KTHREAD) + return; + /* Atomically reserve lowest available vcpu number. */ + do { + vcpu = cpumask_first_zero(cpumask); + WARN_ON_ONCE(vcpu >= nr_cpu_ids); + } while (cpumask_test_and_set_cpu(vcpu, cpumask)); + t->tg_vcpu = vcpu; +} + +static inline void tg_vcpu_put(struct task_struct *t) +{ + if (t->flags & PF_KTHREAD) + return; + cpumask_clear_cpu(t->tg_vcpu, &t->signal->vcpu_mask); + t->tg_vcpu = 0; +} + +static inline int task_tg_vcpu_id(struct task_struct *t) +{ + return t->tg_vcpu; +} +#else +static inline void tg_vcpu_get(struct task_struct *t) { } +static inline void tg_vcpu_put(struct task_struct *t) { } +static inline int task_tg_vcpu_id(struct task_struct *t) +{ + /* + * Use the processor id as a fall-back when the thread group vcpu + * feature is disabled. This provides functional per-cpu data structure + * accesses in user-space, althrough it won't provide the memory usage + * benefits. + */ + return raw_smp_processor_id(); +} +#endif + #endif /* _LINUX_SCHED_SIGNAL_H */ diff --git a/init/Kconfig b/init/Kconfig index e9119bf54b1f..5f72b4212a33 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1023,6 +1023,20 @@ config RT_GROUP_SCHED endif #CGROUP_SCHED +config SCHED_THREAD_GROUP_VCPU + bool "Provide per-thread-group virtual cpu id" + depends on SMP + default n + help + This feature allows the scheduler to expose a current virtual cpu id + to user-space. This virtual cpu id is within the possible cpus range, + and is temporarily (and uniquely) assigned while threads are actively + running within a thread group. If a thread group has fewer threads than + cores, or is limited to run on few cores concurrently through sched + affinity or cgroup cpusets, the virtual cpu ids will be values close + to 0, thus allowing efficient use of user-space memory for per-cpu + data structures. + config UCLAMP_TASK_GROUP bool "Utilization clamping per group of tasks" depends on CGROUP_SCHED diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 2e4ae00e52d1..2690e80977b1 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4795,6 +4795,8 @@ prepare_task_switch(struct rq *rq, struct task_struct *prev, sched_info_switch(rq, prev, next); perf_event_task_sched_out(prev, next); rseq_preempt(prev); + tg_vcpu_put(prev); + tg_vcpu_get(next); fire_sched_out_preempt_notifiers(prev, next); kmap_local_sched_out(); prepare_task(next); -- 2.17.1