Use copy_process in vhost layer
Subsystems (28)
cover Use copy_process in vhost layer
The following patches made over Linus's tree, allow the vhost layer to do a copy_process on the thread that does the VHOST_SET_OWNER ioctl like how io_uring does a copy_process against its userspace app. This allows the vhost layer's worker threads to inherit cgroups, namespaces, address space, etc and this worker thread will also be accounted for against that owner/parent process's RLIMIT_NPROC limit.
If you are not familiar with qemu and vhost here is more detailed problem description:
Qemu will create vhost devices in the kernel which perform network, SCSI, etc IO and management operations from worker threads created by the kthread API. Because the kthread API does a copy_process on the kthreadd thread, the vhost layer has to use kthread_use_mm to access the Qemu thread's memory and cgroup_attach_task_all to add itself to the Qemu thread's cgroups.
The problem with this approach is that we then have to add new functions/ args/functionality for every thing we want to inherit. I started doing that here:
https://lkml.org/lkml/2021/6/23/1233
for the RLIMIT_NPROC check, but it seems it might be easier to just inherit everything from the beginning, becuase I'd need to do something like that patch several times.
V7:
- Drop generic user_worker_* helpers and replace with vhost_task specific ones.
- Drop autoreap patch. Use kernel_wait4 instead.
- Fix issue where vhost.ko could be removed while the worker function is still running.
V6:
- Rename kernel_worker to user_worker and fix prefixes.
- Add better patch descriptions.
V5:
- Handle kbuild errors by building patchset against current kernel that has all deps merged. Also add patch to remove create_io_thread code as it's not used anymore.
- Rebase patchset against current kernel and handle a new vm PF_IO_WORKER case added in 5.16-rc1.
- Add PF_USER_WORKER flag so we can check it later after the initial thread creation for the wake up, vm and singal cses.
- Added patch to auto reap the worker thread.
V4:
- Drop NO_SIG patch and replaced with Christian's SIG_IGN patch.
- Merged Christian's kernel_worker_flags_valid helpers into patch 5 that added the new kernel worker functions.
- Fixed extra "i" issue.
- Added PF_USER_WORKER flag and added check that kernel_worker_start users had that flag set. Also dropped patches that passed worker flags to copy_thread and replaced with PF_USER_WORKER check.
V3:
- Add parentheses in p->flag and work_flags check in copy_thread.
- Fix check in arm/arm64 which was doing the reverse of other archs where it did likely(!flags) instead of unlikely(flags).
V2:
- Rename kernel_copy_process to kernel_worker.
- Instead of exporting functions, make kernel_worker() a proper function/API that does common work for the caller.
- Instead of adding new fields to kernel_clone_args for each option make it flag based similar to CLONE_*.
- Drop unused completion struct in vhost.
- Fix compile warnings by merging vhost cgroup cleanup patch and vhost conversion patch.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH 1/8 fork: Make IO worker options flag based
This patchset adds a couple new options to kernel_clone_args for the vhost layer which is going to work like PF_IO_WORKER but will differ enough that we will need to add several fields to kernel_clone_args. This patch moves us to a flags based approach for these types of users.
--- include/linux/sched/task.h | 4 +++- kernel/fork.c | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-)
include/linux/sched/task.h +3 -1
@@ -18,8 +18,11 @@ struct css_set;/* All the bits taken by the old clone syscall. */ #define CLONE_LEGACY_FLAGS 0xffffffffULL +#define USER_WORKER_IO BIT(0) +struct kernel_clone_args {u64 flags; +u32 worker_flags;int __user *pidfd;int __user *child_tid;int __user *parent_tid;
@@ -31,7 +34,6 @@ struct kernel_clone_args {/* Number of elements in *set_tid */ size_t set_tid_size;int cgroup; -int io_thread;struct cgroup *cgrp;struct css_set *cset; };
kernel/fork.c +2 -2
@@ -1998,7 +1998,7 @@ static __latent_entropy struct task_struct *copy_process( p =dup_task_struct (current, node);if (!p) goto fork_out; -if (args->io_thread) { +if (args->worker_flags &USER_WORKER_IO ) {/* * Mark us an IO worker, and block any signal that isn't * fatal or STOP
@@ -2497,7 +2497,7 @@ struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node) .exit_signal = (lower_32_bits(flags) & CSIGNAL), .stack = (unsigned long)fn, .stack_size = (unsigned long)arg, - .io_thread = 1, + .worker_flags = USER_WORKER_IO, };return copy_process (NULL ,0 , node, &args);
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH 2/8 fork/vm: Move common PF_IO_WORKER behavior to new flag
In reply to Mike ChristieThis adds a new flag, PF_USER_WORKER, that's used for behavior common to to both PF_IO_WORKER and users like vhost which will use a new helper instead of create_io_thread because they require different behavior.
The common behavior PF_USER_WORKER covers is the initial frame and fpu setup and the vm reclaim handling.
--- arch/alpha/kernel/process.c | 2 +- arch/arc/kernel/process.c | 2 +- arch/arm/kernel/process.c | 2 +- arch/arm64/kernel/process.c | 2 +- arch/csky/kernel/process.c | 2 +- arch/h8300/kernel/process.c | 2 +- arch/hexagon/kernel/process.c | 2 +- arch/ia64/kernel/process.c | 2 +- arch/m68k/kernel/process.c | 2 +- arch/microblaze/kernel/process.c | 2 +- arch/mips/kernel/process.c | 2 +- arch/nds32/kernel/process.c | 2 +- arch/nios2/kernel/process.c | 2 +- arch/openrisc/kernel/process.c | 2 +- arch/parisc/kernel/process.c | 2 +- arch/powerpc/kernel/process.c | 2 +- arch/riscv/kernel/process.c | 2 +- arch/s390/kernel/process.c | 2 +- arch/sh/kernel/process_32.c | 2 +- arch/sparc/kernel/process_32.c | 2 +- arch/sparc/kernel/process_64.c | 2 +- arch/um/kernel/process.c | 2 +- arch/x86/kernel/fpu/core.c | 4 ++-- arch/x86/kernel/process.c | 2 +- arch/xtensa/kernel/process.c | 2 +- include/linux/sched.h | 1 + include/linux/sched/task.h | 3 ++- kernel/fork.c | 6 +++++- mm/vmscan.c | 4 ++-- 29 files changed, 36 insertions(+), 30 deletions(-)
arch/alpha/kernel/process.c +1 -1
@@ -249,7 +249,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, childti->pcb.ksp = (unsigned long ) childstack; childti->pcb.flags =1 ;/* set FEN, clear everything else */ -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {/* kernel thread */ memset (childstack,0 ,sizeof (struct switch_stack ) +sizeof (struct pt_regs ));
arch/arc/kernel/process.c +1 -1
@@ -191,7 +191,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, childksp[0 ] =0 ;/* fp */ childksp[1 ] = (unsigned long )ret_from_fork;/* blink */ -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (c_regs,0 ,sizeof (struct pt_regs )); c_callee->r13 = kthread_arg;
arch/arm/kernel/process.c +1 -1
@@ -251,7 +251,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start, thread->cpu_domain =get_domain ();#endif -if (likely (!(p->flags & (PF_KTHREAD |PF_IO_WORKER )))) { +if (likely (!(p->flags & (PF_KTHREAD |PF_USER_WORKER )))) { *childregs = *current_pt_regs (); childregs->ARM_r0 =0 ;if (stack_start)
arch/arm64/kernel/process.c +1 -1
@@ -334,7 +334,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,ptrauth_thread_init_kernel (p); -if (likely (!(p->flags & (PF_KTHREAD |PF_IO_WORKER )))) { +if (likely (!(p->flags & (PF_KTHREAD |PF_USER_WORKER )))) { *childregs = *current_pt_regs (); childregs->regs[0 ] =0 ;
arch/csky/kernel/process.c +1 -1
@@ -49,7 +49,7 @@ int copy_thread(unsigned long clone_flags,/* setup thread.sp for switch_to !!! */ p->thread.sp = (unsigned long )childstack; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (childregs,0 ,sizeof (struct pt_regs )); childstack->r15 = (unsigned long ) ret_from_kernel_thread; childstack->r10 = kthread_arg;
arch/h8300/kernel/process.c +1 -1
@@ -112,7 +112,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, childregs = (struct pt_regs *) (THREAD_SIZE +task_stack_page (p)) -1 ; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (childregs,0 ,sizeof (struct pt_regs )); childregs->retpc = (unsigned long ) ret_from_kernel_thread; childregs->er4 = topstk;/* arg */
arch/hexagon/kernel/process.c +1 -1
@@ -73,7 +73,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg, sizeof(*ss)); ss->lr = (unsigned long )ret_from_fork; p->thread.switch_sp = ss; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (childregs,0 ,sizeof (struct pt_regs ));/* r24 <- fn, r25 <- arg */ ss->r24 = usp;
arch/ia64/kernel/process.c +1 -1
@@ -338,7 +338,7 @@ copy_thread(unsigned long clone_flags, unsigned long user_stack_base,ia64_drop_fpu (p);/* don't pick up stale state from a CPU's fph */ -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {if (unlikely (!user_stack_base)) {/* fork_idle() called us */ return 0 ;
arch/m68k/kernel/process.c +1 -1
@@ -157,7 +157,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg,*/ p->thread.fc =USER_DATA ; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {/* kernel thread */ memset (frame,0 ,sizeof (struct fork_frame )); frame->regs.sr =PS_S ;
arch/microblaze/kernel/process.c +1 -1
@@ -59,7 +59,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg,struct pt_regs *childregs =task_pt_regs (p);struct thread_info *ti =task_thread_info (p); -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {/* if we're creating a new kernel thread then just zeroing all * the registers. That's OK for a brand new thread.*/ memset (childregs,0 ,sizeof (struct pt_regs ));
arch/mips/kernel/process.c +1 -1
@@ -120,7 +120,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,/* Put the stack after the struct pt_regs. */ childksp = (unsigned long ) childregs; p->thread.cp0_status = (read_c0_status () & ~(ST0_CU2 |ST0_CU1 )) |ST0_KERNEL_CUMASK ; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {/* kernel thread */ unsigned long status = p->thread.cp0_status;memset (childregs,0 ,sizeof (struct pt_regs ));
arch/nds32/kernel/process.c +1 -1
@@ -156,7 +156,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,memset (&p->thread.cpu_context,0 ,sizeof (struct cpu_context )); -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (childregs,0 ,sizeof (struct pt_regs ));/* kernel thread fn */ p->thread.cpu_context.r6 = stack_start;
arch/nios2/kernel/process.c +1 -1
@@ -109,7 +109,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg,struct switch_stack *childstack = ((struct switch_stack *)childregs) -1 ; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (childstack,0 ,sizeof (struct switch_stack ) +sizeof (struct pt_regs ));
arch/openrisc/kernel/process.c +1 -1
@@ -172,7 +172,7 @@ copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg, sp -=sizeof (struct pt_regs ); kregs = (struct pt_regs *)sp; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (kregs,0 ,sizeof (struct pt_regs )); kregs->gpr[20 ] = usp;/* fn, kernel thread */ kregs->gpr[22 ] = arg;
arch/parisc/kernel/process.c +1 -1
@@ -197,7 +197,7 @@ copy_thread(unsigned long clone_flags, unsigned long usp,extern void *const ret_from_kernel_thread;extern void *const child_return; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {/* kernel thread */ memset (cregs,0 ,sizeof (struct pt_regs ));if (!usp)/* idle thread */
arch/powerpc/kernel/process.c +1 -1
@@ -1736,7 +1736,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,/* Copy registers */ sp -=sizeof (struct pt_regs ); childregs = (struct pt_regs *) sp; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {/* kernel thread */ memset (childregs,0 ,sizeof (struct pt_regs )); childregs->gpr[1 ] = sp +sizeof (struct pt_regs );
arch/riscv/kernel/process.c +1 -1
@@ -125,7 +125,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg,struct pt_regs *childregs =task_pt_regs (p);/* p->thread holds context to be restored by __switch_to() */ -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {/* Kernel thread */ memset (childregs,0 ,sizeof (struct pt_regs )); childregs->gp = gp_in_global;
arch/s390/kernel/process.c +1 -1
@@ -130,7 +130,7 @@ int copy_thread(unsigned long clone_flags, unsigned long new_stackp, frame->sf.gprs[9 ] = (unsigned long )frame;/* Store access registers to kernel stack of new process. */ -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {/* kernel thread */ memset (&frame->childregs,0 ,sizeof (struct pt_regs )); frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
arch/sh/kernel/process_32.c +1 -1
@@ -114,7 +114,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg, childregs =task_pt_regs (p); p->thread.sp = (unsigned long ) childregs; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (childregs,0 ,sizeof (struct pt_regs )); p->thread.pc = (unsigned long ) ret_from_kernel_thread; childregs->regs[4 ] = arg;
arch/sparc/kernel/process_32.c +1 -1
@@ -296,7 +296,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, ti->ksp = (unsigned long ) new_stack; p->thread.kregs = childregs; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {extern int nwindows;unsigned long psr;memset (new_stack,0 ,STACKFRAME_SZ +TRACEREG_SZ );
arch/sparc/kernel/process_64.c +1 -1
@@ -594,7 +594,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, sizeof(struct sparc_stackf)); t->fpsaved[0 ] =0 ; -if (unlikely (p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (unlikely (p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {memset (child_trap_frame,0 , child_stack_sz);__thread_flag_byte_ptr (t)[TI_FLAG_BYTE_CWP ] = (current_pt_regs ()->tstate +1 ) &TSTATE_CWP ;
arch/um/kernel/process.c +1 -1
@@ -158,7 +158,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, struct task_struct * p, unsigned long tls) { void (*handler)(void); -int kthread = current->flags & (PF_KTHREAD |PF_IO_WORKER ); +int kthread = current->flags & (PF_KTHREAD |PF_USER_WORKER );int ret =0 ; p->thread = (struct thread_struct )INIT_THREAD ;
arch/x86/kernel/fpu/core.c +2 -2
@@ -579,10 +579,10 @@ int fpu_clone(struct task_struct *dst, unsigned long clone_flags)set_tsk_thread_flag (dst,TIF_NEED_FPU_LOAD );/* -* No FPU state inheritance for kernel threads and IO +* No FPU state inheritance for kernel threads and user * worker threads. */ -if (dst->flags & (PF_KTHREAD |PF_IO_WORKER )) { +if (dst->flags & (PF_KTHREAD |PF_USER_WORKER )) {/* Clear out the minimal state */ memcpy (&dst_fpu->fpstate->regs, &init_fpstate.regs,init_fpstate_copy_size ());
arch/x86/kernel/process.c +1 -1
@@ -195,7 +195,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg,task_user_gs (p) =get_user_gs (current_pt_regs ());#endif -if (unlikely (p->flags &PF_IO_WORKER )) { +if (unlikely (p->flags &PF_USER_WORKER )) {/* * An IO thread is a user space thread, but it doesn't * return to ret_after_fork().
arch/xtensa/kernel/process.c +1 -1
@@ -224,7 +224,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,#error Unsupported Xtensa ABI#endif -if (!(p->flags & (PF_KTHREAD |PF_IO_WORKER ))) { +if (!(p->flags & (PF_KTHREAD |PF_USER_WORKER ))) {struct pt_regs *regs =current_pt_regs ();unsigned long usp = usp_thread_fn ? usp_thread_fn : regs->areg[1 ];
include/linux/sched.h +1 -0
@@ -1691,6 +1691,7 @@ extern struct pid *cad_pid;#define PF_KTHREAD 0x00200000/* I am a kernel thread */ #define PF_RANDOMIZE 0x00400000/* Randomize virtual address space */ #define PF_SWAPWRITE 0x00800000/* Allowed to write to swap */ +#define PF_USER_WORKER 0x01000000/* Kernel thread cloned from userspace thread */ #define PF_NO_SETAFFINITY 0x04000000/* Userland is not allowed to meddle with cpus_mask */ #define PF_MCE_EARLY 0x08000000/* Early kill for mce process policy */ #define PF_MEMALLOC_PIN 0x10000000/* Allocation context constrained to zones which allow long term pinning. */
include/linux/sched/task.h +2 -1
@@ -18,7 +18,8 @@ struct css_set;/* All the bits taken by the old clone syscall. */ #define CLONE_LEGACY_FLAGS 0xffffffffULL -#define USER_WORKER_IO BIT(0) +#define USER_WORKER BIT(0) +#define USER_WORKER_IO BIT(1)struct kernel_clone_args {u64 flags;
kernel/fork.c +5 -1
@@ -1998,6 +1998,10 @@ static __latent_entropy struct task_struct *copy_process( p =dup_task_struct (current, node);if (!p) goto fork_out; + +if (args->worker_flags &USER_WORKER ) + p->flags |=PF_USER_WORKER ; +if (args->worker_flags &USER_WORKER_IO ) {/* * Mark us an IO worker, and block any signal that isn't
@@ -2497,7 +2501,7 @@ struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node) .exit_signal = (lower_32_bits(flags) & CSIGNAL), .stack = (unsigned long)fn, .stack_size = (unsigned long)arg, - .worker_flags = USER_WORKER_IO, + .worker_flags = USER_WORKER | USER_WORKER_IO, };return copy_process (NULL ,0 , node, &args);
mm/vmscan.c +2 -2
@@ -1061,12 +1061,12 @@ void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason)DEFINE_WAIT (wait);/* -* Do not throttle IO workers, kthreads other than kswapd or +* Do not throttle user workers, kthreads other than kswapd or * workqueues. They may be required for reclaim to make * forward progress (e.g. journalling workqueues or kthreads). */ if (!current_is_kswapd () && - current->flags & (PF_IO_WORKER |PF_KTHREAD )) + current->flags & (PF_USER_WORKER |PF_KTHREAD ))return ;/*
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH 3/8 fork: add USER_WORKER flag to not dup/clone files
In reply to Mike ChristieEach vhost device gets a thread that is used to perform IO and management operations. Instead of a thread that is accessing a device, the thread is part of the device, so when it creates a thread using a helper based on copy_process we can't dup or clone the parent's files/FDS because it would do an extra increment on ourself.
Later, when we do:
Qemu process exits:
do_exit -> exit_files -> put_files_struct -> close_files
we would leak the device's resources because of that extra refcount on the fd or file_struct.
This patch adds a no_files option so these worker threads can prevent taking an extra refcount on themselves.
--- include/linux/sched/task.h | 1 + kernel/fork.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-)
include/linux/sched/task.h +1 -0
@@ -20,6 +20,7 @@ struct css_set;#define USER_WORKER BIT(0)#define USER_WORKER_IO BIT(1) +#define USER_WORKER_NO_FILES BIT(2)struct kernel_clone_args {u64 flags;
kernel/fork.c +9 -2
@@ -1530,7 +1530,8 @@ static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)return 0 ; } -static int copy_files (unsigned long clone_flags,struct task_struct *tsk) +static int copy_files (unsigned long clone_flags,struct task_struct *tsk, +int no_files) {struct files_struct *oldf, *newf;int error =0 ;
@@ -1542,6 +1543,11 @@ static int copy_files(unsigned long clone_flags, struct task_struct *tsk)if (!oldf) goto out; +if (no_files) { + tsk->files =NULL ; + goto out; + } +if (clone_flags &CLONE_FILES ) {atomic_inc (&oldf->count); goto out;
@@ -2153,7 +2159,8 @@ static __latent_entropy struct task_struct *copy_process( retval =copy_semundo (clone_flags, p);if (retval) goto bad_fork_cleanup_security; - retval =copy_files (clone_flags, p); + retval =copy_files (clone_flags, p, + args->worker_flags &USER_WORKER_NO_FILES );if (retval) goto bad_fork_cleanup_semundo; retval =copy_fs (clone_flags, p);
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH 4/8 fork: Add USER_WORKER flag to ignore signals
In reply to Mike ChristieFrom: Christian Brauner <[email protected]>
Since:
commit 10ab825bdef8 ("change kernel threads to ignore signals instead of blocking them")
kthreads have been ignoring signals by default, and the vhost layer has never had a need to change that. This patch adds an option flag, USER_WORKER_SIG_IGN, handled in copy_process() after copy_sighand() and copy_signals() so vhost_tasks added in the next patches can continue to ignore singals.
--- include/linux/sched/task.h | 1 + kernel/fork.c | 3 +++ 2 files changed, 4 insertions(+)
include/linux/sched/task.h +1 -0
@@ -21,6 +21,7 @@ struct css_set;#define USER_WORKER BIT(0)#define USER_WORKER_IO BIT(1)#define USER_WORKER_NO_FILES BIT(2) +#define USER_WORKER_SIG_IGN BIT(3)struct kernel_clone_args {u64 flags;
kernel/fork.c +3 -0
@@ -2185,6 +2185,9 @@ static __latent_entropy struct task_struct *copy_process(if (retval) goto bad_fork_cleanup_io; +if (args->worker_flags &USER_WORKER_SIG_IGN ) +ignore_signals (p); +stackleak_task_init (p);if (pid != &init_struct_pid) {
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH 5/8 fork: allow kernel code to call copy_process
In reply to Mike ChristieThe next patch adds helpers like create_io_thread, but for use by the vhost layer. There are several functions, so they are in their own file instead of cluttering up fork.c. This patch allows that new file to call copy_process.
--- include/linux/sched/task.h | 2 ++ kernel/fork.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/sched/task.h +2 -0
@@ -90,6 +90,8 @@ extern void exit_files(struct task_struct *);extern void exit_itimers (struct signal_struct *);extern pid_t kernel_clone (struct kernel_clone_args *kargs); +struct task_struct *copy_process (struct pid *pid,int trace,int node, +struct kernel_clone_args *args);struct task_struct *create_io_thread (int (*fn)(void *),void *arg,int node);struct task_struct *fork_idle (int );struct mm_struct *copy_init_mm (void );
kernel/fork.c +1 -1
@@ -1905,7 +1905,7 @@ static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk)* parts of the process environment (as per the clone * flags). The actual kick-off is left to the caller. */ -static __latent_entropy struct task_struct *copy_process( +__latent_entropy struct task_struct *copy_process( struct pid *pid, int trace, int node,
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH 6/8 vhost_task: Allow vhost layer to use copy_process
In reply to Mike ChristieQemu will create vhost devices in the kernel which perform network, SCSI, etc IO and management operations from worker threads created by the kthread API. Because the kthread API does a copy_process on the kthreadd thread, the vhost layer has to use kthread_use_mm to access the Qemu thread's memory and cgroup_attach_task_all to add itself to the Qemu thread's cgroups, and it bypasses the RLIMIT_NPROC limit.
This patch adds a new struct vhost_task which can be used instead of kthreads. They allow the vhost layer to use copy_process and inherit the userspace process's mm and cgroups and the task is accounted for under the userspace's nproc count.
--- MAINTAINERS | 2 + drivers/vhost/Kconfig | 5 ++ include/linux/sched/vhost_task.h | 23 ++++++ kernel/Makefile | 1 + kernel/vhost_task.c | 123 +++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 include/linux/sched/vhost_task.h create mode 100644 kernel/vhost_task.c
MAINTAINERS +2 -0
@@ -20464,7 +20464,9 @@ L: [email protected] L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git +F: kernel/vhost_task.c F: drivers/vhost/ +F: include/linux/sched/vhost_task.h F: include/linux/vhost_iotlb.h F: include/uapi/linux/vhost.h
drivers/vhost/Kconfig +5 -0
@@ -13,9 +13,14 @@ config VHOST_RING This option is selected by any driver which needs to access the host side of a virtio ring. +config VHOST_TASK + bool + default n + config VHOST tristate select VHOST_IOTLB + select VHOST_TASK help This option is selected by any driver which needs to access the core of vhost.
include/linux/sched/vhost_task.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_VHOST_TASK_H +#define _LINUX_VHOST_TASK_H + +#include <linux/completion.h> + +struct task_struct ; + +struct vhost_task { +int (*fn)(void *data); +void *data; +struct completion exited; +unsigned long flags; +struct task_struct *task; +}; + +struct vhost_task *vhost_task_create (int (*fn)(void *),void *arg,int node); +__printf(2, 3) +void vhost_task_start (struct vhost_task *vtsk,const char namefmt[], ...); +void vhost_task_stop (struct vhost_task *vtsk); +bool vhost_task_should_stop (struct vhost_task *vtsk); + +#endif
kernel/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-y = fork.o exec_domain.o panic.o \ obj-$(CONFIG_USERMODE_DRIVER) += usermode_driver.o obj-$(CONFIG_MODULES) += kmod.o obj-$(CONFIG_MULTIUSER) += groups.o +obj-$(CONFIG_VHOST_TASK) += vhost_task.o ifdef CONFIG_FUNCTION_TRACER # Do not trace internal ftrace files
kernel/vhost_task.c new file +123 -0
123 lines not on this page · raw message
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
kernel test robot
build botRe: [PATCH 6/8] vhost_task: Allow vhost layer to use copy_process
Thank you for the patch! Perhaps something to improve:
Hi Mike,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on mst-vhost/linux-next]
[also build test WARNING on tip/x86/core linus/master v5.17-rc2] [cannot apply to davem-sparc/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Mike-Christie/Use-copy_process-in-vhost-layer/20220203-050454 base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next config: x86_64-randconfig-s021 (https://download.01.org/0day-ci/archive/20220203/202202032136.uq6pXzYT-lkp(a)intel.com/config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0 reproduce: # apt-get install sparse # sparse version: v0.6.4-dirty # https://github.com/0day-ci/linux/commit/2c7380ae8136c224f4c7074027303b97b0a0f84c git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Mike-Christie/Use-copy_process-in-vhost-layer/20220203-050454 git checkout 2c7380ae8136c224f4c7074027303b97b0a0f84c # save the config file to linux build tree mkdir build_dir make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=x86_64 SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
sparse warnings: (new ones prefixed by >>)
>> kernel/vhost_task.c:85:24: sparse: sparse: incorrect type in argument 1 (different base types) @@ expected unsigned long [usertype] size @@ got restricted gfp_t @@ kernel/vhost_task.c:85:24: sparse: expected unsigned long [usertype] size kernel/vhost_task.c:85:24: sparse: got restricted gfp_t >> kernel/vhost_task.c:85:36: sparse: sparse: incorrect type in argument 2 (different base types) @@ expected restricted gfp_t [usertype] flags @@ got unsigned long @@
kernel/vhost_task.c:85:36: sparse: expected restricted gfp_t [usertype] flags
kernel/vhost_task.c:85:36: sparse: got unsigned long
vim +85 kernel/vhost_task.c
62
63 /**
64 * vhost_task_create - create a copy of a process to be used by the kernel
65 * @fn: thread stack
66 * @arg: data to be passed to fn
67 * @node: numa node to allocate task from
68 *
69 * This returns a specialized task for use by the vhost layer or NULL on
70 * failure. The returned task is inactive, and the caller must fire it up
71 * through vhost_task_start().
72 */
73 struct vhost_task *vhost_task_create(int (*fn)(void *), void *arg, int node)
74 {
75 struct kernel_clone_args args = {
76 .flags = CLONE_FS | CLONE_UNTRACED | CLONE_VM,
77 .exit_signal = 0,
78 .stack = (unsigned long)vhost_task_fn,
79 .worker_flags = USER_WORKER | USER_WORKER_NO_FILES |
80 USER_WORKER_SIG_IGN,
81 };
82 struct vhost_task *vtsk;
83 struct task_struct *tsk;
84
> 85 vtsk = kzalloc(GFP_KERNEL, sizeof(*vtsk));
86 if (!vtsk)
87 return ERR_PTR(-ENOMEM);
88
89 init_completion(&vtsk->exited);
90 vtsk->data = arg;
91 vtsk->fn = fn;
92 args.stack_size = (unsigned long)vtsk;
93
94 tsk = copy_process(NULL, 0, node, &args);
95 if (IS_ERR(tsk)) {
96 kfree(vtsk);
97 return NULL;
98 }
99 100 vtsk->task = tsk;
101
102 return vtsk;
103 }
104 EXPORT_SYMBOL_GPL(vhost_task_create);
105
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH 6/8] vhost_task: Allow vhost layer to use copy_process
In reply to Mike ChristieThank you for the patch! Perhaps something to improve:
Hi Mike,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on mst-vhost/linux-next]
[also build test WARNING on tip/x86/core linus/master v5.17-rc2 next-20220203] [cannot apply to davem-sparc/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Mike-Christie/Use-copy_process-in-vhost-layer/20220203-050454 base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next config: sparc-randconfig-s032-20220130 (https://download.01.org/0day-ci/archive/20220203/202202032131.gnmg7b6h-lkp(a)intel.com/config) compiler: sparc-linux-gcc (GCC) 11.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-dirty
# https://github.com/0day-ci/linux/commit/2c7380ae8136c224f4c7074027303b97b0a0f84c
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Mike-Christie/Use-copy_process-in-vhost-layer/20220203-050454
git checkout 2c7380ae8136c224f4c7074027303b97b0a0f84c
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=sparc SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
sparse warnings: (new ones prefixed by >>)
>> kernel/vhost_task.c:85:24: sparse: sparse: incorrect type in argument 1 (different base types) @@ expected unsigned int [usertype] size @@ got restricted gfp_t @@ kernel/vhost_task.c:85:24: sparse: expected unsigned int [usertype] size kernel/vhost_task.c:85:24: sparse: got restricted gfp_t >> kernel/vhost_task.c:85:36: sparse: sparse: incorrect type in argument 2 (different base types) @@ expected restricted gfp_t [usertype] flags @@ got unsigned int @@
kernel/vhost_task.c:85:36: sparse: expected restricted gfp_t [usertype] flags
kernel/vhost_task.c:85:36: sparse: got unsigned int
vim +85 kernel/vhost_task.c
62
63 /**
64 * vhost_task_create - create a copy of a process to be used by the kernel
65 * @fn: thread stack
66 * @arg: data to be passed to fn
67 * @node: numa node to allocate task from
68 *
69 * This returns a specialized task for use by the vhost layer or NULL on
70 * failure. The returned task is inactive, and the caller must fire it up
71 * through vhost_task_start().
72 */
73 struct vhost_task *vhost_task_create(int (*fn)(void *), void *arg, int node)
74 {
75 struct kernel_clone_args args = {
76 .flags = CLONE_FS | CLONE_UNTRACED | CLONE_VM,
77 .exit_signal = 0,
78 .stack = (unsigned long)vhost_task_fn,
79 .worker_flags = USER_WORKER | USER_WORKER_NO_FILES |
80 USER_WORKER_SIG_IGN,
81 };
82 struct vhost_task *vtsk;
83 struct task_struct *tsk;
84
> 85 vtsk = kzalloc(GFP_KERNEL, sizeof(*vtsk));
86 if (!vtsk)
87 return ERR_PTR(-ENOMEM);
88
89 init_completion(&vtsk->exited);
90 vtsk->data = arg;
91 vtsk->fn = fn;
92 args.stack_size = (unsigned long)vtsk;
93
94 tsk = copy_process(NULL, 0, node, &args);
95 if (IS_ERR(tsk)) {
96 kfree(vtsk);
97 return NULL;
98 }
99 100 vtsk->task = tsk;
101
102 return vtsk;
103 }
104 EXPORT_SYMBOL_GPL(vhost_task_create);
105
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
PATCH 7/8 vhost: move worker thread fields to new struct
In reply to Mike ChristieThis is just a prep patch. It moves the worker related fields to a new vhost_worker struct and moves the code around to create some helpers that will be used in the next patch.
--- drivers/vhost/vhost.c | 98 ++++++++++++++++++++++++++++--------------- drivers/vhost/vhost.h | 11 +++-- 2 files changed, 72 insertions(+), 37 deletions(-)
drivers/vhost/vhost.c +64 -34
@@ -263,8 +263,8 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)* sure it was not in the list. * test_and_set_bit() implies a memory barrier. */ -llist_add (&work->node, &dev->work_list); -wake_up_process (dev->worker); +llist_add (&work->node, &dev->worker->work_list); +wake_up_process (dev->worker->task); } }EXPORT_SYMBOL_GPL (vhost_work_queue);
@@ -272,7 +272,7 @@ EXPORT_SYMBOL_GPL(vhost_work_queue);/* A lockless hint for busy polling code to exit the loop */ bool vhost_has_work (struct vhost_dev *dev) { -return !llist_empty (&dev->work_list); +return dev->worker && !llist_empty (&dev->worker->work_list); }EXPORT_SYMBOL_GPL (vhost_has_work);
@@ -343,7 +343,8 @@ static void vhost_vq_reset(struct vhost_dev *dev,static int vhost_worker (void *data) { -struct vhost_dev *dev = data; +struct vhost_worker *worker = data; +struct vhost_dev *dev = worker->dev;struct vhost_work *work, *work_next;struct llist_node *node;
@@ -358,7 +359,7 @@ static int vhost_worker(void *data)break ; } - node =llist_del_all (&dev->work_list); + node =llist_del_all (&worker->work_list);if (!node)schedule ();
@@ -368,7 +369,7 @@ static int vhost_worker(void *data)llist_for_each_entry_safe (work, work_next, node, node) {clear_bit (VHOST_WORK_QUEUED , &work->flags);__set_current_state (TASK_RUNNING ); -kcov_remote_start_common (dev->kcov_handle); +kcov_remote_start_common (worker->kcov_handle); work->fn (work);kcov_remote_stop ();if (need_resched ())
@@ -487,7 +488,6 @@ void vhost_dev_init(struct vhost_dev *dev, dev->byte_weight = byte_weight; dev->use_worker = use_worker; dev->msg_handler = msg_handler; -init_llist_head (&dev->work_list);init_waitqueue_head (&dev->wait);INIT_LIST_HEAD (&dev->read_list);INIT_LIST_HEAD (&dev->pending_list);
@@ -579,10 +579,60 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =NULL ; } +static void vhost_worker_free (struct vhost_dev *dev) +{ +struct vhost_worker *worker = dev->worker; + +if (!worker) +return ; + + dev->worker =NULL ; +WARN_ON (!llist_empty (&worker->work_list)); +kthread_stop (worker->task); +kfree (worker); +} + +static int vhost_worker_create (struct vhost_dev *dev) +{ +struct vhost_worker *worker; +struct task_struct *task; +int ret; + + worker =kzalloc (sizeof (*worker),GFP_KERNEL_ACCOUNT ); +if (!worker) +return -ENOMEM ; + + dev->worker = worker; + worker->dev = dev; + worker->kcov_handle =kcov_common_handle (); +init_llist_head (&worker->work_list); + + task =kthread_create (vhost_worker, worker,"vhost-%d" , current->pid); +if (IS_ERR (task)) { + ret =PTR_ERR (task); + goto free_worker; + } + + worker->task = task; +wake_up_process (task);/* avoid contributing to loadavg */ + + ret =vhost_attach_cgroups (dev); +if (ret) + goto stop_worker; + +return 0 ; + +stop_worker: +kthread_stop (worker->task); +free_worker: +kfree (worker); + dev->worker =NULL ; +return ret; +} +/* Caller should have device mutex */ long vhost_dev_set_owner (struct vhost_dev *dev) { -struct task_struct *worker;int err;/* Is there an owner already? */
@@ -593,36 +643,21 @@ long vhost_dev_set_owner(struct vhost_dev *dev)vhost_attach_mm (dev); - dev->kcov_handle =kcov_common_handle ();if (dev->use_worker) { - worker =kthread_create (vhost_worker, dev, -"vhost-%d" , current->pid); -if (IS_ERR (worker)) { - err =PTR_ERR (worker); - goto err_worker; - } - - dev->worker = worker; -wake_up_process (worker);/* avoid contributing to loadavg */ - - err =vhost_attach_cgroups (dev); + err =vhost_worker_create (dev);if (err) - goto err_cgroup; + goto err_worker; } err =vhost_dev_alloc_iovecs (dev);if (err) - goto err_cgroup; + goto err_iovecs;return 0 ; -err_cgroup: -if (dev->worker) { -kthread_stop (dev->worker); - dev->worker =NULL ; - } +err_iovecs: +vhost_worker_free (dev); err_worker:vhost_detach_mm (dev); - dev->kcov_handle =0 ; err_mm:return err; }
@@ -712,12 +747,7 @@ void vhost_dev_cleanup(struct vhost_dev *dev) dev->iotlb =NULL ;vhost_clear_msg (dev);wake_up_interruptible_poll (&dev->wait,EPOLLIN |EPOLLRDNORM ); -WARN_ON (!llist_empty (&dev->work_list)); -if (dev->worker) { -kthread_stop (dev->worker); - dev->worker =NULL ; - dev->kcov_handle =0 ; - } +vhost_worker_free (dev);vhost_detach_mm (dev); }EXPORT_SYMBOL_GPL (vhost_dev_cleanup);
drivers/vhost/vhost.h +8 -3
@@ -25,6 +25,13 @@ struct vhost_work {unsigned long flags; }; +struct vhost_worker { +struct task_struct *task; +struct llist_head work_list; +struct vhost_dev *dev; +u64 kcov_handle; +}; +/* Poll a file (eventfd or socket) */ /* Note: there's nothing vhost specific about this structure. */ struct vhost_poll {
@@ -148,8 +155,7 @@ struct vhost_dev {struct vhost_virtqueue **vqs;int nvqs;struct eventfd_ctx *log_ctx; -struct llist_head work_list; -struct task_struct *worker; +struct vhost_worker *worker;struct vhost_iotlb *umem;struct vhost_iotlb *iotlb;spinlock_t iotlb_lock;
@@ -159,7 +165,6 @@ struct vhost_dev {int iov_limit;int weight;int byte_weight; -u64 kcov_handle;bool use_worker;int (*msg_handler)(struct vhost_dev *dev,struct vhost_iotlb_msg *msg);
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH 8/8 vhost: use vhost_tasks for worker threads
In reply to Mike ChristieFor vhost workers we use the kthread API which inherit's its values from and checks against the kthreadd thread. This results in the wrong RLIMITs being checked, so while tools like libvirt try to control the number of threads based on the nproc rlimit setting we can end up creating more threads than the user wanted.
This patch has us use the vhost_task helpers which will inherit its values/checks from the thread that owns the device similar to if we did a clone in userspace.
--- drivers/vhost/vhost.c | 58 ++++++++----------------------------------- drivers/vhost/vhost.h | 4 +-- 2 files changed, 13 insertions(+), 49 deletions(-)
drivers/vhost/vhost.c +11 -47
@@ -22,11 +22,11 @@#include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/kthread.h> -#include <linux/cgroup.h> #include <linux/module.h> #include <linux/sort.h> #include <linux/sched/mm.h> #include <linux/sched/signal.h> +#include <linux/sched/vhost_task.h> #include <linux/interval_tree_generic.h> #include <linux/nospec.h> #include <linux/kcov.h>
@@ -264,7 +264,7 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)* test_and_set_bit() implies a memory barrier. */ llist_add (&work->node, &dev->worker->work_list); -wake_up_process (dev->worker->task); +wake_up_process (dev->worker->vtsk->task); } }EXPORT_SYMBOL_GPL (vhost_work_queue);
@@ -344,17 +344,14 @@ static void vhost_vq_reset(struct vhost_dev *dev,static int vhost_worker (void *data) {struct vhost_worker *worker = data; -struct vhost_dev *dev = worker->dev;struct vhost_work *work, *work_next;struct llist_node *node; -kthread_use_mm (dev->mm); -for (;;) {/* mb paired w/ kthread_stop */ set_current_state (TASK_INTERRUPTIBLE ); -if (kthread_should_stop ()) { +if (vhost_task_should_stop (worker->vtsk)) {__set_current_state (TASK_RUNNING );break ; }
@@ -376,7 +373,7 @@ static int vhost_worker(void *data)schedule (); } } -kthread_unuse_mm (dev->mm); +return 0 ; }
@@ -517,31 +514,6 @@ long vhost_dev_check_owner(struct vhost_dev *dev) }EXPORT_SYMBOL_GPL (vhost_dev_check_owner); -struct vhost_attach_cgroups_struct { -struct vhost_work work; -struct task_struct *owner; -int ret; -}; - -static void vhost_attach_cgroups_work (struct vhost_work *work) -{ -struct vhost_attach_cgroups_struct *s; - - s = container_of(work, struct vhost_attach_cgroups_struct, work); - s->ret =cgroup_attach_task_all (s->owner, current); -} - -static int vhost_attach_cgroups (struct vhost_dev *dev) -{ -struct vhost_attach_cgroups_struct attach; - - attach.owner = current; -vhost_work_init (&attach.work, vhost_attach_cgroups_work); -vhost_work_queue (dev, &attach.work); -vhost_work_dev_flush (dev); -return attach.ret; -} -/* Caller should have device mutex */ bool vhost_dev_has_owner (struct vhost_dev *dev) {
@@ -588,14 +560,14 @@ static void vhost_worker_free(struct vhost_dev *dev) dev->worker =NULL ;WARN_ON (!llist_empty (&worker->work_list)); -kthread_stop (worker->task); +vhost_task_stop (worker->vtsk);kfree (worker); }static int vhost_worker_create (struct vhost_dev *dev) {struct vhost_worker *worker; -struct task_struct *task; +struct vhost_task *vtsk;int ret; worker =kzalloc (sizeof (*worker),GFP_KERNEL_ACCOUNT );
@@ -603,27 +575,19 @@ static int vhost_worker_create(struct vhost_dev *dev)return -ENOMEM ; dev->worker = worker; - worker->dev = dev; worker->kcov_handle =kcov_common_handle ();init_llist_head (&worker->work_list); - task =kthread_create (vhost_worker, worker,"vhost-%d" , current->pid); -if (IS_ERR (task)) { - ret =PTR_ERR (task); + vtsk =vhost_task_create (vhost_worker, worker,NUMA_NO_NODE ); +if (!vtsk) { + ret = -ENOMEM ; goto free_worker; } - worker->task = task; -wake_up_process (task);/* avoid contributing to loadavg */ - - ret =vhost_attach_cgroups (dev); -if (ret) - goto stop_worker; - + worker->vtsk = vtsk; +vhost_task_start (vtsk,"vhost-%d" , current->pid);return 0 ; -stop_worker: -kthread_stop (worker->task); free_worker:kfree (worker); dev->worker =NULL ;
drivers/vhost/vhost.h +2 -2
@@ -16,6 +16,7 @@#include <linux/irqbypass.h> struct vhost_work ; +struct vhost_task ;typedef void (*vhost_work_fn_t )(struct vhost_work *work);#define VHOST_WORK_QUEUED 1
@@ -26,9 +27,8 @@ struct vhost_work { };struct vhost_worker { -struct task_struct *task; +struct vhost_task *vtsk;struct llist_head work_list; -struct vhost_dev *dev;u64 kcov_handle; };
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization