Skip to content
Snippets Groups Projects
  1. Oct 04, 2023
  2. Aug 18, 2023
  3. Jun 16, 2023
  4. Jun 10, 2023
  5. Mar 28, 2023
  6. Mar 12, 2023
  7. Feb 03, 2023
  8. Sep 26, 2022
  9. Jul 18, 2022
  10. Jun 17, 2022
    • Petr Mladek's avatar
      kthread: make it clear that kthread_create_on_node() might be terminated by any fatal signal · d25c83c6
      Petr Mladek authored
      The comments in kernel/kthread.c create a feeling that only SIGKILL is
      able to terminate the creation of kernel kthreads by
      kthread_create()/_on_node()/_on_cpu() APIs.
      
      In reality, wait_for_completion_killable() might be killed by any fatal
      signal that does not have a custom handler:
      
      	(!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
      	 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
      
      static inline void signal_wake_up(struct task_struct *t, bool resume)
      {
      	signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
      }
      
      static void complete_signal(int sig, struct task_struct *p, enum pid_type type)
      {
      [...]
      	/*
      	 * Found a killable thread.  If the signal will be fatal,
      	 * then start taking the whole group down immediately.
      	 */
      	if (sig_fatal(p, sig) ...) {
      		if (!sig_kernel_coredump(sig)) {
      		[...]
      			do {
      				task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
      				sigaddset(&t->pending.signal, SIGKILL);
      				signal_wake_up(t, 1);
      			} while_each_thread(p, t);
      			return;
      		}
      	}
      }
      
      Update the comments in kernel/kthread.c to make this more obvious.
      
      The motivation for this change was debugging why a module initialization
      failed.  The module was being loaded from initrd.  It "magically" failed
      when systemd was switching to the real root.  The clean up operations sent
      SIGTERM to various pending processed that were started from initrd.
      
      Link: https://lkml.kernel.org/r/20220315102444.2380-1-pmladek@suse.com
      
      
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Reviewed-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
      Cc: Kees Cook <keescook@chromium.org>
      Cc: Marco Elver <elver@google.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      d25c83c6
  11. May 02, 2022
  12. Feb 25, 2022
    • Arnd Bergmann's avatar
      uaccess: remove CONFIG_SET_FS · 967747bb
      Arnd Bergmann authored
      
      There are no remaining callers of set_fs(), so CONFIG_SET_FS
      can be removed globally, along with the thread_info field and
      any references to it.
      
      This turns access_ok() into a cheaper check against TASK_SIZE_MAX.
      
      As CONFIG_SET_FS is now gone, drop all remaining references to
      set_fs()/get_fs(), mm_segment_t, user_addr_max() and uaccess_kernel().
      
      Acked-by: Sam Ravnborg <sam@ravnborg.org> # for sparc32 changes
      Acked-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
      Tested-by: Sergey Matyukevich <sergey.matyukevich@synopsys.com> # for arc changes
      Acked-by: Stafford Horne <shorne@gmail.com> # [openrisc, asm-generic]
      Acked-by: default avatarDinh Nguyen <dinguyen@kernel.org>
      Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
      967747bb
  13. Feb 16, 2022
  14. Jan 20, 2022
    • Yafang Shao's avatar
      kthread: dynamically allocate memory to store kthread's full name · d6986ce2
      Yafang Shao authored
      When I was implementing a new per-cpu kthread cfs_migration, I found the
      comm of it "cfs_migration/%u" is truncated due to the limitation of
      TASK_COMM_LEN.  For example, the comm of the percpu thread on CPU10~19
      all have the same name "cfs_migration/1", which will confuse the user.
      This issue is not critical, because we can get the corresponding CPU
      from the task's Cpus_allowed.  But for kthreads corresponding to other
      hardware devices, it is not easy to get the detailed device info from
      task comm, for example,
      
          jbd2/nvme0n1p2-
          xfs-reclaim/sdf
      
      Currently there are so many truncated kthreads:
      
          rcu_tasks_kthre
          rcu_tasks_rude_
          rcu_tasks_trace
          poll_mpt3sas0_s
          ext4-rsv-conver
          xfs-reclaim/sd{a, b, c, ...}
          xfs-blockgc/sd{a, b, c, ...}
          xfs-inodegc/sd{a, b, c, ...}
          audit_send_repl
          ecryptfs-kthrea
          vfio-irqfd-clea
          jbd2/nvme0n1p2-
          ...
      
      We can shorten these names to work around this problem, but it may be
      not applied to all of the truncated kthreads.  Take 'jbd2/nvme0n1p2-'
      for example, it is a nice name, and it is not a good idea to shorten it.
      
      One possible way to fix this issue is extending the task comm size, but
      as task->comm is used in lots of places, that may cause some potential
      buffer overflows.  Another more conservative approach is introducing a
      new pointer to store kthread's full name if it is truncated, which won't
      introduce too much overhead as it is in the non-critical path.  Finally
      we make a dicision to use the second approach.  See also the discussions
      in this thread:
      https://lore.kernel.org/lkml/20211101060419.4682-1-laoar.shao@gmail.com/
      
      After this change, the full name of these truncated kthreads will be
      displayed via /proc/[pid]/comm:
      
          rcu_tasks_kthread
          rcu_tasks_rude_kthread
          rcu_tasks_trace_kthread
          poll_mpt3sas0_statu
          ext4-rsv-conversion
          xfs-reclaim/sdf1
          xfs-blockgc/sdf1
          xfs-inodegc/sdf1
          audit_send_reply
          ecryptfs-kthread
          vfio-irqfd-cleanup
          jbd2/nvme0n1p2-8
      
      Link: https://lkml.kernel.org/r/20211120112850.46047-1-laoar.shao@gmail.com
      
      
      Signed-off-by: default avatarYafang Shao <laoar.shao@gmail.com>
      Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Suggested-by: default avatarPetr Mladek <pmladek@suse.com>
      Suggested-by: default avatarSteven Rostedt <rostedt@goodmis.org>
      Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
      Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>
      Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
      Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
      Cc: Michal Miroslaw <mirq-linux@rere.qmqm.pl>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Steven Rostedt <rostedt@goodmis.org>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: Kees Cook <keescook@chromium.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      d6986ce2
  15. Jan 15, 2022
    • Cai Huoqing's avatar
      kthread: add the helper function kthread_run_on_cpu() · 800977f6
      Cai Huoqing authored
      Add a new helper function kthread_run_on_cpu(), which includes
      kthread_create_on_cpu/wake_up_process().
      
      In some cases, use kthread_run_on_cpu() directly instead of
      kthread_create_on_node/kthread_bind/wake_up_process() or
      kthread_create_on_cpu/wake_up_process() or
      kthreadd_create/kthread_bind/wake_up_process() to simplify the code.
      
      [akpm@linux-foundation.org: export kthread_create_on_cpu to modules]
      
      Link: https://lkml.kernel.org/r/20211022025711.3673-2-caihuoqing@baidu.com
      
      
      Signed-off-by: default avatarCai Huoqing <caihuoqing@baidu.com>
      Cc: Bernard Metzler <bmt@zurich.ibm.com>
      Cc: Cai Huoqing <caihuoqing@baidu.com>
      Cc: Daniel Bristot de Oliveira <bristot@kernel.org>
      Cc: Davidlohr Bueso <dave@stgolabs.net>
      Cc: Doug Ledford <dledford@redhat.com>
      Cc: Ingo Molnar <mingo@redhat.com>
      Cc: Jason Gunthorpe <jgg@ziepe.ca>
      Cc: Joel Fernandes (Google) <joel@joelfernandes.org>
      Cc: Josh Triplett <josh@joshtriplett.org>
      Cc: Lai Jiangshan <jiangshanlai@gmail.com>
      Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
      Cc: "Paul E . McKenney" <paulmck@kernel.org>
      Cc: Steven Rostedt <rostedt@goodmis.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      800977f6
  16. Jan 08, 2022
  17. Dec 14, 2021
  18. Dec 13, 2021
    • Eric W. Biederman's avatar
      exit/kthread: Move the exit code for kernel threads into struct kthread · 6b124879
      Eric W. Biederman authored
      
      The exit code of kernel threads has different semantics than the
      exit_code of userspace tasks.  To avoid confusion and allow
      the userspace implementation to change as needed move
      the kernel thread exit code into struct kthread.
      
      Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
      6b124879
    • Eric W. Biederman's avatar
      kthread: Ensure struct kthread is present for all kthreads · 40966e31
      Eric W. Biederman authored
      
      Today the rules are a bit iffy and arbitrary about which kernel
      threads have struct kthread present.  Both idle threads and thread
      started with create_kthread want struct kthread present so that is
      effectively all kernel threads.  Make the rule that if PF_KTHREAD
      and the task is running then struct kthread is present.
      
      This will allow the kernel thread code to using tsk->exit_code
      with different semantics from ordinary processes.
      
      To make ensure that struct kthread is present for all
      kernel threads move it's allocation into copy_process.
      
      Add a deallocation of struct kthread in exec for processes
      that were kernel threads.
      
      Move the allocation of struct kthread for the initial thread
      earlier so that it is not repeated for each additional idle
      thread.
      
      Move the initialization of struct kthread into set_kthread_struct
      so that the structure is always and reliably initailized.
      
      Clear set_child_tid in free_kthread_struct to ensure the kthread
      struct is reliably freed during exec.  The function
      free_kthread_struct does not need to clear vfork_done during exec as
      exec_mm_release called from exec_mmap has already cleared vfork_done.
      
      Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
      40966e31
    • Eric W. Biederman's avatar
      exit: Rename complete_and_exit to kthread_complete_and_exit · cead1855
      Eric W. Biederman authored
      
      Update complete_and_exit to call kthread_exit instead of do_exit.
      
      Change the name to reflect this change in functionality.  All of the
      users of complete_and_exit are causing the current kthread to exit so
      this change makes it clear what is happening.
      
      Move the implementation of kthread_complete_and_exit from
      kernel/exit.c to to kernel/kthread.c.  As this function is kthread
      specific it makes most sense to live with the kthread functions.
      
      There are no functional change.
      
      Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
      cead1855
    • Eric W. Biederman's avatar
      exit: Implement kthread_exit · bbda86e9
      Eric W. Biederman authored
      
      The way the per task_struct exit_code is used by kernel threads is not
      quite compatible how it is used by userspace applications.  The low
      byte of the userspace exit_code value encodes the exit signal.  While
      kthreads just use the value as an int holding ordinary kernel function
      exit status like -EPERM.
      
      Add kthread_exit to clearly separate the two kinds of uses.
      
      Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
      bbda86e9
  19. Oct 29, 2021
  20. Oct 05, 2021
  21. Jun 29, 2021
  22. Jun 25, 2021
    • Petr Mladek's avatar
      kthread: prevent deadlock when kthread_mod_delayed_work() races with... · 5fa54346
      Petr Mladek authored
      kthread: prevent deadlock when kthread_mod_delayed_work() races with kthread_cancel_delayed_work_sync()
      
      The system might hang with the following backtrace:
      
      	schedule+0x80/0x100
      	schedule_timeout+0x48/0x138
      	wait_for_common+0xa4/0x134
      	wait_for_completion+0x1c/0x2c
      	kthread_flush_work+0x114/0x1cc
      	kthread_cancel_work_sync.llvm.16514401384283632983+0xe8/0x144
      	kthread_cancel_delayed_work_sync+0x18/0x2c
      	xxxx_pm_notify+0xb0/0xd8
      	blocking_notifier_call_chain_robust+0x80/0x194
      	pm_notifier_call_chain_robust+0x28/0x4c
      	suspend_prepare+0x40/0x260
      	enter_state+0x80/0x3f4
      	pm_suspend+0x60/0xdc
      	state_store+0x108/0x144
      	kobj_attr_store+0x38/0x88
      	sysfs_kf_write+0x64/0xc0
      	kernfs_fop_write_iter+0x108/0x1d0
      	vfs_write+0x2f4/0x368
      	ksys_write+0x7c/0xec
      
      It is caused by the following race between kthread_mod_delayed_work()
      and kthread_cancel_delayed_work_sync():
      
      CPU0				CPU1
      
      Context: Thread A		Context: Thread B
      
      kthread_mod_delayed_work()
        spin_lock()
        __kthread_cancel_work()
           spin_unlock()
           del_timer_sync()
      				kthread_cancel_delayed_work_sync()
      				  spin_lock()
      				  __kthread_cancel_work()
      				    spin_unlock()
      				    del_timer_sync()
      				    spin_lock()
      
      				  work->canceling++
      				  spin_unlock
           spin_lock()
         queue_delayed_work()
           // dwork is put into the worker->delayed_work_list
      
         spin_unlock()
      
      				  kthread_flush_work()
           // flush_work is put at the tail of the dwork
      
      				    wait_for_completion()
      
      Context: IRQ
      
        kthread_delayed_work_timer_fn()
          spin_lock()
          list_del_init(&work->node);
          spin_unlock()
      
      BANG: flush_work is not longer linked and will never get proceed.
      
      The problem is that kthread_mod_delayed_work() checks work->canceling
      flag before canceling the timer.
      
      A simple solution is to (re)check work->canceling after
      __kthread_cancel_work().  But then it is not clear what should be
      returned when __kthread_cancel_work() removed the work from the queue
      (list) and it can't queue it again with the new @delay.
      
      The return value might be used for reference counting.  The caller has
      to know whether a new work has been queued or an existing one was
      replaced.
      
      The proper solution is that kthread_mod_delayed_work() will remove the
      work from the queue (list) _only_ when work->canceling is not set.  The
      flag must be checked after the timer is stopped and the remaining
      operations can be done under worker->lock.
      
      Note that kthread_mod_delayed_work() could remove the timer and then
      bail out.  It is fine.  The other canceling caller needs to cancel the
      timer as well.  The important thing is that the queue (list)
      manipulation is done atomically under worker->lock.
      
      Link: https://lkml.kernel.org/r/20210610133051.15337-3-pmladek@suse.com
      
      
      Fixes: 9a6b06c8 ("kthread: allow to modify delayed kthread work")
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Reported-by: default avatarMartin Liu <liumartin@google.com>
      Cc: <jenhaochen@google.com>
      Cc: Minchan Kim <minchan@google.com>
      Cc: Nathan Chancellor <nathan@kernel.org>
      Cc: Nick Desaulniers <ndesaulniers@google.com>
      Cc: Oleg Nesterov <oleg@redhat.com>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      5fa54346
    • Petr Mladek's avatar
      kthread_worker: split code for canceling the delayed work timer · 34b3d534
      Petr Mladek authored
      Patch series "kthread_worker: Fix race between kthread_mod_delayed_work()
      and kthread_cancel_delayed_work_sync()".
      
      This patchset fixes the race between kthread_mod_delayed_work() and
      kthread_cancel_delayed_work_sync() including proper return value
      handling.
      
      This patch (of 2):
      
      Simple code refactoring as a preparation step for fixing a race between
      kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync().
      
      It does not modify the existing behavior.
      
      Link: https://lkml.kernel.org/r/20210610133051.15337-2-pmladek@suse.com
      
      
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Cc: <jenhaochen@google.com>
      Cc: Martin Liu <liumartin@google.com>
      Cc: Minchan Kim <minchan@google.com>
      Cc: Nathan Chancellor <nathan@kernel.org>
      Cc: Nick Desaulniers <ndesaulniers@google.com>
      Cc: Oleg Nesterov <oleg@redhat.com>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      34b3d534
  23. Jun 18, 2021
  24. May 18, 2021
  25. Apr 21, 2021
    • Peter Zijlstra's avatar
      kthread: Fix PF_KTHREAD vs to_kthread() race · 3a7956e2
      Peter Zijlstra authored
      
      The kthread_is_per_cpu() construct relies on only being called on
      PF_KTHREAD tasks (per the WARN in to_kthread). This gives rise to the
      following usage pattern:
      
      	if ((p->flags & PF_KTHREAD) && kthread_is_per_cpu(p))
      
      However, as reported by syzcaller, this is broken. The scenario is:
      
      	CPU0				CPU1 (running p)
      
      	(p->flags & PF_KTHREAD) // true
      
      					begin_new_exec()
      					  me->flags &= ~(PF_KTHREAD|...);
      	kthread_is_per_cpu(p)
      	  to_kthread(p)
      	    WARN(!(p->flags & PF_KTHREAD) <-- *SPLAT*
      
      Introduce __to_kthread() that omits the WARN and is sure to check both
      values.
      
      Use this to remove the problematic pattern for kthread_is_per_cpu()
      and fix a number of other kthread_*() functions that have similar
      issues but are currently not used in ways that would expose the
      problem.
      
      Notably kthread_func() is only ever called on 'current', while
      kthread_probe_data() is only used for PF_WQ_WORKER, which implies the
      task is from kthread_create*().
      
      Fixes: ac687e6e ("kthread: Extract KTHREAD_IS_PER_CPU")
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Reviewed-by: default avatarValentin Schneider <Valentin.Schneider@arm.com>
      Link: https://lkml.kernel.org/r/YH6WJc825C4P0FCK@hirez.programming.kicks-ass.net
      3a7956e2
  26. Apr 08, 2021
  27. Jan 22, 2021
  28. Jan 11, 2021
  29. Dec 15, 2020
  30. Nov 02, 2020
  31. Oct 29, 2020
  32. Oct 16, 2020
Loading