Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1593904pxb; Wed, 2 Feb 2022 08:18:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJwv1/I8DWhjx+eE53WiUWdZXRO0Cfr88ZwCErq0HDbAWsFtbkwmr1FBXttW90FrXAvCOsfz X-Received: by 2002:a05:6402:35c5:: with SMTP id z5mr28251181edc.43.1643818685504; Wed, 02 Feb 2022 08:18:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643818685; cv=none; d=google.com; s=arc-20160816; b=MCeddo2sC8RP11BvlJ8oJ0j4WAdCU+bolbRy74g84EnQ9BTc2YDi6Eprf1fOtA2ivl v8IDbBCcsviPuCEyc+P6YymaHA9IeJb7ZuyVyLyl5EJDvrJbk/2O6D4bVeN7OI2x6hr+ md7Aialo4mf+H2FW3pRXjKkaoIQFNw0n4XraioByDSBnnCR9m2aaEb6fLVQY7kQ9PE7Q hPck+9RKKdkpaqrWO7uxhj/f5OqoebW899nrDjfxsFGQjPeYXdG6tSu2Z571iqq5fuFS dZ1sJ0Q9PCLVP8wyekGB3p32NfIJVbM6P/+Sj9E1dPbNy6uClFmD+IBhpCK2QQ78RMa2 XgvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:thread-index:thread-topic :content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:dkim-signature:dkim-filter; bh=VQmEXGRQQIxzpVJ3876L2AMeTU75MjY0bPrjRl6cGwE=; b=EHr8qHyCziDtBdaTPd4meughkDQpPgzaV8jqxVez25v25jbzF+VAERF43zctYJ8A/m SN0BEh/Asv0QVau4nJzpZIgUKIoO+3v40UnxWgsPizKhAu7nJtYXjAweGESmDY9otiqx 0946xMkRGnxiAsEnIhbll9zF7iZHWYe1c9/uuIkKjaDUfv4Q2ESCQ4/HZUO1ak6eItlc huTPbKyNUMyDwvOiKlEiCx97vKkYlZr5zIVXLvwG00BZTBoIYeb/okFKWKdUSwTxI1aD nxfe8UPzc3jgP7P0uONnumGinR/Pmh9W7nRFDdNheLP7DMGrHe1j0RTm3p0p1yTI5fm2 dfhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=F889MTHQ; 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 hq6si12731716ejc.802.2022.02.02.08.17.37; Wed, 02 Feb 2022 08:18:05 -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=F889MTHQ; 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 S1344443AbiBBNsx (ORCPT + 99 others); Wed, 2 Feb 2022 08:48:53 -0500 Received: from mail.efficios.com ([167.114.26.124]:39952 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229526AbiBBNsx (ORCPT ); Wed, 2 Feb 2022 08:48:53 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 76F2034F3DA; Wed, 2 Feb 2022 08:48:52 -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 cqdK2jswhzmF; Wed, 2 Feb 2022 08:48:51 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id B97F734F4E0; Wed, 2 Feb 2022 08:48:51 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com B97F734F4E0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1643809731; bh=VQmEXGRQQIxzpVJ3876L2AMeTU75MjY0bPrjRl6cGwE=; h=Date:From:To:Message-ID:MIME-Version; b=F889MTHQl7N/Rl9ZKzoYcNwkiBv4niYRAOnn/BDD+wA2A7SloHaYNlK38A2susBGO DHTIE5iziNt4pGtoHsIn68RFXqIvkQt56rEqHC607KtBmGZMaL5y8A9nW0U9jeojha ABEc6DWL3Mh5xMrYAyaH4a8pIoGiQSEC1KSR5TQzlWxBL5zJ72awB/RRUbHZJm/A6b RDp36Yh+XkzulKyoxxNGV7Dsz0WkWZDUdGPNuNaUIex8TKGLTe3BPEGBMB48vgBnl9 2Py1h7gRtvy6JRNAg3vgR7uDuPk43+ucrAzt9w0J1IDLvANkiuDP4dDuqcC51BnSrw QPDcAGP52HbvA== 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 0HZQSn_vItm0; Wed, 2 Feb 2022 08:48:51 -0500 (EST) Received: from mail03.efficios.com (mail03.efficios.com [167.114.26.124]) by mail.efficios.com (Postfix) with ESMTP id A5D6434F454; Wed, 2 Feb 2022 08:48:51 -0500 (EST) Date: Wed, 2 Feb 2022 08:48:51 -0500 (EST) From: Mathieu Desnoyers To: Peter Zijlstra Cc: linux-kernel , Thomas Gleixner , paulmck , Boqun Feng , "H. Peter Anvin" , Paul Turner , linux-api , Christian Brauner , Florian Weimer , David Laight , carlos , Peter Oskolkov Message-ID: <465039213.27161.1643809731530.JavaMail.zimbra@efficios.com> In-Reply-To: <20220202112343.GZ20638@worktop.programming.kicks-ass.net> References: <20220201192540.10439-1-mathieu.desnoyers@efficios.com> <20220202112343.GZ20638@worktop.programming.kicks-ass.net> Subject: Re: [RFC PATCH 1/3] Introduce per thread group current virtual cpu id MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [167.114.26.124] X-Mailer: Zimbra 8.8.15_GA_4203 (ZimbraWebClient - FF96 (Linux)/8.8.15_GA_4203) Thread-Topic: Introduce per thread group current virtual cpu id Thread-Index: yKlWk+fUpMMdDPyl2WKE2U6dJjx55g== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ----- On Feb 2, 2022, at 6:23 AM, Peter Zijlstra peterz@infradead.org wrote: > On Tue, Feb 01, 2022 at 02:25:38PM -0500, Mathieu Desnoyers wrote: > >> +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; >> +} > >> 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); > > > URGGHHH!!! that's *2* atomics extra on the context switch path. Worse, > that's on a line that's trivially contended with a few threads. There is one obvious optimization that just begs to be done here: when switching between threads belonging to the same process, we can simply take the vcpu_id tag of the prev thread and use it for next, without requiring any atomic operation. This only leaves the overhead of added atomics when scheduling between threads which belong to different processes. Does it matter as much ? If it's the case, then we should really scratch our heads a little more to come up with improvements. Thanks, Mathieu -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com