Skip to content

Commit 4dbed85

Browse files
sgruszkaLinus Torvalds
authored andcommitted
uml: stop gdb from deleting breakpoints when running UML
Sometimes when UML is debugged gdb miss breakpoints. When process traced by gdb do fork, debugger remove breakpoints from child address space. There is possibility to trace more than one fork, but this not work with UML, I guess (only guess) there is a deadlock - gdb waits for UML and UML waits for gdb. When clone() is called with SIGCHLD and CLONE_VM flags, gdb see this as PTRACE_EVENT_FORK not as PTRACE_EVENT_CLONE and remove breakpoints from child and at the same time from traced process, because either have the same address space. Maybe it is possible to do fix in gdb, but I'm not sure if there is easy way to find out if traced and child processes share memory. So I do fix for UML, it simply do not call clone() with both SIGCHLD and CLONE_VM flags together. Additionally __WALL flag is used for waitpid() to assure not miss clone and normal process events. [ jdike - checkpatch fixes ] Signed-off-by: Stanislaw Gruszka <stf_xl@wp.pl> Signed-off-by: Jeff Dike <jdike@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5867a78 commit 4dbed85

12 files changed

Lines changed: 50 additions & 56 deletions

File tree

‎arch/um/drivers/net_user.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static int change_tramp(char **argv, char *output, int output_len)
201201
close(fds[1]);
202202

203203
if (pid > 0)
204-
CATCH_EINTR(err = waitpid(pid, NULL, 0));
204+
helper_wait(pid, 0, "change_tramp");
205205
return pid;
206206
}
207207

‎arch/um/drivers/slip_user.c‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static int slip_tramp(char **argv, int fd)
7777
{
7878
struct slip_pre_exec_data pe_data;
7979
char *output;
80-
int status, pid, fds[2], err, output_len;
80+
int pid, fds[2], err, output_len;
8181

8282
err = os_pipe(fds, 1, 0);
8383
if (err < 0) {
@@ -109,15 +109,7 @@ static int slip_tramp(char **argv, int fd)
109109
read_output(fds[0], output, output_len);
110110
printk("%s", output);
111111

112-
CATCH_EINTR(err = waitpid(pid, &status, 0));
113-
if (err < 0)
114-
err = errno;
115-
else if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
116-
printk(UM_KERN_ERR "'%s' didn't exit with status 0\n", argv[0]);
117-
err = -EINVAL;
118-
}
119-
else err = 0;
120-
112+
err = helper_wait(pid, 0, argv[0]);
121113
close(fds[0]);
122114

123115
out_free:

‎arch/um/drivers/slirp_user.c‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static int slirp_open(void *data)
7979
static void slirp_close(int fd, void *data)
8080
{
8181
struct slirp_data *pri = data;
82-
int status,err;
82+
int err;
8383

8484
close(fd);
8585
close(pri->slave);
@@ -98,18 +98,9 @@ static void slirp_close(int fd, void *data)
9898
"(%d)\n", pri->pid, errno);
9999
}
100100
#endif
101-
102-
CATCH_EINTR(err = waitpid(pri->pid, &status, WNOHANG));
103-
if (err < 0) {
104-
printk(UM_KERN_ERR "slirp_close: waitpid returned %d\n", errno);
105-
return;
106-
}
107-
108-
if (err == 0) {
109-
printk(UM_KERN_ERR "slirp_close: process %d has not exited\n",
110-
pri->pid);
101+
err = helper_wait(pri->pid, 1, "slirp_close");
102+
if (err < 0)
111103
return;
112-
}
113104

114105
pri->pid = -1;
115106
}

‎arch/um/drivers/ubd_user.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ int start_io_thread(unsigned long sp, int *fd_out)
4949
goto out_close;
5050
}
5151

52-
pid = clone(io_thread, (void *) sp, CLONE_FILES | CLONE_VM | SIGCHLD,
53-
NULL);
52+
pid = clone(io_thread, (void *) sp, CLONE_FILES | CLONE_VM, NULL);
5453
if(pid < 0){
5554
err = -errno;
5655
printk("start_io_thread - clone failed : errno = %d\n", errno);

‎arch/um/include/os.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ extern int execvp_noalloc(char *buf, const char *file, char *const argv[]);
214214
extern int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv);
215215
extern int run_helper_thread(int (*proc)(void *), void *arg,
216216
unsigned int flags, unsigned long *stack_out);
217-
extern int helper_wait(int pid);
217+
extern int helper_wait(int pid, int nohang, char *pname);
218218

219219

220220
/* tls.c */

‎arch/um/os-Linux/aio.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ static int init_aio_24(void)
218218
goto out_close_pipe;
219219

220220
err = run_helper_thread(not_aio_thread, NULL,
221-
CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
221+
CLONE_FILES | CLONE_VM, &aio_stack);
222222
if (err < 0)
223223
goto out_close_pipe;
224224

@@ -254,7 +254,7 @@ static int init_aio_26(void)
254254
}
255255

256256
err = run_helper_thread(aio_thread, NULL,
257-
CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
257+
CLONE_FILES | CLONE_VM, &aio_stack);
258258
if (err < 0)
259259
return err;
260260

