Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2790995rda; Wed, 25 Oct 2023 12:21:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGTv4eEgWtOA1pvVRDE33F5MytO1oPkehi7k0ntFtreDnUcQ70dGBw//NHH37Be+XknwbNH X-Received: by 2002:a05:6830:1001:b0:6cd:87d:9f20 with SMTP id a1-20020a056830100100b006cd087d9f20mr15059274otp.29.1698261680464; Wed, 25 Oct 2023 12:21:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698261680; cv=none; d=google.com; s=arc-20160816; b=RrMMIntg4qb88+mdWTTQH57WkII2Wz6yjn+A+hQ+RsPq5YVN4TJIXFwix1fTGQ9TgS Mf6lUm8OpnRLaG4eIAAvYq1u+c7sZqC9GHjYOhRoE/d6wcO/1RTvviYGPoS+xjpSCYzm RPgzD+hXhbwXgdx8sOYKLdUIKts5mwXlX2v+mJJHV4cbtdC4lf1QpXC6NiYKFudL2SaN ummz+LHYOCQKev1Oyvb2YFJ+hpF9DE+o+SvSpS6usgRa0cS4Rt4Cf8m3CoI3VETLBXWR lLGUDg314WRA+X0tmEa1Ku0Iyr2lsEtb7952v1nmPQbYjLpWZLw23L2laNklDJxaQ8O8 YEXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=aN49d+i7xo05qkYfmNk2WHGyvgNebvOmk+TUiH2gPGQ=; fh=tyIqC0NL71LoJD2WSySiUhj2RxwBJETqgjcA66SUDTI=; b=okISjFMo/hkLKpvIeSt300bTQRcS5f7GOow3DU0PWjwBp1ILWg3HIwUWD5/1RIUvsW ZJHmfGGnPcyJQTEupqZ9Mhw6VRM3vvGMD3iLPm1kotmV36ttuBo+JZe2njV9Z1ly2cci mQRcu6z1EMeujaktr9va/Y32L0dvQvoDo3+aJsEilRj2tHzNjcJyPie48OZPEasNF9WT lYfhz4wuepvlm6/74H7WjQYTRYDQl1ln4ES+yk+5EhZTv4FlWC2dlpuMpbbA2NNMChCk K+8iysDwTnbLB8VPjtMULw2i977EnYahhAaCWOmidGYfw0EbI0craYL1A49CKRKr69+i L9AQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id k187-20020a0dc8c4000000b005a7af48d503si12719008ywd.338.2023.10.25.12.20.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 12:21:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 48E708028698; Wed, 25 Oct 2023 12:20:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229557AbjJYTUJ (ORCPT + 99 others); Wed, 25 Oct 2023 15:20:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbjJYTUI (ORCPT ); Wed, 25 Oct 2023 15:20:08 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2E8713A for ; Wed, 25 Oct 2023 12:19:59 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A6ACDC433C7; Wed, 25 Oct 2023 19:19:55 +0000 (UTC) Date: Wed, 25 Oct 2023 15:19:51 -0400 From: Steven Rostedt To: Mathieu Desnoyers Cc: Mateusz Guzik , Peter Zijlstra , LKML , Thomas Gleixner , Ankur Arora , Linus Torvalds , linux-mm@kvack.org, x86@kernel.org, akpm@linux-foundation.org, luto@kernel.org, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, willy@infradead.org, mgorman@suse.de, jon.grimm@amd.com, bharata@amd.com, raghavendra.kt@amd.com, boris.ostrovsky@oracle.com, konrad.wilk@oracle.com, jgross@suse.com, andrew.cooper3@citrix.com, Joel Fernandes , Youssef Esmat , Vineeth Pillai , Suleiman Souhlal , Ingo Molnar , Daniel Bristot de Oliveira Subject: Re: [POC][RFC][PATCH] sched: Extended Scheduler Time Slice Message-ID: <20231025151951.5f1a9ab1@gandalf.local.home> In-Reply-To: <0d95385f-1be1-4dcf-93cb-8c5df3bc9d0c@efficios.com> References: <20231025054219.1acaa3dd@gandalf.local.home> <20231025102952.GG37471@noisy.programming.kicks-ass.net> <20231025085434.35d5f9e0@gandalf.local.home> <20231025135545.GG31201@noisy.programming.kicks-ass.net> <20231025103105.5ec64b89@gandalf.local.home> <884e4603-4d29-41ae-8715-a070c43482c4@efficios.com> <20231025162435.ibhdktcshhzltr3r@f> <20231025131731.48461873@gandalf.local.home> <0d95385f-1be1-4dcf-93cb-8c5df3bc9d0c@efficios.com> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 25 Oct 2023 12:20:51 -0700 (PDT) On Wed, 25 Oct 2023 14:49:44 -0400 Mathieu Desnoyers wrote: > > > > No, I wouldn't say it's the same as priority inheritance, which is to help > > with determinism and not performance. PI adds overhead but removes > > unbounded latency. On average, a non PI mutex is faster than PI mutex, but > > can suffer from unbounded priority inversion. > > AFAIU, this is because PI mutex as implemented by sys futex only boosts the > priority of the lock owner. In my proposal here the owner would be able to > borrow scheduler slices from the waiters as well. I would be worried that that could cause even more scheduling disruption. Now we are taking time slices from other CPUs to run the current one? > > [...] > > >>> > >>> Hopefully I'm not oversimplifying if I state that we have mainly two > >>> actors to consider: > >>> > >>> [A] the lock owner thread > >>> > >>> [B] threads that block trying to acquire the lock > >>> > >>> The fast-path here is [A]. [B] can go through a system call, I don't > >>> think it matters at all. > > > > No, B going into a system call can be just as devastating. Adaptive > > spinning helps with that. The thing here is that if A gets preempted, there > > will be a lot more B's getting stuck. > > I would indeed combine this with an adaptive spinning scheme to allow waiters to > stay in userspace if contention is short. As you know, rseq can also help there: > > https://lore.kernel.org/lkml/20230529191416.53955-1-mathieu.desnoyers@efficios.com/ > > Therefore, it's only the blocking case that would call into the kernel, which > should not be so devastating. I think we are in agreement about the blocking case, which I believe you mean "go to sleep and wait to be woken up when the resource is available". > > > > > I implemented the test with futexes (where you go to sleep on contention) > > and the performance dropped considerably, where the benefits of not having > > A get preempted made no difference at all. Sure, adaptive spinning helps in > > that case, but adaptive spinning would only make it as good as my spinning > > in user space logic is without any changes. > > I'm not sure what you are arguing here. > > My overall idea would be to combine: > > 1) Adaptive spinning in userspace, > 2) Priority inheritance, > 3) Scheduler slices inheritance. The PI and slice inheritance can become very complex. It does sound more like the proxy server, which in theory sounds great but is very difficult to implement in practice. > > > > >>> > >>> Those lock addresses could then be used as keys for private locks, > >>> or transformed into inode/offset keys for shared-memory locks. Threads > >>> [B] blocking trying to acquire the lock can call a system call which > >>> would boost the lock owner's slice and/or priority for a given lock key. > > > > Do you mean that this would be done in user space? Going into the kernel to > > do any of this would make it already lost. > > Going to the kernel only happens when threads need to block, which means > that the typical contended half-happy path should be busy-spinning in userspace > (adaptive spinning). > > I see why blocking in a scenario where busy-spinning would be better is > inefficient, but I don't see how going to the kernel for the _blocking_ > case is bad. My point of view in this patch is for very short duration critical sections. This patch is not for the blocking use case at all. For that, I'm 100% on board with adaptive spinning. IOW, "blocking" is out of scope for this patch. > > > > >>> > >>> When the scheduler preempts [A], it would check whether the rseq > >>> per-thread area has a "held locks" field set and use this information > >>> to find the slice/priority boost which are currently active for each > >>> lock, and use this information to boost the task slice/priority > >>> accordingly. > > > > Why do we care about locks here? Note, I'm looking at using this same > > feature for VMs on interrupt handlers. The only thing user space needs to > > tell the kernel is "It's not a good time to preempt me, but it will be > > shortly". > > > > Quoting https://lore.kernel.org/lkml/20231024103426.4074d319@gandalf.local.home/ > > "The goal is to prevent a thread / vCPU being preempted while holding a lock > or resource that other threads / vCPUs will want. That is, prevent > contention, as that's usually the biggest issue with performance in user > space and VMs." I should have been more specific. Yes, locks is a major use case here, and the one I brought up as it's the easiest to understand, but this can be for any use case that there's a short critical section were preemption could be bad. I don't want to implement a policy that this is only for locking. > > We care about locks here because this is in fact your own problem statement. > If you want to consider the different problem of making VM interrupt handlers > go fast, then you should state it up front. Those two distinct problems may > or may not require entirely different solutions. We'll soon know as we'll be testing that too. Anyway, this patch is for the "short critical section". It's not good for all locking. It's only good for very short held locks. Going back to PREEMPT_RT, I did tests where I implemented NEED_RESCHED_LAZY on all kernel mutexes (it's currently only used for PREEMPT_RT spin locks that turn into mutexes), and found that it made no difference. Maybe a little bit, but not enough to push it. The reason is that long held locks, are either not contended heavily, or if they are, the fact that they are long held, keeping them running isn't as big of a difference than if they stop to let something else run. Of course, RT has priority inheritance, so the PI isn't as much of a overhead compared to the time the locks were held. > > >>> > >>> A scheme like this should allow lock priority inheritance without > >>> requiring system calls on the userspace lock/unlock fast path. > > > > Priority inheritance doesn't make sense when everything is running. > > I should have also said that this scheme should allow the lock owner to > borrow scheduler time slices from waiters (in addition to PI). As I mentioned, that sounds like proxy execution, which is something that's been a work in progress for some time now. Anyway, this patch is a very simple solution that can help a large number of use cases. Even if those use cases are very specific (highly contended short duration critical sections). -- Steve