0001
0002 #ifndef _LINUX_SCHED_SIGNAL_H
0003 #define _LINUX_SCHED_SIGNAL_H
0004
0005 #include <linux/rculist.h>
0006 #include <linux/signal.h>
0007 #include <linux/sched.h>
0008 #include <linux/sched/jobctl.h>
0009 #include <linux/sched/task.h>
0010 #include <linux/cred.h>
0011 #include <linux/refcount.h>
0012 #include <linux/posix-timers.h>
0013 #include <linux/mm_types.h>
0014 #include <asm/ptrace.h>
0015
0016
0017
0018
0019
0020 struct sighand_struct {
0021 spinlock_t siglock;
0022 refcount_t count;
0023 wait_queue_head_t signalfd_wqh;
0024 struct k_sigaction action[_NSIG];
0025 };
0026
0027
0028
0029
0030 struct pacct_struct {
0031 int ac_flag;
0032 long ac_exitcode;
0033 unsigned long ac_mem;
0034 u64 ac_utime, ac_stime;
0035 unsigned long ac_minflt, ac_majflt;
0036 };
0037
0038 struct cpu_itimer {
0039 u64 expires;
0040 u64 incr;
0041 };
0042
0043
0044
0045
0046
0047 struct task_cputime_atomic {
0048 atomic64_t utime;
0049 atomic64_t stime;
0050 atomic64_t sum_exec_runtime;
0051 };
0052
0053 #define INIT_CPUTIME_ATOMIC \
0054 (struct task_cputime_atomic) { \
0055 .utime = ATOMIC64_INIT(0), \
0056 .stime = ATOMIC64_INIT(0), \
0057 .sum_exec_runtime = ATOMIC64_INIT(0), \
0058 }
0059
0060
0061
0062
0063
0064
0065
0066 struct thread_group_cputimer {
0067 struct task_cputime_atomic cputime_atomic;
0068 };
0069
0070 struct multiprocess_signals {
0071 sigset_t signal;
0072 struct hlist_node node;
0073 };
0074
0075 struct core_thread {
0076 struct task_struct *task;
0077 struct core_thread *next;
0078 };
0079
0080 struct core_state {
0081 atomic_t nr_threads;
0082 struct core_thread dumper;
0083 struct completion startup;
0084 };
0085
0086
0087
0088
0089
0090
0091
0092
0093 struct signal_struct {
0094 refcount_t sigcnt;
0095 atomic_t live;
0096 int nr_threads;
0097 struct list_head thread_head;
0098
0099 wait_queue_head_t wait_chldexit;
0100
0101
0102 struct task_struct *curr_target;
0103
0104
0105 struct sigpending shared_pending;
0106
0107
0108 struct hlist_head multiprocess;
0109
0110
0111 int group_exit_code;
0112
0113 int notify_count;
0114 struct task_struct *group_exec_task;
0115
0116
0117 int group_stop_count;
0118 unsigned int flags;
0119
0120 struct core_state *core_state;
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131 unsigned int is_child_subreaper:1;
0132 unsigned int has_child_subreaper:1;
0133
0134 #ifdef CONFIG_POSIX_TIMERS
0135
0136
0137 int posix_timer_id;
0138 struct list_head posix_timers;
0139
0140
0141 struct hrtimer real_timer;
0142 ktime_t it_real_incr;
0143
0144
0145
0146
0147
0148
0149 struct cpu_itimer it[2];
0150
0151
0152
0153
0154
0155 struct thread_group_cputimer cputimer;
0156
0157 #endif
0158
0159 struct posix_cputimers posix_cputimers;
0160
0161
0162 struct pid *pids[PIDTYPE_MAX];
0163
0164 #ifdef CONFIG_NO_HZ_FULL
0165 atomic_t tick_dep_mask;
0166 #endif
0167
0168 struct pid *tty_old_pgrp;
0169
0170
0171 int leader;
0172
0173 struct tty_struct *tty;
0174
0175 #ifdef CONFIG_SCHED_AUTOGROUP
0176 struct autogroup *autogroup;
0177 #endif
0178
0179
0180
0181
0182
0183
0184 seqlock_t stats_lock;
0185 u64 utime, stime, cutime, cstime;
0186 u64 gtime;
0187 u64 cgtime;
0188 struct prev_cputime prev_cputime;
0189 unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
0190 unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
0191 unsigned long inblock, oublock, cinblock, coublock;
0192 unsigned long maxrss, cmaxrss;
0193 struct task_io_accounting ioac;
0194
0195
0196
0197
0198
0199
0200
0201 unsigned long long sum_sched_runtime;
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 struct rlimit rlim[RLIM_NLIMITS];
0213
0214 #ifdef CONFIG_BSD_PROCESS_ACCT
0215 struct pacct_struct pacct;
0216 #endif
0217 #ifdef CONFIG_TASKSTATS
0218 struct taskstats *stats;
0219 #endif
0220 #ifdef CONFIG_AUDIT
0221 unsigned audit_tty;
0222 struct tty_audit_buf *tty_audit_buf;
0223 #endif
0224
0225
0226
0227
0228
0229 bool oom_flag_origin;
0230 short oom_score_adj;
0231 short oom_score_adj_min;
0232
0233 struct mm_struct *oom_mm;
0234
0235
0236 struct mutex cred_guard_mutex;
0237
0238
0239
0240
0241
0242 struct rw_semaphore exec_update_lock;
0243
0244
0245
0246
0247 } __randomize_layout;
0248
0249
0250
0251
0252 #define SIGNAL_STOP_STOPPED 0x00000001
0253 #define SIGNAL_STOP_CONTINUED 0x00000002
0254 #define SIGNAL_GROUP_EXIT 0x00000004
0255
0256
0257
0258 #define SIGNAL_CLD_STOPPED 0x00000010
0259 #define SIGNAL_CLD_CONTINUED 0x00000020
0260 #define SIGNAL_CLD_MASK (SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
0261
0262 #define SIGNAL_UNKILLABLE 0x00000040
0263
0264 #define SIGNAL_STOP_MASK (SIGNAL_CLD_MASK | SIGNAL_STOP_STOPPED | \
0265 SIGNAL_STOP_CONTINUED)
0266
0267 static inline void signal_set_stop_flags(struct signal_struct *sig,
0268 unsigned int flags)
0269 {
0270 WARN_ON(sig->flags & SIGNAL_GROUP_EXIT);
0271 sig->flags = (sig->flags & ~SIGNAL_STOP_MASK) | flags;
0272 }
0273
0274 extern void flush_signals(struct task_struct *);
0275 extern void ignore_signals(struct task_struct *);
0276 extern void flush_signal_handlers(struct task_struct *, int force_default);
0277 extern int dequeue_signal(struct task_struct *task, sigset_t *mask,
0278 kernel_siginfo_t *info, enum pid_type *type);
0279
0280 static inline int kernel_dequeue_signal(void)
0281 {
0282 struct task_struct *task = current;
0283 kernel_siginfo_t __info;
0284 enum pid_type __type;
0285 int ret;
0286
0287 spin_lock_irq(&task->sighand->siglock);
0288 ret = dequeue_signal(task, &task->blocked, &__info, &__type);
0289 spin_unlock_irq(&task->sighand->siglock);
0290
0291 return ret;
0292 }
0293
0294 static inline void kernel_signal_stop(void)
0295 {
0296 spin_lock_irq(¤t->sighand->siglock);
0297 if (current->jobctl & JOBCTL_STOP_DEQUEUED) {
0298 current->jobctl |= JOBCTL_STOPPED;
0299 set_special_state(TASK_STOPPED);
0300 }
0301 spin_unlock_irq(¤t->sighand->siglock);
0302
0303 schedule();
0304 }
0305 #ifdef __ia64__
0306 # define ___ARCH_SI_IA64(_a1, _a2, _a3) , _a1, _a2, _a3
0307 #else
0308 # define ___ARCH_SI_IA64(_a1, _a2, _a3)
0309 #endif
0310
0311 int force_sig_fault_to_task(int sig, int code, void __user *addr
0312 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
0313 , struct task_struct *t);
0314 int force_sig_fault(int sig, int code, void __user *addr
0315 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr));
0316 int send_sig_fault(int sig, int code, void __user *addr
0317 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
0318 , struct task_struct *t);
0319
0320 int force_sig_mceerr(int code, void __user *, short);
0321 int send_sig_mceerr(int code, void __user *, short, struct task_struct *);
0322
0323 int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper);
0324 int force_sig_pkuerr(void __user *addr, u32 pkey);
0325 int send_sig_perf(void __user *addr, u32 type, u64 sig_data);
0326
0327 int force_sig_ptrace_errno_trap(int errno, void __user *addr);
0328 int force_sig_fault_trapno(int sig, int code, void __user *addr, int trapno);
0329 int send_sig_fault_trapno(int sig, int code, void __user *addr, int trapno,
0330 struct task_struct *t);
0331 int force_sig_seccomp(int syscall, int reason, bool force_coredump);
0332
0333 extern int send_sig_info(int, struct kernel_siginfo *, struct task_struct *);
0334 extern void force_sigsegv(int sig);
0335 extern int force_sig_info(struct kernel_siginfo *);
0336 extern int __kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp);
0337 extern int kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid);
0338 extern int kill_pid_usb_asyncio(int sig, int errno, sigval_t addr, struct pid *,
0339 const struct cred *);
0340 extern int kill_pgrp(struct pid *pid, int sig, int priv);
0341 extern int kill_pid(struct pid *pid, int sig, int priv);
0342 extern __must_check bool do_notify_parent(struct task_struct *, int);
0343 extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
0344 extern void force_sig(int);
0345 extern void force_fatal_sig(int);
0346 extern void force_exit_sig(int);
0347 extern int send_sig(int, struct task_struct *, int);
0348 extern int zap_other_threads(struct task_struct *p);
0349 extern struct sigqueue *sigqueue_alloc(void);
0350 extern void sigqueue_free(struct sigqueue *);
0351 extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type);
0352 extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
0353
0354 static inline void clear_notify_signal(void)
0355 {
0356 clear_thread_flag(TIF_NOTIFY_SIGNAL);
0357 smp_mb__after_atomic();
0358 }
0359
0360
0361
0362
0363
0364 static inline bool __set_notify_signal(struct task_struct *task)
0365 {
0366 return !test_and_set_tsk_thread_flag(task, TIF_NOTIFY_SIGNAL) &&
0367 !wake_up_state(task, TASK_INTERRUPTIBLE);
0368 }
0369
0370
0371
0372
0373
0374 static inline void set_notify_signal(struct task_struct *task)
0375 {
0376 if (__set_notify_signal(task))
0377 kick_process(task);
0378 }
0379
0380 static inline int restart_syscall(void)
0381 {
0382 set_tsk_thread_flag(current, TIF_SIGPENDING);
0383 return -ERESTARTNOINTR;
0384 }
0385
0386 static inline int task_sigpending(struct task_struct *p)
0387 {
0388 return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
0389 }
0390
0391 static inline int signal_pending(struct task_struct *p)
0392 {
0393
0394
0395
0396
0397
0398 if (unlikely(test_tsk_thread_flag(p, TIF_NOTIFY_SIGNAL)))
0399 return 1;
0400 return task_sigpending(p);
0401 }
0402
0403 static inline int __fatal_signal_pending(struct task_struct *p)
0404 {
0405 return unlikely(sigismember(&p->pending.signal, SIGKILL));
0406 }
0407
0408 static inline int fatal_signal_pending(struct task_struct *p)
0409 {
0410 return task_sigpending(p) && __fatal_signal_pending(p);
0411 }
0412
0413 static inline int signal_pending_state(unsigned int state, struct task_struct *p)
0414 {
0415 if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
0416 return 0;
0417 if (!signal_pending(p))
0418 return 0;
0419
0420 return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
0421 }
0422
0423
0424
0425
0426
0427
0428
0429 static inline bool fault_signal_pending(vm_fault_t fault_flags,
0430 struct pt_regs *regs)
0431 {
0432 return unlikely((fault_flags & VM_FAULT_RETRY) &&
0433 (fatal_signal_pending(current) ||
0434 (user_mode(regs) && signal_pending(current))));
0435 }
0436
0437
0438
0439
0440
0441
0442
0443 extern void recalc_sigpending_and_wake(struct task_struct *t);
0444 extern void recalc_sigpending(void);
0445 extern void calculate_sigpending(void);
0446
0447 extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
0448
0449 static inline void signal_wake_up(struct task_struct *t, bool fatal)
0450 {
0451 unsigned int state = 0;
0452 if (fatal && !(t->jobctl & JOBCTL_PTRACE_FROZEN)) {
0453 t->jobctl &= ~(JOBCTL_STOPPED | JOBCTL_TRACED);
0454 state = TASK_WAKEKILL | __TASK_TRACED;
0455 }
0456 signal_wake_up_state(t, state);
0457 }
0458 static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
0459 {
0460 unsigned int state = 0;
0461 if (resume) {
0462 t->jobctl &= ~JOBCTL_TRACED;
0463 state = __TASK_TRACED;
0464 }
0465 signal_wake_up_state(t, state);
0466 }
0467
0468 void task_join_group_stop(struct task_struct *task);
0469
0470 #ifdef TIF_RESTORE_SIGMASK
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487 static inline void set_restore_sigmask(void)
0488 {
0489 set_thread_flag(TIF_RESTORE_SIGMASK);
0490 }
0491
0492 static inline void clear_tsk_restore_sigmask(struct task_struct *task)
0493 {
0494 clear_tsk_thread_flag(task, TIF_RESTORE_SIGMASK);
0495 }
0496
0497 static inline void clear_restore_sigmask(void)
0498 {
0499 clear_thread_flag(TIF_RESTORE_SIGMASK);
0500 }
0501 static inline bool test_tsk_restore_sigmask(struct task_struct *task)
0502 {
0503 return test_tsk_thread_flag(task, TIF_RESTORE_SIGMASK);
0504 }
0505 static inline bool test_restore_sigmask(void)
0506 {
0507 return test_thread_flag(TIF_RESTORE_SIGMASK);
0508 }
0509 static inline bool test_and_clear_restore_sigmask(void)
0510 {
0511 return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
0512 }
0513
0514 #else
0515
0516
0517 static inline void set_restore_sigmask(void)
0518 {
0519 current->restore_sigmask = true;
0520 }
0521 static inline void clear_tsk_restore_sigmask(struct task_struct *task)
0522 {
0523 task->restore_sigmask = false;
0524 }
0525 static inline void clear_restore_sigmask(void)
0526 {
0527 current->restore_sigmask = false;
0528 }
0529 static inline bool test_restore_sigmask(void)
0530 {
0531 return current->restore_sigmask;
0532 }
0533 static inline bool test_tsk_restore_sigmask(struct task_struct *task)
0534 {
0535 return task->restore_sigmask;
0536 }
0537 static inline bool test_and_clear_restore_sigmask(void)
0538 {
0539 if (!current->restore_sigmask)
0540 return false;
0541 current->restore_sigmask = false;
0542 return true;
0543 }
0544 #endif
0545
0546 static inline void restore_saved_sigmask(void)
0547 {
0548 if (test_and_clear_restore_sigmask())
0549 __set_current_blocked(¤t->saved_sigmask);
0550 }
0551
0552 extern int set_user_sigmask(const sigset_t __user *umask, size_t sigsetsize);
0553
0554 static inline void restore_saved_sigmask_unless(bool interrupted)
0555 {
0556 if (interrupted)
0557 WARN_ON(!signal_pending(current));
0558 else
0559 restore_saved_sigmask();
0560 }
0561
0562 static inline sigset_t *sigmask_to_save(void)
0563 {
0564 sigset_t *res = ¤t->blocked;
0565 if (unlikely(test_restore_sigmask()))
0566 res = ¤t->saved_sigmask;
0567 return res;
0568 }
0569
0570 static inline int kill_cad_pid(int sig, int priv)
0571 {
0572 return kill_pid(cad_pid, sig, priv);
0573 }
0574
0575
0576 #define SEND_SIG_NOINFO ((struct kernel_siginfo *) 0)
0577 #define SEND_SIG_PRIV ((struct kernel_siginfo *) 1)
0578
0579 static inline int __on_sig_stack(unsigned long sp)
0580 {
0581 #ifdef CONFIG_STACK_GROWSUP
0582 return sp >= current->sas_ss_sp &&
0583 sp - current->sas_ss_sp < current->sas_ss_size;
0584 #else
0585 return sp > current->sas_ss_sp &&
0586 sp - current->sas_ss_sp <= current->sas_ss_size;
0587 #endif
0588 }
0589
0590
0591
0592
0593 static inline int on_sig_stack(unsigned long sp)
0594 {
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604 if (current->sas_ss_flags & SS_AUTODISARM)
0605 return 0;
0606
0607 return __on_sig_stack(sp);
0608 }
0609
0610 static inline int sas_ss_flags(unsigned long sp)
0611 {
0612 if (!current->sas_ss_size)
0613 return SS_DISABLE;
0614
0615 return on_sig_stack(sp) ? SS_ONSTACK : 0;
0616 }
0617
0618 static inline void sas_ss_reset(struct task_struct *p)
0619 {
0620 p->sas_ss_sp = 0;
0621 p->sas_ss_size = 0;
0622 p->sas_ss_flags = SS_DISABLE;
0623 }
0624
0625 static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
0626 {
0627 if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
0628 #ifdef CONFIG_STACK_GROWSUP
0629 return current->sas_ss_sp;
0630 #else
0631 return current->sas_ss_sp + current->sas_ss_size;
0632 #endif
0633 return sp;
0634 }
0635
0636 extern void __cleanup_sighand(struct sighand_struct *);
0637 extern void flush_itimer_signals(void);
0638
0639 #define tasklist_empty() \
0640 list_empty(&init_task.tasks)
0641
0642 #define next_task(p) \
0643 list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
0644
0645 #define for_each_process(p) \
0646 for (p = &init_task ; (p = next_task(p)) != &init_task ; )
0647
0648 extern bool current_is_single_threaded(void);
0649
0650
0651
0652
0653
0654 #define do_each_thread(g, t) \
0655 for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
0656
0657 #define while_each_thread(g, t) \
0658 while ((t = next_thread(t)) != g)
0659
0660 #define __for_each_thread(signal, t) \
0661 list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
0662
0663 #define for_each_thread(p, t) \
0664 __for_each_thread((p)->signal, t)
0665
0666
0667 #define for_each_process_thread(p, t) \
0668 for_each_process(p) for_each_thread(p, t)
0669
0670 typedef int (*proc_visitor)(struct task_struct *p, void *data);
0671 void walk_process_tree(struct task_struct *top, proc_visitor, void *);
0672
0673 static inline
0674 struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
0675 {
0676 struct pid *pid;
0677 if (type == PIDTYPE_PID)
0678 pid = task_pid(task);
0679 else
0680 pid = task->signal->pids[type];
0681 return pid;
0682 }
0683
0684 static inline struct pid *task_tgid(struct task_struct *task)
0685 {
0686 return task->signal->pids[PIDTYPE_TGID];
0687 }
0688
0689
0690
0691
0692
0693
0694 static inline struct pid *task_pgrp(struct task_struct *task)
0695 {
0696 return task->signal->pids[PIDTYPE_PGID];
0697 }
0698
0699 static inline struct pid *task_session(struct task_struct *task)
0700 {
0701 return task->signal->pids[PIDTYPE_SID];
0702 }
0703
0704 static inline int get_nr_threads(struct task_struct *task)
0705 {
0706 return task->signal->nr_threads;
0707 }
0708
0709 static inline bool thread_group_leader(struct task_struct *p)
0710 {
0711 return p->exit_signal >= 0;
0712 }
0713
0714 static inline
0715 bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
0716 {
0717 return p1->signal == p2->signal;
0718 }
0719
0720 static inline struct task_struct *next_thread(const struct task_struct *p)
0721 {
0722 return list_entry_rcu(p->thread_group.next,
0723 struct task_struct, thread_group);
0724 }
0725
0726 static inline int thread_group_empty(struct task_struct *p)
0727 {
0728 return list_empty(&p->thread_group);
0729 }
0730
0731 #define delay_group_leader(p) \
0732 (thread_group_leader(p) && !thread_group_empty(p))
0733
0734 extern bool thread_group_exited(struct pid *pid);
0735
0736 extern struct sighand_struct *__lock_task_sighand(struct task_struct *task,
0737 unsigned long *flags);
0738
0739 static inline struct sighand_struct *lock_task_sighand(struct task_struct *task,
0740 unsigned long *flags)
0741 {
0742 struct sighand_struct *ret;
0743
0744 ret = __lock_task_sighand(task, flags);
0745 (void)__cond_lock(&task->sighand->siglock, ret);
0746 return ret;
0747 }
0748
0749 static inline void unlock_task_sighand(struct task_struct *task,
0750 unsigned long *flags)
0751 {
0752 spin_unlock_irqrestore(&task->sighand->siglock, *flags);
0753 }
0754
0755 #ifdef CONFIG_LOCKDEP
0756 extern void lockdep_assert_task_sighand_held(struct task_struct *task);
0757 #else
0758 static inline void lockdep_assert_task_sighand_held(struct task_struct *task) { }
0759 #endif
0760
0761 static inline unsigned long task_rlimit(const struct task_struct *task,
0762 unsigned int limit)
0763 {
0764 return READ_ONCE(task->signal->rlim[limit].rlim_cur);
0765 }
0766
0767 static inline unsigned long task_rlimit_max(const struct task_struct *task,
0768 unsigned int limit)
0769 {
0770 return READ_ONCE(task->signal->rlim[limit].rlim_max);
0771 }
0772
0773 static inline unsigned long rlimit(unsigned int limit)
0774 {
0775 return task_rlimit(current, limit);
0776 }
0777
0778 static inline unsigned long rlimit_max(unsigned int limit)
0779 {
0780 return task_rlimit_max(current, limit);
0781 }
0782
0783 #endif