]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_numa.c
Remove register keyword from sys/ and ANSIfy prototypes
[FreeBSD/FreeBSD.git] / sys / kern / kern_numa.c
1 /*-
2  * Copyright (c) 2015, Adrian Chadd <adrian@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysproto.h>
34 #include <sys/jail.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/priv.h>
40 #include <sys/proc.h>
41 #include <sys/refcount.h>
42 #include <sys/sched.h>
43 #include <sys/smp.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/cpuset.h>
46 #include <sys/sx.h>
47 #include <sys/queue.h>
48 #include <sys/libkern.h>
49 #include <sys/limits.h>
50 #include <sys/bus.h>
51 #include <sys/interrupt.h>
52
53 #include <vm/uma.h>
54 #include <vm/vm.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_param.h>
57 #include <vm/vm_domain.h>
58
59 int
60 sys_numa_setaffinity(struct thread *td, struct numa_setaffinity_args *uap)
61 {
62         int error;
63         struct vm_domain_policy vp;
64         struct thread *ttd;
65         struct proc *p;
66         struct cpuset *set;
67
68         set = NULL;
69         p = NULL;
70
71         /*
72          * Copy in just the policy information into the policy
73          * struct.  Userland only supplies vm_domain_policy_entry.
74          */
75         error = copyin(uap->policy, &vp.p, sizeof(vp.p));
76         if (error)
77                 goto out;
78
79         /*
80          * Ensure the seq number is zero - otherwise seq.h
81          * may get very confused.
82          */
83         vp.seq = 0;
84
85         /*
86          * Validate policy.
87          */
88         if (vm_domain_policy_validate(&vp) != 0) {
89                 error = EINVAL;
90                 goto out;
91         }
92
93         /*
94          * Go find the desired proc/tid for this operation.
95          */
96         error = cpuset_which(uap->which, uap->id, &p,
97             &ttd, &set);
98         if (error)
99                 goto out;
100
101         /* Only handle CPU_WHICH_TID and CPU_WHICH_PID */
102         /*
103          * XXX if cpuset_which is called with WHICH_CPUSET and NULL cpuset,
104          * it'll return ESRCH.  We should just return EINVAL.
105          */
106         switch (uap->which) {
107         case CPU_WHICH_TID:
108                 vm_domain_policy_copy(&ttd->td_vm_dom_policy, &vp);
109                 break;
110         case CPU_WHICH_PID:
111                 vm_domain_policy_copy(&p->p_vm_dom_policy, &vp);
112                 break;
113         default:
114                 error = EINVAL;
115                 break;
116         }
117
118         PROC_UNLOCK(p);
119 out:
120         if (set)
121                 cpuset_rel(set);
122         return (error);
123 }
124
125 int
126 sys_numa_getaffinity(struct thread *td, struct numa_getaffinity_args *uap)
127 {
128         int error;
129         struct vm_domain_policy vp;
130         struct thread *ttd;
131         struct proc *p;
132         struct cpuset *set;
133
134         set = NULL;
135         p = NULL;
136
137         error = cpuset_which(uap->which, uap->id, &p,
138             &ttd, &set);
139         if (error)
140                 goto out;
141
142         /* Only handle CPU_WHICH_TID and CPU_WHICH_PID */
143         /*
144          * XXX if cpuset_which is called with WHICH_CPUSET and NULL cpuset,
145          * it'll return ESRCH.  We should just return EINVAL.
146          */
147         switch (uap->which) {
148         case CPU_WHICH_TID:
149                 vm_domain_policy_localcopy(&vp, &ttd->td_vm_dom_policy);
150                 break;
151         case CPU_WHICH_PID:
152                 vm_domain_policy_localcopy(&vp, &p->p_vm_dom_policy);
153                 break;
154         default:
155                 error = EINVAL;
156                 break;
157         }
158         if (p)
159                 PROC_UNLOCK(p);
160         /*
161          * Copy out only the vm_domain_policy_entry part.
162          */
163         if (error == 0)
164                 error = copyout(&vp.p, uap->policy, sizeof(vp.p));
165 out:
166         if (set)
167                 cpuset_rel(set);
168         return (error);
169 }