From patchwork Wed Apr 22 00:14:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michel Lespinasse X-Patchwork-Id: 11502559 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 2C4D813B2 for ; Wed, 22 Apr 2020 00:14:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E411820735 for ; Wed, 22 Apr 2020 00:14:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mhFOnMH/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E411820735 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 2F3518E000E; Tue, 21 Apr 2020 20:14:48 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2A7238E0003; Tue, 21 Apr 2020 20:14:48 -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 120928E000E; Tue, 21 Apr 2020 20:14:48 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0089.hostedemail.com [216.40.44.89]) by kanga.kvack.org (Postfix) with ESMTP id E96968E0003 for ; Tue, 21 Apr 2020 20:14:47 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id B9DF9181AEF1E for ; Wed, 22 Apr 2020 00:14:47 +0000 (UTC) X-FDA: 76733570214.18.fork04_46ef8ff646b00 X-Spam-Summary: 2,0,0,010ec5addbd068e2,d41d8cd98f00b204,3doyfxgykcak5jutnwpxxpun.lxvurw36-vvt4jlt.x0p@flex--walken.bounces.google.com,,RULES_HIT:41:152:355:379:541:800:960:973:988:989:1260:1277:1313:1314:1345:1359:1437:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3152:3354:3369:3865:3867:3871:3874:4117:4321:4605:5007:6119:6261:6653:6742:7875:9969:10004:10400:11026:11232:11473:11658:11914:12043:12296:12297:12438:12555:12895:12986:14096:14097:14181:14394:14659:14721:21080:21444:21451:21627:30003:30029:30054:30070,0,RBL:209.85.160.202:@flex--walken.bounces.google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.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:24,LUA_SUMMARY:none X-HE-Tag: fork04_46ef8ff646b00 X-Filterd-Recvd-Size: 6309 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Wed, 22 Apr 2020 00:14:47 +0000 (UTC) Received: by mail-qt1-f202.google.com with SMTP id f56so531018qte.18 for ; Tue, 21 Apr 2020 17:14:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=/eDOjnM9HRS8OZpDk6pfMhfG32YpgGHrkgLs3JMHIzE=; b=mhFOnMH/PFj8RkvZsUJ1VayE7RcJCiCKYRKPCTgHI/fvOovo1BaQA8sWItdsVrCd/T otCFheAAbr+9nY7JfJbtd9rFN77Nmb3bQLd1EZ1FaJMm266MHuGxGP9QKjcS1flE40NY hrlF6yDVCIG9TDEPSLHJ4ND1d7OwiLuEutGlvdSaGyw3Nms6jlWfCja48ssqqLSd4QqK e3ClRzD/PywU8gG42OJcjZBW7tkCk4vn5HujRCV1q9gADsHVr/9d7nZaXbbMBifVSCB5 MDhFhmxmT7QuB91O++3TdnH5sPBTZJrysrMbrwqMIFlTns+fI6RNpAI3TwTrxAuqYovt +MQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/eDOjnM9HRS8OZpDk6pfMhfG32YpgGHrkgLs3JMHIzE=; b=c7BXbJsdTxHYqq6XoSHFgnZfyWJ57yryWhvi4EUnOpOyAoQK1aEvMd5MR3xb7BB4sg L/L/Nmoj49R2Aw7GpURB5lxemI2T8qwkVlQL1xfKTnqskaTBEI/QkT2X5e9cl7Apbidf /qYiLtoGoq5k29nU+rhyzl26LGYOW2MrXPMTksSAy1FqWEse5+KxRqjIBGMijoxxKFv1 IRefJIgTpT2OG2d6FQh/FjtuGnRaTakt07zeYJP5On03Z5uXKLhTI8qRtORpm/hk81yv j0uILHF00JXH/Y/Zl+D/SM+pTW7biXWTIKb3tI8VGXSLE4lHt4JDLu8kM/VCESyschtx j4kQ== X-Gm-Message-State: AGi0Pubn0Khiz7qnHSQ/FN78wweHTc2mg1Qu2btmKLGQk9ljj/+yHmI4 p57QzViusTalvDOupI1h3wmSKXJMyLE= X-Google-Smtp-Source: APiQypLkCgZ5QyMRA2Kls797eOnq3Fvv4/RJFRvdM8oKCvxtZFU9mbexebet60I1wVNAoL/zP89dxl6R5Vw= X-Received: by 2002:a0c:facb:: with SMTP id p11mr22619057qvo.17.1587514486557; Tue, 21 Apr 2020 17:14:46 -0700 (PDT) Date: Tue, 21 Apr 2020 17:14:21 -0700 In-Reply-To: <20200422001422.232330-1-walken@google.com> Message-Id: <20200422001422.232330-10-walken@google.com> Mime-Version: 1.0 References: <20200422001422.232330-1-walken@google.com> X-Mailer: git-send-email 2.26.1.301.g55bc3eb7cb9-goog Subject: [PATCH v5 09/10] mmap locking API: add mmap_assert_locked From: Michel Lespinasse To: Andrew Morton , linux-mm Cc: LKML , Peter Zijlstra , Laurent Dufour , Vlastimil Babka , Matthew Wilcox , Liam Howlett , Jerome Glisse , Davidlohr Bueso , David Rientjes , Hugh Dickins , Ying Han , Jason Gunthorpe , Daniel Jordan , Michel Lespinasse 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 mmap_assert_locked to assert that mmap_sem is held. Signed-off-by: Michel Lespinasse --- fs/userfaultfd.c | 6 +++--- include/linux/mmap_lock.h | 10 ++++++++++ mm/gup.c | 2 +- mm/memory.c | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) 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..f7a3a9550cc5 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,12 @@ 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) +{ + if (IS_ENABLED(CONFIG_LOCKDEP) && debug_locks) + VM_BUG_ON_MM(!lockdep_is_held(&mm->mmap_sem), mm); + else + 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/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;