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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 66B25C63797 for ; Tue, 17 Jan 2023 05:59:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=egOKUQcDoHl8WjUKeoRZFLnXrWxQo7pvpjVxEdhb1FE=; b=AC2h1rzGhwu3Cg BI3cxnik2NV4N49k3RMow8SPbfpPlp6yjVKQmZJ3D4OPiO0ytoKnx7moSs8mnKQqfYvehawXJS7vO 0Eoq3YlDL8FNeCYcBo6PujrdTaee3idrh6yYcYpjBCngPMt7OAv5avMZLnoPXUICWaI3dciwBgmvO 1nQDaV+jMSsf+Yo13kwlLfUaZsF8doi6OPd56wClLBppfQtdaqJzEOXFeHGd1t4qvByPa0fDI9Ub8 JLqCqc6b4oJTpAkXvNIrVqupNoCdtxE7W/ZtUsLpAvRGHJoptxVFiCCGpzLTyqwwfDy/e7PCu8Ysr xG8TpJiYGD7ilHGtJVew==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHezj-00D157-7V; Tue, 17 Jan 2023 05:58:51 +0000 Received: from mail-yb1-xb2e.google.com ([2607:f8b0:4864:20::b2e]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHezf-00D13S-SP for linux-arm-kernel@lists.infradead.org; Tue, 17 Jan 2023 05:58:49 +0000 Received: by mail-yb1-xb2e.google.com with SMTP id e202so9372664ybh.11 for ; Mon, 16 Jan 2023 21:58:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=B1UEa3M1jP3y0W29RzNqF9B+ZNIIlo3tNbOA7FKp7Wg=; b=szFCOms07Q53JQEnknyv2QcVn4fy3vBXVmKKspUTNnPlLtZTRF0d428TYfV9jDTsLX ycdeb1Skm451H4kEb5YktOhQk47tSUAqY6ZKdXDHxntmfT/T2YvRnXr8768wBLUZQYsf NloTHm46j/BZ7LoTaIJJF+P9tpVsuTHp1412qVOeK43J5yJ5SNABR1ITdDod7FabVFjH 0vuzHwBnFOhDtgJLMxLErKRZrVlCIUNa6khChg4g51rInsOaIMS3n4wIXMdHkkz6p02s pbHk2LcOHyoWkStGKc3E4jxHxQYWx4SfSGVVdA9DloQ/MHNTDm37A6udp1IgHDbr11kS kSTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=B1UEa3M1jP3y0W29RzNqF9B+ZNIIlo3tNbOA7FKp7Wg=; b=kEFZNIe5OU3JjfufaA5DzEkHX9fMc6KSNAwHhSFUa8nnCbio8ePRYqd+lD541DG2oE SV2PKwk/zFwzUVeJ6xXJ2hhIZxPHVUf4EVE8MIGmZ1Zb8/7bMMMstAlAxx7UgnDPM1H2 Q/i/lr19OqaRkYEJKfgkIKPZXfigFQ39erDxObPHOi6KDupyb2SbcQVAhYTqaNKHd2OZ H2qGWHNbjXHVSlhvNdb7aCiUsNHMOToy+cNjbwCumJGfBQ2DwB79dFEnYFRwrprTk+7A xmBHYP8TPd/WnDIHhNY3cH5CvQXDPxALBum5EC/6duxatXCWwnECBTV/44pzASWGd/bj NNFg== X-Gm-Message-State: AFqh2koGWDFLQiQiswIigQC0tpdksrL+aRy5O7zmtF4PONKxvfdumU0f jbv17DYOddmbpRgzt43uVpJH/ZZ4cOAWbfvz6s71Wg== X-Google-Smtp-Source: AMrXdXvdHf1+toW/1/obAcs98q3sqrmqpnAvEMhZ2E5TLuL8tC57gEqh2hzxqreEm8uMFGnaV8bTWU/bWAWqALbg44k= X-Received: by 2002:a25:f305:0:b0:7b8:6d00:ef23 with SMTP id c5-20020a25f305000000b007b86d00ef23mr296920ybs.119.1673935126396; Mon, 16 Jan 2023 21:58:46 -0800 (PST) MIME-Version: 1.0 References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-42-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Mon, 16 Jan 2023 21:58:35 -0800 Message-ID: Subject: Re: [PATCH 41/41] mm: replace rw_semaphore with atomic_t in vma_lock To: Matthew Wilcox 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230116_215847_959500_893D529D X-CRM114-Status: GOOD ( 24.64 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel