2 * Copyright (c) 2019 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 #include <sys/cdefs.h>
36 #include <sys/condvar.h>
37 #include <sys/queue.h>
40 #include <linux/compiler.h>
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>
51 * Called when curthread->td_linux_task is NULL. We must allocated, initialize,
52 * and install a task_struct in td (the current thread).
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.
59 linux_task_alloc(struct thread *td)
61 struct task_struct *task;
65 task = kzalloc(sizeof(*task), GFP_KERNEL);
68 if ((p = td->td_proc) != NULL) {
69 if ((mm = p->p_linux_mm) == NULL) {
70 mm = kzalloc(sizeof(*mm), GFP_KERNEL);
72 lockinit(&mm->mmap_sem, "drmmms", 0, LK_CANRECURSE);
73 lwkt_gettoken(&p->p_token);
74 if (p->p_linux_mm == NULL) {
80 lwkt_reltoken(&p->p_token);
83 atomic_add_long(&mm->refs, 1);
85 td->td_linux_task = task;
91 * Called at thread exit
94 linux_task_drop(struct thread *td)
96 struct task_struct *task;
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 */
109 linux_proc_drop(struct proc *p)
111 struct mm_struct *mm;
113 if ((mm = p->p_linux_mm) != NULL) {
114 p->p_linux_mm = NULL;
120 linux_mm_drop(struct mm_struct *mm)
124 refs = atomic_fetchadd_long(&mm->refs, -1);
127 lockuninit(&mm->mmap_sem);