Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5456994rwl; Tue, 11 Apr 2023 05:55:02 -0700 (PDT) X-Google-Smtp-Source: AKy350b7Oqm80ghTRI7G1EapmeH7lz28e3FS+LwoddL5kH9iSlJq00aaPMNZz6vKKKk37lwRGW8/ X-Received: by 2002:a17:906:3bce:b0:930:fa8e:9597 with SMTP id v14-20020a1709063bce00b00930fa8e9597mr2221144ejf.42.1681217702565; Tue, 11 Apr 2023 05:55:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681217702; cv=none; d=google.com; s=arc-20160816; b=czyNeBOH1iP0N9m5ChRegyHs16ouJ0y60yGhK9D2tHGPMeq/hERIlb6Af1VXpIr6NP VU+/jG9cxjDsv5CM0ac3bUj7Y4n3cz7PIRS671kGTxWRAWmPWFH4qn9M4Bkn4yRte0qg SMavd1e/TBMJVp1nQeHyOD4ojNp+to4jz0u/4erXmR7X9c47BmT9Ixk+BDlXJtX3xmCY EMXAfShYb48+HyzNWx82CjgjYc90CXg12ZQMXcrExjwAwHwe5UzTKknWwdSqVyAQrne0 aF9nYtOawRq6ARp5E1ZALGsVYz+c+hVAkRVcFb5LeALfCNcMR1bYswqiNXz0JGR5KnSD VEqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=basrLjAsm8wm7MriM304pkB/w5P3SyYPslBbb07KhLQ=; b=p83pLRQ+kKuIz9WWGjAyo73FAMprcB7ZbDelJeSLaDRPS0T8oARfHs/48aR8uyG2+l 5pRlf8Dnbeho3sgXjjj6GI4cREO3wFb3+82hj1H7hINXYeEMhqusSCymnoTrQG0a8OXo n6JKwY44gZZzYWr82ZQyo7BzLKDAPFfyOcopF3SXiQFCw89ut936agwW4e06HgbyLWzE NZk1GlE8ZazCxa4VNC8up8E3p5a2Wb4Aa4e4gR+3JxFNAxvUOkj0ee7xDwgyz/Q29F+O YiWUGzUe5yfXT2+MDXGPKf/v3o6/SQF/t/nNH7kQJ00rhC6ue2wT7g5uw9qUXXAKvU2V s+DQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b=feLLcyQF; 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 f19-20020a50ee93000000b00504b1f83926si1432282edr.202.2023.04.11.05.54.37; Tue, 11 Apr 2023 05:55:02 -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=@efficios.com header.s=smtpout1 header.b=feLLcyQF; 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 S230116AbjDKMl2 (ORCPT + 99 others); Tue, 11 Apr 2023 08:41:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229920AbjDKMl0 (ORCPT ); Tue, 11 Apr 2023 08:41:26 -0400 Received: from smtpout.efficios.com (unknown [IPv6:2607:5300:203:b2ee::31e5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97212F4 for ; Tue, 11 Apr 2023 05:41:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1681216884; bh=feQnz7LEtrUjR3knhXb/iWIUFOvW72wj0TBYEI4P9CY=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=feLLcyQFTIG82JbvKvpJwlDTtzZMEGbSD0Nf94jBrzsa5GMvY552zq2NucnScCBf7 5yK8Yd6nQEPjrqX5cjNGFRm6pRVFtoI1/eHT7eDJ3sBjb3fjzFj1RX3tpCsmHAgcrA U7997JvirBE9+1S4yLUg9NBSfloDiswiMjjfrEY0MiSxE3Kw1gTo/SLwbM4tiF7QD6 CbqLe6J1RgtW4JyPbags8SeYlh4aTsMFNcjjZ+slGbou+VTvaICMFfJ1KE6Y76eAZC mTjvzhCjBrt7KmA4vq5sRVyeZ+9QaIBbkwyKil4amkwGSB2wQyH646tHFFyQ7ontPe Wz6KpOwhW3rOQ== Received: from [172.16.0.188] (192-222-143-198.qc.cable.ebox.net [192.222.143.198]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4Pwlnw67GlzvQy; Tue, 11 Apr 2023 08:41:24 -0400 (EDT) Message-ID: <7b5bf266-1418-1fe2-5d26-f94d3f67d49f@efficios.com> Date: Tue, 11 Apr 2023 08:41:25 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [RFC PATCH v3] sched: Fix performance regression introduced by mm_cid Content-Language: en-US To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Aaron Lu References: <20230405162635.225245-1-mathieu.desnoyers@efficios.com> <20230411085304.GB576825@hirez.programming.kicks-ass.net> From: Mathieu Desnoyers In-Reply-To: <20230411085304.GB576825@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, SPF_PASS 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 2023-04-11 04:53, Peter Zijlstra wrote: > 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; > Yes, done. >> + /* >> + * 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; done, Thanks! Mathieu > >> + __mm_cid_put(mm, src_cid); >> +} -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com