Skip to contentSkip to conversation

Simplify setting thread flags to a particular value

PATCH In review Dave Martin 5 participants 10 messages May 11, 2018 – May 15, 2018

Subsystems (8)
Arm (32-bit) PowerPC MIPS Tracing +4 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Simplify setting thread flags to a particular value

There are a number of bits of code sprinkled around the kernel to set a thread flag if a certain condition is true, and clear it otherwise.

This series ports relevant bits of code to use the new update_thread_flag() helpers implemented in [2].

Note: This is a repost of a previous RFC [1]. Part of patch 1, and all of patch 4 of that series have been split out and reviewed separately ([2], [3] respectively). I currently expect them to merge via Marc's KVM tree.

The remainder of the patches from [1] are reposted here.

Patch 1 ports some core code that to use the update_thread_flag() helpers.

The remaining patches port relevant bits of arch code.

Build-tested on the affected architectures; some context switch stress testing done on arm64, which exercises a few call sites for the new helpers.

Comments welcome.

Cheers
---Dave

[1] [RFC PATCH 0/6] Simplify setting thread flags to a particular value
https://lkml.org/lkml/2018/4/19/225

[2] [PATCH v7 01/16] thread_info: Add update_thread_flag() helpers
http://lists.infradead.org/pipermail/linux-arm-kernel/2018-May/576596.html

[3] [PATCH v7 02/16] arm64: Use update{,_tsk}_thread_flag()
http://lists.infradead.org/pipermail/linux-arm-kernel/2018-May/576597.html

Dave Martin (5):
thread_info: Port core code to use update_thread_flag() helpers
ARM: Use update_thread_flag()
MIPS: Use update{,_tsk}_thread_flag()
powerpc: Use update_thread_flag()
sparc: Use update_thread_flag()

 arch/arm/kernel/elf.c           |  9 +++------
 arch/mips/kernel/elf.c          | 10 ++--------
 arch/mips/kernel/process.c      | 15 ++++++---------
 arch/mips/kernel/ptrace.c       |  5 +----
 arch/mips/kernel/syscall.c      | 10 ++--------
 arch/powerpc/include/asm/elf.h  | 10 ++--------
 arch/sparc/include/asm/elf_64.h |  5 +----
 include/trace/syscall.h         |  6 ++----
 kernel/ptrace.c                 | 13 +++++--------
 9 files changed, 24 insertions(+), 59 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/5 thread_info: Port core code to use update_thread_flag() helpers

This patch ports a couple of relevant bits of the core kernel to use the new update_thread_flag() helpers.

No functional change.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 include/trace/syscall.h |  6 ++----
 kernel/ptrace.c         | 13 +++++--------
 2 files changed, 7 insertions(+), 12 deletions(-)
include/trace/syscall.h +2 -4
index dc8ac27..dcc9bdf 100644
@@ -37,10 +37,8 @@ struct syscall_metadata {
 #if defined(CONFIG_TRACEPOINTS) && defined(CONFIG_HAVE_SYSCALL_TRACEPOINTS)
 static inline void syscall_tracepoint_update(struct task_struct *p)
 {
-	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
-		set_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT);
-	else
-		clear_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT);
+	update_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT,
+			       test_thread_flag(TIF_SYSCALL_TRACEPOINT));
 }
 #else
 static inline void syscall_tracepoint_update(struct task_struct *p)
