Skip to content

Commit 1e9d90d

Browse files
Nicolin ChenChristoph Hellwig
authored andcommitted
dma-mapping: introduce dma_get_seg_boundary_nr_pages()
We found that callers of dma_get_seg_boundary mostly do an ALIGN with page mask and then do a page shift to get number of pages: ALIGN(boundary + 1, 1 << shift) >> shift However, the boundary might be as large as ULONG_MAX, which means that a device has no specific boundary limit. So either "+ 1" or passing it to ALIGN() would potentially overflow. According to kernel defines: #define ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) #define ALIGN(x, a) ALIGN_MASK(x, (typeof(x))(a) - 1) We can simplify the logic here into a helper function doing: ALIGN(boundary + 1, 1 << shift) >> shift = ALIGN_MASK(b + 1, (1 << s) - 1) >> s = {[b + 1 + (1 << s) - 1] & ~[(1 << s) - 1]} >> s = [b + 1 + (1 << s) - 1] >> s = [b + (1 << s)] >> s = (b >> s) + 1 This patch introduces and applies dma_get_seg_boundary_nr_pages() as an overflow-free helper for the dma_get_seg_boundary() callers to get numbers of pages. It also takes care of the NULL dev case for non-DMA API callers. Suggested-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com> Acked-by: Niklas Schnelle <schnelle@linux.ibm.com> Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc) Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent 2281f79 commit 1e9d90d

11 files changed

Lines changed: 33 additions & 38 deletions

File tree

‎arch/alpha/kernel/pci_iommu.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,7 @@ iommu_arena_find_pages(struct device *dev, struct pci_iommu_arena *arena,
141141
unsigned long boundary_size;
142142

143143
base = arena->dma_base >> PAGE_SHIFT;
144-
if (dev) {
145-
boundary_size = dma_get_seg_boundary(dev) + 1;
146-
boundary_size >>= PAGE_SHIFT;
147-
} else {
148-
boundary_size = 1UL << (32 - PAGE_SHIFT);
149-
}
144+
boundary_size = dma_get_seg_boundary_nr_pages(dev, PAGE_SHIFT);
150145

151146
/* Search forward for the first mask-aligned sequence of N free ptes */
152147
ptes = arena->ptes;

‎arch/ia64/hp/common/sba_iommu.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ sba_search_bitmap(struct ioc *ioc, struct device *dev,
485485
ASSERT(((unsigned long) ioc->res_hint & (sizeof(unsigned long) - 1UL)) == 0);
486486
ASSERT(res_ptr < res_end);
487487

488-
boundary_size = (unsigned long long)dma_get_seg_boundary(dev) + 1;
489-
boundary_size = ALIGN(boundary_size, 1ULL << iovp_shift) >> iovp_shift;
488+
boundary_size = dma_get_seg_boundary_nr_pages(dev, iovp_shift);
490489

491490
BUG_ON(ioc->ibase & ~iovp_mask);
492491
shift = ioc->ibase >> iovp_shift;

‎arch/powerpc/kernel/iommu.c‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ static unsigned long iommu_range_alloc(struct device *dev,
172172
int largealloc = npages > 15;
173173
int pass = 0;
174174
unsigned long align_mask;
175-
unsigned long boundary_size;
176175
unsigned long flags;
177176
unsigned int pool_nr;
178177
struct iommu_pool *pool;
@@ -236,15 +235,9 @@ static unsigned long iommu_range_alloc(struct device *dev,
236235
}
237236
}
238237

239-
if (dev)
240-
boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
241-
1 << tbl->it_page_shift);
242-
else
243-
boundary_size = ALIGN(1UL << 32, 1 << tbl->it_page_shift);
244-
/* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
245-
246238
n = iommu_area_alloc(tbl->it_map, limit, start, npages, tbl->it_offset,
247-
boundary_size >> tbl->it_page_shift, align_mask);
239+
dma_get_seg_boundary_nr_pages(dev, tbl->it_page_shift),
240+
align_mask);
248241
if (n == -1) {
249242
if (likely(pass == 0)) {
250243
/* First try the pool from the start */

‎arch/s390/pci/pci_dma.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,11 @@ static unsigned long __dma_alloc_iommu(struct device *dev,
261261
unsigned long start, int size)
262262
{
263263
struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
264-
unsigned long boundary_size;
265264

266-
boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
267-
PAGE_SIZE) >> PAGE_SHIFT;
268265
return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages,
269266
start, size, zdev->start_dma >> PAGE_SHIFT,
270-
boundary_size, 0);
267+
dma_get_seg_boundary_nr_pages(dev, PAGE_SHIFT),
268+
0);
271269
}
272270

