94a1329d84986320f5891bd156ed1734febb0b13
[openwrt/staging/lynxis.git] /
1 From b763f24aed409296eb76d085c279b2c40462f8a1 Mon Sep 17 00:00:00 2001
2 From: Christoph Hellwig <hch@lst.de>
3 Date: Tue, 19 Nov 2019 17:38:58 +0100
4 Subject: [PATCH] dma-direct: exclude dma_direct_map_resource from the
5 min_low_pfn check
6
7 commit 68a33b1794665ba8a1d1ef1d3bfcc7c587d380a6 upstream.
8
9 The valid memory address check in dma_capable only makes sense when mapping
10 normal memory, not when using dma_map_resource to map a device resource.
11 Add a new boolean argument to dma_capable to exclude that check for the
12 dma_map_resource case.
13
14 Fixes: b12d66278dd6 ("dma-direct: check for overflows on 32 bit DMA addresses")
15 Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
16 Signed-off-by: Christoph Hellwig <hch@lst.de>
17 Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
18 Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
19 ---
20 arch/x86/kernel/amd_gart_64.c | 4 ++--
21 drivers/xen/swiotlb-xen.c | 4 ++--
22 include/linux/dma-direct.h | 5 +++--
23 kernel/dma/direct.c | 4 ++--
24 kernel/dma/swiotlb.c | 2 +-
25 5 files changed, 10 insertions(+), 9 deletions(-)
26
27 --- a/arch/x86/kernel/amd_gart_64.c
28 +++ b/arch/x86/kernel/amd_gart_64.c
29 @@ -185,13 +185,13 @@ static void iommu_full(struct device *de
30 static inline int
31 need_iommu(struct device *dev, unsigned long addr, size_t size)
32 {
33 - return force_iommu || !dma_capable(dev, addr, size);
34 + return force_iommu || !dma_capable(dev, addr, size, true);
35 }
36
37 static inline int
38 nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
39 {
40 - return !dma_capable(dev, addr, size);
41 + return !dma_capable(dev, addr, size, true);
42 }
43
44 /* Map a single continuous physical area into the IOMMU.
45 --- a/drivers/xen/swiotlb-xen.c
46 +++ b/drivers/xen/swiotlb-xen.c
47 @@ -375,7 +375,7 @@ static dma_addr_t xen_swiotlb_map_page(s
48 * we can safely return the device addr and not worry about bounce
49 * buffering it.
50 */
51 - if (dma_capable(dev, dev_addr, size) &&
52 + if (dma_capable(dev, dev_addr, size, true) &&
53 !range_straddles_page_boundary(phys, size) &&
54 !xen_arch_need_swiotlb(dev, phys, dev_addr) &&
55 swiotlb_force != SWIOTLB_FORCE)
56 @@ -397,7 +397,7 @@ static dma_addr_t xen_swiotlb_map_page(s
57 /*
58 * Ensure that the address returned is DMA'ble
59 */
60 - if (unlikely(!dma_capable(dev, dev_addr, size))) {
61 + if (unlikely(!dma_capable(dev, dev_addr, size, true))) {
62 swiotlb_tbl_unmap_single(dev, map, size, size, dir,
63 attrs | DMA_ATTR_SKIP_CPU_SYNC);
64 return DMA_MAPPING_ERROR;
65 --- a/include/linux/dma-direct.h
66 +++ b/include/linux/dma-direct.h
67 @@ -51,14 +51,15 @@ static inline phys_addr_t dma_to_phys(st
68 return __sme_clr(__dma_to_phys(dev, daddr));
69 }
70
71 -static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
72 +static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size,
73 + bool is_ram)
74 {
75 dma_addr_t end = addr + size - 1;
76
77 if (!dev->dma_mask)
78 return false;
79
80 - if (!IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) &&
81 + if (is_ram && !IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) &&
82 min(addr, end) < phys_to_dma(dev, PFN_PHYS(min_low_pfn)))
83 return false;
84
85 --- a/kernel/dma/direct.c
86 +++ b/kernel/dma/direct.c
87 @@ -325,7 +325,7 @@ static inline bool dma_direct_possible(s
88 size_t size)
89 {
90 return swiotlb_force != SWIOTLB_FORCE &&
91 - dma_capable(dev, dma_addr, size);
92 + dma_capable(dev, dma_addr, size, true);
93 }
94
95 dma_addr_t dma_direct_map_page(struct device *dev, struct page *page,
96 @@ -374,7 +374,7 @@ dma_addr_t dma_direct_map_resource(struc
97 {
98 dma_addr_t dma_addr = paddr;
99
100 - if (unlikely(!dma_capable(dev, dma_addr, size))) {
101 + if (unlikely(!dma_capable(dev, dma_addr, size, false))) {
102 report_addr(dev, dma_addr, size);
103 return DMA_MAPPING_ERROR;
104 }
105 --- a/kernel/dma/swiotlb.c
106 +++ b/kernel/dma/swiotlb.c
107 @@ -678,7 +678,7 @@ bool swiotlb_map(struct device *dev, phy
108
109 /* Ensure that the address returned is DMA'ble */
110 *dma_addr = __phys_to_dma(dev, *phys);
111 - if (unlikely(!dma_capable(dev, *dma_addr, size))) {
112 + if (unlikely(!dma_capable(dev, *dma_addr, size, true))) {
113 swiotlb_tbl_unmap_single(dev, *phys, size, size, dir,
114 attrs | DMA_ATTR_SKIP_CPU_SYNC);
115 return false;