2023-11-17 13:22:20

by Cruz Zhao

[permalink] [raw]
Subject: [PATCH] sched/core: put the cookie to uaddr when create cookie

For the control process, it's necessary to get the cookie of the
task when create a cookie with command PR_SCHED_CORE_CREATE. In
current design, we have to use command PR_SCHED_CORE_GET after we
create a cookie, with one more syscall.

To optimize this process, we allow user to pass a userspace address,
and we put the cookie to the uaddr when we create the cookie
successfully. If the uaddr is NULL, the logic is the same as before.

Note that put_user() will return -EFAULT if error, which is different
from any error return value of sched_core_share_pid().

Signed-off-by: Cruz Zhao <[email protected]>
---
kernel/sched/core_sched.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/core_sched.c b/kernel/sched/core_sched.c
index a57fd8f27498..96bd159844b7 100644
--- a/kernel/sched/core_sched.c
+++ b/kernel/sched/core_sched.c
@@ -142,7 +142,7 @@ int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type type,
BUILD_BUG_ON(PR_SCHED_CORE_SCOPE_PROCESS_GROUP != PIDTYPE_PGID);

if (type > PIDTYPE_PGID || cmd >= PR_SCHED_CORE_MAX || pid < 0 ||
- (cmd != PR_SCHED_CORE_GET && uaddr))
+ (cmd != PR_SCHED_CORE_GET && cmd != PR_SCHED_CORE_CREATE && uaddr))
return -EINVAL;

rcu_read_lock();
@@ -229,6 +229,10 @@ int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type type,
read_unlock(&tasklist_lock);

out:
+ if (cmd == PR_SCHED_CORE_CREATE && !err && uaddr) {
+ ptr_to_hashval((void *)cookie, &id);
+ err = put_user(id, (u64 __user *)uaddr);
+ }
sched_core_put_cookie(cookie);
put_task_struct(task);
return err;
--
2.39.3