‎arch/um/os-Linux/drivers/ethertap_user.c‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static int etap_tramp(char *dev, char *gate, int control_me,
9494
int control_remote, int data_me, int data_remote)
9595
{
9696
struct etap_pre_exec_data pe_data;
97-
int pid, status, err, n;
97+
int pid, err, n;
9898
char version_buf[sizeof("nnnnn\0")];
9999
char data_fd_buf[sizeof("nnnnnn\0")];
100100
char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")];
@@ -131,13 +131,7 @@ static int etap_tramp(char *dev, char *gate, int control_me,
131131
}
132132
if (c != 1) {
133133
printk(UM_KERN_ERR "etap_tramp : uml_net failed\n");
134-
err = -EINVAL;
135-
CATCH_EINTR(n = waitpid(pid, &status, 0));
136-
if (n < 0)
137-
err = -errno;
138-
else if (!WIFEXITED(status) || (WEXITSTATUS(status) != 1))
139-
printk(UM_KERN_ERR "uml_net didn't exit with "
140-
"status 1\n");
134+
err = helper_wait(pid, 0, "uml_net");
141135
}
142136
return err;
143137
}

‎arch/um/os-Linux/drivers/tuntap_user.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote,
107107
"errno = %d\n", errno);
108108
return err;
109109
}
110-
CATCH_EINTR(waitpid(pid, NULL, 0));
110+
helper_wait(pid, 0, "tuntap_open_tramp");
111111

112112
cmsg = CMSG_FIRSTHDR(&msg);
113113
if (cmsg == NULL) {

‎arch/um/os-Linux/helper.c‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
7676
data.fd = fds[1];
7777
data.buf = __cant_sleep() ? kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
7878
kmalloc(PATH_MAX, UM_GFP_KERNEL);
79-
pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
79+
pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
8080
if (pid < 0) {
8181
ret = -errno;
8282
printk("run_helper : clone failed, errno = %d\n", errno);
@@ -101,7 +101,7 @@ int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
101101
ret = n;
102102
kill(pid, SIGKILL);
103103
}
104-
CATCH_EINTR(waitpid(pid, NULL, 0));
104+
CATCH_EINTR(waitpid(pid, NULL, __WCLONE));
105105
}
106106

107107
out_free2:
@@ -126,15 +126,15 @@ int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
126126
return -ENOMEM;
127127

128128
sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
129-
pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
129+
pid = clone(proc, (void *) sp, flags, arg);
130130
if (pid < 0) {
131131
err = -errno;
132132
printk("run_helper_thread : clone failed, errno = %d\n",
133133
errno);
134134
return err;
135135
}
136136
if (stack_out == NULL) {
137-
CATCH_EINTR(pid = waitpid(pid, &status, 0));
137+
CATCH_EINTR(pid = waitpid(pid, &status, __WCLONE));
138138
if (pid < 0) {
139139
err = -errno;
140140
printk("run_helper_thread - wait failed, errno = %d\n",
@@ -150,14 +150,30 @@ int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
150150
return pid;
151151
}
152152

153-
int helper_wait(int pid)
153+
int helper_wait(int pid, int nohang, char *pname)
154154
{
155-
int ret;
155+
int ret, status;
156+
int wflags = __WCLONE;
156157

157-
CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
158+
if (nohang)
159+
wflags |= WNOHANG;
160+
161+
if (!pname)
162+
pname = "helper_wait";
163+
164+
CATCH_EINTR(ret = waitpid(pid, &status, wflags));
158165
if (ret < 0) {
159-
ret = -errno;
160-
printk("helper_wait : waitpid failed, errno = %d\n", errno);
161-
}
162-
return ret;
166+
printk(UM_KERN_ERR "%s : waitpid process %d failed, "
167+
"errno = %d\n", pname, pid, errno);
168+
return -errno;
169+
} else if (nohang && ret == 0) {
170+
printk(UM_KERN_ERR "%s : process %d has not exited\n",
171+
pname, pid);
172+
return -ECHILD;
173+
} else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
174+
printk(UM_KERN_ERR "%s : process %d didn't exit with "
175+
"status 0\n", pname, pid);
176+
return -ECHILD;
177+
} else
178+
return 0;
163179
}

‎arch/um/os-Linux/process.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void os_kill_process(int pid, int reap_child)
101101
{
102102
kill(pid, SIGKILL);
103103
if (reap_child)
104-
CATCH_EINTR(waitpid(pid, NULL, 0));
104+
CATCH_EINTR(waitpid(pid, NULL, __WALL));
105105
}
106106

107107
/* This is here uniquely to have access to the userspace errno, i.e. the one
@@ -130,7 +130,7 @@ void os_kill_ptraced_process(int pid, int reap_child)
130130
ptrace(PTRACE_KILL, pid);
131131
ptrace(PTRACE_CONT, pid);
132132
if (reap_child)
133-
CATCH_EINTR(waitpid(pid, NULL, 0));
133+
CATCH_EINTR(waitpid(pid, NULL, __WALL));
134134
}
135135

136136
/* Don't use the glibc version, which caches the result in TLS. It misses some

0 commit comments

Comments
 (0)