thread_info: use helpers to snapshot thread flags
Subsystems (11)
Mark Rutland
reviewerv8 cover thread_info: use helpers to snapshot thread flags
This is a trivial fixup and resend of v7 due to a typo breaking the build on powerpc, as spotted by the kernel test robot:
https://lore.kernel.org/r/[email protected]
I've fixed that, and I've double-checked the series, build testing a few configs for all architectures for chieck the kernel.org crosstool page provides a GCC 10.3.0 binary. That hit some (unrelated) latent issues, but there are no new failrues introduced by this series.
Thomas, I'm hoping you'd be happy to pick this again.
As thread_info::flags scan be manipulated by remote threads, it is necessary to use atomics or READ_ONCE() to ensure that code manipulates a consistent snapshot, but we open-code plain accesses to thread_info::flags across the kernel tree.
Generally we get away with this, but tools like KCSAN legitimately warn that there is a data-race, and this is potentially fragile with compiler optimizations, LTO, etc.
These patches introduce new helpers to snapshot the thread flags, with the intent being that these should replace all plain accesses.
Since v1 [1]:
* Drop RFC
* Make read_ti_thread_flags() __always_inline
* Clarify commit messages
* Fix typo in arm64 patch
* Accumulate Reviewed-by / Acked-by tags
* Drop powerpc patch to avoid potential conflicts (per [2])
Since v2 [3]:
* Rebase to v5.14-rc1
* Reinstate powerpc patch
Since v3 [4]:
* Rebase to v5.14-rc4
Since v4 [5]:
* Rebase to v5.15-rc1
* Apply Acked-by / Tested-by tags
Since v5 [6]:
* Fix trivial whitespace bug in x86 patch
Since v6 [7]:
* Rebase to v5.16-rc1
* Fix new issue on PPC where thread flags could be discarded
Since v7 [8]:
* Add missing & to use of set_bits()
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lore.kernel.org/r/[email protected]
[5] https://lore.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/[email protected]
[7] https://lore.kernel.org/lkml/[email protected]
[8] https://lore.kernel.org/lkml/[email protected]/
Thanks,
Mark.
Mark Rutland (11):
thread_info: add helpers to snapshot thread flags
entry: snapshot thread flags
sched: snapshot thread flags
alpha: snapshot thread flags
arm: snapshot thread flags
arm64: snapshot thread flags
microblaze: snapshot thread flags
openrisc: snapshot thread flags
powerpc: avoid discarding flags in system_call_exception()
powerpc: snapshot thread flags
x86: snapshot thread flags
arch/alpha/kernel/signal.c | 2 +- arch/arm/kernel/signal.c | 2 +- arch/arm/mm/alignment.c | 2 +- arch/arm64/kernel/entry-common.c | 2 +- arch/arm64/kernel/ptrace.c | 4 ++-- arch/arm64/kernel/signal.c | 2 +- arch/arm64/kernel/syscall.c | 4 ++-- arch/microblaze/kernel/signal.c | 2 +- arch/openrisc/kernel/signal.c | 2 +- arch/powerpc/kernel/interrupt.c | 15 +++++++-------- arch/powerpc/kernel/ptrace/ptrace.c | 3 +-- arch/x86/kernel/process.c | 8 ++++---- arch/x86/kernel/process.h | 4 ++-- arch/x86/mm/tlb.c | 2 +- include/linux/entry-kvm.h | 2 +- include/linux/thread_info.h | 14 ++++++++++++++ kernel/entry/common.c | 4 ++-- kernel/entry/kvm.c | 4 ++-- kernel/sched/core.c | 2 +- 19 files changed, 46 insertions(+), 34 deletions(-)
Signature · 1 line
2.30.2
Mark Rutland
reviewerPATCH v8 1/11 thread_info: add helpers to snapshot thread flags
In <linux/thread_info.h> there are helpers to manipulate individual thread flags, but where code wants to check several flags at once, it must open code reading current_thread_info()->flags and operating on a snapshot.
As some flags can be set remotely it's necessary to use READ_ONCE() to get a consistent snapshot even when IRQs are disabled, but some code forgets to do this. Generally this is unlike to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To make it easier to do the right thing, and to highlight that concurrent modification is possible, add new helpers to snapshot the flags, which should be used in preference to plain reads. Subsequent patches will move existing code to use the new helpers.
--- include/linux/thread_info.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/thread_info.h +14 -0
@@ -118,6 +118,15 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)return test_bit (flag, (unsigned long *)&ti->flags); } +/* +* This may be used in noinstr code, and needs to be __always_inline to prevent +* inadvertent instrumentation. +*/ +static __always_inline unsigned long read_ti_thread_flags (struct thread_info *ti) +{ +return READ_ONCE (ti->flags); +} +#define set_thread_flag (flag) \ set_ti_thread_flag(current_thread_info(), flag)#define clear_thread_flag (flag) \
@@ -130,6 +139,11 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag) test_and_clear_ti_thread_flag(current_thread_info(), flag)#define test_thread_flag (flag) \ test_ti_thread_flag(current_thread_info(), flag) +#define read_thread_flags () \ + read_ti_thread_flags(current_thread_info()) + +#define read_task_thread_flags (t) \ + read_ti_thread_flags(task_thread_info(t))#ifdef CONFIG_GENERIC_ENTRY #define set_syscall_work (fl) \
Signature · 1 line
2.30.2
[tip: core/entry] thread_info: Add helpers to snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 7ad639840acf2800b5f387c495795f995a67a329 Gitweb: https://git.kernel.org/tip/7ad639840acf2800b5f387c495795f995a67a329 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:43 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:43 +01:00
thread_info: Add helpers to snapshot thread flags
In <linux/thread_info.h> there are helpers to manipulate individual thread flags, but where code wants to check several flags at once, it must open code reading current_thread_info()->flags and operating on a snapshot.
As some flags can be set remotely it's necessary to use READ_ONCE() to get a consistent snapshot even when IRQs are disabled, but some code forgets to do this. Generally this is unlike to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To make it easier to do the right thing, and to highlight that concurrent modification is possible, add new helpers to snapshot the flags, which should be used in preference to plain reads. Subsequent patches will move existing code to use the new helpers.
--- include/linux/thread_info.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/thread_info.h +14 -0
@@ -118,6 +118,15 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)return test_bit (flag, (unsigned long *)&ti->flags); } +/* +* This may be used in noinstr code, and needs to be __always_inline to prevent +* inadvertent instrumentation. +*/ +static __always_inline unsigned long read_ti_thread_flags (struct thread_info *ti) +{ +return READ_ONCE (ti->flags); +} +#define set_thread_flag (flag) \ set_ti_thread_flag(current_thread_info(), flag)#define clear_thread_flag (flag) \
@@ -130,6 +139,11 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag) test_and_clear_ti_thread_flag(current_thread_info(), flag)#define test_thread_flag (flag) \ test_ti_thread_flag(current_thread_info(), flag) +#define read_thread_flags () \ + read_ti_thread_flags(current_thread_info()) + +#define read_task_thread_flags (t) \ + read_ti_thread_flags(task_thread_info(t))#ifdef CONFIG_GENERIC_ENTRY #define set_syscall_work (fl) \
Mark Rutland
reviewerPATCH v8 2/11 entry: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- include/linux/entry-kvm.h | 2 +- kernel/entry/common.c | 4 ++-- kernel/entry/kvm.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
include/linux/entry-kvm.h +1 -1
@@ -75,7 +75,7 @@ static inline void xfer_to_guest_mode_prepare(void)*/ static inline bool __xfer_to_guest_mode_work_pending (void ) { -unsigned long ti_work =READ_ONCE (current_thread_info ()->flags); +unsigned long ti_work =read_thread_flags ();return !!(ti_work &XFER_TO_GUEST_MODE_WORK ); }
kernel/entry/common.c +2 -2
@@ -187,7 +187,7 @@ static unsigned long exit_to_user_mode_loop(struct pt_regs *regs,/* Check if any of the above work has queued a deferred wakeup */ tick_nohz_user_enter_prepare (); - ti_work =READ_ONCE (current_thread_info ()->flags); + ti_work =read_thread_flags (); }/* Return the latest work state for arch_exit_to_user_mode() */
@@ -196,7 +196,7 @@ static unsigned long exit_to_user_mode_loop(struct pt_regs *regs,static void exit_to_user_mode_prepare (struct pt_regs *regs) { -unsigned long ti_work =READ_ONCE (current_thread_info ()->flags); +unsigned long ti_work =read_thread_flags ();lockdep_assert_irqs_disabled ();
kernel/entry/kvm.c +2 -2
@@ -26,7 +26,7 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work)if (ret)return ret; - ti_work =READ_ONCE (current_thread_info ()->flags); + ti_work =read_thread_flags (); }while (ti_work &XFER_TO_GUEST_MODE_WORK ||need_resched ());return 0 ; }
@@ -43,7 +43,7 @@ int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu)* disabled in the inner loop before going into guest mode. No need * to disable interrupts here. */ - ti_work =READ_ONCE (current_thread_info ()->flags); + ti_work =read_thread_flags ();if (!(ti_work &XFER_TO_GUEST_MODE_WORK ))return 0 ;
Signature · 1 line
2.30.2
[tip: core/entry] entry: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 6ce895128b3bff738fe8d9dd74747a03e319e466 Gitweb: https://git.kernel.org/tip/6ce895128b3bff738fe8d9dd74747a03e319e466 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:44 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:43 +01:00
entry: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- include/linux/entry-kvm.h | 2 +- kernel/entry/common.c | 4 ++-- kernel/entry/kvm.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
include/linux/entry-kvm.h +1 -1
@@ -75,7 +75,7 @@ static inline void xfer_to_guest_mode_prepare(void)*/ static inline bool __xfer_to_guest_mode_work_pending (void ) { -unsigned long ti_work =READ_ONCE (current_thread_info ()->flags); +unsigned long ti_work =read_thread_flags ();return !!(ti_work &XFER_TO_GUEST_MODE_WORK ); }
kernel/entry/common.c +2 -2
@@ -187,7 +187,7 @@ static unsigned long exit_to_user_mode_loop(struct pt_regs *regs,/* Check if any of the above work has queued a deferred wakeup */ tick_nohz_user_enter_prepare (); - ti_work =READ_ONCE (current_thread_info ()->flags); + ti_work =read_thread_flags (); }/* Return the latest work state for arch_exit_to_user_mode() */
@@ -196,7 +196,7 @@ static unsigned long exit_to_user_mode_loop(struct pt_regs *regs,static void exit_to_user_mode_prepare (struct pt_regs *regs) { -unsigned long ti_work =READ_ONCE (current_thread_info ()->flags); +unsigned long ti_work =read_thread_flags ();lockdep_assert_irqs_disabled ();
kernel/entry/kvm.c +2 -2
@@ -26,7 +26,7 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work)if (ret)return ret; - ti_work =READ_ONCE (current_thread_info ()->flags); + ti_work =read_thread_flags (); }while (ti_work &XFER_TO_GUEST_MODE_WORK ||need_resched ());return 0 ; }
@@ -43,7 +43,7 @@ int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu)* disabled in the inner loop before going into guest mode. No need * to disable interrupts here. */ - ti_work =READ_ONCE (current_thread_info ()->flags); + ti_work =read_thread_flags ();if (!(ti_work &XFER_TO_GUEST_MODE_WORK ))return 0 ;
Mark Rutland
reviewerPATCH v8 3/11 sched: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
The READ_ONCE(ti->flags) .. cmpxchg(ti->flags) loop in set_nr_if_polling() is left as-is for clarity.
--- kernel/sched/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +1 -1
@@ -8520,7 +8520,7 @@ void sched_show_task(struct task_struct *p)rcu_read_unlock ();pr_cont (" stack:%5lu pid:%5d ppid:%6d flags:0x%08lx\n" , free,task_pid_nr (p), ppid, - (unsigned long )task_thread_info (p)->flags); +read_task_thread_flags (p));print_worker_info (KERN_INFO , p);print_stop_info (KERN_INFO , p);
Signature · 1 line
2.30.2
[tip: core/entry] sched: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 0569b245132c40015281610353935a50e282eb94 Gitweb: https://git.kernel.org/tip/0569b245132c40015281610353935a50e282eb94 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:45 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:43 +01:00
sched: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
The READ_ONCE(ti->flags) .. cmpxchg(ti->flags) loop in set_nr_if_polling() is left as-is for clarity.
--- kernel/sched/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +1 -1
@@ -8520,7 +8520,7 @@ void sched_show_task(struct task_struct *p)rcu_read_unlock ();pr_cont (" stack:%5lu pid:%5d ppid:%6d flags:0x%08lx\n" , free,task_pid_nr (p), ppid, - (unsigned long )task_thread_info (p)->flags); +read_task_thread_flags (p));print_worker_info (KERN_INFO , p);print_stop_info (KERN_INFO , p);
Mark Rutland
reviewerPATCH v8 4/11 alpha: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/alpha/kernel/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/alpha/kernel/signal.c +1 -1
@@ -535,6 +535,6 @@ do_work_pending(struct pt_regs *regs, unsigned long thread_flags, } }local_irq_disable (); - thread_flags =current_thread_info ()->flags; + thread_flags =read_thread_flags (); }while (thread_flags & _TIF_WORK_MASK); }
Signature · 1 line
2.30.2
[tip: core/entry] alpha: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 7fb2b24bb5c5876a3205cb5b9a580f81e8c9f744 Gitweb: https://git.kernel.org/tip/7fb2b24bb5c5876a3205cb5b9a580f81e8c9f744 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:46 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:43 +01:00
alpha: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/alpha/kernel/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/alpha/kernel/signal.c +1 -1
@@ -535,6 +535,6 @@ do_work_pending(struct pt_regs *regs, unsigned long thread_flags, } }local_irq_disable (); - thread_flags =current_thread_info ()->flags; + thread_flags =read_thread_flags (); }while (thread_flags & _TIF_WORK_MASK); }
Mark Rutland
reviewerPATCH v8 5/11 arm: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/arm/kernel/signal.c | 2 +- arch/arm/mm/alignment.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm/kernel/signal.c +1 -1
@@ -631,7 +631,7 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall) } }local_irq_disable (); - thread_flags =current_thread_info ()->flags; + thread_flags =read_thread_flags (); }while (thread_flags & _TIF_WORK_MASK);return 0 ; }
arch/arm/mm/alignment.c +1 -1
@@ -990,7 +990,7 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)* there is no work pending for this thread. */ raw_local_irq_disable (); -if (!(current_thread_info ()->flags & _TIF_WORK_MASK)) +if (!(read_thread_flags () & _TIF_WORK_MASK))set_cr (cr_no_alignment); }
Signature · 1 line
2.30.2
[tip: core/entry] ARM: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 050e22bfc4f450d342dbb7eaea470858ad567bce Gitweb: https://git.kernel.org/tip/050e22bfc4f450d342dbb7eaea470858ad567bce Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:47 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:43 +01:00
ARM: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/arm/kernel/signal.c | 2 +- arch/arm/mm/alignment.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm/kernel/signal.c +1 -1
@@ -631,7 +631,7 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall) } }local_irq_disable (); - thread_flags =current_thread_info ()->flags; + thread_flags =read_thread_flags (); }while (thread_flags & _TIF_WORK_MASK);return 0 ; }
arch/arm/mm/alignment.c +1 -1
@@ -990,7 +990,7 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)* there is no work pending for this thread. */ raw_local_irq_disable (); -if (!(current_thread_info ()->flags & _TIF_WORK_MASK)) +if (!(read_thread_flags () & _TIF_WORK_MASK))set_cr (cr_no_alignment); }
Mark Rutland
reviewerPATCH v8 6/11 arm64: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/arm64/kernel/entry-common.c | 2 +- arch/arm64/kernel/ptrace.c | 4 ++-- arch/arm64/kernel/signal.c | 2 +- arch/arm64/kernel/syscall.c | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kernel/entry-common.c +1 -1
@@ -129,7 +129,7 @@ static __always_inline void prepare_exit_to_user_mode(struct pt_regs *regs)local_daif_mask (); - flags =READ_ONCE (current_thread_info ()->flags); + flags =read_thread_flags ();if (unlikely (flags & _TIF_WORK_MASK))do_notify_resume (regs, flags); }
arch/arm64/kernel/ptrace.c +2 -2
@@ -1839,7 +1839,7 @@ static void tracehook_report_syscall(struct pt_regs *regs,int syscall_trace_enter (struct pt_regs *regs) { -unsigned long flags =READ_ONCE (current_thread_info ()->flags); +unsigned long flags =read_thread_flags ();if (flags & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE)) {tracehook_report_syscall (regs,PTRACE_SYSCALL_ENTER );
@@ -1862,7 +1862,7 @@ int syscall_trace_enter(struct pt_regs *regs)void syscall_trace_exit (struct pt_regs *regs) { -unsigned long flags =READ_ONCE (current_thread_info ()->flags); +unsigned long flags =read_thread_flags ();audit_syscall_exit (regs);
arch/arm64/kernel/signal.c +1 -1
@@ -948,7 +948,7 @@ void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags) }local_daif_mask (); - thread_flags =READ_ONCE (current_thread_info ()->flags); + thread_flags =read_thread_flags (); }while (thread_flags & _TIF_WORK_MASK); }
arch/arm64/kernel/syscall.c +2 -2
@@ -81,7 +81,7 @@ void syscall_trace_exit(struct pt_regs *regs);static void el0_svc_common (struct pt_regs *regs,int scno,int sc_nr,const syscall_fn_t syscall_table[]) { -unsigned long flags =current_thread_info ()->flags; +unsigned long flags =read_thread_flags (); regs->orig_x0 = regs->regs[0 ]; regs->syscallno = scno;
@@ -148,7 +148,7 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,*/ if (!has_syscall_work (flags) && !IS_ENABLED (CONFIG_DEBUG_RSEQ )) {local_daif_mask (); - flags =current_thread_info ()->flags; + flags =read_thread_flags ();if (!has_syscall_work (flags) && !(flags & _TIF_SINGLESTEP))return ;local_daif_restore (DAIF_PROCCTX );
Signature · 1 line
2.30.2
[tip: core/entry] arm64: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 342b3808786518ced347f40b59bae68664e20007 Gitweb: https://git.kernel.org/tip/342b3808786518ced347f40b59bae68664e20007 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:48 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:44 +01:00
arm64: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/arm64/kernel/entry-common.c | 2 +- arch/arm64/kernel/ptrace.c | 4 ++-- arch/arm64/kernel/signal.c | 2 +- arch/arm64/kernel/syscall.c | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kernel/entry-common.c +1 -1
@@ -129,7 +129,7 @@ static __always_inline void prepare_exit_to_user_mode(struct pt_regs *regs)local_daif_mask (); - flags =READ_ONCE (current_thread_info ()->flags); + flags =read_thread_flags ();if (unlikely (flags & _TIF_WORK_MASK))do_notify_resume (regs, flags); }
arch/arm64/kernel/ptrace.c +2 -2
@@ -1839,7 +1839,7 @@ static void tracehook_report_syscall(struct pt_regs *regs,int syscall_trace_enter (struct pt_regs *regs) { -unsigned long flags =READ_ONCE (current_thread_info ()->flags); +unsigned long flags =read_thread_flags ();if (flags & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE)) {tracehook_report_syscall (regs,PTRACE_SYSCALL_ENTER );
@@ -1862,7 +1862,7 @@ int syscall_trace_enter(struct pt_regs *regs)void syscall_trace_exit (struct pt_regs *regs) { -unsigned long flags =READ_ONCE (current_thread_info ()->flags); +unsigned long flags =read_thread_flags ();audit_syscall_exit (regs);
arch/arm64/kernel/signal.c +1 -1
@@ -948,7 +948,7 @@ void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags) }local_daif_mask (); - thread_flags =READ_ONCE (current_thread_info ()->flags); + thread_flags =read_thread_flags (); }while (thread_flags & _TIF_WORK_MASK); }
arch/arm64/kernel/syscall.c +2 -2
@@ -81,7 +81,7 @@ void syscall_trace_exit(struct pt_regs *regs);static void el0_svc_common (struct pt_regs *regs,int scno,int sc_nr,const syscall_fn_t syscall_table[]) { -unsigned long flags =current_thread_info ()->flags; +unsigned long flags =read_thread_flags (); regs->orig_x0 = regs->regs[0 ]; regs->syscallno = scno;
@@ -148,7 +148,7 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,*/ if (!has_syscall_work (flags) && !IS_ENABLED (CONFIG_DEBUG_RSEQ )) {local_daif_mask (); - flags =current_thread_info ()->flags; + flags =read_thread_flags ();if (!has_syscall_work (flags) && !(flags & _TIF_SINGLESTEP))return ;local_daif_restore (DAIF_PROCCTX );
Mark Rutland
reviewerPATCH v8 7/11 microblaze: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/microblaze/kernel/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/microblaze/kernel/signal.c +1 -1
@@ -283,7 +283,7 @@ static void do_signal(struct pt_regs *regs, int in_syscall)#ifdef DEBUG_SIG pr_info ("do signal: %p %d\n" , regs, in_syscall);pr_info ("do signal2: %lx %lx %ld [%lx]\n" , regs->pc, regs->r1, - regs->r12,current_thread_info ()->flags); + regs->r12,read_thread_flags ());#endif if (get_signal (&ksig)) {
Signature · 1 line
2.30.2
[tip: core/entry] microblaze: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: e538c5849143a7f0aa97006cd45ce4c0c26d0744 Gitweb: https://git.kernel.org/tip/e538c5849143a7f0aa97006cd45ce4c0c26d0744 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:49 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:44 +01:00
microblaze: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/microblaze/kernel/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/microblaze/kernel/signal.c +1 -1
@@ -283,7 +283,7 @@ static void do_signal(struct pt_regs *regs, int in_syscall)#ifdef DEBUG_SIG pr_info ("do signal: %p %d\n" , regs, in_syscall);pr_info ("do signal2: %lx %lx %ld [%lx]\n" , regs->pc, regs->r1, - regs->r12,current_thread_info ()->flags); + regs->r12,read_thread_flags ());#endif if (get_signal (&ksig)) {
Mark Rutland
reviewerPATCH v8 8/11 openrisc: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/openrisc/kernel/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/openrisc/kernel/signal.c +1 -1
@@ -313,7 +313,7 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall) } }local_irq_disable (); - thread_flags =current_thread_info ()->flags; + thread_flags =read_thread_flags (); }while (thread_flags & _TIF_WORK_MASK);return 0 ; }
Signature · 1 line
2.30.2
[tip: core/entry] openrisc: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 4ea7ce0a79b9450b71b9a88f9f5adbfe2bc3f2e5 Gitweb: https://git.kernel.org/tip/4ea7ce0a79b9450b71b9a88f9f5adbfe2bc3f2e5 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:50 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:44 +01:00
openrisc: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/openrisc/kernel/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/openrisc/kernel/signal.c +1 -1
@@ -313,7 +313,7 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall) } }local_irq_disable (); - thread_flags =current_thread_info ()->flags; + thread_flags =read_thread_flags (); }while (thread_flags & _TIF_WORK_MASK);return 0 ; }
Mark Rutland
reviewerPATCH v8 9/11 powerpc: avoid discarding flags in system_call_exception()
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Thus, when setting flags we must use an atomic operation rather than a plain read-modify-write sequence, as a plain read-modify-write may discard flags which are concurrently set by a remote thread, e.g.
// task A // task B tmp = A->thread_info.flags; set_tsk_thread_flag(A, NEWFLAG_B); tmp |= NEWFLAG_A; A->thread_info.flags = tmp;
In arch/powerpc/kernel/interrupt.c's system_call_exception(), we set _TIF_RESTOREALL in the thread info flags with a read-modify-write, which may result in other flags being discarded.
Elsewhere in the file we use clear_bits() to atomically remove flag bits, so let's use set_bits() here for consistency with those.
I presume there may be reasons (e.g. instrumentation) that prevent the use of set_thread_flag() and clear_thread_flag() here, which would otherwise be preferable.
--- arch/powerpc/kernel/interrupt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/interrupt.c +1 -1
@@ -148,7 +148,7 @@ notrace long system_call_exception(long r3, long r4, long r5,*/ if (IS_ENABLED (CONFIG_PPC_TRANSACTIONAL_MEM ) &&unlikely (MSR_TM_TRANSACTIONAL (regs->msr))) -current_thread_info ()->flags |= _TIF_RESTOREALL; +set_bits (_TIF_RESTOREALL, ¤t_thread_info ()->flags);/* * If the system call was made with a transaction active, doom it and
Signature · 1 line
2.30.2
[tip: core/entry] powerpc: Avoid discarding flags in system_call_exception()
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 08b0af5b2affbe7419853e8dd1330e4b3fe27125 Gitweb: https://git.kernel.org/tip/08b0af5b2affbe7419853e8dd1330e4b3fe27125 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:51 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:44 +01:00
powerpc: Avoid discarding flags in system_call_exception()
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Thus, when setting flags we must use an atomic operation rather than a plain read-modify-write sequence, as a plain read-modify-write may discard flags which are concurrently set by a remote thread, e.g.
// task A // task B tmp = A->thread_info.flags; set_tsk_thread_flag(A, NEWFLAG_B); tmp |= NEWFLAG_A; A->thread_info.flags = tmp;
arch/powerpc/kernel/interrupt.c's system_call_exception() sets _TIF_RESTOREALL in the thread info flags with a read-modify-write, which may result in other flags being discarded.
Elsewhere in the file it uses clear_bits() to atomically remove flag bits, so use set_bits() here for consistency with those.
There may be reasons (e.g. instrumentation) that prevent the use of set_thread_flag() and clear_thread_flag() here, which would otherwise be preferable.
--- arch/powerpc/kernel/interrupt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/interrupt.c +1 -1
@@ -148,7 +148,7 @@ notrace long system_call_exception(long r3, long r4, long r5,*/ if (IS_ENABLED (CONFIG_PPC_TRANSACTIONAL_MEM ) &&unlikely (MSR_TM_TRANSACTIONAL (regs->msr))) -current_thread_info ()->flags |= _TIF_RESTOREALL; +set_bits (_TIF_RESTOREALL, ¤t_thread_info ()->flags);/* * If the system call was made with a transaction active, doom it and
Mark Rutland
reviewerPATCH v8 10/11 powerpc: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/powerpc/kernel/interrupt.c | 13 ++++++------- arch/powerpc/kernel/ptrace/ptrace.c | 3 +-- 2 files changed, 7 insertions(+), 9 deletions(-)
arch/powerpc/kernel/interrupt.c +6 -7
@@ -181,7 +181,7 @@ notrace long system_call_exception(long r3, long r4, long r5,local_irq_enable (); -if (unlikely (current_thread_info ()->flags & _TIF_SYSCALL_DOTRACE)) { +if (unlikely (read_thread_flags () & _TIF_SYSCALL_DOTRACE)) {if (unlikely (trap_is_unsupported_scv (regs))) {/* Unsupported scv vector */ _exception (SIGILL , regs,ILL_ILLOPC , regs->nip);
@@ -343,7 +343,7 @@ interrupt_exit_user_prepare_main(unsigned long ret, struct pt_regs *regs)unsigned long ti_flags; again: - ti_flags =READ_ONCE (current_thread_info ()->flags); + ti_flags =read_thread_flags ();while (unlikely (ti_flags & (_TIF_USER_WORK_MASK & ~_TIF_RESTORE_TM))) {local_irq_enable ();if (ti_flags & _TIF_NEED_RESCHED) {
@@ -359,7 +359,7 @@ interrupt_exit_user_prepare_main(unsigned long ret, struct pt_regs *regs)do_notify_resume (regs, ti_flags); }local_irq_disable (); - ti_flags =READ_ONCE (current_thread_info ()->flags); + ti_flags =read_thread_flags (); }if (IS_ENABLED (CONFIG_PPC_BOOK3S_64 ) &&IS_ENABLED (CONFIG_PPC_FPU )) {
@@ -437,7 +437,7 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3,/* Check whether the syscall is issued inside a restartable sequence */ rseq_syscall (regs); - ti_flags =current_thread_info ()->flags; + ti_flags =read_thread_flags ();if (unlikely (r3 >= (unsigned long )-MAX_ERRNO ) && is_not_scv) {if (likely (!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) {
@@ -532,8 +532,7 @@ notrace unsigned long interrupt_exit_kernel_prepare(struct pt_regs *regs)unsigned long flags;unsigned long ret =0 ;unsigned long kuap; -bool stack_store =current_thread_info ()->flags & - _TIF_EMULATE_STACK_STORE; +bool stack_store =read_thread_flags () & _TIF_EMULATE_STACK_STORE;if (regs_is_unrecoverable (regs))unrecoverable_exception (regs);
@@ -554,7 +553,7 @@ notrace unsigned long interrupt_exit_kernel_prepare(struct pt_regs *regs) again:if (IS_ENABLED (CONFIG_PREEMPT )) {/* Return to preemptible kernel context */ -if (unlikely (current_thread_info ()->flags & _TIF_NEED_RESCHED)) { +if (unlikely (read_thread_flags () & _TIF_NEED_RESCHED)) {if (preempt_count () ==0 )preempt_schedule_irq (); }
arch/powerpc/kernel/ptrace/ptrace.c +1 -2
@@ -260,8 +260,7 @@ long do_syscall_trace_enter(struct pt_regs *regs) {u32 flags; - flags =READ_ONCE (current_thread_info ()->flags) & - (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE); + flags =read_thread_flags () & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE);if (flags) {int rc =tracehook_report_syscall_entry (regs);
Signature · 1 line
2.30.2
[tip: core/entry] powerpc: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: 985faa78687de6e583cfd8b8094d87dcb80c33a6 Gitweb: https://git.kernel.org/tip/985faa78687de6e583cfd8b8094d87dcb80c33a6 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:52 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:44 +01:00
powerpc: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/powerpc/kernel/interrupt.c | 13 ++++++------- arch/powerpc/kernel/ptrace/ptrace.c | 3 +-- 2 files changed, 7 insertions(+), 9 deletions(-)
arch/powerpc/kernel/interrupt.c +6 -7
@@ -181,7 +181,7 @@ notrace long system_call_exception(long r3, long r4, long r5,local_irq_enable (); -if (unlikely (current_thread_info ()->flags & _TIF_SYSCALL_DOTRACE)) { +if (unlikely (read_thread_flags () & _TIF_SYSCALL_DOTRACE)) {if (unlikely (trap_is_unsupported_scv (regs))) {/* Unsupported scv vector */ _exception (SIGILL , regs,ILL_ILLOPC , regs->nip);
@@ -343,7 +343,7 @@ interrupt_exit_user_prepare_main(unsigned long ret, struct pt_regs *regs)unsigned long ti_flags; again: - ti_flags =READ_ONCE (current_thread_info ()->flags); + ti_flags =read_thread_flags ();while (unlikely (ti_flags & (_TIF_USER_WORK_MASK & ~_TIF_RESTORE_TM))) {local_irq_enable ();if (ti_flags & _TIF_NEED_RESCHED) {
@@ -359,7 +359,7 @@ again:do_notify_resume (regs, ti_flags); }local_irq_disable (); - ti_flags =READ_ONCE (current_thread_info ()->flags); + ti_flags =read_thread_flags (); }if (IS_ENABLED (CONFIG_PPC_BOOK3S_64 ) &&IS_ENABLED (CONFIG_PPC_FPU )) {
@@ -437,7 +437,7 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3,/* Check whether the syscall is issued inside a restartable sequence */ rseq_syscall (regs); - ti_flags =current_thread_info ()->flags; + ti_flags =read_thread_flags ();if (unlikely (r3 >= (unsigned long )-MAX_ERRNO ) && is_not_scv) {if (likely (!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) {
@@ -532,8 +532,7 @@ notrace unsigned long interrupt_exit_kernel_prepare(struct pt_regs *regs)unsigned long flags;unsigned long ret =0 ;unsigned long kuap; -bool stack_store =current_thread_info ()->flags & - _TIF_EMULATE_STACK_STORE; +bool stack_store =read_thread_flags () & _TIF_EMULATE_STACK_STORE;if (regs_is_unrecoverable (regs))unrecoverable_exception (regs);
@@ -554,7 +553,7 @@ notrace unsigned long interrupt_exit_kernel_prepare(struct pt_regs *regs) again:if (IS_ENABLED (CONFIG_PREEMPT )) {/* Return to preemptible kernel context */ -if (unlikely (current_thread_info ()->flags & _TIF_NEED_RESCHED)) { +if (unlikely (read_thread_flags () & _TIF_NEED_RESCHED)) {if (preempt_count () ==0 )preempt_schedule_irq (); }
arch/powerpc/kernel/ptrace/ptrace.c +1 -2
@@ -260,8 +260,7 @@ long do_syscall_trace_enter(struct pt_regs *regs) {u32 flags; - flags =READ_ONCE (current_thread_info ()->flags) & - (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE); + flags =read_thread_flags () & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE);if (flags) {int rc =tracehook_report_syscall_entry (regs);
Mark Rutland
reviewerPATCH v8 11/11 x86: snapshot thread flags
In reply to Mark RutlandSome thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/x86/kernel/process.c | 8 ++++---- arch/x86/kernel/process.h | 4 ++-- arch/x86/mm/tlb.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kernel/process.c +4 -4
@@ -365,7 +365,7 @@ void arch_setup_new_exec(void)clear_thread_flag (TIF_SSBD );task_clear_spec_ssb_disable (current);task_clear_spec_ssb_noexec (current); -speculation_ctrl_update (task_thread_info (current)->flags); +speculation_ctrl_update (read_thread_flags ()); } }
@@ -617,7 +617,7 @@ static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)clear_tsk_thread_flag (tsk,TIF_SPEC_IB ); }/* Return the updated threadinfo flags*/ -return task_thread_info (tsk)->flags; +return read_task_thread_flags (tsk); }void speculation_ctrl_update (unsigned long tif)
@@ -653,8 +653,8 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p) {unsigned long tifp, tifn; - tifn =READ_ONCE (task_thread_info (next_p)->flags); - tifp =READ_ONCE (task_thread_info (prev_p)->flags); + tifn =read_task_thread_flags (next_p); + tifp =read_task_thread_flags (prev_p);switch_to_bitmap (tifp);
arch/x86/kernel/process.h +2 -2
@@ -13,8 +13,8 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p);static inline void switch_to_extra (struct task_struct *prev,struct task_struct *next) { -unsigned long next_tif =task_thread_info (next)->flags; -unsigned long prev_tif =task_thread_info (prev)->flags; +unsigned long next_tif =read_task_thread_flags (next); +unsigned long prev_tif =read_task_thread_flags (prev);if (IS_ENABLED (CONFIG_SMP )) {/*
arch/x86/mm/tlb.c +1 -1
@@ -361,7 +361,7 @@ static void l1d_flush_evaluate(unsigned long prev_mm, unsigned long next_mm,static unsigned long mm_mangle_tif_spec_bits (struct task_struct *next) { -unsigned long next_tif =task_thread_info (next)->flags; +unsigned long next_tif =read_task_thread_flags (next);unsigned long spec_bits = (next_tif >>TIF_SPEC_IB ) &LAST_USER_MM_SPEC_MASK ;/*
Signature · 1 line
2.30.2
[tip: core/entry] x86: Snapshot thread flags
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: dca99fb643a2e9bc2e67a0f626b09d4f177f0f09 Gitweb: https://git.kernel.org/tip/dca99fb643a2e9bc2e67a0f626b09d4f177f0f09 Author: Mark Rutland <[email protected]> AuthorDate: Mon, 29 Nov 2021 13:06:53 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Dec 2021 00:06:43 +01:00
x86: Snapshot thread flags
Some thread flags can be set remotely, and so even when IRQs are disabled, the flags can change under our feet. Generally this is unlikely to cause a problem in practice, but it is somewhat unsound, and KCSAN will legitimately warn that there is a data race.
To avoid such issues, a snapshot of the flags has to be taken prior to using them. Some places already use READ_ONCE() for that, others do not.
Convert them all to the new flag accessor helpers.
--- arch/x86/kernel/process.c | 8 ++++---- arch/x86/kernel/process.h | 4 ++-- arch/x86/mm/tlb.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kernel/process.c +4 -4
@@ -365,7 +365,7 @@ void arch_setup_new_exec(void)clear_thread_flag (TIF_SSBD );task_clear_spec_ssb_disable (current);task_clear_spec_ssb_noexec (current); -speculation_ctrl_update (task_thread_info (current)->flags); +speculation_ctrl_update (read_thread_flags ()); } }
@@ -617,7 +617,7 @@ static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)clear_tsk_thread_flag (tsk,TIF_SPEC_IB ); }/* Return the updated threadinfo flags*/ -return task_thread_info (tsk)->flags; +return read_task_thread_flags (tsk); }void speculation_ctrl_update (unsigned long tif)
@@ -653,8 +653,8 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p) {unsigned long tifp, tifn; - tifn =READ_ONCE (task_thread_info (next_p)->flags); - tifp =READ_ONCE (task_thread_info (prev_p)->flags); + tifn =read_task_thread_flags (next_p); + tifp =read_task_thread_flags (prev_p);switch_to_bitmap (tifp);
arch/x86/kernel/process.h +2 -2
@@ -13,8 +13,8 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p);static inline void switch_to_extra (struct task_struct *prev,struct task_struct *next) { -unsigned long next_tif =task_thread_info (next)->flags; -unsigned long prev_tif =task_thread_info (prev)->flags; +unsigned long next_tif =read_task_thread_flags (next); +unsigned long prev_tif =read_task_thread_flags (prev);if (IS_ENABLED (CONFIG_SMP )) {/*
arch/x86/mm/tlb.c +1 -1
@@ -361,7 +361,7 @@ static void l1d_flush_evaluate(unsigned long prev_mm, unsigned long next_mm,static unsigned long mm_mangle_tif_spec_bits (struct task_struct *next) { -unsigned long next_tif =task_thread_info (next)->flags; +unsigned long next_tif =read_task_thread_flags (next);unsigned long spec_bits = (next_tif >>TIF_SPEC_IB ) &LAST_USER_MM_SPEC_MASK ;/*