Skip to content

Commit d7f89a9

Browse files
btw616jmberg-intel
authored andcommitted
um: ubd: Switch to the pthread-based helper
The ubd io thread and UML kernel thread share the same errno, which can lead to conflicts when both call syscalls concurrently. Switch to the pthread-based helper to address this issue. Signed-off-by: Tiwei Bie <tiwei.btw@antgroup.com> Link: https://patch.msgid.link/20250319135523.97050-3-tiwei.btw@antgroup.com Signed-off-by: Johannes Berg <johannes.berg@intel.com>
1 parent 4f087ea commit d7f89a9

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

arch/um/drivers/ubd.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
#ifndef __UM_UBD_USER_H
88
#define __UM_UBD_USER_H
99

10-
extern int start_io_thread(unsigned long sp, int *fds_out);
11-
extern int io_thread(void *arg);
10+
#include <os.h>
11+
12+
int start_io_thread(struct os_helper_thread **td_out, int *fd_out);
13+
void *io_thread(void *arg);
1214
extern int kernel_fd;
1315

1416
extern int ubd_read_poll(int timeout);

arch/um/drivers/ubd_kern.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,12 @@ static irqreturn_t ubd_intr(int irq, void *dev)
474474
}
475475

476476
/* Only changed by ubd_init, which is an initcall. */
477-
static int io_pid = -1;
477+
static struct os_helper_thread *io_td;
478478

479479
static void kill_io_thread(void)
480480
{
481-
if(io_pid != -1)
482-
os_kill_process(io_pid, 1);
481+
if (io_td)
482+
os_kill_helper_thread(io_td);
483483
}
484484

485485
__uml_exitcall(kill_io_thread);
@@ -1104,8 +1104,8 @@ static int __init ubd_init(void)
11041104

11051105
late_initcall(ubd_init);
11061106

1107-
static int __init ubd_driver_init(void){
1108-
unsigned long stack;
1107+
static int __init ubd_driver_init(void)
1108+
{
11091109
int err;
11101110

11111111
/* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
@@ -1114,13 +1114,11 @@ static int __init ubd_driver_init(void){
11141114
/* Letting ubd=sync be like using ubd#s= instead of ubd#= is
11151115
* enough. So use anyway the io thread. */
11161116
}
1117-
stack = alloc_stack(0, 0);
1118-
io_pid = start_io_thread(stack + PAGE_SIZE, &thread_fd);
1119-
if(io_pid < 0){
1117+
err = start_io_thread(&io_td, &thread_fd);
1118+
if (err < 0) {
11201119
printk(KERN_ERR
11211120
"ubd : Failed to start I/O thread (errno = %d) - "
1122-
"falling back to synchronous I/O\n", -io_pid);
1123-
io_pid = -1;
1121+
"falling back to synchronous I/O\n", -err);
11241122
return 0;
11251123
}
11261124
err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
@@ -1496,12 +1494,11 @@ int kernel_fd = -1;
14961494
/* Only changed by the io thread. XXX: currently unused. */
14971495
static int io_count;
14981496

1499-
int io_thread(void *arg)
1497+
void *io_thread(void *arg)
15001498
{
15011499
int n, count, written, res;
15021500

1503-
os_set_pdeathsig();
1504-
os_fix_helper_signals();
1501+
os_fix_helper_thread_signals();
15051502

15061503
while(1){
15071504
n = bulk_req_safe_read(
@@ -1543,5 +1540,5 @@ int io_thread(void *arg)
15431540
} while (written < n);
15441541
}
15451542

1546-
return 0;
1543+
return NULL;
15471544
}

arch/um/drivers/ubd_user.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
static struct pollfd kernel_pollfd;
2727

28-
int start_io_thread(unsigned long sp, int *fd_out)
28+
int start_io_thread(struct os_helper_thread **td_out, int *fd_out)
2929
{
30-
int pid, fds[2], err;
30+
int fds[2], err;
3131

3232
err = os_pipe(fds, 1, 1);
3333
if(err < 0){
@@ -47,14 +47,14 @@ int start_io_thread(unsigned long sp, int *fd_out)
4747
goto out_close;
4848
}
4949

50-
pid = clone(io_thread, (void *) sp, CLONE_FILES | CLONE_VM, NULL);
51-
if(pid < 0){
52-
err = -errno;
53-
printk("start_io_thread - clone failed : errno = %d\n", errno);
50+
err = os_run_helper_thread(td_out, io_thread, NULL);
51+
if (err < 0) {
52+
printk("%s - failed to run helper thread, err = %d\n",
53+
__func__, -err);
5454
goto out_close;
5555
}
5656

57-
return(pid);
57+
return 0;
5858

5959
out_close:
6060
os_close_file(fds[0]);

0 commit comments

Comments
 (0)