xref: /dragonfly/sys/dev/drm/linux_sched.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
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 *
linux_task_alloc(struct thread * td)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           task->pid = -1;
68           spin_init(&task->kt_spin, "tspin2");
69 
70           if ((p = td->td_proc) != NULL) {
71                     task->pid = td->td_proc->p_pid;
72                     if ((mm = p->p_linux_mm) == NULL) {
73                               mm = kzalloc(sizeof(*mm), GFP_KERNEL);
74                               mm->refs = 1;
75                               lockinit(&mm->mmap_sem, "drmmms", 0, LK_CANRECURSE);
76                               lwkt_gettoken(&p->p_token);
77                               if (p->p_linux_mm == NULL) {
78                                         p->p_linux_mm = mm;
79                               } else {
80                                         linux_mm_drop(mm);
81                                         mm = p->p_linux_mm;
82                               }
83                               lwkt_reltoken(&p->p_token);
84                     }
85                     task->mm = mm;
86                     atomic_add_long(&mm->refs, 1);
87           }
88           td->td_linux_task = task;
89 
90           return task;
91 }
92 
93 /*
94  * Called at thread exit
95  */
96 void
linux_task_drop(struct thread * td)97 linux_task_drop(struct thread *td)
98 {
99           struct task_struct *task;
100           struct mm_struct *mm;
101 
102           task = td->td_linux_task;
103           td->td_linux_task = NULL;
104           if ((mm = task->mm) != NULL) {
105                     atomic_add_long(&mm->refs, -1);         /* proc ref always remains */
106                     task->mm = NULL;
107           }
108           kfree(task);
109 }
110 
111 void
linux_proc_drop(struct proc * p)112 linux_proc_drop(struct proc *p)
113 {
114           struct mm_struct *mm;
115 
116           if ((mm = p->p_linux_mm) != NULL) {
117                     p->p_linux_mm = NULL;
118                     linux_mm_drop(mm);
119           }
120 }
121 
122 void
linux_mm_drop(struct mm_struct * mm)123 linux_mm_drop(struct mm_struct *mm)
124 {
125           long refs;
126 
127           refs = atomic_fetchadd_long(&mm->refs, -1);
128           KKASSERT(refs > 0);
129           if (refs == 1) {
130                     lockuninit(&mm->mmap_sem);
131                     kfree(mm);
132           }
133 }
134