From 081f6c94c37f4bb057132fa32927ca1983599011 Mon Sep 17 00:00:00 2001 From: Huzhaodong Date: Fri, 19 May 2023 15:54:51 +0800 Subject: [PATCH 1/2] sched: add qos ctrl and authority ctrl for FFRT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ohos inclusion category: feature issue: #I774U9 CVE:NA Signed-off-by: Huzhaodong --- drivers/Kconfig | 2 ++ drivers/Makefile | 1 + fs/proc/base.c | 22 ++++++++++++++++++++++ include/linux/sched.h | 17 +++++++++++++++++ kernel/exit.c | 9 +++++++++ kernel/sched/core.c | 13 +++++++++++++ kernel/sched/rtg/rtg_ctrl.c | 15 +++++++++++++++ 7 files changed, 79 insertions(+) diff --git a/drivers/Kconfig b/drivers/Kconfig index abf8938eb35f..216c52363bd2 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -244,4 +244,6 @@ source "drivers/hooks/Kconfig" source "drivers/hck/Kconfig" +source "drivers/auth_ctl/Kconfig" + endmenu diff --git a/drivers/Makefile b/drivers/Makefile index d9e92b5e31e1..d0ff4fdb0dfb 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -185,6 +185,7 @@ obj-$(CONFIG_NVMEM) += nvmem/ obj-$(CONFIG_FPGA) += fpga/ obj-$(CONFIG_FSI) += fsi/ obj-$(CONFIG_TEE) += tee/ +obj-$(CONFIG_AUTHORITY_CTRL) += auth_ctl/ obj-$(CONFIG_MULTIPLEXER) += mux/ obj-$(CONFIG_UNISYS_VISORBUS) += visorbus/ obj-$(CONFIG_SIOX) += siox/ diff --git a/fs/proc/base.c b/fs/proc/base.c index d101562cafce..08cd05212367 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -54,6 +54,11 @@ #include #include #include + +#ifdef CONFIG_QOS_CTRL +#include +#endif + #include #include #include @@ -1502,6 +1507,23 @@ static const struct file_operations proc_pid_sched_operations = { #endif +#ifdef CONFIG_QOS_CTRL +long proc_qos_ctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + return do_qos_ctrl_ioctl(file, cmd, arg); +} + +int proc_qos_ctrl_open(struct inode *inode, struct file *filp) +{ + return 0; +} + +static const struct file_operations proc_qos_ctrl_operations = { + .open = proc_qos_ctrl_open, + .unlocked_ioctl = proc_qos_ctrl_ioctl, +}; +#endif + #ifdef CONFIG_SCHED_RTG static const struct file_operations proc_rtg_operations = { .open = proc_rtg_open, diff --git a/include/linux/sched.h b/include/linux/sched.h index 8cccb207ecf1..d590141f04d1 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -747,6 +747,20 @@ struct wake_q_node { struct wake_q_node *next; }; +#ifdef CONFIG_QOS_CTRL +struct qos_task_struct { + /* + * 'in_qos' marks the qos level o current task, greater value for + * greater qos, range from (NO_QOS, NR_QOS) + * + * + * 'qos_list' use to track task with qos supply in auth_struct + */ + int in_qos; + struct list_head qos_list; +}; +#endif + struct task_struct { #ifdef CONFIG_THREAD_INFO_IN_TASK /* @@ -1493,6 +1507,9 @@ struct task_struct { #ifdef CONFIG_ACCESS_TOKENID u64 token; u64 ftoken; +#endif +#ifdef CONFIG_QOS_CTRL + struct qos_task_struct qts; #endif /* * New fields for task_struct should be added above here, so that diff --git a/kernel/exit.c b/kernel/exit.c index d1b2e9e25dfb..5bd90aca5f8e 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -13,6 +13,11 @@ #include #include #include + +#ifdef CONFIG_QOS_CTRL +#include +#endif + #include #include #include @@ -767,6 +772,10 @@ void __noreturn do_exit(long code) exit_signals(tsk); /* sets PF_EXITING */ sched_exit(tsk); +#ifdef CONFIG_QOS_CTRL + sched_exit_qos_list(tsk); +#endif + /* sync mm's RSS info before statistics gathering */ if (tsk->mm) sync_mm_rss(tsk->mm); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 52d70832e1be..e86758a8199d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -19,6 +19,10 @@ #include #include +#ifdef CONFIG_QOS_CTRL +#include +#endif + #include #include @@ -3385,6 +3389,11 @@ static inline void init_schedstats(void) {} int sched_fork(unsigned long clone_flags, struct task_struct *p) { init_new_task_load(p); + +#ifdef CONFIG_QOS_CTRL + init_task_qos(p); +#endif + __sched_fork(clone_flags, p); /* * We mark the process as NEW here. This guarantees that @@ -7803,6 +7812,10 @@ void __init sched_init(void) init_idle(current, smp_processor_id()); init_new_task_load(current); +#ifdef CONIG_QOS_CTRL + init_task_qos(current); +#endif + calc_load_update = jiffies + LOAD_FREQ; #ifdef CONFIG_SMP diff --git a/kernel/sched/rtg/rtg_ctrl.c b/kernel/sched/rtg/rtg_ctrl.c index d49e02899495..0931e93dc5fa 100644 --- a/kernel/sched/rtg/rtg_ctrl.c +++ b/kernel/sched/rtg/rtg_ctrl.c @@ -10,6 +10,11 @@ #include #include + +#ifdef CONFIG_AUTHORITY_CTRL +#include +#endif + #include #include #include @@ -882,6 +887,9 @@ static long do_proc_rtg_ioctl(int abi, struct file *file, unsigned int cmd, unsi { void __user *uarg = (void __user *)(uintptr_t)arg; unsigned int func_id = _IOC_NR(cmd); +#ifdef CONFIG_RTG_AUTHORITY + bool authorized = true; +#endif if (uarg == NULL) { pr_err("[SCHED_RTG] %s: invalid user uarg\n", __func__); @@ -905,6 +913,13 @@ static long do_proc_rtg_ioctl(int abi, struct file *file, unsigned int cmd, unsi return -INVALID_CMD; } +#ifdef CONFIG_RTG_AUTHORITY + authorized = check_authorized(func_id, RTG_AUTH_FLAG); + if (!authorized) { + pr_err("[SCHED_RTG] %s: uid not authorized.\n", __func__); + return -INVALID_CMD; + } +#endif if (g_func_array[func_id] != NULL) return (*g_func_array[func_id])(abi, uarg); -- Gitee From a60c6bd967bde45f27253f90610513900657726e Mon Sep 17 00:00:00 2001 From: Huzhaodong Date: Mon, 22 May 2023 09:44:49 +0800 Subject: [PATCH 2/2] sched: change frame RTG scale for FFRT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ohos inclusion category: feature issue: #I774U9 CVE:NA Signed-off-by: Huzhaodong --- fs/proc/base.c | 3 +++ kernel/sched/rtg/frame_rtg.c | 5 ++++- kernel/sched/rtg/frame_rtg.h | 6 +++++- kernel/sched/rtg/rtg_ctrl.h | 3 ++- scripts/kconfig/lexer.l | 1 + 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index 08cd05212367..5da2d8cc7155 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3816,6 +3816,9 @@ static const struct pid_entry tid_base_stuff[] = { #ifdef CONFIG_ACCESS_TOKENID ONE("tokenid", S_IRUSR, proc_token_operations), #endif +#ifdef CONFIG_QOS_CTRL + REG("sched_qos_ctrl", S_IRUGO|S_IWUGO, proc_qos_ctrl_operations), +#endif #ifdef CONFIG_SCHED_RTG_DEBUG REG("sched_group_id", S_IRUGO|S_IWUGO, proc_pid_sched_group_id_operations), #endif diff --git a/kernel/sched/rtg/frame_rtg.c b/kernel/sched/rtg/frame_rtg.c index 62bfe2d13cb2..79db645228c4 100644 --- a/kernel/sched/rtg/frame_rtg.c +++ b/kernel/sched/rtg/frame_rtg.c @@ -455,10 +455,13 @@ static int set_rtg_sched(struct task_struct *task, bool is_rtg, (prio > MAX_USER_RT_PRIO - 1))) return err; /* + * original logic deny the non-cfs task st rt. + * add !fair_policy(task->policy) if needed + * * if CONFIG_HW_FUTEX_PI is set, task->prio and task->sched_class * may be modified by rtmutex. So we use task->policy instead. */ - if (is_rtg && (!fair_policy(task->policy) || (task->flags & PF_EXITING))) + if (is_rtg && task->flags & PF_EXITING) return err; if (in_interrupt()) { diff --git a/kernel/sched/rtg/frame_rtg.h b/kernel/sched/rtg/frame_rtg.h index 584304d01258..01f23d27413a 100644 --- a/kernel/sched/rtg/frame_rtg.h +++ b/kernel/sched/rtg/frame_rtg.h @@ -47,7 +47,11 @@ #define FRAME_DEFAULT_MAX_PREV_UTIL SCHED_CAPACITY_SCALE #define DEFAULT_MAX_RT_THREAD 5 -#define RTG_MAX_RT_THREAD_NUM CONFIG_NR_CPUS +/* + * RTG_MAX_RT_THREAD_NUM should be CONFIG_NR_CPUS in previous version + * fit for FFRT here + */ +#define RTG_MAX_RT_THREAD_NUM 20 #define INVALID_PREFERRED_CLUSTER 10 enum rtg_type { diff --git a/kernel/sched/rtg/rtg_ctrl.h b/kernel/sched/rtg/rtg_ctrl.h index 6d405f589dab..e13ff51deb45 100644 --- a/kernel/sched/rtg/rtg_ctrl.h +++ b/kernel/sched/rtg/rtg_ctrl.h @@ -24,7 +24,8 @@ #define MAX_SUBPROCESS_NUM 8 #define RTG_ID_INVALID (-1) -#define DEFAULT_MAX_RT_FRAME 3 +/* fit for FFRT, original DEFAULT_MAX_RT_FRAME is 3 */ +#define DEFAULT_MAX_RT_FRAME 10 #define MAX_RT_THREAD (MAX_TID_NUM + 2) #define INIT_VALUE (-1) #define UPDATE_RTG_FRAME (1 << 0) diff --git a/scripts/kconfig/lexer.l b/scripts/kconfig/lexer.l index c7953b16e0af..2632461bec79 100644 --- a/scripts/kconfig/lexer.l +++ b/scripts/kconfig/lexer.l @@ -25,6 +25,7 @@ static const char *kconfig_white_list[] = { "vendor/Kconfig", "net/newip/Kconfig", "security/xpm/Kconfig", + "drivers/auth_ctl/Kconfig", }; static struct { -- Gitee