From patchwork Mon Aug 26 20:43:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13778403 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 24ECCC5472C for ; Mon, 26 Aug 2024 20:44:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9E0616B009D; Mon, 26 Aug 2024 16:44:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 992866B009E; Mon, 26 Aug 2024 16:44:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 808D86B009F; Mon, 26 Aug 2024 16:44:19 -0400 (EDT) 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 5B13E6B009D for ; Mon, 26 Aug 2024 16:44:19 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 16BC31613C1 for ; Mon, 26 Aug 2024 20:44:19 +0000 (UTC) X-FDA: 82495574238.05.7EBCE80 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id F0997C001E for ; Mon, 26 Aug 2024 20:44:16 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GB51IwsU; spf=pass (imf10.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724704970; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XzWn17lHZnGD5Tr66iu6y8/zqhTRy72agTNrtdvxzwE=; b=fUty0eAqWEpKKlL40qM2qq2fJ6OmrIPuAlNtjQoRlEFaKQTRMxvP6WTU+VyDxXaJTFnl85 LPo0BNWpRM8mFLd6pGYvrlpeQFhgn0YOHHusOkRNn93ya3UZ1SowKq8ZNjE+hTeaYVIQqj jfwkiPTuzKmOuxz3m1+3BcBDV6HVN/8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724704970; a=rsa-sha256; cv=none; b=PAzLufNIdPrRXb1FWBDrFm6/M5HWDLqsyJBwk/e0PdAxVBQpwzY1+3Hrhu2/NyusbDMLQP uGUX8FgHqFp5gFnsl92gX15RIzWlQote91SJjs9K+1XMlttoQWRXipbSUMw1TbeblVS8cG UDRp6JSNYYYbDPZAA+f0b1O0kAohYV8= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GB51IwsU; spf=pass (imf10.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724705056; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XzWn17lHZnGD5Tr66iu6y8/zqhTRy72agTNrtdvxzwE=; b=GB51IwsUFGz9QiMYb1aeCVArQshtX9/BPqs22vaRFMbgUPbZna0dbgweERVBax6aL419Md lsk2E8/+C62PpHINsxJHpsHa7XVHdtx3ZQR7ceynlKMx7H+BPLTi1ab8FvLjNePv/RzKwv +wsyS6nN6mCeHLCbYt7eNpBfge5Til4= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-678-zsijSCRTOGqEWOseYysFXA-1; Mon, 26 Aug 2024 16:44:15 -0400 X-MC-Unique: zsijSCRTOGqEWOseYysFXA-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7a1df6b3a26so630267885a.1 for ; Mon, 26 Aug 2024 13:44:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724705055; x=1725309855; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XzWn17lHZnGD5Tr66iu6y8/zqhTRy72agTNrtdvxzwE=; b=SV5Ydji92PtPI3ogZ6fYuYR+rdOGmbMsNY9Gl+DLBXAJRComAoTTMeBzMA2zo3HtRY wR0d0xlSSpwMArStu0PJILuO2r36nwzojMLBv/S1qUUopy6df9+i/NEZn4AxmQXTONyo icAFG3xxdbXfXigd2ONZr7+yo9gomWwA/j9jVTrpGI2c8Y7KzeebLlwuUm6Bld4Tf+k2 Ptu+0Ak1oFDQmDwvd2MMaUu5XSCwEO5dRVPt2fR7XP4DLK0IBF1+GjCaMA7LP78RlgNa /m/Tkm/HzI+VspH78nu6FpOZJmXItxObg2CAtop60z/lFQpu5S+EnbuX59WBmLaaxpeO 01/w== X-Forwarded-Encrypted: i=1; AJvYcCXzugR89WqdOHj4ycbTYJ/fu4QAlx3GLdcZL3mj1mc76c9G7+0wux2dKbokV7XXR+1fn0rU1KJJnw==@kvack.org X-Gm-Message-State: AOJu0Ywh4rMgz1oVJZOwLYxADB1qd04tuLhkjEYlgZ2QgFNQytmdaEnS a0L5QQhzYd8Hs1q6st1GdBIyLHlziEQp7Gk09ToWmS8YV1vf77H8eaw7x/0C+zd3R/Zwf8mPEnI PwdXnVa5BZLUcArkcRcgf8D8TlohyD+td2fp4GKSDNBeMTz4r X-Received: by 2002:a05:620a:408d:b0:7a1:e93c:cd04 with SMTP id af79cd13be357-7a6896e0e1cmr1465305785a.9.1724705054682; Mon, 26 Aug 2024 13:44:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH4kwi4qTohFBFu7kWFKQGbZI396sZSJGmlCvwBl5XfpN0sU8TboRNIhnEyWXLKkBzi4MYP4w== X-Received: by 2002:a05:620a:408d:b0:7a1:e93c:cd04 with SMTP id af79cd13be357-7a6896e0e1cmr1465302885a.9.1724705054299; Mon, 26 Aug 2024 13:44:14 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a67f3fd6c1sm491055185a.121.2024.08.26.13.44.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Aug 2024 13:44:13 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Gavin Shan , Catalin Marinas , x86@kernel.org, Ingo Molnar , Andrew Morton , Paolo Bonzini , Dave Hansen , Thomas Gleixner , Alistair Popple , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Sean Christopherson , peterx@redhat.com, Oscar Salvador , Jason Gunthorpe , Borislav Petkov , Zi Yan , Axel Rasmussen , David Hildenbrand , Yan Zhao , Will Deacon , Kefeng Wang , Alex Williamson Subject: [PATCH v2 09/19] mm: New follow_pfnmap API Date: Mon, 26 Aug 2024 16:43:43 -0400 Message-ID: <20240826204353.2228736-10-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240826204353.2228736-1-peterx@redhat.com> References: <20240826204353.2228736-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: 1muzcdosamimitpc9iu544mkom1g6kga X-Rspamd-Queue-Id: F0997C001E X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724705056-683710 X-HE-Meta: U2FsdGVkX1+NthvfzNDO5vfzwiS49exXfY5q/CExCMBqWKg19kCoF/BZeizi+DLIlc2MtVdYZGDeR0xXAzVezh3FZDQdBqm+5K9KMrQ6eOmu/AbGLkTLYHm6ZZGVAXMG4OeBhVIKGdRNrtds6bal8CZR4+jFyATaTxEPAXR/j3dG7yIY2qvxEvTIdpSmMvehOwIg2bd/5lDDuZecPO9uOPHdnL0dkVbJ2Kh7/RMskBu7ktkYVa1JksCkp9T1qGU2ZdDd85AUrXzmfmTCdtYT+ZoOcM17Su6J6tAS2y/jnmkvUOjdVtOngQ54NmOE6CS2LaV7+L2Pi52qhsCUzXd6wt4/jdSKSPIVS4OsovUU2q+qWW7FhvJknqoTR/X5P+2cIfa3FWlk8GPL86VpNupcfjpx5bL1rmjn/PVnk6VsVgzlV788eKnofgiKeIkrdAVNz+G+rKaQzfxgn20c/BdvA8N0M9I2k+gm6B7OETsyk9dgvZCEShvevPxp8wsDvKD+6WNSDwdBu3jk3mPfYVUG1DoobnWDlHNo7mJdUQnLn1VaCp9pzz5I/dfIjAc9io570j+5N3EDCFXQUTi9JHNHvDFAaU6hdJfa8fLiAJBIS1at5t2Ge1o7ZofJiRkzeEja2D/NyOvPJ4HBNbRXlh9L8R0hNNaA41wR/6a1QjuRFeXADLVYKjZoMbka6OT/GZFj5l7Avi50HJnEKGMAW6ynJ9THeuu/1vXd30fxcN0mQ1jv1oAC1NPk5rFlvBLIQYUSjrk0Yf9/L1mCMOVZS6/FK5KNXrh5DrwXRFIoZ84XRqhkKSmrItzSJbwod6sip3yuvbQXQA3pKSzTxBM0j7qGCTTKNIaiIT6W4/y9RmxfWPRn5DgSa+3dlAf4JtQ436ympoDL1N3i4bEz9COkb8n6+6kvS0ny7WQsbz6UMIRErjhy6qumEAoKmz0R4Mxi9KmUYyQHfTQKslfBjotqHQe Kw46gOLh iC5Z6gaFh5M/rzc0NPCDDakBq5OqImny43lGy/HbUkSPuiDDELV2/u41O5DTzSwnOQfuyOZjmq01RkxK8CSj9XcFkzT4FVMNJSuaqAOb9WDM+XG2mS7Y8MCS4cnUVSYZzwGVBeT3R3jVSee9NTQGcWuZrGTPwDepJ72gvkhE8zkvkvp0a1kTGCaAPZiLNMHdf4hHYoMKrIs3ZQRwTTZzv32Vr9EG8xqJjlgj25clz6acHPQgF8sHSzl4c3Fl4h/btiUGCb3QQtfz0BfW8iMX+l1zZT8UCLv6h+ysL7IL+htxVOBqyn+PRJcmSfR00OtoE+lpw7JBiIeEGn/FZUDgAZrs7GEauEo7Ot8dV7p4HMKf+QPoIP0JZvBtv9XIfdek/w/XKmdlYQ0BGGH4MG/iXF8MJkdcKhPCHJYT2zrFJ2GjFFhADIlk7gsyLBXVQYOvUInOUZX/Vt2it790rDe3ADHiKFVevbKlgJSfsj+Dx/6shPhLmMFxzO/xUkOYfwUD9sUEU 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: List-Subscribe: List-Unsubscribe: Introduce a pair of APIs to follow pfn mappings to get entry information. It's very similar to what follow_pte() does before, but different in that it recognizes huge pfn mappings. Signed-off-by: Peter Xu --- include/linux/mm.h | 31 ++++++++++ mm/memory.c | 150 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index d900f15b7650..161d496bfd18 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2373,6 +2373,37 @@ int follow_pte(struct vm_area_struct *vma, unsigned long address, int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, void *buf, int len, int write); +struct follow_pfnmap_args { + /** + * Inputs: + * @vma: Pointer to @vm_area_struct struct + * @address: the virtual address to walk + */ + struct vm_area_struct *vma; + unsigned long address; + /** + * Internals: + * + * The caller shouldn't touch any of these. + */ + spinlock_t *lock; + pte_t *ptep; + /** + * Outputs: + * + * @pfn: the PFN of the address + * @pgprot: the pgprot_t of the mapping + * @writable: whether the mapping is writable + * @special: whether the mapping is a special mapping (real PFN maps) + */ + unsigned long pfn; + pgprot_t pgprot; + bool writable; + bool special; +}; +int follow_pfnmap_start(struct follow_pfnmap_args *args); +void follow_pfnmap_end(struct follow_pfnmap_args *args); + extern void truncate_pagecache(struct inode *inode, loff_t new); extern void truncate_setsize(struct inode *inode, loff_t newsize); void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to); diff --git a/mm/memory.c b/mm/memory.c index 93c0c25433d0..0b136c398257 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6173,6 +6173,156 @@ int follow_pte(struct vm_area_struct *vma, unsigned long address, } EXPORT_SYMBOL_GPL(follow_pte); +static inline void pfnmap_args_setup(struct follow_pfnmap_args *args, + spinlock_t *lock, pte_t *ptep, + pgprot_t pgprot, unsigned long pfn_base, + unsigned long addr_mask, bool writable, + bool special) +{ + args->lock = lock; + args->ptep = ptep; + args->pfn = pfn_base + ((args->address & ~addr_mask) >> PAGE_SHIFT); + args->pgprot = pgprot; + args->writable = writable; + args->special = special; +} + +static inline void pfnmap_lockdep_assert(struct vm_area_struct *vma) +{ +#ifdef CONFIG_LOCKDEP + struct address_space *mapping = vma->vm_file->f_mapping; + + if (mapping) + lockdep_assert(lockdep_is_held(&vma->vm_file->f_mapping->i_mmap_rwsem) || + lockdep_is_held(&vma->vm_mm->mmap_lock)); + else + lockdep_assert(lockdep_is_held(&vma->vm_mm->mmap_lock)); +#endif +} + +/** + * follow_pfnmap_start() - Look up a pfn mapping at a user virtual address + * @args: Pointer to struct @follow_pfnmap_args + * + * The caller needs to setup args->vma and args->address to point to the + * virtual address as the target of such lookup. On a successful return, + * the results will be put into other output fields. + * + * After the caller finished using the fields, the caller must invoke + * another follow_pfnmap_end() to proper releases the locks and resources + * of such look up request. + * + * During the start() and end() calls, the results in @args will be valid + * as proper locks will be held. After the end() is called, all the fields + * in @follow_pfnmap_args will be invalid to be further accessed. Further + * use of such information after end() may require proper synchronizations + * by the caller with page table updates, otherwise it can create a + * security bug. + * + * If the PTE maps a refcounted page, callers are responsible to protect + * against invalidation with MMU notifiers; otherwise access to the PFN at + * a later point in time can trigger use-after-free. + * + * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore + * should be taken for read, and the mmap semaphore cannot be released + * before the end() is invoked. + * + * This function must not be used to modify PTE content. + * + * Return: zero on success, negative otherwise. + */ +int follow_pfnmap_start(struct follow_pfnmap_args *args) +{ + struct vm_area_struct *vma = args->vma; + unsigned long address = args->address; + struct mm_struct *mm = vma->vm_mm; + spinlock_t *lock; + pgd_t *pgdp; + p4d_t *p4dp, p4d; + pud_t *pudp, pud; + pmd_t *pmdp, pmd; + pte_t *ptep, pte; + + pfnmap_lockdep_assert(vma); + + if (unlikely(address < vma->vm_start || address >= vma->vm_end)) + goto out; + + if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) + goto out; +retry: + pgdp = pgd_offset(mm, address); + if (pgd_none(*pgdp) || unlikely(pgd_bad(*pgdp))) + goto out; + + p4dp = p4d_offset(pgdp, address); + p4d = READ_ONCE(*p4dp); + if (p4d_none(p4d) || unlikely(p4d_bad(p4d))) + goto out; + + pudp = pud_offset(p4dp, address); + pud = READ_ONCE(*pudp); + if (pud_none(pud)) + goto out; + if (pud_leaf(pud)) { + lock = pud_lock(mm, pudp); + if (!unlikely(pud_leaf(pud))) { + spin_unlock(lock); + goto retry; + } + pfnmap_args_setup(args, lock, NULL, pud_pgprot(pud), + pud_pfn(pud), PUD_MASK, pud_write(pud), + pud_special(pud)); + return 0; + } + + pmdp = pmd_offset(pudp, address); + pmd = pmdp_get_lockless(pmdp); + if (pmd_leaf(pmd)) { + lock = pmd_lock(mm, pmdp); + if (!unlikely(pmd_leaf(pmd))) { + spin_unlock(lock); + goto retry; + } + pfnmap_args_setup(args, lock, NULL, pmd_pgprot(pmd), + pmd_pfn(pmd), PMD_MASK, pmd_write(pmd), + pmd_special(pmd)); + return 0; + } + + ptep = pte_offset_map_lock(mm, pmdp, address, &lock); + if (!ptep) + goto out; + pte = ptep_get(ptep); + if (!pte_present(pte)) + goto unlock; + pfnmap_args_setup(args, lock, ptep, pte_pgprot(pte), + pte_pfn(pte), PAGE_MASK, pte_write(pte), + pte_special(pte)); + return 0; +unlock: + pte_unmap_unlock(ptep, lock); +out: + return -EINVAL; +} +EXPORT_SYMBOL_GPL(follow_pfnmap_start); + +/** + * follow_pfnmap_end(): End a follow_pfnmap_start() process + * @args: Pointer to struct @follow_pfnmap_args + * + * Must be used in pair of follow_pfnmap_start(). See the start() function + * above for more information. + */ +void follow_pfnmap_end(struct follow_pfnmap_args *args) +{ + if (args->lock) + spin_unlock(args->lock); + if (args->ptep) + pte_unmap(args->ptep); +} +EXPORT_SYMBOL_GPL(follow_pfnmap_end); + #ifdef CONFIG_HAVE_IOREMAP_PROT /** * generic_access_phys - generic implementation for iomem mmap access