Print this page
remove whole-process swapping
Long before Unix supported paging, it used process swapping to reclaim
memory. The code is there and in theory it runs when we get *extremely* low
on memory. In practice, it never runs since the definition of low-on-memory
is antiquated. (XXX: define what antiquated means)
You can check the number of swapout/swapin events with kstats:
$ kstat -p ::vm:swapin ::vm:swapout
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/sparc/v9/vm/seg_nf.c
+++ new/usr/src/uts/sparc/v9/vm/seg_nf.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
24 24 */
25 25
26 26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 27 /* All Rights Reserved */
28 28
29 29 /*
30 30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 31 * under license from the Regents of the University of California.
32 32 */
33 33
34 -#pragma ident "%Z%%M% %I% %E% SMI"
35 -
36 34 /*
37 35 * VM - segment for non-faulting loads.
38 36 */
39 37
40 38 #include <sys/types.h>
41 39 #include <sys/t_lock.h>
42 40 #include <sys/param.h>
43 41 #include <sys/mman.h>
44 42 #include <sys/errno.h>
45 43 #include <sys/kmem.h>
46 44 #include <sys/cmn_err.h>
47 45 #include <sys/vnode.h>
48 46 #include <sys/proc.h>
49 47 #include <sys/conf.h>
50 48 #include <sys/debug.h>
51 49 #include <sys/archsystm.h>
52 50 #include <sys/lgrp.h>
53 51
54 52 #include <vm/page.h>
55 53 #include <vm/hat.h>
56 54 #include <vm/as.h>
57 55 #include <vm/seg.h>
58 56 #include <vm/vpage.h>
59 57
60 58 /*
61 59 * Private seg op routines.
62 60 */
63 61 static int segnf_dup(struct seg *seg, struct seg *newseg);
64 62 static int segnf_unmap(struct seg *seg, caddr_t addr, size_t len);
65 63 static void segnf_free(struct seg *seg);
66 64 static faultcode_t segnf_nomap(void);
67 65 static int segnf_setprot(struct seg *seg, caddr_t addr,
68 66 size_t len, uint_t prot);
69 67 static int segnf_checkprot(struct seg *seg, caddr_t addr,
70 68 size_t len, uint_t prot);
71 69 static void segnf_badop(void);
72 70 static int segnf_nop(void);
73 71 static int segnf_getprot(struct seg *seg, caddr_t addr,
74 72 size_t len, uint_t *protv);
75 73 static u_offset_t segnf_getoffset(struct seg *seg, caddr_t addr);
76 74 static int segnf_gettype(struct seg *seg, caddr_t addr);
77 75 static int segnf_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp);
78 76 static void segnf_dump(struct seg *seg);
79 77 static int segnf_pagelock(struct seg *seg, caddr_t addr, size_t len,
80 78 struct page ***ppp, enum lock_type type, enum seg_rw rw);
81 79 static int segnf_setpagesize(struct seg *seg, caddr_t addr, size_t len,
82 80 uint_t szc);
83 81 static int segnf_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp);
84 82 static lgrp_mem_policy_info_t *segnf_getpolicy(struct seg *seg,
85 83 caddr_t addr);
86 84
87 85
88 86 struct seg_ops segnf_ops = {
89 87 segnf_dup,
↓ open down ↓ |
44 lines elided |
↑ open up ↑ |
90 88 segnf_unmap,
91 89 segnf_free,
92 90 (faultcode_t (*)(struct hat *, struct seg *, caddr_t, size_t,
93 91 enum fault_type, enum seg_rw))
94 92 segnf_nomap, /* fault */
95 93 (faultcode_t (*)(struct seg *, caddr_t))
96 94 segnf_nomap, /* faulta */
97 95 segnf_setprot,
98 96 segnf_checkprot,
99 97 (int (*)())segnf_badop, /* kluster */
100 - (size_t (*)(struct seg *))NULL, /* swapout */
101 98 (int (*)(struct seg *, caddr_t, size_t, int, uint_t))
102 99 segnf_nop, /* sync */
103 100 (size_t (*)(struct seg *, caddr_t, size_t, char *))
104 101 segnf_nop, /* incore */
105 102 (int (*)(struct seg *, caddr_t, size_t, int, int, ulong_t *, size_t))
106 103 segnf_nop, /* lockop */
107 104 segnf_getprot,
108 105 segnf_getoffset,
109 106 segnf_gettype,
110 107 segnf_getvp,
111 108 (int (*)(struct seg *, caddr_t, size_t, uint_t))
112 109 segnf_nop, /* advise */
113 110 segnf_dump,
114 111 segnf_pagelock,
115 112 segnf_setpagesize,
116 113 segnf_getmemid,
117 114 segnf_getpolicy,
118 115 };
119 116
120 117 /*
121 118 * vnode and page for the page of zeros we use for the nf mappings.
122 119 */
123 120 static kmutex_t segnf_lock;
124 121 static struct vnode nfvp;
125 122 static struct page **nfpp;
126 123
127 124 #define addr_to_vcolor(addr) \
128 125 (shm_alignment) ? \
129 126 ((int)(((uintptr_t)(addr) & (shm_alignment - 1)) >> PAGESHIFT)) : 0
130 127
131 128 /*
132 129 * We try to limit the number of Non-fault segments created.
133 130 * Non fault segments are created to optimize sparc V9 code which uses
134 131 * the sparc nonfaulting load ASI (ASI_PRIMARY_NOFAULT).
135 132 *
136 133 * There are several reasons why creating too many non-fault segments
137 134 * could cause problems.
138 135 *
139 136 * First, excessive allocation of kernel resources for the seg
140 137 * structures and the HAT data to map the zero pages.
141 138 *
142 139 * Secondly, creating nofault segments actually uses up user virtual
143 140 * address space. This makes it unavailable for subsequent mmap(0, ...)
144 141 * calls which use as_gap() to find empty va regions. Creation of too
145 142 * many nofault segments could thus interfere with the ability of the
146 143 * runtime linker to load a shared object.
147 144 */
148 145 #define MAXSEGFORNF (10000)
149 146 #define MAXNFSEARCH (5)
150 147
151 148
152 149 /*
153 150 * Must be called from startup()
154 151 */
155 152 void
156 153 segnf_init()
157 154 {
158 155 mutex_init(&segnf_lock, NULL, MUTEX_DEFAULT, NULL);
159 156 }
160 157
161 158
162 159 /*
163 160 * Create a no-fault segment.
164 161 *
165 162 * The no-fault segment is not technically necessary, as the code in
166 163 * nfload() in trap.c will emulate the SPARC instruction and load
167 164 * a value of zero in the destination register.
168 165 *
169 166 * However, this code tries to put a page of zero's at the nofault address
170 167 * so that subsequent non-faulting loads to the same page will not
171 168 * trap with a tlb miss.
172 169 *
173 170 * In order to help limit the number of segments we merge adjacent nofault
174 171 * segments into a single segment. If we get a large number of segments
175 172 * we'll also try to delete a random other nf segment.
176 173 */
177 174 /* ARGSUSED */
178 175 int
179 176 segnf_create(struct seg *seg, void *argsp)
180 177 {
181 178 uint_t prot;
182 179 pgcnt_t vacpgs;
183 180 u_offset_t off = 0;
184 181 caddr_t vaddr = NULL;
185 182 int i, color;
186 183 struct seg *s1;
187 184 struct seg *s2;
188 185 size_t size;
189 186 struct as *as = seg->s_as;
190 187
191 188 ASSERT(as && AS_WRITE_HELD(as, &as->a_lock));
192 189
193 190 /*
194 191 * Need a page per virtual color or just 1 if no vac.
195 192 */
196 193 mutex_enter(&segnf_lock);
197 194 if (nfpp == NULL) {
198 195 struct seg kseg;
199 196
200 197 vacpgs = 1;
201 198 if (shm_alignment > PAGESIZE) {
202 199 vacpgs = shm_alignment >> PAGESHIFT;
203 200 }
204 201
205 202 nfpp = kmem_alloc(sizeof (*nfpp) * vacpgs, KM_SLEEP);
206 203
207 204 kseg.s_as = &kas;
208 205 for (i = 0; i < vacpgs; i++, off += PAGESIZE,
209 206 vaddr += PAGESIZE) {
210 207 nfpp[i] = page_create_va(&nfvp, off, PAGESIZE,
211 208 PG_WAIT | PG_NORELOC, &kseg, vaddr);
212 209 page_io_unlock(nfpp[i]);
213 210 page_downgrade(nfpp[i]);
214 211 pagezero(nfpp[i], 0, PAGESIZE);
215 212 }
216 213 }
217 214 mutex_exit(&segnf_lock);
218 215
219 216 hat_map(as->a_hat, seg->s_base, seg->s_size, HAT_MAP);
220 217
221 218 /*
222 219 * s_data can't be NULL because of ASSERTS in the common vm code.
223 220 */
224 221 seg->s_ops = &segnf_ops;
225 222 seg->s_data = seg;
226 223 seg->s_flags |= S_PURGE;
227 224
228 225 mutex_enter(&as->a_contents);
229 226 as->a_flags |= AS_NEEDSPURGE;
230 227 mutex_exit(&as->a_contents);
231 228
232 229 prot = PROT_READ;
233 230 color = addr_to_vcolor(seg->s_base);
234 231 if (as != &kas)
235 232 prot |= PROT_USER;
236 233 hat_memload(as->a_hat, seg->s_base, nfpp[color],
237 234 prot | HAT_NOFAULT, HAT_LOAD);
238 235
239 236 /*
240 237 * At this point see if we can concatenate a segment to
241 238 * a non-fault segment immediately before and/or after it.
242 239 */
243 240 if ((s1 = AS_SEGPREV(as, seg)) != NULL &&
244 241 s1->s_ops == &segnf_ops &&
245 242 s1->s_base + s1->s_size == seg->s_base) {
246 243 size = s1->s_size;
247 244 seg_free(s1);
248 245 seg->s_base -= size;
249 246 seg->s_size += size;
250 247 }
251 248
252 249 if ((s2 = AS_SEGNEXT(as, seg)) != NULL &&
253 250 s2->s_ops == &segnf_ops &&
254 251 seg->s_base + seg->s_size == s2->s_base) {
255 252 size = s2->s_size;
256 253 seg_free(s2);
257 254 seg->s_size += size;
258 255 }
259 256
260 257 /*
261 258 * if we already have a lot of segments, try to delete some other
262 259 * nofault segment to reduce the probability of uncontrolled segment
263 260 * creation.
264 261 *
265 262 * the code looks around quickly (no more than MAXNFSEARCH segments
266 263 * each way) for another NF segment and then deletes it.
267 264 */
268 265 if (avl_numnodes(&as->a_segtree) > MAXSEGFORNF) {
269 266 size = 0;
270 267 s2 = NULL;
271 268 s1 = AS_SEGPREV(as, seg);
272 269 while (size++ < MAXNFSEARCH && s1 != NULL) {
273 270 if (s1->s_ops == &segnf_ops)
274 271 s2 = s1;
275 272 s1 = AS_SEGPREV(s1->s_as, seg);
276 273 }
277 274 if (s2 == NULL) {
278 275 s1 = AS_SEGNEXT(as, seg);
279 276 while (size-- > 0 && s1 != NULL) {
280 277 if (s1->s_ops == &segnf_ops)
281 278 s2 = s1;
282 279 s1 = AS_SEGNEXT(as, seg);
283 280 }
284 281 }
285 282 if (s2 != NULL)
286 283 seg_unmap(s2);
287 284 }
288 285
289 286 return (0);
290 287 }
291 288
292 289 /*
293 290 * Never really need "No fault" segments, so they aren't dup'd.
294 291 */
295 292 /* ARGSUSED */
296 293 static int
297 294 segnf_dup(struct seg *seg, struct seg *newseg)
298 295 {
299 296 panic("segnf_dup");
300 297 return (0);
301 298 }
302 299
303 300 /*
304 301 * Split a segment at addr for length len.
305 302 */
306 303 static int
307 304 segnf_unmap(struct seg *seg, caddr_t addr, size_t len)
308 305 {
309 306 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
310 307
311 308 /*
312 309 * Check for bad sizes.
313 310 */
314 311 if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size ||
315 312 (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET)) {
316 313 cmn_err(CE_PANIC, "segnf_unmap: bad unmap size");
317 314 }
318 315
319 316 /*
320 317 * Unload any hardware translations in the range to be taken out.
321 318 */
322 319 hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD_UNMAP);
323 320
324 321 if (addr == seg->s_base && len == seg->s_size) {
325 322 /*
326 323 * Freeing entire segment.
327 324 */
328 325 seg_free(seg);
329 326 } else if (addr == seg->s_base) {
330 327 /*
331 328 * Freeing the beginning of the segment.
332 329 */
333 330 seg->s_base += len;
334 331 seg->s_size -= len;
335 332 } else if (addr + len == seg->s_base + seg->s_size) {
336 333 /*
337 334 * Freeing the end of the segment.
338 335 */
339 336 seg->s_size -= len;
340 337 } else {
341 338 /*
342 339 * The section to go is in the middle of the segment, so we
343 340 * have to cut it into two segments. We shrink the existing
344 341 * "seg" at the low end, and create "nseg" for the high end.
345 342 */
346 343 caddr_t nbase = addr + len;
347 344 size_t nsize = (seg->s_base + seg->s_size) - nbase;
348 345 struct seg *nseg;
349 346
350 347 /*
351 348 * Trim down "seg" before trying to stick "nseg" into the as.
352 349 */
353 350 seg->s_size = addr - seg->s_base;
354 351 nseg = seg_alloc(seg->s_as, nbase, nsize);
355 352 if (nseg == NULL)
356 353 cmn_err(CE_PANIC, "segnf_unmap: seg_alloc failed");
357 354
358 355 /*
359 356 * s_data can't be NULL because of ASSERTs in common VM code.
360 357 */
361 358 nseg->s_ops = seg->s_ops;
362 359 nseg->s_data = nseg;
363 360 nseg->s_flags |= S_PURGE;
364 361 mutex_enter(&seg->s_as->a_contents);
365 362 seg->s_as->a_flags |= AS_NEEDSPURGE;
366 363 mutex_exit(&seg->s_as->a_contents);
367 364 }
368 365
369 366 return (0);
370 367 }
371 368
372 369 /*
373 370 * Free a segment.
374 371 */
375 372 static void
376 373 segnf_free(struct seg *seg)
377 374 {
378 375 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
379 376 }
380 377
381 378 /*
382 379 * No faults allowed on segnf.
383 380 */
384 381 static faultcode_t
385 382 segnf_nomap(void)
386 383 {
387 384 return (FC_NOMAP);
388 385 }
389 386
390 387 /* ARGSUSED */
391 388 static int
392 389 segnf_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
393 390 {
394 391 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
395 392 return (EACCES);
396 393 }
397 394
398 395 /* ARGSUSED */
399 396 static int
400 397 segnf_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
401 398 {
402 399 uint_t sprot;
403 400 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
404 401
405 402 sprot = seg->s_as == &kas ? PROT_READ : PROT_READ|PROT_USER;
406 403 return ((prot & sprot) == prot ? 0 : EACCES);
407 404 }
408 405
409 406 static void
410 407 segnf_badop(void)
411 408 {
412 409 panic("segnf_badop");
413 410 /*NOTREACHED*/
414 411 }
415 412
416 413 static int
417 414 segnf_nop(void)
418 415 {
419 416 return (0);
420 417 }
421 418
422 419 static int
423 420 segnf_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
424 421 {
425 422 size_t pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
426 423 size_t p;
427 424 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
428 425
429 426 for (p = 0; p < pgno; ++p)
430 427 protv[p] = PROT_READ;
431 428 return (0);
432 429 }
433 430
434 431 /* ARGSUSED */
435 432 static u_offset_t
436 433 segnf_getoffset(struct seg *seg, caddr_t addr)
437 434 {
438 435 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
439 436
440 437 return ((u_offset_t)0);
441 438 }
442 439
443 440 /* ARGSUSED */
444 441 static int
445 442 segnf_gettype(struct seg *seg, caddr_t addr)
446 443 {
447 444 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
448 445
449 446 return (MAP_SHARED);
450 447 }
451 448
452 449 /* ARGSUSED */
453 450 static int
454 451 segnf_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp)
455 452 {
456 453 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
457 454
458 455 *vpp = &nfvp;
459 456 return (0);
460 457 }
461 458
462 459 /*
463 460 * segnf pages are not dumped, so we just return
464 461 */
465 462 /* ARGSUSED */
466 463 static void
467 464 segnf_dump(struct seg *seg)
468 465 {}
469 466
470 467 /*ARGSUSED*/
471 468 static int
472 469 segnf_pagelock(struct seg *seg, caddr_t addr, size_t len,
473 470 struct page ***ppp, enum lock_type type, enum seg_rw rw)
474 471 {
475 472 return (ENOTSUP);
476 473 }
477 474
478 475 /*ARGSUSED*/
479 476 static int
480 477 segnf_setpagesize(struct seg *seg, caddr_t addr, size_t len,
481 478 uint_t szc)
482 479 {
483 480 return (ENOTSUP);
484 481 }
485 482
486 483 /*ARGSUSED*/
487 484 static int
488 485 segnf_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
489 486 {
490 487 return (ENODEV);
491 488 }
492 489
493 490 /*ARGSUSED*/
494 491 static lgrp_mem_policy_info_t *
495 492 segnf_getpolicy(struct seg *seg, caddr_t addr)
496 493 {
497 494 return (NULL);
498 495 }
↓ open down ↓ |
388 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX