Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*	$NetBSD: uvm_pgflcache.c,v 1.6 2020/10/18 18:31:31 chs Exp $	*/

/*-
 * Copyright (c) 2019 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * 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.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 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 DAMAGE.
 */

/*
 * uvm_pgflcache.c: page freelist cache.
 *
 * This implements a tiny per-CPU cache of pages that sits between the main
 * page allocator and the freelists.  By allocating and freeing pages in
 * batch, it reduces freelist contention by an order of magnitude.
 *
 * The cache can be paused & resumed at runtime so that UVM_HOTPLUG,
 * uvm_pglistalloc() and uvm_page_redim() can have a consistent view of the
 * world.  On system with one CPU per physical package (e.g. a uniprocessor)
 * the cache is not enabled.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uvm_pgflcache.c,v 1.6 2020/10/18 18:31:31 chs Exp $");

#include "opt_uvm.h"
#include "opt_multiprocessor.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sched.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/proc.h>
#include <sys/atomic.h>
#include <sys/cpu.h>
#include <sys/xcall.h>

#include <uvm/uvm.h>
#include <uvm/uvm_pglist.h>
#include <uvm/uvm_pgflcache.h>

/* There is no point doing any of this on a uniprocessor. */
#ifdef MULTIPROCESSOR

/*
 * MAXPGS - maximum pages per color, per bucket.
 * FILLPGS - number of pages to allocate at once, per color, per bucket.
 *
 * Why the chosen values:
 *
 * (1) In 2019, an average Intel system has 4kB pages and 8x L2 cache
 * colors.  We make the assumption that most of the time allocation activity
 * will be centered around one UVM freelist, so most of the time there will
 * be no more than 224kB worth of cached pages per-CPU.  That's tiny, but
 * enough to hugely reduce contention on the freelist locks, and give us a
 * small pool of pages which if we're very lucky may have some L1/L2 cache
 * locality, and do so without subtracting too much from the L2/L3 cache
 * benefits of having per-package free lists in the page allocator.
 *
 * (2) With the chosen values on _LP64, the data structure for each color
 * takes up a single cache line (64 bytes) giving this very low overhead
 * even in the "miss" case.
 *
 * (3) We don't want to cause too much pressure by hiding away memory that
 * could otherwise be put to good use.
 */
#define	MAXPGS		7
#define	FILLPGS		6

/* Variable size, according to # colors. */
struct pgflcache {
	struct pccolor {
		intptr_t	count;
		struct vm_page	*pages[MAXPGS];
	} color[1];
};

static kmutex_t		uvm_pgflcache_lock;
static int		uvm_pgflcache_sem;

/*
 * uvm_pgflcache_fill: fill specified freelist/color from global list
 *
 * => must be called at IPL_VM
 * => must be called with given bucket lock held
 * => must only fill from the correct bucket for this CPU
 */

void
uvm_pgflcache_fill(struct uvm_cpu *ucpu, int fl, int b, int c)
{
	struct pgflbucket *pgb;
	struct pgflcache *pc;
	struct pccolor *pcc;
	struct pgflist *head;
	struct vm_page *pg;
	int count;

	KASSERT(mutex_owned(&uvm_freelist_locks[b].lock));
	KASSERT(ucpu->pgflbucket == b);

	/* If caching is off, then bail out. */
	if (__predict_false((pc = ucpu->pgflcache[fl]) == NULL)) {
		return;
	}

	/* Fill only to the limit. */
	pcc = &pc->color[c];
	pgb = uvm.page_free[fl].pgfl_buckets[b];
	head = &pgb->pgb_colors[c];
	if (pcc->count >= FILLPGS) {
		return;
	}

	/* Pull pages from the bucket until it's empty, or we are full. */
	count = pcc->count;
	pg = LIST_FIRST(head);
	while (__predict_true(pg != NULL && count < FILLPGS)) {
		KASSERT(pg->flags & PG_FREE);
		KASSERT(uvm_page_get_bucket(pg) == b);
		pcc->pages[count++] = pg;
		pg = LIST_NEXT(pg, pageq.list);
	}

	/* Violate LIST abstraction to remove all pages at once. */
	head->lh_first = pg;
	if (__predict_true(pg != NULL)) {
		pg->pageq.list.le_prev = &head->lh_first;
	}
	pgb->pgb_nfree -= (count - pcc->count);
	CPU_COUNT(CPU_COUNT_FREEPAGES, -(count - pcc->count));
	pcc->count = count;
}

