Path: blob/main/sys/compat/linuxkpi/common/src/linux_current.c
39586 views
/*-1* Copyright (c) 2017 Hans Petter Selasky2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice unmodified, this list of conditions, and the following9* disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#include <sys/cdefs.h>27#ifdef __amd64__28#define DEV_APIC29#elif defined(__i386__)30#include "opt_apic.h"31#endif3233#include <linux/compat.h>34#include <linux/completion.h>35#include <linux/mm.h>36#include <linux/kthread.h>37#include <linux/moduleparam.h>3839#include <sys/kernel.h>40#include <sys/eventhandler.h>41#include <sys/malloc.h>42#include <sys/sysctl.h>43#include <vm/uma.h>4445#ifdef DEV_APIC46extern u_int first_msi_irq, num_msi_irqs;47#endif4849static eventhandler_tag linuxkpi_thread_dtor_tag;5051static uma_zone_t linux_current_zone;52static uma_zone_t linux_mm_zone;5354/* check if another thread already has a mm_struct */55static struct mm_struct *56find_other_mm(struct proc *p)57{58struct thread *td;59struct task_struct *ts;60struct mm_struct *mm;6162PROC_LOCK_ASSERT(p, MA_OWNED);63FOREACH_THREAD_IN_PROC(p, td) {64ts = td->td_lkpi_task;65if (ts == NULL)66continue;67mm = ts->mm;68if (mm == NULL)69continue;70/* try to share other mm_struct */71if (atomic_inc_not_zero(&mm->mm_users))72return (mm);73}74return (NULL);75}7677int78linux_alloc_current(struct thread *td, int flags)79{80struct proc *proc;81struct task_struct *ts;82struct mm_struct *mm, *mm_other;8384MPASS(td->td_lkpi_task == NULL);8586if ((td->td_pflags & TDP_ITHREAD) != 0 || !THREAD_CAN_SLEEP()) {87flags &= ~M_WAITOK;88flags |= M_NOWAIT | M_USE_RESERVE;89}9091ts = uma_zalloc(linux_current_zone, flags | M_ZERO);92if (ts == NULL) {93if ((flags & (M_WAITOK | M_NOWAIT)) == M_WAITOK)94panic("linux_alloc_current: failed to allocate task");95return (ENOMEM);96}97mm = NULL;9899/* setup new task structure */100atomic_set(&ts->kthread_flags, 0);101ts->task_thread = td;102ts->comm = td->td_name;103ts->pid = td->td_tid;104ts->group_leader = ts;105atomic_set(&ts->usage, 1);106atomic_set(&ts->state, TASK_RUNNING);107init_completion(&ts->parked);108init_completion(&ts->exited);109110proc = td->td_proc;111112PROC_LOCK(proc);113mm_other = find_other_mm(proc);114115/* use allocated mm_struct as a fallback */116if (mm_other == NULL) {117PROC_UNLOCK(proc);118mm = uma_zalloc(linux_mm_zone, flags | M_ZERO);119if (mm == NULL) {120if ((flags & (M_WAITOK | M_NOWAIT)) == M_WAITOK)121panic(122"linux_alloc_current: failed to allocate mm");123uma_zfree(linux_current_zone, mm);124return (ENOMEM);125}126127PROC_LOCK(proc);128mm_other = find_other_mm(proc);129if (mm_other == NULL) {130/* setup new mm_struct */131init_rwsem(&mm->mmap_sem);132atomic_set(&mm->mm_count, 1);133atomic_set(&mm->mm_users, 1);134/* set mm_struct pointer */135ts->mm = mm;136/* clear pointer to not free memory */137mm = NULL;138} else {139ts->mm = mm_other;140}141} else {142ts->mm = mm_other;143}144145/* store pointer to task struct */146td->td_lkpi_task = ts;147PROC_UNLOCK(proc);148149/* free mm_struct pointer, if any */150uma_zfree(linux_mm_zone, mm);151152return (0);153}154155struct mm_struct *156linux_get_task_mm(struct task_struct *task)157{158struct mm_struct *mm;159160mm = task->mm;161if (mm != NULL) {162atomic_inc(&mm->mm_users);163return (mm);164}165return (NULL);166}167168void169linux_mm_dtor(struct mm_struct *mm)170{171uma_zfree(linux_mm_zone, mm);172}173174void175linux_free_current(struct task_struct *ts)176{177mmput(ts->mm);178uma_zfree(linux_current_zone, ts);179}180181static void182linuxkpi_thread_dtor(void *arg __unused, struct thread *td)183{184struct task_struct *ts;185186ts = td->td_lkpi_task;187if (ts == NULL)188return;189190td->td_lkpi_task = NULL;191put_task_struct(ts);192}193194static struct task_struct *195linux_get_pid_task_int(pid_t pid, const bool do_get)196{197struct thread *td;198struct proc *p;199struct task_struct *ts;200201if (pid > PID_MAX) {202/* try to find corresponding thread */203td = tdfind(pid, -1);204if (td != NULL) {205ts = td->td_lkpi_task;206if (do_get && ts != NULL)207get_task_struct(ts);208PROC_UNLOCK(td->td_proc);209return (ts);210}211} else {212/* try to find corresponding procedure */213p = pfind(pid);214if (p != NULL) {215FOREACH_THREAD_IN_PROC(p, td) {216ts = td->td_lkpi_task;217if (ts != NULL) {218if (do_get)219get_task_struct(ts);220PROC_UNLOCK(p);221return (ts);222}223}224PROC_UNLOCK(p);225}226}227return (NULL);228}229230struct task_struct *231linux_pid_task(pid_t pid)232{233return (linux_get_pid_task_int(pid, false));234}235236struct task_struct *237linux_get_pid_task(pid_t pid)238{239return (linux_get_pid_task_int(pid, true));240}241242bool243linux_task_exiting(struct task_struct *task)244{245struct thread *td;246struct proc *p;247bool ret;248249ret = false;250251/* try to find corresponding thread */252td = tdfind(task->pid, -1);253if (td != NULL) {254p = td->td_proc;255} else {256/* try to find corresponding procedure */257p = pfind(task->pid);258}259260if (p != NULL) {261if ((p->p_flag & P_WEXIT) != 0)262ret = true;263PROC_UNLOCK(p);264}265return (ret);266}267268static int lkpi_task_resrv;269SYSCTL_INT(_compat_linuxkpi, OID_AUTO, task_struct_reserve,270CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &lkpi_task_resrv, 0,271"Number of struct task and struct mm to reserve for non-sleepable "272"allocations");273274static void275linux_current_init(void *arg __unused)276{277TUNABLE_INT_FETCH("compat.linuxkpi.task_struct_reserve",278&lkpi_task_resrv);279if (lkpi_task_resrv == 0) {280#ifdef DEV_APIC281/*282* Number of interrupt threads plus per-cpu callout283* SWI threads.284*/285lkpi_task_resrv = first_msi_irq + num_msi_irqs + MAXCPU;286#else287lkpi_task_resrv = 1024; /* XXXKIB arbitrary */288#endif289}290linux_current_zone = uma_zcreate("lkpicurr",291sizeof(struct task_struct), NULL, NULL, NULL, NULL,292UMA_ALIGN_PTR, 0);293uma_zone_reserve(linux_current_zone, lkpi_task_resrv);294uma_prealloc(linux_current_zone, lkpi_task_resrv);295linux_mm_zone = uma_zcreate("lkpimm",296sizeof(struct mm_struct), NULL, NULL, NULL, NULL,297UMA_ALIGN_PTR, 0);298uma_zone_reserve(linux_mm_zone, lkpi_task_resrv);299uma_prealloc(linux_mm_zone, lkpi_task_resrv);300301atomic_thread_fence_seq_cst();302303linuxkpi_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,304linuxkpi_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);305lkpi_alloc_current = linux_alloc_current;306}307SYSINIT(linux_current, SI_SUB_EVENTHANDLER + 1, SI_ORDER_SECOND,308linux_current_init, NULL);309310static void311linux_current_uninit(void *arg __unused)312{313struct proc *p;314struct task_struct *ts;315struct thread *td;316317lkpi_alloc_current = linux_alloc_current_noop;318319atomic_thread_fence_seq_cst();320321sx_slock(&allproc_lock);322FOREACH_PROC_IN_SYSTEM(p) {323PROC_LOCK(p);324FOREACH_THREAD_IN_PROC(p, td) {325if ((ts = td->td_lkpi_task) != NULL) {326td->td_lkpi_task = NULL;327put_task_struct(ts);328}329}330PROC_UNLOCK(p);331}332sx_sunlock(&allproc_lock);333334thread_reap_barrier();335336EVENTHANDLER_DEREGISTER(thread_dtor, linuxkpi_thread_dtor_tag);337338uma_zdestroy(linux_current_zone);339uma_zdestroy(linux_mm_zone);340}341SYSUNINIT(linux_current, SI_SUB_EVENTHANDLER + 1, SI_ORDER_SECOND,342linux_current_uninit, NULL);343344345