Skip to content

Commit 79eb597

Browse files
danieljordan10torvalds
authored andcommitted
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>
1 parent 73b20c8 commit 79eb597

7 files changed

Lines changed: 98 additions & 190 deletions

File tree

‎arch/powerpc/kvm/book3s_64_vio.c‎

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/anon_inodes.h>
2020
#include <linux/iommu.h>
2121
#include <linux/file.h>
22+
#include <linux/mm.h>
2223

2324
#include <asm/kvm_ppc.h>
2425
#include <asm/kvm_book3s.h>
@@ -45,43 +46,6 @@ static unsigned long kvmppc_stt_pages(unsigned long tce_pages)
4546
return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE;
4647
}
4748

48-
static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
49-
{
50-
long ret = 0;
51-
52-
if (!current || !current->mm)
53-
return ret; /* process exited */
54-
55-
down_write(&current->mm->mmap_sem);
56-
57-
if (inc) {
58-
unsigned long locked, lock_limit;
59-
60-
locked = current->mm->locked_vm + stt_pages;
61-
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
62-
if (locked > lock_limit && !capable(CAP_IPC_LOCK))
63-
ret = -ENOMEM;
64-
else
65-
current->mm->locked_vm += stt_pages;
66-
} else {
67-
if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm))
68-
stt_pages = current->mm->locked_vm;
69-
70-
current->mm->locked_vm -= stt_pages;
71-
}
72-
73-
pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%ld %ld/%ld%s\n", current->pid,
74-
inc ? '+' : '-',
75-
stt_pages << PAGE_SHIFT,
76-
current->mm->locked_vm << PAGE_SHIFT,
77-
rlimit(RLIMIT_MEMLOCK),
78-
ret ? " - exceeded" : "");
79-
80-
up_write(&current->mm->mmap_sem);
81-
82-
return ret;
83-
}
84-
8549
static void kvm_spapr_tce_iommu_table_free(struct rcu_head *head)
8650
{
8751
struct kvmppc_spapr_tce_iommu_table *stit = container_of(head,
@@ -291,7 +255,7 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
291255

292256
kvm_put_kvm(stt->kvm);
293257

294-
kvmppc_account_memlimit(
258+
account_locked_vm(current->mm,
295259
kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);
296260
call_rcu(&stt->rcu, release_spapr_tce_table);
297261

@@ -316,7 +280,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
316280
return -EINVAL;
317281

318282
npages = kvmppc_tce_pages(size);
319-
ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true);
283+
ret = account_locked_vm(current->mm, kvmppc_stt_pages(npages), true);
320284
if (ret)
321285
return ret;
322286

@@ -362,7 +326,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
362326

363327
kfree(stt);
364328
fail_acct:
365-
kvmppc_account_memlimit(kvmppc_stt_pages(npages), false);
329+
account_locked_vm(current->mm, kvmppc_stt_pages(npages), false);
366330
return ret;
367331
}
368332

‎arch/powerpc/mm/book3s64/iommu_api.c‎

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/hugetlb.h>
1515
#include <linux/swap.h>
1616
#include <linux/sizes.h>
17+
#include <linux/mm.h>
1718
#include <asm/mmu_context.h>
1819
#include <asm/pte-walk.h>
1920
#include <linux/mm_inline.h>
@@ -46,40 +47,6 @@ struct mm_iommu_table_group_mem_t {
4647
u64 dev_hpa; /* Device memory base address */
4748
};
4849

49-
static long mm_iommu_adjust_locked_vm(struct mm_struct *mm,
50-
unsigned long npages, bool incr)
51-
{
52-
long ret = 0, locked, lock_limit;
53-
54-
if (!npages)
55-
return 0;
56-
57-
down_write(&mm->mmap_sem);
58-
59-
if (incr) {
60-
locked = mm->locked_vm + npages;
61-
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
62-
if (locked > lock_limit && !capable(CAP_IPC_LOCK))
63-
ret = -ENOMEM;
64-
else
65-
mm->locked_vm += npages;
66-
} else {
67-
if (WARN_ON_ONCE(npages > mm->locked_vm))
68-
npages = mm->locked_vm;
69-
mm->locked_vm -= npages;
70-
}
71-
72-
pr_debug("[%d] RLIMIT_MEMLOCK HASH64 %c%ld %ld/%ld\n",
73-
current ? current->pid : 0,
74-
incr ? '+' : '-',
75-
npages << PAGE_SHIFT,
76-
mm->locked_vm << PAGE_SHIFT,
77-
rlimit(RLIMIT_MEMLOCK));
78-
up_write(&mm->mmap_sem);
79-
80-
return ret;
81-
}
82-
8350
bool mm_iommu_preregistered(struct mm_struct *mm)
8451
{
8552
return !list_empty(&mm->context.iommu_group_mem_list);
@@ -96,7 +63,7 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
9663
unsigned long entry, chunk;
9764

9865
if (dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) {
99-
ret = mm_iommu_adjust_locked_vm(mm, entries, true);
66+
ret = account_locked_vm(mm, entries, true);
10067
if (ret)
10168
return ret;
10269

@@ -211,7 +178,7 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
211178
kfree(mem);
212179

213180
unlock_exit:
214-
mm_iommu_adjust_locked_vm(mm, locked_entries, false);
181+
account_locked_vm(mm, locked_entries, false);
215182

216183
return ret;
217184
}
@@ -311,7 +278,7 @@ long mm_iommu_put(struct mm_struct *mm, struct mm_iommu_table_group_mem_t *mem)
311278
unlock_exit:
312279
mutex_unlock(&mem_list_mutex);
313280

314-
mm_iommu_adjust_locked_vm(mm, unlock_entries, false);
281+
account_locked_vm(mm, unlock_entries, false);
315282

316283
return ret;
317284
}

‎drivers/fpga/dfl-afu-dma-region.c‎

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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"
1718

@@ -31,52 +32,6 @@ void afu_dma_region_init(struct dfl_feature_platform_data *pdata)
3132
afu->dma_regions = RB_ROOT;
3233
}
3334

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 ? "- exceeded" : "");
74-
75-
up_write(&current->mm->mmap_sem);
76-
77-
return ret;
78-
}
79-
8035
/**
8136
* afu_dma_pin_pages - pin pages of given dma memory region
8237
* @pdata: feature device platform data
@@ -92,7 +47,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata,
9247
struct device *dev = &pdata->dev->dev;
9348
int ret, pinned;
9449

95-
ret = afu_dma_adjust_locked_vm(dev, npages, true);
50+
ret = account_locked_vm(current->mm, npages, true);
9651
if (ret)
9752
return ret;
9853

@@ -121,7 +76,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata,
12176
free_pages:
12277
kfree(region->pages);
12378
unlock_vm:
124-
afu_dma_adjust_locked_vm(dev, npages, false);
79+
account_locked_vm(current->mm, npages, false);
12580
return ret;
12681
}
12782

@@ -141,7 +96,7 @@ static void afu_dma_unpin_pages(struct dfl_feature_platform_data *pdata,
14196

14297
put_all_pages(region->pages, npages);
14398
kfree(region->pages);
144-
afu_dma_adjust_locked_vm(dev, npages, false);
99+
account_locked_vm(current->mm, npages, false);
145100

146101
dev_dbg(dev, "%ld pages unpinned\n", npages);
147102
}

‎drivers/vfio/vfio_iommu_spapr_tce.c‎

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/vmalloc.h>
2020
#include <linux/sched/mm.h>
2121
#include <linux/sched/signal.h>
22+
#include <linux/mm.h>
2223

2324
#include <asm/iommu.h>
2425
#include <asm/tce.h>
@@ -31,51 +32,6 @@
3132
static void tce_iommu_detach_group(void *iommu_data,
3233
struct iommu_group *iommu_group);
3334

34-
static 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-
63-
static 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-
7935
/*
8036
* VFIO IOMMU fd for SPAPR_TCE IOMMU implementation
8137
*
@@ -333,7 +289,7 @@ static int tce_iommu_enable(struct tce_container *container)
333289
return ret;
334290

335291
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);
337293
if (ret)
338294
return ret;
339295

@@ -352,7 +308,7 @@ static void tce_iommu_disable(struct tce_container *container)
352308
container->enabled = false;
353309

354310
BUG_ON(!container->mm);
355-
decrement_locked_vm(container->mm, container->locked_pages);
311+
account_locked_vm(container->mm, container->locked_pages, false);
356312
}
357313

358314
static void *tce_iommu_open(unsigned long arg)
@@ -656,7 +612,7 @@ static long tce_iommu_create_table(struct tce_container *container,
656612
if (!table_size)
657613
return -EINVAL;
658614

659-
ret = try_increment_locked_vm(container->mm, table_size >> PAGE_SHIFT);
615+
ret = account_locked_vm(container->mm, table_size >> PAGE_SHIFT, true);
660616
if (ret)
661617
return ret;
662618

@@ -675,7 +631,7 @@ static void tce_iommu_free_table(struct tce_container *container,
675631
unsigned long pages = tbl->it_allocated_size >> PAGE_SHIFT;
676632

677633
iommu_tce_table_put(tbl);
678-
decrement_locked_vm(container->mm, pages);
634+
account_locked_vm(container->mm, pages, false);
679635
}
680636

681637
static long tce_iommu_create_window(struct tce_container *container,

‎drivers/vfio/vfio_iommu_type1.c‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -272,21 +272,8 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)
272272

273273
ret = down_write_killable(&mm->mmap_sem);
274274
if (!ret) {
275-
if (npage > 0) {
276-
if (!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-
275+
ret = __account_locked_vm(mm, abs(npage), npage > 0, dma->task,
276+
dma->lock_cap);
290277
up_write(&mm->mmap_sem);
291278
}
292279

‎include/linux/mm.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,10 @@ long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
15431543
int get_user_pages_fast(unsigned long start, int nr_pages,
15441544
unsigned int gup_flags, struct page **pages);
15451545

1546+
int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc);
1547+
int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc,
1548+
struct task_struct *task, bool bypass_rlim);
1549+
15461550
/* Container for pinned pfns / pages */
15471551
struct frame_vector {
15481552
unsigned int nr_allocated; /* Number of frames we have space for */

0 commit comments

Comments
 (0)