Path: blob/main/sys/compat/linuxkpi/common/src/linux_current.c
102292 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)93return (ENOMEM);94mm = NULL;9596/* setup new task structure */97atomic_set(&ts->kthread_flags, 0);98ts->task_thread = td;99ts->comm = td->td_name;100ts->pid = td->td_tid;101ts->group_leader = ts;102atomic_set(&ts->usage, 1);103atomic_set(&ts->state, TASK_RUNNING);104init_completion(&ts->parked);105init_completion(&ts->exited);106107proc = td->td_proc;108109PROC_LOCK(proc);110mm_other = find_other_mm(proc);111112/* use allocated mm_struct as a fallback */113if (mm_other == NULL) {114PROC_UNLOCK(proc);115mm = uma_zalloc(linux_mm_zone, flags | M_ZERO);116if (mm == NULL) {117uma_zfree(linux_current_zone, ts);118return (ENOMEM);119}120121PROC_LOCK(proc);122mm_other = find_other_mm(proc);123if (mm_other == NULL) {124/* setup new mm_struct */125init_rwsem(&mm->mmap_sem);126atomic_set(&mm->mm_count, 1);127atomic_set(&mm->mm_users, 1);128/* set mm_struct pointer */129ts->mm = mm;130/* clear pointer to not free memory */131mm = NULL;132} else {133ts->mm = mm_other;134}135} else {136ts->mm = mm_other;137}138139/* store pointer to task struct */140td->td_lkpi_task = ts;141PROC_UNLOCK(proc);142143/* free mm_struct pointer, if any */144uma_zfree(linux_mm_zone, mm);145146return (0);147}148149struct mm_struct *150linux_get_task_mm(struct task_struct *task)151{152struct mm_struct *mm;153154mm = task->mm;155if (mm != NULL) {156atomic_inc(&mm->mm_users);157return (mm);158}159return (NULL);160}161162void163linux_mm_dtor(struct mm_struct *mm)164{165uma_zfree(linux_mm_zone, mm);166}167168void169linux_free_current(struct task_struct *ts)170{171mmput(ts->mm);172uma_zfree(linux_current_zone, ts);173}174175static void176linuxkpi_thread_dtor(void *arg __unused, struct thread *td)177{178struct task_struct *ts;179180ts = td->td_lkpi_task;181if (ts == NULL)182return;183184td->td_lkpi_task = NULL;185put_task_struct(ts);186}187188static struct task_struct *189linux_get_pid_task_int(pid_t pid, const bool do_get)190{191struct thread *td;192struct proc *p;193struct task_struct *ts;194195if (pid > PID_MAX) {196/* try to find corresponding thread */197td = tdfind(pid, -1);198if (td != NULL) {199ts = td->td_lkpi_task;200if (do_get && ts != NULL)201get_task_struct(ts);202PROC_UNLOCK(td->td_proc);203return (ts);204}205} else {206/* try to find corresponding procedure */207p = pfind(pid);208if (p != NULL) {209FOREACH_THREAD_IN_PROC(p, td) {210ts = td->td_lkpi_task;211if (ts != NULL) {212if (do_get)213get_task_struct(ts);214PROC_UNLOCK(p);215return (ts);216}217}218PROC_UNLOCK(p);219}220}221return (NULL);222}223224struct task_struct *225linux_pid_task(pid_t pid)226{227return (linux_get_pid_task_int(pid, false));228}229230struct task_struct *231linux_get_pid_task(pid_t pid)232{233return (linux_get_pid_task_int(pid, true));234}235236bool237linux_task_exiting(struct task_struct *task)238{239struct thread *td;240struct proc *p;241bool ret;242243ret = false;244245/* try to find corresponding thread */246td = tdfind(task->pid, -1);247if (td != NULL) {248p = td->td_proc;249} else {250/* try to find corresponding procedure */251p = pfind(task->pid);252}253254if (p != NULL) {255if ((p->p_flag & P_WEXIT) != 0)256ret = true;257PROC_UNLOCK(p);258}259return (ret);260}261262static int lkpi_task_resrv;263SYSCTL_INT(_compat_linuxkpi, OID_AUTO, task_struct_reserve,264CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &lkpi_task_resrv, 0,265"Number of struct task and struct mm to reserve for non-sleepable "266"allocations");267268static void269linux_current_init(void *arg __unused)270{271TUNABLE_INT_FETCH("compat.linuxkpi.task_struct_reserve",272&lkpi_task_resrv);273if (lkpi_task_resrv == 0) {274#ifdef DEV_APIC275/*276* Number of interrupt threads plus per-cpu callout277* SWI threads.278*/279lkpi_task_resrv = first_msi_irq + num_msi_irqs + MAXCPU;280#else281lkpi_task_resrv = 1024; /* XXXKIB arbitrary */282#endif283}284linux_current_zone = uma_zcreate("lkpicurr",285sizeof(struct task_struct), NULL, NULL, NULL, NULL,286UMA_ALIGN_PTR, 0);287uma_zone_reserve(linux_current_zone, lkpi_task_resrv);288uma_prealloc(linux_current_zone, lkpi_task_resrv);289linux_mm_zone = uma_zcreate("lkpimm",290sizeof(struct mm_struct), NULL, NULL, NULL, NULL,291UMA_ALIGN_PTR, 0);292uma_zone_reserve(linux_mm_zone, lkpi_task_resrv);293uma_prealloc(linux_mm_zone, lkpi_task_resrv);294295atomic_thread_fence_seq_cst();296297linuxkpi_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,298linuxkpi_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);299lkpi_alloc_current = linux_alloc_current;300}301SYSINIT(linux_current, SI_SUB_EVENTHANDLER + 1, SI_ORDER_SECOND,302linux_current_init, NULL);303304static void305linux_current_uninit(void *arg __unused)306{307struct proc *p;308struct task_struct *ts;309struct thread *td;310311lkpi_alloc_current = linux_alloc_current_noop;312313atomic_thread_fence_seq_cst();314315sx_slock(&allproc_lock);316FOREACH_PROC_IN_SYSTEM(p) {317PROC_LOCK(p);318FOREACH_THREAD_IN_PROC(p, td) {319if ((ts = td->td_lkpi_task) != NULL) {320td->td_lkpi_task = NULL;321put_task_struct(ts);322}323}324PROC_UNLOCK(p);325}326sx_sunlock(&allproc_lock);327328thread_reap_barrier();329330EVENTHANDLER_DEREGISTER(thread_dtor, linuxkpi_thread_dtor_tag);331332uma_zdestroy(linux_current_zone);333uma_zdestroy(linux_mm_zone);334}335SYSUNINIT(linux_current, SI_SUB_EVENTHANDLER + 1, SI_ORDER_SECOND,336linux_current_uninit, NULL);337338339