summaryrefslogtreecommitdiffstats
path: root/sys/vm/vm_domain.c
blob: 49454967346e6f61b2403d827be3291e4efecb58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*-
 * Copyright (c) 2015 Adrian Chadd <adrian@FreeBSD.org>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 *    redistribution must be conditioned upon including a substantially
 *    similar Disclaimer requirement for further binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGES.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_vm.h"
#include "opt_ddb.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/lock.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#ifdef VM_NUMA_ALLOC
#include <sys/proc.h>
#endif
#include <sys/queue.h>
#include <sys/rwlock.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <sys/tree.h>
#include <sys/vmmeter.h>
#include <sys/seq.h>

#include <ddb/ddb.h>

#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_kern.h>
#include <vm/vm_object.h>
#include <vm/vm_page.h>
#include <vm/vm_phys.h>

#include <vm/vm_domain.h>

#ifdef VM_NUMA_ALLOC
static __inline int
vm_domain_rr_selectdomain(int skip_domain)
{
	struct thread *td;

	td = curthread;

	td->td_dom_rr_idx++;
	td->td_dom_rr_idx %= vm_ndomains;

	/*
	 * If skip_domain is provided then skip over that
	 * domain.  This is intended for round robin variants
	 * which first try a fixed domain.
	 */
	if ((skip_domain > -1) && (td->td_dom_rr_idx == skip_domain)) {
		td->td_dom_rr_idx++;
		td->td_dom_rr_idx %= vm_ndomains;
	}
	return (td->td_dom_rr_idx);
}
#endif

/*
 * This implements a very simple set of VM domain memory allocation
 * policies and iterators.
 */

/*
 * A VM domain policy represents a desired VM domain policy.
 * Iterators implement searching through VM domains in a specific
 * order.
 */

/*
 * When setting a policy, the caller must establish their own
 * exclusive write protection for the contents of the domain
 * policy.
 */
int
vm_domain_policy_init(struct vm_domain_policy *vp)
{

	bzero(vp, sizeof(*vp));
	vp->p.policy = VM_POLICY_NONE;
	vp->p.domain = -1;
	return (0);
}

int
vm_domain_policy_set(struct vm_domain_policy *vp,
    vm_domain_policy_type_t vt, int domain)
{

	seq_write_begin(&vp->seq);
	vp->p.policy = vt;
	vp->p.domain = domain;
	seq_write_end(&vp->seq);
	return (0);
}

/*
 * Take a local copy of a policy.
 *
 * The destination policy isn't write-barriered; this is used
 * for doing local copies into something that isn't shared.
 */
void
vm_domain_policy_localcopy(struct vm_domain_policy *dst,
    const struct vm_domain_policy *src)
{
	seq_t seq;

	for (;;) {
		seq = seq_read(&src->seq);
		*dst = *src;
		if (seq_consistent(&src->seq, seq))
			return;
	}
}

/*
 * Take a write-barrier copy of a policy.
 *
 * The destination policy is write -barriered; this is used
 * for doing copies into policies that may be read by other
 * threads.
 */
void
vm_domain_policy_copy(struct vm_domain_policy *dst,
    const struct vm_domain_policy *src)
{
	seq_t seq;
	struct vm_domain_policy d;

	for (;;) {
		seq = seq_read(&src->seq);
		d = *src;
		if (seq_consistent(&src->seq, seq)) {
			seq_write_begin(&dst->seq);
			dst->p.domain = d.p.domain;
			dst->p.policy = d.p.policy;
			seq_write_end(&dst->seq);
			return;
		}
	}
}

int
vm_domain_policy_validate(const struct vm_domain_policy *vp)
{

	switch (vp->p.policy) {
	case VM_POLICY_NONE:
	case VM_POLICY_ROUND_ROBIN:
	case VM_POLICY_FIRST_TOUCH:
	case VM_POLICY_FIRST_TOUCH_ROUND_ROBIN:
		if (vp->p.domain == -1)
			return (0);
		return (-1);
	case VM_POLICY_FIXED_DOMAIN:
	case VM_POLICY_FIXED_DOMAIN_ROUND_ROBIN:
#ifdef VM_NUMA_ALLOC
		if (vp->p.domain >= 0 && vp->p.domain < vm_ndomains)
			return (0);
#else
		if (vp->p.domain == 0)
			return (0);
#endif
		return (-1);
	default:
		return (-1);
	}
	return (-1);
}

int
vm_domain_policy_cleanup(struct vm_domain_policy *vp)
{

	/* For now, empty */
	return (0);
}

int
vm_domain_iterator_init(struct vm_domain_iterator *vi)
{

	/* Nothing to do for now */
	return (0);
}

/*
 * Manually setup an iterator with the given details.
 */
