Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp4227945rwb; Mon, 16 Jan 2023 20:33:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXvfIm9RjTxe1hMnqVgp2YELgQ8gHPPzW4LyTteLw2evDH280crevsIn6U2FR1NSVF3KCc5h X-Received: by 2002:a17:906:6a27:b0:872:8c97:db27 with SMTP id qw39-20020a1709066a2700b008728c97db27mr1141825ejc.69.1673929992239; Mon, 16 Jan 2023 20:33:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673929992; cv=none; d=google.com; s=arc-20160816; b=hMh3R86TlJ5eAWRonRKJNqzR56GBuHM4uScHokTDOZwaoBIC1kLdcLH5Xs1rqMkPMR CWqrhHFdGyLznlpFw/Gm2T5uR2oxitEKSptp+LmWlZjyL+fuzr3sbu5Ta0qJF3yMoFVt x7b2j5Iopg3qhxrScclKy4dNPtC+NWAcsW77HmmT8HFKmPXs9vlCIDfta5jRGBgmlXrP rYuL1rdOGU0u2Z7Ql1isHpLywDkNw022amNwXvXf8YdCR9t0p8voCoPUvPxZnj7ebuVj Cj7VUq5fX1pU3dzttlkHkkE4fy4p+4SMbmYh8JQj1G3RKHFQbcnGVErIfJG47/E042Bo wo/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=eeaHVXVVYLUZhPJsx3SPIZ13iIAgts/Qp1qngWaTzqA=; b=jvm0AU+ccQrY/gBGtvJ2eNONqY/F8n+r1VTVpZnZenO7hhHSckpYt6ikCxnHVqoM0a IQTZqKg9cls/t0S9AJffTfstQEYFSV7GyqVJOI/LmEb6j24Yx0YG7cEms7nAyncjS/7J sAw4mP9LD/MoIlW5cz8dmqqTnkJ6+MjDGOIMq0xqkV2RKWHOImCRZMGy9D4rRXCbqf3V sSNaZloUAzm9tAOnQhGAtfUtkA6rdw60DPJsvl95Fxo6gndzRzKbo5IpJ6TjD1XGsRq7 VEc3WmvSTK1QZML+9xKfMK0EmUc4hp0HN3v3Jqz8xN5G3WXjAPwVmozQGvBA0WpG6meB IFfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=V1SsFbxr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xf5-20020a17090731c500b0084d7c8a4bdasi23164569ejb.285.2023.01.16.20.32.59; Mon, 16 Jan 2023 20:33:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=V1SsFbxr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235230AbjAQEPx (ORCPT + 52 others); Mon, 16 Jan 2023 23:15:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235303AbjAQEPT (ORCPT ); Mon, 16 Jan 2023 23:15:19 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4DA72385A for ; Mon, 16 Jan 2023 20:15:16 -0800 (PST) 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=eeaHVXVVYLUZhPJsx3SPIZ13iIAgts/Qp1qngWaTzqA=; b=V1SsFbxr+Ofyz74nBDy89AeApK ITCGUjTbUYk0t0GnfGJvQhOi9bTVMlDqqYPb8ZKyHGX5VKrdGajG/0IrlkT3xwVNJVJSs4MpOHcw4 1o3BcV8OuH63EI4/DaISzfONMgNKgWqv43cR4icParaJPBeZsXbFhtI/hZ9hVPY5QBmJ4kPQOQo9+ /yrPBQTtijA7/1rRlDK6Douvko5c/dkV2Ky2jYh5sVdhTSuHkgq0S24m5GoQcx34KzJOkOg50L8SH +o+lKJ45URFR4xZn3YUppCurr5mmiHlhfZGl6DFTS8eI4elQrX2C4k5DVWe8JUmKi0dPAYw1xpnER vA/RBWKA==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHdMg-009MFh-CK; Tue, 17 Jan 2023 04:14:26 +0000 Date: Tue, 17 Jan 2023 04:14:26 +0000 From: Matthew Wilcox To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Suren Baghdasaryan , 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-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.