Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * General MIPS MT support routines, usable in AP/SP and SMVP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2005 Mips Technologies, Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/cpuset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) cpumask_t mt_fpu_cpumask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int fpaff_threshold = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) unsigned long mt_fpemul_threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * Replacement functions for the sys_sched_setaffinity() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * sys_sched_getaffinity() system calls, so that we can integrate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * FPU affinity with the user's requested processor affinity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * This code is 98% identical with the sys_sched_setaffinity()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * and sys_sched_getaffinity() system calls, and should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * updated when kernel/sched/core.c changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * find_process_by_pid - find a process with a matching PID value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * cloned here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static inline struct task_struct *find_process_by_pid(pid_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	return pid ? find_task_by_vpid(pid) : current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * check the target process has a UID that matches the current process's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static bool check_same_owner(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	const struct cred *cred = current_cred(), *pcred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	bool match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	pcred = __task_cred(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	match = (uid_eq(cred->euid, pcred->euid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		 uid_eq(cred->euid, pcred->uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 				      unsigned long __user *user_mask_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	cpumask_var_t cpus_allowed, new_mask, effective_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct thread_info *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (len < sizeof(new_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	p = find_process_by_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	/* Prevent p going away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	get_task_struct(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		goto out_put_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		goto out_free_cpus_allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (!alloc_cpumask_var(&effective_mask, GFP_KERNEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		goto out_free_new_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (!check_same_owner(p) && !capable(CAP_SYS_NICE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		retval = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	retval = security_task_setscheduler(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* Record new user-specified CPU set for future reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	cpumask_copy(&p->thread.user_cpus_allowed, new_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/* Compute new global allowed CPU set if necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	ti = task_thread_info(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (test_ti_thread_flag(ti, TIF_FPUBOUND) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	    cpumask_intersects(new_mask, &mt_fpu_cpumask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		cpumask_and(effective_mask, new_mask, &mt_fpu_cpumask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		retval = set_cpus_allowed_ptr(p, effective_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		cpumask_copy(effective_mask, new_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		clear_ti_thread_flag(ti, TIF_FPUBOUND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		retval = set_cpus_allowed_ptr(p, new_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (!retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		cpuset_cpus_allowed(p, cpus_allowed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		if (!cpumask_subset(effective_mask, cpus_allowed)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			 * We must have raced with a concurrent cpuset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			 * update. Just reset the cpus_allowed to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			 * cpuset's cpus_allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			cpumask_copy(new_mask, cpus_allowed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	free_cpumask_var(effective_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) out_free_new_mask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	free_cpumask_var(new_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) out_free_cpus_allowed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	free_cpumask_var(cpus_allowed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) out_put_task:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	put_task_struct(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				      unsigned long __user *user_mask_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	unsigned int real_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	cpumask_t allowed, mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	real_len = sizeof(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (len < real_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	retval = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	p = find_process_by_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	retval = security_task_getscheduler(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	cpumask_or(&allowed, &p->thread.user_cpus_allowed, p->cpus_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	cpumask_and(&mask, &allowed, cpu_active_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (copy_to_user(user_mask_ptr, &mask, real_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return real_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int __init fpaff_thresh(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	get_option(&str, &fpaff_threshold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) __setup("fpaff=", fpaff_thresh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * FPU Use Factor empirically derived from experiments on 34K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) #define FPUSEFACTOR 2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static __init int mt_fp_affinity_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (fpaff_threshold >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		mt_fpemul_threshold = fpaff_threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		mt_fpemul_threshold =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			(FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	printk(KERN_DEBUG "FPU Affinity set after %ld emulations\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	       mt_fpemul_threshold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) arch_initcall(mt_fp_affinity_init);