Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp146730ybz; Tue, 21 Apr 2020 17:19:27 -0700 (PDT) X-Google-Smtp-Source: APiQypLS1cqvoMFuhxNOLM6FJ3ipA5/lgSXs076xh5I9cSYGfmo3n2m8c6zGEN6SyzDnJTPth2Om X-Received: by 2002:a17:907:1199:: with SMTP id uz25mr23655045ejb.131.1587514767049; Tue, 21 Apr 2020 17:19:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587514767; cv=none; d=google.com; s=arc-20160816; b=hTYFtcgOIs2mP73Q2d1EruB1mtK2Celo+mMCTso5avZzgxPKZ8rOLw4iZQeB9hCzTn jWPwcMUFA38oxNO6ciREn5RiGn87PBo45USWzQQgVxGZ4gPTWbWJ5Atobn5vzDdYq3gy /IxCQbcEhl2OkEt7QUTE7pL9Q5jTOp1Lii0w7V9pqCnHiMV5t3oz9eluFKzmDHMYQfB9 zh5IO3Gynhw9IU6pmOdJ1keBdIW89SpMTOalTRgjZJYXyEkOIwJtkMWZSNzL9mOx5vky IbhpZreNx7py7tu3X09JQu5owV8z7wT6VuDvnH3AC/mokek7IqB6L5wS0udsgPwY4AP0 Vz9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=/eDOjnM9HRS8OZpDk6pfMhfG32YpgGHrkgLs3JMHIzE=; b=AN39MVr3FdJ7f3rMsJI36X1cBHEyFMSI/oJlizO77zRObMo1UOi65oF1dXJAHrBfPA bhO7YWQ1mBHgDM6GmszPvNpM/BiOwmnxmPnaSVmcMeUEcJ3q8E9ei6vhUHz60r/yt9qH DOz83UzEoluK+7hPbT+AqDFhvhIKkmkai6H8JwsKjMvKS5m/oM5GU9fh/mAMFLxMInW9 I4IBsUWRihgTzn3MbqVvA+bxdIexNU9fa51cZiKKyfbka2g0JU6b2xJPaicCxQWUs6Ua f7xVYz0XBse8XxBSwU+Sj04veD2JSpEEzyt/EfZcKvaf+zU9KQ57phtaezy8Mpxc2rlm nehQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="mhFOnMH/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b26si1360987edw.447.2020.04.21.17.19.04; Tue, 21 Apr 2020 17:19:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="mhFOnMH/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726485AbgDVAO7 (ORCPT + 99 others); Tue, 21 Apr 2020 20:14:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726451AbgDVAOt (ORCPT ); Tue, 21 Apr 2020 20:14:49 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76CBEC0610D5 for ; Tue, 21 Apr 2020 17:14:47 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id w6so523469qtt.21 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=VksMLnReWaJWw5fNLvntKBzZKJOIeXV3nFRStffCLA1+avOe6O/uwvR64ySARtRHTz RhK9nmLevB8LphnyGd1Ox/bHUmVgURsPFAEINBiPeEOSBXzD0ZRxgmgU/O1nGglfiHYD 6JqlT/EujHvAK4XfUcW8Ww5vPesWID/J8aPRi1prqLfBD1B6jkq3pL/UiHX2qoP/PC+e AONecKjES8cl5TBDJqPdiMcFJVBssA/Gf3FroiqxKeMEIapVXCXx/7ky8aYu0v+/koxu fG+al+OcPlbZcsDcfK2TR4I38/iQSIK8iTvA4fpyY1lUV43/RfNivECyerTXn4RwFkYn IPug== X-Gm-Message-State: AGi0PuarNrWksh7KqmbX7X8f3yLfLdZZh8Ch66DPSmW/5MnYEKWM5HGK H1yafk/Ic5Bec4zNusMTtq9xlzda08w= 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 Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.26.1.301.g55bc3eb7cb9-goog