1 /* 2 * Copyright (c) 2019 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 #include <sys/cdefs.h> 35 36 #include <sys/condvar.h> 37 #include <sys/queue.h> 38 #include <sys/lock.h> 39 40 #include <linux/compiler.h> 41 42 #include <linux/atomic.h> 43 #include <linux/errno.h> 44 #include <linux/kref.h> 45 #include <linux/dma-fence.h> 46 #include <linux/sched.h> 47 #include <linux/slab.h> 48 #include <linux/spinlock.h> 49 50 /* 51 * Called when curthread->td_linux_task is NULL. We must allocated, initialize, 52 * and install a task_struct in td (the current thread). 53 * 54 * All threads belonging to the same process have a common mm_struct which 55 * is stored as p->p_linux_mm. This must be allocated, initialized, and 56 * and installed if necessary. 57 */ 58 struct task_struct * 59 linux_task_alloc(struct thread *td) 60 { 61 struct task_struct *task; 62 struct mm_struct *mm; 63 struct proc *p; 64 65 task = kzalloc(sizeof(*task), GFP_KERNEL); 66 task->dfly_td = td; 67 68 if ((p = td->td_proc) != NULL) { 69 if ((mm = p->p_linux_mm) == NULL) { 70 mm = kzalloc(sizeof(*mm), GFP_KERNEL); 71 mm->refs = 1; 72 lockinit(&mm->mmap_sem, "drmmms", 0, LK_CANRECURSE); 73 lwkt_gettoken(&p->p_token); 74 if (p->p_linux_mm == NULL) { 75 p->p_linux_mm = mm; 76 } else { 77 linux_mm_drop(mm); 78 mm = p->p_linux_mm; 79 } 80 lwkt_reltoken(&p->p_token); 81 } 82 task->mm = mm; 83 atomic_add_long(&mm->refs, 1); 84 } 85 td->td_linux_task = task; 86 87 return task; 88 } 89 90 /* 91 * Called at thread exit 92 */ 93 void 94 linux_task_drop(struct thread *td) 95 { 96 struct task_struct *task; 97 struct mm_struct *mm; 98 99 task = td->td_linux_task; 100 td->td_linux_task = NULL; 101 if ((mm = task->mm) != NULL) { 102 atomic_add_long(&mm->refs, -1); /* proc ref always remains */ 103 task->mm = NULL; 104 } 105 kfree(task); 106 } 107 108 void 109 linux_proc_drop(struct proc *p) 110 { 111 struct mm_struct *mm; 112 113 if ((mm = p->p_linux_mm) != NULL) { 114 p->p_linux_mm = NULL; 115 linux_mm_drop(mm); 116 } 117 } 118 119 void 120 linux_mm_drop(struct mm_struct *mm) 121 { 122 long refs; 123 124 refs = atomic_fetchadd_long(&mm->refs, -1); 125 KKASSERT(refs > 0); 126 if (refs == 1) { 127 lockuninit(&mm->mmap_sem); 128 kfree(mm); 129 } 130 } 131