aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorDaniel Jordan <daniel.m.jordan@oracle.com>2019-07-16 19:30:54 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2019-07-16 22:23:25 -0400
commit79eb597cba06c435b72f220e9d426ae413fc2579 (patch)
treee639df77d252fde2e81fee12a90bc840f6bca7fc /drivers
parent73b20c84d42de14673a987816dd4d132c7b1f801 (diff)
mm: add account_locked_vm utility function
locked_vm accounting is done roughly the same way in five places, so unify them in a helper. Include the helper's caller in the debug print to distinguish between callsites. Error codes stay the same, so user-visible behavior does too. The one exception is that the -EPERM case in tce_account_locked_vm is removed because Alexey has never seen it triggered. [daniel.m.jordan@oracle.com: v3] Link: http://lkml.kernel.org/r/20190529205019.20927-1-daniel.m.jordan@oracle.com [sfr@canb.auug.org.au: fix mm/util.c] Link: http://lkml.kernel.org/r/20190524175045.26897-1-daniel.m.jordan@oracle.com Signed-off-by: Daniel Jordan <daniel.m.jordan@oracle.com> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au> Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru> Acked-by: Alex Williamson <alex.williamson@redhat.com> Cc: Alan Tull <atull@kernel.org> Cc: Alex Williamson <alex.williamson@redhat.com> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Christoph Lameter <cl@linux.com> Cc: Christophe Leroy <christophe.leroy@c-s.fr> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Jason Gunthorpe <jgg@mellanox.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Moritz Fischer <mdf@kernel.org> Cc: Paul Mackerras <paulus@ozlabs.org> Cc: Steve Sistare <steven.sistare@oracle.com> Cc: Wu Hao <hao.wu@intel.com> Cc: Ira Weiny <ira.weiny@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/fpga/dfl-afu-dma-region.c53
-rw-r--r--drivers/vfio/vfio_iommu_spapr_tce.c54
-rw-r--r--drivers/vfio/vfio_iommu_type1.c17
3 files changed, 11 insertions, 113 deletions
diff --git a/drivers/fpga/dfl-afu-dma-region.c b/drivers/fpga/dfl-afu-dma-region.c
index dcd80b088c7b..62f924489db5 100644
--- a/drivers/fpga/dfl-afu-dma-region.c
+++ b/drivers/fpga/dfl-afu-dma-region.c
@@ -12,6 +12,7 @@
12#include <linux/dma-mapping.h> 12#include <linux/dma-mapping.h>
13#include <linux/sched/signal.h> 13#include <linux/sched/signal.h>
14#include <linux/uaccess.h> 14#include <linux/uaccess.h>
15#include <linux/mm.h>
15 16
16#include "dfl-afu.h" 17#include "dfl-afu.h"
17 18
@@ -32,52 +33,6 @@ void afu_dma_region_init(struct dfl_feature_platform_data *pdata)
32} 33}
33 34
34/** 35/**
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 */
45static 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 ? "- exceeded" : "");
74
75 up_write(&current->mm->mmap_sem);
76
77 return ret;
78}
79
80/**
81 * afu_dma_pin_pages - pin pages of given dma memory region 36 * afu_dma_pin_pages - pin pages of given dma memory region
82 * @pdata: feature device platform data 37 * @pdata: feature device platform data
83 * @region: dma memory region to be pinned 38 * @region: dma memory region to be pinned
@@ -92,7 +47,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata,
92 struct device *dev = &pdata->dev->dev; 47 struct device *dev = &pdata->dev->dev;
93 int ret, pinned; 48 int ret, pinned;
94 49
95 ret = afu_dma_adjust_locked_vm(dev, npages, true); 50 ret = account_locked_vm(current->mm, npages, true);
96 if (ret) 51 if (ret)
97 return ret; 52 return ret;
98 53
@@ -121,7 +76,7 @@ put_pages:
121free_pages: 76free_pages:
122 kfree(region->pages); 77 kfree(region->pages);
123unlock_vm: 78unlock_vm:
124 afu_dma_adjust_locked_vm(dev, npages, false); 79 account_locked_vm(current->mm, npages, false);
125 return ret; 80 return ret;
126} 81}
127 82
@@ -141,7 +96,7 @@ static void afu_dma_unpin_pages(struct dfl_feature_platform_data *pdata,
141 96
142 put_all_pages(region->pages, npages); 97 put_all_pages(region->pages, npages);
143 kfree(region->pages); 98 kfree(region->pages);
144 afu_dma_adjust_locked_vm(dev, npages, false); 99 account_locked_vm(current->mm, npages, false);
145 100
146 dev_dbg(dev, "%ld pages unpinned\n", npages); 101 dev_dbg(dev, "%ld pages unpinned\n", npages);
147} 102}
diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c
index 7048c9198c21..8ce9ad21129f 100644
--- a/drivers/vfio/vfio_iommu_spapr_tce.c
+++ b/drivers/vfio/vfio_iommu_spapr_tce.c
@@ -19,6 +19,7 @@
19#include <linux/vmalloc.h> 19#include <linux/vmalloc.h>
20#include <linux/sched/mm.h> 20#include <linux/sched/mm.h>
21#include <linux/sched/signal.h> 21#include <linux/sched/signal.h>
22#include <linux/mm.h>
22 23
23#include <asm/iommu.h> 24#include <asm/iommu.h>
24#include <asm/tce.h> 25#include <asm/tce.h>
@@ -31,51 +32,6 @@
31static void tce_iommu_detach_group(void *iommu_data, 32static void tce_iommu_detach_group(void *iommu_data,
32 struct iommu_group *iommu_group); 33 struct iommu_group *iommu_group);
33 34
34static long try_increment_locked_vm(struct mm_struct *mm, long npages)
35{
36 long ret = 0, locked, lock_limit;
37
38 if (WARN_ON_ONCE(!mm))
39 return -EPERM;
40
41 if (!npages)
42 return 0;
43
44 down_write(&mm->mmap_sem);
45 locked = mm->locked_vm + npages;
46 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
47 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
48 ret = -ENOMEM;
49 else
50 mm->locked_vm += npages;
51
52 pr_debug("[%d] RLIMIT_MEMLOCK +%ld %ld/%ld%s\n", current->pid,
53 npages << PAGE_SHIFT,
54 mm->locked_vm << PAGE_SHIFT,
55 rlimit(RLIMIT_MEMLOCK),
56 ret ? " - exceeded" : "");
57
58 up_write(&mm->mmap_sem);
59
60 return ret;
61}
62
63static void decrement_locked_vm(struct mm_struct *mm, long npages)
64{
65 if (!mm || !npages)
66 return;
67
68 down_write(&mm->mmap_sem);
69 if (WARN_ON_ONCE(npages > mm->locked_vm))
70 npages = mm->locked_vm;
71 mm->locked_vm -= npages;
72 pr_debug("[%d] RLIMIT_MEMLOCK -%ld %ld/%ld\n", current->pid,
73 npages << PAGE_SHIFT,
74 mm->locked_vm << PAGE_SHIFT,
75 rlimit(RLIMIT_MEMLOCK));
76 up_write(&mm->mmap_sem);
77}
78
79/* 35/*
80 * VFIO IOMMU fd for SPAPR_TCE IOMMU implementation 36 * VFIO IOMMU fd for SPAPR_TCE IOMMU implementation
81 * 37 *
@@ -333,7 +289,7 @@ static int tce_iommu_enable(struct tce_container *container)
333 return ret; 289 return ret;
334 290
335 locked = table_group->tce32_size >> PAGE_SHIFT; 291 locked = table_group->tce32_size >> PAGE_SHIFT;
336 ret = try_increment_locked_vm(container->mm, locked); 292 ret = account_locked_vm(container->mm, locked, true);
337 if (ret) 293 if (ret)
338 return ret; 294 return ret;
339 295
@@ -352,7 +308,7 @@ static void tce_iommu_disable(struct tce_container *container)
352 container->enabled = false; 308 container->enabled = false;
353 309
354 BUG_ON(!container->mm); 310 BUG_ON(!container->mm);
355 decrement_locked_vm(container->mm, container->locked_pages); 311 account_locked_vm(container->mm, container->locked_pages, false);
356} 312}
357 313
358static void *tce_iommu_open(unsigned long arg) 314static void *tce_iommu_open(unsigned long arg)
@@ -656,7 +612,7 @@ static long tce_iommu_create_table(struct tce_container *container,
656 if (!table_size) 612 if (!table_size)
657 return -EINVAL; 613 return -EINVAL;
658 614
659 ret = try_increment_locked_vm(container->mm, table_size >> PAGE_SHIFT); 615 ret = account_locked_vm(container->mm, table_size >> PAGE_SHIFT, true);
660 if (ret) 616 if (ret)
661 return ret; 617 return ret;
662 618
@@ -675,7 +631,7 @@ static void tce_iommu_free_table(struct tce_container *container,
675 unsigned long pages = tbl->it_allocated_size >> PAGE_SHIFT; 631 unsigned long pages = tbl->it_allocated_size >> PAGE_SHIFT;
676 632
677 iommu_tce_table_put(tbl); 633 iommu_tce_table_put(tbl);
678 decrement_locked_vm(container->mm, pages); 634 account_locked_vm(container->mm, pages, false);
679} 635}
680 636
681static long tce_iommu_create_window(struct tce_container *container, 637static long tce_iommu_create_window(struct tce_container *container,
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index add34adfadc7..054391f30fa8 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -272,21 +272,8 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)
272 272
273 ret = down_write_killable(&mm->mmap_sem); 273 ret = down_write_killable(&mm->mmap_sem);
274 if (!ret) { 274 if (!ret) {
275 if (npage > 0) { 275 ret = __account_locked_vm(mm, abs(npage), npage > 0, dma->task,
276 if (!dma->lock_cap) { 276 dma->lock_cap);
277 unsigned long limit;
278
279 limit = task_rlimit(dma->task,
280 RLIMIT_MEMLOCK) >> PAGE_SHIFT;
281
282 if (mm->locked_vm + npage > limit)
283 ret = -ENOMEM;
284 }
285 }
286
287 if (!ret)
288 mm->locked_vm += npage;
289
290 up_write(&mm->mmap_sem); 277 up_write(&mm->mmap_sem);
291 } 278 }
292 279