kernel/ptrace.c +5 -8
index 21fec73..7a2bd8d 100644
@@ -785,16 +785,13 @@ static int ptrace_resume(struct task_struct *child, long request,
 	if (!valid_signal(data))
 		return -EIO;
 
-	if (request == PTRACE_SYSCALL)
-		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
-	else
-		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
+	update_tsk_thread_flag(child, TIF_SYSCALL_TRACE,
+			       request == PTRACE_SYSCALL);
 
 #ifdef TIF_SYSCALL_EMU
-	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
-		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
-	else
-		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
+	update_tsk_thread_flag(child, TIF_SYSCALL_EMU,
+			       request == PTRACE_SYSEMU ||
+			       request == PTRACE_SYSEMU_SINGLESTEP);
 #endif
 
 	if (is_singleblock(request)) {
Signature · 1 line
2.1.4
#4 lore raw

PATCH 2/5 ARM: Use update_thread_flag()

In reply to Dave Martin

This patch uses the new update_thread_flag() helper to simplify an if () set; else clear; construct.

No functional change.

---
 arch/arm/kernel/elf.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
arch/arm/kernel/elf.c +3 -6
index 1824229..aade393 100644
@@ -68,12 +68,9 @@ void elf_set_personality(const struct elf32_hdr *x)
 	 * binary is EABI or softfloat (and thus, guaranteed not to use
 	 * FPA instructions.)
 	 */
-	if (elf_hwcap & HWCAP_IWMMXT &&
-	    eflags & (EF_ARM_EABI_MASK | EF_ARM_SOFT_FLOAT)) {
-		set_thread_flag(TIF_USING_IWMMXT);
-	} else {
-		clear_thread_flag(TIF_USING_IWMMXT);
-	}
+	update_thread_flag(TIF_USING_IWMMXT,
+			   elf_hwcap & HWCAP_IWMMXT &&
+			   eflags & (EF_ARM_EABI_MASK | EF_ARM_SOFT_FLOAT));
 }
 EXPORT_SYMBOL(elf_set_personality);
 
Signature · 1 line
2.1.4
#5 lore raw

PATCH 3/5 MIPS: Use update{,_tsk}_thread_flag()

In reply to Dave Martin

This patch uses the new update_thread_flag() helpers to simplify a couple of if () set; else clear; constructs.

No functional change.

Cc Ralf Baechle <[email protected]>
Cc James Hogan <[email protected]>
---
 arch/mips/kernel/elf.c     | 10 ++--------
 arch/mips/kernel/process.c | 15 ++++++---------
 arch/mips/kernel/ptrace.c  |  5 +----
 arch/mips/kernel/syscall.c | 10 ++--------
 4 files changed, 11 insertions(+), 29 deletions(-)
arch/mips/kernel/elf.c +2 -8
index 731325a..2351509 100644
@@ -267,14 +267,8 @@ int arch_check_elf(void *_ehdr, bool has_interpreter, void *_interp_ehdr,
 
 static inline void set_thread_fp_mode(int hybrid, int regs32)
 {
-	if (hybrid)
-		set_thread_flag(TIF_HYBRID_FPREGS);
-	else
-		clear_thread_flag(TIF_HYBRID_FPREGS);
-	if (regs32)
-		set_thread_flag(TIF_32BIT_FPREGS);
-	else
-		clear_thread_flag(TIF_32BIT_FPREGS);
+	update_thread_flag(TIF_HYBRID_FPREGS, hybrid);
+	update_thread_flag(TIF_32BIT_FPREGS, regs32);
 }
 
 void mips_set_personality_fp(struct arch_elf_state *state)
arch/mips/kernel/process.c +6 -9
index b9e9bf6..4bdecb6 100644
@@ -763,18 +763,15 @@ int mips_set_process_fp_mode(struct task_struct *task, unsigned int value)
 	 */
 	for_each_thread(task, t) {
 		/* Update desired FP register width */
-		if (value & PR_FP_MODE_FR) {
-			clear_tsk_thread_flag(t, TIF_32BIT_FPREGS);
-		} else {
-			set_tsk_thread_flag(t, TIF_32BIT_FPREGS);
+		update_tsk_thread_flag(t, TIF_32BIT_REGS,
+				       !(value & PR_FP_MODE_FR));
+
+		if (!(value & PR_FP_MODE_FR))
 			clear_tsk_thread_flag(t, TIF_MSA_CTX_LIVE);
-		}
 
 		/* Update desired FP single layout */
-		if (value & PR_FP_MODE_FRE)
-			set_tsk_thread_flag(t, TIF_HYBRID_FPREGS);
-		else
-			clear_tsk_thread_flag(t, TIF_HYBRID_FPREGS);
+		update_tsk_thread_flag(t, TIF_HYBRID_FPREGS,
+				       value & PR_FP_MODE_FRE);
 	}
 
 	/* Allow threads to use FP again */
arch/mips/kernel/ptrace.c +1 -4
index 0b23b1a..b218812 100644
@@ -279,10 +279,7 @@ int ptrace_set_watch_regs(struct task_struct *child,
 		child->thread.watch.mips3264.watchhi[i] = ht[i];
 	}
 
-	if (watch_active)
-		set_tsk_thread_flag(child, TIF_LOAD_WATCH);
-	else
-		clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
+	update_tsk_thread_flag(child, TIF_LOAD_WATCH, watch_active);
 
 	return 0;
 }
arch/mips/kernel/syscall.c +2 -8
index 69c17b5..e9fa130 100644
@@ -209,14 +209,8 @@ SYSCALL_DEFINE3(sysmips, long, cmd, long, arg1, long, arg2)
 		if (arg1 & ~3)
 			return -EINVAL;
 
-		if (arg1 & 1)
-			set_thread_flag(TIF_FIXADE);
-		else
-			clear_thread_flag(TIF_FIXADE);
-		if (arg1 & 2)
-			set_thread_flag(TIF_LOGADE);
-		else
-			clear_thread_flag(TIF_LOGADE);
+		update_thread_flag(TIF_FIXADE, arg1 & 1);
+		update_thread_flag(TIF_LOGADE, arg1 & 2);
 
 		return 0;
 
Signature · 1 line
2.1.4
#6 lore raw

PATCH 4/5 powerpc: Use update_thread_flag()

In reply to Dave Martin

This patch uses the new update_thread_flag() helper to simplify a couple of if () set; else clear; constructs.

No functional change.

Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Michael Ellerman <[email protected]>
---
 arch/powerpc/include/asm/elf.h | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
arch/powerpc/include/asm/elf.h +2 -8
index 548d9a4..136c9b1 100644
@@ -88,14 +88,8 @@ typedef elf_vrregset_t elf_fpxregset_t;
 #ifdef __powerpc64__
 # define SET_PERSONALITY(ex)					\
 do {								\
-	if (((ex).e_flags & 0x3) == 2)				\
-		set_thread_flag(TIF_ELF2ABI);			\
-	else							\
-		clear_thread_flag(TIF_ELF2ABI);			\
-	if ((ex).e_ident[EI_CLASS] == ELFCLASS32)		\
-		set_thread_flag(TIF_32BIT);			\
-	else							\
-		clear_thread_flag(TIF_32BIT);			\
+	update_thread_flag(TIF_ELF2ABI, ((ex).e_flags & 0x3) == 2);	\
+	update_thread_flag(TIF_32BIT, (ex).e_ident[EI_CLASS] == ELFCLASS32); \
 	if (personality(current->personality) != PER_LINUX32)	\
 		set_personality(PER_LINUX |			\
 			(current->personality & (~PER_MASK)));	\
Signature · 1 line
2.1.4
#8 lore raw

PATCH 5/5 sparc: Use update_thread_flag()

In reply to Dave Martin

This patch uses the new update_thread_flag() helper to simplify an if () set; else clear; construct.

No functional change.

Cc "David S. Miller" <[email protected]>
---
 arch/sparc/include/asm/elf_64.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/sparc/include/asm/elf_64.h +1 -4
index 7e078bc..f1ef89c 100644
@@ -202,10 +202,7 @@ extern unsigned long sparc64_elf_hwcap;
 #define ELF_PLATFORM	(NULL)
 
 #define SET_PERSONALITY(ex)				\
-do {	if ((ex).e_ident[EI_CLASS] == ELFCLASS32)	\
-		set_thread_flag(TIF_32BIT);		\
-	else						\
-		clear_thread_flag(TIF_32BIT);		\
+do {	update_thread_flag(TIF_32BIT, (ex).e_ident[EI_CLASS] == ELFCLASS32); \
 	/* flush_thread will update pgd cache */	\
 	if (personality(current->personality) != PER_LINUX32)	\
 		set_personality(PER_LINUX |		\
Signature · 1 line
2.1.4
maintainer Acked-by on v1 1/5
#2 lore raw

Re: [PATCH REPOST 1/5] thread_info: Port core code to use update_thread_flag() helpers

In reply to Dave Martin
Quoted 20 lines · On Fri, 11 May 2018 16:05:00 +0100 Dave Martin <[email protected]> wrote:

This patch ports a couple of relevant bits of the core kernel to use the new update_thread_flag() helpers.

No functional change.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 include/trace/syscall.h |  6 ++----
 kernel/ptrace.c         | 13 +++++--------
 2 files changed, 7 insertions(+), 12 deletions(-)
include/trace/syscall.h +0 -0
index dc8ac27..dcc9bdf 100644

include/trace/syscall.h struct syscall_metadata { · line 42

Quoted 10 lines
include/trace/syscall.h +2 -4
@@ -37,10 +37,8 @@ struct syscall_metadata {
 #if defined(CONFIG_TRACEPOINTS) && defined(CONFIG_HAVE_SYSCALL_TRACEPOINTS)
 static inline void syscall_tracepoint_update(struct task_struct *p)
 {
-	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
-		set_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT);
-	else
-		clear_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT);
+	update_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT,
+			       test_thread_flag(TIF_SYSCALL_TRACEPOINT));
 }

-- Steve

Quoted 28 lines
include/trace/syscall.h +0 -0
 #else
 static inline void syscall_tracepoint_update(struct task_struct *p)
kernel/ptrace.c +5 -8
index 21fec73..7a2bd8d 100644
@@ -785,16 +785,13 @@ static int ptrace_resume(struct task_struct *child, long request,
 	if (!valid_signal(data))
 		return -EIO;
 
-	if (request == PTRACE_SYSCALL)
-		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
-	else
-		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
+	update_tsk_thread_flag(child, TIF_SYSCALL_TRACE,
+			       request == PTRACE_SYSCALL);
 
 #ifdef TIF_SYSCALL_EMU
-	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
-		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
-	else
-		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
+	update_tsk_thread_flag(child, TIF_SYSCALL_EMU,
+			       request == PTRACE_SYSEMU ||
+			       request == PTRACE_SYSEMU_SINGLESTEP);
 #endif
 
 	if (is_singleblock(request)) {
maintainer Acked-by on v1 1/5
#3 lore raw

Re: [PATCH REPOST 1/5] thread_info: Port core code to use update_thread_flag() helpers

In reply to Dave Martin
Quoted 11 lines · Dave Martin

This patch ports a couple of relevant bits of the core kernel to use the new update_thread_flag() helpers.

No functional change.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Oleg Nesterov <[email protected]>
Quoted 4 lines
---
 include/trace/syscall.h |  6 ++----
 kernel/ptrace.c         | 13 +++++--------
 2 files changed, 7 insertions(+), 12 deletions(-)
reviewer Acked-by on v1 4/5
#7 lore raw

Re: [PATCH REPOST 4/5] powerpc: Use update_thread_flag()

In reply to Dave Martin
Quoted 24 lines · Dave Martin

This patch uses the new update_thread_flag() helper to simplify a couple of if () set; else clear; constructs.

No functional change.

Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Michael Ellerman <[email protected]>
---
 arch/powerpc/include/asm/elf.h | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
arch/powerpc/include/asm/elf.h +0 -3
index 548d9a4..136c9b1 100644
@@ -88,14 +88,8 @@ typedef elf_vrregset_t elf_fpxregset_t;
 #ifdef __powerpc64__
 # define SET_PERSONALITY(ex)					\
 do {								\
-	if (((ex).e_flags & 0x3) == 2)				\
-		set_thread_flag(TIF_ELF2ABI);			\
-	else							\

arch/powerpc/include/asm/elf.h typedef elf_vrregset_t elf_fpxregset_t; · line 95

Quoted 10 lines
arch/powerpc/include/asm/elf.h +2 -5
-		clear_thread_flag(TIF_ELF2ABI);			\
-	if ((ex).e_ident[EI_CLASS] == ELFCLASS32)		\
-		set_thread_flag(TIF_32BIT);			\
-	else							\
-		clear_thread_flag(TIF_32BIT);			\
+	update_thread_flag(TIF_ELF2ABI, ((ex).e_flags & 0x3) == 2);	\
+	update_thread_flag(TIF_32BIT, (ex).e_ident[EI_CLASS] == ELFCLASS32); \
 	if (personality(current->personality) != PER_LINUX32)	\
 		set_personality(PER_LINUX |			\
 			(current->personality & (~PER_MASK)));	\

Thanks for cleaning it up.

cheers