]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/sched/signal.h
sched/headers: Move signal wakeup & sigpending methods from <linux/sched.h> into...
[mirror_ubuntu-bionic-kernel.git] / include / linux / sched / signal.h
1 #ifndef _LINUX_SCHED_SIGNAL_H
2 #define _LINUX_SCHED_SIGNAL_H
3
4 #include <linux/rculist.h>
5 #include <linux/signal.h>
6 #include <linux/sched.h>
7 #include <linux/sched/jobctl.h>
8 #include <linux/sched/task.h>
9 #include <linux/cred.h>
10
11 /*
12 * Types defining task->signal and task->sighand and APIs using them:
13 */
14
15 struct sighand_struct {
16 atomic_t count;
17 struct k_sigaction action[_NSIG];
18 spinlock_t siglock;
19 wait_queue_head_t signalfd_wqh;
20 };
21
22 /*
23 * Per-process accounting stats:
24 */
25 struct pacct_struct {
26 int ac_flag;
27 long ac_exitcode;
28 unsigned long ac_mem;
29 u64 ac_utime, ac_stime;
30 unsigned long ac_minflt, ac_majflt;
31 };
32
33 struct cpu_itimer {
34 u64 expires;
35 u64 incr;
36 };
37
38 /*
39 * NOTE! "signal_struct" does not have its own
40 * locking, because a shared signal_struct always
41 * implies a shared sighand_struct, so locking
42 * sighand_struct is always a proper superset of
43 * the locking of signal_struct.
44 */
45 struct signal_struct {
46 atomic_t sigcnt;
47 atomic_t live;
48 int nr_threads;
49 struct list_head thread_head;
50
51 wait_queue_head_t wait_chldexit; /* for wait4() */
52
53 /* current thread group signal load-balancing target: */
54 struct task_struct *curr_target;
55
56 /* shared signal handling: */
57 struct sigpending shared_pending;
58
59 /* thread group exit support */
60 int group_exit_code;
61 /* overloaded:
62 * - notify group_exit_task when ->count is equal to notify_count
63 * - everyone except group_exit_task is stopped during signal delivery
64 * of fatal signals, group_exit_task processes the signal.
65 */
66 int notify_count;
67 struct task_struct *group_exit_task;
68
69 /* thread group stop support, overloads group_exit_code too */
70 int group_stop_count;
71 unsigned int flags; /* see SIGNAL_* flags below */
72
73 /*
74 * PR_SET_CHILD_SUBREAPER marks a process, like a service
75 * manager, to re-parent orphan (double-forking) child processes
76 * to this process instead of 'init'. The service manager is
77 * able to receive SIGCHLD signals and is able to investigate
78 * the process until it calls wait(). All children of this
79 * process will inherit a flag if they should look for a
80 * child_subreaper process at exit.
81 */
82 unsigned int is_child_subreaper:1;
83 unsigned int has_child_subreaper:1;
84
85 #ifdef CONFIG_POSIX_TIMERS
86
87 /* POSIX.1b Interval Timers */
88 int posix_timer_id;
89 struct list_head posix_timers;
90
91 /* ITIMER_REAL timer for the process */
92 struct hrtimer real_timer;
93 ktime_t it_real_incr;
94
95 /*
96 * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use
97 * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these
98 * values are defined to 0 and 1 respectively
99 */
100 struct cpu_itimer it[2];
101
102 /*
103 * Thread group totals for process CPU timers.
104 * See thread_group_cputimer(), et al, for details.
105 */
106 struct thread_group_cputimer cputimer;
107
108 /* Earliest-expiration cache. */
109 struct task_cputime cputime_expires;
110
111 struct list_head cpu_timers[3];
112
113 #endif
114
115 struct pid *leader_pid;
116
117 #ifdef CONFIG_NO_HZ_FULL
118 atomic_t tick_dep_mask;
119 #endif
120
121 struct pid *tty_old_pgrp;
122
123 /* boolean value for session group leader */
124 int leader;
125
126 struct tty_struct *tty; /* NULL if no tty */
127
128 #ifdef CONFIG_SCHED_AUTOGROUP
129 struct autogroup *autogroup;
130 #endif
131 /*
132 * Cumulative resource counters for dead threads in the group,
133 * and for reaped dead child processes forked by this group.
134 * Live threads maintain their own counters and add to these
135 * in __exit_signal, except for the group leader.
136 */
137 seqlock_t stats_lock;
138 u64 utime, stime, cutime, cstime;
139 u64 gtime;
140 u64 cgtime;
141 struct prev_cputime prev_cputime;
142 unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
143 unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
144 unsigned long inblock, oublock, cinblock, coublock;
145 unsigned long maxrss, cmaxrss;
146 struct task_io_accounting ioac;
147
148 /*
149 * Cumulative ns of schedule CPU time fo dead threads in the
150 * group, not including a zombie group leader, (This only differs
151 * from jiffies_to_ns(utime + stime) if sched_clock uses something
152 * other than jiffies.)
153 */
154 unsigned long long sum_sched_runtime;
155
156 /*
157 * We don't bother to synchronize most readers of this at all,
158 * because there is no reader checking a limit that actually needs
159 * to get both rlim_cur and rlim_max atomically, and either one
160 * alone is a single word that can safely be read normally.
161 * getrlimit/setrlimit use task_lock(current->group_leader) to
162 * protect this instead of the siglock, because they really
163 * have no need to disable irqs.
164 */
165 struct rlimit rlim[RLIM_NLIMITS];
166
167 #ifdef CONFIG_BSD_PROCESS_ACCT
168 struct pacct_struct pacct; /* per-process accounting information */
169 #endif
170 #ifdef CONFIG_TASKSTATS
171 struct taskstats *stats;
172 #endif
173 #ifdef CONFIG_AUDIT
174 unsigned audit_tty;
175 struct tty_audit_buf *tty_audit_buf;
176 #endif
177
178 /*
179 * Thread is the potential origin of an oom condition; kill first on
180 * oom
181 */
182 bool oom_flag_origin;
183 short oom_score_adj; /* OOM kill score adjustment */
184 short oom_score_adj_min; /* OOM kill score adjustment min value.
185 * Only settable by CAP_SYS_RESOURCE. */
186 struct mm_struct *oom_mm; /* recorded mm when the thread group got
187 * killed by the oom killer */
188
189 struct mutex cred_guard_mutex; /* guard against foreign influences on
190 * credential calculations
191 * (notably. ptrace) */
192 };
193
194 /*
195 * Bits in flags field of signal_struct.
196 */
197 #define SIGNAL_STOP_STOPPED 0x00000001 /* job control stop in effect */
198 #define SIGNAL_STOP_CONTINUED 0x00000002 /* SIGCONT since WCONTINUED reap */
199 #define SIGNAL_GROUP_EXIT 0x00000004 /* group exit in progress */
200 #define SIGNAL_GROUP_COREDUMP 0x00000008 /* coredump in progress */
201 /*
202 * Pending notifications to parent.
203 */
204 #define SIGNAL_CLD_STOPPED 0x00000010
205 #define SIGNAL_CLD_CONTINUED 0x00000020
206 #define SIGNAL_CLD_MASK (SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
207
208 #define SIGNAL_UNKILLABLE 0x00000040 /* for init: ignore fatal signals */
209
210 #define SIGNAL_STOP_MASK (SIGNAL_CLD_MASK | SIGNAL_STOP_STOPPED | \
211 SIGNAL_STOP_CONTINUED)
212
213 static inline void signal_set_stop_flags(struct signal_struct *sig,
214 unsigned int flags)
215 {
216 WARN_ON(sig->flags & (SIGNAL_GROUP_EXIT|SIGNAL_GROUP_COREDUMP));
217 sig->flags = (sig->flags & ~SIGNAL_STOP_MASK) | flags;
218 }
219
220 /* If true, all threads except ->group_exit_task have pending SIGKILL */
221 static inline int signal_group_exit(const struct signal_struct *sig)
222 {
223 return (sig->flags & SIGNAL_GROUP_EXIT) ||
224 (sig->group_exit_task != NULL);
225 }
226
227 extern void flush_signals(struct task_struct *);
228 extern void ignore_signals(struct task_struct *);
229 extern void flush_signal_handlers(struct task_struct *, int force_default);
230 extern int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info);
231
232 static inline int kernel_dequeue_signal(siginfo_t *info)
233 {
234 struct task_struct *tsk = current;
235 siginfo_t __info;
236 int ret;
237
238 spin_lock_irq(&tsk->sighand->siglock);
239 ret = dequeue_signal(tsk, &tsk->blocked, info ?: &__info);
240 spin_unlock_irq(&tsk->sighand->siglock);
241
242 return ret;
243 }
244
245 static inline void kernel_signal_stop(void)
246 {
247 spin_lock_irq(&current->sighand->siglock);
248 if (current->jobctl & JOBCTL_STOP_DEQUEUED)
249 __set_current_state(TASK_STOPPED);
250 spin_unlock_irq(&current->sighand->siglock);
251
252 schedule();
253 }
254 extern int send_sig_info(int, struct siginfo *, struct task_struct *);
255 extern int force_sigsegv(int, struct task_struct *);
256 extern int force_sig_info(int, struct siginfo *, struct task_struct *);
257 extern int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp);
258 extern int kill_pid_info(int sig, struct siginfo *info, struct pid *pid);
259 extern int kill_pid_info_as_cred(int, struct siginfo *, struct pid *,
260 const struct cred *, u32);
261 extern int kill_pgrp(struct pid *pid, int sig, int priv);
262 extern int kill_pid(struct pid *pid, int sig, int priv);
263 extern int kill_proc_info(int, struct siginfo *, pid_t);
264 extern __must_check bool do_notify_parent(struct task_struct *, int);
265 extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
266 extern void force_sig(int, struct task_struct *);
267 extern int send_sig(int, struct task_struct *, int);
268 extern int zap_other_threads(struct task_struct *p);
269 extern struct sigqueue *sigqueue_alloc(void);
270 extern void sigqueue_free(struct sigqueue *);
271 extern int send_sigqueue(struct sigqueue *, struct task_struct *, int group);
272 extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
273
274 static inline int restart_syscall(void)
275 {
276 set_tsk_thread_flag(current, TIF_SIGPENDING);
277 return -ERESTARTNOINTR;
278 }
279
280 static inline int signal_pending(struct task_struct *p)
281 {
282 return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
283 }
284
285 static inline int __fatal_signal_pending(struct task_struct *p)
286 {
287 return unlikely(sigismember(&p->pending.signal, SIGKILL));
288 }
289
290 static inline int fatal_signal_pending(struct task_struct *p)
291 {
292 return signal_pending(p) && __fatal_signal_pending(p);
293 }
294
295 static inline int signal_pending_state(long state, struct task_struct *p)
296 {
297 if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
298 return 0;
299 if (!signal_pending(p))
300 return 0;
301
302 return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
303 }
304
305 /*
306 * Reevaluate whether the task has signals pending delivery.
307 * Wake the task if so.
308 * This is required every time the blocked sigset_t changes.
309 * callers must hold sighand->siglock.
310 */
311 extern void recalc_sigpending_and_wake(struct task_struct *t);
312 extern void recalc_sigpending(void);
313
314 extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
315
316 static inline void signal_wake_up(struct task_struct *t, bool resume)
317 {
318 signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
319 }
320 static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
321 {
322 signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
323 }
324
325 #ifdef TIF_RESTORE_SIGMASK
326 /*
327 * Legacy restore_sigmask accessors. These are inefficient on
328 * SMP architectures because they require atomic operations.
329 */
330
331 /**
332 * set_restore_sigmask() - make sure saved_sigmask processing gets done
333 *
334 * This sets TIF_RESTORE_SIGMASK and ensures that the arch signal code
335 * will run before returning to user mode, to process the flag. For
336 * all callers, TIF_SIGPENDING is already set or it's no harm to set
337 * it. TIF_RESTORE_SIGMASK need not be in the set of bits that the
338 * arch code will notice on return to user mode, in case those bits
339 * are scarce. We set TIF_SIGPENDING here to ensure that the arch
340 * signal code always gets run when TIF_RESTORE_SIGMASK is set.
341 */
342 static inline void set_restore_sigmask(void)
343 {
344 set_thread_flag(TIF_RESTORE_SIGMASK);
345 WARN_ON(!test_thread_flag(TIF_SIGPENDING));
346 }
347 static inline void clear_restore_sigmask(void)
348 {
349 clear_thread_flag(TIF_RESTORE_SIGMASK);
350 }
351 static inline bool test_restore_sigmask(void)
352 {
353 return test_thread_flag(TIF_RESTORE_SIGMASK);
354 }
355 static inline bool test_and_clear_restore_sigmask(void)
356 {
357 return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
358 }
359
360 #else /* TIF_RESTORE_SIGMASK */
361
362 /* Higher-quality implementation, used if TIF_RESTORE_SIGMASK doesn't exist. */
363 static inline void set_restore_sigmask(void)
364 {
365 current->restore_sigmask = true;
366 WARN_ON(!test_thread_flag(TIF_SIGPENDING));
367 }
368 static inline void clear_restore_sigmask(void)
369 {
370 current->restore_sigmask = false;
371 }
372 static inline bool test_restore_sigmask(void)
373 {
374 return current->restore_sigmask;
375 }
376 static inline bool test_and_clear_restore_sigmask(void)
377 {
378 if (!current->restore_sigmask)
379 return false;
380 current->restore_sigmask = false;
381 return true;
382 }
383 #endif
384
385 static inline void restore_saved_sigmask(void)
386 {
387 if (test_and_clear_restore_sigmask())
388 __set_current_blocked(&current->saved_sigmask);
389 }
390
391 static inline sigset_t *sigmask_to_save(void)
392 {
393 sigset_t *res = &current->blocked;
394 if (unlikely(test_restore_sigmask()))
395 res = &current->saved_sigmask;
396 return res;
397 }
398
399 static inline int kill_cad_pid(int sig, int priv)
400 {
401 return kill_pid(cad_pid, sig, priv);
402 }
403
404 /* These can be the second arg to send_sig_info/send_group_sig_info. */
405 #define SEND_SIG_NOINFO ((struct siginfo *) 0)
406 #define SEND_SIG_PRIV ((struct siginfo *) 1)
407 #define SEND_SIG_FORCED ((struct siginfo *) 2)
408
409 /*
410 * True if we are on the alternate signal stack.
411 */
412 static inline int on_sig_stack(unsigned long sp)
413 {
414 /*
415 * If the signal stack is SS_AUTODISARM then, by construction, we
416 * can't be on the signal stack unless user code deliberately set
417 * SS_AUTODISARM when we were already on it.
418 *
419 * This improves reliability: if user state gets corrupted such that
420 * the stack pointer points very close to the end of the signal stack,
421 * then this check will enable the signal to be handled anyway.
422 */
423 if (current->sas_ss_flags & SS_AUTODISARM)
424 return 0;
425
426 #ifdef CONFIG_STACK_GROWSUP
427 return sp >= current->sas_ss_sp &&
428 sp - current->sas_ss_sp < current->sas_ss_size;
429 #else
430 return sp > current->sas_ss_sp &&
431 sp - current->sas_ss_sp <= current->sas_ss_size;
432 #endif
433 }
434
435 static inline int sas_ss_flags(unsigned long sp)
436 {
437 if (!current->sas_ss_size)
438 return SS_DISABLE;
439
440 return on_sig_stack(sp) ? SS_ONSTACK : 0;
441 }
442
443 static inline void sas_ss_reset(struct task_struct *p)
444 {
445 p->sas_ss_sp = 0;
446 p->sas_ss_size = 0;
447 p->sas_ss_flags = SS_DISABLE;
448 }
449
450 static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
451 {
452 if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
453 #ifdef CONFIG_STACK_GROWSUP
454 return current->sas_ss_sp;
455 #else
456 return current->sas_ss_sp + current->sas_ss_size;
457 #endif
458 return sp;
459 }
460
461 extern void __cleanup_sighand(struct sighand_struct *);
462 extern void flush_itimer_signals(void);
463
464 #define tasklist_empty() \
465 list_empty(&init_task.tasks)
466
467 #define next_task(p) \
468 list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
469
470 #define for_each_process(p) \
471 for (p = &init_task ; (p = next_task(p)) != &init_task ; )
472
473 extern bool current_is_single_threaded(void);
474
475 /*
476 * Careful: do_each_thread/while_each_thread is a double loop so
477 * 'break' will not work as expected - use goto instead.
478 */
479 #define do_each_thread(g, t) \
480 for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
481
482 #define while_each_thread(g, t) \
483 while ((t = next_thread(t)) != g)
484
485 #define __for_each_thread(signal, t) \
486 list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
487
488 #define for_each_thread(p, t) \
489 __for_each_thread((p)->signal, t)
490
491 /* Careful: this is a double loop, 'break' won't work as expected. */
492 #define for_each_process_thread(p, t) \
493 for_each_process(p) for_each_thread(p, t)
494
495 typedef int (*proc_visitor)(struct task_struct *p, void *data);
496 void walk_process_tree(struct task_struct *top, proc_visitor, void *);
497
498 static inline int get_nr_threads(struct task_struct *tsk)
499 {
500 return tsk->signal->nr_threads;
501 }
502
503 static inline bool thread_group_leader(struct task_struct *p)
504 {
505 return p->exit_signal >= 0;
506 }
507
508 /* Do to the insanities of de_thread it is possible for a process
509 * to have the pid of the thread group leader without actually being
510 * the thread group leader. For iteration through the pids in proc
511 * all we care about is that we have a task with the appropriate
512 * pid, we don't actually care if we have the right task.
513 */
514 static inline bool has_group_leader_pid(struct task_struct *p)
515 {
516 return task_pid(p) == p->signal->leader_pid;
517 }
518
519 static inline
520 bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
521 {
522 return p1->signal == p2->signal;
523 }
524
525 static inline struct task_struct *next_thread(const struct task_struct *p)
526 {
527 return list_entry_rcu(p->thread_group.next,
528 struct task_struct, thread_group);
529 }
530
531 static inline int thread_group_empty(struct task_struct *p)
532 {
533 return list_empty(&p->thread_group);
534 }
535
536 #define delay_group_leader(p) \
537 (thread_group_leader(p) && !thread_group_empty(p))
538
539 extern struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
540 unsigned long *flags);
541
542 static inline struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
543 unsigned long *flags)
544 {
545 struct sighand_struct *ret;
546
547 ret = __lock_task_sighand(tsk, flags);
548 (void)__cond_lock(&tsk->sighand->siglock, ret);
549 return ret;
550 }
551
552 static inline void unlock_task_sighand(struct task_struct *tsk,
553 unsigned long *flags)
554 {
555 spin_unlock_irqrestore(&tsk->sighand->siglock, *flags);
556 }
557
558 static inline unsigned long task_rlimit(const struct task_struct *tsk,
559 unsigned int limit)
560 {
561 return READ_ONCE(tsk->signal->rlim[limit].rlim_cur);
562 }
563
564 static inline unsigned long task_rlimit_max(const struct task_struct *tsk,
565 unsigned int limit)
566 {
567 return READ_ONCE(tsk->signal->rlim[limit].rlim_max);
568 }
569
570 static inline unsigned long rlimit(unsigned int limit)
571 {
572 return task_rlimit(current, limit);
573 }
574
575 static inline unsigned long rlimit_max(unsigned int limit)
576 {
577 return task_rlimit_max(current, limit);
578 }
579
580 #endif /* _LINUX_SCHED_SIGNAL_H */