Skip to content

Commit 1b8e5d1

Browse files
author
Christoph Hellwig
committed
swiotlb: use the right nslabs-derived sizes in swiotlb_init_late
nslabs can shrink when allocations or the remap don't succeed, so make sure to use it for all sizing. For that remove the bytes value that can get stale and replace it with local calculations and a boolean to indicate if the originally requested size could not be allocated. Fixes: 6424e31 ("swiotlb: remove swiotlb_init_with_tbl and swiotlb_init_late_with_tbl") Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Stefano Stabellini <[email protected]>
1 parent a5e8913 commit 1b8e5d1

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

kernel/dma/swiotlb.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
297297
{
298298
struct io_tlb_mem *mem = &io_tlb_default_mem;
299299
unsigned long nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
300-
unsigned long bytes;
301300
unsigned char *vstart = NULL;
302301
unsigned int order;
302+
bool retried = false;
303303
int rc = 0;
304304

305305
if (swiotlb_force_disable)
@@ -308,24 +308,20 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
308308
retry:
309309
order = get_order(nslabs << IO_TLB_SHIFT);
310310
nslabs = SLABS_PER_PAGE << order;
311-
bytes = nslabs << IO_TLB_SHIFT;
312311

313312
while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
314313
vstart = (void *)__get_free_pages(gfp_mask | __GFP_NOWARN,
315314
order);
316315
if (vstart)
317316
break;
318317
order--;
318+
nslabs = SLABS_PER_PAGE << order;
319+
retried = true;
319320
}
320321

321322
if (!vstart)
322323
return -ENOMEM;
323324

324-
if (order != get_order(bytes)) {
325-
pr_warn("only able to allocate %ld MB\n",
326-
(PAGE_SIZE << order) >> 20);
327-
nslabs = SLABS_PER_PAGE << order;
328-
}
329325
if (remap)
330326
rc = remap(vstart, nslabs);
331327
if (rc) {
@@ -334,17 +330,24 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
334330
nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
335331
if (nslabs < IO_TLB_MIN_SLABS)
336332
return rc;
333+
retried = true;
337334
goto retry;
338335
}
339336

337+
if (retried) {
338+
pr_warn("only able to allocate %ld MB\n",
339+
(PAGE_SIZE << order) >> 20);
340+
}
341+
340342
mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
341343
get_order(array_size(sizeof(*mem->slots), nslabs)));
342344
if (!mem->slots) {
343345
free_pages((unsigned long)vstart, order);
344346
return -ENOMEM;
345347
}
346348

347-
set_memory_decrypted((unsigned long)vstart, bytes >> PAGE_SHIFT);
349+
set_memory_decrypted((unsigned long)vstart,
350+
(nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT);
348351
swiotlb_init_io_tlb_mem(mem, virt_to_phys(vstart), nslabs, true);
349352

350353
swiotlb_print_info();

0 commit comments

Comments
 (0)