* Allocate the kernel stack for a new thread. */
| 359 | * Allocate the kernel stack for a new thread. |
| 360 | */ |
| 361 | int |
| 362 | vm_thread_new(struct thread *td, int pages) |
| 363 | { |
| 364 | vm_offset_t ks; |
| 365 | |
| 366 | /* Bounds check */ |
| 367 | if (pages <= 1) |
| 368 | pages = kstack_pages; |
| 369 | else if (pages > KSTACK_MAX_PAGES) |
| 370 | pages = KSTACK_MAX_PAGES; |
| 371 | |
| 372 | ks = 0; |
| 373 | if (pages == kstack_pages && kstack_cache != NULL) |
| 374 | ks = (vm_offset_t)uma_zalloc(kstack_cache, M_NOWAIT); |
| 375 | |
| 376 | /* |
| 377 | * Ensure that kstack objects can draw pages from any memory |
| 378 | * domain. Otherwise a local memory shortage can block a process |
| 379 | * swap-in. |
| 380 | */ |
| 381 | if (ks == 0) |
| 382 | ks = vm_thread_stack_create(DOMAINSET_PREF(PCPU_GET(domain)), |
| 383 | pages); |
| 384 | if (ks == 0) |
| 385 | return (0); |
| 386 | td->td_kstack = ks; |
| 387 | td->td_kstack_pages = pages; |
| 388 | return (1); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Dispose of a thread's kernel stack. |
no test coverage detected