Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp889359pxb; Thu, 15 Apr 2021 08:51:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQ25jUf5yPFHVtMT2o5kY0V/5iagkKlYbVQyzYiH88Jf1aecA0K+AdAeLcrUVoqtXR4Qjf X-Received: by 2002:a63:c6:: with SMTP id 189mr3848948pga.216.1618501868369; Thu, 15 Apr 2021 08:51:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618501868; cv=none; d=google.com; s=arc-20160816; b=qnav6zhIhz+gpJeh9FWz0pChcs3tJtGv/DufKHba9816al/BG5Cq+I9Fh8lYk6cquL +dGdhHZH+zZ5ymiZxbheCUsnmEFTYITz4Sz2aGGZe9/vAZRAgccL9u7LqXK2e+2Rz1+y iw8OHHAovbQHBP0+Je1mOlsf7e37JOM8Wqghh79UeuKdGnxbR2zlsGAph7G6U7Ap+3+Z 48t9+Pik+hfQeGqa5WM5ux3GzUPlYHn5uxxQAOcWvSgmoRFUEvSR0Dpo0oQBEqdVw75e g9JjCIfeJwdSDUNsU4rSSGa96D9ibICNlRIpjpIficNl4kTpgVaBweoPG48rQhtohg5V LIlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=eGmt9Hgf0pAiMK10Vwq669jp41kQMM7HjH/YyUe9zvk=; b=HzZINP1sTgF33uRHnDpiOOezrU8zLvneMG6QXr7c4q8FCUGQOOuSfXjtsAf8PdePgU XpQORd2aLX9eWcFxcBXD4F7PNO+WR6v+Ltteue2s5ZwZkhVBRf/WM7j/MC/nL/RYgWp0 OKebU2lk8X74Y1dyB+tim3oBJOmDjsGIeLIxvTE7V/pZ8uiBkvRlvVrDKhJBmaQ78lMs dcGobVakjWmxm2lAwQBUiV0udbqVkBmojh7C1Ul32hQgzYVkBZVqMX1iy64S5lLa03uL SEa/vd1htAMx73wCWKT/ic20qM8xb9Rq1i7K4iJLfbpXHEDahKU6MiwRMOsXB//rnyK1 mKWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EBlF1wHU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j11si3293748plj.168.2021.04.15.08.50.55; Thu, 15 Apr 2021 08:51:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EBlF1wHU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233518AbhDOPuj (ORCPT + 99 others); Thu, 15 Apr 2021 11:50:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:36452 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231137AbhDOPuh (ORCPT ); Thu, 15 Apr 2021 11:50:37 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1DC2A610CE; Thu, 15 Apr 2021 15:50:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618501814; bh=+y8VIY5y82ytEDiO2xzY9qqH4agdV6HrrFbRCGjkdFY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=EBlF1wHUH9Xo3wnmCcIyAMTJi0XuRjVaac474qfUDyqASmy/pRNMrjZeaujjzi2U0 A8Z5HV37yhdStseMCJOL6cDo4OVLJP8WRxEUKPQS7twKArfRsX2k09f2pijAz7l07q 3yX5yRRthqrSHZDsa2ttQnIvPFkhPxLSmzaaZM8C8jBx5HpeZjaSQwtThseK6x1010 GeNDQDEAfW2+pB2KUCbctbRnZpvsntA8oSkuL0kZ3XeXAc7JsV1x3yQd5n7NbC29Il h9QG8LiuYQIOsIWFjarIebr2Pp4nVBzYJ1p34pK8wMCGK68HQ5PYB8+sTRkltGwnE0 A+opmTM0ZN9Tw== Date: Thu, 15 Apr 2021 16:50:10 +0100 From: Will Deacon To: Catalin Marinas Cc: Peter Zijlstra , Ali Saidi , linux-kernel@vger.kernel.org, steve.capper@arm.com, benh@kernel.crashing.org, stable@vger.kernel.org, Ingo Molnar , Waiman Long , Boqun Feng Subject: Re: [PATCH] locking/qrwlock: Fix ordering in queued_write_lock_slowpath Message-ID: <20210415155009.GA26594@willie-the-truck> References: <20210415142552.30916-1-alisaidi@amazon.com> <20210415152820.GB26439@willie-the-truck> <20210415153758.GF1015@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210415153758.GF1015@arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 15, 2021 at 04:37:58PM +0100, Catalin Marinas wrote: > On Thu, Apr 15, 2021 at 04:28:21PM +0100, Will Deacon wrote: > > On Thu, Apr 15, 2021 at 05:03:58PM +0200, Peter Zijlstra wrote: > > > diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c > > > index 4786dd271b45..22aeccc363ca 100644 > > > --- a/kernel/locking/qrwlock.c > > > +++ b/kernel/locking/qrwlock.c > > > @@ -60,6 +60,8 @@ EXPORT_SYMBOL(queued_read_lock_slowpath); > > > */ > > > void queued_write_lock_slowpath(struct qrwlock *lock) > > > { > > > + u32 cnt; > > > + > > > /* Put the writer into the wait queue */ > > > arch_spin_lock(&lock->wait_lock); > > > > > > @@ -73,9 +75,8 @@ void queued_write_lock_slowpath(struct qrwlock *lock) > > > > > > /* When no more readers or writers, set the locked flag */ > > > do { > > > - atomic_cond_read_acquire(&lock->cnts, VAL == _QW_WAITING); > > > - } while (atomic_cmpxchg_relaxed(&lock->cnts, _QW_WAITING, > > > - _QW_LOCKED) != _QW_WAITING); > > > + cnt = atomic_cond_read_acquire(&lock->cnts, VAL == _QW_WAITING); > > > > I think the issue is that >here< a concurrent reader in interrupt context > > can take the lock and release it again, but we could speculate reads from > > the critical section up over the later release and up before the control > > dependency here... > > > > > + } while (!atomic_try_cmpxchg_relaxed(&lock->cnts, &cnt, _QW_LOCKED)); > > > > ... and then this cmpxchg() will succeed, so our speculated stale reads > > could be used. > > > > *HOWEVER* > > > > Speculating a read should be fine in the face of a concurrent _reader_, > > so for this to be an issue it implies that the reader is also doing some > > (atomic?) updates. > > There's at least one such case: see chain_epi_lockless() updating > epi->next, called from ep_poll_callback() with a read_lock held. This > races with ep_done_scan() which has the write_lock held. Do you know if that's the code which triggered this patch? If so, it would be great to have this information in the changelog! > I think the authors of the above code interpreted the read_lock as > something that multiple threads can own disregarding the _read_ part. Using RmW atomics should be fine for that; it's no worse than some of the tricks pulled in RCU read context in the dentry cache (but then again, what is? ;) Will