diff mbox series

[v6,11/15] mm/madvise: add MADV_COLLAPSE to process_madvise()

Message ID 20220604004004.954674-12-zokeefe@google.com (mailing list archive)
State New
Headers show
Series mm: userspace hugepage collapse | expand

Commit Message

Zach O'Keefe June 4, 2022, 12:40 a.m. UTC
Allow MADV_COLLAPSE behavior for process_madvise(2) if caller has
CAP_SYS_ADMIN or is requesting collapse of it's own memory.

This is useful for the development of userspace agents that seek to
optimize THP utilization system-wide by using userspace signals to
prioritize what memory is most deserving of being THP-backed.

Signed-off-by: Zach O'Keefe <zokeefe@google.com>
---
 mm/madvise.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Yang Shi June 7, 2022, 7:14 p.m. UTC | #1
On Fri, Jun 3, 2022 at 5:40 PM Zach O'Keefe <zokeefe@google.com> wrote:
>
> Allow MADV_COLLAPSE behavior for process_madvise(2) if caller has
> CAP_SYS_ADMIN or is requesting collapse of it's own memory.

It is fine to me. But I'd like to hear more from other folks.

>
> This is useful for the development of userspace agents that seek to
> optimize THP utilization system-wide by using userspace signals to
> prioritize what memory is most deserving of being THP-backed.
>
> Signed-off-by: Zach O'Keefe <zokeefe@google.com>
> ---
>  mm/madvise.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/mm/madvise.c b/mm/madvise.c
> index eccac2620226..b19e2f4b924c 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -1171,13 +1171,15 @@ madvise_behavior_valid(int behavior)
>  }
>
>  static bool
> -process_madvise_behavior_valid(int behavior)
> +process_madvise_behavior_valid(int behavior, struct task_struct *task)
>  {
>         switch (behavior) {
>         case MADV_COLD:
>         case MADV_PAGEOUT:
>         case MADV_WILLNEED:
>                 return true;
> +       case MADV_COLLAPSE:
> +               return task == current || capable(CAP_SYS_ADMIN);
>         default:
>                 return false;
>         }
> @@ -1455,7 +1457,7 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
>                 goto free_iov;
>         }
>
> -       if (!process_madvise_behavior_valid(behavior)) {
> +       if (!process_madvise_behavior_valid(behavior, task)) {
>                 ret = -EINVAL;
>                 goto release_task;
>         }
> --
> 2.36.1.255.ge46751e96f-goog
>
diff mbox series

Patch

diff --git a/mm/madvise.c b/mm/madvise.c
index eccac2620226..b19e2f4b924c 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -1171,13 +1171,15 @@  madvise_behavior_valid(int behavior)
 }
 
 static bool
-process_madvise_behavior_valid(int behavior)
+process_madvise_behavior_valid(int behavior, struct task_struct *task)
 {
 	switch (behavior) {
 	case MADV_COLD:
 	case MADV_PAGEOUT:
 	case MADV_WILLNEED:
 		return true;
+	case MADV_COLLAPSE:
+		return task == current || capable(CAP_SYS_ADMIN);
 	default:
 		return false;
 	}
@@ -1455,7 +1457,7 @@  SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
 		goto free_iov;
 	}
 
-	if (!process_madvise_behavior_valid(behavior)) {
+	if (!process_madvise_behavior_valid(behavior, task)) {
 		ret = -EINVAL;
 		goto release_task;
 	}