From patchwork Fri Apr 24 01:38:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michel Lespinasse X-Patchwork-Id: 11506931 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 393FA92A for ; Fri, 24 Apr 2020 01:39:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DBE0F2168B for ; Fri, 24 Apr 2020 01:39:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iBxxvM+Q" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DBE0F2168B Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E43428E0005; Thu, 23 Apr 2020 21:39:04 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id DF29D8E0003; Thu, 23 Apr 2020 21:39:04 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE0EB8E0005; Thu, 23 Apr 2020 21:39:04 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0018.hostedemail.com [216.40.44.18]) by kanga.kvack.org (Postfix) with ESMTP id B36958E0003 for ; Thu, 23 Apr 2020 21:39:04 -0400 (EDT) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 703A8180AD81F for ; Fri, 24 Apr 2020 01:39:04 +0000 (UTC) X-FDA: 76741040208.13.spark75_32055cd8af155 X-Spam-Summary: 2,0,0,9f87ce88c4222faa,d41d8cd98f00b204,walken@google.com,,RULES_HIT:1:2:41:355:379:800:960:973:982:988:989:1260:1277:1312:1313:1314:1345:1359:1437:1516:1518:1519:1593:1594:1595:1596:1605:1730:1747:1777:1792:2198:2199:2393:2559:2562:3138:3139:3140:3141:3142:3152:3165:3369:3608:3865:3866:3867:3868:3871:3874:4050:4321:4470:4605:5007:6119:6261:6653:6742:7875:9969:10004:10226:11026:11232:11473:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12895:12986:13149:13230:13255:13439:13895:13972:14096:14097:14394:21080:21324:21433:21444:21450:21451:21627:21990:30003:30029:30054:30070,0,RBL:209.85.214.194:@google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:333,LUA_SUMMARY:none X-HE-Tag: spark75_32055cd8af155 X-Filterd-Recvd-Size: 10729 Received: from mail-pl1-f194.google.com (mail-pl1-f194.google.com [209.85.214.194]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Fri, 24 Apr 2020 01:39:03 +0000 (UTC) Received: by mail-pl1-f194.google.com with SMTP id g2so3161281plo.3 for ; Thu, 23 Apr 2020 18:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=eZcf0QPT3ZvI34tvvY0T2MtEDb+ZHbHmyYW0ad7vjJc=; b=iBxxvM+QNomOa7lVAyh6PRH8iW3RTfaCAxwwypmALNJgt5ObVteaV4+5U/Z9Zm5SQt qfT4Y5PA+ZPY5kVetf/LTb5J7DdeqM5UMjweLizYxswKt2Vee7DtmRlpEZeq8nYc0hd4 X7IMB6TDsdBmcTEV509t1ccHbximkauBTRzduVgW9vEjoey8+dGaDVcBOa5Q6jfV4haE hZ5FJxe44f9aEix+xex2I3nmtsCqKa/vqZ9TBs+WSQdqL16pSQmxqFxrOOjWqSt+xHqx KkdhW9d89/uyu2VKi0EoavXYGzKe67/O4U5+yvRawASTczOzLKAPTqBszIvCpclcPjeF yTGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=eZcf0QPT3ZvI34tvvY0T2MtEDb+ZHbHmyYW0ad7vjJc=; b=ZwRyEQJqXSwy+RLLiAuA2JVTq/EU0VZt5s3WbdtC6K9xaLL+l2GedDYqsRrFRarv9/ 9lwtWFb//3vu/vF05Mq/tSwOM19rlz8nrenwmqb0fiWxQLD1Dlox35YgT12hs3zBp9QY 0kvUN27e6NhuDm+dYSQnIfZqCLTMxq1+MoKOOCcwj3PbPgjM8lNIrPjMCD5Pc6EhPgya u1w4v+SqDVVv+j4t2ibhVuDInN8odrD4gsAX5LK1vG7F79LOUzD1eP18wmwmsjGLVQJ6 sRYDY2dyKvGbC2hZOHDSYqa08NAoS0P0pnA+c+LsuUfn364DJ4J6pQnq8R5bcUsNcP9a LH2w== X-Gm-Message-State: AGi0PubzZpE/GBBwIf9lgDNGvumZhHQu5H+LXHcM/uOlKBcIwVpOIbsX SNsX47Vas9ECE40xUdpXhShedA== X-Google-Smtp-Source: APiQypKlst01cw/y+XQoF/d2CAeJEm3O9oYhK0VQeTP5culqA6mYQMpqBhc5FNeTq2G2WM2kl4iL6A== X-Received: by 2002:a17:90a:e28f:: with SMTP id d15mr3671951pjz.79.1587692342246; Thu, 23 Apr 2020 18:39:02 -0700 (PDT) Received: from google.com ([2620:15c:2cd:202:2523:d194:de3b:636f]) by smtp.gmail.com with ESMTPSA id 22sm3977091pfb.132.2020.04.23.18.39.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2020 18:39:00 -0700 (PDT) Date: Thu, 23 Apr 2020 18:38:58 -0700 From: Michel Lespinasse To: Matthew Wilcox Cc: Andrew Morton , linux-mm , LKML , Peter Zijlstra , Laurent Dufour , Vlastimil Babka , Liam Howlett , Jerome Glisse , Davidlohr Bueso , David Rientjes , Hugh Dickins , Ying Han , Jason Gunthorpe , Daniel Jordan Subject: [PATCH v5.5 09/10] mmap locking API: add mmap_assert_locked() and mmap_assert_write_locked() Message-ID: <20200424013858.GB158937@google.com> References: <20200422001422.232330-1-walken@google.com> <20200422001422.232330-11-walken@google.com> <20200422015829.GR5820@bombadil.infradead.org> <20200423015917.GA13910@bombadil.infradead.org> <20200424012612.GA158937@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200424012612.GA158937@google.com> 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: Add new APIs to assert that mmap_sem is held. Using this instead of rwsem_is_locked and lockdep_assert_held[_write] makes the assertions more tolerant of future changes to the lock type. Signed-off-by: Michel Lespinasse --- arch/x86/events/core.c | 2 +- fs/userfaultfd.c | 6 +++--- include/linux/mmap_lock.h | 14 ++++++++++++++ mm/gup.c | 2 +- mm/hmm.c | 2 +- mm/memory.c | 2 +- mm/mmu_notifier.c | 6 +++--- mm/pagewalk.c | 6 +++--- mm/util.c | 2 +- 9 files changed, 28 insertions(+), 14 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index a619763e96e1..66559ac4f89e 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -2182,7 +2182,7 @@ static void x86_pmu_event_mapped(struct perf_event *event, struct mm_struct *mm) * For now, this can't happen because all callers hold mmap_sem * for write. If this changes, we'll need a different solution. */ - lockdep_assert_held_write(&mm->mmap_sem); + mmap_assert_write_locked(mm); if (atomic_inc_return(&mm->context.perf_rdpmc_allowed) == 1) on_each_cpu_mask(mm_cpumask(mm), refresh_pce, NULL, 1); diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 9c645eee1a59..12b492409040 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -234,7 +234,7 @@ static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, pte_t *ptep, pte; bool ret = true; - VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem)); + mmap_assert_locked(mm); ptep = huge_pte_offset(mm, address, vma_mmu_pagesize(vma)); @@ -286,7 +286,7 @@ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, pte_t *pte; bool ret = true; - VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem)); + mmap_assert_locked(mm); pgd = pgd_offset(mm, address); if (!pgd_present(*pgd)) @@ -405,7 +405,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) * Coredumping runs without mmap_sem so we can only check that * the mmap_sem is held, if PF_DUMPCORE was not set. */ - WARN_ON_ONCE(!rwsem_is_locked(&mm->mmap_sem)); + mmap_assert_locked(mm); ctx = vmf->vma->vm_userfaultfd_ctx.ctx; if (!ctx) diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index 9e104835a0d1..5bf7cee5d93b 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -1,6 +1,8 @@ #ifndef _LINUX_MMAP_LOCK_H #define _LINUX_MMAP_LOCK_H +#include + #define MMAP_LOCK_INITIALIZER(name) \ .mmap_sem = __RWSEM_INITIALIZER(name.mmap_sem), @@ -73,4 +75,16 @@ static inline void mmap_read_unlock_non_owner(struct mm_struct *mm) up_read_non_owner(&mm->mmap_sem); } +static inline void mmap_assert_locked(struct mm_struct *mm) +{ + VM_BUG_ON_MM(!lockdep_is_held_type(&mm->mmap_sem, -1), mm); + VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); +} + +static inline void mmap_assert_write_locked(struct mm_struct *mm) +{ + VM_BUG_ON_MM(!lockdep_is_held_type(&mm->mmap_sem, 0), mm); + VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); +} + #endif /* _LINUX_MMAP_LOCK_H */ diff --git a/mm/gup.c b/mm/gup.c index 0404e52513b2..e12993ceb711 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1403,7 +1403,7 @@ long populate_vma_page_range(struct vm_area_struct *vma, VM_BUG_ON(end & ~PAGE_MASK); VM_BUG_ON_VMA(start < vma->vm_start, vma); VM_BUG_ON_VMA(end > vma->vm_end, vma); - VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); + mmap_assert_locked(mm); gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK; if (vma->vm_flags & VM_LOCKONFAULT) diff --git a/mm/hmm.c b/mm/hmm.c index 280585833adf..660a4bcf932a 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -581,7 +581,7 @@ long hmm_range_fault(struct hmm_range *range) struct mm_struct *mm = range->notifier->mm; int ret; - lockdep_assert_held(&mm->mmap_sem); + mmap_assert_locked(mm); do { /* If range is no longer valid force retry. */ diff --git a/mm/memory.c b/mm/memory.c index e6dd3309c5a3..20f98ea8968e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1214,7 +1214,7 @@ static inline unsigned long zap_pud_range(struct mmu_gather *tlb, next = pud_addr_end(addr, end); if (pud_trans_huge(*pud) || pud_devmap(*pud)) { if (next - addr != HPAGE_PUD_SIZE) { - VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma); + mmap_assert_locked(tlb->mm); split_huge_pud(vma, pud, addr); } else if (zap_huge_pud(tlb, vma, pud, addr)) goto next; diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c index cfd0a03bf5cc..24eb9d1ed0a7 100644 --- a/mm/mmu_notifier.c +++ b/mm/mmu_notifier.c @@ -609,7 +609,7 @@ int __mmu_notifier_register(struct mmu_notifier *subscription, struct mmu_notifier_subscriptions *subscriptions = NULL; int ret; - lockdep_assert_held_write(&mm->mmap_sem); + mmap_assert_write_locked(mm); BUG_ON(atomic_read(&mm->mm_users) <= 0); if (IS_ENABLED(CONFIG_LOCKDEP)) { @@ -761,7 +761,7 @@ struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops, struct mmu_notifier *subscription; int ret; - lockdep_assert_held_write(&mm->mmap_sem); + mmap_assert_write_locked(mm); if (mm->notifier_subscriptions) { subscription = find_get_mmu_notifier(mm, ops); @@ -1006,7 +1006,7 @@ int mmu_interval_notifier_insert_locked( mm->notifier_subscriptions; int ret; - lockdep_assert_held_write(&mm->mmap_sem); + mmap_assert_write_locked(mm); if (!subscriptions || !subscriptions->has_itree) { ret = __mmu_notifier_register(NULL, mm); diff --git a/mm/pagewalk.c b/mm/pagewalk.c index 928df1638c30..3b452aa05cd0 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -395,7 +395,7 @@ int walk_page_range(struct mm_struct *mm, unsigned long start, if (!walk.mm) return -EINVAL; - lockdep_assert_held(&walk.mm->mmap_sem); + mmap_assert_locked(walk.mm); vma = find_vma(walk.mm, start); do { @@ -453,7 +453,7 @@ int walk_page_range_novma(struct mm_struct *mm, unsigned long start, if (start >= end || !walk.mm) return -EINVAL; - lockdep_assert_held(&walk.mm->mmap_sem); + mmap_assert_locked(walk.mm); return __walk_page_range(start, end, &walk); } @@ -472,7 +472,7 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops, if (!walk.mm) return -EINVAL; - lockdep_assert_held(&walk.mm->mmap_sem); + mmap_assert_locked(walk.mm); err = walk_page_test(vma->vm_start, vma->vm_end, &walk); if (err > 0) diff --git a/mm/util.c b/mm/util.c index ea2e15b21446..e7cf9c815f5a 100644 --- a/mm/util.c +++ b/mm/util.c @@ -437,7 +437,7 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, unsigned long locked_vm, limit; int ret = 0; - lockdep_assert_held_write(&mm->mmap_sem); + mmap_assert_write_locked(mm); locked_vm = mm->locked_vm; if (inc) {