From mboxrd@z Thu Jan 1 00:00:00 1970 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 4F0B1C54E76 for ; Tue, 17 Jan 2023 18:23:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC6136B0074; Tue, 17 Jan 2023 13:23:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A75F36B0075; Tue, 17 Jan 2023 13:23:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9653E6B0078; Tue, 17 Jan 2023 13:23:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 839126B0074 for ; Tue, 17 Jan 2023 13:23:56 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4EC33A0636 for ; Tue, 17 Jan 2023 18:23:56 +0000 (UTC) X-FDA: 80365114872.09.C12FBBA Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf10.hostedemail.com (Postfix) with ESMTP id CDD0BC000D for ; Tue, 17 Jan 2023 18:23:54 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=mEluEUKE; spf=none (imf10.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673979834; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XSVCY3skPmpUfyMO6XuiNShJAohbJi5MJ+0Tl/OQUuk=; b=Rvqw5Z0ZPhTFIL9/DOB2GiPRKnyxoCCEZOuWSgB++of2uZ2yWw3cLGHMx0HAWeadTDA+53 VcnXe7coeB96qNbaAvlhI99Mj+r/sG7iloYElmgWec2xW3qxu+28kLl1j3Z0sXbb2HX2aX 3LLWLn3zyHtwd5judchDcZR0ctDiuDM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=mEluEUKE; spf=none (imf10.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673979834; a=rsa-sha256; cv=none; b=30XnY6mVIoY5ptV6vguRR64EwA41Z5k36XAnuk7JqomI4ujE11tsFYftWNA+qkP3ykh498 DN0w56twiRwjaZ9Bhtc/2C9q1C7lCzmQrDMwwDudEM3n23tkgy+yNuBI3bioVCWDfhGiAp AW4/aWHpItWBbOxmQsURBDeGd5POSqk= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.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; bh=XSVCY3skPmpUfyMO6XuiNShJAohbJi5MJ+0Tl/OQUuk=; b=mEluEUKEDZTyyDRpL261mqgUP9 +oWDZVX/939c909IkEujz+5khCUlk0qOmmCBOOt8jtKddSVusCBFb9ScfvddAieediYlWLsZGvbPU gGGXDadE03QIEDHX5V7yGFQsW7tadIpolftpzK+aHNUrxUhONvsFXMWrupAS+vHfu2z7nIdDpPLnr sUr6vc584V1N2NE72pohE5J1oRqScKE6VlFtORFuIFpAcUiPgUB+duQxe9dZ7JbgOqu24MFvdIZpo 06YDcK8usO6+UsjwIgUQzhshHBEVMboByXpfHEdkep+Eov/2x34RXGz4SHfdJiNQc/QTDhwNOULG5 FkfgV8aQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHqcQ-009uA2-Oe; Tue, 17 Jan 2023 18:23:34 +0000 Date: Tue, 17 Jan 2023 18:23:34 +0000 From: Matthew Wilcox To: Suren Baghdasaryan Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>, akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, liam.howlett@oracle.com, peterz@infradead.org, ldufour@linux.ibm.com, laurent.dufour@fr.ibm.com, paulmck@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, hughlynch@google.com, leewalsh@google.com, posk@google.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH 41/41] mm: replace rw_semaphore with atomic_t in vma_lock Message-ID: References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-42-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: CDD0BC000D X-Stat-Signature: 5f3sj9qz9ufkm8ydqkw3a7is93z49pam X-Rspam-User: X-HE-Tag: 1673979834-960522 X-HE-Meta: U2FsdGVkX18C66Fsb3YxE5xULW9M2CgpEugqzjxePqHLKkNCJRm5RHlsUywvJmCqrtxGKI0uaSnPds/lFKKbTovan7EtFv3mKoE15VGmX1hulifm6FSrHtbuVLr2HQCjxCvqOlg6S70aak7X2NzKlO1RRyNiOudFXwH9Nwk3yJonkkHyJk/CEI9G4LOX4RBK7G9fLH98K9HDjyb9G1psebQPU3FExChv/VYHZzyyBXCYFH5AYpl6u2Hc4QMTAK7ZBLz+a24hSwi/+k+m5aDFFZwoqEt4dXXA8kcx4pQKaFlT6+yx8uxUu/cy9i0+OO//TiPGGdCkTxu1n5JG5FdoK6srlGw5Ngu37jsYgrCaXJI1tEzmQC+kYiNkmAX2zvFi+Z6vxSTYOYChLMme6Mi9kc5rJVkVw4tGlKQE7vzkWzreodn3DlDBPUNdPqpwKcd0AaWoFvfuPhw0QEfsTQGoMe4rchuM0qIYfsvOPjgECw8NV2snzg3OvRENTtdhGWaLiTPvlSOX9PuZj/l7XErkp3wz4skPZUGKe/kBHF+uwgh3QuW0T/AGkba5AZKIOpdLM1ue15ZwEO2zOB9SxD8w9b6Ch1zjFXMsjAcLnE7HetVvBu81Z/x0YTgSwwIBiACCt8ckiaoffUdejIqgC8wfdy29WXYsaWSTK+C1PspOF/BllkEASpFZqApFkiGFLLmIxe2aetJ3CRG6FR3AgnBnV/79KIf7n3N4Y0/UB0mPifS7LaBIBHDA/uLO1kdI7wCE/eSaE1FGhdfNrjHOGYIAjY9Vgkot7oAtdJebOHYlpnLxH8L56u+FjowO7PmdCxwVtTyBU9Eeh1h2vUyek4oTFwH6NLphZUyIiNTllx4rkIOFGFqbqJhtcWleaF8+c6a10O+2TaOTTFc1bAUjt50gVto5GVFonHD+jH/iUN6pJyo+SHBtI794l24Y0Y8Nt6TBbdGJdr5OxPZiWC+4QqC cRHGDjnG B01y8 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, Jan 16, 2023 at 09:58:35PM -0800, Suren Baghdasaryan wrote: > On Mon, Jan 16, 2023 at 9:46 PM Matthew Wilcox wrote: > > > > On Mon, Jan 16, 2023 at 08:34:36PM -0800, Suren Baghdasaryan wrote: > > > On Mon, Jan 16, 2023 at 8:14 PM Matthew Wilcox wrote: > > > > > > > > On Mon, Jan 16, 2023 at 11:14:38AM +0000, Hyeonggon Yoo wrote: > > > > > > @@ -643,20 +647,28 @@ static inline void vma_write_lock(struct vm_area_struct *vma) > > > > > > static inline bool vma_read_trylock(struct vm_area_struct *vma) > > > > > > { > > > > > > /* Check before locking. A race might cause false locked result. */ > > > > > > - if (vma->vm_lock_seq == READ_ONCE(vma->vm_mm->mm_lock_seq)) > > > > > > + if (vma->vm_lock->lock_seq == READ_ONCE(vma->vm_mm->mm_lock_seq)) > > > > > > return false; > > > > > > > > > > > > - if (unlikely(down_read_trylock(&vma->vm_lock->lock) == 0)) > > > > > > + if (unlikely(!atomic_inc_unless_negative(&vma->vm_lock->count))) > > > > > > return false; > > > > > > > > > > > > + /* If atomic_t overflows, restore and fail to lock. */ > > > > > > + if (unlikely(atomic_read(&vma->vm_lock->count) < 0)) { > > > > > > + if (atomic_dec_and_test(&vma->vm_lock->count)) > > > > > > + wake_up(&vma->vm_mm->vma_writer_wait); > > > > > > + return false; > > > > > > + } > > > > > > + > > > > > > /* > > > > > > * Overflow might produce false locked result. > > > > > > * False unlocked result is impossible because we modify and check > > > > > > * vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq > > > > > > * modification invalidates all existing locks. > > > > > > */ > > > > > > - if (unlikely(vma->vm_lock_seq == READ_ONCE(vma->vm_mm->mm_lock_seq))) { > > > > > > - up_read(&vma->vm_lock->lock); > > > > > > + if (unlikely(vma->vm_lock->lock_seq == READ_ONCE(vma->vm_mm->mm_lock_seq))) { > > > > > > + if (atomic_dec_and_test(&vma->vm_lock->count)) > > > > > > + wake_up(&vma->vm_mm->vma_writer_wait); > > > > > > return false; > > > > > > } > > > > > > > > > > With this change readers can cause writers to starve. > > > > > What about checking waitqueue_active() before or after increasing > > > > > vma->vm_lock->count? > > > > > > > > I don't understand how readers can starve a writer. Readers do > > > > atomic_inc_unless_negative() so a writer can always force readers > > > > to fail. > > > > > > I think the point here was that if page faults keep occuring and they > > > prevent vm_lock->count from reaching 0 then a writer will be blocked > > > and there is no reader throttling mechanism (no max time that writer > > > will be waiting). > > > > Perhaps I misunderstood your description; I thought that a _waiting_ > > writer would make the count negative, not a successfully acquiring > > writer. > > A waiting writer does not modify the counter, instead it's placed on > the wait queue and the last reader which sets the count to 0 while > releasing its read lock will wake it up. Once the writer is woken it > will try to set the count to negative and if successful will own the > lock, otherwise it goes back to sleep. Then yes, that's a starvable lock. Preventing starvation on the mmap sem was the original motivation for making rwsems non-starvable, so changing that behaviour now seems like a bad idea. For efficiency, I'd suggest that a waiting writer set the top bit of the counter. That way, all new readers will back off without needing to check a second variable and old readers will know that they *may* need to do the wakeup when atomic_sub_return_release() is negative. (rwsem.c has a more complex bitfield, but I don't think we need to go that far; the important point is that the waiting writer indicates its presence in the count field so that readers can modify their behaviour)