Skip to content

Commit c87a126

Browse files
Huan Yangvivekkreddy
authored andcommitted
udmabuf: reuse folio array when pin folios
When invoke memfd_pin_folios, we need offer an array to save each folio which we pinned. The current way is dynamic alloc an array(use kvmalloc), get folios, save into udmabuf and then free. Depend on the size, kvmalloc can do something different: Below PAGE_SIZE, slab allocator will be used, which have good alloc performance, due to it cached page. PAGE_SIZE - PCP Order, PCP(per-cpu-pageset) also given buddy page a cache in each CPU, so different CPU no need to hold some lock(zone or some) to get the locally page. If PCP cached page, the access also fast. PAGE_SIZE - BUDDY_MAX, try to get page from buddy, due to kvmalloc adjusted the gfp flags, if zone freelist can't alloc page(fast path), we will not enter slowpath to reclaim memory. Due to need hold lock and check, may slow, but still fast than vmalloc. Anything wrong will fallback into vmalloc to alloc memory, it obtains contiguous virtual addresses by loop alloc order 0 page(PAGE_SIZE), and then map it into vmalloc area. If necessary, page alloc may enter slowpath to reclaim memory. Hence, if fallback into vmalloc, it's slow. When create, we need to iter each udmabuf item, then pin it's range folios, if each item's range folio's count is large, we may fallback each into vmalloc. This patch find the largest range folio in items, then alloc this size's folio array. When pin range folios, reuse this array. Signed-off-by: Huan Yang <link@vivo.com> Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com> Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20240918025238.2957823-8-link@vivo.com
1 parent 6b68b74 commit c87a126

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

drivers/dma-buf/udmabuf.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,28 +338,20 @@ static int export_udmabuf(struct udmabuf *ubuf,
338338
}
339339

340340
static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
341-
loff_t start, loff_t size)
341+
loff_t start, loff_t size, struct folio **folios)
342342
{
343343
pgoff_t nr_pinned = ubuf->nr_pinned;
344344
pgoff_t upgcnt = ubuf->pagecount;
345-
struct folio **folios = NULL;
346345
u32 cur_folio, cur_pgcnt;
347346
pgoff_t pgoff, pgcnt;
348347
long nr_folios;
349-
long ret = 0;
350348
loff_t end;
351349

352350
pgcnt = size >> PAGE_SHIFT;
353-
folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
354-
if (!folios)
355-
return -ENOMEM;
356-
357351
end = start + (pgcnt << PAGE_SHIFT) - 1;
358352
nr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt, &pgoff);
359-
if (nr_folios <= 0) {
360-
ret = nr_folios ? nr_folios : -EINVAL;
361-
goto end;
362-
}
353+
if (nr_folios <= 0)
354+
return nr_folios ? nr_folios : -EINVAL;
363355

364356
cur_pgcnt = 0;
365357
for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) {
@@ -388,14 +380,15 @@ static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
388380
end:
389381
ubuf->pagecount = upgcnt;
390382
ubuf->nr_pinned = nr_pinned;
391-
kvfree(folios);
392-
return ret;
383+
return 0;
393384
}
394385

395386
static long udmabuf_create(struct miscdevice *device,
396387
struct udmabuf_create_list *head,
397388
struct udmabuf_create_item *list)
398389
{
390+
unsigned long max_nr_folios = 0;
391+
struct folio **folios = NULL;
399392
pgoff_t pgcnt = 0, pglimit;
400393
struct udmabuf *ubuf;
401394
long ret = -EINVAL;
@@ -407,14 +400,19 @@ static long udmabuf_create(struct miscdevice *device,
407400

408401
pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
409402
for (i = 0; i < head->count; i++) {
403+
pgoff_t subpgcnt;
404+
410405
if (!PAGE_ALIGNED(list[i].offset))
411406
goto err_noinit;
412407
if (!PAGE_ALIGNED(list[i].size))
413408
goto err_noinit;
414409

415-
pgcnt += list[i].size >> PAGE_SHIFT;
410+
subpgcnt = list[i].size >> PAGE_SHIFT;
411+
pgcnt += subpgcnt;
416412
if (pgcnt > pglimit)
417413
goto err_noinit;
414+
415+
max_nr_folios = max_t(unsigned long, subpgcnt, max_nr_folios);
418416
}
419417

420418
if (!pgcnt)
@@ -424,6 +422,12 @@ static long udmabuf_create(struct miscdevice *device,
424422
if (ret)
425423
goto err;
426424

425+
folios = kvmalloc_array(max_nr_folios, sizeof(*folios), GFP_KERNEL);
426+
if (!folios) {
427+
ret = -ENOMEM;
428+
goto err;
429+
}
430+
427431
for (i = 0; i < head->count; i++) {
428432
struct file *memfd = fget(list[i].memfd);
429433

@@ -439,7 +443,7 @@ static long udmabuf_create(struct miscdevice *device,
439443
}
440444

441445
ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset,
442-
list[i].size);
446+
list[i].size, folios);
443447
fput(memfd);
444448
if (ret)
445449
goto err;
@@ -450,12 +454,14 @@ static long udmabuf_create(struct miscdevice *device,
450454
if (ret < 0)
451455
goto err;
452456

457+
kvfree(folios);
453458
return ret;
454459

455460
err:
456461
deinit_udmabuf(ubuf);
457462
err_noinit:
458463
kfree(ubuf);
464+
kvfree(folios);
459465
return ret;
460466
}
461467

0 commit comments

Comments
 (0)