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