From patchwork Thu Feb 16 05:17:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13142505 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 58678C636CC for ; Thu, 16 Feb 2023 05:18:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3E866B008A; Thu, 16 Feb 2023 00:18:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DC7E56B008C; Thu, 16 Feb 2023 00:18:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C18DA6B0092; Thu, 16 Feb 2023 00:18:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A75696B008A for ; Thu, 16 Feb 2023 00:18:27 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7AACB1603AF for ; Thu, 16 Feb 2023 05:18:27 +0000 (UTC) X-FDA: 80471999454.14.DCA9F78 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf09.hostedemail.com (Postfix) with ESMTP id AF75B14000A for ; Thu, 16 Feb 2023 05:18:25 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=jg6fXXyN; spf=pass (imf09.hostedemail.com: domain of 3oLztYwYKCPUprobkYdlldib.Zljifkru-jjhsXZh.lod@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3oLztYwYKCPUprobkYdlldib.Zljifkru-jjhsXZh.lod@flex--surenb.bounces.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=1676524705; 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=S0NkKUx4dkIelWS5aTsq7UgpXIDBIhGrMRdd8DrqTMg=; b=E8KpVZzf+aMTVIqCOc5SwqtUp/KA4unTvYWIWPXsjZjb//D3M0U6yoBtpLXEw1VQHTL0hd AzpvvhaqqstccsYH3O+a7rGmIGAngm2ppRAtVdFg+v4XSrY/O/d3V7Smjlo1Z+G1qj+ifO kVUQ52+6MU4Wcihpl5oEIxrvLq/8TGI= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=jg6fXXyN; spf=pass (imf09.hostedemail.com: domain of 3oLztYwYKCPUprobkYdlldib.Zljifkru-jjhsXZh.lod@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3oLztYwYKCPUprobkYdlldib.Zljifkru-jjhsXZh.lod@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676524705; a=rsa-sha256; cv=none; b=bAIJVptX3P4EktuK+1lSCS/0CzuM4AwA01Hrhzcqq/AMAG0c6DRzxpSc+7x958HNLjJibC sVPV3FHIiHwfFwCqcaBYgaFdxZjiY+dXK6iYKPMegc83G6O+FhTii6MTi3bGC4Foaxsn3H lPwpy9/+Iup4Hy4QEZnniO6MQnSAmUs= Received: by mail-yb1-f201.google.com with SMTP id e191-20020a2537c8000000b009433a21be0dso836281yba.19 for ; Wed, 15 Feb 2023 21:18:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=S0NkKUx4dkIelWS5aTsq7UgpXIDBIhGrMRdd8DrqTMg=; b=jg6fXXyNPA7kGzHuabxhVQS/lUkV32r/2v7x3huSOdcQqflpdfqhoIlNaR0t9wMIZ5 tNsdAuEIvz4tV4UF6ouWYwPH6jjBst45X8ZS39R/or9bhVEvk/hAhXAhGiG6HODkz0Ti 4+W9CepIhu/sbVVhPdRUrFOaALk+XM5cteC/he2x1EN76QEzJ6hbvpDzGk804gLMUejh OhIx76lfAQ2Aj8i3vKfn29ePpUTgzAK6jOUxJ6v4seZ7ANgrIyb45IfAAVauV03+BjjR E1pd5h52L83bbDKmbPDtlCRxv6trm/z+Fa4Qs6PgKsiEdeHNP77Ta8uAxqqtUigosnhI vlvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=S0NkKUx4dkIelWS5aTsq7UgpXIDBIhGrMRdd8DrqTMg=; b=BDXJ6E4h89LorbCJ453+L9CAzv5VNwx8jp4pgdB+n8x2aWFChOyKsD8x99ci9ONfGs gByD1olpsthtCLGUJc7eYPm0FjqnmVr54UoismLocvYWMIlU3z9n9H/GKoAg4m1RnTai i0dGXT4cwAu/RzXgcr2k61vhAQE6cj/X8hG1m+VjhikzeZeQE9fzZFp4I4ZOFiC90vSi jftmP93OvME3ErRjUz6YJv6ywFx2vaKTHdNqoZaVzP1qAIMWXJDh7DKCoGXKI/BcobaA 0S//5gE/6FtEmDdGm+hZDxdXMqBPhRfaFJpSD42Xh6fW+xocafJi64tiGJueLWIFSXwk 5UQQ== X-Gm-Message-State: AO0yUKUyr1ODBqK3RUSJzhVsmIAvvPJ46LVh4Jd9DGmz6v10TXUUfE+P UjdHgf5VU71w8MuHUPue+lZ50VtlUNk= X-Google-Smtp-Source: AK7set9uXuFcAAjB5O0QV0Xb2ftT+HcRWdedCtf2z+W+4L/MaBisfzCjx3cFBAo6If411AuBWDXU7wCyXBg= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:f781:d5ed:1806:6ebb]) (user=surenb job=sendgmr) by 2002:a05:6902:13c6:b0:8da:3163:224 with SMTP id y6-20020a05690213c600b008da31630224mr5ybu.0.1676524704439; Wed, 15 Feb 2023 21:18:24 -0800 (PST) Date: Wed, 15 Feb 2023 21:17:27 -0800 In-Reply-To: <20230216051750.3125598-1-surenb@google.com> Mime-Version: 1.0 References: <20230216051750.3125598-1-surenb@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230216051750.3125598-13-surenb@google.com> Subject: [PATCH v3 12/35] mm: add per-VMA lock and helper functions to control it From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, peterz@infradead.org, ldufour@linux.ibm.com, paulmck@kernel.org, mingo@redhat.com, will@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, chriscli@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, rppt@kernel.org, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, leewalsh@google.com, posk@google.com, michalechner92@googlemail.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, Suren Baghdasaryan X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: AF75B14000A X-Stat-Signature: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rfSWEV2gYpvHhd9ITAwTmbR8yeUk2bSVHEXAPLMaaYVmsO6yx36oEu9QJBR5XqEMM2xFo0Y7laIDL+ZAjlIq5yK0f91mdoe6BtAyAuDmXdGu/qKFg5Oq4aBSNElRMIw2CB03SoYc0MezIGGxXAkAjNNMNOdYYWYE4ZX9ryEQgJVHBxlR2kt5A4NrHLZkB3t5fpfo/HB8PBonNb/sg6byfRuyXrrGAimqzhpbofApIcll/UqvsjkmPHkmWvRYd03Q1icOy6Jo+87pyXd53bDm/gO1ZAtTiMBEB7GV0F7v1fEroIk4fa1/xbeVYfqh71S29QsfQuWse2TrtWpCIx5MqFF4FYj3GBlVsy33C5OM7vZR3w3PTFeBA9Z+4GiGfMMsfPVJoyQ14Hyi6DMhmR40gyfeeHIU25RyGPEg2wELHfya+aj7YzLz9Pcb8EQT5vwK3YnDgJddQcApzsESbvdN64Q2/ECsCwjSwC++gVXKhgK+DLwY2SOIoT4ObrM4OZkECBOzCF94OB6H6JHw3rb1zZTbRirPsGd5A== 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: Introduce per-VMA locking. The lock implementation relies on a per-vma and per-mm sequence counters to note exclusive locking: - read lock - (implemented by vma_start_read) requires the vma (vm_lock_seq) and mm (mm_lock_seq) sequence counters to differ. If they match then there must be a vma exclusive lock held somewhere. - read unlock - (implemented by vma_end_read) is a trivial vma->lock unlock. - write lock - (vma_start_write) requires the mmap_lock to be held exclusively and the current mm counter is assigned to the vma counter. This will allow multiple vmas to be locked under a single mmap_lock write lock (e.g. during vma merging). The vma counter is modified under exclusive vma lock. - write unlock - (vma_end_write_all) is a batch release of all vma locks held. It doesn't pair with a specific vma_start_write! It is done before exclusive mmap_lock is released by incrementing mm sequence counter (mm_lock_seq). - write downgrade - if the mmap_lock is downgraded to the read lock, all vma write locks are released as well (effectivelly same as write unlock). Signed-off-by: Suren Baghdasaryan --- include/linux/mm.h | 82 +++++++++++++++++++++++++++++++++++++++ include/linux/mm_types.h | 8 ++++ include/linux/mmap_lock.h | 13 +++++++ kernel/fork.c | 4 ++ mm/init-mm.c | 3 ++ 5 files changed, 110 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 2992a2d55aee..a056ee170e34 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -623,6 +623,87 @@ struct vm_operations_struct { unsigned long addr); }; +#ifdef CONFIG_PER_VMA_LOCK +static inline void vma_init_lock(struct vm_area_struct *vma) +{ + init_rwsem(&vma->lock); + vma->vm_lock_seq = -1; +} + +/* + * Try to read-lock a vma. The function is allowed to occasionally yield false + * locked result to avoid performance overhead, in which case we fall back to + * using mmap_lock. The function should never yield false unlocked result. + */ +static inline bool vma_start_read(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)) + return false; + + if (unlikely(down_read_trylock(&vma->lock) == 0)) + return false; + + /* + * Overflow might produce false locked result. + * False unlocked result is impossible because we modify and check + * vma->vm_lock_seq under vma->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->lock); + return false; + } + return true; +} + +static inline void vma_end_read(struct vm_area_struct *vma) +{ + rcu_read_lock(); /* keeps vma alive till the end of up_read */ + up_read(&vma->lock); + rcu_read_unlock(); +} + +static inline void vma_start_write(struct vm_area_struct *vma) +{ + int mm_lock_seq; + + mmap_assert_write_locked(vma->vm_mm); + + /* + * current task is holding mmap_write_lock, both vma->vm_lock_seq and + * 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) + return; + + down_write(&vma->lock); + vma->vm_lock_seq = mm_lock_seq; + up_write(&vma->lock); +} + +static inline void vma_assert_write_locked(struct vm_area_struct *vma) +{ + mmap_assert_write_locked(vma->vm_mm); + /* + * current task is holding mmap_write_lock, both vma->vm_lock_seq and + * mm->mm_lock_seq can't be concurrently modified. + */ + VM_BUG_ON_VMA(vma->vm_lock_seq != READ_ONCE(vma->vm_mm->mm_lock_seq), vma); +} + +#else /* CONFIG_PER_VMA_LOCK */ + +static inline void vma_init_lock(struct vm_area_struct *vma) {} +static inline bool vma_start_read(struct vm_area_struct *vma) + { return false; } +static inline void vma_end_read(struct vm_area_struct *vma) {} +static inline void vma_start_write(struct vm_area_struct *vma) {} +static inline void vma_assert_write_locked(struct vm_area_struct *vma) {} + +#endif /* CONFIG_PER_VMA_LOCK */ + static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) { static const struct vm_operations_struct dummy_vm_ops = {}; @@ -631,6 +712,7 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &dummy_vm_ops; INIT_LIST_HEAD(&vma->anon_vma_chain); + vma_init_lock(vma); } /* Use when VMA is not part of the VMA tree and needs no locking */ diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index fb4e2afad787..e268723eaf44 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -508,6 +508,11 @@ struct vm_area_struct { vm_flags_t __private __vm_flags; }; +#ifdef CONFIG_PER_VMA_LOCK + int vm_lock_seq; + struct rw_semaphore lock; +#endif + /* * For areas with an address space and backing store, * linkage into the address_space->i_mmap interval tree. @@ -633,6 +638,9 @@ struct mm_struct { * init_mm.mmlist, and are protected * by mmlist_lock */ +#ifdef CONFIG_PER_VMA_LOCK + int mm_lock_seq; +#endif unsigned long hiwater_rss; /* High-watermark of RSS usage */ diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index e49ba91bb1f0..aab8f1b28d26 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -72,6 +72,17 @@ static inline void mmap_assert_write_locked(struct mm_struct *mm) VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm); } +#ifdef CONFIG_PER_VMA_LOCK +static inline void vma_end_write_all(struct mm_struct *mm) +{ + mmap_assert_write_locked(mm); + /* No races during update due to exclusive mmap_lock being held */ + WRITE_ONCE(mm->mm_lock_seq, mm->mm_lock_seq + 1); +} +#else +static inline void vma_end_write_all(struct mm_struct *mm) {} +#endif + static inline void mmap_init_lock(struct mm_struct *mm) { init_rwsem(&mm->mmap_lock); @@ -114,12 +125,14 @@ static inline bool mmap_write_trylock(struct mm_struct *mm) static inline void mmap_write_unlock(struct mm_struct *mm) { __mmap_lock_trace_released(mm, true); + vma_end_write_all(mm); up_write(&mm->mmap_lock); } static inline void mmap_write_downgrade(struct mm_struct *mm) { __mmap_lock_trace_acquire_returned(mm, false, true); + vma_end_write_all(mm); downgrade_write(&mm->mmap_lock); } diff --git a/kernel/fork.c b/kernel/fork.c index 314d51eb91da..9141427a98b2 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -474,6 +474,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) */ data_race(memcpy(new, orig, sizeof(*new))); INIT_LIST_HEAD(&new->anon_vma_chain); + vma_init_lock(new); dup_anon_vma_name(orig, new); } return new; @@ -1147,6 +1148,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, seqcount_init(&mm->write_protect_seq); mmap_init_lock(mm); INIT_LIST_HEAD(&mm->mmlist); +#ifdef CONFIG_PER_VMA_LOCK + mm->mm_lock_seq = 0; +#endif mm_pgtables_bytes_init(mm); mm->map_count = 0; mm->locked_vm = 0; diff --git a/mm/init-mm.c b/mm/init-mm.c index c9327abb771c..33269314e060 100644 --- a/mm/init-mm.c +++ b/mm/init-mm.c @@ -37,6 +37,9 @@ struct mm_struct init_mm = { .page_table_lock = __SPIN_LOCK_UNLOCKED(init_mm.page_table_lock), .arg_lock = __SPIN_LOCK_UNLOCKED(init_mm.arg_lock), .mmlist = LIST_HEAD_INIT(init_mm.mmlist), +#ifdef CONFIG_PER_VMA_LOCK + .mm_lock_seq = 0, +#endif .user_ns = &init_user_ns, .cpu_bitmap = CPU_BITS_NONE, #ifdef CONFIG_IOMMU_SVA