/*
 * uvm_pgflcache_spill: spill specified freelist/color to global list
 *
 * => must be called at IPL_VM
 * => mark __noinline so we don't pull it into uvm_pgflcache_free()
 */

static void __noinline
uvm_pgflcache_spill(struct uvm_cpu *ucpu, int fl, int c)
{
	struct pgflbucket *pgb;
	struct pgfreelist *pgfl;
	struct pgflcache *pc;
	struct pccolor *pcc;
	struct pgflist *head;
	kmutex_t *lock;
	int b, adj;

	pc = ucpu->pgflcache[fl];
	pcc = &pc->color[c];
	pgfl = &uvm.page_free[fl];
	b = ucpu->pgflbucket;
	pgb = pgfl->pgfl_buckets[b];
	head = &pgb->pgb_colors[c];
	lock = &uvm_freelist_locks[b].lock;

	mutex_spin_enter(lock);
	for (adj = pcc->count; pcc->count != 0;) {
		pcc->count--;
		KASSERT(pcc->pages[pcc->count] != NULL);
		KASSERT(pcc->pages[pcc->count]->flags & PG_FREE);
		LIST_INSERT_HEAD(head, pcc->pages[pcc->count], pageq.list);
	}
	pgb->pgb_nfree += adj;
	CPU_COUNT(CPU_COUNT_FREEPAGES, adj);
	mutex_spin_exit(lock);
}

/*
 * uvm_pgflcache_alloc: try to allocate a cached page.
 *
 * => must be called at IPL_VM
 * => allocate only from the given freelist and given page color
 */

struct vm_page *
uvm_pgflcache_alloc(struct uvm_cpu *ucpu, int fl, int c)
{
	struct pgflcache *pc;
	struct pccolor *pcc;
	struct vm_page *pg;

	/* If caching is off, then bail out. */
	if (__predict_false((pc = ucpu->pgflcache[fl]) == NULL)) {
		return NULL;
	}

	/* Very simple: if we have a page then return it. */
	pcc = &pc->color[c];
	if (__predict_false(pcc->count == 0)) {
		return NULL;
	}
	pg = pcc->pages[--(pcc->count)];
	KASSERT(pg != NULL);
	KASSERT(pg->flags == PG_FREE);
	KASSERT(uvm_page_get_freelist(pg) == fl);
	KASSERT(uvm_page_get_bucket(pg) == ucpu->pgflbucket);
	pg->flags = PG_BUSY | PG_CLEAN | PG_FAKE;
	return pg;
}

/*
 * uvm_pgflcache_free: cache a page, if possible.
 *
 * => must be called at IPL_VM
 * => must only send pages for the correct bucket for this CPU
 */

bool
uvm_pgflcache_free(struct uvm_cpu *ucpu, struct vm_page *pg)
{
	struct pgflcache *pc;
	struct pccolor *pcc;
	int fl, c;

	KASSERT(uvm_page_get_bucket(pg) == ucpu->pgflbucket);

	/* If caching is off, then bail out. */
 	fl = uvm_page_get_freelist(pg);
	if (__predict_false((pc = ucpu->pgflcache[fl]) == NULL)) {
		return false;
	}

	/* If the array is full spill it first, then add page to array. */
	c = VM_PGCOLOR(pg);
	pcc = &pc->color[c];
	KASSERT((pg->flags & PG_FREE) == 0);
	if (__predict_false(pcc->count == MAXPGS)) {
		uvm_pgflcache_spill(ucpu, fl, c);
	}
	pg->flags = PG_FREE;
	pcc->pages[pcc->count] = pg;
	pcc->count++;
	return true;
}

/*
 * uvm_pgflcache_init: allocate and initialize per-CPU data structures for
 * the free page cache.  Don't set anything in motion - that's taken care
 * of by uvm_pgflcache_resume().
 */

static void
uvm_pgflcache_init_cpu(struct cpu_info *ci)
{
	struct uvm_cpu *ucpu;
	size_t sz;

	ucpu = ci->ci_data.cpu_uvm;
	KASSERT(ucpu->pgflcachemem == NULL);
	KASSERT(ucpu->pgflcache[0] == NULL);

	sz = offsetof(struct pgflcache, color[uvmexp.ncolors]);
	ucpu->pgflcachememsz =
	    (roundup2(sz * VM_NFREELIST, coherency_unit) + coherency_unit - 1);
	ucpu->pgflcachemem = kmem_zalloc(ucpu->pgflcachememsz, KM_SLEEP);
}

