Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp709112ybc; Tue, 19 Nov 2019 08:03:21 -0800 (PST) X-Google-Smtp-Source: APXvYqxUOY6oHodQ96+gKJN8zSEQMC98tGIlgkdeRg65ixE3EYYvWVV/JCzKI3teYeQi+AY2HS6P X-Received: by 2002:a67:fc98:: with SMTP id x24mr21805433vsp.232.1574179400943; Tue, 19 Nov 2019 08:03:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574179400; cv=none; d=google.com; s=arc-20160816; b=bAs3vsgNhCq2Vy3U0DC4SFswPgvoujP41y05IfRL9TlX01UNRJfJC/GrsaMygBBCUj g2lYmM3VPG9Mp81qPyODWIFJx9zx5rUcRn2VLIGnzZ2cYfEqRahDtHU05wIj7zc1Yf6k kqwhOVlwOjjbxCUzEI18R9EEwzEYzyHyrT18SiVhVoLlREGeIyuO3RAKCs43XgK/Qdjf RnNm2gkcfHYXffu8MQqB4ykvlckkFehF0Te1G4iHZq34Pffy5cPFBocBUIqC58TB8cNx iEu/mKOyy+ujVpEFtfU7w3b4aKug87HHxCKmozj603szOvOMqg4Ift9k6oWpwIvjiGMI UBAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:user-agent:in-reply-to:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=QYuT05bQRH1TDY+TH4L5voF9XhTBnlMPUL3L+VxlLhk=; b=Qw4bdC1olOIDxjOK2NW8KYYRtZoK28BCyWK1a7h46z4fTDio9RosXS35Zuf+XgcWj/ g1s0RtWa2g5MqZR6Bww8vIgUsaHKv5IpM2CvepgErvg6ovLoO5j6pcL/u9pQWIDvmnqx xxYwNJAhczSTvtEbr3iJSQ/qxDtX6Ho2FLDJ8YLFAfs4POfvpdmRY4BQCH9kkJiDlLny 3ae7EkZOG6uhS7huKkRkt5Z0AhmnCwRhOfX/8V97UGjUOVip+neHtdQhKPJNDbImwPd3 xEeboxvE1YDhcrLCCkzWdoUg+ThekSiwRFmW23rOytYcpJHz5J5ac/YM9kHg97byDyYc A5qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NIrx3VSp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l11si14247199ejq.208.2019.11.19.08.02.43; Tue, 19 Nov 2019 08:03:20 -0800 (PST) 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=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NIrx3VSp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728361AbfKSP6q (ORCPT + 99 others); Tue, 19 Nov 2019 10:58:46 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:31551 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727591AbfKSP6q (ORCPT ); Tue, 19 Nov 2019 10:58:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574179124; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QYuT05bQRH1TDY+TH4L5voF9XhTBnlMPUL3L+VxlLhk=; b=NIrx3VSpR+2z71347mpCIptJwpfl3U7EgteqeE6rwtodnMuxDivjxxg6d9rO9BysQQtFSw TRW7yX0N1+iZEs2wNaCNm7RJmWUCVncVMeJ22TS5veqlaN+VVgJylGf5fADjCiv9XsVQOt hihd8LWBcPgIrz8h4JsQpqHQYhZUkW4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-298-FPZIHhWjMtCcDYIFUQk-0g-1; Tue, 19 Nov 2019 10:58:40 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E7C9C8DDCD6; Tue, 19 Nov 2019 15:58:38 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.43.17.44]) by smtp.corp.redhat.com (Postfix) with SMTP id D528360255; Tue, 19 Nov 2019 15:58:28 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 19 Nov 2019 16:58:37 +0100 (CET) Date: Tue, 19 Nov 2019 16:58:26 +0100 From: Oleg Nesterov To: Waiman Long Cc: Peter Zijlstra , mingo@kernel.org, will@kernel.org, tglx@linutronix.de, linux-kernel@vger.kernel.org, bigeasy@linutronix.de, juri.lelli@redhat.com, williams@redhat.com, bristot@redhat.com, dave@stgolabs.net, jack@suse.com Subject: Re: [PATCH 5/5] locking/percpu-rwsem: Remove the embedded rwsem Message-ID: <20191119155826.GA4739@redhat.com> References: <20191113102115.116470462@infradead.org> <20191113102855.925208237@infradead.org> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: FPZIHhWjMtCcDYIFUQk-0g-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/19, Waiman Long wrote: > > On 11/13/19 5:21 AM, Peter Zijlstra wrote: > > +static int percpu_rwsem_wake_function(struct wait_queue_entry *wq_entr= y, > > +=09=09=09=09 unsigned int mode, int wake_flags, > > +=09=09=09=09 void *key) > > +{ > > +=09struct task_struct *p =3D get_task_struct(wq_entry->private); > > +=09bool reader =3D wq_entry->flags & WQ_FLAG_CUSTOM; > > +=09struct percpu_rw_semaphore *sem =3D key; > > + > > +=09/* concurrent against percpu_down_write(), can get stolen */ > > +=09if (!__percpu_rwsem_trylock(sem, reader)) > > +=09=09return 1; > > + > > +=09list_del_init(&wq_entry->entry); > > +=09smp_store_release(&wq_entry->private, NULL); > > + > > +=09wake_up_process(p); > > +=09put_task_struct(p); > > + > > +=09return !reader; /* wake 'all' readers and 1 writer */ > > +} > > + > > If I read the function correctly, you are setting the WQ_FLAG_EXCLUSIVE > for both readers and writers and __wake_up() is called with an exclusive > count of one. So only one reader or writer is woken up each time. This depends on what percpu_rwsem_wake_function() returns. If it returns 1, __wake_up_common() stops, exactly because all waiters have WQ_FLAG_EXCLUSIV= E. > However, the comment above said we wake 'all' readers and 1 writer. That > doesn't match the actual code, IMO. Well, "'all' readers" probably means "all readers before writer", > To match the comments, you should > have set WQ_FLAG_EXCLUSIVE flag only on writer. In this case, you > probably don't need WQ_FLAG_CUSTOM to differentiate between readers and > writers. See above... note also the =09if (!__percpu_rwsem_trylock(sem, reader)) =09=09return 1; at the start of percpu_rwsem_wake_function(). We want to stop wake_up_commo= n() as soon as percpu_rwsem_trylock() fails. Because we know that if it fails o= nce it can't succeed later. Although iiuc this can only happen if another (new) writer races with __wake_up(&sem->waiters). I guess WQ_FLAG_CUSTOM can be avoided, percpu_rwsem_wait() could do =09if (read) =09=09__add_wait_queue_entry_tail(...); =09else { =09=09wq_entry.flags |=3D WQ_FLAG_EXCLUSIVE; =09=09__add_wait_queue(...); =09} but this is "unfair". Oleg.