From: Alistair Popple <apopple@nvidia.com> To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple <apopple@nvidia.com> Subject: [RFC PATCH 16/19] mm/util: Refactor account_locked_vm Date: Tue, 24 Jan 2023 16:42:45 +1100 [thread overview] Message-ID: <e72f46bd6be4f3e3c85ba6a560201cf23bdb39a9.1674538665.git-series.apopple@nvidia.com> (raw) In-Reply-To: <cover.f52b9eb2792bccb8a9ecd6bc95055705cfe2ae03.1674538665.git-series.apopple@nvidia.com> account_locked_vm() takes a flag to indicate if pages are being accounted or unaccounted for. A flag is also provided to bypass rlimits. However unaccounting of pages always succeeds and the flag to ignore the limits is ignored. The flags make calling code harder to understand so refactor the accounting and unaccounting paths into separate functions. Signed-off-by: Alistair Popple <apopple@nvidia.com> Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- include/linux/mm.h | 5 +-- mm/util.c | 73 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8f85716..126b756 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2090,9 +2090,10 @@ int get_user_pages_fast(unsigned long start, int nr_pages, int pin_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); -int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc); -int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, +int account_locked_vm(struct mm_struct *mm, unsigned long pages); +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim); +void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages); struct kvec; int get_kernel_pages(const struct kvec *iov, int nr_pages, int write, diff --git a/mm/util.c b/mm/util.c index e5fb01a..78b060d 100644 --- a/mm/util.c +++ b/mm/util.c @@ -537,7 +537,6 @@ EXPORT_SYMBOL_GPL(vm_unaccount_pinned); * __account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against * @pages: number of pages to account - * @inc: %true if @pages should be considered positive, %false if not * @task: task used to check RLIMIT_MEMLOCK * @bypass_rlim: %true if checking RLIMIT_MEMLOCK should be skipped * @@ -548,7 +547,7 @@ EXPORT_SYMBOL_GPL(vm_unaccount_pinned); * * 0 on success * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. */ -int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim) { unsigned long locked_vm, limit; @@ -557,33 +556,44 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, mmap_assert_write_locked(mm); locked_vm = mm->locked_vm; - if (inc) { - if (!bypass_rlim) { - limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; - if (locked_vm + pages > limit) - ret = -ENOMEM; - } - if (!ret) - mm->locked_vm = locked_vm + pages; - } else { - WARN_ON_ONCE(pages > locked_vm); - mm->locked_vm = locked_vm - pages; + if (!bypass_rlim) { + limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; + if (locked_vm + pages > limit) + ret = -ENOMEM; } - pr_debug("%s: [%d] caller %ps %c%lu %lu/%lu%s\n", __func__, task->pid, - (void *)_RET_IP_, (inc) ? '+' : '-', pages << PAGE_SHIFT, - locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK), - ret ? " - exceeded" : ""); + if (!ret) + mm->locked_vm = locked_vm + pages; + + pr_debug("%s: [%d] caller %ps %lu %lu/%lu%s\n", __func__, task->pid, + (void *)_RET_IP_, pages << PAGE_SHIFT, locked_vm << PAGE_SHIFT, + task_rlimit(task, RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); return ret; } EXPORT_SYMBOL_GPL(__account_locked_vm); /** + * __unaccount_locked_vm - unaccount locked pages to an mm's locked_vm + * @mm: mm to account against + * @pages: number of pages to account + * + * Assumes @mm are valid and that mmap_lock is held as writer. + */ +void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) +{ + unsigned long locked_vm = mm->locked_vm; + + mmap_assert_write_locked(mm); + WARN_ON_ONCE(pages > locked_vm); + mm->locked_vm = locked_vm - pages; +} +EXPORT_SYMBOL_GPL(__unaccount_locked_vm); + +/** * account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against, may be NULL * @pages: number of pages to account - * @inc: %true if @pages should be considered positive, %false if not * * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). * @@ -591,7 +601,7 @@ EXPORT_SYMBOL_GPL(__account_locked_vm); * * 0 on success, or if mm is NULL * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. */ -int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) +int account_locked_vm(struct mm_struct *mm, unsigned long pages) { int ret; @@ -599,14 +609,35 @@ int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) return 0; mmap_write_lock(mm); - ret = __account_locked_vm(mm, pages, inc, current, - capable(CAP_IPC_LOCK)); + ret = __account_locked_vm(mm, pages, current, capable(CAP_IPC_LOCK)); mmap_write_unlock(mm); return ret; } EXPORT_SYMBOL_GPL(account_locked_vm); +/** + * unaccount_locked_vm - account locked pages to an mm's locked_vm + * @mm: mm to account against, may be NULL + * @pages: number of pages to account + * + * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). + * + * Return: + * * 0 on success, or if mm is NULL + * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. + */ +void unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) +{ + if (pages == 0 || !mm) + return; + + mmap_write_lock(mm); + __unaccount_locked_vm(mm, pages); + mmap_write_unlock(mm); +} +EXPORT_SYMBOL_GPL(unaccount_locked_vm); + unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long pgoff) -- git-series 0.9.1
WARNING: multiple messages have this Message-ID (diff)
From: Alistair Popple <apopple-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org> To: linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org, cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, jgg-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org, jhubbard-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org, tjmercier-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org, surenb-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, mkoutny-IBi9RG/b67k@public.gmane.org, daniel-/w4YWyX8dFk@public.gmane.org, Alistair Popple <apopple-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org> Subject: [RFC PATCH 16/19] mm/util: Refactor account_locked_vm Date: Tue, 24 Jan 2023 16:42:45 +1100 [thread overview] Message-ID: <e72f46bd6be4f3e3c85ba6a560201cf23bdb39a9.1674538665.git-series.apopple@nvidia.com> (raw) In-Reply-To: <cover.f52b9eb2792bccb8a9ecd6bc95055705cfe2ae03.1674538665.git-series.apopple-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org> account_locked_vm() takes a flag to indicate if pages are being accounted or unaccounted for. A flag is also provided to bypass rlimits. However unaccounting of pages always succeeds and the flag to ignore the limits is ignored. The flags make calling code harder to understand so refactor the accounting and unaccounting paths into separate functions. Signed-off-by: Alistair Popple <apopple-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org> Cc: linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org --- include/linux/mm.h | 5 +-- mm/util.c | 73 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8f85716..126b756 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2090,9 +2090,10 @@ int get_user_pages_fast(unsigned long start, int nr_pages, int pin_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); -int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc); -int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, +int account_locked_vm(struct mm_struct *mm, unsigned long pages); +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim); +void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages); struct kvec; int get_kernel_pages(const struct kvec *iov, int nr_pages, int write, diff --git a/mm/util.c b/mm/util.c index e5fb01a..78b060d 100644 --- a/mm/util.c +++ b/mm/util.c @@ -537,7 +537,6 @@ EXPORT_SYMBOL_GPL(vm_unaccount_pinned); * __account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against * @pages: number of pages to account - * @inc: %true if @pages should be considered positive, %false if not * @task: task used to check RLIMIT_MEMLOCK * @bypass_rlim: %true if checking RLIMIT_MEMLOCK should be skipped * @@ -548,7 +547,7 @@ EXPORT_SYMBOL_GPL(vm_unaccount_pinned); * * 0 on success * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. */ -int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim) { unsigned long locked_vm, limit; @@ -557,33 +556,44 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, mmap_assert_write_locked(mm); locked_vm = mm->locked_vm; - if (inc) { - if (!bypass_rlim) { - limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; - if (locked_vm + pages > limit) - ret = -ENOMEM; - } - if (!ret) - mm->locked_vm = locked_vm + pages; - } else { - WARN_ON_ONCE(pages > locked_vm); - mm->locked_vm = locked_vm - pages; + if (!bypass_rlim) { + limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; + if (locked_vm + pages > limit) + ret = -ENOMEM; } - pr_debug("%s: [%d] caller %ps %c%lu %lu/%lu%s\n", __func__, task->pid, - (void *)_RET_IP_, (inc) ? '+' : '-', pages << PAGE_SHIFT, - locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK), - ret ? " - exceeded" : ""); + if (!ret) + mm->locked_vm = locked_vm + pages; + + pr_debug("%s: [%d] caller %ps %lu %lu/%lu%s\n", __func__, task->pid, + (void *)_RET_IP_, pages << PAGE_SHIFT, locked_vm << PAGE_SHIFT, + task_rlimit(task, RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); return ret; } EXPORT_SYMBOL_GPL(__account_locked_vm); /** + * __unaccount_locked_vm - unaccount locked pages to an mm's locked_vm + * @mm: mm to account against + * @pages: number of pages to account + * + * Assumes @mm are valid and that mmap_lock is held as writer. + */ +void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) +{ + unsigned long locked_vm = mm->locked_vm; + + mmap_assert_write_locked(mm); + WARN_ON_ONCE(pages > locked_vm); + mm->locked_vm = locked_vm - pages; +} +EXPORT_SYMBOL_GPL(__unaccount_locked_vm); + +/** * account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against, may be NULL * @pages: number of pages to account - * @inc: %true if @pages should be considered positive, %false if not * * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). * @@ -591,7 +601,7 @@ EXPORT_SYMBOL_GPL(__account_locked_vm); * * 0 on success, or if mm is NULL * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. */ -int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) +int account_locked_vm(struct mm_struct *mm, unsigned long pages) { int ret; @@ -599,14 +609,35 @@ int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) return 0; mmap_write_lock(mm); - ret = __account_locked_vm(mm, pages, inc, current, - capable(CAP_IPC_LOCK)); + ret = __account_locked_vm(mm, pages, current, capable(CAP_IPC_LOCK)); mmap_write_unlock(mm); return ret; } EXPORT_SYMBOL_GPL(account_locked_vm); +/** + * unaccount_locked_vm - account locked pages to an mm's locked_vm + * @mm: mm to account against, may be NULL + * @pages: number of pages to account + * + * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). + * + * Return: + * * 0 on success, or if mm is NULL + * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. + */ +void unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) +{ + if (pages == 0 || !mm) + return; + + mmap_write_lock(mm); + __unaccount_locked_vm(mm, pages); + mmap_write_unlock(mm); +} +EXPORT_SYMBOL_GPL(unaccount_locked_vm); + unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long pgoff) -- git-series 0.9.1
next prev parent reply other threads:[~2023-01-24 5:50 UTC|newest] Thread overview: 108+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-01-24 5:42 [RFC PATCH 00/19] mm: Introduce a cgroup to limit the amount of locked and pinned memory Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 01/19] mm: Introduce vm_account Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 6:29 ` Christoph Hellwig 2023-01-24 6:29 ` Christoph Hellwig 2023-01-24 6:29 ` Christoph Hellwig 2023-01-24 14:32 ` Jason Gunthorpe 2023-01-24 14:32 ` Jason Gunthorpe 2023-01-30 11:36 ` Alistair Popple 2023-01-30 11:36 ` Alistair Popple 2023-01-31 14:00 ` David Hildenbrand 2023-01-31 14:00 ` David Hildenbrand 2023-01-31 14:00 ` David Hildenbrand 2023-01-24 5:42 ` [RFC PATCH 02/19] drivers/vhost: Convert to use vm_account Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:55 ` Michael S. Tsirkin 2023-01-24 5:55 ` Michael S. Tsirkin 2023-01-24 5:55 ` Michael S. Tsirkin 2023-01-30 10:43 ` Alistair Popple 2023-01-30 10:43 ` Alistair Popple 2023-01-24 14:34 ` Jason Gunthorpe 2023-01-24 5:42 ` [RFC PATCH 03/19] drivers/vdpa: Convert vdpa to use the new vm_structure Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 14:35 ` Jason Gunthorpe 2023-01-24 14:35 ` Jason Gunthorpe 2023-01-24 5:42 ` [RFC PATCH 04/19] infiniband/umem: Convert to use vm_account Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 05/19] RMDA/siw: " Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 14:37 ` Jason Gunthorpe 2023-01-24 15:22 ` Bernard Metzler 2023-01-24 15:22 ` Bernard Metzler 2023-01-24 15:56 ` Bernard Metzler 2023-01-24 15:56 ` Bernard Metzler 2023-01-30 11:34 ` Alistair Popple 2023-01-30 11:34 ` Alistair Popple 2023-01-30 13:27 ` Bernard Metzler 2023-01-24 5:42 ` [RFC PATCH 06/19] RDMA/usnic: convert " Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 14:41 ` Jason Gunthorpe 2023-01-24 14:41 ` Jason Gunthorpe 2023-01-30 11:10 ` Alistair Popple 2023-01-30 11:10 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 07/19] vfio/type1: Charge pinned pages to pinned_vm instead of locked_vm Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 08/19] vfio/spapr_tce: Convert accounting to pinned_vm Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 09/19] io_uring: convert to use vm_account Alistair Popple 2023-01-24 14:44 ` Jason Gunthorpe 2023-01-30 11:12 ` Alistair Popple 2023-01-30 11:12 ` Alistair Popple 2023-01-30 13:21 ` Jason Gunthorpe 2023-01-24 5:42 ` [RFC PATCH 10/19] net: skb: Switch to using vm_account Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 14:51 ` Jason Gunthorpe 2023-01-24 14:51 ` Jason Gunthorpe 2023-01-30 11:17 ` Alistair Popple 2023-02-06 4:36 ` Alistair Popple 2023-02-06 4:36 ` Alistair Popple 2023-02-06 13:14 ` Jason Gunthorpe 2023-02-06 13:14 ` Jason Gunthorpe 2023-01-24 5:42 ` [RFC PATCH 11/19] xdp: convert to use vm_account Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 12/19] kvm/book3s_64_vio: Convert account_locked_vm() to vm_account_pinned() Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 13/19] fpga: dfl: afu: convert to use vm_account Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 14/19] mm: Introduce a cgroup for pinned memory Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 8:20 ` kernel test robot 2023-01-24 15:00 ` kernel test robot 2023-01-24 15:41 ` kernel test robot 2023-01-27 21:44 ` Tejun Heo 2023-01-27 21:44 ` Tejun Heo 2023-01-30 13:20 ` Jason Gunthorpe 2023-01-30 13:20 ` Jason Gunthorpe 2023-01-24 5:42 ` [RFC PATCH 15/19] mm/util: Extend vm_account to charge pages against the pin cgroup Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` Alistair Popple [this message] 2023-01-24 5:42 ` [RFC PATCH 16/19] mm/util: Refactor account_locked_vm Alistair Popple 2023-01-24 9:52 ` kernel test robot 2023-01-24 5:42 ` [RFC PATCH 17/19] mm: Convert mmap and mlock to use account_locked_vm Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 18/19] mm/mmap: Charge locked memory to pins cgroup Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 5:42 ` [RFC PATCH 19/19] selftests/vm: Add pins-cgroup selftest for mlock/mmap Alistair Popple 2023-01-24 5:42 ` Alistair Popple 2023-01-24 18:26 ` [RFC PATCH 00/19] mm: Introduce a cgroup to limit the amount of locked and pinned memory Yosry Ahmed 2023-01-24 18:26 ` Yosry Ahmed 2023-01-31 0:54 ` Alistair Popple 2023-01-31 0:54 ` Alistair Popple 2023-01-31 5:14 ` Yosry Ahmed 2023-01-31 5:14 ` Yosry Ahmed 2023-01-31 11:22 ` Alistair Popple 2023-01-31 11:22 ` Alistair Popple 2023-01-31 19:49 ` Yosry Ahmed 2023-01-31 19:49 ` Yosry Ahmed 2023-01-24 20:12 ` Jason Gunthorpe 2023-01-24 20:12 ` Jason Gunthorpe 2023-01-31 13:57 ` David Hildenbrand 2023-01-31 14:03 ` Jason Gunthorpe 2023-01-31 14:03 ` Jason Gunthorpe 2023-01-31 14:06 ` David Hildenbrand 2023-01-31 14:10 ` Jason Gunthorpe 2023-01-31 14:10 ` Jason Gunthorpe 2023-01-31 14:15 ` David Hildenbrand 2023-01-31 14:15 ` David Hildenbrand 2023-01-31 14:21 ` Jason Gunthorpe
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=e72f46bd6be4f3e3c85ba6a560201cf23bdb39a9.1674538665.git-series.apopple@nvidia.com \ --to=apopple@nvidia.com \ --cc=cgroups@vger.kernel.org \ --cc=daniel@ffwll.ch \ --cc=hannes@cmpxchg.org \ --cc=jgg@nvidia.com \ --cc=jhubbard@nvidia.com \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-mm@kvack.org \ --cc=mkoutny@suse.com \ --cc=surenb@google.com \ --cc=tjmercier@google.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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.