Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp329507yba; Thu, 18 Apr 2019 01:53:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqy1YQlgupVYfpSU+gpKXOxf74ZJ0e1qM+aZzq5JXjo15uKUC+Gna0aMFQE9VPQYJyhqhzZU X-Received: by 2002:a62:448d:: with SMTP id m13mr95514450pfi.182.1555577632516; Thu, 18 Apr 2019 01:53:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555577632; cv=none; d=google.com; s=arc-20160816; b=uFhQtulbk32hFhzzJ376SsalCMfnb5E4E111j3KShsDYFqSPIVTa+aSPFB1so0eAJn OcKTewIJP8jFnfviwaNDXpmyZ3pc+9qXd4ORpP3ZH0y6Q5bhgSOpBX43jnsrOKMu/+Ye 5WZ2z0FhCJfaKiJgKEI8s5zCOi8wRpU8siWQplPUkwsRvmCX0Kp3bp2GdIwG6k9Z3EJO Syc3TC5Cqh6w1VBpzYBQo+YD17Y5o/H6HJWJn9dSTRLTSXvCKSIweTLWiPA5QAhmaIn/ J8BJAOp+90+92Eit6dgrHpRN8p7RhXy/ufF88WYDJG/tzHf5QMCms5qp45rYpmxefVis uWiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ABfuDNegMmvg64HlNWj/EUfOOiHsDeMbbxpXc0NqIN4=; b=XL+WpDXbenFyzvEa0VhEgyGTIhDhbxUkhqgPuOlN4CChGmZvab9jazjFcaj+zS7xTE +x4vQKgVwlDj7ML96OQF97ESsiDvCbC1eMb1g6LUjcGzVBvor+MvUjtk1osUYlazN7YE fXr+jLyhBraIZLQDxONSGIXCTnPt9Cih3G1wbBxYf0eZAiu6qwaMymE/y3qVqc0vzkRe KIwUWxwwJ/UVhelS6Uy3k5GfdWoLpoZZcxvaLCkB3ea2fvBa6Uz/zhaxF+7vvQfBcymm mQ1jlhAaGnUcHzD3ju6c4L+zOt92/jaVbrPK7ZD+7zx//gxJ3AkEc3gEybbqeVdL0m2C FPnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=fAK7nV75; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p14si1335007pgb.292.2019.04.18.01.53.37; Thu, 18 Apr 2019 01:53:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=fAK7nV75; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388201AbfDRIwn (ORCPT + 99 others); Thu, 18 Apr 2019 04:52:43 -0400 Received: from merlin.infradead.org ([205.233.59.134]:37426 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725747AbfDRIwm (ORCPT ); Thu, 18 Apr 2019 04:52:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.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:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=ABfuDNegMmvg64HlNWj/EUfOOiHsDeMbbxpXc0NqIN4=; b=fAK7nV75mYvErbSmiAxQBkXjX khDcOUJQU0c7rfZVGGW0+tHHV3CIoHN6gbRY2SxsR0Ir0upDNhjyYAw88qlHyY/gp3DOIRZRqwcAO oPKkmufVhv40v+t28qdYloXz7WKRTyIsavza5YQc6nxmLWoZHd7/KlFYc7OsqpBuF7GmmJslrDThL vIFQtNsNIfiFOBxw3yN4bAfD9jjQUfVA6wFdqQxJKNGQHiU815NaG4Qnc3fBW54QF/k5sUhSOjgNS MJRD+59JVF7uFGwKBLaV8DhTFfYUlchcy0DGRC1YYO4KuwSDUCdli/HtBqMLKkGQ9ISULuzPjBr09 pynYN87jA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hH2mT-0000j4-UE; Thu, 18 Apr 2019 08:52:30 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id E53A129B98550; Thu, 18 Apr 2019 10:52:27 +0200 (CEST) Date: Thu, 18 Apr 2019 10:52:27 +0200 From: Peter Zijlstra To: Waiman Long Cc: Ingo Molnar , Will Deacon , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Davidlohr Bueso , Linus Torvalds , Tim Chen , huang ying Subject: Re: [PATCH v4 09/16] locking/rwsem: Ensure an RT task will not spin on reader Message-ID: <20190418085227.GV12232@hirez.programming.kicks-ass.net> References: <20190413172259.2740-1-longman@redhat.com> <20190413172259.2740-10-longman@redhat.com> <20190417131841.GF4038@hirez.programming.kicks-ass.net> <1c37b8f2-232e-7ced-0483-31c8c6a4ffd1@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1c37b8f2-232e-7ced-0483-31c8c6a4ffd1@redhat.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 17, 2019 at 02:47:07PM -0400, Waiman Long wrote: > >> @@ -566,13 +573,28 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem) > >> } > >> > >> /* > >> - * When there's no owner, we might have preempted between the > >> - * owner acquiring the lock and setting the owner field. If > >> - * we're an RT task that will live-lock because we won't let > >> - * the owner complete. > >> + * An RT task cannot do optimistic spinning if it cannot > >> + * be sure the lock holder is running or live-lock may > >> + * happen if the current task and the lock holder happen > >> + * to run in the same CPU. > >> + * > >> + * When there's no owner or is reader-owned, an RT task > >> + * will stop spinning if the owner state is not a writer > >> + * at the previous iteration of the loop. This allows the > >> + * RT task to recheck if the task that steals the lock is > >> + * a spinnable writer. If so, it can keeps on spinning. > >> + * > >> + * If the owner is a writer, the need_resched() check is > >> + * done inside rwsem_spin_on_owner(). If the owner is not > >> + * a writer, need_resched() check needs to be done here. > >> */ > >> - if (!sem->owner && (need_resched() || rt_task(current))) > >> - break; > >> + if (owner_state != OWNER_WRITER) { > >> + if (need_resched()) > >> + break; > >> + if (is_rt_task && (prev_owner_state != OWNER_WRITER)) > >> + break; > >> + } > >> + prev_owner_state = owner_state; > >> > >> /* > >> * The cpu_relax() call is a compiler barrier which forces > > This patch confuses me mightily. I mean, I see what it does, but I can't > > figure out why. The Changelog is just one big source of confusion. > > Sorry for confusing you. If count and owner are separate, there is a > time lag where the owner is NULL, but the lock is not free yet. Right. > Similarly, the lock could be free but another task may have stolen the > lock if the waiter bit isn't set. > In the former case, (free) > an extra iteration gives it more time for the lock holder to release > the lock. > In the latter case, (stolen) > if the new lock owner is a writer and set owner in time, > the RT task can keep on spinning. Will clarify that in the commit log > and the comment. Blergh.. so by going around once extra, you hope ->owner will be set again and we keep spinning. And this is actually measurable. Yuck yuck yuck. I much prefer getting rid of that hole, as you do later on in the series, that would avoid this complecity. Let me continue reading...