Date: Fri, 17 Mar 2017 15:40:24 +0000 (UTC) From: Hans Petter Selasky <hselasky@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r315457 - in head/sys/compat/linuxkpi/common: include/linux src Message-ID: <201703171540.v2HFeOxZ099158@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: hselasky Date: Fri Mar 17 15:40:24 2017 New Revision: 315457 URL: https://svnweb.freebsd.org/changeset/base/315457 Log: 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 Added: head/sys/compat/linuxkpi/common/include/linux/pid.h (contents, props changed) Modified: head/sys/compat/linuxkpi/common/include/linux/sched.h head/sys/compat/linuxkpi/common/src/linux_current.c head/sys/compat/linuxkpi/common/src/linux_kthread.c Added: head/sys/compat/linuxkpi/common/include/linux/pid.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/compat/linuxkpi/common/include/linux/pid.h Fri Mar 17 15:40:24 2017 (r315457) @@ -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_ */ Modified: head/sys/compat/linuxkpi/common/include/linux/sched.h ============================================================================== --- head/sys/compat/linuxkpi/common/include/linux/sched.h Fri Mar 17 14:54:10 2017 (r315456) +++ head/sys/compat/linuxkpi/common/include/linux/sched.h Fri Mar 17 15:40:24 2017 (r315457) @@ -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 *)¤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 { \ Modified: head/sys/compat/linuxkpi/common/src/linux_current.c ============================================================================== --- head/sys/compat/linuxkpi/common/src/linux_current.c Fri Mar 17 14:54:10 2017 (r315456) +++ head/sys/compat/linuxkpi/common/src/linux_current.c Fri Mar 17 15:40:24 2017 (r315457) @@ -62,6 +62,7 @@ linux_alloc_current(struct thread *td, i 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, 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 Modified: head/sys/compat/linuxkpi/common/src/linux_kthread.c ============================================================================== --- head/sys/compat/linuxkpi/common/src/linux_kthread.c Fri Mar 17 14:54:10 2017 (r315456) +++ head/sys/compat/linuxkpi/common/src/linux_kthread.c Fri Mar 17 15:40:24 2017 (r315457) @@ -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); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201703171540.v2HFeOxZ099158>