From patchwork Wed Apr 3 21:21:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13616713 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 111B8CD1288 for ; Wed, 3 Apr 2024 21:22:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 942476B0099; Wed, 3 Apr 2024 17:21:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F2B06B009A; Wed, 3 Apr 2024 17:21:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 793076B009B; Wed, 3 Apr 2024 17:21:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 56B5E6B0099 for ; Wed, 3 Apr 2024 17:21:59 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 238DA1C0EF8 for ; Wed, 3 Apr 2024 21:21:59 +0000 (UTC) X-FDA: 81969493158.14.CB3B91E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 62CA240011 for ; Wed, 3 Apr 2024 21:21:57 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Bpj8nXHP; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712179317; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=efLT3sOcQF/h2hQN5fWNO1q+LEWoeOZtTC+KwC/NbJA=; b=U6qjaNAcnnm2en2Sgh04jEP0qP4zuOHfw1/VbnpavoSCjwb/cvhflGl3d422E5mu43gLgx ZjvSfI/xMMGIiovcHAEms4vqDoAGWbBP77XnbRVzE9NTqqwiynKBNORX4XzQAw3ZtitdAf SSm8s3SpWtcF7/p5hDZIbMKhBmKy42w= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Bpj8nXHP; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712179317; a=rsa-sha256; cv=none; b=YP8mYhWpepzRj9xHhANjo3640VE7VzwzWFEvzChbXaiotvVYLcbIK77UoFLbxVX6RG0ruR aqHsI8372DQssG2NJ7Jw5qZv+BEqBlaUDmyvQ1PRg65UWoyG/WEw/Yhikbin7i20iy7iqM ba7RiDkbnlxdJ+PXfPlJ1KuU1cspNeg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712179316; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=efLT3sOcQF/h2hQN5fWNO1q+LEWoeOZtTC+KwC/NbJA=; b=Bpj8nXHPgGrbTiJJsIEnpBqVvfpf5bhqpbv5reArwH3i9rCCSOZpd6dMz0gtxXiWK+kAap kbgnAaXgq9nGwXCI8BxDZ4CwH35+CzlLLb3FYUsms8pbauCyFi3J/NiL3AIx2w2GRpvfC/ cT9Js+yUmK97bFv3Fww4bcnRKoW5Jvo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-662-lsbBclVqNHi4N3-nmhzPHA-1; Wed, 03 Apr 2024 17:21:54 -0400 X-MC-Unique: lsbBclVqNHi4N3-nmhzPHA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E3D73101A535; Wed, 3 Apr 2024 21:21:52 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1CD02111E408; Wed, 3 Apr 2024 21:21:48 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, x86@kernel.org, David Hildenbrand , Andrew Morton , Peter Zijlstra , Ingo Molnar , Thomas Gleixner , Christoph Hellwig , Borislav Petkov , "H. Peter Anvin" , Andy Lutomirski , Dave Hansen , Fei Li , Nathan Chancellor , Ingo Molnar Subject: [PATCH v2 3/3] mm: move follow_phys to arch/x86/mm/pat/memtype.c Date: Wed, 3 Apr 2024 23:21:31 +0200 Message-ID: <20240403212131.929421-4-david@redhat.com> In-Reply-To: <20240403212131.929421-1-david@redhat.com> References: <20240403212131.929421-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Rspamd-Queue-Id: 62CA240011 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: hwyxbqn5xdiwxh6ghet4nokc6k4r7dei X-HE-Tag: 1712179317-286586 X-HE-Meta: U2Fs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C/AcCGqdh5WJWyM1+YecxekvCNYTppBig8pjap0u39MRXzU+sQRxM1EOxCjfnBGprhYEZKwxfQtpYfaJzNVXQtNcRILMnIkrxdApifmtBd6nQTuL+vnjW7wCN4vTunE30bQLTyDlSWviYw2xDjnjlh2NwSJUK4vwkMW5YYCDRhOQ1uNr0hAU+5OZeU1hJP5ONvi7vOs0RUJbgAq9YsLwCw6mYBgtsnAQcg6WJ9rAK5XU3xE5LinEOdoM2kVeYot/tqf+vmQ+H8= 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: From: Christoph Hellwig follow_phys is only used by two callers in arch/x86/mm/pat/memtype.c. Move it there and hardcode the two arguments that get the same values passed by both callers. Link: https://lkml.kernel.org/r/20240324234542.2038726-4-hch@lst.de Signed-off-by: Christoph Hellwig Reviewed-by: David Hildenbrand Cc: Andy Lutomirski Cc: Dave Hansen Cc: Fei Li Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Nathan Chancellor Signed-off-by: David Hildenbrand # rebased, applied fixups --- arch/x86/mm/pat/memtype.c | 29 ++++++++++++++++++++++++++++- include/linux/mm.h | 2 -- mm/memory.c | 32 -------------------------------- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c index 36b603d0cdde..d01c3b0bd6eb 100644 --- a/arch/x86/mm/pat/memtype.c +++ b/arch/x86/mm/pat/memtype.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -947,6 +948,32 @@ static void free_pfn_range(u64 paddr, unsigned long size) memtype_free(paddr, paddr + size); } +static int follow_phys(struct vm_area_struct *vma, unsigned long *prot, + resource_size_t *phys) +{ + pte_t *ptep, pte; + spinlock_t *ptl; + + if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) + return -EINVAL; + + if (follow_pte(vma->vm_mm, vma->vm_start, &ptep, &ptl)) + return -EINVAL; + + pte = ptep_get(ptep); + + /* Never return PFNs of anon folios in COW mappings. */ + if (vm_normal_folio(vma, vma->vm_start, pte)) { + pte_unmap_unlock(ptep, ptl); + return -EINVAL; + } + + *prot = pgprot_val(pte_pgprot(pte)); + *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; + pte_unmap_unlock(ptep, ptl); + return 0; +} + static int get_pat_info(struct vm_area_struct *vma, resource_size_t *paddr, pgprot_t *pgprot) { @@ -964,7 +991,7 @@ static int get_pat_info(struct vm_area_struct *vma, resource_size_t *paddr, * detect the PFN. If we need the cachemode as well, we're out of luck * for now and have to fail fork(). */ - if (!follow_phys(vma, vma->vm_start, 0, &prot, paddr)) { + if (!follow_phys(vma, &prot, paddr)) { if (pgprot) *pgprot = __pgprot(prot); return 0; diff --git a/include/linux/mm.h b/include/linux/mm.h index 97e779993c74..bc0cd34a8042 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2424,8 +2424,6 @@ int copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma); int follow_pte(struct mm_struct *mm, unsigned long address, pte_t **ptepp, spinlock_t **ptlp); -int follow_phys(struct vm_area_struct *vma, unsigned long address, - unsigned int flags, unsigned long *prot, resource_size_t *phys); int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, void *buf, int len, int write); diff --git a/mm/memory.c b/mm/memory.c index 1e9a0288fdaf..912cd738ec03 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5987,38 +5987,6 @@ int follow_pte(struct mm_struct *mm, unsigned long address, EXPORT_SYMBOL_GPL(follow_pte); #ifdef CONFIG_HAVE_IOREMAP_PROT -int follow_phys(struct vm_area_struct *vma, - unsigned long address, unsigned int flags, - unsigned long *prot, resource_size_t *phys) -{ - int ret = -EINVAL; - pte_t *ptep, pte; - spinlock_t *ptl; - - if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) - goto out; - - if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) - goto out; - pte = ptep_get(ptep); - - /* Never return PFNs of anon folios in COW mappings. */ - if (vm_normal_folio(vma, address, pte)) - goto unlock; - - if ((flags & FOLL_WRITE) && !pte_write(pte)) - goto unlock; - - *prot = pgprot_val(pte_pgprot(pte)); - *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; - - ret = 0; -unlock: - pte_unmap_unlock(ptep, ptl); -out: - return ret; -} - /** * generic_access_phys - generic implementation for iomem mmap access * @vma: the vma to access