hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/fpga/dfl-afu-dma-region.c
....@@ -12,69 +12,15 @@
1212 #include <linux/dma-mapping.h>
1313 #include <linux/sched/signal.h>
1414 #include <linux/uaccess.h>
15
+#include <linux/mm.h>
1516
1617 #include "dfl-afu.h"
17
-
18
-static void put_all_pages(struct page **pages, int npages)
19
-{
20
- int i;
21
-
22
- for (i = 0; i < npages; i++)
23
- if (pages[i])
24
- put_page(pages[i]);
25
-}
2618
2719 void afu_dma_region_init(struct dfl_feature_platform_data *pdata)
2820 {
2921 struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
3022
3123 afu->dma_regions = RB_ROOT;
32
-}
33
-
34
-/**
35
- * afu_dma_adjust_locked_vm - adjust locked memory
36
- * @dev: port device
37
- * @npages: number of pages
38
- * @incr: increase or decrease locked memory
39
- *
40
- * Increase or decrease the locked memory size with npages input.
41
- *
42
- * Return 0 on success.
43
- * Return -ENOMEM if locked memory size is over the limit and no CAP_IPC_LOCK.
44
- */
45
-static int afu_dma_adjust_locked_vm(struct device *dev, long npages, bool incr)
46
-{
47
- unsigned long locked, lock_limit;
48
- int ret = 0;
49
-
50
- /* the task is exiting. */
51
- if (!current->mm)
52
- return 0;
53
-
54
- down_write(&current->mm->mmap_sem);
55
-
56
- if (incr) {
57
- locked = current->mm->locked_vm + npages;
58
- lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
59
-
60
- if (locked > lock_limit && !capable(CAP_IPC_LOCK))
61
- ret = -ENOMEM;
62
- else
63
- current->mm->locked_vm += npages;
64
- } else {
65
- if (WARN_ON_ONCE(npages > current->mm->locked_vm))
66
- npages = current->mm->locked_vm;
67
- current->mm->locked_vm -= npages;
68
- }
69
-
70
- dev_dbg(dev, "[%d] RLIMIT_MEMLOCK %c%ld %ld/%ld%s\n", current->pid,
71
- incr ? '+' : '-', npages << PAGE_SHIFT,
72
- current->mm->locked_vm << PAGE_SHIFT, rlimit(RLIMIT_MEMLOCK),
73
- ret ? "- execeeded" : "");
74
-
75
- up_write(&current->mm->mmap_sem);
76
-
77
- return ret;
7824 }
7925
8026 /**
....@@ -92,7 +38,7 @@
9238 struct device *dev = &pdata->dev->dev;
9339 int ret, pinned;
9440
95
- ret = afu_dma_adjust_locked_vm(dev, npages, true);
41
+ ret = account_locked_vm(current->mm, npages, true);
9642 if (ret)
9743 return ret;
9844
....@@ -102,26 +48,26 @@
10248 goto unlock_vm;
10349 }
10450
105
- pinned = get_user_pages_fast(region->user_addr, npages, 1,
51
+ pinned = pin_user_pages_fast(region->user_addr, npages, FOLL_WRITE,
10652 region->pages);
10753 if (pinned < 0) {
10854 ret = pinned;
10955 goto free_pages;
11056 } else if (pinned != npages) {
11157 ret = -EFAULT;
112
- goto put_pages;
58
+ goto unpin_pages;
11359 }
11460
11561 dev_dbg(dev, "%d pages pinned\n", pinned);
11662
11763 return 0;
11864
119
-put_pages:
120
- put_all_pages(region->pages, pinned);
65
+unpin_pages:
66
+ unpin_user_pages(region->pages, pinned);
12167 free_pages:
12268 kfree(region->pages);
12369 unlock_vm:
124
- afu_dma_adjust_locked_vm(dev, npages, false);
70
+ account_locked_vm(current->mm, npages, false);
12571 return ret;
12672 }
12773
....@@ -139,9 +85,9 @@
13985 long npages = region->length >> PAGE_SHIFT;
14086 struct device *dev = &pdata->dev->dev;
14187
142
- put_all_pages(region->pages, npages);
88
+ unpin_user_pages(region->pages, npages);
14389 kfree(region->pages);
144
- afu_dma_adjust_locked_vm(dev, npages, false);
90
+ account_locked_vm(current->mm, npages, false);
14591
14692 dev_dbg(dev, "%ld pages unpinned\n", npages);
14793 }
....@@ -367,10 +313,6 @@
367313
368314 /* Check overflow */
369315 if (user_addr + length < user_addr)
370
- return -EINVAL;
371
-
372
- if (!access_ok(VERIFY_WRITE, (void __user *)(unsigned long)user_addr,
373
- length))
374316 return -EINVAL;
375317
376318 region = kzalloc(sizeof(*region), GFP_KERNEL);