int
vm_domain_iterator_set(struct vm_domain_iterator *vi,
    vm_domain_policy_type_t vt, int domain)
{

#ifdef VM_NUMA_ALLOC
	switch (vt) {
	case VM_POLICY_FIXED_DOMAIN:
		vi->policy = VM_POLICY_FIXED_DOMAIN;
		vi->domain = domain;
		vi->n = 1;
		break;
	case VM_POLICY_FIXED_DOMAIN_ROUND_ROBIN:
		vi->policy = VM_POLICY_FIXED_DOMAIN_ROUND_ROBIN;
		vi->domain = domain;
		vi->n = vm_ndomains;
		break;
	case VM_POLICY_FIRST_TOUCH:
		vi->policy = VM_POLICY_FIRST_TOUCH;
		vi->domain = PCPU_GET(domain);
		vi->n = 1;
		break;
	case VM_POLICY_FIRST_TOUCH_ROUND_ROBIN:
		vi->policy = VM_POLICY_FIRST_TOUCH_ROUND_ROBIN;
		vi->domain = PCPU_GET(domain);
		vi->n = vm_ndomains;
		break;
	case VM_POLICY_ROUND_ROBIN:
	default:
		vi->policy = VM_POLICY_ROUND_ROBIN;
		vi->domain = -1;
		vi->n = vm_ndomains;
		break;
	}
#else
	vi->domain = 0;
	vi->n = 1;
#endif
	return (0);
}

/*
 * Setup an iterator based on the given policy.
 */
static inline void
_vm_domain_iterator_set_policy(struct vm_domain_iterator *vi,
    const struct vm_domain_policy *vt)
{

#ifdef VM_NUMA_ALLOC
	/*
	 * Initialise the iterator.
	 *
	 * For first-touch, the initial domain is set
	 * via the current thread CPU domain.
	 *
	 * For fixed-domain, it's assumed that the
	 * caller has initialised the specific domain
	 * it is after.
	 */
	switch (vt->p.policy) {
	case VM_POLICY_FIXED_DOMAIN:
		vi->policy = vt->p.policy;
		vi->domain = vt->p.domain;
		vi->n = 1;
		break;
	case VM_POLICY_FIXED_DOMAIN_ROUND_ROBIN:
		vi->policy = vt->p.policy;
		vi->domain = vt->p.domain;
		vi->n = vm_ndomains;
		break;
	case VM_POLICY_FIRST_TOUCH:
		vi->policy = vt->p.policy;
		vi->domain = PCPU_GET(domain);
		vi->n = 1;
		break;
	case VM_POLICY_FIRST_TOUCH_ROUND_ROBIN:
		vi->policy = vt->p.policy;
		vi->domain = PCPU_GET(domain);
		vi->n = vm_ndomains;
		break;
	case VM_POLICY_ROUND_ROBIN:
	default:
		/*
		 * Default to round-robin policy.
		 */
		vi->policy = VM_POLICY_ROUND_ROBIN;
		vi->domain = -1;
		vi->n = vm_ndomains;
		break;
	}
#else
	vi->domain = 0;
	vi->n = 1;
#endif
}

void
vm_domain_iterator_set_policy(struct vm_domain_iterator *vi,
    const struct vm_domain_policy *vt)
{
	seq_t seq;
	struct vm_domain_policy vt_lcl;

	for (;;) {
		seq = seq_read(&vt->seq);
		vt_lcl = *vt;
		if (seq_consistent(&vt->seq, seq)) {
			_vm_domain_iterator_set_policy(vi, &vt_lcl);
			return;
		}
	}
}

/*
 * Return the next VM domain to use.
 *
 * Returns 0 w/ domain set to the next domain to use, or
 * -1 to indicate no more domains are available.
 */
int
vm_domain_iterator_run(struct vm_domain_iterator *vi, int *domain)
{

	/* General catch-all */
	if (vi->n <= 0)
		return (-1);

#ifdef VM_NUMA_ALLOC
	switch (vi->policy) {
	case VM_POLICY_FIXED_DOMAIN:
	case VM_POLICY_FIRST_TOUCH:
		*domain = vi->domain;
		vi->n--;
		break;
	case VM_POLICY_FIXED_DOMAIN_ROUND_ROBIN:
	case VM_POLICY_FIRST_TOUCH_ROUND_ROBIN:
		/*
		 * XXX TODO: skip over the rr'ed domain
		 * if it equals the one we started with.
		 */
		if (vi->n == vm_ndomains)
			*domain = vi->domain;
		else
			*domain = vm_domain_rr_selectdomain(vi->domain);
		vi->n--;
		break;
	case VM_POLICY_ROUND_ROBIN:
	default:
		*domain = vm_domain_rr_selectdomain(-1);
		vi->n--;
		break;
	}
#else
	*domain = 0;
	vi->n--;
#endif

	return (0);
}

/*
 * Returns 1 if the iteration is done, or 0 if it has not.

 * This can only be called after at least one loop through
 * the iterator.  Ie, it's designed to be used as a tail
 * check of a loop, not the head check of a loop.
 */
int
vm_domain_iterator_isdone(struct vm_domain_iterator *vi)
{

	return (vi->n <= 0);
}

int
vm_domain_iterator_cleanup(struct vm_domain_iterator *vi)
{

	return (0);
}
OpenPOWER on IntegriCloud