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>
This commit is contained in:
Tiwei Bie
2025-03-19 21:55:21 +08:00
committed by Johannes Berg
parent 4f087eafdc
commit d7f89a9da4
3 changed files with 22 additions and 23 deletions

View File

@@ -7,8 +7,10 @@
#ifndef __UM_UBD_USER_H
#define __UM_UBD_USER_H
extern int start_io_thread(unsigned long sp, int *fds_out);
extern int io_thread(void *arg);
#include <os.h>
int start_io_thread(struct os_helper_thread **td_out, int *fd_out);
void *io_thread(void *arg);
extern int kernel_fd;
extern int ubd_read_poll(int timeout);

View File

@@ -474,12 +474,12 @@ static irqreturn_t ubd_intr(int irq, void *dev)
}
/* Only changed by ubd_init, which is an initcall. */
static int io_pid = -1;
static struct os_helper_thread *io_td;
static void kill_io_thread(void)
{
if(io_pid != -1)
os_kill_process(io_pid, 1);
if (io_td)
os_kill_helper_thread(io_td);
}
__uml_exitcall(kill_io_thread);
@@ -1104,8 +1104,8 @@ static int __init ubd_init(void)
late_initcall(ubd_init);
static int __init ubd_driver_init(void){
unsigned long stack;
static int __init ubd_driver_init(void)
{
int err;
/* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
@@ -1114,13 +1114,11 @@ static int __init ubd_driver_init(void){
/* Letting ubd=sync be like using ubd#s= instead of ubd#= is
* enough. So use anyway the io thread. */
}
stack = alloc_stack(0, 0);
io_pid = start_io_thread(stack + PAGE_SIZE, &thread_fd);
if(io_pid < 0){
err = start_io_thread(&io_td, &thread_fd);
if (err < 0) {
printk(KERN_ERR
"ubd : Failed to start I/O thread (errno = %d) - "
"falling back to synchronous I/O\n", -io_pid);
io_pid = -1;
"falling back to synchronous I/O\n", -err);
return 0;
}
err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
@@ -1496,12 +1494,11 @@ int kernel_fd = -1;
/* Only changed by the io thread. XXX: currently unused. */
static int io_count;
int io_thread(void *arg)
void *io_thread(void *arg)
{
int n, count, written, res;
os_set_pdeathsig();
os_fix_helper_signals();
os_fix_helper_thread_signals();
while(1){
n = bulk_req_safe_read(
@@ -1543,5 +1540,5 @@ int io_thread(void *arg)
} while (written < n);
}
return 0;
return NULL;
}

View File

@@ -25,9 +25,9 @@
static struct pollfd kernel_pollfd;
int start_io_thread(unsigned long sp, int *fd_out)
int start_io_thread(struct os_helper_thread **td_out, int *fd_out)
{
int pid, fds[2], err;
int fds[2], err;
err = os_pipe(fds, 1, 1);
if(err < 0){
@@ -47,14 +47,14 @@ int start_io_thread(unsigned long sp, int *fd_out)
goto out_close;
}
pid = clone(io_thread, (void *) sp, CLONE_FILES | CLONE_VM, NULL);
if(pid < 0){
err = -errno;
printk("start_io_thread - clone failed : errno = %d\n", errno);
err = os_run_helper_thread(td_out, io_thread, NULL);
if (err < 0) {
printk("%s - failed to run helper thread, err = %d\n",
__func__, -err);
goto out_close;
}
return(pid);
return 0;
out_close:
os_close_file(fds[0]);