273271
static dma_addr_t dma_alloc_address(struct device *dev, int size)

‎arch/sparc/kernel/iommu-common.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,6 @@ unsigned long iommu_tbl_range_alloc(struct device *dev,
166166
}
167167
}
168168

169-
if (dev)
170-
boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
171-
1 << iommu->table_shift);
172-
else
173-
boundary_size = ALIGN(1ULL << 32, 1 << iommu->table_shift);
174-
175-
boundary_size = boundary_size >> iommu->table_shift;
176169
/*
177170
* if the skip_span_boundary_check had been set during init, we set
178171
* things up so that iommu_is_span_boundary() merely checks if the
@@ -181,6 +174,9 @@ unsigned long iommu_tbl_range_alloc(struct device *dev,
181174
if ((iommu->flags & IOMMU_NO_SPAN_BOUND) != 0) {
182175
shift = 0;
183176
boundary_size = iommu->poolsize * iommu->nr_pools;
177+
} else {
178+
boundary_size = dma_get_seg_boundary_nr_pages(dev,
179+
iommu->table_shift);
184180
}
185181
n = iommu_area_alloc(iommu->map, limit, start, npages, shift,
186182
boundary_size, align_mask);

‎arch/sparc/kernel/iommu.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,7 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
472472
outs->dma_length = 0;
473473

474474
max_seg_size = dma_get_max_seg_size(dev);
475-
seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
476-
IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
475+
seg_boundary_size = dma_get_seg_boundary_nr_pages(dev, IO_PAGE_SHIFT);
477476
base_shift = iommu->tbl.table_map_base >> IO_PAGE_SHIFT;
478477
for_each_sg(sglist, s, nelems, i) {
479478
unsigned long paddr, npages, entry, out_entry = 0, slen;

‎arch/sparc/kernel/pci_sun4v.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,7 @@ static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
508508
iommu_batch_start(dev, prot, ~0UL);
509509

510510
max_seg_size = dma_get_max_seg_size(dev);
511-
seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
512-
IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
511+
seg_boundary_size = dma_get_seg_boundary_nr_pages(dev, IO_PAGE_SHIFT);
513512

514513
mask = *dev->dma_mask;
515514
if (!iommu_use_atu(iommu, mask))

‎arch/x86/kernel/amd_gart_64.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ static unsigned long alloc_iommu(struct device *dev, int size,
9696

9797
base_index = ALIGN(iommu_bus_base & dma_get_seg_boundary(dev),
9898
PAGE_SIZE) >> PAGE_SHIFT;
99-
boundary_size = ALIGN((u64)dma_get_seg_boundary(dev) + 1,
100-
PAGE_SIZE) >> PAGE_SHIFT;
99+
boundary_size = dma_get_seg_boundary_nr_pages(dev, PAGE_SHIFT);
101100

102101
spin_lock_irqsave(&iommu_bitmap_lock, flags);
103102
offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, next_bit,

‎drivers/parisc/ccio-dma.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ ccio_alloc_range(struct ioc *ioc, struct device *dev, size_t size)
356356
** ggg sacrifices another 710 to the computer gods.
357357
*/
358358

359-
boundary_size = ALIGN((unsigned long long)dma_get_seg_boundary(dev) + 1,
360-
1ULL << IOVP_SHIFT) >> IOVP_SHIFT;
359+
boundary_size = dma_get_seg_boundary_nr_pages(dev, IOVP_SHIFT);
361360

362361
if (pages_needed <= 8) {
363362
/*

‎drivers/parisc/sba_iommu.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ sba_search_bitmap(struct ioc *ioc, struct device *dev,
342342
unsigned long shift;
343343
int ret;
344344

345-
boundary_size = ALIGN((unsigned long long)dma_get_seg_boundary(dev) + 1,
346-
1ULL << IOVP_SHIFT) >> IOVP_SHIFT;
345+
boundary_size = dma_get_seg_boundary_nr_pages(dev, IOVP_SHIFT);
347346

348347
#if defined(ZX1_SUPPORT)
349348
BUG_ON(ioc->ibase & ~IOVP_MASK);

0 commit comments

Comments
 (0)