aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2018-08-21 16:47:29 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-08-21 16:47:29 -0400
commit0214f46b3a0383d6e33c297e7706216b6a550e4b (patch)
tree98f94fa818963d4c2b5ad2a16cd97ec19d036dce
parent40fafdcbcd7a74e80ab0229b35bf6e497831e28f (diff)
parent84fe4cc09abc1a5ef3a282db3ed10f4d3f1e6a0b (diff)
Merge branch 'siginfo-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace
Pull core signal handling updates from Eric Biederman: "It was observed that a periodic timer in combination with a sufficiently expensive fork could prevent fork from every completing. This contains the changes to remove the need for that restart. This set of changes is split into several parts: - The first part makes PIDTYPE_TGID a proper pid type instead something only for very special cases. The part starts using PIDTYPE_TGID enough so that in __send_signal where signals are actually delivered we know if the signal is being sent to a a group of processes or just a single process. - With that prep work out of the way the logic in fork is modified so that fork logically makes signals received while it is running appear to be received after the fork completes" * 'siginfo-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace: (22 commits) signal: Don't send signals to tasks that don't exist signal: Don't restart fork when signals come in. fork: Have new threads join on-going signal group stops fork: Skip setting TIF_SIGPENDING in ptrace_init_task signal: Add calculate_sigpending() fork: Unconditionally exit if a fatal signal is pending fork: Move and describe why the code examines PIDNS_ADDING signal: Push pid type down into complete_signal. signal: Push pid type down into __send_signal signal: Push pid type down into send_signal signal: Pass pid type into do_send_sig_info signal: Pass pid type into send_sigio_to_task & send_sigurg_to_task signal: Pass pid type into group_send_sig_info signal: Pass pid and pid type into send_sigqueue posix-timers: Noralize good_sigevent signal: Use PIDTYPE_TGID to clearly store where file signals will be sent pid: Implement PIDTYPE_TGID pids: Move the pgrp and session pid pointers from task_struct to signal_struct kvm: Don't open code task_pid in kvm_vcpu_ioctl pids: Compute task_tgid using signal->leader_pid ...
-rw-r--r--arch/ia64/kernel/asm-offsets.c4
-rw-r--r--arch/ia64/kernel/fsys.S12
-rw-r--r--arch/s390/kernel/perf_cpum_sf.c2
-rw-r--r--drivers/net/tun.c2
-rw-r--r--drivers/platform/x86/thinkpad_acpi.c1
-rw-r--r--drivers/tty/sysrq.c2
-rw-r--r--drivers/tty/tty_io.c2
-rw-r--r--fs/autofs/autofs_i.h1
-rw-r--r--fs/exec.c1
-rw-r--r--fs/fcntl.c74
-rw-r--r--fs/fuse/file.c1
-rw-r--r--fs/locks.c2
-rw-r--r--fs/notify/dnotify/dnotify.c3
-rw-r--r--fs/notify/fanotify/fanotify.c1
-rw-r--r--include/linux/init_task.h9
-rw-r--r--include/linux/pid.h11
-rw-r--r--include/linux/ptrace.h2
-rw-r--r--include/linux/sched.h31
-rw-r--r--include/linux/sched/signal.h49
-rw-r--r--include/linux/signal.h6
-rw-r--r--include/net/scm.h1
-rw-r--r--init/init_task.c13
-rw-r--r--kernel/events/core.c2
-rw-r--r--kernel/exit.c12
-rw-r--r--kernel/fork.c75
-rw-r--r--kernel/pid.c42
-rw-r--r--kernel/sched/core.c2
-rw-r--r--kernel/signal.c98
-rw-r--r--kernel/time/itimer.c5
-rw-r--r--kernel/time/posix-cpu-timers.c2
-rw-r--r--kernel/time/posix-timers.c21
-rw-r--r--mm/oom_kill.c4
-rw-r--r--virt/kvm/kvm_main.c2
33 files changed, 294 insertions, 201 deletions
diff --git a/arch/ia64/kernel/asm-offsets.c b/arch/ia64/kernel/asm-offsets.c
index f4db2168d1b8..00e8e2a1eb19 100644
--- a/arch/ia64/kernel/asm-offsets.c
+++ b/arch/ia64/kernel/asm-offsets.c
@@ -50,8 +50,7 @@ void foo(void)
50 50
51 DEFINE(IA64_TASK_BLOCKED_OFFSET,offsetof (struct task_struct, blocked)); 51 DEFINE(IA64_TASK_BLOCKED_OFFSET,offsetof (struct task_struct, blocked));
52 DEFINE(IA64_TASK_CLEAR_CHILD_TID_OFFSET,offsetof (struct task_struct, clear_child_tid)); 52 DEFINE(IA64_TASK_CLEAR_CHILD_TID_OFFSET,offsetof (struct task_struct, clear_child_tid));
53 DEFINE(IA64_TASK_GROUP_LEADER_OFFSET, offsetof (struct task_struct, group_leader)); 53 DEFINE(IA64_TASK_THREAD_PID_OFFSET,offsetof (struct task_struct, thread_pid));
54 DEFINE(IA64_TASK_TGIDLINK_OFFSET, offsetof (struct task_struct, pids[PIDTYPE_PID].pid));
55 DEFINE(IA64_PID_LEVEL_OFFSET, offsetof (struct pid, level)); 54 DEFINE(IA64_PID_LEVEL_OFFSET, offsetof (struct pid, level));
56 DEFINE(IA64_PID_UPID_OFFSET, offsetof (struct pid, numbers[0])); 55 DEFINE(IA64_PID_UPID_OFFSET, offsetof (struct pid, numbers[0]));
57 DEFINE(IA64_TASK_PENDING_OFFSET,offsetof (struct task_struct, pending)); 56 DEFINE(IA64_TASK_PENDING_OFFSET,offsetof (struct task_struct, pending));
@@ -68,6 +67,7 @@ void foo(void)
68 DEFINE(IA64_SIGNAL_GROUP_STOP_COUNT_OFFSET,offsetof (struct signal_struct, 67 DEFINE(IA64_SIGNAL_GROUP_STOP_COUNT_OFFSET,offsetof (struct signal_struct,
69 group_stop_count)); 68 group_stop_count));
70 DEFINE(IA64_SIGNAL_SHARED_PENDING_OFFSET,offsetof (struct signal_struct, shared_pending)); 69 DEFINE(IA64_SIGNAL_SHARED_PENDING_OFFSET,offsetof (struct signal_struct, shared_pending));
70 DEFINE(IA64_SIGNAL_PIDS_TGID_OFFSET, offsetof (struct signal_struct, pids[PIDTYPE_TGID]));
71 71
72 BLANK(); 72 BLANK();
73 73
diff --git a/arch/ia64/kernel/fsys.S b/arch/ia64/kernel/fsys.S
index fe742ffafc7a..d80c99a5f55d 100644
--- a/arch/ia64/kernel/fsys.S
+++ b/arch/ia64/kernel/fsys.S
@@ -62,16 +62,16 @@ ENTRY(fsys_getpid)
62 .prologue 62 .prologue
63 .altrp b6 63 .altrp b6
64 .body 64 .body
65 add r17=IA64_TASK_GROUP_LEADER_OFFSET,r16 65 add r17=IA64_TASK_SIGNAL_OFFSET,r16
66 ;; 66 ;;
67 ld8 r17=[r17] // r17 = current->group_leader 67 ld8 r17=[r17] // r17 = current->signal
68 add r9=TI_FLAGS+IA64_TASK_SIZE,r16 68 add r9=TI_FLAGS+IA64_TASK_SIZE,r16
69 ;; 69 ;;
70 ld4 r9=[r9] 70 ld4 r9=[r9]
71 add r17=IA64_TASK_TGIDLINK_OFFSET,r17 71 add r17=IA64_SIGNAL_PIDS_TGID_OFFSET,r17
72 ;; 72 ;;
73 and r9=TIF_ALLWORK_MASK,r9 73 and r9=TIF_ALLWORK_MASK,r9
74 ld8 r17=[r17] // r17 = current->group_leader->pids[PIDTYPE_PID].pid 74 ld8 r17=[r17] // r17 = current->signal->pids[PIDTYPE_TGID]
75 ;; 75 ;;
76 add r8=IA64_PID_LEVEL_OFFSET,r17 76 add r8=IA64_PID_LEVEL_OFFSET,r17
77 ;; 77 ;;
@@ -96,11 +96,11 @@ ENTRY(fsys_set_tid_address)
96 .altrp b6 96 .altrp b6
97 .body 97 .body
98 add r9=TI_FLAGS+IA64_TASK_SIZE,r16 98 add r9=TI_FLAGS+IA64_TASK_SIZE,r16
99 add r17=IA64_TASK_TGIDLINK_OFFSET,r16 99 add r17=IA64_TASK_THREAD_PID_OFFSET,r16
100 ;; 100 ;;
101 ld4 r9=[r9] 101 ld4 r9=[r9]
102 tnat.z p6,p7=r32 // check argument register for being NaT 102 tnat.z p6,p7=r32 // check argument register for being NaT
103 ld8 r17=[r17] // r17 = current->pids[PIDTYPE_PID].pid 103 ld8 r17=[r17] // r17 = current->thread_pid
104 ;; 104 ;;
105 and r9=TIF_ALLWORK_MASK,r9 105 and r9=TIF_ALLWORK_MASK,r9
106 add r8=IA64_PID_LEVEL_OFFSET,r17 106 add r8=IA64_PID_LEVEL_OFFSET,r17
diff --git a/arch/s390/kernel/perf_cpum_sf.c b/arch/s390/kernel/perf_cpum_sf.c
index cb198d4a6dca..5c53e977be62 100644
--- a/arch/s390/kernel/perf_cpum_sf.c
+++ b/arch/s390/kernel/perf_cpum_sf.c
@@ -665,7 +665,7 @@ static void cpumsf_output_event_pid(struct perf_event *event,
665 goto out; 665 goto out;
666 666
667 /* Update the process ID (see also kernel/events/core.c) */ 667 /* Update the process ID (see also kernel/events/core.c) */
668 data->tid_entry.pid = cpumsf_pid_type(event, pid, __PIDTYPE_TGID); 668 data->tid_entry.pid = cpumsf_pid_type(event, pid, PIDTYPE_TGID);
669 data->tid_entry.tid = cpumsf_pid_type(event, pid, PIDTYPE_PID); 669 data->tid_entry.tid = cpumsf_pid_type(event, pid, PIDTYPE_PID);
670 670
671 perf_output_sample(&handle, &header, data, event); 671 perf_output_sample(&handle, &header, data, event);
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 2bbefe828670..ebd07ad82431 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -3217,7 +3217,7 @@ static int tun_chr_fasync(int fd, struct file *file, int on)
3217 goto out; 3217 goto out;
3218 3218
3219 if (on) { 3219 if (on) {
3220 __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 3220 __f_setown(file, task_pid(current), PIDTYPE_TGID, 0);
3221 tfile->flags |= TUN_FASYNC; 3221 tfile->flags |= TUN_FASYNC;
3222 } else 3222 } else
3223 tfile->flags &= ~TUN_FASYNC; 3223 tfile->flags &= ~TUN_FASYNC;
diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c
index cae9b0595692..d556e95c532c 100644
--- a/drivers/platform/x86/thinkpad_acpi.c
+++ b/drivers/platform/x86/thinkpad_acpi.c
@@ -57,6 +57,7 @@
57#include <linux/list.h> 57#include <linux/list.h>
58#include <linux/mutex.h> 58#include <linux/mutex.h>
59#include <linux/sched.h> 59#include <linux/sched.h>
60#include <linux/sched/signal.h>
60#include <linux/kthread.h> 61#include <linux/kthread.h>
61#include <linux/freezer.h> 62#include <linux/freezer.h>
62#include <linux/delay.h> 63#include <linux/delay.h>
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
index 6364890575ec..06ed20dd01ba 100644
--- a/drivers/tty/sysrq.c
+++ b/drivers/tty/sysrq.c
@@ -348,7 +348,7 @@ static void send_sig_all(int sig)
348 if (is_global_init(p)) 348 if (is_global_init(p))
349 continue; 349 continue;
350 350
351 do_send_sig_info(sig, SEND_SIG_FORCED, p, true); 351 do_send_sig_info(sig, SEND_SIG_FORCED, p, PIDTYPE_MAX);
352 } 352 }
353 read_unlock(&tasklist_lock); 353 read_unlock(&tasklist_lock);
354} 354}
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 11c2df904ac9..32bc3e3fe4d3 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -2113,7 +2113,7 @@ static int __tty_fasync(int fd, struct file *filp, int on)
2113 type = PIDTYPE_PGID; 2113 type = PIDTYPE_PGID;
2114 } else { 2114 } else {
2115 pid = task_pid(current); 2115 pid = task_pid(current);
2116 type = PIDTYPE_PID; 2116 type = PIDTYPE_TGID;
2117 } 2117 }
2118 get_pid(pid); 2118 get_pid(pid);
2119 spin_unlock_irqrestore(&tty->ctrl_lock, flags); 2119 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
diff --git a/fs/autofs/autofs_i.h b/fs/autofs/autofs_i.h
index 9400a9f6318a..502812289850 100644
--- a/fs/autofs/autofs_i.h
+++ b/fs/autofs/autofs_i.h
@@ -18,6 +18,7 @@
18#include <linux/string.h> 18#include <linux/string.h>
19#include <linux/wait.h> 19#include <linux/wait.h>
20#include <linux/sched.h> 20#include <linux/sched.h>
21#include <linux/sched/signal.h>
21#include <linux/mount.h> 22#include <linux/mount.h>
22#include <linux/namei.h> 23#include <linux/namei.h>
23#include <linux/uaccess.h> 24#include <linux/uaccess.h>
diff --git a/fs/exec.c b/fs/exec.c
index bdd0eacefdf5..1ebf6e5a521d 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1145,6 +1145,7 @@ static int de_thread(struct task_struct *tsk)
1145 */ 1145 */
1146 tsk->pid = leader->pid; 1146 tsk->pid = leader->pid;
1147 change_pid(tsk, PIDTYPE_PID, task_pid(leader)); 1147 change_pid(tsk, PIDTYPE_PID, task_pid(leader));
1148 transfer_pid(leader, tsk, PIDTYPE_TGID);
1148 transfer_pid(leader, tsk, PIDTYPE_PGID); 1149 transfer_pid(leader, tsk, PIDTYPE_PGID);
1149 transfer_pid(leader, tsk, PIDTYPE_SID); 1150 transfer_pid(leader, tsk, PIDTYPE_SID);
1150 1151
diff --git a/fs/fcntl.c b/fs/fcntl.c
index 12273b6ea56d..4137d96534a6 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -116,7 +116,7 @@ int f_setown(struct file *filp, unsigned long arg, int force)
116 struct pid *pid = NULL; 116 struct pid *pid = NULL;
117 int who = arg, ret = 0; 117 int who = arg, ret = 0;
118 118
119 type = PIDTYPE_PID; 119 type = PIDTYPE_TGID;
120 if (who < 0) { 120 if (who < 0) {
121 /* avoid overflow below */ 121 /* avoid overflow below */
122 if (who == INT_MIN) 122 if (who == INT_MIN)
@@ -143,7 +143,7 @@ EXPORT_SYMBOL(f_setown);
143 143
144void f_delown(struct file *filp) 144void f_delown(struct file *filp)
145{ 145{
146 f_modown(filp, NULL, PIDTYPE_PID, 1); 146 f_modown(filp, NULL, PIDTYPE_TGID, 1);
147} 147}
148 148
149pid_t f_getown(struct file *filp) 149pid_t f_getown(struct file *filp)
@@ -171,11 +171,11 @@ static int f_setown_ex(struct file *filp, unsigned long arg)
171 171
172 switch (owner.type) { 172 switch (owner.type) {
173 case F_OWNER_TID: 173 case F_OWNER_TID:
174 type = PIDTYPE_MAX; 174 type = PIDTYPE_PID;
175 break; 175 break;
176 176
177 case F_OWNER_PID: 177 case F_OWNER_PID:
178 type = PIDTYPE_PID; 178 type = PIDTYPE_TGID;
179 break; 179 break;
180 180
181 case F_OWNER_PGRP: 181 case F_OWNER_PGRP:
@@ -206,11 +206,11 @@ static int f_getown_ex(struct file *filp, unsigned long arg)
206 read_lock(&filp->f_owner.lock); 206 read_lock(&filp->f_owner.lock);
207 owner.pid = pid_vnr(filp->f_owner.pid); 207 owner.pid = pid_vnr(filp->f_owner.pid);
208 switch (filp->f_owner.pid_type) { 208 switch (filp->f_owner.pid_type) {
209 case PIDTYPE_MAX: 209 case PIDTYPE_PID:
210 owner.type = F_OWNER_TID; 210 owner.type = F_OWNER_TID;
211 break; 211 break;
212 212
213 case PIDTYPE_PID: 213 case PIDTYPE_TGID:
214 owner.type = F_OWNER_PID; 214 owner.type = F_OWNER_PID;
215 break; 215 break;
216 216
@@ -723,7 +723,7 @@ static inline int sigio_perm(struct task_struct *p,
723 723
724static void send_sigio_to_task(struct task_struct *p, 724static void send_sigio_to_task(struct task_struct *p,
725 struct fown_struct *fown, 725 struct fown_struct *fown,
726 int fd, int reason, int group) 726 int fd, int reason, enum pid_type type)
727{ 727{
728 /* 728 /*
729 * F_SETSIG can change ->signum lockless in parallel, make 729 * F_SETSIG can change ->signum lockless in parallel, make
@@ -767,11 +767,11 @@ static void send_sigio_to_task(struct task_struct *p,
767 else 767 else
768 si.si_band = mangle_poll(band_table[reason - POLL_IN]); 768 si.si_band = mangle_poll(band_table[reason - POLL_IN]);
769 si.si_fd = fd; 769 si.si_fd = fd;
770 if (!do_send_sig_info(signum, &si, p, group)) 770 if (!do_send_sig_info(signum, &si, p, type))
771 break; 771 break;
772 /* fall-through: fall back on the old plain SIGIO signal */ 772 /* fall-through: fall back on the old plain SIGIO signal */
773 case 0: 773 case 0:
774 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group); 774 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, type);
775 } 775 }
776} 776}
777 777
@@ -780,34 +780,36 @@ void send_sigio(struct fown_struct *fown, int fd, int band)
780 struct task_struct *p; 780 struct task_struct *p;
781 enum pid_type type; 781 enum pid_type type;
782 struct pid *pid; 782 struct pid *pid;
783 int group = 1;
784 783
785 read_lock(&fown->lock); 784 read_lock(&fown->lock);
786 785
787 type = fown->pid_type; 786 type = fown->pid_type;
788 if (type == PIDTYPE_MAX) {
789 group = 0;
790 type = PIDTYPE_PID;
791 }
792
793 pid = fown->pid; 787 pid = fown->pid;
794 if (!pid) 788 if (!pid)
795 goto out_unlock_fown; 789 goto out_unlock_fown;
796 790
797 read_lock(&tasklist_lock); 791 if (type <= PIDTYPE_TGID) {
798 do_each_pid_task(pid, type, p) { 792 rcu_read_lock();
799 send_sigio_to_task(p, fown, fd, band, group); 793 p = pid_task(pid, PIDTYPE_PID);
800 } while_each_pid_task(pid, type, p); 794 if (p)
801 read_unlock(&tasklist_lock); 795 send_sigio_to_task(p, fown, fd, band, type);
796 rcu_read_unlock();
797 } else {
798 read_lock(&tasklist_lock);
799 do_each_pid_task(pid, type, p) {
800 send_sigio_to_task(p, fown, fd, band, type);
801 } while_each_pid_task(pid, type, p);
802 read_unlock(&tasklist_lock);
803 }
802 out_unlock_fown: 804 out_unlock_fown:
803 read_unlock(&fown->lock); 805 read_unlock(&fown->lock);
804} 806}
805 807
806static void send_sigurg_to_task(struct task_struct *p, 808static void send_sigurg_to_task(struct task_struct *p,
807 struct fown_struct *fown, int group) 809 struct fown_struct *fown, enum pid_type type)
808{ 810{
809 if (sigio_perm(p, fown, SIGURG)) 811 if (sigio_perm(p, fown, SIGURG))
810 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group); 812 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, type);
811} 813}
812 814
813int send_sigurg(struct fown_struct *fown) 815int send_sigurg(struct fown_struct *fown)
@@ -815,28 +817,30 @@ int send_sigurg(struct fown_struct *fown)
815 struct task_struct *p; 817 struct task_struct *p;
816 enum pid_type type; 818 enum pid_type type;
817 struct pid *pid; 819 struct pid *pid;
818 int group = 1;
819 int ret = 0; 820 int ret = 0;
820 821
821 read_lock(&fown->lock); 822 read_lock(&fown->lock);
822 823
823 type = fown->pid_type; 824 type = fown->pid_type;
824 if (type == PIDTYPE_MAX) {
825 group = 0;
826 type = PIDTYPE_PID;
827 }
828
829 pid = fown->pid; 825 pid = fown->pid;
830 if (!pid) 826 if (!pid)
831 goto out_unlock_fown; 827 goto out_unlock_fown;
832 828
833 ret = 1; 829 ret = 1;
834 830
835 read_lock(&tasklist_lock); 831 if (type <= PIDTYPE_TGID) {
836 do_each_pid_task(pid, type, p) { 832 rcu_read_lock();
837 send_sigurg_to_task(p, fown, group); 833 p = pid_task(pid, PIDTYPE_PID);
838 } while_each_pid_task(pid, type, p); 834 if (p)
839 read_unlock(&tasklist_lock); 835 send_sigurg_to_task(p, fown, type);
836 rcu_read_unlock();
837 } else {
838 read_lock(&tasklist_lock);
839 do_each_pid_task(pid, type, p) {
840 send_sigurg_to_task(p, fown, type);
841 } while_each_pid_task(pid, type, p);
842 read_unlock(&tasklist_lock);
843 }
840 out_unlock_fown: 844 out_unlock_fown:
841 read_unlock(&fown->lock); 845 read_unlock(&fown->lock);
842 return ret; 846 return ret;
diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index a201fb0ac64f..b00a3f126a89 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -12,6 +12,7 @@
12#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/kernel.h> 13#include <linux/kernel.h>
14#include <linux/sched.h> 14#include <linux/sched.h>
15#include <linux/sched/signal.h>
15#include <linux/module.h> 16#include <linux/module.h>
16#include <linux/compat.h> 17#include <linux/compat.h>
17#include <linux/swap.h> 18#include <linux/swap.h>
diff --git a/fs/locks.c b/fs/locks.c
index bc047a7edc47..5086bde5a18e 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -542,7 +542,7 @@ lease_setup(struct file_lock *fl, void **priv)
542 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) 542 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
543 *priv = NULL; 543 *priv = NULL;
544 544
545 __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 545 __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
546} 546}
547 547
548static const struct lock_manager_operations lease_manager_ops = { 548static const struct lock_manager_operations lease_manager_ops = {
diff --git a/fs/notify/dnotify/dnotify.c b/fs/notify/dnotify/dnotify.c
index a6365e6bc047..58d77dc696eb 100644
--- a/fs/notify/dnotify/dnotify.c
+++ b/fs/notify/dnotify/dnotify.c
@@ -19,6 +19,7 @@
19#include <linux/fs.h> 19#include <linux/fs.h>
20#include <linux/module.h> 20#include <linux/module.h>
21#include <linux/sched.h> 21#include <linux/sched.h>
22#include <linux/sched/signal.h>
22#include <linux/dnotify.h> 23#include <linux/dnotify.h>
23#include <linux/init.h> 24#include <linux/init.h>
24#include <linux/spinlock.h> 25#include <linux/spinlock.h>
@@ -353,7 +354,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
353 goto out; 354 goto out;
354 } 355 }
355 356
356 __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 357 __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
357 358
358 error = attach_dn(dn, dn_mark, id, fd, filp, mask); 359 error = attach_dn(dn, dn_mark, id, fd, filp, mask);
359 /* !error means that we attached the dn to the dn_mark, so don't free it */ 360 /* !error means that we attached the dn to the dn_mark, so don't free it */
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index eb4e75175cfb..94b52157bf8d 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -8,6 +8,7 @@
8#include <linux/mount.h> 8#include <linux/mount.h>
9#include <linux/sched.h> 9#include <linux/sched.h>
10#include <linux/sched/user.h> 10#include <linux/sched/user.h>
11#include <linux/sched/signal.h>
11#include <linux/types.h> 12#include <linux/types.h>
12#include <linux/wait.h> 13#include <linux/wait.h>
13#include <linux/audit.h> 14#include <linux/audit.h>
diff --git a/include/linux/init_task.h b/include/linux/init_task.h
index a454b8aeb938..a7083a45a26c 100644
--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -46,15 +46,6 @@ extern struct cred init_cred;
46#define INIT_CPU_TIMERS(s) 46#define INIT_CPU_TIMERS(s)
47#endif 47#endif
48 48
49#define INIT_PID_LINK(type) \
50{ \
51 .node = { \
52 .next = NULL, \
53 .pprev = NULL, \
54 }, \
55 .pid = &init_struct_pid, \
56}
57
58#define INIT_TASK_COMM "swapper" 49#define INIT_TASK_COMM "swapper"
59 50
60/* Attach to the init_task data structure for proper alignment */ 51/* Attach to the init_task data structure for proper alignment */
diff --git a/include/linux/pid.h b/include/linux/pid.h
index 7633d55d9a24..14a9a39da9c7 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -7,11 +7,10 @@
7enum pid_type 7enum pid_type
8{ 8{
9 PIDTYPE_PID, 9 PIDTYPE_PID,
10 PIDTYPE_TGID,
10 PIDTYPE_PGID, 11 PIDTYPE_PGID,
11 PIDTYPE_SID, 12 PIDTYPE_SID,
12 PIDTYPE_MAX, 13 PIDTYPE_MAX,
13 /* only valid to __task_pid_nr_ns() */
14 __PIDTYPE_TGID
15}; 14};
16 15
17/* 16/*
@@ -67,12 +66,6 @@ struct pid
67 66
68extern struct pid init_struct_pid; 67extern struct pid init_struct_pid;
69 68
70struct pid_link
71{
72 struct hlist_node node;
73 struct pid *pid;
74};
75
76static inline struct pid *get_pid(struct pid *pid) 69static inline struct pid *get_pid(struct pid *pid)
77{ 70{
78 if (pid) 71 if (pid)
@@ -177,7 +170,7 @@ pid_t pid_vnr(struct pid *pid);
177 do { \ 170 do { \
178 if ((pid) != NULL) \ 171 if ((pid) != NULL) \
179 hlist_for_each_entry_rcu((task), \ 172 hlist_for_each_entry_rcu((task), \
180 &(pid)->tasks[type], pids[type].node) { 173 &(pid)->tasks[type], pid_links[type]) {
181 174
182 /* 175 /*
183 * Both old and new leaders may be attached to 176 * Both old and new leaders may be attached to
diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
index 037bf0ef1ae9..4f36431c380b 100644
--- a/include/linux/ptrace.h
+++ b/include/linux/ptrace.h
@@ -214,8 +214,6 @@ static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
214 task_set_jobctl_pending(child, JOBCTL_TRAP_STOP); 214 task_set_jobctl_pending(child, JOBCTL_TRAP_STOP);
215 else 215 else
216 sigaddset(&child->pending.signal, SIGSTOP); 216 sigaddset(&child->pending.signal, SIGSTOP);
217
218 set_tsk_thread_flag(child, TIF_SIGPENDING);
219 } 217 }
220 else 218 else
221 child->ptracer_cred = NULL; 219 child->ptracer_cred = NULL;
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 789923fbee3a..00de3e950dd4 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -779,7 +779,8 @@ struct task_struct {
779 struct list_head ptrace_entry; 779 struct list_head ptrace_entry;
780 780
781 /* PID/PID hash table linkage. */ 781 /* PID/PID hash table linkage. */
782 struct pid_link pids[PIDTYPE_MAX]; 782 struct pid *thread_pid;
783 struct hlist_node pid_links[PIDTYPE_MAX];
783 struct list_head thread_group; 784 struct list_head thread_group;
784 struct list_head thread_node; 785 struct list_head thread_node;
785 786
@@ -1209,27 +1210,7 @@ struct task_struct {
1209 1210
1210static inline struct pid *task_pid(struct task_struct *task) 1211static inline struct pid *task_pid(struct task_struct *task)
1211{ 1212{
1212 return task->pids[PIDTYPE_PID].pid; 1213 return task->thread_pid;
1213}
1214
1215static inline struct pid *task_tgid(struct task_struct *task)
1216{
1217 return task->group_leader->pids[PIDTYPE_PID].pid;
1218}
1219
1220/*
1221 * Without tasklist or RCU lock it is not safe to dereference
1222 * the result of task_pgrp/task_session even if task == current,
1223 * we can race with another thread doing sys_setsid/sys_setpgid.
1224 */
1225static inline struct pid *task_pgrp(struct task_struct *task)
1226{
1227 return task->group_leader->pids[PIDTYPE_PGID].pid;
1228}
1229
1230static inline struct pid *task_session(struct task_struct *task)
1231{
1232 return task->group_leader->pids[PIDTYPE_SID].pid;
1233} 1214}
1234 1215
1235/* 1216/*
@@ -1278,7 +1259,7 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)
1278 */ 1259 */
1279static inline int pid_alive(const struct task_struct *p) 1260static inline int pid_alive(const struct task_struct *p)
1280{ 1261{
1281 return p->pids[PIDTYPE_PID].pid != NULL; 1262 return p->thread_pid != NULL;
1282} 1263}
1283 1264
1284static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns) 1265static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
@@ -1304,12 +1285,12 @@ static inline pid_t task_session_vnr(struct task_struct *tsk)
1304 1285
1305static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns) 1286static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1306{ 1287{
1307 return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, ns); 1288 return __task_pid_nr_ns(tsk, PIDTYPE_TGID, ns);
1308} 1289}
1309 1290
1310static inline pid_t task_tgid_vnr(struct task_struct *tsk) 1291static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1311{ 1292{
1312 return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, NULL); 1293 return __task_pid_nr_ns(tsk, PIDTYPE_TGID, NULL);
1313} 1294}
1314 1295
1315static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns) 1296static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index 113d1ad1ced7..4e9b77fb702d 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -69,6 +69,11 @@ struct thread_group_cputimer {
69 bool checking_timer; 69 bool checking_timer;
70}; 70};
71 71
72struct multiprocess_signals {
73 sigset_t signal;
74 struct hlist_node node;
75};
76
72/* 77/*
73 * NOTE! "signal_struct" does not have its own 78 * NOTE! "signal_struct" does not have its own
74 * locking, because a shared signal_struct always 79 * locking, because a shared signal_struct always
@@ -90,6 +95,9 @@ struct signal_struct {
90 /* shared signal handling: */ 95 /* shared signal handling: */
91 struct sigpending shared_pending; 96 struct sigpending shared_pending;
92 97
98 /* For collecting multiprocess signals during fork */
99 struct hlist_head multiprocess;
100
93 /* thread group exit support */ 101 /* thread group exit support */
94 int group_exit_code; 102 int group_exit_code;
95 /* overloaded: 103 /* overloaded:
@@ -146,7 +154,8 @@ struct signal_struct {
146 154
147#endif 155#endif
148 156
149 struct pid *leader_pid; 157 /* PID/PID hash table linkage. */
158 struct pid *pids[PIDTYPE_MAX];
150 159
151#ifdef CONFIG_NO_HZ_FULL 160#ifdef CONFIG_NO_HZ_FULL
152 atomic_t tick_dep_mask; 161 atomic_t tick_dep_mask;
@@ -329,7 +338,7 @@ extern int send_sig(int, struct task_struct *, int);
329extern int zap_other_threads(struct task_struct *p); 338extern int zap_other_threads(struct task_struct *p);
330extern struct sigqueue *sigqueue_alloc(void); 339extern struct sigqueue *sigqueue_alloc(void);
331extern void sigqueue_free(struct sigqueue *); 340extern void sigqueue_free(struct sigqueue *);
332extern int send_sigqueue(struct sigqueue *, struct task_struct *, int group); 341extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type);
333extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *); 342extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
334 343
335static inline int restart_syscall(void) 344static inline int restart_syscall(void)
@@ -371,6 +380,7 @@ static inline int signal_pending_state(long state, struct task_struct *p)
371 */ 380 */
372extern void recalc_sigpending_and_wake(struct task_struct *t); 381extern void recalc_sigpending_and_wake(struct task_struct *t);
373extern void recalc_sigpending(void); 382extern void recalc_sigpending(void);
383extern void calculate_sigpending(void);
374 384
375extern void signal_wake_up_state(struct task_struct *t, unsigned int state); 385extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
376 386
@@ -383,6 +393,8 @@ static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
383 signal_wake_up_state(t, resume ? __TASK_TRACED : 0); 393 signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
384} 394}
385 395
396void task_join_group_stop(struct task_struct *task);
397
386#ifdef TIF_RESTORE_SIGMASK 398#ifdef TIF_RESTORE_SIGMASK
387/* 399/*
388 * Legacy restore_sigmask accessors. These are inefficient on 400 * Legacy restore_sigmask accessors. These are inefficient on
@@ -556,6 +568,37 @@ extern bool current_is_single_threaded(void);
556typedef int (*proc_visitor)(struct task_struct *p, void *data); 568typedef int (*proc_visitor)(struct task_struct *p, void *data);
557void walk_process_tree(struct task_struct *top, proc_visitor, void *); 569void walk_process_tree(struct task_struct *top, proc_visitor, void *);
558 570
571static inline
572struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
573{
574 struct pid *pid;
575 if (type == PIDTYPE_PID)
576 pid = task_pid(task);
577 else
578 pid = task->signal->pids[type];
579 return pid;
580}
581
582static inline struct pid *task_tgid(struct task_struct *task)
583{
584 return task->signal->pids[PIDTYPE_TGID];
585}
586
587/*
588 * Without tasklist or RCU lock it is not safe to dereference
589 * the result of task_pgrp/task_session even if task == current,
590 * we can race with another thread doing sys_setsid/sys_setpgid.
591 */
592static inline struct pid *task_pgrp(struct task_struct *task)
593{
594 return task->signal->pids[PIDTYPE_PGID];
595}
596
597static inline struct pid *task_session(struct task_struct *task)
598{
599 return task->signal->pids[PIDTYPE_SID];
600}
601
559static inline int get_nr_threads(struct task_struct *tsk) 602static inline int get_nr_threads(struct task_struct *tsk)
560{ 603{
561 return tsk->signal->nr_threads; 604 return tsk->signal->nr_threads;
@@ -574,7 +617,7 @@ static inline bool thread_group_leader(struct task_struct *p)
574 */ 617 */
575static inline bool has_group_leader_pid(struct task_struct *p) 618static inline bool has_group_leader_pid(struct task_struct *p)
576{ 619{
577 return task_pid(p) == p->signal->leader_pid; 620 return task_pid(p) == task_tgid(p);
578} 621}
579 622
580static inline 623static inline
diff --git a/include/linux/signal.h b/include/linux/signal.h
index 3c5200137b24..fe125b0335f7 100644
--- a/include/linux/signal.h
+++ b/include/linux/signal.h
@@ -254,11 +254,13 @@ static inline int valid_signal(unsigned long sig)
254 254
255struct timespec; 255struct timespec;
256struct pt_regs; 256struct pt_regs;
257enum pid_type;
257 258
258extern int next_signal(struct sigpending *pending, sigset_t *mask); 259extern int next_signal(struct sigpending *pending, sigset_t *mask);
259extern int do_send_sig_info(int sig, struct siginfo *info, 260extern int do_send_sig_info(int sig, struct siginfo *info,
260 struct task_struct *p, bool group); 261 struct task_struct *p, enum pid_type type);
261extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p); 262extern int group_send_sig_info(int sig, struct siginfo *info,
263 struct task_struct *p, enum pid_type type);
262extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *); 264extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
263extern int sigprocmask(int, sigset_t *, sigset_t *); 265extern int sigprocmask(int, sigset_t *, sigset_t *);
264extern void set_current_blocked(sigset_t *); 266extern void set_current_blocked(sigset_t *);
diff --git a/include/net/scm.h b/include/net/scm.h
index 903771c8d4e3..1ce365f4c256 100644
--- a/include/net/scm.h
+++ b/include/net/scm.h
@@ -8,6 +8,7 @@
8#include <linux/security.h> 8#include <linux/security.h>
9#include <linux/pid.h> 9#include <linux/pid.h>
10#include <linux/nsproxy.h> 10#include <linux/nsproxy.h>
11#include <linux/sched/signal.h>
11 12
12/* Well, we should have at least one descriptor open 13/* Well, we should have at least one descriptor open
13 * to accept passed FDs 8) 14 * to accept passed FDs 8)
diff --git a/init/init_task.c b/init/init_task.c
index 74f60baa2799..5aebe3be4d7c 100644
--- a/init/init_task.c
+++ b/init/init_task.c
@@ -22,6 +22,7 @@ static struct signal_struct init_signals = {
22 .list = LIST_HEAD_INIT(init_signals.shared_pending.list), 22 .list = LIST_HEAD_INIT(init_signals.shared_pending.list),
23 .signal = {{0}} 23 .signal = {{0}}
24 }, 24 },
25 .multiprocess = HLIST_HEAD_INIT,
25 .rlim = INIT_RLIMITS, 26 .rlim = INIT_RLIMITS,
26 .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), 27 .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex),
27#ifdef CONFIG_POSIX_TIMERS 28#ifdef CONFIG_POSIX_TIMERS
@@ -33,6 +34,12 @@ static struct signal_struct init_signals = {
33 }, 34 },
34#endif 35#endif
35 INIT_CPU_TIMERS(init_signals) 36 INIT_CPU_TIMERS(init_signals)
37 .pids = {
38 [PIDTYPE_PID] = &init_struct_pid,
39 [PIDTYPE_TGID] = &init_struct_pid,
40 [PIDTYPE_PGID] = &init_struct_pid,
41 [PIDTYPE_SID] = &init_struct_pid,
42 },
36 INIT_PREV_CPUTIME(init_signals) 43 INIT_PREV_CPUTIME(init_signals)
37}; 44};
38 45
@@ -111,11 +118,7 @@ struct task_struct init_task
111 INIT_CPU_TIMERS(init_task) 118 INIT_CPU_TIMERS(init_task)
112 .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(init_task.pi_lock), 119 .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(init_task.pi_lock),
113 .timer_slack_ns = 50000, /* 50 usec default slack */ 120 .timer_slack_ns = 50000, /* 50 usec default slack */
114 .pids = { 121 .thread_pid = &init_struct_pid,
115 [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID),
116 [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID),
117 [PIDTYPE_SID] = INIT_PID_LINK(PIDTYPE_SID),
118 },
119 .thread_group = LIST_HEAD_INIT(init_task.thread_group), 122 .thread_group = LIST_HEAD_INIT(init_task.thread_group),
120 .thread_node = LIST_HEAD_INIT(init_signals.thread_head), 123 .thread_node = LIST_HEAD_INIT(init_signals.thread_head),
121#ifdef CONFIG_AUDITSYSCALL 124#ifdef CONFIG_AUDITSYSCALL
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 80f456ec5d89..2a62b96600ad 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1334,7 +1334,7 @@ static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
1334 1334
1335static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) 1335static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1336{ 1336{
1337 return perf_event_pid_type(event, p, __PIDTYPE_TGID); 1337 return perf_event_pid_type(event, p, PIDTYPE_TGID);
1338} 1338}
1339 1339
1340static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) 1340static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
diff --git a/kernel/exit.c b/kernel/exit.c
index c3c7ac560114..0e21e6d21f35 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -73,6 +73,7 @@ static void __unhash_process(struct task_struct *p, bool group_dead)
73 nr_threads--; 73 nr_threads--;
74 detach_pid(p, PIDTYPE_PID); 74 detach_pid(p, PIDTYPE_PID);
75 if (group_dead) { 75 if (group_dead) {
76 detach_pid(p, PIDTYPE_TGID);
76 detach_pid(p, PIDTYPE_PGID); 77 detach_pid(p, PIDTYPE_PGID);
77 detach_pid(p, PIDTYPE_SID); 78 detach_pid(p, PIDTYPE_SID);
78 79
@@ -680,7 +681,8 @@ static void forget_original_parent(struct task_struct *father,
680 t->parent = t->real_parent; 681 t->parent = t->real_parent;
681 if (t->pdeath_signal) 682 if (t->pdeath_signal)
682 group_send_sig_info(t->pdeath_signal, 683 group_send_sig_info(t->pdeath_signal,
683 SEND_SIG_NOINFO, t); 684 SEND_SIG_NOINFO, t,
685 PIDTYPE_TGID);
684 } 686 }
685 /* 687 /*
686 * If this is a threaded reparent there is no need to 688 * If this is a threaded reparent there is no need to
@@ -1001,14 +1003,6 @@ struct wait_opts {
1001 int notask_error; 1003 int notask_error;
1002}; 1004};
1003 1005
1004static inline
1005struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
1006{
1007 if (type != PIDTYPE_PID)
1008 task = task->group_leader;
1009 return task->pids[type].pid;
1010}
1011
1012static int eligible_pid(struct wait_opts *wo, struct task_struct *p) 1006static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
1013{ 1007{
1014 return wo->wo_type == PIDTYPE_MAX || 1008 return wo->wo_type == PIDTYPE_MAX ||
diff --git a/kernel/fork.c b/kernel/fork.c
index 5ee74c113381..ff5037be7771 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1487,6 +1487,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
1487 init_waitqueue_head(&sig->wait_chldexit); 1487 init_waitqueue_head(&sig->wait_chldexit);
1488 sig->curr_target = tsk; 1488 sig->curr_target = tsk;
1489 init_sigpending(&sig->shared_pending); 1489 init_sigpending(&sig->shared_pending);
1490 INIT_HLIST_HEAD(&sig->multiprocess);
1490 seqlock_init(&sig->stats_lock); 1491 seqlock_init(&sig->stats_lock);
1491 prev_cputime_init(&sig->prev_cputime); 1492 prev_cputime_init(&sig->prev_cputime);
1492 1493
@@ -1580,10 +1581,22 @@ static void posix_cpu_timers_init(struct task_struct *tsk)
1580static inline void posix_cpu_timers_init(struct task_struct *tsk) { } 1581static inline void posix_cpu_timers_init(struct task_struct *tsk) { }
1581#endif 1582#endif
1582 1583
1584static inline void init_task_pid_links(struct task_struct *task)
1585{
1586 enum pid_type type;
1587
1588 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
1589 INIT_HLIST_NODE(&task->pid_links[type]);
1590 }
1591}
1592
1583static inline void 1593static inline void
1584init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 1594init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
1585{ 1595{
1586 task->pids[type].pid = pid; 1596 if (type == PIDTYPE_PID)
1597 task->thread_pid = pid;
1598 else
1599 task->signal->pids[type] = pid;
1587} 1600}
1588 1601
1589static inline void rcu_copy_process(struct task_struct *p) 1602static inline void rcu_copy_process(struct task_struct *p)
@@ -1621,6 +1634,7 @@ static __latent_entropy struct task_struct *copy_process(
1621{ 1634{
1622 int retval; 1635 int retval;
1623 struct task_struct *p; 1636 struct task_struct *p;
1637 struct multiprocess_signals delayed;
1624 1638
1625 /* 1639 /*
1626 * Don't allow sharing the root directory with processes in a different 1640 * Don't allow sharing the root directory with processes in a different
@@ -1668,6 +1682,24 @@ static __latent_entropy struct task_struct *copy_process(
1668 return ERR_PTR(-EINVAL); 1682 return ERR_PTR(-EINVAL);
1669 } 1683 }
1670 1684
1685 /*
1686 * Force any signals received before this point to be delivered
1687 * before the fork happens. Collect up signals sent to multiple
1688 * processes that happen during the fork and delay them so that
1689 * they appear to happen after the fork.
1690 */
1691 sigemptyset(&delayed.signal);
1692 INIT_HLIST_NODE(&delayed.node);
1693
1694 spin_lock_irq(&current->sighand->siglock);
1695 if (!(clone_flags & CLONE_THREAD))
1696 hlist_add_head(&delayed.node, &current->signal->multiprocess);
1697 recalc_sigpending();
1698 spin_unlock_irq(&current->sighand->siglock);
1699 retval = -ERESTARTNOINTR;
1700 if (signal_pending(current))
1701 goto fork_out;
1702
1671 retval = -ENOMEM; 1703 retval = -ENOMEM;
1672 p = dup_task_struct(current, node); 1704 p = dup_task_struct(current, node);
1673 if (!p) 1705 if (!p)
@@ -1941,29 +1973,26 @@ static __latent_entropy struct task_struct *copy_process(
1941 1973
1942 rseq_fork(p, clone_flags); 1974 rseq_fork(p, clone_flags);
1943 1975
1944 /* 1976 /* Don't start children in a dying pid namespace */
1945 * Process group and session signals need to be delivered to just the
1946 * parent before the fork or both the parent and the child after the
1947 * fork. Restart if a signal comes in before we add the new process to
1948 * it's process group.
1949 * A fatal signal pending means that current will exit, so the new
1950 * thread can't slip out of an OOM kill (or normal SIGKILL).
1951 */
1952 recalc_sigpending();
1953 if (signal_pending(current)) {
1954 retval = -ERESTARTNOINTR;
1955 goto bad_fork_cancel_cgroup;
1956 }
1957 if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { 1977 if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {
1958 retval = -ENOMEM; 1978 retval = -ENOMEM;
1959 goto bad_fork_cancel_cgroup; 1979 goto bad_fork_cancel_cgroup;
1960 } 1980 }
1961 1981
1982 /* Let kill terminate clone/fork in the middle */
1983 if (fatal_signal_pending(current)) {
1984 retval = -EINTR;
1985 goto bad_fork_cancel_cgroup;
1986 }
1987
1988
1989 init_task_pid_links(p);
1962 if (likely(p->pid)) { 1990 if (likely(p->pid)) {
1963 ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 1991 ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
1964 1992
1965 init_task_pid(p, PIDTYPE_PID, pid); 1993 init_task_pid(p, PIDTYPE_PID, pid);
1966 if (thread_group_leader(p)) { 1994 if (thread_group_leader(p)) {
1995 init_task_pid(p, PIDTYPE_TGID, pid);
1967 init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 1996 init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
1968 init_task_pid(p, PIDTYPE_SID, task_session(current)); 1997 init_task_pid(p, PIDTYPE_SID, task_session(current));
1969 1998
@@ -1971,8 +2000,7 @@ static __latent_entropy struct task_struct *copy_process(
1971 ns_of_pid(pid)->child_reaper = p; 2000 ns_of_pid(pid)->child_reaper = p;
1972 p->signal->flags |= SIGNAL_UNKILLABLE; 2001 p->signal->flags |= SIGNAL_UNKILLABLE;
1973 } 2002 }
1974 2003 p->signal->shared_pending.signal = delayed.signal;
1975 p->signal->leader_pid = pid;
1976 p->signal->tty = tty_kref_get(current->signal->tty); 2004 p->signal->tty = tty_kref_get(current->signal->tty);
1977 /* 2005 /*
1978 * Inherit has_child_subreaper flag under the same 2006 * Inherit has_child_subreaper flag under the same
@@ -1983,6 +2011,7 @@ static __latent_entropy struct task_struct *copy_process(
1983 p->real_parent->signal->is_child_subreaper; 2011 p->real_parent->signal->is_child_subreaper;
1984 list_add_tail(&p->sibling, &p->real_parent->children); 2012 list_add_tail(&p->sibling, &p->real_parent->children);
1985 list_add_tail_rcu(&p->tasks, &init_task.tasks); 2013 list_add_tail_rcu(&p->tasks, &init_task.tasks);
2014 attach_pid(p, PIDTYPE_TGID);
1986 attach_pid(p, PIDTYPE_PGID); 2015 attach_pid(p, PIDTYPE_PGID);
1987 attach_pid(p, PIDTYPE_SID); 2016 attach_pid(p, PIDTYPE_SID);
1988 __this_cpu_inc(process_counts); 2017 __this_cpu_inc(process_counts);
@@ -1990,6 +2019,7 @@ static __latent_entropy struct task_struct *copy_process(
1990 current->signal->nr_threads++; 2019 current->signal->nr_threads++;
1991 atomic_inc(&current->signal->live); 2020 atomic_inc(&current->signal->live);
1992 atomic_inc(&current->signal->sigcnt); 2021 atomic_inc(&current->signal->sigcnt);
2022 task_join_group_stop(p);
1993 list_add_tail_rcu(&p->thread_group, 2023 list_add_tail_rcu(&p->thread_group,
1994 &p->group_leader->thread_group); 2024 &p->group_leader->thread_group);
1995 list_add_tail_rcu(&p->thread_node, 2025 list_add_tail_rcu(&p->thread_node,
@@ -1998,8 +2028,8 @@ static __latent_entropy struct task_struct *copy_process(
1998 attach_pid(p, PIDTYPE_PID); 2028 attach_pid(p, PIDTYPE_PID);
1999 nr_threads++; 2029 nr_threads++;
2000 } 2030 }
2001
2002 total_forks++; 2031 total_forks++;
2032 hlist_del_init(&delayed.node);
2003 spin_unlock(&current->sighand->siglock); 2033 spin_unlock(&current->sighand->siglock);
2004 syscall_tracepoint_update(p); 2034 syscall_tracepoint_update(p);
2005 write_unlock_irq(&tasklist_lock); 2035 write_unlock_irq(&tasklist_lock);
@@ -2064,16 +2094,19 @@ bad_fork_free:
2064 put_task_stack(p); 2094 put_task_stack(p);
2065 free_task(p); 2095 free_task(p);
2066fork_out: 2096fork_out:
2097 spin_lock_irq(&current->sighand->siglock);
2098 hlist_del_init(&delayed.node);
2099 spin_unlock_irq(&current->sighand->siglock);
2067 return ERR_PTR(retval); 2100 return ERR_PTR(retval);
2068} 2101}
2069 2102
2070static inline void init_idle_pids(struct pid_link *links) 2103static inline void init_idle_pids(struct task_struct *idle)
2071{ 2104{
2072 enum pid_type type; 2105 enum pid_type type;
2073 2106
2074 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 2107 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
2075 INIT_HLIST_NODE(&links[type].node); /* not really needed */ 2108 INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */
2076 links[type].pid = &init_struct_pid; 2109 init_task_pid(idle, type, &init_struct_pid);
2077 } 2110 }
2078} 2111}
2079 2112
@@ -2083,7 +2116,7 @@ struct task_struct *fork_idle(int cpu)
2083 task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0, 2116 task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0,
2084 cpu_to_node(cpu)); 2117 cpu_to_node(cpu));
2085 if (!IS_ERR(task)) { 2118 if (!IS_ERR(task)) {
2086 init_idle_pids(task->pids); 2119 init_idle_pids(task);
2087 init_idle(task, cpu); 2120 init_idle(task, cpu);
2088 } 2121 }
2089 2122
diff --git a/kernel/pid.c b/kernel/pid.c
index 157fe4b19971..de1cfc4f75a2 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -265,27 +265,33 @@ struct pid *find_vpid(int nr)
265} 265}
266EXPORT_SYMBOL_GPL(find_vpid); 266EXPORT_SYMBOL_GPL(find_vpid);
267 267
268static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
269{
270 return (type == PIDTYPE_PID) ?
271 &task->thread_pid :
272 &task->signal->pids[type];
273}
274
268/* 275/*
269 * attach_pid() must be called with the tasklist_lock write-held. 276 * attach_pid() must be called with the tasklist_lock write-held.
270 */ 277 */
271void attach_pid(struct task_struct *task, enum pid_type type) 278void attach_pid(struct task_struct *task, enum pid_type type)
272{ 279{
273 struct pid_link *link = &task->pids[type]; 280 struct pid *pid = *task_pid_ptr(task, type);
274 hlist_add_head_rcu(&link->node, &link->pid->tasks[type]); 281 hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]);
275} 282}
276 283
277static void __change_pid(struct task_struct *task, enum pid_type type, 284static void __change_pid(struct task_struct *task, enum pid_type type,
278 struct pid *new) 285 struct pid *new)
279{ 286{
280 struct pid_link *link; 287 struct pid **pid_ptr = task_pid_ptr(task, type);
281 struct pid *pid; 288 struct pid *pid;
282 int tmp; 289 int tmp;
283 290
284 link = &task->pids[type]; 291 pid = *pid_ptr;
285 pid = link->pid;
286 292
287 hlist_del_rcu(&link->node); 293 hlist_del_rcu(&task->pid_links[type]);
288 link->pid = new; 294 *pid_ptr = new;
289 295
290 for (tmp = PIDTYPE_MAX; --tmp >= 0; ) 296 for (tmp = PIDTYPE_MAX; --tmp >= 0; )
291 if (!hlist_empty(&pid->tasks[tmp])) 297 if (!hlist_empty(&pid->tasks[tmp]))
@@ -310,8 +316,9 @@ void change_pid(struct task_struct *task, enum pid_type type,
310void transfer_pid(struct task_struct *old, struct task_struct *new, 316void transfer_pid(struct task_struct *old, struct task_struct *new,
311 enum pid_type type) 317 enum pid_type type)
312{ 318{
313 new->pids[type].pid = old->pids[type].pid; 319 if (type == PIDTYPE_PID)
314 hlist_replace_rcu(&old->pids[type].node, &new->pids[type].node); 320 new->thread_pid = old->thread_pid;
321 hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]);
315} 322}
316 323
317struct task_struct *pid_task(struct pid *pid, enum pid_type type) 324struct task_struct *pid_task(struct pid *pid, enum pid_type type)
@@ -322,7 +329,7 @@ struct task_struct *pid_task(struct pid *pid, enum pid_type type)
322 first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]), 329 first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
323 lockdep_tasklist_lock_is_held()); 330 lockdep_tasklist_lock_is_held());
324 if (first) 331 if (first)
325 result = hlist_entry(first, struct task_struct, pids[(type)].node); 332 result = hlist_entry(first, struct task_struct, pid_links[(type)]);
326 } 333 }
327 return result; 334 return result;
328} 335}
@@ -360,9 +367,7 @@ struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
360{ 367{
361 struct pid *pid; 368 struct pid *pid;
362 rcu_read_lock(); 369 rcu_read_lock();
363 if (type != PIDTYPE_PID) 370 pid = get_pid(rcu_dereference(*task_pid_ptr(task, type)));
364 task = task->group_leader;
365 pid = get_pid(rcu_dereference(task->pids[type].pid));
366 rcu_read_unlock(); 371 rcu_read_unlock();
367 return pid; 372 return pid;
368} 373}
@@ -420,15 +425,8 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
420 rcu_read_lock(); 425 rcu_read_lock();
421 if (!ns) 426 if (!ns)
422 ns = task_active_pid_ns(current); 427 ns = task_active_pid_ns(current);
423 if (likely(pid_alive(task))) { 428 if (likely(pid_alive(task)))
424 if (type != PIDTYPE_PID) { 429 nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
425 if (type == __PIDTYPE_TGID)
426 type = PIDTYPE_PID;
427
428 task = task->group_leader;
429 }
430 nr = pid_nr_ns(rcu_dereference(task->pids[type].pid), ns);
431 }
432 rcu_read_unlock(); 430 rcu_read_unlock();
433 431
434 return nr; 432 return nr;
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index c89302ddf807..625bc9897f62 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2774,6 +2774,8 @@ asmlinkage __visible void schedule_tail(struct task_struct *prev)
2774 2774
2775 if (current->set_child_tid) 2775 if (current->set_child_tid)
2776 put_user(task_pid_vnr(current), current->set_child_tid); 2776 put_user(task_pid_vnr(current), current->set_child_tid);
2777
2778 calculate_sigpending();
2777} 2779}
2778 2780
2779/* 2781/*
diff --git a/kernel/signal.c b/kernel/signal.c
index 8d8a940422a8..cfa9d10e731a 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -172,6 +172,17 @@ void recalc_sigpending(void)
172 172
173} 173}
174 174
175void calculate_sigpending(void)
176{
177 /* Have any signals or users of TIF_SIGPENDING been delayed
178 * until after fork?
179 */
180 spin_lock_irq(&current->sighand->siglock);
181 set_tsk_thread_flag(current, TIF_SIGPENDING);
182 recalc_sigpending();
183 spin_unlock_irq(&current->sighand->siglock);
184}
185
175/* Given the mask, find the first available signal that should be serviced. */ 186/* Given the mask, find the first available signal that should be serviced. */
176 187
177#define SYNCHRONOUS_MASK \ 188#define SYNCHRONOUS_MASK \
@@ -362,6 +373,20 @@ static bool task_participate_group_stop(struct task_struct *task)
362 return false; 373 return false;
363} 374}
364 375
376void task_join_group_stop(struct task_struct *task)
377{
378 /* Have the new thread join an on-going signal group stop */
379 unsigned long jobctl = current->jobctl;
380 if (jobctl & JOBCTL_STOP_PENDING) {
381 struct signal_struct *sig = current->signal;
382 unsigned long signr = jobctl & JOBCTL_STOP_SIGMASK;
383 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
384 if (task_set_jobctl_pending(task, signr | gstop)) {
385 sig->group_stop_count++;
386 }
387 }
388}
389
365/* 390/*
366 * allocate a new signal queue record 391 * allocate a new signal queue record
367 * - this may be called without locks if and only if t == current, otherwise an 392 * - this may be called without locks if and only if t == current, otherwise an
@@ -895,7 +920,7 @@ static inline int wants_signal(int sig, struct task_struct *p)
895 return task_curr(p) || !signal_pending(p); 920 return task_curr(p) || !signal_pending(p);
896} 921}
897 922
898static void complete_signal(int sig, struct task_struct *p, int group) 923static void complete_signal(int sig, struct task_struct *p, enum pid_type type)
899{ 924{
900 struct signal_struct *signal = p->signal; 925 struct signal_struct *signal = p->signal;
901 struct task_struct *t; 926 struct task_struct *t;
@@ -908,7 +933,7 @@ static void complete_signal(int sig, struct task_struct *p, int group)
908 */ 933 */
909 if (wants_signal(sig, p)) 934 if (wants_signal(sig, p))
910 t = p; 935 t = p;
911 else if (!group || thread_group_empty(p)) 936 else if ((type == PIDTYPE_PID) || thread_group_empty(p))
912 /* 937 /*
913 * There is just one thread and it does not need to be woken. 938 * There is just one thread and it does not need to be woken.
914 * It will dequeue unblocked signals before it runs again. 939 * It will dequeue unblocked signals before it runs again.
@@ -998,7 +1023,7 @@ static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_str
998#endif 1023#endif
999 1024
1000static int __send_signal(int sig, struct siginfo *info, struct task_struct *t, 1025static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
1001 int group, int from_ancestor_ns) 1026 enum pid_type type, int from_ancestor_ns)
1002{ 1027{
1003 struct sigpending *pending; 1028 struct sigpending *pending;
1004 struct sigqueue *q; 1029 struct sigqueue *q;
@@ -1012,7 +1037,7 @@ static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
1012 from_ancestor_ns || (info == SEND_SIG_FORCED))) 1037 from_ancestor_ns || (info == SEND_SIG_FORCED)))
1013 goto ret; 1038 goto ret;
1014 1039
1015 pending = group ? &t->signal->shared_pending : &t->pending; 1040 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1016 /* 1041 /*
1017 * Short-circuit ignored signals and support queuing 1042 * Short-circuit ignored signals and support queuing
1018 * exactly one non-rt signal, so that we can get more 1043 * exactly one non-rt signal, so that we can get more
@@ -1096,14 +1121,29 @@ static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
1096out_set: 1121out_set:
1097 signalfd_notify(t, sig); 1122 signalfd_notify(t, sig);
1098 sigaddset(&pending->signal, sig); 1123 sigaddset(&pending->signal, sig);
1099 complete_signal(sig, t, group); 1124
1125 /* Let multiprocess signals appear after on-going forks */
1126 if (type > PIDTYPE_TGID) {
1127 struct multiprocess_signals *delayed;
1128 hlist_for_each_entry(delayed, &t->signal->multiprocess, node) {
1129 sigset_t *signal = &delayed->signal;
1130 /* Can't queue both a stop and a continue signal */
1131 if (sig == SIGCONT)
1132 sigdelsetmask(signal, SIG_KERNEL_STOP_MASK);
1133 else if (sig_kernel_stop(sig))
1134 sigdelset(signal, SIGCONT);
1135 sigaddset(signal, sig);
1136 }
1137 }
1138
1139 complete_signal(sig, t, type);
1100ret: 1140ret:
1101 trace_signal_generate(sig, info, t, group, result); 1141 trace_signal_generate(sig, info, t, type != PIDTYPE_PID, result);
1102 return ret; 1142 return ret;
1103} 1143}
1104 1144
1105static int send_signal(int sig, struct siginfo *info, struct task_struct *t, 1145static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1106 int group) 1146 enum pid_type type)
1107{ 1147{
1108 int from_ancestor_ns = 0; 1148 int from_ancestor_ns = 0;
1109 1149
@@ -1112,7 +1152,7 @@ static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1112 !task_pid_nr_ns(current, task_active_pid_ns(t)); 1152 !task_pid_nr_ns(current, task_active_pid_ns(t));
1113#endif 1153#endif
1114 1154
1115 return __send_signal(sig, info, t, group, from_ancestor_ns); 1155 return __send_signal(sig, info, t, type, from_ancestor_ns);
1116} 1156}
1117 1157
1118static void print_fatal_signal(int signr) 1158static void print_fatal_signal(int signr)
@@ -1151,23 +1191,23 @@ __setup("print-fatal-signals=", setup_print_fatal_signals);
1151int 1191int
1152__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p) 1192__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1153{ 1193{
1154 return send_signal(sig, info, p, 1); 1194 return send_signal(sig, info, p, PIDTYPE_TGID);
1155} 1195}
1156 1196
1157static int 1197static int
1158specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t) 1198specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1159{ 1199{
1160 return send_signal(sig, info, t, 0); 1200 return send_signal(sig, info, t, PIDTYPE_PID);
1161} 1201}
1162 1202
1163int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p, 1203int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1164 bool group) 1204 enum pid_type type)
1165{ 1205{
1166 unsigned long flags; 1206 unsigned long flags;
1167 int ret = -ESRCH; 1207 int ret = -ESRCH;
1168 1208
1169 if (lock_task_sighand(p, &flags)) { 1209 if (lock_task_sighand(p, &flags)) {
1170 ret = send_signal(sig, info, p, group); 1210 ret = send_signal(sig, info, p, type);
1171 unlock_task_sighand(p, &flags); 1211 unlock_task_sighand(p, &flags);
1172 } 1212 }
1173 1213
@@ -1274,7 +1314,8 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1274/* 1314/*
1275 * send signal info to all the members of a group 1315 * send signal info to all the members of a group
1276 */ 1316 */
1277int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p) 1317int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1318 enum pid_type type)
1278{ 1319{
1279 int ret; 1320 int ret;
1280 1321
@@ -1283,7 +1324,7 @@ int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1283 rcu_read_unlock(); 1324 rcu_read_unlock();
1284 1325
1285 if (!ret && sig) 1326 if (!ret && sig)
1286 ret = do_send_sig_info(sig, info, p, true); 1327 ret = do_send_sig_info(sig, info, p, type);
1287 1328
1288 return ret; 1329 return ret;
1289} 1330}
@@ -1301,7 +1342,7 @@ int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1301 success = 0; 1342 success = 0;
1302 retval = -ESRCH; 1343 retval = -ESRCH;
1303 do_each_pid_task(pgrp, PIDTYPE_PGID, p) { 1344 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1304 int err = group_send_sig_info(sig, info, p); 1345 int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID);
1305 success |= !err; 1346 success |= !err;
1306 retval = err; 1347 retval = err;
1307 } while_each_pid_task(pgrp, PIDTYPE_PGID, p); 1348 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
@@ -1317,7 +1358,7 @@ int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1317 rcu_read_lock(); 1358 rcu_read_lock();
1318 p = pid_task(pid, PIDTYPE_PID); 1359 p = pid_task(pid, PIDTYPE_PID);
1319 if (p) 1360 if (p)
1320 error = group_send_sig_info(sig, info, p); 1361 error = group_send_sig_info(sig, info, p, PIDTYPE_TGID);
1321 rcu_read_unlock(); 1362 rcu_read_unlock();
1322 if (likely(!p || error != -ESRCH)) 1363 if (likely(!p || error != -ESRCH))
1323 return error; 1364 return error;
@@ -1376,7 +1417,7 @@ int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
1376 1417
1377 if (sig) { 1418 if (sig) {
1378 if (lock_task_sighand(p, &flags)) { 1419 if (lock_task_sighand(p, &flags)) {
1379 ret = __send_signal(sig, info, p, 1, 0); 1420 ret = __send_signal(sig, info, p, PIDTYPE_TGID, 0);
1380 unlock_task_sighand(p, &flags); 1421 unlock_task_sighand(p, &flags);
1381 } else 1422 } else
1382 ret = -ESRCH; 1423 ret = -ESRCH;
@@ -1420,7 +1461,8 @@ static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1420 for_each_process(p) { 1461 for_each_process(p) {
1421 if (task_pid_vnr(p) > 1 && 1462 if (task_pid_vnr(p) > 1 &&
1422 !same_thread_group(p, current)) { 1463 !same_thread_group(p, current)) {
1423 int err = group_send_sig_info(sig, info, p); 1464 int err = group_send_sig_info(sig, info, p,
1465 PIDTYPE_MAX);
1424 ++count; 1466 ++count;
1425 if (err != -EPERM) 1467 if (err != -EPERM)
1426 retval = err; 1468 retval = err;
@@ -1446,7 +1488,7 @@ int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1446 if (!valid_signal(sig)) 1488 if (!valid_signal(sig))
1447 return -EINVAL; 1489 return -EINVAL;
1448 1490
1449 return do_send_sig_info(sig, info, p, false); 1491 return do_send_sig_info(sig, info, p, PIDTYPE_PID);
1450} 1492}
1451 1493
1452#define __si_special(priv) \ 1494#define __si_special(priv) \
@@ -1664,17 +1706,20 @@ void sigqueue_free(struct sigqueue *q)
1664 __sigqueue_free(q); 1706 __sigqueue_free(q);
1665} 1707}
1666 1708
1667int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group) 1709int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
1668{ 1710{
1669 int sig = q->info.si_signo; 1711 int sig = q->info.si_signo;
1670 struct sigpending *pending; 1712 struct sigpending *pending;
1713 struct task_struct *t;
1671 unsigned long flags; 1714 unsigned long flags;
1672 int ret, result; 1715 int ret, result;
1673 1716
1674 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); 1717 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1675 1718
1676 ret = -1; 1719 ret = -1;
1677 if (!likely(lock_task_sighand(t, &flags))) 1720 rcu_read_lock();
1721 t = pid_task(pid, type);
1722 if (!t || !likely(lock_task_sighand(t, &flags)))
1678 goto ret; 1723 goto ret;
1679 1724
1680 ret = 1; /* the signal is ignored */ 1725 ret = 1; /* the signal is ignored */
@@ -1696,15 +1741,16 @@ int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1696 q->info.si_overrun = 0; 1741 q->info.si_overrun = 0;
1697 1742
1698 signalfd_notify(t, sig); 1743 signalfd_notify(t, sig);
1699 pending = group ? &t->signal->shared_pending : &t->pending; 1744 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1700 list_add_tail(&q->list, &pending->list); 1745 list_add_tail(&q->list, &pending->list);
1701 sigaddset(&pending->signal, sig); 1746 sigaddset(&pending->signal, sig);
1702 complete_signal(sig, t, group); 1747 complete_signal(sig, t, type);
1703 result = TRACE_SIGNAL_DELIVERED; 1748 result = TRACE_SIGNAL_DELIVERED;
1704out: 1749out:
1705 trace_signal_generate(sig, &q->info, t, group, result); 1750 trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
1706 unlock_task_sighand(t, &flags); 1751 unlock_task_sighand(t, &flags);
1707ret: 1752ret:
1753 rcu_read_unlock();
1708 return ret; 1754 return ret;
1709} 1755}
1710 1756
@@ -3193,7 +3239,7 @@ do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
3193 * probe. No signal is actually delivered. 3239 * probe. No signal is actually delivered.
3194 */ 3240 */
3195 if (!error && sig) { 3241 if (!error && sig) {
3196 error = do_send_sig_info(sig, info, p, false); 3242 error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
3197 /* 3243 /*
3198 * If lock_task_sighand() failed we pretend the task 3244 * If lock_task_sighand() failed we pretend the task
3199 * dies after receiving the signal. The window is tiny, 3245 * dies after receiving the signal. The window is tiny,
@@ -3960,7 +4006,7 @@ void kdb_send_sig(struct task_struct *t, int sig)
3960 "the deadlock.\n"); 4006 "the deadlock.\n");
3961 return; 4007 return;
3962 } 4008 }
3963 ret = send_signal(sig, SEND_SIG_PRIV, t, false); 4009 ret = send_signal(sig, SEND_SIG_PRIV, t, PIDTYPE_PID);
3964 spin_unlock(&t->sighand->siglock); 4010 spin_unlock(&t->sighand->siglock);
3965 if (ret) 4011 if (ret)
3966 kdb_printf("Fail to deliver Signal %d to process %d.\n", 4012 kdb_printf("Fail to deliver Signal %d to process %d.\n",
diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c
index f26acef5d7b4..9a65713c8309 100644
--- a/kernel/time/itimer.c
+++ b/kernel/time/itimer.c
@@ -139,9 +139,10 @@ enum hrtimer_restart it_real_fn(struct hrtimer *timer)
139{ 139{
140 struct signal_struct *sig = 140 struct signal_struct *sig =
141 container_of(timer, struct signal_struct, real_timer); 141 container_of(timer, struct signal_struct, real_timer);
142 struct pid *leader_pid = sig->pids[PIDTYPE_TGID];
142 143
143 trace_itimer_expire(ITIMER_REAL, sig->leader_pid, 0); 144 trace_itimer_expire(ITIMER_REAL, leader_pid, 0);
144 kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid); 145 kill_pid_info(SIGALRM, SEND_SIG_PRIV, leader_pid);
145 146
146 return HRTIMER_NORESTART; 147 return HRTIMER_NORESTART;
147} 148}
diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index 294d7b65af33..ce32cf741b25 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -894,7 +894,7 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
894 894
895 trace_itimer_expire(signo == SIGPROF ? 895 trace_itimer_expire(signo == SIGPROF ?
896 ITIMER_PROF : ITIMER_VIRTUAL, 896 ITIMER_PROF : ITIMER_VIRTUAL,
897 tsk->signal->leader_pid, cur_time); 897 task_tgid(tsk), cur_time);
898 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk); 898 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
899 } 899 }
900 900
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index f23cc46ecf3e..4b9127e95430 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -333,8 +333,8 @@ void posixtimer_rearm(struct siginfo *info)
333 333
334int posix_timer_event(struct k_itimer *timr, int si_private) 334int posix_timer_event(struct k_itimer *timr, int si_private)
335{ 335{
336 struct task_struct *task; 336 enum pid_type type;
337 int shared, ret = -1; 337 int ret = -1;
338 /* 338 /*
339 * FIXME: if ->sigq is queued we can race with 339 * FIXME: if ->sigq is queued we can race with
340 * dequeue_signal()->posixtimer_rearm(). 340 * dequeue_signal()->posixtimer_rearm().
@@ -348,13 +348,8 @@ int posix_timer_event(struct k_itimer *timr, int si_private)
348 */ 348 */
349 timr->sigq->info.si_sys_private = si_private; 349 timr->sigq->info.si_sys_private = si_private;
350 350
351 rcu_read_lock(); 351 type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
352 task = pid_task(timr->it_pid, PIDTYPE_PID); 352 ret = send_sigqueue(timr->sigq, timr->it_pid, type);
353 if (task) {
354 shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
355 ret = send_sigqueue(timr->sigq, task, shared);
356 }
357 rcu_read_unlock();
358 /* If we failed to send the signal the timer stops. */ 353 /* If we failed to send the signal the timer stops. */
359 return ret > 0; 354 return ret > 0;
360} 355}
@@ -433,11 +428,13 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
433 428
434static struct pid *good_sigevent(sigevent_t * event) 429static struct pid *good_sigevent(sigevent_t * event)
435{ 430{
436 struct task_struct *rtn = current->group_leader; 431 struct pid *pid = task_tgid(current);
432 struct task_struct *rtn;
437 433
438 switch (event->sigev_notify) { 434 switch (event->sigev_notify) {
439 case SIGEV_SIGNAL | SIGEV_THREAD_ID: 435 case SIGEV_SIGNAL | SIGEV_THREAD_ID:
440 rtn = find_task_by_vpid(event->sigev_notify_thread_id); 436 pid = find_vpid(event->sigev_notify_thread_id);
437 rtn = pid_task(pid, PIDTYPE_PID);
441 if (!rtn || !same_thread_group(rtn, current)) 438 if (!rtn || !same_thread_group(rtn, current))
442 return NULL; 439 return NULL;
443 /* FALLTHRU */ 440 /* FALLTHRU */
@@ -447,7 +444,7 @@ static struct pid *good_sigevent(sigevent_t * event)
447 return NULL; 444 return NULL;
448 /* FALLTHRU */ 445 /* FALLTHRU */
449 case SIGEV_NONE: 446 case SIGEV_NONE:
450 return task_pid(rtn); 447 return pid;
451 default: 448 default:
452 return NULL; 449 return NULL;
453 } 450 }
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 412f43453a68..7c74dcc2d26d 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -928,7 +928,7 @@ static void oom_kill_process(struct oom_control *oc, const char *message)
928 * in order to prevent the OOM victim from depleting the memory 928 * in order to prevent the OOM victim from depleting the memory
929 * reserves from the user space under its control. 929 * reserves from the user space under its control.
930 */ 930 */
931 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, true); 931 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, PIDTYPE_TGID);
932 mark_oom_victim(victim); 932 mark_oom_victim(victim);
933 pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n", 933 pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
934 task_pid_nr(victim), victim->comm, K(victim->mm->total_vm), 934 task_pid_nr(victim), victim->comm, K(victim->mm->total_vm),
@@ -966,7 +966,7 @@ static void oom_kill_process(struct oom_control *oc, const char *message)
966 */ 966 */
967 if (unlikely(p->flags & PF_KTHREAD)) 967 if (unlikely(p->flags & PF_KTHREAD))
968 continue; 968 continue;
969 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true); 969 do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, PIDTYPE_TGID);
970 } 970 }
971 rcu_read_unlock(); 971 rcu_read_unlock();
972 972
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 9263ead9fd32..0df592c4f09f 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2568,7 +2568,7 @@ static long kvm_vcpu_ioctl(struct file *filp,
2568 if (arg) 2568 if (arg)
2569 goto out; 2569 goto out;
2570 oldpid = rcu_access_pointer(vcpu->pid); 2570 oldpid = rcu_access_pointer(vcpu->pid);
2571 if (unlikely(oldpid != current->pids[PIDTYPE_PID].pid)) { 2571 if (unlikely(oldpid != task_pid(current))) {
2572 /* The thread running this VCPU changed. */ 2572 /* The thread running this VCPU changed. */
2573 struct pid *newpid; 2573 struct pid *newpid;
2574 2574