/*
 * uvm_pgflcache_fini_cpu: dump all cached pages back to global free list
 * and shut down caching on the CPU.  Called on each CPU in the system via
 * xcall.
 */

static void
uvm_pgflcache_fini_cpu(void *arg1 __unused, void *arg2 __unused)
{
	struct uvm_cpu *ucpu;
	int fl, color, s;

	ucpu = curcpu()->ci_data.cpu_uvm;
	for (fl = 0; fl < VM_NFREELIST; fl++) {
		s = splvm();
		for (color = 0; color < uvmexp.ncolors; color++) {
			uvm_pgflcache_spill(ucpu, fl, color);
		}
		ucpu->pgflcache[fl] = NULL;
		splx(s);
	}
}

/*
 * uvm_pgflcache_pause: pause operation of the caches
 */

void
uvm_pgflcache_pause(void)
{
	uint64_t where;

	/* First one in starts draining.  Everyone else waits. */
	mutex_enter(&uvm_pgflcache_lock);
	if (uvm_pgflcache_sem++ == 0) {
		where = xc_broadcast(XC_HIGHPRI, uvm_pgflcache_fini_cpu,
		    (void *)1, NULL);
		xc_wait(where);
	}
	mutex_exit(&uvm_pgflcache_lock);
}

/*
 * uvm_pgflcache_resume: resume operation of the caches
 */

void
uvm_pgflcache_resume(void)
{
	CPU_INFO_ITERATOR cii;
	struct cpu_info *ci;
	struct uvm_cpu *ucpu;
	uintptr_t addr;
	size_t sz;
	int fl;

	/* Last guy out takes care of business. */
	mutex_enter(&uvm_pgflcache_lock);
	KASSERT(uvm_pgflcache_sem > 0);
	if (uvm_pgflcache_sem-- > 1) {
		mutex_exit(&uvm_pgflcache_lock);
		return;
	}

	/*
	 * Make sure dependant data structure updates are remotely visible.
	 * Essentially this functions as a global memory barrier.
	 */
	xc_barrier(XC_HIGHPRI);

	/*
	 * Then set all of the pointers in place on each CPU.  As soon as
	 * each pointer is set, caching is operational in that dimension.
	 */
	sz = offsetof(struct pgflcache, color[uvmexp.ncolors]);
	for (CPU_INFO_FOREACH(cii, ci)) {
		ucpu = ci->ci_data.cpu_uvm;
		addr = roundup2((uintptr_t)ucpu->pgflcachemem, coherency_unit);
		for (fl = 0; fl < VM_NFREELIST; fl++) {
			ucpu->pgflcache[fl] = (struct pgflcache *)addr;
			addr += sz;
		}
	}
	mutex_exit(&uvm_pgflcache_lock);
}

/*
 * uvm_pgflcache_start: start operation of the cache.
 *
 * => called once only, when init(8) is about to be started
 */

void
uvm_pgflcache_start(void)
{
	CPU_INFO_ITERATOR cii;
	struct cpu_info *ci;

	KASSERT(uvm_pgflcache_sem > 0);

	/*
	 * There's not much point doing this if every CPU has its own
	 * bucket (and that includes the uniprocessor case).
	 */
	if (ncpu == uvm.bucketcount) {
		return;
	}

	/* Create data structures for each CPU. */
	for (CPU_INFO_FOREACH(cii, ci)) {
		uvm_pgflcache_init_cpu(ci);
	}

	/* Kick it into action. */
	uvm_pgflcache_resume();
}

/*
 * uvm_pgflcache_init: set up data structures for the free page cache.
 */

void
uvm_pgflcache_init(void)
{

	uvm_pgflcache_sem = 1;
	mutex_init(&uvm_pgflcache_lock, MUTEX_DEFAULT, IPL_NONE);
}

#else	/* MULTIPROCESSOR */

struct vm_page *
uvm_pgflcache_alloc(struct uvm_cpu *ucpu, int fl, int c)
{

	return NULL;
}

bool
uvm_pgflcache_free(struct uvm_cpu *ucpu, struct vm_page *pg)
{

	return false;
}

void
uvm_pgflcache_fill(struct uvm_cpu *ucpu, int fl, int b, int c)
{

}

void
uvm_pgflcache_pause(void)
{

}

void
uvm_pgflcache_resume(void)
{

}

void
uvm_pgflcache_start(void)
{

}

void
uvm_pgflcache_init(void)
{

}

#endif	/* MULTIPROCESSOR */