Merge commit '7e934d3acc051b7ee3ef0d11571fd1225800a607'
[unleashed.git] / kernel / os / mem_config.c
blobe676fa3e69adab99772fbe2485ac8853796f5617
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/cmn_err.h>
28 #include <sys/vmem.h>
29 #include <sys/kmem.h>
30 #include <sys/systm.h>
31 #include <sys/machsystm.h> /* for page_freelist_coalesce() */
32 #include <sys/errno.h>
33 #include <sys/memnode.h>
34 #include <sys/memlist.h>
35 #include <sys/memlist_impl.h>
36 #include <sys/tuneable.h>
37 #include <sys/proc.h>
38 #include <sys/disp.h>
39 #include <sys/debug.h>
40 #include <sys/vm.h>
41 #include <sys/callb.h>
42 #include <sys/memlist_plat.h> /* for installed_top_size() */
43 #include <sys/condvar_impl.h> /* for CV_HAS_WAITERS() */
44 #include <sys/dumphdr.h> /* for dump_resize() */
45 #include <sys/atomic.h> /* for use in stats collection */
46 #include <sys/rwlock.h>
47 #include <sys/cpuvar.h>
48 #include <vm/seg_kmem.h>
49 #include <vm/seg_kpm.h>
50 #include <vm/page.h>
51 #include <vm/vm_dep.h>
52 #define SUNDDI_IMPL /* so sunddi.h will not redefine splx() et al */
53 #include <sys/sunddi.h>
54 #include <sys/mem_config.h>
55 #include <sys/lgrp.h>
56 #include <sys/ddi.h>
57 #include <sys/modctl.h>
59 extern struct memlist *phys_avail;
61 extern uint_t page_ctrs_adjust(int);
62 void page_ctrs_cleanup(void);
63 static void kphysm_setup_post_add(pgcnt_t);
64 static int kphysm_setup_pre_del(pgcnt_t);
65 static void kphysm_setup_post_del(pgcnt_t, int);
67 static int kphysm_split_memseg(pfn_t base, pgcnt_t npgs);
69 static int delspan_reserve(pfn_t, pgcnt_t);
70 static void delspan_unreserve(pfn_t, pgcnt_t);
72 kmutex_t memseg_lists_lock;
73 struct memseg *memseg_va_avail;
74 struct memseg *memseg_alloc(void);
75 static struct memseg *memseg_delete_junk;
76 static struct memseg *memseg_edit_junk;
77 void memseg_remap_init(void);
78 static void memseg_remap_to_dummy(struct memseg *);
79 static void kphysm_addmem_error_undospan(pfn_t, pgcnt_t);
80 static struct memseg *memseg_reuse(pgcnt_t);
82 static struct kmem_cache *memseg_cache;
85 * Interfaces to manage externally allocated
86 * page_t memory (metadata) for a memseg.
88 #pragma weak memseg_alloc_meta
89 #pragma weak memseg_free_meta
90 #pragma weak memseg_get_metapfn
91 #pragma weak memseg_remap_meta
93 extern int ppvm_enable;
94 extern page_t *ppvm_base;
95 extern int memseg_alloc_meta(pfn_t, pgcnt_t, void **, pgcnt_t *);
96 extern void memseg_free_meta(void *, pgcnt_t);
97 extern pfn_t memseg_get_metapfn(void *, pgcnt_t);
98 extern void memseg_remap_meta(struct memseg *);
99 static int memseg_is_dynamic(struct memseg *);
100 static int memseg_includes_meta(struct memseg *);
101 pfn_t memseg_get_start(struct memseg *);
102 static void memseg_cpu_vm_flush(void);
104 int meta_alloc_enable;
106 #ifdef DEBUG
107 static int memseg_debug;
108 #define MEMSEG_DEBUG(args...) if (memseg_debug) printf(args)
109 #else
110 #define MEMSEG_DEBUG(...)
111 #endif
114 * Add a chunk of memory to the system.
115 * base: starting PAGESIZE page of new memory.
116 * npgs: length in PAGESIZE pages.
118 * Adding mem this way doesn't increase the size of the hash tables;
119 * growing them would be too hard. This should be OK, but adding memory
120 * dynamically most likely means more hash misses, since the tables will
121 * be smaller than they otherwise would be.
124 kphysm_add_memory_dynamic(pfn_t base, pgcnt_t npgs)
126 page_t *pp;
127 page_t *opp, *oepp, *segpp;
128 struct memseg *seg;
129 uint64_t avmem;
130 pfn_t pfn;
131 pfn_t pt_base = base;
132 pgcnt_t tpgs = npgs;
133 pgcnt_t metapgs = 0;
134 int exhausted;
135 pfn_t pnum;
136 int mnode;
137 caddr_t vaddr;
138 int reuse;
139 int mlret;
140 int rv;
141 int flags;
142 int meta_alloc = 0;
143 void *mapva;
144 void *metabase = (void *)base;
145 pgcnt_t nkpmpgs = 0;
146 offset_t kpm_pages_off;
148 cmn_err(CE_CONT,
149 "?kphysm_add_memory_dynamic: adding %ldK at 0x%" PRIx64 "\n",
150 npgs << (PAGESHIFT - 10), (uint64_t)base << PAGESHIFT);
153 * Add this span in the delete list to prevent interactions.
155 if (!delspan_reserve(base, npgs)) {
156 return (KPHYSM_ESPAN);
159 * Check to see if any of the memory span has been added
160 * by trying an add to the installed memory list. This
161 * forms the interlocking process for add.
164 memlist_write_lock();
166 mlret = memlist_add_span((uint64_t)(pt_base) << PAGESHIFT,
167 (uint64_t)(tpgs) << PAGESHIFT, &phys_install);
169 if (mlret == MEML_SPANOP_OK)
170 installed_top_size(phys_install, &physmax, &physinstalled);
172 memlist_write_unlock();
174 if (mlret != MEML_SPANOP_OK) {
175 if (mlret == MEML_SPANOP_EALLOC) {
176 delspan_unreserve(pt_base, tpgs);
177 return (KPHYSM_ERESOURCE);
178 } else if (mlret == MEML_SPANOP_ESPAN) {
179 delspan_unreserve(pt_base, tpgs);
180 return (KPHYSM_ESPAN);
181 } else {
182 delspan_unreserve(pt_base, tpgs);
183 return (KPHYSM_ERESOURCE);
187 if (meta_alloc_enable) {
189 * Allocate the page_t's from existing memory;
190 * if that fails, allocate from the incoming memory.
192 rv = memseg_alloc_meta(base, npgs, &metabase, &metapgs);
193 if (rv == KPHYSM_OK) {
194 ASSERT(metapgs);
195 ASSERT(btopr(npgs * sizeof (page_t)) <= metapgs);
196 meta_alloc = 1;
197 goto mapalloc;
202 * We store the page_t's for this new memory in the first
203 * few pages of the chunk. Here, we go and get'em ...
207 * The expression after the '-' gives the number of pages
208 * that will fit in the new memory based on a requirement
209 * of (PAGESIZE + sizeof (page_t)) bytes per page.
211 metapgs = npgs - (((uint64_t)(npgs) << PAGESHIFT) /
212 (PAGESIZE + sizeof (page_t)));
214 npgs -= metapgs;
215 base += metapgs;
217 ASSERT(btopr(npgs * sizeof (page_t)) <= metapgs);
219 exhausted = (metapgs == 0 || npgs == 0);
221 if (kpm_enable && !exhausted) {
222 pgcnt_t start, end, nkpmpgs_prelim;
223 size_t ptsz;
226 * A viable kpm large page mapping must not overlap two
227 * dynamic memsegs. Therefore the total size is checked
228 * to be at least kpm_pgsz and also whether start and end
229 * points are at least kpm_pgsz aligned.
231 if (ptokpmp(tpgs) < 1 || pmodkpmp(pt_base) ||
232 pmodkpmp(base + npgs)) {
234 kphysm_addmem_error_undospan(pt_base, tpgs);
237 * There is no specific error code for violating
238 * kpm granularity constraints.
240 return (KPHYSM_ENOTVIABLE);
243 start = kpmptop(ptokpmp(base));
244 end = kpmptop(ptokpmp(base + npgs));
245 nkpmpgs_prelim = ptokpmp(end - start);
246 ptsz = npgs * sizeof (page_t);
247 metapgs = btopr(ptsz + nkpmpgs_prelim * KPMPAGE_T_SZ);
248 exhausted = (tpgs <= metapgs);
249 if (!exhausted) {
250 npgs = tpgs - metapgs;
251 base = pt_base + metapgs;
253 /* final nkpmpgs */
254 start = kpmptop(ptokpmp(base));
255 nkpmpgs = ptokpmp(end - start);
256 kpm_pages_off = ptsz +
257 (nkpmpgs_prelim - nkpmpgs) * KPMPAGE_T_SZ;
262 * Is memory area supplied too small?
264 if (exhausted) {
265 kphysm_addmem_error_undospan(pt_base, tpgs);
267 * There is no specific error code for 'too small'.
269 return (KPHYSM_ERESOURCE);
272 mapalloc:
274 * We may re-use a previously allocated VA space for the page_ts
275 * eventually, but we need to initialize and lock the pages first.
279 * Get an address in the kernel address map, map
280 * the page_t pages and see if we can touch them.
283 mapva = vmem_alloc(heap_arena, ptob(metapgs), VM_NOSLEEP);
284 if (mapva == NULL) {
285 cmn_err(CE_WARN, "kphysm_add_memory_dynamic:"
286 " Can't allocate VA for page_ts");
288 if (meta_alloc)
289 memseg_free_meta(metabase, metapgs);
290 kphysm_addmem_error_undospan(pt_base, tpgs);
292 return (KPHYSM_ERESOURCE);
294 pp = mapva;
296 if (physmax < (pt_base + tpgs))
297 physmax = (pt_base + tpgs);
300 * In the remapping code we map one page at a time so we must do
301 * the same here to match mapping sizes.
303 pfn = pt_base;
304 vaddr = (caddr_t)pp;
305 for (pnum = 0; pnum < metapgs; pnum++) {
306 if (meta_alloc)
307 pfn = memseg_get_metapfn(metabase, (pgcnt_t)pnum);
308 hat_devload(kas.a_hat, vaddr, ptob(1), pfn,
309 PROT_READ | PROT_WRITE,
310 HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
311 pfn++;
312 vaddr += ptob(1);
315 if (ddi_peek32((dev_info_t *)NULL,
316 (int32_t *)pp, (int32_t *)0) == DDI_FAILURE) {
318 cmn_err(CE_WARN, "kphysm_add_memory_dynamic:"
319 " Can't access pp array at 0x%p [phys 0x%lx]",
320 (void *)pp, pt_base);
322 hat_unload(kas.a_hat, (caddr_t)pp, ptob(metapgs),
323 HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
325 vmem_free(heap_arena, mapva, ptob(metapgs));
326 if (meta_alloc)
327 memseg_free_meta(metabase, metapgs);
328 kphysm_addmem_error_undospan(pt_base, tpgs);
330 return (KPHYSM_EFAULT);
334 * Add this memory slice to its memory node translation.
336 * Note that right now, each node may have only one slice;
337 * this may change with COD or in larger SSM systems with
338 * nested latency groups, so we must not assume that the
339 * node does not yet exist.
341 * Note that there may be multiple memory nodes associated with
342 * a single lgrp node on x86 systems.
344 pnum = pt_base + tpgs - 1;
345 mem_node_add_range(pt_base, pnum);
348 * Allocate or resize page counters as necessary to accommodate
349 * the increase in memory pages.
351 mnode = PFN_2_MEM_NODE(pnum);
352 PAGE_CTRS_ADJUST(base, npgs, rv);
353 if (rv) {
355 mem_node_del_range(pt_base, pnum);
357 /* cleanup the page counters */
358 page_ctrs_cleanup();
360 hat_unload(kas.a_hat, (caddr_t)pp, ptob(metapgs),
361 HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
363 vmem_free(heap_arena, mapva, ptob(metapgs));
364 if (meta_alloc)
365 memseg_free_meta(metabase, metapgs);
366 kphysm_addmem_error_undospan(pt_base, tpgs);
368 return (KPHYSM_ERESOURCE);
372 * Update the phys_avail memory list.
373 * The phys_install list was done at the start.
376 memlist_write_lock();
378 mlret = memlist_add_span((uint64_t)(base) << PAGESHIFT,
379 (uint64_t)(npgs) << PAGESHIFT, &phys_avail);
380 ASSERT(mlret == MEML_SPANOP_OK);
382 memlist_write_unlock();
384 /* See if we can find a memseg to re-use. */
385 if (meta_alloc) {
386 seg = memseg_reuse(0);
387 reuse = 1; /* force unmapping of temp mapva */
388 flags = MEMSEG_DYNAMIC | MEMSEG_META_ALLOC;
390 * There is a 1:1 fixed relationship between a pfn
391 * and a page_t VA. The pfn is used as an index into
392 * the ppvm_base page_t table in order to calculate
393 * the page_t base address for a given pfn range.
395 segpp = ppvm_base + base;
396 } else {
397 seg = memseg_reuse(metapgs);
398 reuse = (seg != NULL);
399 flags = MEMSEG_DYNAMIC | MEMSEG_META_INCL;
400 segpp = pp;
404 * Initialize the memseg structure representing this memory
405 * and add it to the existing list of memsegs. Do some basic
406 * initialization and add the memory to the system.
407 * In order to prevent lock deadlocks, the add_physmem()
408 * code is repeated here, but split into several stages.
410 * If a memseg is reused, invalidate memseg pointers in
411 * all cpu vm caches. We need to do this this since the check
412 * pp >= seg->pages && pp < seg->epages
413 * used in various places is not atomic and so the first compare
414 * can happen before reuse and the second compare after reuse.
415 * The invalidation ensures that a memseg is not deferenced while
416 * it's page/pfn pointers are changing.
418 if (seg == NULL) {
419 seg = memseg_alloc();
420 ASSERT(seg != NULL);
421 seg->msegflags = flags;
422 MEMSEG_DEBUG("memseg_get: alloc seg=0x%p, pages=0x%p",
423 (void *)seg, (void *)(seg->pages));
424 seg->pages = segpp;
425 } else {
426 ASSERT(seg->msegflags == flags);
427 ASSERT(seg->pages_base == seg->pages_end);
428 MEMSEG_DEBUG("memseg_get: reuse seg=0x%p, pages=0x%p",
429 (void *)seg, (void *)(seg->pages));
430 if (meta_alloc) {
431 memseg_cpu_vm_flush();
432 seg->pages = segpp;
436 seg->epages = seg->pages + npgs;
437 seg->pages_base = base;
438 seg->pages_end = base + npgs;
441 * Initialize metadata. The page_ts are set to locked state
442 * ready to be freed.
444 bzero((caddr_t)pp, ptob(metapgs));
446 pfn = seg->pages_base;
447 /* Save the original pp base in case we reuse a memseg. */
448 opp = pp;
449 oepp = opp + npgs;
450 for (pp = opp; pp < oepp; pp++) {
451 pp->p_pagenum = pfn;
452 pfn++;
453 page_iolock_init(pp);
454 while (!page_lock(pp, SE_EXCL, NULL, P_RECLAIM))
455 continue;
456 pp->p_offset = (uoff_t)-1;
459 if (reuse) {
460 /* Remap our page_ts to the re-used memseg VA space. */
461 pfn = pt_base;
462 vaddr = (caddr_t)seg->pages;
463 for (pnum = 0; pnum < metapgs; pnum++) {
464 if (meta_alloc)
465 pfn = memseg_get_metapfn(metabase,
466 (pgcnt_t)pnum);
467 hat_devload(kas.a_hat, vaddr, ptob(1), pfn,
468 PROT_READ | PROT_WRITE,
469 HAT_LOAD_REMAP | HAT_LOAD | HAT_LOAD_NOCONSIST);
470 pfn++;
471 vaddr += ptob(1);
474 hat_unload(kas.a_hat, (caddr_t)opp, ptob(metapgs),
475 HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
477 vmem_free(heap_arena, mapva, ptob(metapgs));
480 hat_kpm_addmem_mseg_update(seg, nkpmpgs, kpm_pages_off);
482 memsegs_lock(1);
485 * The new memseg is inserted at the beginning of the list.
486 * Not only does this save searching for the tail, but in the
487 * case of a re-used memseg, it solves the problem of what
488 * happens if some process has still got a pointer to the
489 * memseg and follows the next pointer to continue traversing
490 * the memsegs list.
493 hat_kpm_addmem_mseg_insert(seg);
495 seg->next = memsegs;
496 membar_producer();
498 hat_kpm_addmem_memsegs_update(seg);
500 memsegs = seg;
502 build_pfn_hash();
504 total_pages += npgs;
507 * Recalculate the paging parameters now total_pages has changed.
508 * This will also cause the clock hands to be reset before next use.
510 setupclock(1);
512 memsegs_unlock(1);
514 PLCNT_MODIFY_MAX(seg->pages_base, (long)npgs);
517 * Free the pages outside the lock to avoid locking loops.
519 for (pp = seg->pages; pp < seg->epages; pp++) {
520 page_free(pp, 1);
524 * Now that we've updated the appropriate memory lists we
525 * need to reset a number of globals, since we've increased memory.
526 * Several have already been updated for us as noted above. The
527 * globals we're interested in at this point are:
528 * physmax - highest page frame number.
529 * physinstalled - number of pages currently installed (done earlier)
530 * maxmem - max free pages in the system
531 * physmem - physical memory pages available
532 * availrmem - real memory available
535 mutex_enter(&freemem_lock);
536 maxmem += npgs;
537 physmem += npgs;
538 availrmem += npgs;
539 availrmem_initial += npgs;
541 mutex_exit(&freemem_lock);
543 dump_resize();
545 page_freelist_coalesce_all(mnode);
547 kphysm_setup_post_add(npgs);
549 cmn_err(CE_CONT, "?kphysm_add_memory_dynamic: mem = %ldK "
550 "(0x%" PRIx64 ")\n",
551 physinstalled << (PAGESHIFT - 10),
552 (uint64_t)physinstalled << PAGESHIFT);
554 avmem = (uint64_t)freemem << PAGESHIFT;
555 cmn_err(CE_CONT, "?kphysm_add_memory_dynamic: "
556 "avail mem = %" PRId64 "\n", avmem);
559 * Update lgroup generation number on single lgroup systems
561 if (nlgrps == 1)
562 lgrp_config(LGRP_CONFIG_GEN_UPDATE, 0, 0);
565 * Inform DDI of update
567 ddi_mem_update((uint64_t)(pt_base) << PAGESHIFT,
568 (uint64_t)(tpgs) << PAGESHIFT);
570 delspan_unreserve(pt_base, tpgs);
572 return (KPHYSM_OK); /* Successfully added system memory */
576 * There are various error conditions in kphysm_add_memory_dynamic()
577 * which require a rollback of already changed global state.
579 static void
580 kphysm_addmem_error_undospan(pfn_t pt_base, pgcnt_t tpgs)
582 int mlret;
584 /* Unreserve memory span. */
585 memlist_write_lock();
587 mlret = memlist_delete_span(
588 (uint64_t)(pt_base) << PAGESHIFT,
589 (uint64_t)(tpgs) << PAGESHIFT, &phys_install);
591 ASSERT(mlret == MEML_SPANOP_OK);
592 phys_install_has_changed();
593 installed_top_size(phys_install, &physmax, &physinstalled);
595 memlist_write_unlock();
596 delspan_unreserve(pt_base, tpgs);
600 * Only return an available memseg of exactly the right size
601 * if size is required.
602 * When the meta data area has it's own virtual address space
603 * we will need to manage this more carefully and do best fit
604 * allocations, possibly splitting an available area.
606 struct memseg *
607 memseg_reuse(pgcnt_t metapgs)
609 int type;
610 struct memseg **segpp, *seg;
612 mutex_enter(&memseg_lists_lock);
614 segpp = &memseg_va_avail;
615 for (; (seg = *segpp) != NULL; segpp = &seg->lnext) {
616 caddr_t end;
619 * Make sure we are reusing the right segment type.
621 type = metapgs ? MEMSEG_META_INCL : MEMSEG_META_ALLOC;
623 if ((seg->msegflags & (MEMSEG_META_INCL | MEMSEG_META_ALLOC))
624 != type)
625 continue;
627 if (kpm_enable)
628 end = hat_kpm_mseg_reuse(seg);
629 else
630 end = (caddr_t)seg->epages;
633 * Check for the right size if it is provided.
635 if (!metapgs || btopr(end - (caddr_t)seg->pages) == metapgs) {
636 *segpp = seg->lnext;
637 seg->lnext = NULL;
638 break;
641 mutex_exit(&memseg_lists_lock);
643 return (seg);
646 static uint_t handle_gen;
648 struct memdelspan {
649 struct memdelspan *mds_next;
650 pfn_t mds_base;
651 pgcnt_t mds_npgs;
652 uint_t *mds_bitmap;
653 uint_t *mds_bitmap_retired;
656 #define NBPBMW (sizeof (uint_t) * NBBY)
657 #define MDS_BITMAPBYTES(MDSP) \
658 ((((MDSP)->mds_npgs + NBPBMW - 1) / NBPBMW) * sizeof (uint_t))
660 struct transit_list {
661 struct transit_list *trl_next;
662 struct memdelspan *trl_spans;
663 int trl_collect;
666 struct transit_list_head {
667 kmutex_t trh_lock;
668 struct transit_list *trh_head;
671 static struct transit_list_head transit_list_head;
673 struct mem_handle;
674 static void transit_list_collect(struct mem_handle *, int);
675 static void transit_list_insert(struct transit_list *);
676 static void transit_list_remove(struct transit_list *);
678 #ifdef DEBUG
679 #define MEM_DEL_STATS
680 #endif /* DEBUG */
682 #ifdef MEM_DEL_STATS
683 static int mem_del_stat_print = 0;
684 struct mem_del_stat {
685 uint_t nloop;
686 uint_t need_free;
687 uint_t free_loop;
688 uint_t free_low;
689 uint_t free_failed;
690 uint_t ncheck;
691 uint_t nopaget;
692 uint_t lockfail;
693 uint_t nfree;
694 uint_t nreloc;
695 uint_t nrelocfail;
696 uint_t already_done;
697 uint_t first_notfree;
698 uint_t npplocked;
699 uint_t nlockreloc;
700 uint_t nnorepl;
701 uint_t nmodreloc;
702 uint_t ndestroy;
703 uint_t nputpage;
704 uint_t nnoreclaim;
705 uint_t ndelay;
706 uint_t demotefail;
707 uint64_t nticks_total;
708 uint64_t nticks_pgrp;
709 uint_t retired;
710 uint_t toxic;
711 uint_t failing;
712 uint_t modtoxic;
713 uint_t npplkdtoxic;
714 uint_t gptlmodfail;
715 uint_t gptllckfail;
718 * The stat values are only incremented in the delete thread
719 * so no locking or atomic required.
721 #define MDSTAT_INCR(MHP, FLD) (MHP)->mh_delstat.FLD++
722 #define MDSTAT_TOTAL(MHP, ntck) ((MHP)->mh_delstat.nticks_total += (ntck))
723 #define MDSTAT_PGRP(MHP, ntck) ((MHP)->mh_delstat.nticks_pgrp += (ntck))
724 static void mem_del_stat_print_func(struct mem_handle *);
725 #define MDSTAT_PRINT(MHP) mem_del_stat_print_func((MHP))
726 #else /* MEM_DEL_STATS */
727 #define MDSTAT_INCR(MHP, FLD)
728 #define MDSTAT_TOTAL(MHP, ntck)
729 #define MDSTAT_PGRP(MHP, ntck)
730 #define MDSTAT_PRINT(MHP)
731 #endif /* MEM_DEL_STATS */
733 typedef enum mhnd_state {MHND_FREE = 0, MHND_INIT, MHND_STARTING,
734 MHND_RUNNING, MHND_DONE, MHND_RELEASE} mhnd_state_t;
737 * mh_mutex must be taken to examine or change mh_exthandle and mh_state.
738 * The mutex may not be required for other fields, dependent on mh_state.
740 struct mem_handle {
741 kmutex_t mh_mutex;
742 struct mem_handle *mh_next;
743 memhandle_t mh_exthandle;
744 mhnd_state_t mh_state;
745 struct transit_list mh_transit;
746 pgcnt_t mh_phys_pages;
747 pgcnt_t mh_vm_pages;
748 pgcnt_t mh_hold_todo;
749 void (*mh_delete_complete)(void *, int error);
750 void *mh_delete_complete_arg;
751 volatile uint_t mh_cancel;
752 volatile uint_t mh_dr_aio_cleanup_cancel;
753 volatile uint_t mh_aio_cleanup_done;
754 kcondvar_t mh_cv;
755 kthread_id_t mh_thread_id;
756 page_t *mh_deleted; /* link through p_next */
757 #ifdef MEM_DEL_STATS
758 struct mem_del_stat mh_delstat;
759 #endif /* MEM_DEL_STATS */
762 static struct mem_handle *mem_handle_head;
763 static kmutex_t mem_handle_list_mutex;
765 static struct mem_handle *
766 kphysm_allocate_mem_handle()
768 struct mem_handle *mhp;
770 mhp = kmem_zalloc(sizeof (struct mem_handle), KM_SLEEP);
771 mutex_init(&mhp->mh_mutex, NULL, MUTEX_DEFAULT, NULL);
772 mutex_enter(&mem_handle_list_mutex);
773 mutex_enter(&mhp->mh_mutex);
774 /* handle_gen is protected by list mutex. */
775 mhp->mh_exthandle = (memhandle_t)(uintptr_t)(++handle_gen);
776 mhp->mh_next = mem_handle_head;
777 mem_handle_head = mhp;
778 mutex_exit(&mem_handle_list_mutex);
780 return (mhp);
783 static void
784 kphysm_free_mem_handle(struct mem_handle *mhp)
786 struct mem_handle **mhpp;
788 ASSERT(mutex_owned(&mhp->mh_mutex));
789 ASSERT(mhp->mh_state == MHND_FREE);
791 * Exit the mutex to preserve locking order. This is OK
792 * here as once in the FREE state, the handle cannot
793 * be found by a lookup.
795 mutex_exit(&mhp->mh_mutex);
797 mutex_enter(&mem_handle_list_mutex);
798 mhpp = &mem_handle_head;
799 while (*mhpp != NULL && *mhpp != mhp)
800 mhpp = &(*mhpp)->mh_next;
801 ASSERT(*mhpp == mhp);
803 * No need to lock the handle (mh_mutex) as only
804 * mh_next changing and this is the only thread that
805 * can be referncing mhp.
807 *mhpp = mhp->mh_next;
808 mutex_exit(&mem_handle_list_mutex);
810 mutex_destroy(&mhp->mh_mutex);
811 kmem_free(mhp, sizeof (struct mem_handle));
815 * This function finds the internal mem_handle corresponding to an
816 * external handle and returns it with the mh_mutex held.
818 static struct mem_handle *
819 kphysm_lookup_mem_handle(memhandle_t handle)
821 struct mem_handle *mhp;
823 mutex_enter(&mem_handle_list_mutex);
824 for (mhp = mem_handle_head; mhp != NULL; mhp = mhp->mh_next) {
825 if (mhp->mh_exthandle == handle) {
826 mutex_enter(&mhp->mh_mutex);
828 * The state of the handle could have been changed
829 * by kphysm_del_release() while waiting for mh_mutex.
831 if (mhp->mh_state == MHND_FREE) {
832 mutex_exit(&mhp->mh_mutex);
833 continue;
835 break;
838 mutex_exit(&mem_handle_list_mutex);
839 return (mhp);
843 kphysm_del_gethandle(memhandle_t *xmhp)
845 struct mem_handle *mhp;
847 mhp = kphysm_allocate_mem_handle();
849 * The handle is allocated using KM_SLEEP, so cannot fail.
850 * If the implementation is changed, the correct error to return
851 * here would be KPHYSM_ENOHANDLES.
853 ASSERT(mhp->mh_state == MHND_FREE);
854 mhp->mh_state = MHND_INIT;
855 *xmhp = mhp->mh_exthandle;
856 mutex_exit(&mhp->mh_mutex);
857 return (KPHYSM_OK);
860 static int
861 overlapping(pfn_t b1, pgcnt_t l1, pfn_t b2, pgcnt_t l2)
863 pfn_t e1, e2;
865 e1 = b1 + l1;
866 e2 = b2 + l2;
868 return (!(b2 >= e1 || b1 >= e2));
871 static int can_remove_pgs(pgcnt_t);
873 static struct memdelspan *
874 span_to_install(pfn_t base, pgcnt_t npgs)
876 struct memdelspan *mdsp;
877 struct memdelspan *mdsp_new;
878 uint64_t address, size, thislen;
879 struct memlist *mlp;
881 mdsp_new = NULL;
883 address = (uint64_t)base << PAGESHIFT;
884 size = (uint64_t)npgs << PAGESHIFT;
885 while (size != 0) {
886 memlist_read_lock();
887 for (mlp = phys_install; mlp != NULL; mlp = mlp->ml_next) {
888 if (address >= (mlp->ml_address + mlp->ml_size))
889 continue;
890 if ((address + size) > mlp->ml_address)
891 break;
893 if (mlp == NULL) {
894 address += size;
895 size = 0;
896 thislen = 0;
897 } else {
898 if (address < mlp->ml_address) {
899 size -= (mlp->ml_address - address);
900 address = mlp->ml_address;
902 ASSERT(address >= mlp->ml_address);
903 if ((address + size) >
904 (mlp->ml_address + mlp->ml_size)) {
905 thislen =
906 mlp->ml_size - (address - mlp->ml_address);
907 } else {
908 thislen = size;
911 memlist_read_unlock();
912 /* TODO: phys_install could change now */
913 if (thislen == 0)
914 continue;
915 mdsp = kmem_zalloc(sizeof (struct memdelspan), KM_SLEEP);
916 mdsp->mds_base = btop(address);
917 mdsp->mds_npgs = btop(thislen);
918 mdsp->mds_next = mdsp_new;
919 mdsp_new = mdsp;
920 address += thislen;
921 size -= thislen;
923 return (mdsp_new);
926 static void
927 free_delspans(struct memdelspan *mdsp)
929 struct memdelspan *amdsp;
931 while ((amdsp = mdsp) != NULL) {
932 mdsp = amdsp->mds_next;
933 kmem_free(amdsp, sizeof (struct memdelspan));
938 * Concatenate lists. No list ordering is required.
941 static void
942 delspan_concat(struct memdelspan **mdspp, struct memdelspan *mdsp)
944 while (*mdspp != NULL)
945 mdspp = &(*mdspp)->mds_next;
947 *mdspp = mdsp;
951 * Given a new list of delspans, check there is no overlap with
952 * all existing span activity (add or delete) and then concatenate
953 * the new spans to the given list.
954 * Return 1 for OK, 0 if overlapping.
956 static int
957 delspan_insert(
958 struct transit_list *my_tlp,
959 struct memdelspan *mdsp_new)
961 struct transit_list_head *trh;
962 struct transit_list *tlp;
963 int ret;
965 trh = &transit_list_head;
967 ASSERT(my_tlp != NULL);
968 ASSERT(mdsp_new != NULL);
970 ret = 1;
971 mutex_enter(&trh->trh_lock);
972 /* ASSERT(my_tlp->trl_spans == NULL || tlp_in_list(trh, my_tlp)); */
973 for (tlp = trh->trh_head; tlp != NULL; tlp = tlp->trl_next) {
974 struct memdelspan *mdsp;
976 for (mdsp = tlp->trl_spans; mdsp != NULL;
977 mdsp = mdsp->mds_next) {
978 struct memdelspan *nmdsp;
980 for (nmdsp = mdsp_new; nmdsp != NULL;
981 nmdsp = nmdsp->mds_next) {
982 if (overlapping(mdsp->mds_base, mdsp->mds_npgs,
983 nmdsp->mds_base, nmdsp->mds_npgs)) {
984 ret = 0;
985 goto done;
990 done:
991 if (ret != 0) {
992 if (my_tlp->trl_spans == NULL)
993 transit_list_insert(my_tlp);
994 delspan_concat(&my_tlp->trl_spans, mdsp_new);
996 mutex_exit(&trh->trh_lock);
997 return (ret);
1000 static void
1001 delspan_remove(
1002 struct transit_list *my_tlp,
1003 pfn_t base,
1004 pgcnt_t npgs)
1006 struct transit_list_head *trh;
1007 struct memdelspan *mdsp;
1009 trh = &transit_list_head;
1011 ASSERT(my_tlp != NULL);
1013 mutex_enter(&trh->trh_lock);
1014 if ((mdsp = my_tlp->trl_spans) != NULL) {
1015 if (npgs == 0) {
1016 my_tlp->trl_spans = NULL;
1017 free_delspans(mdsp);
1018 transit_list_remove(my_tlp);
1019 } else {
1020 struct memdelspan **prv;
1022 prv = &my_tlp->trl_spans;
1023 while (mdsp != NULL) {
1024 pfn_t p_end;
1026 p_end = mdsp->mds_base + mdsp->mds_npgs;
1027 if (mdsp->mds_base >= base &&
1028 p_end <= (base + npgs)) {
1029 *prv = mdsp->mds_next;
1030 mdsp->mds_next = NULL;
1031 free_delspans(mdsp);
1032 } else {
1033 prv = &mdsp->mds_next;
1035 mdsp = *prv;
1037 if (my_tlp->trl_spans == NULL)
1038 transit_list_remove(my_tlp);
1041 mutex_exit(&trh->trh_lock);
1045 * Reserve interface for add to stop delete before add finished.
1046 * This list is only accessed through the delspan_insert/remove
1047 * functions and so is fully protected by the mutex in struct transit_list.
1050 static struct transit_list reserve_transit;
1052 static int
1053 delspan_reserve(pfn_t base, pgcnt_t npgs)
1055 struct memdelspan *mdsp;
1056 int ret;
1058 mdsp = kmem_zalloc(sizeof (struct memdelspan), KM_SLEEP);
1059 mdsp->mds_base = base;
1060 mdsp->mds_npgs = npgs;
1061 if ((ret = delspan_insert(&reserve_transit, mdsp)) == 0) {
1062 free_delspans(mdsp);
1064 return (ret);
1067 static void
1068 delspan_unreserve(pfn_t base, pgcnt_t npgs)
1070 delspan_remove(&reserve_transit, base, npgs);
1074 * Return whether memseg was created by kphysm_add_memory_dynamic().
1076 static int
1077 memseg_is_dynamic(struct memseg *seg)
1079 return (seg->msegflags & MEMSEG_DYNAMIC);
1083 kphysm_del_span(
1084 memhandle_t handle,
1085 pfn_t base,
1086 pgcnt_t npgs)
1088 struct mem_handle *mhp;
1089 struct memseg *seg;
1090 struct memdelspan *mdsp;
1091 struct memdelspan *mdsp_new;
1092 pgcnt_t phys_pages, vm_pages;
1093 pfn_t p_end;
1094 page_t *pp;
1095 int ret;
1097 mhp = kphysm_lookup_mem_handle(handle);
1098 if (mhp == NULL) {
1099 return (KPHYSM_EHANDLE);
1101 if (mhp->mh_state != MHND_INIT) {
1102 mutex_exit(&mhp->mh_mutex);
1103 return (KPHYSM_ESEQUENCE);
1107 * Intersect the span with the installed memory list (phys_install).
1109 mdsp_new = span_to_install(base, npgs);
1110 if (mdsp_new == NULL) {
1112 * No physical memory in this range. Is this an
1113 * error? If an attempt to start the delete is made
1114 * for OK returns from del_span such as this, start will
1115 * return an error.
1116 * Could return KPHYSM_ENOWORK.
1119 * It is assumed that there are no error returns
1120 * from span_to_install() due to kmem_alloc failure.
1122 mutex_exit(&mhp->mh_mutex);
1123 return (KPHYSM_OK);
1126 * Does this span overlap an existing span?
1128 if (delspan_insert(&mhp->mh_transit, mdsp_new) == 0) {
1130 * Differentiate between already on list for this handle
1131 * (KPHYSM_EDUP) and busy elsewhere (KPHYSM_EBUSY).
1133 ret = KPHYSM_EBUSY;
1134 for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
1135 mdsp = mdsp->mds_next) {
1136 if (overlapping(mdsp->mds_base, mdsp->mds_npgs,
1137 base, npgs)) {
1138 ret = KPHYSM_EDUP;
1139 break;
1142 mutex_exit(&mhp->mh_mutex);
1143 free_delspans(mdsp_new);
1144 return (ret);
1147 * At this point the spans in mdsp_new have been inserted into the
1148 * list of spans for this handle and thereby to the global list of
1149 * spans being processed. Each of these spans must now be checked
1150 * for relocatability. As a side-effect segments in the memseg list
1151 * may be split.
1153 * Note that mdsp_new can no longer be used as it is now part of
1154 * a larger list. Select elements of this larger list based
1155 * on base and npgs.
1157 restart:
1158 phys_pages = 0;
1159 vm_pages = 0;
1160 ret = KPHYSM_OK;
1161 for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
1162 mdsp = mdsp->mds_next) {
1163 pgcnt_t pages_checked;
1165 if (!overlapping(mdsp->mds_base, mdsp->mds_npgs, base, npgs)) {
1166 continue;
1168 p_end = mdsp->mds_base + mdsp->mds_npgs;
1170 * The pages_checked count is a hack. All pages should be
1171 * checked for relocatability. Those not covered by memsegs
1172 * should be tested with arch_kphysm_del_span_ok().
1174 pages_checked = 0;
1175 for (seg = memsegs; seg; seg = seg->next) {
1176 pfn_t mseg_start;
1178 if (seg->pages_base >= p_end ||
1179 seg->pages_end <= mdsp->mds_base) {
1180 /* Span and memseg don't overlap. */
1181 continue;
1183 mseg_start = memseg_get_start(seg);
1184 /* Check that segment is suitable for delete. */
1185 if (memseg_includes_meta(seg)) {
1187 * Check that this segment is completely
1188 * within the span.
1190 if (mseg_start < mdsp->mds_base ||
1191 seg->pages_end > p_end) {
1192 ret = KPHYSM_EBUSY;
1193 break;
1195 pages_checked += seg->pages_end - mseg_start;
1196 } else {
1198 * If this segment is larger than the span,
1199 * try to split it. After the split, it
1200 * is necessary to restart.
1202 if (seg->pages_base < mdsp->mds_base ||
1203 seg->pages_end > p_end) {
1204 pfn_t abase;
1205 pgcnt_t anpgs;
1206 int s_ret;
1208 /* Split required. */
1209 if (mdsp->mds_base < seg->pages_base)
1210 abase = seg->pages_base;
1211 else
1212 abase = mdsp->mds_base;
1213 if (p_end > seg->pages_end)
1214 anpgs = seg->pages_end - abase;
1215 else
1216 anpgs = p_end - abase;
1217 s_ret = kphysm_split_memseg(abase,
1218 anpgs);
1219 if (s_ret == 0) {
1220 /* Split failed. */
1221 ret = KPHYSM_ERESOURCE;
1222 break;
1224 goto restart;
1226 pages_checked +=
1227 seg->pages_end - seg->pages_base;
1230 * The memseg is wholly within the delete span.
1231 * The individual pages can now be checked.
1233 /* Cage test. */
1234 for (pp = seg->pages; pp < seg->epages; pp++) {
1235 if (PP_ISNORELOC(pp)) {
1236 ret = KPHYSM_ENONRELOC;
1237 break;
1240 if (ret != KPHYSM_OK) {
1241 break;
1243 phys_pages += (seg->pages_end - mseg_start);
1244 vm_pages += MSEG_NPAGES(seg);
1246 if (ret != KPHYSM_OK)
1247 break;
1248 if (pages_checked != mdsp->mds_npgs) {
1249 ret = KPHYSM_ENONRELOC;
1250 break;
1254 if (ret == KPHYSM_OK) {
1255 mhp->mh_phys_pages += phys_pages;
1256 mhp->mh_vm_pages += vm_pages;
1257 } else {
1259 * Keep holding the mh_mutex to prevent it going away.
1261 delspan_remove(&mhp->mh_transit, base, npgs);
1263 mutex_exit(&mhp->mh_mutex);
1264 return (ret);
1268 kphysm_del_span_query(
1269 pfn_t base,
1270 pgcnt_t npgs,
1271 memquery_t *mqp)
1273 struct memdelspan *mdsp;
1274 struct memdelspan *mdsp_new;
1275 int done_first_nonreloc;
1277 mqp->phys_pages = 0;
1278 mqp->managed = 0;
1279 mqp->nonrelocatable = 0;
1280 mqp->first_nonrelocatable = 0;
1281 mqp->last_nonrelocatable = 0;
1283 mdsp_new = span_to_install(base, npgs);
1285 * It is OK to proceed here if mdsp_new == NULL.
1287 done_first_nonreloc = 0;
1288 for (mdsp = mdsp_new; mdsp != NULL; mdsp = mdsp->mds_next) {
1289 pfn_t sbase;
1290 pgcnt_t snpgs;
1292 mqp->phys_pages += mdsp->mds_npgs;
1293 sbase = mdsp->mds_base;
1294 snpgs = mdsp->mds_npgs;
1295 while (snpgs != 0) {
1296 struct memseg *lseg, *seg;
1297 pfn_t p_end;
1298 page_t *pp;
1299 pfn_t mseg_start;
1301 p_end = sbase + snpgs;
1303 * Find the lowest addressed memseg that starts
1304 * after sbase and account for it.
1305 * This is to catch dynamic memsegs whose start
1306 * is hidden.
1308 seg = NULL;
1309 for (lseg = memsegs; lseg != NULL; lseg = lseg->next) {
1310 if ((lseg->pages_base >= sbase) ||
1311 (lseg->pages_base < p_end &&
1312 lseg->pages_end > sbase)) {
1313 if (seg == NULL ||
1314 seg->pages_base > lseg->pages_base)
1315 seg = lseg;
1318 if (seg != NULL) {
1319 mseg_start = memseg_get_start(seg);
1321 * Now have the full extent of the memseg so
1322 * do the range check.
1324 if (mseg_start >= p_end ||
1325 seg->pages_end <= sbase) {
1326 /* Span does not overlap memseg. */
1327 seg = NULL;
1331 * Account for gap either before the segment if
1332 * there is one or to the end of the span.
1334 if (seg == NULL || mseg_start > sbase) {
1335 pfn_t a_end;
1337 a_end = (seg == NULL) ? p_end : mseg_start;
1339 * Check with arch layer for relocatability.
1341 if (arch_kphysm_del_span_ok(sbase,
1342 (a_end - sbase))) {
1344 * No non-relocatble pages in this
1345 * area, avoid the fine-grained
1346 * test.
1348 snpgs -= (a_end - sbase);
1349 sbase = a_end;
1351 while (sbase < a_end) {
1352 if (!arch_kphysm_del_span_ok(sbase,
1353 1)) {
1354 mqp->nonrelocatable++;
1355 if (!done_first_nonreloc) {
1356 mqp->
1357 first_nonrelocatable
1358 = sbase;
1359 done_first_nonreloc = 1;
1361 mqp->last_nonrelocatable =
1362 sbase;
1364 sbase++;
1365 snpgs--;
1368 if (seg != NULL) {
1369 ASSERT(mseg_start <= sbase);
1370 if (seg->pages_base != mseg_start &&
1371 seg->pages_base > sbase) {
1372 pgcnt_t skip_pgs;
1375 * Skip the page_t area of a
1376 * dynamic memseg.
1378 skip_pgs = seg->pages_base - sbase;
1379 if (snpgs <= skip_pgs) {
1380 sbase += snpgs;
1381 snpgs = 0;
1382 continue;
1384 snpgs -= skip_pgs;
1385 sbase += skip_pgs;
1387 ASSERT(snpgs != 0);
1388 ASSERT(seg->pages_base <= sbase);
1390 * The individual pages can now be checked.
1392 for (pp = seg->pages +
1393 (sbase - seg->pages_base);
1394 snpgs != 0 && pp < seg->epages; pp++) {
1395 mqp->managed++;
1396 if (PP_ISNORELOC(pp)) {
1397 mqp->nonrelocatable++;
1398 if (!done_first_nonreloc) {
1399 mqp->
1400 first_nonrelocatable
1401 = sbase;
1402 done_first_nonreloc = 1;
1404 mqp->last_nonrelocatable =
1405 sbase;
1407 sbase++;
1408 snpgs--;
1414 free_delspans(mdsp_new);
1416 return (KPHYSM_OK);
1420 * This release function can be called at any stage as follows:
1421 * _gethandle only called
1422 * _span(s) only called
1423 * _start called but failed
1424 * delete thread exited
1427 kphysm_del_release(memhandle_t handle)
1429 struct mem_handle *mhp;
1431 mhp = kphysm_lookup_mem_handle(handle);
1432 if (mhp == NULL) {
1433 return (KPHYSM_EHANDLE);
1435 switch (mhp->mh_state) {
1436 case MHND_STARTING:
1437 case MHND_RUNNING:
1438 mutex_exit(&mhp->mh_mutex);
1439 return (KPHYSM_ENOTFINISHED);
1440 case MHND_FREE:
1441 ASSERT(mhp->mh_state != MHND_FREE);
1442 mutex_exit(&mhp->mh_mutex);
1443 return (KPHYSM_EHANDLE);
1444 case MHND_INIT:
1445 break;
1446 case MHND_DONE:
1447 break;
1448 case MHND_RELEASE:
1449 mutex_exit(&mhp->mh_mutex);
1450 return (KPHYSM_ESEQUENCE);
1451 default:
1452 #ifdef DEBUG
1453 cmn_err(CE_WARN, "kphysm_del_release(0x%p) state corrupt %d",
1454 (void *)mhp, mhp->mh_state);
1455 #endif /* DEBUG */
1456 mutex_exit(&mhp->mh_mutex);
1457 return (KPHYSM_EHANDLE);
1460 * Set state so that we can wait if necessary.
1461 * Also this means that we have read/write access to all
1462 * fields except mh_exthandle and mh_state.
1464 mhp->mh_state = MHND_RELEASE;
1466 * The mem_handle cannot be de-allocated by any other operation
1467 * now, so no need to hold mh_mutex.
1469 mutex_exit(&mhp->mh_mutex);
1471 delspan_remove(&mhp->mh_transit, 0, 0);
1472 mhp->mh_phys_pages = 0;
1473 mhp->mh_vm_pages = 0;
1474 mhp->mh_hold_todo = 0;
1475 mhp->mh_delete_complete = NULL;
1476 mhp->mh_delete_complete_arg = NULL;
1477 mhp->mh_cancel = 0;
1479 mutex_enter(&mhp->mh_mutex);
1480 ASSERT(mhp->mh_state == MHND_RELEASE);
1481 mhp->mh_state = MHND_FREE;
1483 kphysm_free_mem_handle(mhp);
1485 return (KPHYSM_OK);
1489 * This cancel function can only be called with the thread running.
1492 kphysm_del_cancel(memhandle_t handle)
1494 struct mem_handle *mhp;
1496 mhp = kphysm_lookup_mem_handle(handle);
1497 if (mhp == NULL) {
1498 return (KPHYSM_EHANDLE);
1500 if (mhp->mh_state != MHND_STARTING && mhp->mh_state != MHND_RUNNING) {
1501 mutex_exit(&mhp->mh_mutex);
1502 return (KPHYSM_ENOTRUNNING);
1505 * Set the cancel flag and wake the delete thread up.
1506 * The thread may be waiting on I/O, so the effect of the cancel
1507 * may be delayed.
1509 if (mhp->mh_cancel == 0) {
1510 mhp->mh_cancel = KPHYSM_ECANCELLED;
1511 cv_signal(&mhp->mh_cv);
1513 mutex_exit(&mhp->mh_mutex);
1514 return (KPHYSM_OK);
1518 kphysm_del_status(
1519 memhandle_t handle,
1520 memdelstat_t *mdstp)
1522 struct mem_handle *mhp;
1524 mhp = kphysm_lookup_mem_handle(handle);
1525 if (mhp == NULL) {
1526 return (KPHYSM_EHANDLE);
1529 * Calling kphysm_del_status() is allowed before the delete
1530 * is started to allow for status display.
1532 if (mhp->mh_state != MHND_INIT && mhp->mh_state != MHND_STARTING &&
1533 mhp->mh_state != MHND_RUNNING) {
1534 mutex_exit(&mhp->mh_mutex);
1535 return (KPHYSM_ENOTRUNNING);
1537 mdstp->phys_pages = mhp->mh_phys_pages;
1538 mdstp->managed = mhp->mh_vm_pages;
1539 mdstp->collected = mhp->mh_vm_pages - mhp->mh_hold_todo;
1540 mutex_exit(&mhp->mh_mutex);
1541 return (KPHYSM_OK);
1544 static int mem_delete_additional_pages = 100;
1546 static int
1547 can_remove_pgs(pgcnt_t npgs)
1550 * If all pageable pages were paged out, freemem would
1551 * equal availrmem. There is a minimum requirement for
1552 * availrmem.
1554 if ((availrmem - (tune.t_minarmem + mem_delete_additional_pages))
1555 < npgs)
1556 return (0);
1557 /* TODO: check swap space, etc. */
1558 return (1);
1561 static int
1562 get_availrmem(pgcnt_t npgs)
1564 int ret;
1566 mutex_enter(&freemem_lock);
1567 ret = can_remove_pgs(npgs);
1568 if (ret != 0)
1569 availrmem -= npgs;
1570 mutex_exit(&freemem_lock);
1571 return (ret);
1574 static void
1575 put_availrmem(pgcnt_t npgs)
1577 mutex_enter(&freemem_lock);
1578 availrmem += npgs;
1579 mutex_exit(&freemem_lock);
1582 #define FREEMEM_INCR 100
1583 static pgcnt_t freemem_incr = FREEMEM_INCR;
1584 #define DEL_FREE_WAIT_FRAC 4
1585 #define DEL_FREE_WAIT_TICKS ((hz+DEL_FREE_WAIT_FRAC-1)/DEL_FREE_WAIT_FRAC)
1587 #define DEL_BUSY_WAIT_FRAC 20
1588 #define DEL_BUSY_WAIT_TICKS ((hz+DEL_BUSY_WAIT_FRAC-1)/DEL_BUSY_WAIT_FRAC)
1590 static void kphysm_del_cleanup(struct mem_handle *);
1592 static void page_delete_collect(page_t *, struct mem_handle *);
1594 static pgcnt_t
1595 delthr_get_freemem(struct mem_handle *mhp)
1597 pgcnt_t free_get;
1598 int ret;
1600 ASSERT(MUTEX_HELD(&mhp->mh_mutex));
1602 MDSTAT_INCR(mhp, need_free);
1604 * Get up to freemem_incr pages.
1606 free_get = freemem_incr;
1607 if (free_get > mhp->mh_hold_todo)
1608 free_get = mhp->mh_hold_todo;
1610 * Take free_get pages away from freemem,
1611 * waiting if necessary.
1614 while (!mhp->mh_cancel) {
1615 mutex_exit(&mhp->mh_mutex);
1616 MDSTAT_INCR(mhp, free_loop);
1618 * Duplicate test from page_create_throttle()
1619 * but don't override with !PG_WAIT.
1621 if (freemem < (free_get + throttlefree)) {
1622 MDSTAT_INCR(mhp, free_low);
1623 ret = 0;
1624 } else {
1625 ret = page_create_wait(free_get, 0);
1626 if (ret == 0) {
1627 /* EMPTY */
1628 MDSTAT_INCR(mhp, free_failed);
1631 if (ret != 0) {
1632 mutex_enter(&mhp->mh_mutex);
1633 return (free_get);
1637 * Put pressure on pageout.
1639 page_needfree(free_get);
1640 cv_signal(&proc_pageout->p_cv);
1642 mutex_enter(&mhp->mh_mutex);
1643 (void) cv_reltimedwait(&mhp->mh_cv, &mhp->mh_mutex,
1644 DEL_FREE_WAIT_TICKS, TR_CLOCK_TICK);
1645 mutex_exit(&mhp->mh_mutex);
1646 page_needfree(-(spgcnt_t)free_get);
1648 mutex_enter(&mhp->mh_mutex);
1650 return (0);
1653 #define DR_AIO_CLEANUP_DELAY 25000 /* 0.025secs, in usec */
1654 #define DR_AIO_CLEANUP_MAXLOOPS_NODELAY 100
1656 * This function is run as a helper thread for delete_memory_thread.
1657 * It is needed in order to force kaio cleanup, so that pages used in kaio
1658 * will be unlocked and subsequently relocated by delete_memory_thread.
1659 * The address of the delete_memory_threads's mem_handle is passed in to
1660 * this thread function, and is used to set the mh_aio_cleanup_done member
1661 * prior to calling thread_exit().
1663 static void
1664 dr_aio_cleanup_thread(caddr_t amhp)
1666 proc_t *procp;
1667 int (*aio_cleanup_dr_delete_memory)(proc_t *);
1668 int cleaned;
1669 int n = 0;
1670 struct mem_handle *mhp;
1671 volatile uint_t *pcancel;
1673 mhp = (struct mem_handle *)amhp;
1674 ASSERT(mhp != NULL);
1675 pcancel = &mhp->mh_dr_aio_cleanup_cancel;
1676 if (modload("sys", "kaio") == -1) {
1677 mhp->mh_aio_cleanup_done = 1;
1678 cmn_err(CE_WARN, "dr_aio_cleanup_thread: cannot load kaio");
1679 thread_exit();
1681 aio_cleanup_dr_delete_memory = (int (*)(proc_t *))
1682 modgetsymvalue("aio_cleanup_dr_delete_memory", 0);
1683 if (aio_cleanup_dr_delete_memory == NULL) {
1684 mhp->mh_aio_cleanup_done = 1;
1685 cmn_err(CE_WARN,
1686 "aio_cleanup_dr_delete_memory not found in kaio");
1687 thread_exit();
1689 do {
1690 cleaned = 0;
1691 mutex_enter(&pidlock);
1692 for (procp = practive; (*pcancel == 0) && (procp != NULL);
1693 procp = procp->p_next) {
1694 mutex_enter(&procp->p_lock);
1695 if (procp->p_aio != NULL) {
1696 /* cleanup proc's outstanding kaio */
1697 cleaned +=
1698 (*aio_cleanup_dr_delete_memory)(procp);
1700 mutex_exit(&procp->p_lock);
1702 mutex_exit(&pidlock);
1703 if ((*pcancel == 0) &&
1704 (!cleaned || (++n == DR_AIO_CLEANUP_MAXLOOPS_NODELAY))) {
1705 /* delay a bit before retrying all procs again */
1706 delay(drv_usectohz(DR_AIO_CLEANUP_DELAY));
1707 n = 0;
1709 } while (*pcancel == 0);
1710 mhp->mh_aio_cleanup_done = 1;
1711 thread_exit();
1714 static void
1715 delete_memory_thread(caddr_t amhp)
1717 struct mem_handle *mhp;
1718 struct memdelspan *mdsp;
1719 callb_cpr_t cprinfo;
1720 page_t *pp_targ;
1721 spgcnt_t freemem_left;
1722 void (*del_complete_funcp)(void *, int error);
1723 void *del_complete_arg;
1724 int comp_code;
1725 int ret;
1726 int first_scan;
1727 uint_t szc;
1728 #ifdef MEM_DEL_STATS
1729 uint64_t start_total, ntick_total;
1730 uint64_t start_pgrp, ntick_pgrp;
1731 #endif /* MEM_DEL_STATS */
1733 mhp = (struct mem_handle *)amhp;
1735 #ifdef MEM_DEL_STATS
1736 start_total = ddi_get_lbolt();
1737 #endif /* MEM_DEL_STATS */
1739 CALLB_CPR_INIT(&cprinfo, &mhp->mh_mutex,
1740 callb_generic_cpr, "memdel");
1742 mutex_enter(&mhp->mh_mutex);
1743 ASSERT(mhp->mh_state == MHND_STARTING);
1745 mhp->mh_state = MHND_RUNNING;
1746 mhp->mh_thread_id = curthread;
1748 mhp->mh_hold_todo = mhp->mh_vm_pages;
1749 mutex_exit(&mhp->mh_mutex);
1751 /* Allocate the remap pages now, if necessary. */
1752 memseg_remap_init();
1755 * Subtract from availrmem now if possible as availrmem
1756 * may not be available by the end of the delete.
1758 if (!get_availrmem(mhp->mh_vm_pages)) {
1759 comp_code = KPHYSM_ENOTVIABLE;
1760 mutex_enter(&mhp->mh_mutex);
1761 goto early_exit;
1764 ret = kphysm_setup_pre_del(mhp->mh_vm_pages);
1766 mutex_enter(&mhp->mh_mutex);
1768 if (ret != 0) {
1769 mhp->mh_cancel = KPHYSM_EREFUSED;
1770 goto refused;
1773 transit_list_collect(mhp, 1);
1775 for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
1776 mdsp = mdsp->mds_next) {
1777 ASSERT(mdsp->mds_bitmap == NULL);
1778 mdsp->mds_bitmap = kmem_zalloc(MDS_BITMAPBYTES(mdsp), KM_SLEEP);
1779 mdsp->mds_bitmap_retired = kmem_zalloc(MDS_BITMAPBYTES(mdsp),
1780 KM_SLEEP);
1783 first_scan = 1;
1784 freemem_left = 0;
1786 * Start dr_aio_cleanup_thread, which periodically iterates
1787 * through the process list and invokes aio cleanup. This
1788 * is needed in order to avoid a deadly embrace between the
1789 * delete_memory_thread (waiting on writer lock for page, with the
1790 * exclusive-wanted bit set), kaio read request threads (waiting for a
1791 * reader lock on the same page that is wanted by the
1792 * delete_memory_thread), and threads waiting for kaio completion
1793 * (blocked on spt_amp->lock).
1795 mhp->mh_dr_aio_cleanup_cancel = 0;
1796 mhp->mh_aio_cleanup_done = 0;
1797 (void) thread_create(NULL, 0, dr_aio_cleanup_thread,
1798 (caddr_t)mhp, 0, &p0, TS_RUN, maxclsyspri - 1);
1799 while ((mhp->mh_hold_todo != 0) && (mhp->mh_cancel == 0)) {
1800 pgcnt_t collected;
1802 MDSTAT_INCR(mhp, nloop);
1803 collected = 0;
1804 for (mdsp = mhp->mh_transit.trl_spans; (mdsp != NULL) &&
1805 (mhp->mh_cancel == 0); mdsp = mdsp->mds_next) {
1806 pfn_t pfn, p_end;
1808 p_end = mdsp->mds_base + mdsp->mds_npgs;
1809 for (pfn = mdsp->mds_base; (pfn < p_end) &&
1810 (mhp->mh_cancel == 0); pfn++) {
1811 page_t *pp, *tpp, *tpp_targ;
1812 pgcnt_t bit;
1813 struct vnode *vp;
1814 uoff_t offset;
1815 int mod, result;
1816 spgcnt_t pgcnt;
1818 bit = pfn - mdsp->mds_base;
1819 if ((mdsp->mds_bitmap[bit / NBPBMW] &
1820 (1 << (bit % NBPBMW))) != 0) {
1821 MDSTAT_INCR(mhp, already_done);
1822 continue;
1824 if (freemem_left == 0) {
1825 freemem_left += delthr_get_freemem(mhp);
1826 if (freemem_left == 0)
1827 break;
1831 * Release mh_mutex - some of this
1832 * stuff takes some time (eg PUTPAGE).
1835 mutex_exit(&mhp->mh_mutex);
1836 MDSTAT_INCR(mhp, ncheck);
1838 pp = page_numtopp_nolock(pfn);
1839 if (pp == NULL) {
1841 * Not covered by a page_t - will
1842 * be dealt with elsewhere.
1844 MDSTAT_INCR(mhp, nopaget);
1845 mutex_enter(&mhp->mh_mutex);
1846 mdsp->mds_bitmap[bit / NBPBMW] |=
1847 (1 << (bit % NBPBMW));
1848 continue;
1851 if (!page_try_reclaim_lock(pp, SE_EXCL,
1852 SE_EXCL_WANTED | SE_RETIRED)) {
1854 * Page in use elsewhere. Skip it.
1856 MDSTAT_INCR(mhp, lockfail);
1857 mutex_enter(&mhp->mh_mutex);
1858 continue;
1861 * See if the cage expanded into the delete.
1862 * This can happen as we have to allow the
1863 * cage to expand.
1865 if (PP_ISNORELOC(pp)) {
1866 page_unlock(pp);
1867 mutex_enter(&mhp->mh_mutex);
1868 mhp->mh_cancel = KPHYSM_ENONRELOC;
1869 break;
1871 if (PP_RETIRED(pp)) {
1873 * Page has been retired and is
1874 * not part of the cage so we
1875 * can now do the accounting for
1876 * it.
1878 MDSTAT_INCR(mhp, retired);
1879 mutex_enter(&mhp->mh_mutex);
1880 mdsp->mds_bitmap[bit / NBPBMW]
1881 |= (1 << (bit % NBPBMW));
1882 mdsp->mds_bitmap_retired[bit /
1883 NBPBMW] |=
1884 (1 << (bit % NBPBMW));
1885 mhp->mh_hold_todo--;
1886 continue;
1888 ASSERT(freemem_left != 0);
1889 if (PP_ISFREE(pp)) {
1891 * Like page_reclaim() only 'freemem'
1892 * processing is already done.
1894 MDSTAT_INCR(mhp, nfree);
1895 free_page_collect:
1896 if (PP_ISAGED(pp)) {
1897 page_list_sub(pp,
1898 PG_FREE_LIST);
1899 } else {
1900 page_list_sub(pp,
1901 PG_CACHE_LIST);
1903 PP_CLRFREE(pp);
1904 PP_CLRAGED(pp);
1905 collected++;
1906 mutex_enter(&mhp->mh_mutex);
1907 page_delete_collect(pp, mhp);
1908 mdsp->mds_bitmap[bit / NBPBMW] |=
1909 (1 << (bit % NBPBMW));
1910 freemem_left--;
1911 continue;
1913 VERIFY(pp->p_object != NULL);
1914 ASSERT(pp->p_vnode != NULL);
1915 if (first_scan) {
1916 MDSTAT_INCR(mhp, first_notfree);
1917 page_unlock(pp);
1918 mutex_enter(&mhp->mh_mutex);
1919 continue;
1922 * Keep stats on pages encountered that
1923 * are marked for retirement.
1925 if (PP_TOXIC(pp)) {
1926 MDSTAT_INCR(mhp, toxic);
1927 } else if (PP_PR_REQ(pp)) {
1928 MDSTAT_INCR(mhp, failing);
1931 * In certain cases below, special exceptions
1932 * are made for pages that are toxic. This
1933 * is because the current meaning of toxic
1934 * is that an uncorrectable error has been
1935 * previously associated with the page.
1937 if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
1938 if (!PP_TOXIC(pp)) {
1940 * Must relocate locked in
1941 * memory pages.
1943 #ifdef MEM_DEL_STATS
1944 start_pgrp = ddi_get_lbolt();
1945 #endif /* MEM_DEL_STATS */
1947 * Lock all constituent pages
1948 * of a large page to ensure
1949 * that p_szc won't change.
1951 if (!group_page_trylock(pp,
1952 SE_EXCL)) {
1953 MDSTAT_INCR(mhp,
1954 gptllckfail);
1955 page_unlock(pp);
1956 mutex_enter(
1957 &mhp->mh_mutex);
1958 continue;
1960 MDSTAT_INCR(mhp, npplocked);
1961 pp_targ =
1962 page_get_replacement_page(
1963 pp, NULL, 0);
1964 if (pp_targ != NULL) {
1965 #ifdef MEM_DEL_STATS
1966 ntick_pgrp =
1967 (uint64_t)
1968 ddi_get_lbolt() -
1969 start_pgrp;
1970 #endif /* MEM_DEL_STATS */
1971 MDSTAT_PGRP(mhp,
1972 ntick_pgrp);
1973 MDSTAT_INCR(mhp,
1974 nlockreloc);
1975 goto reloc;
1977 group_page_unlock(pp);
1978 page_unlock(pp);
1979 #ifdef MEM_DEL_STATS
1980 ntick_pgrp =
1981 (uint64_t)ddi_get_lbolt() -
1982 start_pgrp;
1983 #endif /* MEM_DEL_STATS */
1984 MDSTAT_PGRP(mhp, ntick_pgrp);
1985 MDSTAT_INCR(mhp, nnorepl);
1986 mutex_enter(&mhp->mh_mutex);
1987 continue;
1988 } else {
1990 * Cannot do anything about
1991 * this page because it is
1992 * toxic.
1994 MDSTAT_INCR(mhp, npplkdtoxic);
1995 page_unlock(pp);
1996 mutex_enter(&mhp->mh_mutex);
1997 continue;
2001 * Unload the mappings and check if mod bit
2002 * is set.
2004 ASSERT(!PP_ISKAS(pp));
2005 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
2006 mod = hat_ismod(pp);
2008 #ifdef MEM_DEL_STATS
2009 start_pgrp = ddi_get_lbolt();
2010 #endif /* MEM_DEL_STATS */
2011 if (mod && !PP_TOXIC(pp)) {
2013 * Lock all constituent pages
2014 * of a large page to ensure
2015 * that p_szc won't change.
2017 if (!group_page_trylock(pp, SE_EXCL)) {
2018 MDSTAT_INCR(mhp, gptlmodfail);
2019 page_unlock(pp);
2020 mutex_enter(&mhp->mh_mutex);
2021 continue;
2023 pp_targ = page_get_replacement_page(pp,
2024 NULL, 0);
2025 if (pp_targ != NULL) {
2026 MDSTAT_INCR(mhp, nmodreloc);
2027 #ifdef MEM_DEL_STATS
2028 ntick_pgrp =
2029 (uint64_t)ddi_get_lbolt() -
2030 start_pgrp;
2031 #endif /* MEM_DEL_STATS */
2032 MDSTAT_PGRP(mhp, ntick_pgrp);
2033 goto reloc;
2035 group_page_unlock(pp);
2038 if (!page_try_demote_pages(pp)) {
2039 MDSTAT_INCR(mhp, demotefail);
2040 page_unlock(pp);
2041 #ifdef MEM_DEL_STATS
2042 ntick_pgrp = (uint64_t)ddi_get_lbolt() -
2043 start_pgrp;
2044 #endif /* MEM_DEL_STATS */
2045 MDSTAT_PGRP(mhp, ntick_pgrp);
2046 mutex_enter(&mhp->mh_mutex);
2047 continue;
2051 * Regular 'page-out'.
2053 if (!mod) {
2054 MDSTAT_INCR(mhp, ndestroy);
2055 page_destroy(pp, 1);
2057 * page_destroy was called with
2058 * dontfree. As long as p_lckcnt
2059 * and p_cowcnt are both zero, the
2060 * only additional action of
2061 * page_destroy with !dontfree is to
2062 * call page_free, so we can collect
2063 * the page here.
2065 collected++;
2066 #ifdef MEM_DEL_STATS
2067 ntick_pgrp = (uint64_t)ddi_get_lbolt() -
2068 start_pgrp;
2069 #endif /* MEM_DEL_STATS */
2070 MDSTAT_PGRP(mhp, ntick_pgrp);
2071 mutex_enter(&mhp->mh_mutex);
2072 page_delete_collect(pp, mhp);
2073 mdsp->mds_bitmap[bit / NBPBMW] |=
2074 (1 << (bit % NBPBMW));
2075 continue;
2078 * The page is toxic and the mod bit is
2079 * set, we cannot do anything here to deal
2080 * with it.
2082 if (PP_TOXIC(pp)) {
2083 page_unlock(pp);
2084 #ifdef MEM_DEL_STATS
2085 ntick_pgrp = (uint64_t)ddi_get_lbolt() -
2086 start_pgrp;
2087 #endif /* MEM_DEL_STATS */
2088 MDSTAT_PGRP(mhp, ntick_pgrp);
2089 MDSTAT_INCR(mhp, modtoxic);
2090 mutex_enter(&mhp->mh_mutex);
2091 continue;
2093 MDSTAT_INCR(mhp, nputpage);
2094 vp = pp->p_vnode;
2095 offset = pp->p_offset;
2096 VN_HOLD(vp);
2097 page_unlock(pp);
2098 (void) fop_putpage(vp, offset, PAGESIZE,
2099 B_INVAL|B_FORCE, kcred, NULL);
2100 VN_RELE(vp);
2101 #ifdef MEM_DEL_STATS
2102 ntick_pgrp = (uint64_t)ddi_get_lbolt() -
2103 start_pgrp;
2104 #endif /* MEM_DEL_STATS */
2105 MDSTAT_PGRP(mhp, ntick_pgrp);
2107 * Try to get the page back immediately
2108 * so that it can be collected.
2110 pp = page_numtopp_nolock(pfn);
2111 if (pp == NULL) {
2112 MDSTAT_INCR(mhp, nnoreclaim);
2114 * This should not happen as this
2115 * thread is deleting the page.
2116 * If this code is generalized, this
2117 * becomes a reality.
2119 #ifdef DEBUG
2120 cmn_err(CE_WARN,
2121 "delete_memory_thread(0x%p) "
2122 "pfn 0x%lx has no page_t",
2123 (void *)mhp, pfn);
2124 #endif /* DEBUG */
2125 mutex_enter(&mhp->mh_mutex);
2126 continue;
2128 if (page_try_reclaim_lock(pp, SE_EXCL,
2129 SE_EXCL_WANTED | SE_RETIRED)) {
2130 if (PP_ISFREE(pp)) {
2131 goto free_page_collect;
2133 page_unlock(pp);
2135 MDSTAT_INCR(mhp, nnoreclaim);
2136 mutex_enter(&mhp->mh_mutex);
2137 continue;
2139 reloc:
2141 * Got some freemem and a target
2142 * page, so move the data to avoid
2143 * I/O and lock problems.
2145 ASSERT(!page_iolock_assert(pp));
2146 MDSTAT_INCR(mhp, nreloc);
2148 * page_relocate() will return pgcnt: the
2149 * number of consecutive pages relocated.
2150 * If it is successful, pp will be a
2151 * linked list of the page structs that
2152 * were relocated. If page_relocate() is
2153 * unsuccessful, pp will be unmodified.
2155 #ifdef MEM_DEL_STATS
2156 start_pgrp = ddi_get_lbolt();
2157 #endif /* MEM_DEL_STATS */
2158 result = page_relocate(&pp, &pp_targ, 0, 0,
2159 &pgcnt, NULL);
2160 #ifdef MEM_DEL_STATS
2161 ntick_pgrp = (uint64_t)ddi_get_lbolt() -
2162 start_pgrp;
2163 #endif /* MEM_DEL_STATS */
2164 MDSTAT_PGRP(mhp, ntick_pgrp);
2165 if (result != 0) {
2166 MDSTAT_INCR(mhp, nrelocfail);
2168 * We did not succeed. We need
2169 * to give the pp_targ pages back.
2170 * page_free(pp_targ, 1) without
2171 * the freemem accounting.
2173 group_page_unlock(pp);
2174 page_free_replacement_page(pp_targ);
2175 page_unlock(pp);
2176 mutex_enter(&mhp->mh_mutex);
2177 continue;
2181 * We will then collect pgcnt pages.
2183 ASSERT(pgcnt > 0);
2184 mutex_enter(&mhp->mh_mutex);
2186 * We need to make sure freemem_left is
2187 * large enough.
2189 while ((freemem_left < pgcnt) &&
2190 (!mhp->mh_cancel)) {
2191 freemem_left +=
2192 delthr_get_freemem(mhp);
2196 * Do not proceed if mh_cancel is set.
2198 if (mhp->mh_cancel) {
2199 while (pp_targ != NULL) {
2201 * Unlink and unlock each page.
2203 tpp_targ = pp_targ;
2204 page_sub(&pp_targ, tpp_targ);
2205 page_unlock(tpp_targ);
2208 * We need to give the pp pages back.
2209 * page_free(pp, 1) without the
2210 * freemem accounting.
2212 page_free_replacement_page(pp);
2213 break;
2216 /* Now remove pgcnt from freemem_left */
2217 freemem_left -= pgcnt;
2218 ASSERT(freemem_left >= 0);
2219 szc = pp->p_szc;
2220 while (pp != NULL) {
2222 * pp and pp_targ were passed back as
2223 * a linked list of pages.
2224 * Unlink and unlock each page.
2226 tpp_targ = pp_targ;
2227 page_sub(&pp_targ, tpp_targ);
2228 page_unlock(tpp_targ);
2230 * The original page is now free
2231 * so remove it from the linked
2232 * list and collect it.
2234 tpp = pp;
2235 page_sub(&pp, tpp);
2236 pfn = page_pptonum(tpp);
2237 collected++;
2238 ASSERT(PAGE_EXCL(tpp));
2239 VERIFY(tpp->p_object == NULL);
2240 ASSERT(tpp->p_vnode == NULL);
2241 ASSERT(!hat_page_is_mapped(tpp));
2242 ASSERT(tpp->p_szc == szc);
2243 tpp->p_szc = 0;
2244 page_delete_collect(tpp, mhp);
2245 bit = pfn - mdsp->mds_base;
2246 mdsp->mds_bitmap[bit / NBPBMW] |=
2247 (1 << (bit % NBPBMW));
2249 ASSERT(pp_targ == NULL);
2252 first_scan = 0;
2253 if ((mhp->mh_cancel == 0) && (mhp->mh_hold_todo != 0) &&
2254 (collected == 0)) {
2256 * This code is needed as we cannot wait
2257 * for a page to be locked OR the delete to
2258 * be cancelled. Also, we must delay so
2259 * that other threads get a chance to run
2260 * on our cpu, otherwise page locks may be
2261 * held indefinitely by those threads.
2263 MDSTAT_INCR(mhp, ndelay);
2264 CALLB_CPR_SAFE_BEGIN(&cprinfo);
2265 (void) cv_reltimedwait(&mhp->mh_cv, &mhp->mh_mutex,
2266 DEL_BUSY_WAIT_TICKS, TR_CLOCK_TICK);
2267 CALLB_CPR_SAFE_END(&cprinfo, &mhp->mh_mutex);
2270 /* stop the dr aio cleanup thread */
2271 mhp->mh_dr_aio_cleanup_cancel = 1;
2272 transit_list_collect(mhp, 0);
2273 if (freemem_left != 0) {
2274 /* Return any surplus. */
2275 page_create_putback(freemem_left);
2276 freemem_left = 0;
2278 #ifdef MEM_DEL_STATS
2279 ntick_total = (uint64_t)ddi_get_lbolt() - start_total;
2280 #endif /* MEM_DEL_STATS */
2281 MDSTAT_TOTAL(mhp, ntick_total);
2282 MDSTAT_PRINT(mhp);
2285 * If the memory delete was cancelled, exclusive-wanted bits must
2286 * be cleared. If there are retired pages being deleted, they need
2287 * to be unretired.
2289 for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
2290 mdsp = mdsp->mds_next) {
2291 pfn_t pfn, p_end;
2293 p_end = mdsp->mds_base + mdsp->mds_npgs;
2294 for (pfn = mdsp->mds_base; pfn < p_end; pfn++) {
2295 page_t *pp;
2296 pgcnt_t bit;
2298 bit = pfn - mdsp->mds_base;
2299 if (mhp->mh_cancel) {
2300 pp = page_numtopp_nolock(pfn);
2301 if (pp != NULL) {
2302 if ((mdsp->mds_bitmap[bit / NBPBMW] &
2303 (1 << (bit % NBPBMW))) == 0) {
2304 page_lock_clr_exclwanted(pp);
2307 } else {
2308 pp = NULL;
2310 if ((mdsp->mds_bitmap_retired[bit / NBPBMW] &
2311 (1 << (bit % NBPBMW))) != 0) {
2312 /* do we already have pp? */
2313 if (pp == NULL) {
2314 pp = page_numtopp_nolock(pfn);
2316 ASSERT(pp != NULL);
2317 ASSERT(PP_RETIRED(pp));
2318 if (mhp->mh_cancel != 0) {
2319 page_unlock(pp);
2321 * To satisfy ASSERT below in
2322 * cancel code.
2324 mhp->mh_hold_todo++;
2325 } else {
2326 (void) page_unretire_pp(pp,
2327 PR_UNR_CLEAN);
2333 * Free retired page bitmap and collected page bitmap
2335 for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
2336 mdsp = mdsp->mds_next) {
2337 ASSERT(mdsp->mds_bitmap_retired != NULL);
2338 kmem_free(mdsp->mds_bitmap_retired, MDS_BITMAPBYTES(mdsp));
2339 mdsp->mds_bitmap_retired = NULL; /* Paranoia. */
2340 ASSERT(mdsp->mds_bitmap != NULL);
2341 kmem_free(mdsp->mds_bitmap, MDS_BITMAPBYTES(mdsp));
2342 mdsp->mds_bitmap = NULL; /* Paranoia. */
2345 /* wait for our dr aio cancel thread to exit */
2346 while (!(mhp->mh_aio_cleanup_done)) {
2347 CALLB_CPR_SAFE_BEGIN(&cprinfo);
2348 delay(drv_usectohz(DR_AIO_CLEANUP_DELAY));
2349 CALLB_CPR_SAFE_END(&cprinfo, &mhp->mh_mutex);
2351 refused:
2352 if (mhp->mh_cancel != 0) {
2353 page_t *pp;
2355 comp_code = mhp->mh_cancel;
2357 * Go through list of deleted pages (mh_deleted) freeing
2358 * them.
2360 while ((pp = mhp->mh_deleted) != NULL) {
2361 mhp->mh_deleted = pp->p_next;
2362 mhp->mh_hold_todo++;
2363 mutex_exit(&mhp->mh_mutex);
2364 /* Restore p_next. */
2365 pp->p_next = pp->p_prev;
2366 if (PP_ISFREE(pp)) {
2367 cmn_err(CE_PANIC,
2368 "page %p is free",
2369 (void *)pp);
2371 page_free(pp, 1);
2372 mutex_enter(&mhp->mh_mutex);
2374 ASSERT(mhp->mh_hold_todo == mhp->mh_vm_pages);
2376 mutex_exit(&mhp->mh_mutex);
2377 put_availrmem(mhp->mh_vm_pages);
2378 mutex_enter(&mhp->mh_mutex);
2380 goto t_exit;
2384 * All the pages are no longer in use and are exclusively locked.
2387 mhp->mh_deleted = NULL;
2389 kphysm_del_cleanup(mhp);
2392 * mem_node_del_range needs to be after kphysm_del_cleanup so
2393 * that the mem_node_config[] will remain intact for the cleanup.
2395 for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
2396 mdsp = mdsp->mds_next) {
2397 mem_node_del_range(mdsp->mds_base,
2398 mdsp->mds_base + mdsp->mds_npgs - 1);
2400 /* cleanup the page counters */
2401 page_ctrs_cleanup();
2403 comp_code = KPHYSM_OK;
2405 t_exit:
2406 mutex_exit(&mhp->mh_mutex);
2407 kphysm_setup_post_del(mhp->mh_vm_pages,
2408 (comp_code == KPHYSM_OK) ? 0 : 1);
2409 mutex_enter(&mhp->mh_mutex);
2411 early_exit:
2412 /* mhp->mh_mutex exited by CALLB_CPR_EXIT() */
2413 mhp->mh_state = MHND_DONE;
2414 del_complete_funcp = mhp->mh_delete_complete;
2415 del_complete_arg = mhp->mh_delete_complete_arg;
2416 CALLB_CPR_EXIT(&cprinfo);
2417 (*del_complete_funcp)(del_complete_arg, comp_code);
2418 thread_exit();
2419 /*NOTREACHED*/
2423 * Start the delete of the memory from the system.
2426 kphysm_del_start(
2427 memhandle_t handle,
2428 void (*complete)(void *, int),
2429 void *complete_arg)
2431 struct mem_handle *mhp;
2433 mhp = kphysm_lookup_mem_handle(handle);
2434 if (mhp == NULL) {
2435 return (KPHYSM_EHANDLE);
2437 switch (mhp->mh_state) {
2438 case MHND_FREE:
2439 ASSERT(mhp->mh_state != MHND_FREE);
2440 mutex_exit(&mhp->mh_mutex);
2441 return (KPHYSM_EHANDLE);
2442 case MHND_INIT:
2443 break;
2444 case MHND_STARTING:
2445 case MHND_RUNNING:
2446 mutex_exit(&mhp->mh_mutex);
2447 return (KPHYSM_ESEQUENCE);
2448 case MHND_DONE:
2449 mutex_exit(&mhp->mh_mutex);
2450 return (KPHYSM_ESEQUENCE);
2451 case MHND_RELEASE:
2452 mutex_exit(&mhp->mh_mutex);
2453 return (KPHYSM_ESEQUENCE);
2454 default:
2455 #ifdef DEBUG
2456 cmn_err(CE_WARN, "kphysm_del_start(0x%p) state corrupt %d",
2457 (void *)mhp, mhp->mh_state);
2458 #endif /* DEBUG */
2459 mutex_exit(&mhp->mh_mutex);
2460 return (KPHYSM_EHANDLE);
2463 if (mhp->mh_transit.trl_spans == NULL) {
2464 mutex_exit(&mhp->mh_mutex);
2465 return (KPHYSM_ENOWORK);
2468 ASSERT(complete != NULL);
2469 mhp->mh_delete_complete = complete;
2470 mhp->mh_delete_complete_arg = complete_arg;
2471 mhp->mh_state = MHND_STARTING;
2473 * Release the mutex in case thread_create sleeps.
2475 mutex_exit(&mhp->mh_mutex);
2478 * The "obvious" process for this thread is pageout (proc_pageout)
2479 * but this gives the thread too much power over freemem
2480 * which results in freemem starvation.
2482 (void) thread_create(NULL, 0, delete_memory_thread, mhp, 0, &p0,
2483 TS_RUN, maxclsyspri - 1);
2485 return (KPHYSM_OK);
2488 static kmutex_t pp_dummy_lock; /* Protects init. of pp_dummy. */
2489 static caddr_t pp_dummy;
2490 static pgcnt_t pp_dummy_npages;
2491 static pfn_t *pp_dummy_pfn; /* Array of dummy pfns. */
2493 static void
2494 memseg_remap_init_pages(page_t *pages, page_t *epages)
2496 page_t *pp;
2498 for (pp = pages; pp < epages; pp++) {
2499 pp->p_pagenum = PFN_INVALID; /* XXXX */
2500 pp->p_offset = (uoff_t)-1;
2501 page_iolock_init(pp);
2502 while (!page_lock(pp, SE_EXCL, NULL, P_RECLAIM))
2503 continue;
2504 page_lock_delete(pp);
2508 void
2509 memseg_remap_init()
2511 mutex_enter(&pp_dummy_lock);
2512 if (pp_dummy == NULL) {
2513 uint_t dpages;
2514 int i;
2517 * dpages starts off as the size of the structure and
2518 * ends up as the minimum number of pages that will
2519 * hold a whole number of page_t structures.
2521 dpages = sizeof (page_t);
2522 ASSERT(dpages != 0);
2523 ASSERT(dpages <= MMU_PAGESIZE);
2525 while ((dpages & 1) == 0)
2526 dpages >>= 1;
2528 pp_dummy_npages = dpages;
2530 * Allocate pp_dummy pages directly from static_arena,
2531 * since these are whole page allocations and are
2532 * referenced by physical address. This also has the
2533 * nice fringe benefit of hiding the memory from
2534 * ::findleaks since it doesn't deal well with allocated
2535 * kernel heap memory that doesn't have any mappings.
2537 pp_dummy = vmem_xalloc(static_arena, ptob(pp_dummy_npages),
2538 PAGESIZE, 0, 0, NULL, NULL, VM_SLEEP);
2539 bzero(pp_dummy, ptob(pp_dummy_npages));
2540 ASSERT(((uintptr_t)pp_dummy & MMU_PAGEOFFSET) == 0);
2541 pp_dummy_pfn = kmem_alloc(sizeof (*pp_dummy_pfn) *
2542 pp_dummy_npages, KM_SLEEP);
2543 for (i = 0; i < pp_dummy_npages; i++) {
2544 pp_dummy_pfn[i] = hat_getpfnum(kas.a_hat,
2545 &pp_dummy[MMU_PAGESIZE * i]);
2546 ASSERT(pp_dummy_pfn[i] != PFN_INVALID);
2549 * Initialize the page_t's to a known 'deleted' state
2550 * that matches the state of deleted pages.
2552 memseg_remap_init_pages((page_t *)pp_dummy,
2553 (page_t *)(pp_dummy + ptob(pp_dummy_npages)));
2554 /* Remove kmem mappings for the pages for safety. */
2555 hat_unload(kas.a_hat, pp_dummy, ptob(pp_dummy_npages),
2556 HAT_UNLOAD_UNLOCK);
2557 /* Leave pp_dummy pointer set as flag that init is done. */
2559 mutex_exit(&pp_dummy_lock);
2563 * Remap a page-aglined range of page_t's to dummy pages.
2565 void
2566 remap_to_dummy(caddr_t va, pgcnt_t metapgs)
2568 int phase;
2570 ASSERT(IS_P2ALIGNED((uint64_t)(uintptr_t)va, PAGESIZE));
2573 * We may start remapping at a non-zero page offset
2574 * within the dummy pages since the low/high ends
2575 * of the outgoing pp's could be shared by other
2576 * memsegs (see memseg_remap_meta).
2578 phase = btop((uint64_t)(uintptr_t)va) % pp_dummy_npages;
2579 /*CONSTCOND*/
2580 ASSERT(PAGESIZE % sizeof (page_t) || phase == 0);
2582 while (metapgs != 0) {
2583 pgcnt_t n;
2584 int i, j;
2586 n = pp_dummy_npages;
2587 if (n > metapgs)
2588 n = metapgs;
2589 for (i = 0; i < n; i++) {
2590 j = (i + phase) % pp_dummy_npages;
2591 hat_devload(kas.a_hat, va, ptob(1), pp_dummy_pfn[j],
2592 PROT_READ,
2593 HAT_LOAD | HAT_LOAD_NOCONSIST |
2594 HAT_LOAD_REMAP);
2595 va += ptob(1);
2597 metapgs -= n;
2601 static void
2602 memseg_remap_to_dummy(struct memseg *seg)
2604 caddr_t pp;
2605 pgcnt_t metapgs;
2607 ASSERT(memseg_is_dynamic(seg));
2608 ASSERT(pp_dummy != NULL);
2611 if (!memseg_includes_meta(seg)) {
2612 memseg_remap_meta(seg);
2613 return;
2616 pp = (caddr_t)seg->pages;
2617 metapgs = seg->pages_base - memseg_get_start(seg);
2618 ASSERT(metapgs != 0);
2620 seg->pages_end = seg->pages_base;
2622 remap_to_dummy(pp, metapgs);
2626 * Transition all the deleted pages to the deleted state so that
2627 * page_lock will not wait. The page_lock_delete call will
2628 * also wake up any waiters.
2630 static void
2631 memseg_lock_delete_all(struct memseg *seg)
2633 page_t *pp;
2635 for (pp = seg->pages; pp < seg->epages; pp++) {
2636 pp->p_pagenum = PFN_INVALID; /* XXXX */
2637 page_lock_delete(pp);
2641 static void
2642 kphysm_del_cleanup(struct mem_handle *mhp)
2644 struct memdelspan *mdsp;
2645 struct memseg *seg;
2646 struct memseg **segpp;
2647 struct memseg *seglist;
2648 pfn_t p_end;
2649 uint64_t avmem;
2650 pgcnt_t avpgs;
2651 pgcnt_t npgs;
2653 avpgs = mhp->mh_vm_pages;
2655 memsegs_lock(1);
2658 * remove from main segment list.
2660 npgs = 0;
2661 seglist = NULL;
2662 for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
2663 mdsp = mdsp->mds_next) {
2664 p_end = mdsp->mds_base + mdsp->mds_npgs;
2665 for (segpp = &memsegs; (seg = *segpp) != NULL; ) {
2666 if (seg->pages_base >= p_end ||
2667 seg->pages_end <= mdsp->mds_base) {
2668 /* Span and memseg don't overlap. */
2669 segpp = &((*segpp)->next);
2670 continue;
2672 ASSERT(seg->pages_base >= mdsp->mds_base);
2673 ASSERT(seg->pages_end <= p_end);
2675 PLCNT_MODIFY_MAX(seg->pages_base,
2676 seg->pages_base - seg->pages_end);
2678 /* Hide the memseg from future scans. */
2679 hat_kpm_delmem_mseg_update(seg, segpp);
2680 *segpp = seg->next;
2681 membar_producer(); /* TODO: Needed? */
2682 npgs += MSEG_NPAGES(seg);
2685 * Leave the deleted segment's next pointer intact
2686 * in case a memsegs scanning loop is walking this
2687 * segment concurrently.
2689 seg->lnext = seglist;
2690 seglist = seg;
2694 build_pfn_hash();
2696 ASSERT(npgs < total_pages);
2697 total_pages -= npgs;
2700 * Recalculate the paging parameters now total_pages has changed.
2701 * This will also cause the clock hands to be reset before next use.
2703 setupclock(1);
2705 memsegs_unlock(1);
2707 mutex_exit(&mhp->mh_mutex);
2709 while ((seg = seglist) != NULL) {
2710 pfn_t mseg_start;
2711 pfn_t mseg_base, mseg_end;
2712 pgcnt_t mseg_npgs;
2713 int mlret;
2715 seglist = seg->lnext;
2718 * Put the page_t's into the deleted state to stop
2719 * cv_wait()s on the pages. When we remap, the dummy
2720 * page_t's will be in the same state.
2722 memseg_lock_delete_all(seg);
2724 * Collect up information based on pages_base and pages_end
2725 * early so that we can flag early that the memseg has been
2726 * deleted by setting pages_end == pages_base.
2728 mseg_base = seg->pages_base;
2729 mseg_end = seg->pages_end;
2730 mseg_npgs = MSEG_NPAGES(seg);
2731 mseg_start = memseg_get_start(seg);
2733 if (memseg_is_dynamic(seg)) {
2734 /* Remap the meta data to our special dummy area. */
2735 memseg_remap_to_dummy(seg);
2737 mutex_enter(&memseg_lists_lock);
2738 seg->lnext = memseg_va_avail;
2739 memseg_va_avail = seg;
2740 mutex_exit(&memseg_lists_lock);
2741 } else {
2743 * For memory whose page_ts were allocated
2744 * at boot, we need to find a new use for
2745 * the page_t memory.
2746 * For the moment, just leak it.
2747 * (It is held in the memseg_delete_junk list.)
2749 seg->pages_end = seg->pages_base;
2751 mutex_enter(&memseg_lists_lock);
2752 seg->lnext = memseg_delete_junk;
2753 memseg_delete_junk = seg;
2754 mutex_exit(&memseg_lists_lock);
2757 /* Must not use seg now as it could be re-used. */
2759 memlist_write_lock();
2761 mlret = memlist_delete_span(
2762 (uint64_t)(mseg_base) << PAGESHIFT,
2763 (uint64_t)(mseg_npgs) << PAGESHIFT,
2764 &phys_avail);
2765 ASSERT(mlret == MEML_SPANOP_OK);
2767 mlret = memlist_delete_span(
2768 (uint64_t)(mseg_start) << PAGESHIFT,
2769 (uint64_t)(mseg_end - mseg_start) <<
2770 PAGESHIFT,
2771 &phys_install);
2772 ASSERT(mlret == MEML_SPANOP_OK);
2773 phys_install_has_changed();
2775 memlist_write_unlock();
2778 memlist_read_lock();
2779 installed_top_size(phys_install, &physmax, &physinstalled);
2780 memlist_read_unlock();
2782 mutex_enter(&freemem_lock);
2783 maxmem -= avpgs;
2784 physmem -= avpgs;
2785 /* availrmem is adjusted during the delete. */
2786 availrmem_initial -= avpgs;
2788 mutex_exit(&freemem_lock);
2790 dump_resize();
2792 cmn_err(CE_CONT, "?kphysm_delete: mem = %ldK "
2793 "(0x%" PRIx64 ")\n",
2794 physinstalled << (PAGESHIFT - 10),
2795 (uint64_t)physinstalled << PAGESHIFT);
2797 avmem = (uint64_t)freemem << PAGESHIFT;
2798 cmn_err(CE_CONT, "?kphysm_delete: "
2799 "avail mem = %" PRId64 "\n", avmem);
2802 * Update lgroup generation number on single lgroup systems
2804 if (nlgrps == 1)
2805 lgrp_config(LGRP_CONFIG_GEN_UPDATE, 0, 0);
2807 /* Successfully deleted system memory */
2808 mutex_enter(&mhp->mh_mutex);
2811 static uint_t mdel_nullvp_waiter;
2813 static void
2814 page_delete_collect(
2815 page_t *pp,
2816 struct mem_handle *mhp)
2818 if (pp->p_vnode) {
2819 page_hashout(pp, false);
2820 /* do not do PP_SETAGED(pp); */
2821 } else {
2822 kmutex_t *sep;
2824 sep = page_se_mutex(pp);
2825 mutex_enter(sep);
2826 if (CV_HAS_WAITERS(&pp->p_cv)) {
2827 mdel_nullvp_waiter++;
2828 cv_broadcast(&pp->p_cv);
2830 mutex_exit(sep);
2832 ASSERT(pp->p_next == pp->p_prev);
2833 ASSERT(pp->p_next == NULL || pp->p_next == pp);
2834 pp->p_next = mhp->mh_deleted;
2835 mhp->mh_deleted = pp;
2836 ASSERT(mhp->mh_hold_todo != 0);
2837 mhp->mh_hold_todo--;
2840 static void
2841 transit_list_collect(struct mem_handle *mhp, int v)
2843 struct transit_list_head *trh;
2845 trh = &transit_list_head;
2846 mutex_enter(&trh->trh_lock);
2847 mhp->mh_transit.trl_collect = v;
2848 mutex_exit(&trh->trh_lock);
2851 static void
2852 transit_list_insert(struct transit_list *tlp)
2854 struct transit_list_head *trh;
2856 trh = &transit_list_head;
2857 ASSERT(MUTEX_HELD(&trh->trh_lock));
2858 tlp->trl_next = trh->trh_head;
2859 trh->trh_head = tlp;
2862 static void
2863 transit_list_remove(struct transit_list *tlp)
2865 struct transit_list_head *trh;
2866 struct transit_list **tlpp;
2868 trh = &transit_list_head;
2869 tlpp = &trh->trh_head;
2870 ASSERT(MUTEX_HELD(&trh->trh_lock));
2871 while (*tlpp != NULL && *tlpp != tlp)
2872 tlpp = &(*tlpp)->trl_next;
2873 ASSERT(*tlpp != NULL);
2874 if (*tlpp == tlp)
2875 *tlpp = tlp->trl_next;
2876 tlp->trl_next = NULL;
2879 static struct transit_list *
2880 pfnum_to_transit_list(struct transit_list_head *trh, pfn_t pfnum)
2882 struct transit_list *tlp;
2884 for (tlp = trh->trh_head; tlp != NULL; tlp = tlp->trl_next) {
2885 struct memdelspan *mdsp;
2887 for (mdsp = tlp->trl_spans; mdsp != NULL;
2888 mdsp = mdsp->mds_next) {
2889 if (pfnum >= mdsp->mds_base &&
2890 pfnum < (mdsp->mds_base + mdsp->mds_npgs)) {
2891 return (tlp);
2895 return (NULL);
2899 pfn_is_being_deleted(pfn_t pfnum)
2901 struct transit_list_head *trh;
2902 struct transit_list *tlp;
2903 int ret;
2905 trh = &transit_list_head;
2906 if (trh->trh_head == NULL)
2907 return (0);
2909 mutex_enter(&trh->trh_lock);
2910 tlp = pfnum_to_transit_list(trh, pfnum);
2911 ret = (tlp != NULL && tlp->trl_collect);
2912 mutex_exit(&trh->trh_lock);
2914 return (ret);
2917 #ifdef MEM_DEL_STATS
2918 extern int hz;
2919 static void
2920 mem_del_stat_print_func(struct mem_handle *mhp)
2922 uint64_t tmp;
2924 if (mem_del_stat_print) {
2925 printf("memory delete loop %x/%x, statistics%s\n",
2926 (uint_t)mhp->mh_transit.trl_spans->mds_base,
2927 (uint_t)mhp->mh_transit.trl_spans->mds_npgs,
2928 (mhp->mh_cancel ? " (cancelled)" : ""));
2929 printf("\t%8u nloop\n", mhp->mh_delstat.nloop);
2930 printf("\t%8u need_free\n", mhp->mh_delstat.need_free);
2931 printf("\t%8u free_loop\n", mhp->mh_delstat.free_loop);
2932 printf("\t%8u free_low\n", mhp->mh_delstat.free_low);
2933 printf("\t%8u free_failed\n", mhp->mh_delstat.free_failed);
2934 printf("\t%8u ncheck\n", mhp->mh_delstat.ncheck);
2935 printf("\t%8u nopaget\n", mhp->mh_delstat.nopaget);
2936 printf("\t%8u lockfail\n", mhp->mh_delstat.lockfail);
2937 printf("\t%8u nfree\n", mhp->mh_delstat.nfree);
2938 printf("\t%8u nreloc\n", mhp->mh_delstat.nreloc);
2939 printf("\t%8u nrelocfail\n", mhp->mh_delstat.nrelocfail);
2940 printf("\t%8u already_done\n", mhp->mh_delstat.already_done);
2941 printf("\t%8u first_notfree\n", mhp->mh_delstat.first_notfree);
2942 printf("\t%8u npplocked\n", mhp->mh_delstat.npplocked);
2943 printf("\t%8u nlockreloc\n", mhp->mh_delstat.nlockreloc);
2944 printf("\t%8u nnorepl\n", mhp->mh_delstat.nnorepl);
2945 printf("\t%8u nmodreloc\n", mhp->mh_delstat.nmodreloc);
2946 printf("\t%8u ndestroy\n", mhp->mh_delstat.ndestroy);
2947 printf("\t%8u nputpage\n", mhp->mh_delstat.nputpage);
2948 printf("\t%8u nnoreclaim\n", mhp->mh_delstat.nnoreclaim);
2949 printf("\t%8u ndelay\n", mhp->mh_delstat.ndelay);
2950 printf("\t%8u demotefail\n", mhp->mh_delstat.demotefail);
2951 printf("\t%8u retired\n", mhp->mh_delstat.retired);
2952 printf("\t%8u toxic\n", mhp->mh_delstat.toxic);
2953 printf("\t%8u failing\n", mhp->mh_delstat.failing);
2954 printf("\t%8u modtoxic\n", mhp->mh_delstat.modtoxic);
2955 printf("\t%8u npplkdtoxic\n", mhp->mh_delstat.npplkdtoxic);
2956 printf("\t%8u gptlmodfail\n", mhp->mh_delstat.gptlmodfail);
2957 printf("\t%8u gptllckfail\n", mhp->mh_delstat.gptllckfail);
2958 tmp = mhp->mh_delstat.nticks_total / hz; /* seconds */
2959 printf(
2960 "\t%"PRIu64" nticks_total - %"PRIu64" min %"PRIu64" sec\n",
2961 mhp->mh_delstat.nticks_total, tmp / 60, tmp % 60);
2963 tmp = mhp->mh_delstat.nticks_pgrp / hz; /* seconds */
2964 printf(
2965 "\t%"PRIu64" nticks_pgrp - %"PRIu64" min %"PRIu64" sec\n",
2966 mhp->mh_delstat.nticks_pgrp, tmp / 60, tmp % 60);
2969 #endif /* MEM_DEL_STATS */
2971 struct mem_callback {
2972 kphysm_setup_vector_t *vec;
2973 void *arg;
2976 #define NMEMCALLBACKS 100
2978 static struct mem_callback mem_callbacks[NMEMCALLBACKS];
2979 static uint_t nmemcallbacks;
2980 static krwlock_t mem_callback_rwlock;
2983 kphysm_setup_func_register(kphysm_setup_vector_t *vec, void *arg)
2985 uint_t i, found;
2988 * This test will become more complicated when the version must
2989 * change.
2991 if (vec->version != KPHYSM_SETUP_VECTOR_VERSION)
2992 return (EINVAL);
2994 if (vec->post_add == NULL || vec->pre_del == NULL ||
2995 vec->post_del == NULL)
2996 return (EINVAL);
2998 rw_enter(&mem_callback_rwlock, RW_WRITER);
2999 for (i = 0, found = 0; i < nmemcallbacks; i++) {
3000 if (mem_callbacks[i].vec == NULL && found == 0)
3001 found = i + 1;
3002 if (mem_callbacks[i].vec == vec &&
3003 mem_callbacks[i].arg == arg) {
3004 #ifdef DEBUG
3005 /* Catch this in DEBUG kernels. */
3006 cmn_err(CE_WARN, "kphysm_setup_func_register"
3007 "(0x%p, 0x%p) duplicate registration from 0x%p",
3008 (void *)vec, arg, (void *)caller());
3009 #endif /* DEBUG */
3010 rw_exit(&mem_callback_rwlock);
3011 return (EEXIST);
3014 if (found != 0) {
3015 i = found - 1;
3016 } else {
3017 ASSERT(nmemcallbacks < NMEMCALLBACKS);
3018 if (nmemcallbacks == NMEMCALLBACKS) {
3019 rw_exit(&mem_callback_rwlock);
3020 return (ENOMEM);
3022 i = nmemcallbacks++;
3024 mem_callbacks[i].vec = vec;
3025 mem_callbacks[i].arg = arg;
3026 rw_exit(&mem_callback_rwlock);
3027 return (0);
3030 void
3031 kphysm_setup_func_unregister(kphysm_setup_vector_t *vec, void *arg)
3033 uint_t i;
3035 rw_enter(&mem_callback_rwlock, RW_WRITER);
3036 for (i = 0; i < nmemcallbacks; i++) {
3037 if (mem_callbacks[i].vec == vec &&
3038 mem_callbacks[i].arg == arg) {
3039 mem_callbacks[i].vec = NULL;
3040 mem_callbacks[i].arg = NULL;
3041 if (i == (nmemcallbacks - 1))
3042 nmemcallbacks--;
3043 break;
3046 rw_exit(&mem_callback_rwlock);
3049 static void
3050 kphysm_setup_post_add(pgcnt_t delta_pages)
3052 uint_t i;
3054 rw_enter(&mem_callback_rwlock, RW_READER);
3055 for (i = 0; i < nmemcallbacks; i++) {
3056 if (mem_callbacks[i].vec != NULL) {
3057 (*mem_callbacks[i].vec->post_add)
3058 (mem_callbacks[i].arg, delta_pages);
3061 rw_exit(&mem_callback_rwlock);
3065 * Note the locking between pre_del and post_del: The reader lock is held
3066 * between the two calls to stop the set of functions from changing.
3069 static int
3070 kphysm_setup_pre_del(pgcnt_t delta_pages)
3072 uint_t i;
3073 int ret;
3074 int aret;
3076 ret = 0;
3077 rw_enter(&mem_callback_rwlock, RW_READER);
3078 for (i = 0; i < nmemcallbacks; i++) {
3079 if (mem_callbacks[i].vec != NULL) {
3080 aret = (*mem_callbacks[i].vec->pre_del)
3081 (mem_callbacks[i].arg, delta_pages);
3082 ret |= aret;
3086 return (ret);
3089 static void
3090 kphysm_setup_post_del(pgcnt_t delta_pages, int cancelled)
3092 uint_t i;
3094 for (i = 0; i < nmemcallbacks; i++) {
3095 if (mem_callbacks[i].vec != NULL) {
3096 (*mem_callbacks[i].vec->post_del)
3097 (mem_callbacks[i].arg, delta_pages, cancelled);
3100 rw_exit(&mem_callback_rwlock);
3103 static int
3104 kphysm_split_memseg(
3105 pfn_t base,
3106 pgcnt_t npgs)
3108 struct memseg *seg;
3109 struct memseg **segpp;
3110 pgcnt_t size_low, size_high;
3111 struct memseg *seg_low, *seg_mid, *seg_high;
3114 * Lock the memsegs list against other updates now
3116 memsegs_lock(1);
3119 * Find boot time memseg that wholly covers this area.
3122 /* First find the memseg with page 'base' in it. */
3123 for (segpp = &memsegs; (seg = *segpp) != NULL;
3124 segpp = &((*segpp)->next)) {
3125 if (base >= seg->pages_base && base < seg->pages_end)
3126 break;
3128 if (seg == NULL) {
3129 memsegs_unlock(1);
3130 return (0);
3132 if (memseg_includes_meta(seg)) {
3133 memsegs_unlock(1);
3134 return (0);
3136 if ((base + npgs) > seg->pages_end) {
3137 memsegs_unlock(1);
3138 return (0);
3142 * Work out the size of the two segments that will
3143 * surround the new segment, one for low address
3144 * and one for high.
3146 ASSERT(base >= seg->pages_base);
3147 size_low = base - seg->pages_base;
3148 ASSERT(seg->pages_end >= (base + npgs));
3149 size_high = seg->pages_end - (base + npgs);
3152 * Sanity check.
3154 if ((size_low + size_high) == 0) {
3155 memsegs_unlock(1);
3156 return (0);
3160 * Allocate the new structures. The old memseg will not be freed
3161 * as there may be a reference to it.
3163 seg_low = NULL;
3164 seg_high = NULL;
3166 if (size_low != 0)
3167 seg_low = memseg_alloc();
3169 seg_mid = memseg_alloc();
3171 if (size_high != 0)
3172 seg_high = memseg_alloc();
3175 * All allocation done now.
3177 if (size_low != 0) {
3178 seg_low->pages = seg->pages;
3179 seg_low->epages = seg_low->pages + size_low;
3180 seg_low->pages_base = seg->pages_base;
3181 seg_low->pages_end = seg_low->pages_base + size_low;
3182 seg_low->next = seg_mid;
3183 seg_low->msegflags = seg->msegflags;
3185 if (size_high != 0) {
3186 seg_high->pages = seg->epages - size_high;
3187 seg_high->epages = seg_high->pages + size_high;
3188 seg_high->pages_base = seg->pages_end - size_high;
3189 seg_high->pages_end = seg_high->pages_base + size_high;
3190 seg_high->next = seg->next;
3191 seg_high->msegflags = seg->msegflags;
3194 seg_mid->pages = seg->pages + size_low;
3195 seg_mid->pages_base = seg->pages_base + size_low;
3196 seg_mid->epages = seg->epages - size_high;
3197 seg_mid->pages_end = seg->pages_end - size_high;
3198 seg_mid->next = (seg_high != NULL) ? seg_high : seg->next;
3199 seg_mid->msegflags = seg->msegflags;
3202 * Update hat_kpm specific info of all involved memsegs and
3203 * allow hat_kpm specific global chain updates.
3205 hat_kpm_split_mseg_update(seg, segpp, seg_low, seg_mid, seg_high);
3208 * At this point we have two equivalent memseg sub-chains,
3209 * seg and seg_low/seg_mid/seg_high, which both chain on to
3210 * the same place in the global chain. By re-writing the pointer
3211 * in the previous element we switch atomically from using the old
3212 * (seg) to the new.
3214 *segpp = (seg_low != NULL) ? seg_low : seg_mid;
3216 membar_enter();
3218 build_pfn_hash();
3219 memsegs_unlock(1);
3222 * We leave the old segment, 'seg', intact as there may be
3223 * references to it. Also, as the value of total_pages has not
3224 * changed and the memsegs list is effectively the same when
3225 * accessed via the old or the new pointer, we do not have to
3226 * cause pageout_scanner() to re-evaluate its hand pointers.
3228 * We currently do not re-use or reclaim the page_t memory.
3229 * If we do, then this may have to change.
3232 mutex_enter(&memseg_lists_lock);
3233 seg->lnext = memseg_edit_junk;
3234 memseg_edit_junk = seg;
3235 mutex_exit(&memseg_lists_lock);
3237 return (1);
3241 * The sfmmu hat layer (e.g.) accesses some parts of the memseg
3242 * structure using physical addresses. Therefore a kmem_cache is
3243 * used with KMC_NOHASH to avoid page crossings within a memseg
3244 * structure. KMC_NOHASH requires that no external (outside of
3245 * slab) information is allowed. This, in turn, implies that the
3246 * cache's slabsize must be exactly a single page, since per-slab
3247 * information (e.g. the freelist for the slab) is kept at the
3248 * end of the slab, where it is easy to locate. Should be changed
3249 * when a more obvious kmem_cache interface/flag will become
3250 * available.
3252 void
3253 mem_config_init()
3255 memseg_cache = kmem_cache_create("memseg_cache", sizeof (struct memseg),
3256 0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
3259 struct memseg *
3260 memseg_alloc()
3262 struct memseg *seg;
3264 seg = kmem_cache_alloc(memseg_cache, KM_SLEEP);
3265 bzero(seg, sizeof (struct memseg));
3267 return (seg);
3271 * Return whether the page_t memory for this memseg
3272 * is included in the memseg itself.
3274 static int
3275 memseg_includes_meta(struct memseg *seg)
3277 return (seg->msegflags & MEMSEG_META_INCL);
3280 pfn_t
3281 memseg_get_start(struct memseg *seg)
3283 pfn_t pt_start;
3285 if (memseg_includes_meta(seg)) {
3286 pt_start = hat_getpfnum(kas.a_hat, (caddr_t)seg->pages);
3288 /* Meta data is required to be at the beginning */
3289 ASSERT(pt_start < seg->pages_base);
3290 } else
3291 pt_start = seg->pages_base;
3293 return (pt_start);
3297 * Invalidate memseg pointers in cpu private vm data caches.
3299 static void
3300 memseg_cpu_vm_flush()
3302 cpu_t *cp;
3303 vm_cpu_data_t *vc;
3305 mutex_enter(&cpu_lock);
3306 pause_cpus(NULL, NULL);
3308 cp = cpu_list;
3309 do {
3310 vc = cp->cpu_vm_data;
3311 vc->vc_pnum_memseg = NULL;
3312 vc->vc_pnext_memseg = NULL;
3314 } while ((cp = cp->cpu_next) != cpu_list);
3316 start_cpus();
3317 mutex_exit(&cpu_lock);