diff --git a/sys/compat/linuxkpi/common/include/linux/pid.h b/sys/compat/linuxkpi/common/include/linux/pid.h new file mode 100644 index 000000000000..2c7e0eaf706f --- /dev/null +++ b/sys/compat/linuxkpi/common/include/linux/pid.h @@ -0,0 +1,65 @@ +/*- + * Copyright (c) 2017 Mellanox Technologies, Ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _LINUX_PID_H_ +#define _LINUX_PID_H_ + +#include +#include +#include + +enum pid_type { + PIDTYPE_PID, + PIDTYPE_PGID, + PIDTYPE_SID, + PIDTYPE_MAX +}; + +#define pid_nr(n) (n) +#define pid_vnr(n) (n) +#define from_kuid_munged(a, uid) (uid) + +#define pid_task(pid, type) ({ \ + struct task_struct *__ts; \ + CTASSERT((type) == PIDTYPE_PID); \ + __ts = linux_pid_task(pid); \ + __ts; \ +}) + +#define get_pid_task(pid, type) ({ \ + struct task_struct *__ts; \ + CTASSERT((type) == PIDTYPE_PID); \ + __ts = linux_get_pid_task(pid); \ + __ts; \ +}) + +struct task_struct; +extern struct task_struct *linux_pid_task(pid_t); +extern struct task_struct *linux_get_pid_task(pid_t); + +#endif /* _LINUX_PID_H_ */ diff --git a/sys/compat/linuxkpi/common/include/linux/sched.h b/sys/compat/linuxkpi/common/include/linux/sched.h index 3ea479d2b85d..4439fc00c092 100644 --- a/sys/compat/linuxkpi/common/include/linux/sched.h +++ b/sys/compat/linuxkpi/common/include/linux/sched.h @@ -38,7 +38,9 @@ #include #include +#include #include +#include #include #include @@ -59,9 +61,10 @@ struct task_struct { linux_task_fn_t *task_fn; void *task_data; int task_ret; + atomic_t usage; int state; atomic_t kthread_flags; - pid_t pid; + pid_t pid; /* BSD thread ID */ const char *comm; void *bsd_ioctl_data; unsigned bsd_ioctl_len; @@ -71,16 +74,30 @@ struct task_struct { #define current ((struct task_struct *)curthread->td_lkpi_task) -#define task_pid(task) ((task)->task_thread->td_proc->p_pid) -#define task_pid_nr(task) ((task)->task_thread->td_tid) -#define get_pid(x) (x) -#define put_pid(x) +#define task_pid_group_leader(task) \ + FIRST_THREAD_IN_PROC((task)->task_thread->td_proc)->td_tid +#define task_pid(task) ((task)->pid) +#define task_pid_nr(task) ((task)->pid) +#define get_pid(x) (x) +#define put_pid(x) do { } while (0) #define current_euid() (curthread->td_ucred->cr_uid) #define set_current_state(x) \ atomic_store_rel_int((volatile int *)¤t->state, (x)) #define __set_current_state(x) current->state = (x) +static inline void +get_task_struct(struct task_struct *task) +{ + atomic_inc(&task->usage); +} + +static inline void +put_task_struct(struct task_struct *task) +{ + if (atomic_dec_and_test(&task->usage)) + linux_free_current(task); +} #define schedule() \ do { \ diff --git a/sys/compat/linuxkpi/common/src/linux_current.c b/sys/compat/linuxkpi/common/src/linux_current.c index 01ddc0e5dcc9..19f9e47c9758 100644 --- a/sys/compat/linuxkpi/common/src/linux_current.c +++ b/sys/compat/linuxkpi/common/src/linux_current.c @@ -62,6 +62,7 @@ linux_alloc_current(struct thread *td, int flags) ts->comm = td->td_name; ts->pid = td->td_tid; ts->mm = mm; + atomic_set(&ts->usage, 1); ts->state = TASK_RUNNING; /* setup mm_struct */ @@ -113,7 +114,37 @@ linuxkpi_thread_dtor(void *arg __unused, struct thread *td) return; td->td_lkpi_task = NULL; - linux_free_current(ts); + put_task_struct(ts); +} + +struct task_struct * +linux_pid_task(pid_t pid) +{ + struct thread *td; + + td = tdfind(pid, -1); + if (td != NULL) { + struct task_struct *ts = td->td_lkpi_task; + PROC_UNLOCK(td->td_proc); + return (ts); + } + return (NULL); +} + +struct task_struct * +linux_get_pid_task(pid_t pid) +{ + struct thread *td; + + td = tdfind(pid, -1); + if (td != NULL) { + struct task_struct *ts = td->td_lkpi_task; + if (ts != NULL) + get_task_struct(ts); + PROC_UNLOCK(td->td_proc); + return (ts); + } + return (NULL); } static void diff --git a/sys/compat/linuxkpi/common/src/linux_kthread.c b/sys/compat/linuxkpi/common/src/linux_kthread.c index 90da31f1ad0a..ab5a95601f5e 100644 --- a/sys/compat/linuxkpi/common/src/linux_kthread.c +++ b/sys/compat/linuxkpi/common/src/linux_kthread.c @@ -72,7 +72,7 @@ kthread_stop(struct task_struct *task) * Get return code and free task structure: */ retval = task->task_ret; - linux_free_current(task); + put_task_struct(task); return (retval); }