Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp3796042pxb; Fri, 4 Feb 2022 17:17:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJwG9xVlIZyoxf7+b75AoI972SD+kd7oZEp0KkltHkh8EjU29/iROwkiXpdH4KGMDgqy73Ip X-Received: by 2002:a17:906:9ac4:: with SMTP id ah4mr1343000ejc.738.1644023876876; Fri, 04 Feb 2022 17:17:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644023876; cv=none; d=google.com; s=arc-20160816; b=RfZTKbo1iR6OIBHTwhR/33nNzk6Dd1EKuEaf8J0i8VhpxfcouOPmn/mVC3kRgN1DOb m2Ds62Z4r3W8I7Hi2ZcnBZGo4AjRVpBTzwKptfheTkbhexNy4zWDZpHJlHA82J0P4ATl QZxLVxamX0L4hs6TfgbhIZmzBF7bewFPdwUi+GB2DyIvr+Ydia7IxwUGrVSPVPzFGLrs 3FW/iEUsyZlJB3o1wDOyLqXS4K0wYxEBDHQo80mHkJUZq8Hm2jV/q9SPOQ2Gln+HZkQR wau6O4Mf6zy5L6824/Wg55vAQ1bSsEoc8GEhyyl8NgwmWTHLvY5ixu9Acmxw25HL0RJP g7nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=0uChyntsGbZTYF0NOYbJgym9Avq1awMCpANR+feQH8w=; b=0yadBykrRzAqrYRXtsf4wP1QjL12p7iVpmMzIxM7AA3HHFQDRoRzgaPifwGXhHE1Ac dL2LIJV1w2xe6XAEwpTnsH4JwgO34+cv6lXMsZoBoyk6mPlBCBLZyjkn8uz8qN0ef6/Q tWcicYg7Y5MmyIaLnPIX/jt3WFDDW0y4RHEg2ljnTh8/HlwswRI5vFkGzSn32+pdA1Uv X2bAXVEFDwJDePg2L+OiQ7EDY6fQ8oPdt2N5GWHBNYYYrxDTon59YLMX+yvhQP1tqEMo at73bFYukQU81tmFf+ym2ce6+jMfIqfjrq3uC/buj6W0BenV3HAZ1UcVq2tPae3epQgV d8KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=DYhEZ9Kr; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e12si2386438ejm.298.2022.02.04.17.17.21; Fri, 04 Feb 2022 17:17:56 -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=@infradead.org header.s=casper.20170209 header.b=DYhEZ9Kr; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343814AbiBBLYD (ORCPT + 99 others); Wed, 2 Feb 2022 06:24:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230478AbiBBLYC (ORCPT ); Wed, 2 Feb 2022 06:24:02 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D71C9C061714; Wed, 2 Feb 2022 03:24:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=0uChyntsGbZTYF0NOYbJgym9Avq1awMCpANR+feQH8w=; b=DYhEZ9Kr6q4Vf8w3RGMPtj5fer CeDPlxxRKjB8rYp0besQzPX5n1VUUl5wdTY02xwPKrz1GS/iw5KUJv85yQRLqiR1T9/JghiH1FdAM eh5Jz6bvL7r/WriU89jKlO8TANkIcRPNPtVuWC6yOLsHZzVy+F8LjKfKZAGIEGZfxVQTVubw9+tD4 CTS7fph0lnMnS+SHC2E5Xju+4aCx3OeTjeKy1/6/p4mWFmeS+VQQKyulRwbXOQZDSmVGeHILZVTIf wC8uHNKYY65xQ2KcEz9zVkSoAltQ8O9jHvtoqWmiaJP9jj9okWkuq3BL5FQdHe/OZoMjbpO4sk1R0 XcCAgJ3A==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=worktop.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nFDjk-00ESwW-8Y; Wed, 02 Feb 2022 11:23:44 +0000 Received: by worktop.programming.kicks-ass.net (Postfix, from userid 1000) id 1ACFB98327C; Wed, 2 Feb 2022 12:23:43 +0100 (CET) Date: Wed, 2 Feb 2022 12:23:43 +0100 From: Peter Zijlstra To: Mathieu Desnoyers 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 Subject: Re: [RFC PATCH 1/3] Introduce per thread group current virtual cpu id Message-ID: <20220202112343.GZ20638@worktop.programming.kicks-ass.net> References: <20220201192540.10439-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220201192540.10439-1-mathieu.desnoyers@efficios.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.