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 5A8D6C46467 for ; Mon, 16 Jan 2023 23:09:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA61F6B0075; Mon, 16 Jan 2023 18:09:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A2F686B0078; Mon, 16 Jan 2023 18:09:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D0BA6B007B; Mon, 16 Jan 2023 18:09:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7B7B96B0075 for ; Mon, 16 Jan 2023 18:09:03 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4F826A05AB for ; Mon, 16 Jan 2023 23:09:03 +0000 (UTC) X-FDA: 80362204566.30.3396EF8 Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) by imf21.hostedemail.com (Postfix) with ESMTP id C9B581C0006 for ; Mon, 16 Jan 2023 23:09:00 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=E5v9Zy7z; spf=pass (imf21.hostedemail.com: domain of surenb@google.com designates 209.85.219.169 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673910540; 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=CCmcgNVg+h1QPPdn7LnXBY1iAHC/zKU20AQ2NLQkiFA=; b=Xp/Sf4EMYpx/cIvC73CmcetV4I/ZSIaOO+clg2xfBR+YcpZjcvBfvaVGyHcV7S9tVKPEVF STwzrIDhFJbm2h3n722Eqtw7+sC4EdUX8337t1pfDKK4lgjZxwxPA7f9EtL743SkHHTOwT pzNHs1NgeJLI3iNcRfPp2mEe8UBN40s= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=E5v9Zy7z; spf=pass (imf21.hostedemail.com: domain of surenb@google.com designates 209.85.219.169 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673910540; a=rsa-sha256; cv=none; b=2DILRgaDvREPyjkZsYFC2E9So7vn6z/g6YO/yBXdOxViD1cmw865JWBerITJgF4OcW3+lV JKTQuoYytDSqUMVIAtz9wmKH+5bseyuiopu0b14S1W3R3uw0kbBEoqx95ci3StCmnc9wzK Le1bgL9HNQbp8z6bu9xW1hhmf7smki0= Received: by mail-yb1-f169.google.com with SMTP id e130so2446011yba.7 for ; Mon, 16 Jan 2023 15:09:00 -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=CCmcgNVg+h1QPPdn7LnXBY1iAHC/zKU20AQ2NLQkiFA=; b=E5v9Zy7zWLUAijIhoGuGY4A7Xz+Skyg7ATIc7CA2K5y4gPwwEHP3JyJi9u8cJNM6zQ ABidkAs9IsO2IctTC62zMYzOvSCSpVcI3+ZCRk6sEZ820fBC5i1bh7W2sSwApNH5iUzJ 2+T/SDFJTtm4RinEZEfFdXlU0vXW9g0/ctzKRkOm56VyO8C/eQrXFv6rxV36A196aq48 bJQiAJLN19gsaoiFYIrVUKjyiTzWHzA6mN5YfCzSYnCS9ut/YFoZm1nqvNpbeowyr5lO PXsSS/eonuyF1qDGfe3g11a70vBcslP59ZXN3BINJauHaWN5hJEmrr2Nn2viPs9lmFN4 POHA== 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=CCmcgNVg+h1QPPdn7LnXBY1iAHC/zKU20AQ2NLQkiFA=; b=MX5rGsXN10A3vTykutPs/MNYyG/R3QF752Q+2OaVhS7nt1e9y++RtKblKAyAYrZ3ee lJAjl77k/QQViUuQ9Ajd7faWG8shros8cxrNAiXqURWOiikCHtSjWCsrtdsY02iJJp9p JybTQDFRf5R7W2eskmnFkPisCXOp2CCjF4p9JP31hTqH+ZPZawhnqdemmxIy9h1UB12X rpNJUD4K1d4/E4+aHCaVzOQhCzmCjbeaK6zdZtZLIf6hVXlvngLZCQu7AhB58jPr+jhR SNLpbOnhZ4e/iS8O0YxSBVvOHIIy253/0n0mZAEaAw9hBy6pOAYWdJw35iWmBIka2vK+ Kxhg== X-Gm-Message-State: AFqh2koHw8jnde8aHQWKniLo0xmVbgM3y9biK2GhHVXD1hmt8TUQiUXd EMa/N21eSEDvRGn2erFjSdNBAw5VQOFt3iVDgiBAp5zGLwy2Wp03 X-Google-Smtp-Source: AMrXdXuC83KCNmLbi9dxB0b39+OgMITEOwaUjUEh+a4kfZnL/EJltO0eVDwXIinTp2Ug+3vGt1V8Z9C8FT7VNVA2HUM= X-Received: by 2002:a05:6902:11cd:b0:7d6:c4f6:b4ea with SMTP id n13-20020a05690211cd00b007d6c4f6b4eamr139498ybu.59.1673910539752; Mon, 16 Jan 2023 15:08:59 -0800 (PST) MIME-Version: 1.0 References: <20230109205336.3665937-42-surenb@google.com> <20230116140649.2012-1-hdanton@sina.com> In-Reply-To: <20230116140649.2012-1-hdanton@sina.com> From: Suren Baghdasaryan Date: Mon, 16 Jan 2023 15:08:48 -0800 Message-ID: Subject: Re: [PATCH 41/41] mm: replace rw_semaphore with atomic_t in vma_lock To: Hillf Danton Cc: vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, peterz@infradead.org, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: h3grn65e3wg34fu1mqi7ox318obcsz1g X-Rspam-User: X-Rspamd-Queue-Id: C9B581C0006 X-Rspamd-Server: rspam06 X-HE-Tag: 1673910540-553974 X-HE-Meta: U2FsdGVkX18aZzv7TljfghV6FHF73hieat/HnUIxjFeS3auCmz/7/EU87Vp6tRX7IrB10oQQC43JFtVbg1kBma/ufOM3Yoo7PVHAQ6lSTmNRMremY1XWpFRoLYS6Ex4iuVi5qZRTmHqTst4gBnm8J6fP2Qm1Mv3GF3tHhVT41vtf9xmfgKmAYTD2aWtj0F2KGlG+s8FgVRnZBMKBW1Frt+VZUShn21zYw6+TlKswVg3AYJgCXQ5T8Ee3NbSuF8HZ4KWNE1Y7rdCFmwW/F7L64lEWRK9tZhvtFMryTiUv3WN8MAvP4dMK/ZG6eNmDDedq6NfgRJlSqFZHXiyw3QRy2/dT4fuue0uGdakwqKnfShByIiAuG/zvJ4BeZm5RgNQtDekJETE4/IDCqpI1UaEEMabuTXIFtIK3l5yRsoQPOsMAJHbojx1+Kg3nZes0Zm64UcUXJwws0+36vxTazCGml31dFL+6962DzfWKLl0zMMmbYtPuDwp1pGPtfvp96Kq9IkatNyin5R/7j476ek/oyIwB+Y2eBNz2aa06Oyz1dZ2tEt3NOA+SAC7Sdjso2W0EXGzSn8kdvGVQ0qWUkbnQkBkUJrz000SkPQUkg4PjnPP9wJSQNM/ufBru7youbyHJyev46gdjNJ1xTXX+IJw52msVSipK15iTUJdn70bzjp8GqbkBs1oOJiBUp7yUcpQVX+iGAfr1SaCAb8ZZFgpHdbJLiysG46X3ImI1cwbL8O06TX0l/kGuXctbIwjftIbX5KzEqqhWp77CYfjumx4ym7iiVRRvkGvIrzr/I+aXuafzMzcaLVoZMHHuoBHk2E4F7uFSwSqMLQ1ZhBiJyjrl6RqIbKRUxAxtXuI435X7omJzV+ZOfpPXGf6Q0JncNsLU9FqVBJP3zYqRUFhKKLdxUrdHV1z6oKGuIR8M1732XRER9Zuolc2M2AfDGuL4nasUd2xxWBswdLl/Pnc7N3S 5CmGTDqS ljJeaur9dic7S1MTUt0BNligvR8SXxKzYGSwRwCxEWlnXbFiFGir2D5N7ug== 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 6:07 AM Hillf Danton wrote: > > On Mon, 9 Jan 2023 12:53:36 -0800 Suren Baghdasaryan > > --- a/include/linux/mm.h > > +++ b/include/linux/mm.h > > @@ -627,12 +627,16 @@ static inline void vma_write_lock(struct vm_area_struct *vma) > > * mm->mm_lock_seq can't be concurrently modified. > > */ > > mm_lock_seq = READ_ONCE(vma->vm_mm->mm_lock_seq); > > - if (vma->vm_lock_seq == mm_lock_seq) > > + if (vma->vm_lock->lock_seq == mm_lock_seq) > > return; > > lock acquire for write to info lockdep. Thanks for the review Hillf! Good idea. Will add in the next version. > > > > - down_write(&vma->vm_lock->lock); > > - vma->vm_lock_seq = mm_lock_seq; > > - up_write(&vma->vm_lock->lock); > > + if (atomic_cmpxchg(&vma->vm_lock->count, 0, -1)) > > + wait_event(vma->vm_mm->vma_writer_wait, > > + atomic_cmpxchg(&vma->vm_lock->count, 0, -1) == 0); > > + vma->vm_lock->lock_seq = mm_lock_seq; > > + /* Write barrier to ensure lock_seq change is visible before count */ > > + smp_wmb(); > > + atomic_set(&vma->vm_lock->count, 0); > > } > > > > /* > > @@ -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; > > Add mb to pair with the above wmb like The wmb above is to ensure the ordering between updates of lock_seq and vm_lock->count (lock_seq is updated first and vm_lock->count only after that). The first access to vm_lock->count in this function is atomic_inc_unless_negative() and it's an atomic RMW operation with a return value. According to documentation such functions are fully ordered, therefore I think we already have an implicit full memory barrier between reads of lock_seq and vm_lock->count here. Am I wrong? > > if (READ_ONCE(vma->vm_lock->lock_seq) == READ_ONCE(vma->vm_mm->mm_lock_seq)) { > smp_acquire__after_ctrl_dep(); > 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; > > } > > Simpler way to detect write lock owner and count overflow like > > int count = atomic_read(&vma->vm_lock->count); > for (;;) { > int new = count + 1; > > if (count < 0 || new < 0) > return false; > > new = atomic_cmpxchg(&vma->vm_lock->count, count, new); > if (new == count) > break; > count = new; > cpu_relax(); > } > > (wake up waiting readers after taking the lock; > but the write lock owner before this read trylock should be > responsible for waking waiters up.) > > lock acquire for read. This schema might cause readers to wait, which is not an exact replacement for down_read_trylock(). The requirement to wake up waiting readers also complicates things and since we can always fall back to mmap_lock, that complication is unnecessary IMHO. I could use part of your suggestion like this: int new = count + 1; if (count < 0 || new < 0) return false; new = atomic_cmpxchg(&vma->vm_lock->count, count, new); if (new == count) return false; Compared to doing atomic_inc_unless_negative() first, like I did originally, this schema opens a bit wider window for the writer to get in the middle and cause the reader to fail locking but I don't think it would result in any visible regression. > > > return true; > > @@ -664,7 +676,8 @@ static inline bool vma_read_trylock(struct vm_area_struct *vma) > > > > static inline void vma_read_unlock(struct vm_area_struct *vma) > > { > lock release for read. Ack. > > > - up_read(&vma->vm_lock->lock); > > + if (atomic_dec_and_test(&vma->vm_lock->count)) > > + wake_up(&vma->vm_mm->vma_writer_wait); > > } >