Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08AFCC433F5 for ; Fri, 1 Apr 2022 12:16:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 48AF88D0002; Fri, 1 Apr 2022 08:16:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43A0A8D0001; Fri, 1 Apr 2022 08:16:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 301B48D0002; Fri, 1 Apr 2022 08:16:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 1CF768D0001 for ; Fri, 1 Apr 2022 08:16:01 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A0A9E24BAB for ; Fri, 1 Apr 2022 12:15:50 +0000 (UTC) X-FDA: 79308206460.12.7D72F44 Received: from r3-20.sinamail.sina.com.cn (r3-20.sinamail.sina.com.cn [202.108.3.20]) by imf14.hostedemail.com (Postfix) with SMTP id D683B100014 for ; Fri, 1 Apr 2022 12:15:47 +0000 (UTC) Received: from unknown (HELO localhost.localdomain)([114.249.57.134]) by sina.com (172.16.97.35) with ESMTP id 6246ECEE00009479; Fri, 1 Apr 2022 20:15:44 +0800 (CST) X-Sender: hdanton@sina.com X-Auth-ID: hdanton@sina.com X-SMAIL-MID: 48808315073604 From: Hillf Danton To: Waiman Long Cc: Peter Zijlstra , MM , David Hildenbrand , LKML Subject: Re: [RFC] locking/rwsem: dont wake up wwaiter in case of lock holder Date: Fri, 1 Apr 2022 20:15:33 +0800 Message-Id: <20220401121533.4979-1-hdanton@sina.com> In-Reply-To: References: <20220326134059.4082-1-hdanton@sina.com> <02f50037-46ce-ec08-63cb-e855694e69a5@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of hdanton@sina.com designates 202.108.3.20 as permitted sender) smtp.mailfrom=hdanton@sina.com; dmarc=none X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: D683B100014 X-Stat-Signature: 54tuq46ek5wbmkbtakbs96cas43zdbg5 X-HE-Tag: 1648815347-519129 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, 28 Mar 2022 11:11:31 -0400 Waiman Long wrote: > On 3/28/22 10:18, David Hildenbrand wrote: > > On 26.03.22 14:40, Hillf Danton wrote: > >> In the slowpath of down for write, we bail out in case of signal received and > >> try to wake up any pending waiter but it makes no sense to wake up a write > >> waiter given any lock holder, either write or read. > > > > But is handling this better really worth additional code and runtime > > checks? IOW, does this happen often enough that we actually care about > > optimizing this? I have no idea :) Thanks for taking a look, David. > > > >> The RFC is do nothing for wwaiter if any lock holder present - they will fill > >> their duty at lock release time. > >> > >> Only for thoughts now. > >> > >> Hillf > >> > >> --- x/kernel/locking/rwsem.c > >> +++ y/kernel/locking/rwsem.c > >> @@ -418,6 +418,8 @@ static void rwsem_mark_wake(struct rw_se > >> waiter = rwsem_first_waiter(sem); > >> > >> if (waiter->type == RWSEM_WAITING_FOR_WRITE) { > >> + if (RWSEM_LOCK_MASK & atomic_long_read(&sem->count)) > >> + return; > >> if (wake_type == RWSEM_WAKE_ANY) { > >> /* > >> * Mark writer at the front of the queue for wakeup. > >> -- > > That check isn't good enough. First of all, any reader count in > sem->count can be transient due to the fact that we do an unconditional > atomic_long_add() on down_read(). The reader may then remove its reader > count in the slow path. Correct. > This patch may cause missed wakeup which is a > much bigger problem than spending a bit of cpu time to check for lock > availability and sleep again. In rwsem_down_read_slowpath(), the comment prior to the RWSEM_WAKE_ANY wakeup rules out the chance for missed wakeup, because the RFC is only for wwaiter who is exclusive from any lock holder. It is not unusual for me to miss anything, OTOH, particularly in cases like this one worth twenty minutes of scratching scalp. Hillf /* * If there are no active locks, wake the front queued process(es). The RFC goes in line with the top half. * * If there are no writers and we are first in the queue, * wake our own waiter to join the existing active readers ! */