]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
x86/devmem: Remove duplicate range_is_allowed() definition
authorDan Williams <dan.j.williams@intel.com>
Wed, 30 Apr 2025 02:46:21 +0000 (19:46 -0700)
committerDave Hansen <dave.hansen@linux.intel.com>
Thu, 1 May 2025 16:43:48 +0000 (09:43 -0700)
17 years ago, Venki suggested [1] "A future improvement would be to
avoid the range_is_allowed duplication".

The only thing preventing a common implementation is that
phys_mem_access_prot_allowed() expects the range check to exit
immediately when PAT is disabled [2]. I.e. there is no cache conflict to
manage in that case. This cleanup was noticed on the path to
considering changing range_is_allowed() policy to blanket deny /dev/mem
for private (confidential computing) memory.

Note, however that phys_mem_access_prot_allowed() has long since stopped
being relevant for managing cache-type validation due to [3], and [4].

Commit 0124cecfc85a ("x86, PAT: disable /dev/mem mmap RAM with PAT") [1]
Commit 9e41bff2708e ("x86: fix /dev/mem mmap breakage when PAT is disabled") [2]
Commit 1886297ce0c8 ("x86/mm/pat: Fix BUG_ON() in mmap_mem() on QEMU/i386") [3]
Commit 0c3c8a18361a ("x86, PAT: Remove duplicate memtype reserve in devmem mmap") [4]

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Nikolay Borisov <nik.borisov@suse.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://lore.kernel.org/all/20250430024622.1134277-2-dan.j.williams%40intel.com
arch/x86/mm/pat/memtype.c
drivers/char/mem.c
include/linux/io.h

index 72d8cbc611583228bc86333ca1b128c47e7eefec..c97b6598f187f5f3e4a67842663874e853552471 100644 (file)
@@ -38,6 +38,7 @@
 #include <linux/kernel.h>
 #include <linux/pfn_t.h>
 #include <linux/slab.h>
+#include <linux/io.h>
 #include <linux/mm.h>
 #include <linux/highmem.h>
 #include <linux/fs.h>
@@ -773,38 +774,14 @@ pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
        return vma_prot;
 }
 
-#ifdef CONFIG_STRICT_DEVMEM
-/* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM */
-static inline int range_is_allowed(unsigned long pfn, unsigned long size)
-{
-       return 1;
-}
-#else
-/* This check is needed to avoid cache aliasing when PAT is enabled */
-static inline int range_is_allowed(unsigned long pfn, unsigned long size)
-{
-       u64 from = ((u64)pfn) << PAGE_SHIFT;
-       u64 to = from + size;
-       u64 cursor = from;
-
-       if (!pat_enabled())
-               return 1;
-
-       while (cursor < to) {
-               if (!devmem_is_allowed(pfn))
-                       return 0;
-               cursor += PAGE_SIZE;
-               pfn++;
-       }
-       return 1;
-}
-#endif /* CONFIG_STRICT_DEVMEM */
-
 int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
                                unsigned long size, pgprot_t *vma_prot)
 {
        enum page_cache_mode pcm = _PAGE_CACHE_MODE_WB;
 
+       if (!pat_enabled())
+               return 1;
+
        if (!range_is_allowed(pfn, size))
                return 0;
 
index 169eed162a7fba2e8357efc01520fe750b7577f2..48839958b0b15e99482c8b7eef703c1f8530bdd1 100644 (file)
@@ -61,29 +61,11 @@ static inline int page_is_allowed(unsigned long pfn)
 {
        return devmem_is_allowed(pfn);
 }
-static inline int range_is_allowed(unsigned long pfn, unsigned long size)
-{
-       u64 from = ((u64)pfn) << PAGE_SHIFT;
-       u64 to = from + size;
-       u64 cursor = from;
-
-       while (cursor < to) {
-               if (!devmem_is_allowed(pfn))
-                       return 0;
-               cursor += PAGE_SIZE;
-               pfn++;
-       }
-       return 1;
-}
 #else
 static inline int page_is_allowed(unsigned long pfn)
 {
        return 1;
 }
-static inline int range_is_allowed(unsigned long pfn, unsigned long size)
-{
-       return 1;
-}
 #endif
 
 static inline bool should_stop_iteration(void)
index 6a6bc4d46d0af16ac11de6d1b8de7d2a79c712c3..0642c7ee41db71e7228cca9c710dbb7cc9a99096 100644 (file)
@@ -183,4 +183,25 @@ static inline void arch_io_free_memtype_wc(resource_size_t base,
 int devm_arch_io_reserve_memtype_wc(struct device *dev, resource_size_t start,
                                    resource_size_t size);
 
+#ifdef CONFIG_STRICT_DEVMEM
+static inline int range_is_allowed(unsigned long pfn, unsigned long size)
+{
+       u64 from = ((u64)pfn) << PAGE_SHIFT;
+       u64 to = from + size;
+       u64 cursor = from;
+
+       while (cursor < to) {
+               if (!devmem_is_allowed(pfn))
+                       return 0;
+               cursor += PAGE_SIZE;
+               pfn++;
+       }
+       return 1;
+}
+#else
+static inline int range_is_allowed(unsigned long pfn, unsigned long size)
+{
+       return 1;
+}
+#endif
 #endif /* _LINUX_IO_H */