From 473ddd592c55c4fcafc27b61ee17905377d336b1 Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@openwrt.org>
Date: Sat, 18 Jul 2015 22:49:39 +0000
Subject: [PATCH] kernel: fix sched_getaffinity on MIPS SMP systems (fixes
 enabling RPS/XPS in netifd with musl)

Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 46420
---
 ...sched_getaffinity-with-MT-FPAFF-enab.patch | 35 +++++++++++++++++++
 ...sched_getaffinity-with-MT-FPAFF-enab.patch | 35 +++++++++++++++++++
 ...sched_getaffinity-with-MT-FPAFF-enab.patch | 35 +++++++++++++++++++
 3 files changed, 105 insertions(+)
 create mode 100644 target/linux/generic/patches-3.18/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch
 create mode 100644 target/linux/generic/patches-4.0/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch
 create mode 100644 target/linux/generic/patches-4.1/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch

diff --git a/target/linux/generic/patches-3.18/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch b/target/linux/generic/patches-3.18/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch
new file mode 100644
index 0000000000..a7bfef347c
--- /dev/null
+++ b/target/linux/generic/patches-3.18/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch
@@ -0,0 +1,35 @@
+From: Felix Fietkau <nbd@openwrt.org>
+Date: Sun, 19 Jul 2015 00:21:57 +0200
+Subject: [PATCH] MIPS: kernel: fix sched_getaffinity with MT FPAFF enabled
+
+p->thread.user_cpus_allowed is zero-initialized and is only filled on
+the first sched_setaffinity call.
+
+To avoid adding overhead in the task initialization codepath, simply OR
+the returned mask in sched_getaffinity with p->cpus_allowed.
+
+Cc: stable@vger.kernel.org
+Signed-off-by: Felix Fietkau <nbd@openwrt.org>
+---
+
+--- a/arch/mips/kernel/mips-mt-fpaff.c
++++ b/arch/mips/kernel/mips-mt-fpaff.c
+@@ -154,7 +154,7 @@ asmlinkage long mipsmt_sys_sched_getaffi
+ 				      unsigned long __user *user_mask_ptr)
+ {
+ 	unsigned int real_len;
+-	cpumask_t mask;
++	cpumask_t allowed, mask;
+ 	int retval;
+ 	struct task_struct *p;
+ 
+@@ -173,7 +173,8 @@ asmlinkage long mipsmt_sys_sched_getaffi
+ 	if (retval)
+ 		goto out_unlock;
+ 
+-	cpumask_and(&mask, &p->thread.user_cpus_allowed, cpu_possible_mask);
++	cpumask_or(&allowed, &p->thread.user_cpus_allowed, &p->cpus_allowed);
++	cpumask_and(&mask, &allowed, cpu_active_mask);
+ 
+ out_unlock:
+ 	read_unlock(&tasklist_lock);
diff --git a/target/linux/generic/patches-4.0/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch b/target/linux/generic/patches-4.0/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch
new file mode 100644
index 0000000000..a7bfef347c
--- /dev/null
+++ b/target/linux/generic/patches-4.0/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch
@@ -0,0 +1,35 @@
+From: Felix Fietkau <nbd@openwrt.org>
+Date: Sun, 19 Jul 2015 00:21:57 +0200
+Subject: [PATCH] MIPS: kernel: fix sched_getaffinity with MT FPAFF enabled
+
+p->thread.user_cpus_allowed is zero-initialized and is only filled on
+the first sched_setaffinity call.
+
+To avoid adding overhead in the task initialization codepath, simply OR
+the returned mask in sched_getaffinity with p->cpus_allowed.
+
+Cc: stable@vger.kernel.org
+Signed-off-by: Felix Fietkau <nbd@openwrt.org>
+---
+
+--- a/arch/mips/kernel/mips-mt-fpaff.c
++++ b/arch/mips/kernel/mips-mt-fpaff.c
+@@ -154,7 +154,7 @@ asmlinkage long mipsmt_sys_sched_getaffi
+ 				      unsigned long __user *user_mask_ptr)
+ {
+ 	unsigned int real_len;
+-	cpumask_t mask;
++	cpumask_t allowed, mask;
+ 	int retval;
+ 	struct task_struct *p;
+ 
+@@ -173,7 +173,8 @@ asmlinkage long mipsmt_sys_sched_getaffi
+ 	if (retval)
+ 		goto out_unlock;
+ 
+-	cpumask_and(&mask, &p->thread.user_cpus_allowed, cpu_possible_mask);
++	cpumask_or(&allowed, &p->thread.user_cpus_allowed, &p->cpus_allowed);
++	cpumask_and(&mask, &allowed, cpu_active_mask);
+ 
+ out_unlock:
+ 	read_unlock(&tasklist_lock);
diff --git a/target/linux/generic/patches-4.1/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch b/target/linux/generic/patches-4.1/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch
new file mode 100644
index 0000000000..a7bfef347c
--- /dev/null
+++ b/target/linux/generic/patches-4.1/130-MIPS-kernel-fix-sched_getaffinity-with-MT-FPAFF-enab.patch
@@ -0,0 +1,35 @@
+From: Felix Fietkau <nbd@openwrt.org>
+Date: Sun, 19 Jul 2015 00:21:57 +0200
+Subject: [PATCH] MIPS: kernel: fix sched_getaffinity with MT FPAFF enabled
+
+p->thread.user_cpus_allowed is zero-initialized and is only filled on
+the first sched_setaffinity call.
+
+To avoid adding overhead in the task initialization codepath, simply OR
+the returned mask in sched_getaffinity with p->cpus_allowed.
+
+Cc: stable@vger.kernel.org
+Signed-off-by: Felix Fietkau <nbd@openwrt.org>
+---
+
+--- a/arch/mips/kernel/mips-mt-fpaff.c
++++ b/arch/mips/kernel/mips-mt-fpaff.c
+@@ -154,7 +154,7 @@ asmlinkage long mipsmt_sys_sched_getaffi
+ 				      unsigned long __user *user_mask_ptr)
+ {
+ 	unsigned int real_len;
+-	cpumask_t mask;
++	cpumask_t allowed, mask;
+ 	int retval;
+ 	struct task_struct *p;
+ 
+@@ -173,7 +173,8 @@ asmlinkage long mipsmt_sys_sched_getaffi
+ 	if (retval)
+ 		goto out_unlock;
+ 
+-	cpumask_and(&mask, &p->thread.user_cpus_allowed, cpu_possible_mask);
++	cpumask_or(&allowed, &p->thread.user_cpus_allowed, &p->cpus_allowed);
++	cpumask_and(&mask, &allowed, cpu_active_mask);
+ 
+ out_unlock:
+ 	read_unlock(&tasklist_lock);
-- 
2.30.2