aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorHans Petter Selasky <hselasky@FreeBSD.org>2017-03-17 15:40:24 +0000
committerHans Petter Selasky <hselasky@FreeBSD.org>2017-03-17 15:40:24 +0000
commita0699ebf77fd50aec9504a4f93e303b90af6d1d7 (patch)
tree5d79e099c578ce58c8438c027bd909d433275e31 /sys
parent08a49957b3f06926744987207d542efa2631394d (diff)
downloadsrc-a0699ebf77fd50aec9504a4f93e303b90af6d1d7.tar.gz
src-a0699ebf77fd50aec9504a4f93e303b90af6d1d7.zip
Implement get_pid_task(), pid_task() and some other PID helper
functions in the LinuxKPI. Add a usage atomic to the task_struct structure to facilitate refcounting the task structure when returned from get_pid_task(). The get_task_struct() and put_task_struct() function is used to manage atomic refcounting. After this change the task_struct should only be freed through put_task_struct(). Obtained from: kmacy @ MFC after: 1 week Sponsored by: Mellanox Technologies
Notes
Notes: svn path=/head/; revision=315457
Diffstat (limited to 'sys')
-rw-r--r--sys/compat/linuxkpi/common/include/linux/pid.h65
-rw-r--r--sys/compat/linuxkpi/common/include/linux/sched.h27
-rw-r--r--sys/compat/linuxkpi/common/src/linux_current.c33
-rw-r--r--sys/compat/linuxkpi/common/src/linux_kthread.c2
4 files changed, 120 insertions, 7 deletions
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 <sys/param.h>
+#include <sys/systm.h>
+#include <sys/proc.h>
+
+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 <sys/sleepqueue.h>
#include <linux/types.h>
+#include <linux/compat.h>
#include <linux/completion.h>
+#include <linux/pid.h>
#include <linux/slab.h>
#include <linux/mm_types.h>
@@ -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 *)&current->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);
}