Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5217298rwl; Tue, 11 Apr 2023 02:04:53 -0700 (PDT) X-Google-Smtp-Source: AKy350YSqzh9/3dd1ZLKlFzDtV57ySohNo3FW4qMSlQi5W4xeuU9V2l3gG2V4YqlsGXgvOdTQrQ/ X-Received: by 2002:a17:906:cec1:b0:94b:869b:267 with SMTP id si1-20020a170906cec100b0094b869b0267mr4182471ejb.28.1681203893668; Tue, 11 Apr 2023 02:04:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681203893; cv=none; d=google.com; s=arc-20160816; b=bKXdqkXCcABcZ4DrKa8KJkZeTZ36Z/9No6FAj1gAVhKY/esG2Hc1FAJDT79wUMFcLE niCs9nWSmecqpu6DrmaToHn8xz9TjXhaBjmTq1Rk60yp9tLVBbfxZXhlrp1jgBAg36rb lukxGMbg5uhFjJgqtnCuHJj2Fnwm7WBksAGf3R119/tNxuGwntKAR4Sj5sGSgRrXzr/H kfxnmGk9XvmNWV7aR3VPjr9DYmNbfb6CjSn+qs+xb7oYZvlFJH6jFBm8280x7H/whLfw mjC7Rpo8pOFjC6d0K2RLcLmWlL19IwREa4V8Qeiuc3SMMxFPMEf8nwKAaPTqaRh8Hlyj Jb4g== 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=DYZqFeT2TkDql7LNr9KHtQzkAfQ3U+U8rxb08iVVG8k=; b=q/e3dRhLmrhMubED5Bu7qkYNQst3AKMXwL0xqtujDeZS4v/g2xCo7DpvFaEoq/pHAg ll+y4Y2ovJIjca9zjUq38uLWSvlH/ErXlOk6GVsE2KP+SEpEbNVEI6ovvRF+AbV7Ketj TqCWThhj0ylLFAtqxM7DD/ubfIvyygXGWQ8hoYId+plEShILjjptweKJT5YYXA9I2JiA WTKZbG9M7wYCk3onKKVrDvzNumr42OadJX0+2Bc2YeqhRr4jHultogdTAWxcC4C8073Y yc9Enu2aU73k053cA1afNZNzMPWewSaqwbDCToaOQP0UFuo/rbH3f2vkmqqKBywwsS8Q 4xkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=ANfLtIK3; 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 kt1-20020a170906aac100b0094a9116306fsi845240ejb.471.2023.04.11.02.04.28; Tue, 11 Apr 2023 02:04:53 -0700 (PDT) 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=desiato.20200630 header.b=ANfLtIK3; 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 S229950AbjDKIxR (ORCPT + 99 others); Tue, 11 Apr 2023 04:53:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229721AbjDKIxP (ORCPT ); Tue, 11 Apr 2023 04:53:15 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 675D9E76 for ; Tue, 11 Apr 2023 01:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; 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=DYZqFeT2TkDql7LNr9KHtQzkAfQ3U+U8rxb08iVVG8k=; b=ANfLtIK31XNIkd7xjVRtNNRwiA xBY9XVTDaO7y7O1ptc7tPHUb96LAHbeoDa8j/awII5eLEQp5J65eN8mPbXEIAo7cmhPcjS+j/bSj6 3PaFxh4lxO1jR/9V5grKWHRIPo4NLIP8hK2y/87PESjrtUI2uVGitKjrL+D+nF86nMUYLks0AFaUD C/ekRkWRvYeac3efpN7XXLTLHoOeDTn1U+x+gt7PtZtEGKiXPV+LBAlfdyUFBDADMk0tikP2Eodzp e6zMB4oiS9yfLQZspZ+vFAkiw3+lv/ypx2TiWuT2+nlZFgiMgXCZ7Cm2crrUAy3khXBM5ITd5BRkS wNyY2YFg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pm9kP-00DIBx-29; Tue, 11 Apr 2023 08:53:06 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 152D830002F; Tue, 11 Apr 2023 10:53:05 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id F257E2BC3D07B; Tue, 11 Apr 2023 10:53:04 +0200 (CEST) Date: Tue, 11 Apr 2023 10:53:04 +0200 From: Peter Zijlstra To: Mathieu Desnoyers Cc: linux-kernel@vger.kernel.org, Aaron Lu Subject: Re: [RFC PATCH v3] sched: Fix performance regression introduced by mm_cid Message-ID: <20230411085304.GB576825@hirez.programming.kicks-ass.net> References: <20230405162635.225245-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230405162635.225245-1-mathieu.desnoyers@efficios.com> X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 05, 2023 at 12:26:35PM -0400, Mathieu Desnoyers wrote: > +void sched_mm_cid_migrate_from(struct task_struct *t) > +{ > + /* > + * If the source cpu cid is set, and matches the last cid of the > + * migrated task, clear the source cpu cid to keep cid allocation > + * compact to cover the case where this task is the last task using > + * this mm on the source cpu. If there happens to be other tasks left > + * on the source cpu using this mm, the next task using this mm will > + * reallocate its cid on context switch. > + * > + * We cannot keep ownership of concurrency ID without runqueue > + * lock held when it is not used by a current task, because it > + * would lead to allocation of more concurrency ids than there > + * are possible cpus in the system. The last_mm_cid is used as > + * a hint to conditionally unset the dst cpu cid, keeping > + * allocated concurrency ids compact. > + */ > + if (cmpxchg(src_pcpu_cid, src_cid, mm_cid_set_lazy_put(src_cid)) != src_cid) > + return; > + FWIW, I'm thinking that if we write this using try_cmpxchg() it'll be a little nicer: lazy_cid = mm_cid_set_lazy_put(src_cid); if (!try_cmpxchg(src_pcpu_cid, &src_cid, lazy_cid)) return; > + /* > + * The implicit barrier after cmpxchg per-mm/cpu cid before loading > + * rq->curr->mm matches the scheduler barrier in context_switch() > + * between store to rq->curr and load of prev and next task's > + * per-mm/cpu cid. > + * > + * The implicit barrier after cmpxchg per-mm/cpu cid before loading > + * rq->curr->mm_cid_active matches the barrier in > + * sched_mm_cid_exit_signals(), sched_mm_cid_before_execve(), and > + * sched_mm_cid_after_execve() between store to t->mm_cid_active and > + * load of per-mm/cpu cid. > + */ > + > + /* > + * If we observe an active task using the mm on this rq after setting the lazy-put > + * flag, this task will be responsible for transitioning from lazy-put > + * flag set to MM_CID_UNSET. > + */ > + rcu_read_lock(); > + src_task = rcu_dereference(src_rq->curr); > + if (src_task->mm_cid_active && src_task->mm == mm) { > + rcu_read_unlock(); > + /* > + * We observed an active task for this mm, clearing the destination > + * cpu mm_cid is not relevant for compactness. > + */ > + t->last_mm_cid = -1; > + return; > + } > + rcu_read_unlock(); > + > + /* > + * The src_cid is unused, so it can be unset. > + */ > + if (cmpxchg(src_pcpu_cid, mm_cid_set_lazy_put(src_cid), MM_CID_UNSET) != mm_cid_set_lazy_put(src_cid)) > + return; if (!try_cmpxchg(src_pcpu_cid, &lazy_cid, MM_CID_UNSET)) return; > + __mm_cid_put(mm, src_cid); > +}