From: Jeff Moyer <jmoyer@redhat.com>
To: linux-aio <linux-aio@kvack.org>, bos@pathscale.com
Cc: zach.brown@oracle.com, bcrl@kvack.org,
Andrew Morton <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org
Subject: [patch] factor out checks against the memlock rlimit
Date: Mon, 09 Mar 2009 11:54:39 -0400 [thread overview]
Message-ID: <x497i2yelog.fsf@segfault.boston.devel.redhat.com> (raw)
In-Reply-To: <x49bpsaelwa.fsf@segfault.boston.devel.redhat.com> (Jeff Moyer's message of "Mon, 09 Mar 2009 11:49:57 -0400")
Hi,
There are several places in the kernel where the memlock rlimit is
checked, all duplicating code. I added another in a recent patch and it
made me feel dirty, so this patch factors all of that into a single
function, can_mlock_pages.
The infiniband implementation of the rlimit check was actually broken.
Weeding through changelogs showed that the initial implementation was
wrong, code was #if 0'd out because it didn't work, and instead of
fixing things, they just removed the code all together. If my
assessment of that is wrong, please let me know! ;-)
Comments, as always, are encouraged.
Cheers,
Jeff
diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c
index 6f7c096..c4d4f1b 100644
--- a/drivers/infiniband/core/umem.c
+++ b/drivers/infiniband/core/umem.c
@@ -135,14 +135,11 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
down_write(¤t->mm->mmap_sem);
- locked = npages + current->mm->locked_vm;
- lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
-
- if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
+ if (!can_mlock_pages(npages)) {
ret = -ENOMEM;
goto out;
}
-
+ locked = npages + current->mm->locked_vm;
cur_base = addr & PAGE_MASK;
ret = 0;
diff --git a/drivers/infiniband/hw/ipath/ipath_user_pages.c b/drivers/infiniband/hw/ipath/ipath_user_pages.c
index 0190edc..9ab17e3 100644
--- a/drivers/infiniband/hw/ipath/ipath_user_pages.c
+++ b/drivers/infiniband/hw/ipath/ipath_user_pages.c
@@ -58,10 +58,7 @@ static int __get_user_pages(unsigned long start_page, size_t num_pages,
size_t got;
int ret;
- lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >>
- PAGE_SHIFT;
-
- if (num_pages > lock_limit) {
+ if (!can_mlock_pages(num_pages)) {
ret = -ENOMEM;
goto bail;
}
diff --git a/fs/aio.c b/fs/aio.c
index 3bbda9d..0ca64eb 100644
--- a/fs/aio.c
+++ b/fs/aio.c
@@ -136,10 +136,7 @@ static int aio_setup_ring(struct kioctx *ctx)
* Check that the memory reserved for the completion ring does
* not exceed the memlock memory limit.
*/
- locked = nr_pages + ctx->mm->locked_vm;
- lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
- lock_limit >>= PAGE_SHIFT;
- if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
+ if (!can_mlock_pages(nr_pages)) {
up_write(&ctx->mm->mmap_sem);
info->mmap_size = 0;
aio_free_ring(ctx);
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 065cdf8..79c1127 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -737,6 +737,7 @@ extern unsigned long shmem_get_unmapped_area(struct file *file,
#endif
extern int can_do_mlock(void);
+extern int can_mlock_pages(unsigned long);
extern int user_shm_lock(size_t, struct user_struct *);
extern void user_shm_unlock(size_t, struct user_struct *);
diff --git a/mm/mlock.c b/mm/mlock.c
index cbe9e05..4ec96c2 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -31,6 +31,33 @@ int can_do_mlock(void)
}
EXPORT_SYMBOL(can_do_mlock);
+/**
+ * can_mlock_pages() - tell whether mlocking nr_pages is possible
+ * @nr_pages: Number of pages to be locked in memory
+ *
+ * Boolean function which tells whether the MEMLOCK rlimit will prevent
+ * locking nr_pages pages.
+ */
+int can_mlock_pages(unsigned long nr_pages)
+{
+ unsigned long locked, lock_limit;
+
+ if (capable(CAP_IPC_LOCK))
+ return 1;
+
+ locked = nr_pages;
+ locked += current->mm->locked_vm;
+
+ lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
+ lock_limit >>= PAGE_SHIFT;
+
+ /* check against resource limits */
+ if (locked <= lock_limit)
+ return 1;
+ return 0;
+}
+EXPORT_SYMBOL(can_mlock_pages);
+
#ifdef CONFIG_UNEVICTABLE_LRU
/*
* Mlocked pages are marked with PageMlocked() flag for efficient testing
@@ -505,14 +532,8 @@ SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
start &= PAGE_MASK;
- locked = len >> PAGE_SHIFT;
- locked += current->mm->locked_vm;
-
- lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
- lock_limit >>= PAGE_SHIFT;
-
/* check against resource limits */
- if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
+ if (can_mlock_pages(len >> PAGE_SHIFT))
error = do_mlock(start, len, 1);
up_write(¤t->mm->mmap_sem);
return error;
diff --git a/mm/mmap.c b/mm/mmap.c
index 00ced3e..52d1c32 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -975,12 +975,7 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
/* mlock MCL_FUTURE? */
if (vm_flags & VM_LOCKED) {
- unsigned long locked, lock_limit;
- locked = len >> PAGE_SHIFT;
- locked += mm->locked_vm;
- lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
- lock_limit >>= PAGE_SHIFT;
- if (locked > lock_limit && !capable(CAP_IPC_LOCK))
+ if (!can_mlock_pages(len >> PAGE_SHIFT))
return -EAGAIN;
}
@@ -2006,12 +2001,7 @@ unsigned long do_brk(unsigned long addr, unsigned long len)
* mlock MCL_FUTURE?
*/
if (mm->def_flags & VM_LOCKED) {
- unsigned long locked, lock_limit;
- locked = len >> PAGE_SHIFT;
- locked += mm->locked_vm;
- lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
- lock_limit >>= PAGE_SHIFT;
- if (locked > lock_limit && !capable(CAP_IPC_LOCK))
+ if (!can_mlock_pages(len>>PAGE_SHIFT))
return -EAGAIN;
}
diff --git a/mm/mremap.c b/mm/mremap.c
index a39b7b9..6fb50e2 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -342,12 +342,8 @@ unsigned long do_mremap(unsigned long addr,
goto out;
}
if (vma->vm_flags & VM_LOCKED) {
- unsigned long locked, lock_limit;
- locked = mm->locked_vm << PAGE_SHIFT;
- lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
- locked += new_len - old_len;
ret = -EAGAIN;
- if (locked > lock_limit && !capable(CAP_IPC_LOCK))
+ if (!can_mlock_pages((new_len - old_len)>>PAGE_SHIFT))
goto out;
}
if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT)) {
next prev parent reply other threads:[~2009-03-09 15:55 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-03-09 15:49 [patch] aio: remove aio-max-nr and instead use the memlock rlimit to limit the number of pages pinned for the aio completion ring Jeff Moyer
2009-03-09 15:54 ` Jeff Moyer [this message]
2009-03-09 15:59 ` [patch] man-pages: add documentation about the memlock implications of io_setup Jeff Moyer
2009-03-09 16:45 ` Michael Kerrisk
2009-03-09 16:48 ` Michael Kerrisk
2009-03-09 20:44 ` Jeff Moyer
2009-03-09 16:18 ` [patch] aio: remove aio-max-nr and instead use the memlock rlimit to limit the number of pages pinned for the aio completion ring Avi Kivity
2009-03-09 17:57 ` Jeff Moyer
2009-03-09 19:45 ` Avi Kivity
2009-03-09 20:36 ` Jamie Lokier
2009-03-10 8:36 ` Avi Kivity
2009-03-09 20:31 ` Eric Dumazet
2009-03-12 2:39 ` Eric Dumazet
2009-03-12 2:44 ` Benjamin LaHaise
2009-03-12 3:24 ` Eric Dumazet
2009-03-12 3:29 ` Benjamin LaHaise
2009-03-12 3:33 ` Eric Dumazet
2009-03-12 3:36 ` Benjamin LaHaise
2009-03-12 3:40 ` Eric Dumazet
2009-03-12 3:09 ` Eric Dumazet
2009-03-12 5:18 ` [PATCH] fs: fput() can be called from interrupt context Eric Dumazet
2009-03-12 5:42 ` [PATCH] aio: " Eric Dumazet
2009-03-12 5:47 ` [PATCH] fs: " Andrew Morton
2009-03-12 6:10 ` Eric Dumazet
2009-03-12 6:39 ` Andrew Morton
2009-03-12 13:39 ` Davide Libenzi
2009-03-13 22:34 ` Davide Libenzi
2009-03-13 22:43 ` Eric Dumazet
2009-03-13 23:28 ` Trond Myklebust
2009-03-14 1:40 ` Davide Libenzi
2009-03-14 4:02 ` Trond Myklebust
2009-03-14 14:32 ` Davide Libenzi
2009-03-15 1:36 ` [patch] eventfd - remove fput() call from possible IRQ context Davide Libenzi
2009-03-15 17:44 ` Benjamin LaHaise
2009-03-15 20:08 ` [patch] eventfd - remove fput() call from possible IRQ context (2nd rev) Davide Libenzi
2009-03-16 17:25 ` Jamie Lokier
2009-03-16 18:36 ` Davide Libenzi
2009-03-18 14:22 ` Jeff Moyer
2009-03-18 14:46 ` Davide Libenzi
2009-03-18 14:55 ` Eric Dumazet
2009-03-18 15:25 ` Jeff Moyer
2009-03-18 15:43 ` Eric Dumazet
2009-03-18 16:13 ` Jeff Moyer
2009-03-18 17:25 ` [patch] eventfd - remove fput() call from possible IRQ context (3rd rev) Davide Libenzi
2009-03-18 17:34 ` Jeff Moyer
2009-03-12 19:22 ` [PATCH] fs: fput() can be called from interrupt context Eric Dumazet
2009-03-12 20:21 ` Andrew Morton
2009-03-09 22:36 ` [patch] aio: remove aio-max-nr and instead use the memlock rlimit to limit the number of pages pinned for the aio completion ring Andrew Morton
2009-03-10 13:43 ` Jeff Moyer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=x497i2yelog.fsf@segfault.boston.devel.redhat.com \
--to=jmoyer@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=bcrl@kvack.org \
--cc=bos@pathscale.com \
--cc=linux-aio@kvack.org \
--cc=linux-kernel@vger.kernel.org \
--cc=zach.brown@oracle.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®