10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51582Skchow * Common Development and Distribution License (the "License").
61582Skchow * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
2211474SJonathan.Adams@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/cmn_err.h>
280Sstevel@tonic-gate #include <sys/vmem.h>
290Sstevel@tonic-gate #include <sys/kmem.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/machsystm.h> /* for page_freelist_coalesce() */
320Sstevel@tonic-gate #include <sys/errno.h>
330Sstevel@tonic-gate #include <sys/memnode.h>
340Sstevel@tonic-gate #include <sys/memlist.h>
350Sstevel@tonic-gate #include <sys/memlist_impl.h>
360Sstevel@tonic-gate #include <sys/tuneable.h>
370Sstevel@tonic-gate #include <sys/proc.h>
380Sstevel@tonic-gate #include <sys/disp.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/vm.h>
410Sstevel@tonic-gate #include <sys/callb.h>
420Sstevel@tonic-gate #include <sys/memlist_plat.h> /* for installed_top_size() */
430Sstevel@tonic-gate #include <sys/condvar_impl.h> /* for CV_HAS_WAITERS() */
440Sstevel@tonic-gate #include <sys/dumphdr.h> /* for dump_resize() */
450Sstevel@tonic-gate #include <sys/atomic.h> /* for use in stats collection */
460Sstevel@tonic-gate #include <sys/rwlock.h>
470Sstevel@tonic-gate #include <sys/cpuvar.h>
480Sstevel@tonic-gate #include <vm/seg_kmem.h>
490Sstevel@tonic-gate #include <vm/seg_kpm.h>
500Sstevel@tonic-gate #include <vm/page.h>
511373Skchow #include <vm/vm_dep.h>
520Sstevel@tonic-gate #define SUNDDI_IMPL /* so sunddi.h will not redefine splx() et al */
530Sstevel@tonic-gate #include <sys/sunddi.h>
540Sstevel@tonic-gate #include <sys/mem_config.h>
550Sstevel@tonic-gate #include <sys/mem_cage.h>
560Sstevel@tonic-gate #include <sys/lgrp.h>
570Sstevel@tonic-gate #include <sys/ddi.h>
580Sstevel@tonic-gate #include <sys/modctl.h>
590Sstevel@tonic-gate
600Sstevel@tonic-gate extern struct memlist *phys_avail;
610Sstevel@tonic-gate
620Sstevel@tonic-gate extern uint_t page_ctrs_adjust(int);
6311185SSean.McEnroe@Sun.COM void page_ctrs_cleanup(void);
640Sstevel@tonic-gate static void kphysm_setup_post_add(pgcnt_t);
650Sstevel@tonic-gate static int kphysm_setup_pre_del(pgcnt_t);
660Sstevel@tonic-gate static void kphysm_setup_post_del(pgcnt_t, int);
670Sstevel@tonic-gate
680Sstevel@tonic-gate static int kphysm_split_memseg(pfn_t base, pgcnt_t npgs);
690Sstevel@tonic-gate
700Sstevel@tonic-gate static int delspan_reserve(pfn_t, pgcnt_t);
710Sstevel@tonic-gate static void delspan_unreserve(pfn_t, pgcnt_t);
720Sstevel@tonic-gate
7310106SJason.Beloro@Sun.COM kmutex_t memseg_lists_lock;
7410106SJason.Beloro@Sun.COM struct memseg *memseg_va_avail;
7510106SJason.Beloro@Sun.COM struct memseg *memseg_alloc(void);
760Sstevel@tonic-gate static struct memseg *memseg_delete_junk;
770Sstevel@tonic-gate static struct memseg *memseg_edit_junk;
780Sstevel@tonic-gate void memseg_remap_init(void);
7910106SJason.Beloro@Sun.COM static void memseg_remap_to_dummy(struct memseg *);
800Sstevel@tonic-gate static void kphysm_addmem_error_undospan(pfn_t, pgcnt_t);
810Sstevel@tonic-gate static struct memseg *memseg_reuse(pgcnt_t);
820Sstevel@tonic-gate
830Sstevel@tonic-gate static struct kmem_cache *memseg_cache;
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
8610106SJason.Beloro@Sun.COM * Interfaces to manage externally allocated
8710106SJason.Beloro@Sun.COM * page_t memory (metadata) for a memseg.
8810106SJason.Beloro@Sun.COM */
8910106SJason.Beloro@Sun.COM #pragma weak memseg_alloc_meta
9010106SJason.Beloro@Sun.COM #pragma weak memseg_free_meta
9110106SJason.Beloro@Sun.COM #pragma weak memseg_get_metapfn
9210106SJason.Beloro@Sun.COM #pragma weak memseg_remap_meta
9310106SJason.Beloro@Sun.COM
9410106SJason.Beloro@Sun.COM extern int ppvm_enable;
9510106SJason.Beloro@Sun.COM extern page_t *ppvm_base;
9610106SJason.Beloro@Sun.COM extern int memseg_alloc_meta(pfn_t, pgcnt_t, void **, pgcnt_t *);
9710106SJason.Beloro@Sun.COM extern void memseg_free_meta(void *, pgcnt_t);
9810106SJason.Beloro@Sun.COM extern pfn_t memseg_get_metapfn(void *, pgcnt_t);
9910106SJason.Beloro@Sun.COM extern void memseg_remap_meta(struct memseg *);
10010106SJason.Beloro@Sun.COM static int memseg_is_dynamic(struct memseg *);
10110106SJason.Beloro@Sun.COM static int memseg_includes_meta(struct memseg *);
10211185SSean.McEnroe@Sun.COM pfn_t memseg_get_start(struct memseg *);
10310106SJason.Beloro@Sun.COM static void memseg_cpu_vm_flush(void);
10410106SJason.Beloro@Sun.COM
10510106SJason.Beloro@Sun.COM int meta_alloc_enable;
10610106SJason.Beloro@Sun.COM
107*12004Sjiang.liu@intel.com #ifdef DEBUG
108*12004Sjiang.liu@intel.com static int memseg_debug;
109*12004Sjiang.liu@intel.com #define MEMSEG_DEBUG(args...) if (memseg_debug) printf(args)
110*12004Sjiang.liu@intel.com #else
111*12004Sjiang.liu@intel.com #define MEMSEG_DEBUG(...)
112*12004Sjiang.liu@intel.com #endif
113*12004Sjiang.liu@intel.com
11410106SJason.Beloro@Sun.COM /*
11510106SJason.Beloro@Sun.COM * Add a chunk of memory to the system.
1160Sstevel@tonic-gate * base: starting PAGESIZE page of new memory.
1170Sstevel@tonic-gate * npgs: length in PAGESIZE pages.
1180Sstevel@tonic-gate *
1190Sstevel@tonic-gate * Adding mem this way doesn't increase the size of the hash tables;
1200Sstevel@tonic-gate * growing them would be too hard. This should be OK, but adding memory
1210Sstevel@tonic-gate * dynamically most likely means more hash misses, since the tables will
1220Sstevel@tonic-gate * be smaller than they otherwise would be.
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate int
kphysm_add_memory_dynamic(pfn_t base,pgcnt_t npgs)1250Sstevel@tonic-gate kphysm_add_memory_dynamic(pfn_t base, pgcnt_t npgs)
1260Sstevel@tonic-gate {
12710106SJason.Beloro@Sun.COM page_t *pp;
12810106SJason.Beloro@Sun.COM page_t *opp, *oepp, *segpp;
1290Sstevel@tonic-gate struct memseg *seg;
1300Sstevel@tonic-gate uint64_t avmem;
1310Sstevel@tonic-gate pfn_t pfn;
1320Sstevel@tonic-gate pfn_t pt_base = base;
1330Sstevel@tonic-gate pgcnt_t tpgs = npgs;
13410106SJason.Beloro@Sun.COM pgcnt_t metapgs = 0;
1350Sstevel@tonic-gate int exhausted;
1360Sstevel@tonic-gate pfn_t pnum;
1370Sstevel@tonic-gate int mnode;
1380Sstevel@tonic-gate caddr_t vaddr;
1390Sstevel@tonic-gate int reuse;
1400Sstevel@tonic-gate int mlret;
14110106SJason.Beloro@Sun.COM int rv;
14210106SJason.Beloro@Sun.COM int flags;
14310106SJason.Beloro@Sun.COM int meta_alloc = 0;
1440Sstevel@tonic-gate void *mapva;
14510106SJason.Beloro@Sun.COM void *metabase = (void *)base;
1460Sstevel@tonic-gate pgcnt_t nkpmpgs = 0;
1470Sstevel@tonic-gate offset_t kpm_pages_off;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate cmn_err(CE_CONT,
1500Sstevel@tonic-gate "?kphysm_add_memory_dynamic: adding %ldK at 0x%" PRIx64 "\n",
1510Sstevel@tonic-gate npgs << (PAGESHIFT - 10), (uint64_t)base << PAGESHIFT);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Add this span in the delete list to prevent interactions.
1550Sstevel@tonic-gate */
1560Sstevel@tonic-gate if (!delspan_reserve(base, npgs)) {
1570Sstevel@tonic-gate return (KPHYSM_ESPAN);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate * Check to see if any of the memory span has been added
1610Sstevel@tonic-gate * by trying an add to the installed memory list. This
1620Sstevel@tonic-gate * forms the interlocking process for add.
1630Sstevel@tonic-gate */
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate memlist_write_lock();
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate mlret = memlist_add_span((uint64_t)(pt_base) << PAGESHIFT,
1680Sstevel@tonic-gate (uint64_t)(tpgs) << PAGESHIFT, &phys_install);
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate if (mlret == MEML_SPANOP_OK)
1710Sstevel@tonic-gate installed_top_size(phys_install, &physmax, &physinstalled);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate memlist_write_unlock();
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate if (mlret != MEML_SPANOP_OK) {
1760Sstevel@tonic-gate if (mlret == MEML_SPANOP_EALLOC) {
1770Sstevel@tonic-gate delspan_unreserve(pt_base, tpgs);
1780Sstevel@tonic-gate return (KPHYSM_ERESOURCE);
17910106SJason.Beloro@Sun.COM } else if (mlret == MEML_SPANOP_ESPAN) {
1800Sstevel@tonic-gate delspan_unreserve(pt_base, tpgs);
1810Sstevel@tonic-gate return (KPHYSM_ESPAN);
1820Sstevel@tonic-gate } else {
1830Sstevel@tonic-gate delspan_unreserve(pt_base, tpgs);
1840Sstevel@tonic-gate return (KPHYSM_ERESOURCE);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
18810106SJason.Beloro@Sun.COM if (meta_alloc_enable) {
18910106SJason.Beloro@Sun.COM /*
19010106SJason.Beloro@Sun.COM * Allocate the page_t's from existing memory;
19110106SJason.Beloro@Sun.COM * if that fails, allocate from the incoming memory.
19210106SJason.Beloro@Sun.COM */
19310106SJason.Beloro@Sun.COM rv = memseg_alloc_meta(base, npgs, &metabase, &metapgs);
19410106SJason.Beloro@Sun.COM if (rv == KPHYSM_OK) {
19510106SJason.Beloro@Sun.COM ASSERT(metapgs);
19610106SJason.Beloro@Sun.COM ASSERT(btopr(npgs * sizeof (page_t)) <= metapgs);
19710106SJason.Beloro@Sun.COM meta_alloc = 1;
19810106SJason.Beloro@Sun.COM goto mapalloc;
19910106SJason.Beloro@Sun.COM }
20010106SJason.Beloro@Sun.COM }
20110106SJason.Beloro@Sun.COM
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate * We store the page_t's for this new memory in the first
2040Sstevel@tonic-gate * few pages of the chunk. Here, we go and get'em ...
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * The expression after the '-' gives the number of pages
2090Sstevel@tonic-gate * that will fit in the new memory based on a requirement
2100Sstevel@tonic-gate * of (PAGESIZE + sizeof (page_t)) bytes per page.
2110Sstevel@tonic-gate */
2120Sstevel@tonic-gate metapgs = npgs - (((uint64_t)(npgs) << PAGESHIFT) /
2130Sstevel@tonic-gate (PAGESIZE + sizeof (page_t)));
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate npgs -= metapgs;
2160Sstevel@tonic-gate base += metapgs;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate ASSERT(btopr(npgs * sizeof (page_t)) <= metapgs);
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate exhausted = (metapgs == 0 || npgs == 0);
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate if (kpm_enable && !exhausted) {
2230Sstevel@tonic-gate pgcnt_t start, end, nkpmpgs_prelim;
2240Sstevel@tonic-gate size_t ptsz;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * A viable kpm large page mapping must not overlap two
2280Sstevel@tonic-gate * dynamic memsegs. Therefore the total size is checked
2290Sstevel@tonic-gate * to be at least kpm_pgsz and also whether start and end
2300Sstevel@tonic-gate * points are at least kpm_pgsz aligned.
2310Sstevel@tonic-gate */
2320Sstevel@tonic-gate if (ptokpmp(tpgs) < 1 || pmodkpmp(pt_base) ||
2330Sstevel@tonic-gate pmodkpmp(base + npgs)) {
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate kphysm_addmem_error_undospan(pt_base, tpgs);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate * There is no specific error code for violating
2390Sstevel@tonic-gate * kpm granularity constraints.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate return (KPHYSM_ENOTVIABLE);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate start = kpmptop(ptokpmp(base));
2450Sstevel@tonic-gate end = kpmptop(ptokpmp(base + npgs));
2460Sstevel@tonic-gate nkpmpgs_prelim = ptokpmp(end - start);
2470Sstevel@tonic-gate ptsz = npgs * sizeof (page_t);
2480Sstevel@tonic-gate metapgs = btopr(ptsz + nkpmpgs_prelim * KPMPAGE_T_SZ);
2490Sstevel@tonic-gate exhausted = (tpgs <= metapgs);
2500Sstevel@tonic-gate if (!exhausted) {
2510Sstevel@tonic-gate npgs = tpgs - metapgs;
2520Sstevel@tonic-gate base = pt_base + metapgs;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /* final nkpmpgs */
2550Sstevel@tonic-gate start = kpmptop(ptokpmp(base));
2560Sstevel@tonic-gate nkpmpgs = ptokpmp(end - start);
2570Sstevel@tonic-gate kpm_pages_off = ptsz +
2586242Smb158278 (nkpmpgs_prelim - nkpmpgs) * KPMPAGE_T_SZ;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate * Is memory area supplied too small?
2640Sstevel@tonic-gate */
2650Sstevel@tonic-gate if (exhausted) {
2660Sstevel@tonic-gate kphysm_addmem_error_undospan(pt_base, tpgs);
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * There is no specific error code for 'too small'.
2690Sstevel@tonic-gate */
2700Sstevel@tonic-gate return (KPHYSM_ERESOURCE);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
27310106SJason.Beloro@Sun.COM mapalloc:
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate * We may re-use a previously allocated VA space for the page_ts
2760Sstevel@tonic-gate * eventually, but we need to initialize and lock the pages first.
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate * Get an address in the kernel address map, map
2810Sstevel@tonic-gate * the page_t pages and see if we can touch them.
2820Sstevel@tonic-gate */
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate mapva = vmem_alloc(heap_arena, ptob(metapgs), VM_NOSLEEP);
2850Sstevel@tonic-gate if (mapva == NULL) {
2860Sstevel@tonic-gate cmn_err(CE_WARN, "kphysm_add_memory_dynamic:"
2870Sstevel@tonic-gate " Can't allocate VA for page_ts");
2880Sstevel@tonic-gate
28910106SJason.Beloro@Sun.COM if (meta_alloc)
29010106SJason.Beloro@Sun.COM memseg_free_meta(metabase, metapgs);
2910Sstevel@tonic-gate kphysm_addmem_error_undospan(pt_base, tpgs);
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate return (KPHYSM_ERESOURCE);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate pp = mapva;
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if (physmax < (pt_base + tpgs))
2980Sstevel@tonic-gate physmax = (pt_base + tpgs);
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate * In the remapping code we map one page at a time so we must do
3020Sstevel@tonic-gate * the same here to match mapping sizes.
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate pfn = pt_base;
3050Sstevel@tonic-gate vaddr = (caddr_t)pp;
3060Sstevel@tonic-gate for (pnum = 0; pnum < metapgs; pnum++) {
30710106SJason.Beloro@Sun.COM if (meta_alloc)
30810106SJason.Beloro@Sun.COM pfn = memseg_get_metapfn(metabase, (pgcnt_t)pnum);
3090Sstevel@tonic-gate hat_devload(kas.a_hat, vaddr, ptob(1), pfn,
3100Sstevel@tonic-gate PROT_READ | PROT_WRITE,
3110Sstevel@tonic-gate HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
3120Sstevel@tonic-gate pfn++;
3130Sstevel@tonic-gate vaddr += ptob(1);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate if (ddi_peek32((dev_info_t *)NULL,
3170Sstevel@tonic-gate (int32_t *)pp, (int32_t *)0) == DDI_FAILURE) {
3180Sstevel@tonic-gate
3197694SJakub.Jirsa@Sun.COM cmn_err(CE_WARN, "kphysm_add_memory_dynamic:"
3200Sstevel@tonic-gate " Can't access pp array at 0x%p [phys 0x%lx]",
3210Sstevel@tonic-gate (void *)pp, pt_base);
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate hat_unload(kas.a_hat, (caddr_t)pp, ptob(metapgs),
3240Sstevel@tonic-gate HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate vmem_free(heap_arena, mapva, ptob(metapgs));
32710106SJason.Beloro@Sun.COM if (meta_alloc)
32810106SJason.Beloro@Sun.COM memseg_free_meta(metabase, metapgs);
3290Sstevel@tonic-gate kphysm_addmem_error_undospan(pt_base, tpgs);
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate return (KPHYSM_EFAULT);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate * Add this memory slice to its memory node translation.
3360Sstevel@tonic-gate *
3370Sstevel@tonic-gate * Note that right now, each node may have only one slice;
3380Sstevel@tonic-gate * this may change with COD or in larger SSM systems with
3390Sstevel@tonic-gate * nested latency groups, so we must not assume that the
3400Sstevel@tonic-gate * node does not yet exist.
341*12004Sjiang.liu@intel.com *
342*12004Sjiang.liu@intel.com * Note that there may be multiple memory nodes associated with
343*12004Sjiang.liu@intel.com * a single lgrp node on x86 systems.
3440Sstevel@tonic-gate */
3457923SChristopher.Baumbauer@Sun.COM pnum = pt_base + tpgs - 1;
34610106SJason.Beloro@Sun.COM mem_node_add_range(pt_base, pnum);
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate /*
3495331Samw * Allocate or resize page counters as necessary to accommodate
3500Sstevel@tonic-gate * the increase in memory pages.
3510Sstevel@tonic-gate */
3520Sstevel@tonic-gate mnode = PFN_2_MEM_NODE(pnum);
35310106SJason.Beloro@Sun.COM PAGE_CTRS_ADJUST(base, npgs, rv);
35410106SJason.Beloro@Sun.COM if (rv) {
35510106SJason.Beloro@Sun.COM
35610106SJason.Beloro@Sun.COM mem_node_del_range(pt_base, pnum);
3570Sstevel@tonic-gate
35811185SSean.McEnroe@Sun.COM /* cleanup the page counters */
35911185SSean.McEnroe@Sun.COM page_ctrs_cleanup();
36011185SSean.McEnroe@Sun.COM
3610Sstevel@tonic-gate hat_unload(kas.a_hat, (caddr_t)pp, ptob(metapgs),
3620Sstevel@tonic-gate HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate vmem_free(heap_arena, mapva, ptob(metapgs));
36510106SJason.Beloro@Sun.COM if (meta_alloc)
36610106SJason.Beloro@Sun.COM memseg_free_meta(metabase, metapgs);
3670Sstevel@tonic-gate kphysm_addmem_error_undospan(pt_base, tpgs);
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate return (KPHYSM_ERESOURCE);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate * Update the phys_avail memory list.
3740Sstevel@tonic-gate * The phys_install list was done at the start.
3750Sstevel@tonic-gate */
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate memlist_write_lock();
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate mlret = memlist_add_span((uint64_t)(base) << PAGESHIFT,
3800Sstevel@tonic-gate (uint64_t)(npgs) << PAGESHIFT, &phys_avail);
3810Sstevel@tonic-gate ASSERT(mlret == MEML_SPANOP_OK);
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate memlist_write_unlock();
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate /* See if we can find a memseg to re-use. */
38610106SJason.Beloro@Sun.COM if (meta_alloc) {
38710106SJason.Beloro@Sun.COM seg = memseg_reuse(0);
38810106SJason.Beloro@Sun.COM reuse = 1; /* force unmapping of temp mapva */
38910106SJason.Beloro@Sun.COM flags = MEMSEG_DYNAMIC | MEMSEG_META_ALLOC;
39010106SJason.Beloro@Sun.COM /*
39110106SJason.Beloro@Sun.COM * There is a 1:1 fixed relationship between a pfn
39210106SJason.Beloro@Sun.COM * and a page_t VA. The pfn is used as an index into
39310106SJason.Beloro@Sun.COM * the ppvm_base page_t table in order to calculate
39410106SJason.Beloro@Sun.COM * the page_t base address for a given pfn range.
39510106SJason.Beloro@Sun.COM */
39610106SJason.Beloro@Sun.COM segpp = ppvm_base + base;
39710106SJason.Beloro@Sun.COM } else {
39810106SJason.Beloro@Sun.COM seg = memseg_reuse(metapgs);
39910106SJason.Beloro@Sun.COM reuse = (seg != NULL);
40010106SJason.Beloro@Sun.COM flags = MEMSEG_DYNAMIC | MEMSEG_META_INCL;
40110106SJason.Beloro@Sun.COM segpp = pp;
40210106SJason.Beloro@Sun.COM }
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate * Initialize the memseg structure representing this memory
4060Sstevel@tonic-gate * and add it to the existing list of memsegs. Do some basic
4070Sstevel@tonic-gate * initialization and add the memory to the system.
4080Sstevel@tonic-gate * In order to prevent lock deadlocks, the add_physmem()
4090Sstevel@tonic-gate * code is repeated here, but split into several stages.
41010106SJason.Beloro@Sun.COM *
41110106SJason.Beloro@Sun.COM * If a memseg is reused, invalidate memseg pointers in
41210106SJason.Beloro@Sun.COM * all cpu vm caches. We need to do this this since the check
41310106SJason.Beloro@Sun.COM * pp >= seg->pages && pp < seg->epages
41410106SJason.Beloro@Sun.COM * used in various places is not atomic and so the first compare
41510106SJason.Beloro@Sun.COM * can happen before reuse and the second compare after reuse.
41610106SJason.Beloro@Sun.COM * The invalidation ensures that a memseg is not deferenced while
41710106SJason.Beloro@Sun.COM * it's page/pfn pointers are changing.
4180Sstevel@tonic-gate */
4190Sstevel@tonic-gate if (seg == NULL) {
42010106SJason.Beloro@Sun.COM seg = memseg_alloc();
42110106SJason.Beloro@Sun.COM ASSERT(seg != NULL);
42210106SJason.Beloro@Sun.COM seg->msegflags = flags;
42310106SJason.Beloro@Sun.COM MEMSEG_DEBUG("memseg_get: alloc seg=0x%p, pages=0x%p",
42410106SJason.Beloro@Sun.COM (void *)seg, (void *)(seg->pages));
42510106SJason.Beloro@Sun.COM seg->pages = segpp;
4260Sstevel@tonic-gate } else {
42710106SJason.Beloro@Sun.COM ASSERT(seg->msegflags == flags);
42810106SJason.Beloro@Sun.COM ASSERT(seg->pages_base == seg->pages_end);
42910106SJason.Beloro@Sun.COM MEMSEG_DEBUG("memseg_get: reuse seg=0x%p, pages=0x%p",
43010106SJason.Beloro@Sun.COM (void *)seg, (void *)(seg->pages));
43110106SJason.Beloro@Sun.COM if (meta_alloc) {
43210106SJason.Beloro@Sun.COM memseg_cpu_vm_flush();
43310106SJason.Beloro@Sun.COM seg->pages = segpp;
43410106SJason.Beloro@Sun.COM }
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate seg->epages = seg->pages + npgs;
4380Sstevel@tonic-gate seg->pages_base = base;
4390Sstevel@tonic-gate seg->pages_end = base + npgs;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate * Initialize metadata. The page_ts are set to locked state
4430Sstevel@tonic-gate * ready to be freed.
4440Sstevel@tonic-gate */
4450Sstevel@tonic-gate bzero((caddr_t)pp, ptob(metapgs));
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate pfn = seg->pages_base;
4480Sstevel@tonic-gate /* Save the original pp base in case we reuse a memseg. */
4490Sstevel@tonic-gate opp = pp;
4500Sstevel@tonic-gate oepp = opp + npgs;
4510Sstevel@tonic-gate for (pp = opp; pp < oepp; pp++) {
4520Sstevel@tonic-gate pp->p_pagenum = pfn;
4530Sstevel@tonic-gate pfn++;
4540Sstevel@tonic-gate page_iolock_init(pp);
4550Sstevel@tonic-gate while (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_RECLAIM))
4560Sstevel@tonic-gate continue;
4570Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate if (reuse) {
4610Sstevel@tonic-gate /* Remap our page_ts to the re-used memseg VA space. */
4620Sstevel@tonic-gate pfn = pt_base;
4630Sstevel@tonic-gate vaddr = (caddr_t)seg->pages;
4640Sstevel@tonic-gate for (pnum = 0; pnum < metapgs; pnum++) {
46510106SJason.Beloro@Sun.COM if (meta_alloc)
46610106SJason.Beloro@Sun.COM pfn = memseg_get_metapfn(metabase,
46710106SJason.Beloro@Sun.COM (pgcnt_t)pnum);
4680Sstevel@tonic-gate hat_devload(kas.a_hat, vaddr, ptob(1), pfn,
4690Sstevel@tonic-gate PROT_READ | PROT_WRITE,
4700Sstevel@tonic-gate HAT_LOAD_REMAP | HAT_LOAD | HAT_LOAD_NOCONSIST);
4710Sstevel@tonic-gate pfn++;
4720Sstevel@tonic-gate vaddr += ptob(1);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate hat_unload(kas.a_hat, (caddr_t)opp, ptob(metapgs),
4760Sstevel@tonic-gate HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate vmem_free(heap_arena, mapva, ptob(metapgs));
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate hat_kpm_addmem_mseg_update(seg, nkpmpgs, kpm_pages_off);
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate memsegs_lock(1);
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate * The new memseg is inserted at the beginning of the list.
4870Sstevel@tonic-gate * Not only does this save searching for the tail, but in the
4880Sstevel@tonic-gate * case of a re-used memseg, it solves the problem of what
4897694SJakub.Jirsa@Sun.COM * happens if some process has still got a pointer to the
4900Sstevel@tonic-gate * memseg and follows the next pointer to continue traversing
4910Sstevel@tonic-gate * the memsegs list.
4920Sstevel@tonic-gate */
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate hat_kpm_addmem_mseg_insert(seg);
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate seg->next = memsegs;
4970Sstevel@tonic-gate membar_producer();
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate hat_kpm_addmem_memsegs_update(seg);
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate memsegs = seg;
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate build_pfn_hash();
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate total_pages += npgs;
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate * Recalculate the paging parameters now total_pages has changed.
5090Sstevel@tonic-gate * This will also cause the clock hands to be reset before next use.
5100Sstevel@tonic-gate */
5110Sstevel@tonic-gate setupclock(1);
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate memsegs_unlock(1);
5140Sstevel@tonic-gate
5151582Skchow PLCNT_MODIFY_MAX(seg->pages_base, (long)npgs);
5161582Skchow
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate * Free the pages outside the lock to avoid locking loops.
5190Sstevel@tonic-gate */
5200Sstevel@tonic-gate for (pp = seg->pages; pp < seg->epages; pp++) {
5210Sstevel@tonic-gate page_free(pp, 1);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate * Now that we've updated the appropriate memory lists we
5260Sstevel@tonic-gate * need to reset a number of globals, since we've increased memory.
5270Sstevel@tonic-gate * Several have already been updated for us as noted above. The
5280Sstevel@tonic-gate * globals we're interested in at this point are:
5290Sstevel@tonic-gate * physmax - highest page frame number.
5300Sstevel@tonic-gate * physinstalled - number of pages currently installed (done earlier)
5310Sstevel@tonic-gate * maxmem - max free pages in the system
5320Sstevel@tonic-gate * physmem - physical memory pages available
5330Sstevel@tonic-gate * availrmem - real memory available
5340Sstevel@tonic-gate */
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate mutex_enter(&freemem_lock);
5370Sstevel@tonic-gate maxmem += npgs;
5380Sstevel@tonic-gate physmem += npgs;
5390Sstevel@tonic-gate availrmem += npgs;
5400Sstevel@tonic-gate availrmem_initial += npgs;
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate mutex_exit(&freemem_lock);
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate dump_resize();
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate page_freelist_coalesce_all(mnode);
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate kphysm_setup_post_add(npgs);
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate cmn_err(CE_CONT, "?kphysm_add_memory_dynamic: mem = %ldK "
5510Sstevel@tonic-gate "(0x%" PRIx64 ")\n",
5520Sstevel@tonic-gate physinstalled << (PAGESHIFT - 10),
5530Sstevel@tonic-gate (uint64_t)physinstalled << PAGESHIFT);
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate avmem = (uint64_t)freemem << PAGESHIFT;
5560Sstevel@tonic-gate cmn_err(CE_CONT, "?kphysm_add_memory_dynamic: "
5570Sstevel@tonic-gate "avail mem = %" PRId64 "\n", avmem);
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate /*
5600Sstevel@tonic-gate * Update lgroup generation number on single lgroup systems
5610Sstevel@tonic-gate */
5620Sstevel@tonic-gate if (nlgrps == 1)
5630Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_GEN_UPDATE, 0, 0);
5640Sstevel@tonic-gate
56511600SVikram.Hegde@Sun.COM /*
56611600SVikram.Hegde@Sun.COM * Inform DDI of update
56711600SVikram.Hegde@Sun.COM */
56811600SVikram.Hegde@Sun.COM ddi_mem_update((uint64_t)(pt_base) << PAGESHIFT,
56911600SVikram.Hegde@Sun.COM (uint64_t)(tpgs) << PAGESHIFT);
57011600SVikram.Hegde@Sun.COM
5710Sstevel@tonic-gate delspan_unreserve(pt_base, tpgs);
572*12004Sjiang.liu@intel.com
5730Sstevel@tonic-gate return (KPHYSM_OK); /* Successfully added system memory */
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate * There are various error conditions in kphysm_add_memory_dynamic()
5780Sstevel@tonic-gate * which require a rollback of already changed global state.
5790Sstevel@tonic-gate */
5800Sstevel@tonic-gate static void
kphysm_addmem_error_undospan(pfn_t pt_base,pgcnt_t tpgs)5810Sstevel@tonic-gate kphysm_addmem_error_undospan(pfn_t pt_base, pgcnt_t tpgs)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate int mlret;
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate /* Unreserve memory span. */
5860Sstevel@tonic-gate memlist_write_lock();
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate mlret = memlist_delete_span(
5890Sstevel@tonic-gate (uint64_t)(pt_base) << PAGESHIFT,
5900Sstevel@tonic-gate (uint64_t)(tpgs) << PAGESHIFT, &phys_install);
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate ASSERT(mlret == MEML_SPANOP_OK);
5930Sstevel@tonic-gate phys_install_has_changed();
5940Sstevel@tonic-gate installed_top_size(phys_install, &physmax, &physinstalled);
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate memlist_write_unlock();
5970Sstevel@tonic-gate delspan_unreserve(pt_base, tpgs);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate /*
60110106SJason.Beloro@Sun.COM * Only return an available memseg of exactly the right size
60210106SJason.Beloro@Sun.COM * if size is required.
6030Sstevel@tonic-gate * When the meta data area has it's own virtual address space
6040Sstevel@tonic-gate * we will need to manage this more carefully and do best fit
6055331Samw * allocations, possibly splitting an available area.
6060Sstevel@tonic-gate */
60710106SJason.Beloro@Sun.COM struct memseg *
memseg_reuse(pgcnt_t metapgs)6080Sstevel@tonic-gate memseg_reuse(pgcnt_t metapgs)
6090Sstevel@tonic-gate {
61010106SJason.Beloro@Sun.COM int type;
6110Sstevel@tonic-gate struct memseg **segpp, *seg;
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate mutex_enter(&memseg_lists_lock);
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate segpp = &memseg_va_avail;
6160Sstevel@tonic-gate for (; (seg = *segpp) != NULL; segpp = &seg->lnext) {
6170Sstevel@tonic-gate caddr_t end;
6180Sstevel@tonic-gate
61910106SJason.Beloro@Sun.COM /*
62010106SJason.Beloro@Sun.COM * Make sure we are reusing the right segment type.
62110106SJason.Beloro@Sun.COM */
62210106SJason.Beloro@Sun.COM type = metapgs ? MEMSEG_META_INCL : MEMSEG_META_ALLOC;
62310106SJason.Beloro@Sun.COM
62410106SJason.Beloro@Sun.COM if ((seg->msegflags & (MEMSEG_META_INCL | MEMSEG_META_ALLOC))
62510106SJason.Beloro@Sun.COM != type)
62610106SJason.Beloro@Sun.COM continue;
62710106SJason.Beloro@Sun.COM
6280Sstevel@tonic-gate if (kpm_enable)
6290Sstevel@tonic-gate end = hat_kpm_mseg_reuse(seg);
6300Sstevel@tonic-gate else
6310Sstevel@tonic-gate end = (caddr_t)seg->epages;
6320Sstevel@tonic-gate
63310106SJason.Beloro@Sun.COM /*
63410106SJason.Beloro@Sun.COM * Check for the right size if it is provided.
63510106SJason.Beloro@Sun.COM */
63610106SJason.Beloro@Sun.COM if (!metapgs || btopr(end - (caddr_t)seg->pages) == metapgs) {
6370Sstevel@tonic-gate *segpp = seg->lnext;
6380Sstevel@tonic-gate seg->lnext = NULL;
6390Sstevel@tonic-gate break;
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate mutex_exit(&memseg_lists_lock);
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate return (seg);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate static uint_t handle_gen;
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate struct memdelspan {
6500Sstevel@tonic-gate struct memdelspan *mds_next;
6510Sstevel@tonic-gate pfn_t mds_base;
6520Sstevel@tonic-gate pgcnt_t mds_npgs;
6530Sstevel@tonic-gate uint_t *mds_bitmap;
6540Sstevel@tonic-gate uint_t *mds_bitmap_retired;
6550Sstevel@tonic-gate };
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate #define NBPBMW (sizeof (uint_t) * NBBY)
6580Sstevel@tonic-gate #define MDS_BITMAPBYTES(MDSP) \
6590Sstevel@tonic-gate ((((MDSP)->mds_npgs + NBPBMW - 1) / NBPBMW) * sizeof (uint_t))
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate struct transit_list {
6620Sstevel@tonic-gate struct transit_list *trl_next;
6630Sstevel@tonic-gate struct memdelspan *trl_spans;
6640Sstevel@tonic-gate int trl_collect;
6650Sstevel@tonic-gate };
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate struct transit_list_head {
6680Sstevel@tonic-gate kmutex_t trh_lock;
6690Sstevel@tonic-gate struct transit_list *trh_head;
6700Sstevel@tonic-gate };
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate static struct transit_list_head transit_list_head;
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate struct mem_handle;
6750Sstevel@tonic-gate static void transit_list_collect(struct mem_handle *, int);
6760Sstevel@tonic-gate static void transit_list_insert(struct transit_list *);
6770Sstevel@tonic-gate static void transit_list_remove(struct transit_list *);
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate #ifdef DEBUG
6800Sstevel@tonic-gate #define MEM_DEL_STATS
6810Sstevel@tonic-gate #endif /* DEBUG */
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate #ifdef MEM_DEL_STATS
6840Sstevel@tonic-gate static int mem_del_stat_print = 0;
6850Sstevel@tonic-gate struct mem_del_stat {
6860Sstevel@tonic-gate uint_t nloop;
6870Sstevel@tonic-gate uint_t need_free;
6880Sstevel@tonic-gate uint_t free_loop;
6890Sstevel@tonic-gate uint_t free_low;
6900Sstevel@tonic-gate uint_t free_failed;
6910Sstevel@tonic-gate uint_t ncheck;
6920Sstevel@tonic-gate uint_t nopaget;
6930Sstevel@tonic-gate uint_t lockfail;
6940Sstevel@tonic-gate uint_t nfree;
6950Sstevel@tonic-gate uint_t nreloc;
6960Sstevel@tonic-gate uint_t nrelocfail;
6970Sstevel@tonic-gate uint_t already_done;
6980Sstevel@tonic-gate uint_t first_notfree;
6990Sstevel@tonic-gate uint_t npplocked;
7000Sstevel@tonic-gate uint_t nlockreloc;
7010Sstevel@tonic-gate uint_t nnorepl;
7020Sstevel@tonic-gate uint_t nmodreloc;
7030Sstevel@tonic-gate uint_t ndestroy;
7040Sstevel@tonic-gate uint_t nputpage;
7050Sstevel@tonic-gate uint_t nnoreclaim;
7060Sstevel@tonic-gate uint_t ndelay;
7070Sstevel@tonic-gate uint_t demotefail;
7080Sstevel@tonic-gate uint64_t nticks_total;
7090Sstevel@tonic-gate uint64_t nticks_pgrp;
7100Sstevel@tonic-gate uint_t retired;
7110Sstevel@tonic-gate uint_t toxic;
7120Sstevel@tonic-gate uint_t failing;
7130Sstevel@tonic-gate uint_t modtoxic;
7140Sstevel@tonic-gate uint_t npplkdtoxic;
7150Sstevel@tonic-gate uint_t gptlmodfail;
7160Sstevel@tonic-gate uint_t gptllckfail;
7170Sstevel@tonic-gate };
7180Sstevel@tonic-gate /*
7190Sstevel@tonic-gate * The stat values are only incremented in the delete thread
7200Sstevel@tonic-gate * so no locking or atomic required.
7210Sstevel@tonic-gate */
7220Sstevel@tonic-gate #define MDSTAT_INCR(MHP, FLD) (MHP)->mh_delstat.FLD++
7230Sstevel@tonic-gate #define MDSTAT_TOTAL(MHP, ntck) ((MHP)->mh_delstat.nticks_total += (ntck))
7240Sstevel@tonic-gate #define MDSTAT_PGRP(MHP, ntck) ((MHP)->mh_delstat.nticks_pgrp += (ntck))
7250Sstevel@tonic-gate static void mem_del_stat_print_func(struct mem_handle *);
7260Sstevel@tonic-gate #define MDSTAT_PRINT(MHP) mem_del_stat_print_func((MHP))
7270Sstevel@tonic-gate #else /* MEM_DEL_STATS */
7280Sstevel@tonic-gate #define MDSTAT_INCR(MHP, FLD)
7290Sstevel@tonic-gate #define MDSTAT_TOTAL(MHP, ntck)
7300Sstevel@tonic-gate #define MDSTAT_PGRP(MHP, ntck)
7310Sstevel@tonic-gate #define MDSTAT_PRINT(MHP)
7320Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate typedef enum mhnd_state {MHND_FREE = 0, MHND_INIT, MHND_STARTING,
7350Sstevel@tonic-gate MHND_RUNNING, MHND_DONE, MHND_RELEASE} mhnd_state_t;
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate /*
7380Sstevel@tonic-gate * mh_mutex must be taken to examine or change mh_exthandle and mh_state.
7390Sstevel@tonic-gate * The mutex may not be required for other fields, dependent on mh_state.
7400Sstevel@tonic-gate */
7410Sstevel@tonic-gate struct mem_handle {
7420Sstevel@tonic-gate kmutex_t mh_mutex;
7430Sstevel@tonic-gate struct mem_handle *mh_next;
7440Sstevel@tonic-gate memhandle_t mh_exthandle;
7450Sstevel@tonic-gate mhnd_state_t mh_state;
7460Sstevel@tonic-gate struct transit_list mh_transit;
7470Sstevel@tonic-gate pgcnt_t mh_phys_pages;
7480Sstevel@tonic-gate pgcnt_t mh_vm_pages;
7490Sstevel@tonic-gate pgcnt_t mh_hold_todo;
7500Sstevel@tonic-gate void (*mh_delete_complete)(void *, int error);
7510Sstevel@tonic-gate void *mh_delete_complete_arg;
7520Sstevel@tonic-gate volatile uint_t mh_cancel;
7530Sstevel@tonic-gate volatile uint_t mh_dr_aio_cleanup_cancel;
7540Sstevel@tonic-gate volatile uint_t mh_aio_cleanup_done;
7550Sstevel@tonic-gate kcondvar_t mh_cv;
7560Sstevel@tonic-gate kthread_id_t mh_thread_id;
7570Sstevel@tonic-gate page_t *mh_deleted; /* link through p_next */
7580Sstevel@tonic-gate #ifdef MEM_DEL_STATS
7590Sstevel@tonic-gate struct mem_del_stat mh_delstat;
7600Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
7610Sstevel@tonic-gate };
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate static struct mem_handle *mem_handle_head;
7640Sstevel@tonic-gate static kmutex_t mem_handle_list_mutex;
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate static struct mem_handle *
kphysm_allocate_mem_handle()7670Sstevel@tonic-gate kphysm_allocate_mem_handle()
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate struct mem_handle *mhp;
7700Sstevel@tonic-gate
7710Sstevel@tonic-gate mhp = kmem_zalloc(sizeof (struct mem_handle), KM_SLEEP);
7720Sstevel@tonic-gate mutex_init(&mhp->mh_mutex, NULL, MUTEX_DEFAULT, NULL);
7730Sstevel@tonic-gate mutex_enter(&mem_handle_list_mutex);
7740Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
7750Sstevel@tonic-gate /* handle_gen is protected by list mutex. */
776567Sdmick mhp->mh_exthandle = (memhandle_t)(uintptr_t)(++handle_gen);
7770Sstevel@tonic-gate mhp->mh_next = mem_handle_head;
7780Sstevel@tonic-gate mem_handle_head = mhp;
7790Sstevel@tonic-gate mutex_exit(&mem_handle_list_mutex);
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate return (mhp);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate static void
kphysm_free_mem_handle(struct mem_handle * mhp)7850Sstevel@tonic-gate kphysm_free_mem_handle(struct mem_handle *mhp)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate struct mem_handle **mhpp;
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate ASSERT(mutex_owned(&mhp->mh_mutex));
7900Sstevel@tonic-gate ASSERT(mhp->mh_state == MHND_FREE);
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate * Exit the mutex to preserve locking order. This is OK
7930Sstevel@tonic-gate * here as once in the FREE state, the handle cannot
7940Sstevel@tonic-gate * be found by a lookup.
7950Sstevel@tonic-gate */
7960Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate mutex_enter(&mem_handle_list_mutex);
7990Sstevel@tonic-gate mhpp = &mem_handle_head;
8000Sstevel@tonic-gate while (*mhpp != NULL && *mhpp != mhp)
8010Sstevel@tonic-gate mhpp = &(*mhpp)->mh_next;
8020Sstevel@tonic-gate ASSERT(*mhpp == mhp);
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate * No need to lock the handle (mh_mutex) as only
8050Sstevel@tonic-gate * mh_next changing and this is the only thread that
8060Sstevel@tonic-gate * can be referncing mhp.
8070Sstevel@tonic-gate */
8080Sstevel@tonic-gate *mhpp = mhp->mh_next;
8090Sstevel@tonic-gate mutex_exit(&mem_handle_list_mutex);
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate mutex_destroy(&mhp->mh_mutex);
8120Sstevel@tonic-gate kmem_free(mhp, sizeof (struct mem_handle));
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate
8150Sstevel@tonic-gate /*
8160Sstevel@tonic-gate * This function finds the internal mem_handle corresponding to an
8170Sstevel@tonic-gate * external handle and returns it with the mh_mutex held.
8180Sstevel@tonic-gate */
8190Sstevel@tonic-gate static struct mem_handle *
kphysm_lookup_mem_handle(memhandle_t handle)8200Sstevel@tonic-gate kphysm_lookup_mem_handle(memhandle_t handle)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate struct mem_handle *mhp;
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate mutex_enter(&mem_handle_list_mutex);
8250Sstevel@tonic-gate for (mhp = mem_handle_head; mhp != NULL; mhp = mhp->mh_next) {
8260Sstevel@tonic-gate if (mhp->mh_exthandle == handle) {
8270Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
8280Sstevel@tonic-gate /*
8290Sstevel@tonic-gate * The state of the handle could have been changed
8300Sstevel@tonic-gate * by kphysm_del_release() while waiting for mh_mutex.
8310Sstevel@tonic-gate */
8320Sstevel@tonic-gate if (mhp->mh_state == MHND_FREE) {
8330Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
8340Sstevel@tonic-gate continue;
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate break;
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate mutex_exit(&mem_handle_list_mutex);
8400Sstevel@tonic-gate return (mhp);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate
8430Sstevel@tonic-gate int
kphysm_del_gethandle(memhandle_t * xmhp)8440Sstevel@tonic-gate kphysm_del_gethandle(memhandle_t *xmhp)
8450Sstevel@tonic-gate {
8460Sstevel@tonic-gate struct mem_handle *mhp;
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate mhp = kphysm_allocate_mem_handle();
8490Sstevel@tonic-gate /*
8500Sstevel@tonic-gate * The handle is allocated using KM_SLEEP, so cannot fail.
8510Sstevel@tonic-gate * If the implementation is changed, the correct error to return
8520Sstevel@tonic-gate * here would be KPHYSM_ENOHANDLES.
8530Sstevel@tonic-gate */
8540Sstevel@tonic-gate ASSERT(mhp->mh_state == MHND_FREE);
8550Sstevel@tonic-gate mhp->mh_state = MHND_INIT;
8560Sstevel@tonic-gate *xmhp = mhp->mh_exthandle;
8570Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
8580Sstevel@tonic-gate return (KPHYSM_OK);
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate static int
overlapping(pfn_t b1,pgcnt_t l1,pfn_t b2,pgcnt_t l2)8620Sstevel@tonic-gate overlapping(pfn_t b1, pgcnt_t l1, pfn_t b2, pgcnt_t l2)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate pfn_t e1, e2;
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate e1 = b1 + l1;
8670Sstevel@tonic-gate e2 = b2 + l2;
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate return (!(b2 >= e1 || b1 >= e2));
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate static int can_remove_pgs(pgcnt_t);
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate static struct memdelspan *
span_to_install(pfn_t base,pgcnt_t npgs)8750Sstevel@tonic-gate span_to_install(pfn_t base, pgcnt_t npgs)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate struct memdelspan *mdsp;
8780Sstevel@tonic-gate struct memdelspan *mdsp_new;
8790Sstevel@tonic-gate uint64_t address, size, thislen;
8800Sstevel@tonic-gate struct memlist *mlp;
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate mdsp_new = NULL;
8830Sstevel@tonic-gate
8840Sstevel@tonic-gate address = (uint64_t)base << PAGESHIFT;
8850Sstevel@tonic-gate size = (uint64_t)npgs << PAGESHIFT;
8860Sstevel@tonic-gate while (size != 0) {
8870Sstevel@tonic-gate memlist_read_lock();
88811474SJonathan.Adams@Sun.COM for (mlp = phys_install; mlp != NULL; mlp = mlp->ml_next) {
88911474SJonathan.Adams@Sun.COM if (address >= (mlp->ml_address + mlp->ml_size))
8900Sstevel@tonic-gate continue;
89111474SJonathan.Adams@Sun.COM if ((address + size) > mlp->ml_address)
8920Sstevel@tonic-gate break;
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate if (mlp == NULL) {
8950Sstevel@tonic-gate address += size;
8960Sstevel@tonic-gate size = 0;
8970Sstevel@tonic-gate thislen = 0;
8980Sstevel@tonic-gate } else {
89911474SJonathan.Adams@Sun.COM if (address < mlp->ml_address) {
90011474SJonathan.Adams@Sun.COM size -= (mlp->ml_address - address);
90111474SJonathan.Adams@Sun.COM address = mlp->ml_address;
9020Sstevel@tonic-gate }
90311474SJonathan.Adams@Sun.COM ASSERT(address >= mlp->ml_address);
90411474SJonathan.Adams@Sun.COM if ((address + size) >
90511474SJonathan.Adams@Sun.COM (mlp->ml_address + mlp->ml_size)) {
90611474SJonathan.Adams@Sun.COM thislen =
90711474SJonathan.Adams@Sun.COM mlp->ml_size - (address - mlp->ml_address);
9080Sstevel@tonic-gate } else {
9090Sstevel@tonic-gate thislen = size;
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate memlist_read_unlock();
9130Sstevel@tonic-gate /* TODO: phys_install could change now */
9140Sstevel@tonic-gate if (thislen == 0)
9150Sstevel@tonic-gate continue;
9160Sstevel@tonic-gate mdsp = kmem_zalloc(sizeof (struct memdelspan), KM_SLEEP);
9170Sstevel@tonic-gate mdsp->mds_base = btop(address);
9180Sstevel@tonic-gate mdsp->mds_npgs = btop(thislen);
9190Sstevel@tonic-gate mdsp->mds_next = mdsp_new;
9200Sstevel@tonic-gate mdsp_new = mdsp;
9210Sstevel@tonic-gate address += thislen;
9220Sstevel@tonic-gate size -= thislen;
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate return (mdsp_new);
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate static void
free_delspans(struct memdelspan * mdsp)9280Sstevel@tonic-gate free_delspans(struct memdelspan *mdsp)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate struct memdelspan *amdsp;
9310Sstevel@tonic-gate
9320Sstevel@tonic-gate while ((amdsp = mdsp) != NULL) {
9330Sstevel@tonic-gate mdsp = amdsp->mds_next;
9340Sstevel@tonic-gate kmem_free(amdsp, sizeof (struct memdelspan));
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate /*
9390Sstevel@tonic-gate * Concatenate lists. No list ordering is required.
9400Sstevel@tonic-gate */
9410Sstevel@tonic-gate
9420Sstevel@tonic-gate static void
delspan_concat(struct memdelspan ** mdspp,struct memdelspan * mdsp)9430Sstevel@tonic-gate delspan_concat(struct memdelspan **mdspp, struct memdelspan *mdsp)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate while (*mdspp != NULL)
9460Sstevel@tonic-gate mdspp = &(*mdspp)->mds_next;
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate *mdspp = mdsp;
9490Sstevel@tonic-gate }
9500Sstevel@tonic-gate
9510Sstevel@tonic-gate /*
9520Sstevel@tonic-gate * Given a new list of delspans, check there is no overlap with
9530Sstevel@tonic-gate * all existing span activity (add or delete) and then concatenate
9540Sstevel@tonic-gate * the new spans to the given list.
9550Sstevel@tonic-gate * Return 1 for OK, 0 if overlapping.
9560Sstevel@tonic-gate */
9570Sstevel@tonic-gate static int
delspan_insert(struct transit_list * my_tlp,struct memdelspan * mdsp_new)9580Sstevel@tonic-gate delspan_insert(
9590Sstevel@tonic-gate struct transit_list *my_tlp,
9600Sstevel@tonic-gate struct memdelspan *mdsp_new)
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate struct transit_list_head *trh;
9630Sstevel@tonic-gate struct transit_list *tlp;
9640Sstevel@tonic-gate int ret;
9650Sstevel@tonic-gate
9660Sstevel@tonic-gate trh = &transit_list_head;
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate ASSERT(my_tlp != NULL);
9690Sstevel@tonic-gate ASSERT(mdsp_new != NULL);
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate ret = 1;
9720Sstevel@tonic-gate mutex_enter(&trh->trh_lock);
9730Sstevel@tonic-gate /* ASSERT(my_tlp->trl_spans == NULL || tlp_in_list(trh, my_tlp)); */
9740Sstevel@tonic-gate for (tlp = trh->trh_head; tlp != NULL; tlp = tlp->trl_next) {
9750Sstevel@tonic-gate struct memdelspan *mdsp;
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate for (mdsp = tlp->trl_spans; mdsp != NULL;
9780Sstevel@tonic-gate mdsp = mdsp->mds_next) {
9790Sstevel@tonic-gate struct memdelspan *nmdsp;
9800Sstevel@tonic-gate
9810Sstevel@tonic-gate for (nmdsp = mdsp_new; nmdsp != NULL;
9820Sstevel@tonic-gate nmdsp = nmdsp->mds_next) {
9830Sstevel@tonic-gate if (overlapping(mdsp->mds_base, mdsp->mds_npgs,
9840Sstevel@tonic-gate nmdsp->mds_base, nmdsp->mds_npgs)) {
9850Sstevel@tonic-gate ret = 0;
9860Sstevel@tonic-gate goto done;
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate done:
9920Sstevel@tonic-gate if (ret != 0) {
9930Sstevel@tonic-gate if (my_tlp->trl_spans == NULL)
9940Sstevel@tonic-gate transit_list_insert(my_tlp);
9950Sstevel@tonic-gate delspan_concat(&my_tlp->trl_spans, mdsp_new);
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate mutex_exit(&trh->trh_lock);
9980Sstevel@tonic-gate return (ret);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate
10010Sstevel@tonic-gate static void
delspan_remove(struct transit_list * my_tlp,pfn_t base,pgcnt_t npgs)10020Sstevel@tonic-gate delspan_remove(
10030Sstevel@tonic-gate struct transit_list *my_tlp,
10040Sstevel@tonic-gate pfn_t base,
10050Sstevel@tonic-gate pgcnt_t npgs)
10060Sstevel@tonic-gate {
10070Sstevel@tonic-gate struct transit_list_head *trh;
10080Sstevel@tonic-gate struct memdelspan *mdsp;
10090Sstevel@tonic-gate
10100Sstevel@tonic-gate trh = &transit_list_head;
10110Sstevel@tonic-gate
10120Sstevel@tonic-gate ASSERT(my_tlp != NULL);
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate mutex_enter(&trh->trh_lock);
10150Sstevel@tonic-gate if ((mdsp = my_tlp->trl_spans) != NULL) {
10160Sstevel@tonic-gate if (npgs == 0) {
10170Sstevel@tonic-gate my_tlp->trl_spans = NULL;
10180Sstevel@tonic-gate free_delspans(mdsp);
10190Sstevel@tonic-gate transit_list_remove(my_tlp);
10200Sstevel@tonic-gate } else {
10210Sstevel@tonic-gate struct memdelspan **prv;
10220Sstevel@tonic-gate
10230Sstevel@tonic-gate prv = &my_tlp->trl_spans;
10240Sstevel@tonic-gate while (mdsp != NULL) {
10250Sstevel@tonic-gate pfn_t p_end;
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate p_end = mdsp->mds_base + mdsp->mds_npgs;
10280Sstevel@tonic-gate if (mdsp->mds_base >= base &&
10290Sstevel@tonic-gate p_end <= (base + npgs)) {
10300Sstevel@tonic-gate *prv = mdsp->mds_next;
10310Sstevel@tonic-gate mdsp->mds_next = NULL;
10320Sstevel@tonic-gate free_delspans(mdsp);
10330Sstevel@tonic-gate } else {
10340Sstevel@tonic-gate prv = &mdsp->mds_next;
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate mdsp = *prv;
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate if (my_tlp->trl_spans == NULL)
10390Sstevel@tonic-gate transit_list_remove(my_tlp);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate mutex_exit(&trh->trh_lock);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate
10450Sstevel@tonic-gate /*
10460Sstevel@tonic-gate * Reserve interface for add to stop delete before add finished.
10470Sstevel@tonic-gate * This list is only accessed through the delspan_insert/remove
10480Sstevel@tonic-gate * functions and so is fully protected by the mutex in struct transit_list.
10490Sstevel@tonic-gate */
10500Sstevel@tonic-gate
10510Sstevel@tonic-gate static struct transit_list reserve_transit;
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate static int
delspan_reserve(pfn_t base,pgcnt_t npgs)10540Sstevel@tonic-gate delspan_reserve(pfn_t base, pgcnt_t npgs)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate struct memdelspan *mdsp;
10570Sstevel@tonic-gate int ret;
10580Sstevel@tonic-gate
10590Sstevel@tonic-gate mdsp = kmem_zalloc(sizeof (struct memdelspan), KM_SLEEP);
10600Sstevel@tonic-gate mdsp->mds_base = base;
10610Sstevel@tonic-gate mdsp->mds_npgs = npgs;
10620Sstevel@tonic-gate if ((ret = delspan_insert(&reserve_transit, mdsp)) == 0) {
10630Sstevel@tonic-gate free_delspans(mdsp);
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate return (ret);
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate static void
delspan_unreserve(pfn_t base,pgcnt_t npgs)10690Sstevel@tonic-gate delspan_unreserve(pfn_t base, pgcnt_t npgs)
10700Sstevel@tonic-gate {
10710Sstevel@tonic-gate delspan_remove(&reserve_transit, base, npgs);
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate /*
10750Sstevel@tonic-gate * Return whether memseg was created by kphysm_add_memory_dynamic().
10760Sstevel@tonic-gate */
10770Sstevel@tonic-gate static int
memseg_is_dynamic(struct memseg * seg)107810106SJason.Beloro@Sun.COM memseg_is_dynamic(struct memseg *seg)
10790Sstevel@tonic-gate {
108010106SJason.Beloro@Sun.COM return (seg->msegflags & MEMSEG_DYNAMIC);
10810Sstevel@tonic-gate }
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate int
kphysm_del_span(memhandle_t handle,pfn_t base,pgcnt_t npgs)10840Sstevel@tonic-gate kphysm_del_span(
10850Sstevel@tonic-gate memhandle_t handle,
10860Sstevel@tonic-gate pfn_t base,
10870Sstevel@tonic-gate pgcnt_t npgs)
10880Sstevel@tonic-gate {
10890Sstevel@tonic-gate struct mem_handle *mhp;
10900Sstevel@tonic-gate struct memseg *seg;
10910Sstevel@tonic-gate struct memdelspan *mdsp;
10920Sstevel@tonic-gate struct memdelspan *mdsp_new;
10930Sstevel@tonic-gate pgcnt_t phys_pages, vm_pages;
10940Sstevel@tonic-gate pfn_t p_end;
10950Sstevel@tonic-gate page_t *pp;
10960Sstevel@tonic-gate int ret;
10970Sstevel@tonic-gate
10980Sstevel@tonic-gate mhp = kphysm_lookup_mem_handle(handle);
10990Sstevel@tonic-gate if (mhp == NULL) {
11000Sstevel@tonic-gate return (KPHYSM_EHANDLE);
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate if (mhp->mh_state != MHND_INIT) {
11030Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
11040Sstevel@tonic-gate return (KPHYSM_ESEQUENCE);
11050Sstevel@tonic-gate }
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate /*
11080Sstevel@tonic-gate * Intersect the span with the installed memory list (phys_install).
11090Sstevel@tonic-gate */
11100Sstevel@tonic-gate mdsp_new = span_to_install(base, npgs);
11110Sstevel@tonic-gate if (mdsp_new == NULL) {
11120Sstevel@tonic-gate /*
11130Sstevel@tonic-gate * No physical memory in this range. Is this an
11140Sstevel@tonic-gate * error? If an attempt to start the delete is made
11150Sstevel@tonic-gate * for OK returns from del_span such as this, start will
11160Sstevel@tonic-gate * return an error.
11170Sstevel@tonic-gate * Could return KPHYSM_ENOWORK.
11180Sstevel@tonic-gate */
11190Sstevel@tonic-gate /*
11200Sstevel@tonic-gate * It is assumed that there are no error returns
11210Sstevel@tonic-gate * from span_to_install() due to kmem_alloc failure.
11220Sstevel@tonic-gate */
11230Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
11240Sstevel@tonic-gate return (KPHYSM_OK);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate /*
11270Sstevel@tonic-gate * Does this span overlap an existing span?
11280Sstevel@tonic-gate */
11290Sstevel@tonic-gate if (delspan_insert(&mhp->mh_transit, mdsp_new) == 0) {
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate * Differentiate between already on list for this handle
11320Sstevel@tonic-gate * (KPHYSM_EDUP) and busy elsewhere (KPHYSM_EBUSY).
11330Sstevel@tonic-gate */
11340Sstevel@tonic-gate ret = KPHYSM_EBUSY;
11350Sstevel@tonic-gate for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
11360Sstevel@tonic-gate mdsp = mdsp->mds_next) {
11370Sstevel@tonic-gate if (overlapping(mdsp->mds_base, mdsp->mds_npgs,
11380Sstevel@tonic-gate base, npgs)) {
11390Sstevel@tonic-gate ret = KPHYSM_EDUP;
11400Sstevel@tonic-gate break;
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
11440Sstevel@tonic-gate free_delspans(mdsp_new);
11450Sstevel@tonic-gate return (ret);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate * At this point the spans in mdsp_new have been inserted into the
11490Sstevel@tonic-gate * list of spans for this handle and thereby to the global list of
11500Sstevel@tonic-gate * spans being processed. Each of these spans must now be checked
11510Sstevel@tonic-gate * for relocatability. As a side-effect segments in the memseg list
11520Sstevel@tonic-gate * may be split.
11530Sstevel@tonic-gate *
11540Sstevel@tonic-gate * Note that mdsp_new can no longer be used as it is now part of
11550Sstevel@tonic-gate * a larger list. Select elements of this larger list based
11560Sstevel@tonic-gate * on base and npgs.
11570Sstevel@tonic-gate */
11580Sstevel@tonic-gate restart:
11590Sstevel@tonic-gate phys_pages = 0;
11600Sstevel@tonic-gate vm_pages = 0;
11610Sstevel@tonic-gate ret = KPHYSM_OK;
11620Sstevel@tonic-gate for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
11630Sstevel@tonic-gate mdsp = mdsp->mds_next) {
11640Sstevel@tonic-gate pgcnt_t pages_checked;
11650Sstevel@tonic-gate
11660Sstevel@tonic-gate if (!overlapping(mdsp->mds_base, mdsp->mds_npgs, base, npgs)) {
11670Sstevel@tonic-gate continue;
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate p_end = mdsp->mds_base + mdsp->mds_npgs;
11700Sstevel@tonic-gate /*
11710Sstevel@tonic-gate * The pages_checked count is a hack. All pages should be
11720Sstevel@tonic-gate * checked for relocatability. Those not covered by memsegs
11730Sstevel@tonic-gate * should be tested with arch_kphysm_del_span_ok().
11740Sstevel@tonic-gate */
11750Sstevel@tonic-gate pages_checked = 0;
11760Sstevel@tonic-gate for (seg = memsegs; seg; seg = seg->next) {
11770Sstevel@tonic-gate pfn_t mseg_start;
11780Sstevel@tonic-gate
11790Sstevel@tonic-gate if (seg->pages_base >= p_end ||
11800Sstevel@tonic-gate seg->pages_end <= mdsp->mds_base) {
11810Sstevel@tonic-gate /* Span and memseg don't overlap. */
11820Sstevel@tonic-gate continue;
11830Sstevel@tonic-gate }
118410106SJason.Beloro@Sun.COM mseg_start = memseg_get_start(seg);
11850Sstevel@tonic-gate /* Check that segment is suitable for delete. */
118610106SJason.Beloro@Sun.COM if (memseg_includes_meta(seg)) {
11870Sstevel@tonic-gate /*
118810106SJason.Beloro@Sun.COM * Check that this segment is completely
118910106SJason.Beloro@Sun.COM * within the span.
11900Sstevel@tonic-gate */
11910Sstevel@tonic-gate if (mseg_start < mdsp->mds_base ||
11920Sstevel@tonic-gate seg->pages_end > p_end) {
11930Sstevel@tonic-gate ret = KPHYSM_EBUSY;
11940Sstevel@tonic-gate break;
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate pages_checked += seg->pages_end - mseg_start;
11970Sstevel@tonic-gate } else {
11980Sstevel@tonic-gate /*
11990Sstevel@tonic-gate * If this segment is larger than the span,
12000Sstevel@tonic-gate * try to split it. After the split, it
12010Sstevel@tonic-gate * is necessary to restart.
12020Sstevel@tonic-gate */
12030Sstevel@tonic-gate if (seg->pages_base < mdsp->mds_base ||
12040Sstevel@tonic-gate seg->pages_end > p_end) {
12050Sstevel@tonic-gate pfn_t abase;
12060Sstevel@tonic-gate pgcnt_t anpgs;
12070Sstevel@tonic-gate int s_ret;
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate /* Split required. */
12100Sstevel@tonic-gate if (mdsp->mds_base < seg->pages_base)
12110Sstevel@tonic-gate abase = seg->pages_base;
12120Sstevel@tonic-gate else
12130Sstevel@tonic-gate abase = mdsp->mds_base;
12140Sstevel@tonic-gate if (p_end > seg->pages_end)
12150Sstevel@tonic-gate anpgs = seg->pages_end - abase;
12160Sstevel@tonic-gate else
12170Sstevel@tonic-gate anpgs = p_end - abase;
12180Sstevel@tonic-gate s_ret = kphysm_split_memseg(abase,
12190Sstevel@tonic-gate anpgs);
12200Sstevel@tonic-gate if (s_ret == 0) {
12210Sstevel@tonic-gate /* Split failed. */
12220Sstevel@tonic-gate ret = KPHYSM_ERESOURCE;
12230Sstevel@tonic-gate break;
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate goto restart;
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate pages_checked +=
12280Sstevel@tonic-gate seg->pages_end - seg->pages_base;
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate /*
12310Sstevel@tonic-gate * The memseg is wholly within the delete span.
12320Sstevel@tonic-gate * The individual pages can now be checked.
12330Sstevel@tonic-gate */
12340Sstevel@tonic-gate /* Cage test. */
12350Sstevel@tonic-gate for (pp = seg->pages; pp < seg->epages; pp++) {
12360Sstevel@tonic-gate if (PP_ISNORELOC(pp)) {
12370Sstevel@tonic-gate ret = KPHYSM_ENONRELOC;
12380Sstevel@tonic-gate break;
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate if (ret != KPHYSM_OK) {
12420Sstevel@tonic-gate break;
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate phys_pages += (seg->pages_end - mseg_start);
12450Sstevel@tonic-gate vm_pages += MSEG_NPAGES(seg);
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate if (ret != KPHYSM_OK)
12480Sstevel@tonic-gate break;
12490Sstevel@tonic-gate if (pages_checked != mdsp->mds_npgs) {
12500Sstevel@tonic-gate ret = KPHYSM_ENONRELOC;
12510Sstevel@tonic-gate break;
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate
12550Sstevel@tonic-gate if (ret == KPHYSM_OK) {
12560Sstevel@tonic-gate mhp->mh_phys_pages += phys_pages;
12570Sstevel@tonic-gate mhp->mh_vm_pages += vm_pages;
12580Sstevel@tonic-gate } else {
12590Sstevel@tonic-gate /*
12600Sstevel@tonic-gate * Keep holding the mh_mutex to prevent it going away.
12610Sstevel@tonic-gate */
12620Sstevel@tonic-gate delspan_remove(&mhp->mh_transit, base, npgs);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
12650Sstevel@tonic-gate return (ret);
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate
12680Sstevel@tonic-gate int
kphysm_del_span_query(pfn_t base,pgcnt_t npgs,memquery_t * mqp)12690Sstevel@tonic-gate kphysm_del_span_query(
12700Sstevel@tonic-gate pfn_t base,
12710Sstevel@tonic-gate pgcnt_t npgs,
12720Sstevel@tonic-gate memquery_t *mqp)
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate struct memdelspan *mdsp;
12750Sstevel@tonic-gate struct memdelspan *mdsp_new;
12760Sstevel@tonic-gate int done_first_nonreloc;
12770Sstevel@tonic-gate
12780Sstevel@tonic-gate mqp->phys_pages = 0;
12790Sstevel@tonic-gate mqp->managed = 0;
12800Sstevel@tonic-gate mqp->nonrelocatable = 0;
12810Sstevel@tonic-gate mqp->first_nonrelocatable = 0;
12820Sstevel@tonic-gate mqp->last_nonrelocatable = 0;
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate mdsp_new = span_to_install(base, npgs);
12850Sstevel@tonic-gate /*
12860Sstevel@tonic-gate * It is OK to proceed here if mdsp_new == NULL.
12870Sstevel@tonic-gate */
12880Sstevel@tonic-gate done_first_nonreloc = 0;
12890Sstevel@tonic-gate for (mdsp = mdsp_new; mdsp != NULL; mdsp = mdsp->mds_next) {
12900Sstevel@tonic-gate pfn_t sbase;
12910Sstevel@tonic-gate pgcnt_t snpgs;
12920Sstevel@tonic-gate
12930Sstevel@tonic-gate mqp->phys_pages += mdsp->mds_npgs;
12940Sstevel@tonic-gate sbase = mdsp->mds_base;
12950Sstevel@tonic-gate snpgs = mdsp->mds_npgs;
12960Sstevel@tonic-gate while (snpgs != 0) {
12970Sstevel@tonic-gate struct memseg *lseg, *seg;
12980Sstevel@tonic-gate pfn_t p_end;
12990Sstevel@tonic-gate page_t *pp;
13000Sstevel@tonic-gate pfn_t mseg_start;
13010Sstevel@tonic-gate
13020Sstevel@tonic-gate p_end = sbase + snpgs;
13030Sstevel@tonic-gate /*
13040Sstevel@tonic-gate * Find the lowest addressed memseg that starts
13050Sstevel@tonic-gate * after sbase and account for it.
13060Sstevel@tonic-gate * This is to catch dynamic memsegs whose start
13070Sstevel@tonic-gate * is hidden.
13080Sstevel@tonic-gate */
13090Sstevel@tonic-gate seg = NULL;
13100Sstevel@tonic-gate for (lseg = memsegs; lseg != NULL; lseg = lseg->next) {
13110Sstevel@tonic-gate if ((lseg->pages_base >= sbase) ||
13120Sstevel@tonic-gate (lseg->pages_base < p_end &&
13130Sstevel@tonic-gate lseg->pages_end > sbase)) {
13140Sstevel@tonic-gate if (seg == NULL ||
13150Sstevel@tonic-gate seg->pages_base > lseg->pages_base)
13160Sstevel@tonic-gate seg = lseg;
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate if (seg != NULL) {
132010106SJason.Beloro@Sun.COM mseg_start = memseg_get_start(seg);
13210Sstevel@tonic-gate /*
13220Sstevel@tonic-gate * Now have the full extent of the memseg so
13230Sstevel@tonic-gate * do the range check.
13240Sstevel@tonic-gate */
13250Sstevel@tonic-gate if (mseg_start >= p_end ||
13260Sstevel@tonic-gate seg->pages_end <= sbase) {
13270Sstevel@tonic-gate /* Span does not overlap memseg. */
13280Sstevel@tonic-gate seg = NULL;
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate /*
13320Sstevel@tonic-gate * Account for gap either before the segment if
13330Sstevel@tonic-gate * there is one or to the end of the span.
13340Sstevel@tonic-gate */
13350Sstevel@tonic-gate if (seg == NULL || mseg_start > sbase) {
13360Sstevel@tonic-gate pfn_t a_end;
13370Sstevel@tonic-gate
13380Sstevel@tonic-gate a_end = (seg == NULL) ? p_end : mseg_start;
13390Sstevel@tonic-gate /*
13400Sstevel@tonic-gate * Check with arch layer for relocatability.
13410Sstevel@tonic-gate */
13420Sstevel@tonic-gate if (arch_kphysm_del_span_ok(sbase,
13430Sstevel@tonic-gate (a_end - sbase))) {
13440Sstevel@tonic-gate /*
13450Sstevel@tonic-gate * No non-relocatble pages in this
13460Sstevel@tonic-gate * area, avoid the fine-grained
13470Sstevel@tonic-gate * test.
13480Sstevel@tonic-gate */
13490Sstevel@tonic-gate snpgs -= (a_end - sbase);
13500Sstevel@tonic-gate sbase = a_end;
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate while (sbase < a_end) {
13530Sstevel@tonic-gate if (!arch_kphysm_del_span_ok(sbase,
13540Sstevel@tonic-gate 1)) {
13550Sstevel@tonic-gate mqp->nonrelocatable++;
13560Sstevel@tonic-gate if (!done_first_nonreloc) {
13570Sstevel@tonic-gate mqp->
13580Sstevel@tonic-gate first_nonrelocatable
13590Sstevel@tonic-gate = sbase;
13600Sstevel@tonic-gate done_first_nonreloc = 1;
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate mqp->last_nonrelocatable =
13630Sstevel@tonic-gate sbase;
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate sbase++;
13660Sstevel@tonic-gate snpgs--;
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate }
13690Sstevel@tonic-gate if (seg != NULL) {
13700Sstevel@tonic-gate ASSERT(mseg_start <= sbase);
13710Sstevel@tonic-gate if (seg->pages_base != mseg_start &&
13720Sstevel@tonic-gate seg->pages_base > sbase) {
13730Sstevel@tonic-gate pgcnt_t skip_pgs;
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate /*
13760Sstevel@tonic-gate * Skip the page_t area of a
13770Sstevel@tonic-gate * dynamic memseg.
13780Sstevel@tonic-gate */
13790Sstevel@tonic-gate skip_pgs = seg->pages_base - sbase;
13800Sstevel@tonic-gate if (snpgs <= skip_pgs) {
13810Sstevel@tonic-gate sbase += snpgs;
13820Sstevel@tonic-gate snpgs = 0;
13830Sstevel@tonic-gate continue;
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate snpgs -= skip_pgs;
13860Sstevel@tonic-gate sbase += skip_pgs;
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate ASSERT(snpgs != 0);
13890Sstevel@tonic-gate ASSERT(seg->pages_base <= sbase);
13900Sstevel@tonic-gate /*
13910Sstevel@tonic-gate * The individual pages can now be checked.
13920Sstevel@tonic-gate */
13930Sstevel@tonic-gate for (pp = seg->pages +
13940Sstevel@tonic-gate (sbase - seg->pages_base);
13950Sstevel@tonic-gate snpgs != 0 && pp < seg->epages; pp++) {
13960Sstevel@tonic-gate mqp->managed++;
13970Sstevel@tonic-gate if (PP_ISNORELOC(pp)) {
13980Sstevel@tonic-gate mqp->nonrelocatable++;
13990Sstevel@tonic-gate if (!done_first_nonreloc) {
14000Sstevel@tonic-gate mqp->
14010Sstevel@tonic-gate first_nonrelocatable
14020Sstevel@tonic-gate = sbase;
14030Sstevel@tonic-gate done_first_nonreloc = 1;
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate mqp->last_nonrelocatable =
14060Sstevel@tonic-gate sbase;
14070Sstevel@tonic-gate }
14080Sstevel@tonic-gate sbase++;
14090Sstevel@tonic-gate snpgs--;
14100Sstevel@tonic-gate }
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate free_delspans(mdsp_new);
14160Sstevel@tonic-gate
14170Sstevel@tonic-gate return (KPHYSM_OK);
14180Sstevel@tonic-gate }
14190Sstevel@tonic-gate
14200Sstevel@tonic-gate /*
14210Sstevel@tonic-gate * This release function can be called at any stage as follows:
14220Sstevel@tonic-gate * _gethandle only called
14230Sstevel@tonic-gate * _span(s) only called
14240Sstevel@tonic-gate * _start called but failed
14250Sstevel@tonic-gate * delete thread exited
14260Sstevel@tonic-gate */
14270Sstevel@tonic-gate int
kphysm_del_release(memhandle_t handle)14280Sstevel@tonic-gate kphysm_del_release(memhandle_t handle)
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate struct mem_handle *mhp;
14310Sstevel@tonic-gate
14320Sstevel@tonic-gate mhp = kphysm_lookup_mem_handle(handle);
14330Sstevel@tonic-gate if (mhp == NULL) {
14340Sstevel@tonic-gate return (KPHYSM_EHANDLE);
14350Sstevel@tonic-gate }
14360Sstevel@tonic-gate switch (mhp->mh_state) {
14370Sstevel@tonic-gate case MHND_STARTING:
14380Sstevel@tonic-gate case MHND_RUNNING:
14390Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
14400Sstevel@tonic-gate return (KPHYSM_ENOTFINISHED);
14410Sstevel@tonic-gate case MHND_FREE:
14420Sstevel@tonic-gate ASSERT(mhp->mh_state != MHND_FREE);
14430Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
14440Sstevel@tonic-gate return (KPHYSM_EHANDLE);
14450Sstevel@tonic-gate case MHND_INIT:
14460Sstevel@tonic-gate break;
14470Sstevel@tonic-gate case MHND_DONE:
14480Sstevel@tonic-gate break;
14490Sstevel@tonic-gate case MHND_RELEASE:
14500Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
14510Sstevel@tonic-gate return (KPHYSM_ESEQUENCE);
14520Sstevel@tonic-gate default:
14530Sstevel@tonic-gate #ifdef DEBUG
14540Sstevel@tonic-gate cmn_err(CE_WARN, "kphysm_del_release(0x%p) state corrupt %d",
14550Sstevel@tonic-gate (void *)mhp, mhp->mh_state);
14560Sstevel@tonic-gate #endif /* DEBUG */
14570Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
14580Sstevel@tonic-gate return (KPHYSM_EHANDLE);
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate /*
14610Sstevel@tonic-gate * Set state so that we can wait if necessary.
14620Sstevel@tonic-gate * Also this means that we have read/write access to all
14630Sstevel@tonic-gate * fields except mh_exthandle and mh_state.
14640Sstevel@tonic-gate */
14650Sstevel@tonic-gate mhp->mh_state = MHND_RELEASE;
14660Sstevel@tonic-gate /*
14670Sstevel@tonic-gate * The mem_handle cannot be de-allocated by any other operation
14680Sstevel@tonic-gate * now, so no need to hold mh_mutex.
14690Sstevel@tonic-gate */
14700Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
14710Sstevel@tonic-gate
14720Sstevel@tonic-gate delspan_remove(&mhp->mh_transit, 0, 0);
14730Sstevel@tonic-gate mhp->mh_phys_pages = 0;
14740Sstevel@tonic-gate mhp->mh_vm_pages = 0;
14750Sstevel@tonic-gate mhp->mh_hold_todo = 0;
14760Sstevel@tonic-gate mhp->mh_delete_complete = NULL;
14770Sstevel@tonic-gate mhp->mh_delete_complete_arg = NULL;
14780Sstevel@tonic-gate mhp->mh_cancel = 0;
14790Sstevel@tonic-gate
14800Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
14810Sstevel@tonic-gate ASSERT(mhp->mh_state == MHND_RELEASE);
14820Sstevel@tonic-gate mhp->mh_state = MHND_FREE;
14830Sstevel@tonic-gate
14840Sstevel@tonic-gate kphysm_free_mem_handle(mhp);
14850Sstevel@tonic-gate
14860Sstevel@tonic-gate return (KPHYSM_OK);
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate
14890Sstevel@tonic-gate /*
14900Sstevel@tonic-gate * This cancel function can only be called with the thread running.
14910Sstevel@tonic-gate */
14920Sstevel@tonic-gate int
kphysm_del_cancel(memhandle_t handle)14930Sstevel@tonic-gate kphysm_del_cancel(memhandle_t handle)
14940Sstevel@tonic-gate {
14950Sstevel@tonic-gate struct mem_handle *mhp;
14960Sstevel@tonic-gate
14970Sstevel@tonic-gate mhp = kphysm_lookup_mem_handle(handle);
14980Sstevel@tonic-gate if (mhp == NULL) {
14990Sstevel@tonic-gate return (KPHYSM_EHANDLE);
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate if (mhp->mh_state != MHND_STARTING && mhp->mh_state != MHND_RUNNING) {
15020Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
15030Sstevel@tonic-gate return (KPHYSM_ENOTRUNNING);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate /*
15060Sstevel@tonic-gate * Set the cancel flag and wake the delete thread up.
15070Sstevel@tonic-gate * The thread may be waiting on I/O, so the effect of the cancel
15080Sstevel@tonic-gate * may be delayed.
15090Sstevel@tonic-gate */
15100Sstevel@tonic-gate if (mhp->mh_cancel == 0) {
15110Sstevel@tonic-gate mhp->mh_cancel = KPHYSM_ECANCELLED;
15120Sstevel@tonic-gate cv_signal(&mhp->mh_cv);
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
15150Sstevel@tonic-gate return (KPHYSM_OK);
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate int
kphysm_del_status(memhandle_t handle,memdelstat_t * mdstp)15190Sstevel@tonic-gate kphysm_del_status(
15200Sstevel@tonic-gate memhandle_t handle,
15210Sstevel@tonic-gate memdelstat_t *mdstp)
15220Sstevel@tonic-gate {
15230Sstevel@tonic-gate struct mem_handle *mhp;
15240Sstevel@tonic-gate
15250Sstevel@tonic-gate mhp = kphysm_lookup_mem_handle(handle);
15260Sstevel@tonic-gate if (mhp == NULL) {
15270Sstevel@tonic-gate return (KPHYSM_EHANDLE);
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate /*
15300Sstevel@tonic-gate * Calling kphysm_del_status() is allowed before the delete
15310Sstevel@tonic-gate * is started to allow for status display.
15320Sstevel@tonic-gate */
15330Sstevel@tonic-gate if (mhp->mh_state != MHND_INIT && mhp->mh_state != MHND_STARTING &&
15340Sstevel@tonic-gate mhp->mh_state != MHND_RUNNING) {
15350Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
15360Sstevel@tonic-gate return (KPHYSM_ENOTRUNNING);
15370Sstevel@tonic-gate }
15380Sstevel@tonic-gate mdstp->phys_pages = mhp->mh_phys_pages;
15390Sstevel@tonic-gate mdstp->managed = mhp->mh_vm_pages;
15400Sstevel@tonic-gate mdstp->collected = mhp->mh_vm_pages - mhp->mh_hold_todo;
15410Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
15420Sstevel@tonic-gate return (KPHYSM_OK);
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate
15450Sstevel@tonic-gate static int mem_delete_additional_pages = 100;
15460Sstevel@tonic-gate
15470Sstevel@tonic-gate static int
can_remove_pgs(pgcnt_t npgs)15480Sstevel@tonic-gate can_remove_pgs(pgcnt_t npgs)
15490Sstevel@tonic-gate {
15500Sstevel@tonic-gate /*
15510Sstevel@tonic-gate * If all pageable pages were paged out, freemem would
15520Sstevel@tonic-gate * equal availrmem. There is a minimum requirement for
15530Sstevel@tonic-gate * availrmem.
15540Sstevel@tonic-gate */
15550Sstevel@tonic-gate if ((availrmem - (tune.t_minarmem + mem_delete_additional_pages))
15560Sstevel@tonic-gate < npgs)
15570Sstevel@tonic-gate return (0);
15580Sstevel@tonic-gate /* TODO: check swap space, etc. */
15590Sstevel@tonic-gate return (1);
15600Sstevel@tonic-gate }
15610Sstevel@tonic-gate
15620Sstevel@tonic-gate static int
get_availrmem(pgcnt_t npgs)15630Sstevel@tonic-gate get_availrmem(pgcnt_t npgs)
15640Sstevel@tonic-gate {
15650Sstevel@tonic-gate int ret;
15660Sstevel@tonic-gate
15670Sstevel@tonic-gate mutex_enter(&freemem_lock);
15680Sstevel@tonic-gate ret = can_remove_pgs(npgs);
15690Sstevel@tonic-gate if (ret != 0)
15700Sstevel@tonic-gate availrmem -= npgs;
15710Sstevel@tonic-gate mutex_exit(&freemem_lock);
15720Sstevel@tonic-gate return (ret);
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate
15750Sstevel@tonic-gate static void
put_availrmem(pgcnt_t npgs)15760Sstevel@tonic-gate put_availrmem(pgcnt_t npgs)
15770Sstevel@tonic-gate {
15780Sstevel@tonic-gate mutex_enter(&freemem_lock);
15790Sstevel@tonic-gate availrmem += npgs;
15800Sstevel@tonic-gate mutex_exit(&freemem_lock);
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate #define FREEMEM_INCR 100
15840Sstevel@tonic-gate static pgcnt_t freemem_incr = FREEMEM_INCR;
15850Sstevel@tonic-gate #define DEL_FREE_WAIT_FRAC 4
15860Sstevel@tonic-gate #define DEL_FREE_WAIT_TICKS ((hz+DEL_FREE_WAIT_FRAC-1)/DEL_FREE_WAIT_FRAC)
15870Sstevel@tonic-gate
15880Sstevel@tonic-gate #define DEL_BUSY_WAIT_FRAC 20
15890Sstevel@tonic-gate #define DEL_BUSY_WAIT_TICKS ((hz+DEL_BUSY_WAIT_FRAC-1)/DEL_BUSY_WAIT_FRAC)
15900Sstevel@tonic-gate
15910Sstevel@tonic-gate static void kphysm_del_cleanup(struct mem_handle *);
15920Sstevel@tonic-gate
15930Sstevel@tonic-gate static void page_delete_collect(page_t *, struct mem_handle *);
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate static pgcnt_t
delthr_get_freemem(struct mem_handle * mhp)15960Sstevel@tonic-gate delthr_get_freemem(struct mem_handle *mhp)
15970Sstevel@tonic-gate {
15980Sstevel@tonic-gate pgcnt_t free_get;
15990Sstevel@tonic-gate int ret;
16000Sstevel@tonic-gate
16010Sstevel@tonic-gate ASSERT(MUTEX_HELD(&mhp->mh_mutex));
16020Sstevel@tonic-gate
16030Sstevel@tonic-gate MDSTAT_INCR(mhp, need_free);
16040Sstevel@tonic-gate /*
16050Sstevel@tonic-gate * Get up to freemem_incr pages.
16060Sstevel@tonic-gate */
16070Sstevel@tonic-gate free_get = freemem_incr;
16080Sstevel@tonic-gate if (free_get > mhp->mh_hold_todo)
16090Sstevel@tonic-gate free_get = mhp->mh_hold_todo;
16100Sstevel@tonic-gate /*
16110Sstevel@tonic-gate * Take free_get pages away from freemem,
16120Sstevel@tonic-gate * waiting if necessary.
16130Sstevel@tonic-gate */
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate while (!mhp->mh_cancel) {
16160Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
16170Sstevel@tonic-gate MDSTAT_INCR(mhp, free_loop);
16180Sstevel@tonic-gate /*
16190Sstevel@tonic-gate * Duplicate test from page_create_throttle()
16200Sstevel@tonic-gate * but don't override with !PG_WAIT.
16210Sstevel@tonic-gate */
16220Sstevel@tonic-gate if (freemem < (free_get + throttlefree)) {
16230Sstevel@tonic-gate MDSTAT_INCR(mhp, free_low);
16240Sstevel@tonic-gate ret = 0;
16250Sstevel@tonic-gate } else {
16260Sstevel@tonic-gate ret = page_create_wait(free_get, 0);
16270Sstevel@tonic-gate if (ret == 0) {
16280Sstevel@tonic-gate /* EMPTY */
16290Sstevel@tonic-gate MDSTAT_INCR(mhp, free_failed);
16300Sstevel@tonic-gate }
16310Sstevel@tonic-gate }
16320Sstevel@tonic-gate if (ret != 0) {
16330Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
16340Sstevel@tonic-gate return (free_get);
16350Sstevel@tonic-gate }
16360Sstevel@tonic-gate
16370Sstevel@tonic-gate /*
16380Sstevel@tonic-gate * Put pressure on pageout.
16390Sstevel@tonic-gate */
16400Sstevel@tonic-gate page_needfree(free_get);
16410Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv);
16420Sstevel@tonic-gate
16430Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
164411066Srafael.vanoni@sun.com (void) cv_reltimedwait(&mhp->mh_cv, &mhp->mh_mutex,
164511066Srafael.vanoni@sun.com DEL_FREE_WAIT_TICKS, TR_CLOCK_TICK);
16460Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
16470Sstevel@tonic-gate page_needfree(-(spgcnt_t)free_get);
16480Sstevel@tonic-gate
16490Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
16500Sstevel@tonic-gate }
16510Sstevel@tonic-gate return (0);
16520Sstevel@tonic-gate }
16530Sstevel@tonic-gate
16540Sstevel@tonic-gate #define DR_AIO_CLEANUP_DELAY 25000 /* 0.025secs, in usec */
16550Sstevel@tonic-gate #define DR_AIO_CLEANUP_MAXLOOPS_NODELAY 100
16560Sstevel@tonic-gate /*
16570Sstevel@tonic-gate * This function is run as a helper thread for delete_memory_thread.
16580Sstevel@tonic-gate * It is needed in order to force kaio cleanup, so that pages used in kaio
16590Sstevel@tonic-gate * will be unlocked and subsequently relocated by delete_memory_thread.
16600Sstevel@tonic-gate * The address of the delete_memory_threads's mem_handle is passed in to
16610Sstevel@tonic-gate * this thread function, and is used to set the mh_aio_cleanup_done member
16620Sstevel@tonic-gate * prior to calling thread_exit().
16630Sstevel@tonic-gate */
16640Sstevel@tonic-gate static void
dr_aio_cleanup_thread(caddr_t amhp)16650Sstevel@tonic-gate dr_aio_cleanup_thread(caddr_t amhp)
16660Sstevel@tonic-gate {
16670Sstevel@tonic-gate proc_t *procp;
16680Sstevel@tonic-gate int (*aio_cleanup_dr_delete_memory)(proc_t *);
16690Sstevel@tonic-gate int cleaned;
16700Sstevel@tonic-gate int n = 0;
16710Sstevel@tonic-gate struct mem_handle *mhp;
16720Sstevel@tonic-gate volatile uint_t *pcancel;
16730Sstevel@tonic-gate
16740Sstevel@tonic-gate mhp = (struct mem_handle *)amhp;
16750Sstevel@tonic-gate ASSERT(mhp != NULL);
16760Sstevel@tonic-gate pcancel = &mhp->mh_dr_aio_cleanup_cancel;
16770Sstevel@tonic-gate if (modload("sys", "kaio") == -1) {
16780Sstevel@tonic-gate mhp->mh_aio_cleanup_done = 1;
16790Sstevel@tonic-gate cmn_err(CE_WARN, "dr_aio_cleanup_thread: cannot load kaio");
16800Sstevel@tonic-gate thread_exit();
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate aio_cleanup_dr_delete_memory = (int (*)(proc_t *))
16830Sstevel@tonic-gate modgetsymvalue("aio_cleanup_dr_delete_memory", 0);
16840Sstevel@tonic-gate if (aio_cleanup_dr_delete_memory == NULL) {
16850Sstevel@tonic-gate mhp->mh_aio_cleanup_done = 1;
16860Sstevel@tonic-gate cmn_err(CE_WARN,
16870Sstevel@tonic-gate "aio_cleanup_dr_delete_memory not found in kaio");
16880Sstevel@tonic-gate thread_exit();
16890Sstevel@tonic-gate }
16900Sstevel@tonic-gate do {
16910Sstevel@tonic-gate cleaned = 0;
16920Sstevel@tonic-gate mutex_enter(&pidlock);
16930Sstevel@tonic-gate for (procp = practive; (*pcancel == 0) && (procp != NULL);
16940Sstevel@tonic-gate procp = procp->p_next) {
16950Sstevel@tonic-gate mutex_enter(&procp->p_lock);
16960Sstevel@tonic-gate if (procp->p_aio != NULL) {
16970Sstevel@tonic-gate /* cleanup proc's outstanding kaio */
16980Sstevel@tonic-gate cleaned +=
16990Sstevel@tonic-gate (*aio_cleanup_dr_delete_memory)(procp);
17000Sstevel@tonic-gate }
17010Sstevel@tonic-gate mutex_exit(&procp->p_lock);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate mutex_exit(&pidlock);
17040Sstevel@tonic-gate if ((*pcancel == 0) &&
17050Sstevel@tonic-gate (!cleaned || (++n == DR_AIO_CLEANUP_MAXLOOPS_NODELAY))) {
17060Sstevel@tonic-gate /* delay a bit before retrying all procs again */
17070Sstevel@tonic-gate delay(drv_usectohz(DR_AIO_CLEANUP_DELAY));
17080Sstevel@tonic-gate n = 0;
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate } while (*pcancel == 0);
17110Sstevel@tonic-gate mhp->mh_aio_cleanup_done = 1;
17120Sstevel@tonic-gate thread_exit();
17130Sstevel@tonic-gate }
17140Sstevel@tonic-gate
17150Sstevel@tonic-gate static void
delete_memory_thread(caddr_t amhp)17160Sstevel@tonic-gate delete_memory_thread(caddr_t amhp)
17170Sstevel@tonic-gate {
17180Sstevel@tonic-gate struct mem_handle *mhp;
17190Sstevel@tonic-gate struct memdelspan *mdsp;
17200Sstevel@tonic-gate callb_cpr_t cprinfo;
17210Sstevel@tonic-gate page_t *pp_targ;
17220Sstevel@tonic-gate spgcnt_t freemem_left;
17230Sstevel@tonic-gate void (*del_complete_funcp)(void *, int error);
17240Sstevel@tonic-gate void *del_complete_arg;
17250Sstevel@tonic-gate int comp_code;
17260Sstevel@tonic-gate int ret;
17270Sstevel@tonic-gate int first_scan;
17280Sstevel@tonic-gate uint_t szc;
17290Sstevel@tonic-gate #ifdef MEM_DEL_STATS
17300Sstevel@tonic-gate uint64_t start_total, ntick_total;
17310Sstevel@tonic-gate uint64_t start_pgrp, ntick_pgrp;
17320Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
17330Sstevel@tonic-gate
17340Sstevel@tonic-gate mhp = (struct mem_handle *)amhp;
17350Sstevel@tonic-gate
17360Sstevel@tonic-gate #ifdef MEM_DEL_STATS
17370Sstevel@tonic-gate start_total = ddi_get_lbolt();
17380Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
17390Sstevel@tonic-gate
17400Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &mhp->mh_mutex,
17410Sstevel@tonic-gate callb_generic_cpr, "memdel");
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
17440Sstevel@tonic-gate ASSERT(mhp->mh_state == MHND_STARTING);
17450Sstevel@tonic-gate
17460Sstevel@tonic-gate mhp->mh_state = MHND_RUNNING;
17470Sstevel@tonic-gate mhp->mh_thread_id = curthread;
17480Sstevel@tonic-gate
17490Sstevel@tonic-gate mhp->mh_hold_todo = mhp->mh_vm_pages;
17500Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
17510Sstevel@tonic-gate
17520Sstevel@tonic-gate /* Allocate the remap pages now, if necessary. */
17530Sstevel@tonic-gate memseg_remap_init();
17540Sstevel@tonic-gate
17550Sstevel@tonic-gate /*
17560Sstevel@tonic-gate * Subtract from availrmem now if possible as availrmem
17570Sstevel@tonic-gate * may not be available by the end of the delete.
17580Sstevel@tonic-gate */
17590Sstevel@tonic-gate if (!get_availrmem(mhp->mh_vm_pages)) {
17600Sstevel@tonic-gate comp_code = KPHYSM_ENOTVIABLE;
17610Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
17620Sstevel@tonic-gate goto early_exit;
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate
17650Sstevel@tonic-gate ret = kphysm_setup_pre_del(mhp->mh_vm_pages);
17660Sstevel@tonic-gate
17670Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
17680Sstevel@tonic-gate
17690Sstevel@tonic-gate if (ret != 0) {
17700Sstevel@tonic-gate mhp->mh_cancel = KPHYSM_EREFUSED;
17710Sstevel@tonic-gate goto refused;
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate
17740Sstevel@tonic-gate transit_list_collect(mhp, 1);
17750Sstevel@tonic-gate
17760Sstevel@tonic-gate for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
17770Sstevel@tonic-gate mdsp = mdsp->mds_next) {
17780Sstevel@tonic-gate ASSERT(mdsp->mds_bitmap == NULL);
17790Sstevel@tonic-gate mdsp->mds_bitmap = kmem_zalloc(MDS_BITMAPBYTES(mdsp), KM_SLEEP);
17800Sstevel@tonic-gate mdsp->mds_bitmap_retired = kmem_zalloc(MDS_BITMAPBYTES(mdsp),
17816242Smb158278 KM_SLEEP);
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate
17840Sstevel@tonic-gate first_scan = 1;
17850Sstevel@tonic-gate freemem_left = 0;
17860Sstevel@tonic-gate /*
17870Sstevel@tonic-gate * Start dr_aio_cleanup_thread, which periodically iterates
17880Sstevel@tonic-gate * through the process list and invokes aio cleanup. This
17890Sstevel@tonic-gate * is needed in order to avoid a deadly embrace between the
17900Sstevel@tonic-gate * delete_memory_thread (waiting on writer lock for page, with the
17910Sstevel@tonic-gate * exclusive-wanted bit set), kaio read request threads (waiting for a
17920Sstevel@tonic-gate * reader lock on the same page that is wanted by the
17930Sstevel@tonic-gate * delete_memory_thread), and threads waiting for kaio completion
17940Sstevel@tonic-gate * (blocked on spt_amp->lock).
17950Sstevel@tonic-gate */
17960Sstevel@tonic-gate mhp->mh_dr_aio_cleanup_cancel = 0;
17970Sstevel@tonic-gate mhp->mh_aio_cleanup_done = 0;
17980Sstevel@tonic-gate (void) thread_create(NULL, 0, dr_aio_cleanup_thread,
17990Sstevel@tonic-gate (caddr_t)mhp, 0, &p0, TS_RUN, maxclsyspri - 1);
18000Sstevel@tonic-gate while ((mhp->mh_hold_todo != 0) && (mhp->mh_cancel == 0)) {
18010Sstevel@tonic-gate pgcnt_t collected;
18020Sstevel@tonic-gate
18030Sstevel@tonic-gate MDSTAT_INCR(mhp, nloop);
18040Sstevel@tonic-gate collected = 0;
18050Sstevel@tonic-gate for (mdsp = mhp->mh_transit.trl_spans; (mdsp != NULL) &&
18060Sstevel@tonic-gate (mhp->mh_cancel == 0); mdsp = mdsp->mds_next) {
18070Sstevel@tonic-gate pfn_t pfn, p_end;
18080Sstevel@tonic-gate
18090Sstevel@tonic-gate p_end = mdsp->mds_base + mdsp->mds_npgs;
18100Sstevel@tonic-gate for (pfn = mdsp->mds_base; (pfn < p_end) &&
18110Sstevel@tonic-gate (mhp->mh_cancel == 0); pfn++) {
18120Sstevel@tonic-gate page_t *pp, *tpp, *tpp_targ;
18130Sstevel@tonic-gate pgcnt_t bit;
18140Sstevel@tonic-gate struct vnode *vp;
18150Sstevel@tonic-gate u_offset_t offset;
18160Sstevel@tonic-gate int mod, result;
18170Sstevel@tonic-gate spgcnt_t pgcnt;
18180Sstevel@tonic-gate
18190Sstevel@tonic-gate bit = pfn - mdsp->mds_base;
18200Sstevel@tonic-gate if ((mdsp->mds_bitmap[bit / NBPBMW] &
18210Sstevel@tonic-gate (1 << (bit % NBPBMW))) != 0) {
18220Sstevel@tonic-gate MDSTAT_INCR(mhp, already_done);
18230Sstevel@tonic-gate continue;
18240Sstevel@tonic-gate }
18250Sstevel@tonic-gate if (freemem_left == 0) {
18260Sstevel@tonic-gate freemem_left += delthr_get_freemem(mhp);
18270Sstevel@tonic-gate if (freemem_left == 0)
18280Sstevel@tonic-gate break;
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate
18310Sstevel@tonic-gate /*
18320Sstevel@tonic-gate * Release mh_mutex - some of this
18330Sstevel@tonic-gate * stuff takes some time (eg PUTPAGE).
18340Sstevel@tonic-gate */
18350Sstevel@tonic-gate
18360Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
18370Sstevel@tonic-gate MDSTAT_INCR(mhp, ncheck);
18380Sstevel@tonic-gate
18390Sstevel@tonic-gate pp = page_numtopp_nolock(pfn);
18400Sstevel@tonic-gate if (pp == NULL) {
18410Sstevel@tonic-gate /*
18420Sstevel@tonic-gate * Not covered by a page_t - will
18430Sstevel@tonic-gate * be dealt with elsewhere.
18440Sstevel@tonic-gate */
18450Sstevel@tonic-gate MDSTAT_INCR(mhp, nopaget);
18460Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
18470Sstevel@tonic-gate mdsp->mds_bitmap[bit / NBPBMW] |=
18480Sstevel@tonic-gate (1 << (bit % NBPBMW));
18490Sstevel@tonic-gate continue;
18500Sstevel@tonic-gate }
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate if (!page_try_reclaim_lock(pp, SE_EXCL,
1853917Selowe SE_EXCL_WANTED | SE_RETIRED)) {
1854917Selowe /*
1855917Selowe * Page in use elsewhere. Skip it.
1856917Selowe */
1857917Selowe MDSTAT_INCR(mhp, lockfail);
1858917Selowe mutex_enter(&mhp->mh_mutex);
1859917Selowe continue;
18600Sstevel@tonic-gate }
18610Sstevel@tonic-gate /*
18620Sstevel@tonic-gate * See if the cage expanded into the delete.
18630Sstevel@tonic-gate * This can happen as we have to allow the
18640Sstevel@tonic-gate * cage to expand.
18650Sstevel@tonic-gate */
18660Sstevel@tonic-gate if (PP_ISNORELOC(pp)) {
1867917Selowe page_unlock(pp);
18680Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
18690Sstevel@tonic-gate mhp->mh_cancel = KPHYSM_ENONRELOC;
18700Sstevel@tonic-gate break;
18710Sstevel@tonic-gate }
1872917Selowe if (PP_RETIRED(pp)) {
18730Sstevel@tonic-gate /*
18740Sstevel@tonic-gate * Page has been retired and is
18750Sstevel@tonic-gate * not part of the cage so we
18760Sstevel@tonic-gate * can now do the accounting for
18770Sstevel@tonic-gate * it.
18780Sstevel@tonic-gate */
18790Sstevel@tonic-gate MDSTAT_INCR(mhp, retired);
18800Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
18810Sstevel@tonic-gate mdsp->mds_bitmap[bit / NBPBMW]
18820Sstevel@tonic-gate |= (1 << (bit % NBPBMW));
18830Sstevel@tonic-gate mdsp->mds_bitmap_retired[bit /
18840Sstevel@tonic-gate NBPBMW] |=
18850Sstevel@tonic-gate (1 << (bit % NBPBMW));
18860Sstevel@tonic-gate mhp->mh_hold_todo--;
18870Sstevel@tonic-gate continue;
18880Sstevel@tonic-gate }
18890Sstevel@tonic-gate ASSERT(freemem_left != 0);
18900Sstevel@tonic-gate if (PP_ISFREE(pp)) {
18910Sstevel@tonic-gate /*
18920Sstevel@tonic-gate * Like page_reclaim() only 'freemem'
18930Sstevel@tonic-gate * processing is already done.
18940Sstevel@tonic-gate */
18950Sstevel@tonic-gate MDSTAT_INCR(mhp, nfree);
18960Sstevel@tonic-gate free_page_collect:
18970Sstevel@tonic-gate if (PP_ISAGED(pp)) {
18980Sstevel@tonic-gate page_list_sub(pp,
18990Sstevel@tonic-gate PG_FREE_LIST);
19000Sstevel@tonic-gate } else {
19010Sstevel@tonic-gate page_list_sub(pp,
19020Sstevel@tonic-gate PG_CACHE_LIST);
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate PP_CLRFREE(pp);
19050Sstevel@tonic-gate PP_CLRAGED(pp);
19060Sstevel@tonic-gate collected++;
19070Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
19080Sstevel@tonic-gate page_delete_collect(pp, mhp);
19090Sstevel@tonic-gate mdsp->mds_bitmap[bit / NBPBMW] |=
19100Sstevel@tonic-gate (1 << (bit % NBPBMW));
19110Sstevel@tonic-gate freemem_left--;
19120Sstevel@tonic-gate continue;
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL);
19150Sstevel@tonic-gate if (first_scan) {
19160Sstevel@tonic-gate MDSTAT_INCR(mhp, first_notfree);
19170Sstevel@tonic-gate page_unlock(pp);
19180Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
19190Sstevel@tonic-gate continue;
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate /*
19220Sstevel@tonic-gate * Keep stats on pages encountered that
1923917Selowe * are marked for retirement.
19240Sstevel@tonic-gate */
1925917Selowe if (PP_TOXIC(pp)) {
19260Sstevel@tonic-gate MDSTAT_INCR(mhp, toxic);
1927917Selowe } else if (PP_PR_REQ(pp)) {
19280Sstevel@tonic-gate MDSTAT_INCR(mhp, failing);
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate /*
19310Sstevel@tonic-gate * In certain cases below, special exceptions
19320Sstevel@tonic-gate * are made for pages that are toxic. This
19330Sstevel@tonic-gate * is because the current meaning of toxic
19340Sstevel@tonic-gate * is that an uncorrectable error has been
19350Sstevel@tonic-gate * previously associated with the page.
19360Sstevel@tonic-gate */
19370Sstevel@tonic-gate if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
1938917Selowe if (!PP_TOXIC(pp)) {
19390Sstevel@tonic-gate /*
19400Sstevel@tonic-gate * Must relocate locked in
19410Sstevel@tonic-gate * memory pages.
19420Sstevel@tonic-gate */
19430Sstevel@tonic-gate #ifdef MEM_DEL_STATS
19440Sstevel@tonic-gate start_pgrp = ddi_get_lbolt();
19450Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
19460Sstevel@tonic-gate /*
19470Sstevel@tonic-gate * Lock all constituent pages
19480Sstevel@tonic-gate * of a large page to ensure
19490Sstevel@tonic-gate * that p_szc won't change.
19500Sstevel@tonic-gate */
19510Sstevel@tonic-gate if (!group_page_trylock(pp,
19520Sstevel@tonic-gate SE_EXCL)) {
19530Sstevel@tonic-gate MDSTAT_INCR(mhp,
19540Sstevel@tonic-gate gptllckfail);
19550Sstevel@tonic-gate page_unlock(pp);
19560Sstevel@tonic-gate mutex_enter(
19570Sstevel@tonic-gate &mhp->mh_mutex);
19580Sstevel@tonic-gate continue;
19590Sstevel@tonic-gate }
19600Sstevel@tonic-gate MDSTAT_INCR(mhp, npplocked);
19610Sstevel@tonic-gate pp_targ =
19620Sstevel@tonic-gate page_get_replacement_page(
19636242Smb158278 pp, NULL, 0);
19640Sstevel@tonic-gate if (pp_targ != NULL) {
19650Sstevel@tonic-gate #ifdef MEM_DEL_STATS
19660Sstevel@tonic-gate ntick_pgrp =
19670Sstevel@tonic-gate (uint64_t)
19680Sstevel@tonic-gate ddi_get_lbolt() -
19690Sstevel@tonic-gate start_pgrp;
19700Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
19710Sstevel@tonic-gate MDSTAT_PGRP(mhp,
19720Sstevel@tonic-gate ntick_pgrp);
19730Sstevel@tonic-gate MDSTAT_INCR(mhp,
19740Sstevel@tonic-gate nlockreloc);
19750Sstevel@tonic-gate goto reloc;
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate group_page_unlock(pp);
19780Sstevel@tonic-gate page_unlock(pp);
19790Sstevel@tonic-gate #ifdef MEM_DEL_STATS
19800Sstevel@tonic-gate ntick_pgrp =
19810Sstevel@tonic-gate (uint64_t)ddi_get_lbolt() -
19820Sstevel@tonic-gate start_pgrp;
19830Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
19840Sstevel@tonic-gate MDSTAT_PGRP(mhp, ntick_pgrp);
19850Sstevel@tonic-gate MDSTAT_INCR(mhp, nnorepl);
19860Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
19870Sstevel@tonic-gate continue;
19880Sstevel@tonic-gate } else {
19890Sstevel@tonic-gate /*
19900Sstevel@tonic-gate * Cannot do anything about
19910Sstevel@tonic-gate * this page because it is
19920Sstevel@tonic-gate * toxic.
19930Sstevel@tonic-gate */
19940Sstevel@tonic-gate MDSTAT_INCR(mhp, npplkdtoxic);
19950Sstevel@tonic-gate page_unlock(pp);
19960Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
19970Sstevel@tonic-gate continue;
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate }
20000Sstevel@tonic-gate /*
20010Sstevel@tonic-gate * Unload the mappings and check if mod bit
20020Sstevel@tonic-gate * is set.
20030Sstevel@tonic-gate */
20043290Sjohansen ASSERT(!PP_ISKAS(pp));
20050Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
20060Sstevel@tonic-gate mod = hat_ismod(pp);
20070Sstevel@tonic-gate
20080Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20090Sstevel@tonic-gate start_pgrp = ddi_get_lbolt();
20100Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
2011917Selowe if (mod && !PP_TOXIC(pp)) {
20120Sstevel@tonic-gate /*
20130Sstevel@tonic-gate * Lock all constituent pages
20140Sstevel@tonic-gate * of a large page to ensure
20150Sstevel@tonic-gate * that p_szc won't change.
20160Sstevel@tonic-gate */
20170Sstevel@tonic-gate if (!group_page_trylock(pp, SE_EXCL)) {
20180Sstevel@tonic-gate MDSTAT_INCR(mhp, gptlmodfail);
20190Sstevel@tonic-gate page_unlock(pp);
20200Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
20210Sstevel@tonic-gate continue;
20220Sstevel@tonic-gate }
20230Sstevel@tonic-gate pp_targ = page_get_replacement_page(pp,
20240Sstevel@tonic-gate NULL, 0);
20250Sstevel@tonic-gate if (pp_targ != NULL) {
20260Sstevel@tonic-gate MDSTAT_INCR(mhp, nmodreloc);
20270Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20280Sstevel@tonic-gate ntick_pgrp =
20290Sstevel@tonic-gate (uint64_t)ddi_get_lbolt() -
20306242Smb158278 start_pgrp;
20310Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20320Sstevel@tonic-gate MDSTAT_PGRP(mhp, ntick_pgrp);
20330Sstevel@tonic-gate goto reloc;
20340Sstevel@tonic-gate }
20350Sstevel@tonic-gate group_page_unlock(pp);
20360Sstevel@tonic-gate }
20370Sstevel@tonic-gate
20380Sstevel@tonic-gate if (!page_try_demote_pages(pp)) {
20390Sstevel@tonic-gate MDSTAT_INCR(mhp, demotefail);
20400Sstevel@tonic-gate page_unlock(pp);
20410Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20420Sstevel@tonic-gate ntick_pgrp = (uint64_t)ddi_get_lbolt() -
20430Sstevel@tonic-gate start_pgrp;
20440Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20450Sstevel@tonic-gate MDSTAT_PGRP(mhp, ntick_pgrp);
20460Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
20470Sstevel@tonic-gate continue;
20480Sstevel@tonic-gate }
20490Sstevel@tonic-gate
20500Sstevel@tonic-gate /*
20510Sstevel@tonic-gate * Regular 'page-out'.
20520Sstevel@tonic-gate */
20530Sstevel@tonic-gate if (!mod) {
20540Sstevel@tonic-gate MDSTAT_INCR(mhp, ndestroy);
20550Sstevel@tonic-gate page_destroy(pp, 1);
20560Sstevel@tonic-gate /*
20570Sstevel@tonic-gate * page_destroy was called with
20580Sstevel@tonic-gate * dontfree. As long as p_lckcnt
20590Sstevel@tonic-gate * and p_cowcnt are both zero, the
20600Sstevel@tonic-gate * only additional action of
20610Sstevel@tonic-gate * page_destroy with !dontfree is to
20620Sstevel@tonic-gate * call page_free, so we can collect
20630Sstevel@tonic-gate * the page here.
20640Sstevel@tonic-gate */
20650Sstevel@tonic-gate collected++;
20660Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20670Sstevel@tonic-gate ntick_pgrp = (uint64_t)ddi_get_lbolt() -
20680Sstevel@tonic-gate start_pgrp;
20690Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20700Sstevel@tonic-gate MDSTAT_PGRP(mhp, ntick_pgrp);
20710Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
20720Sstevel@tonic-gate page_delete_collect(pp, mhp);
20730Sstevel@tonic-gate mdsp->mds_bitmap[bit / NBPBMW] |=
20740Sstevel@tonic-gate (1 << (bit % NBPBMW));
20750Sstevel@tonic-gate continue;
20760Sstevel@tonic-gate }
20770Sstevel@tonic-gate /*
20780Sstevel@tonic-gate * The page is toxic and the mod bit is
20790Sstevel@tonic-gate * set, we cannot do anything here to deal
20800Sstevel@tonic-gate * with it.
20810Sstevel@tonic-gate */
2082917Selowe if (PP_TOXIC(pp)) {
20830Sstevel@tonic-gate page_unlock(pp);
20840Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20850Sstevel@tonic-gate ntick_pgrp = (uint64_t)ddi_get_lbolt() -
20860Sstevel@tonic-gate start_pgrp;
20870Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20880Sstevel@tonic-gate MDSTAT_PGRP(mhp, ntick_pgrp);
20890Sstevel@tonic-gate MDSTAT_INCR(mhp, modtoxic);
20900Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
20910Sstevel@tonic-gate continue;
20920Sstevel@tonic-gate }
20930Sstevel@tonic-gate MDSTAT_INCR(mhp, nputpage);
20940Sstevel@tonic-gate vp = pp->p_vnode;
20950Sstevel@tonic-gate offset = pp->p_offset;
20960Sstevel@tonic-gate VN_HOLD(vp);
20970Sstevel@tonic-gate page_unlock(pp);
20980Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, offset, PAGESIZE,
20995331Samw B_INVAL|B_FORCE, kcred, NULL);
21000Sstevel@tonic-gate VN_RELE(vp);
21010Sstevel@tonic-gate #ifdef MEM_DEL_STATS
21020Sstevel@tonic-gate ntick_pgrp = (uint64_t)ddi_get_lbolt() -
21030Sstevel@tonic-gate start_pgrp;
21040Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
21050Sstevel@tonic-gate MDSTAT_PGRP(mhp, ntick_pgrp);
21060Sstevel@tonic-gate /*
21070Sstevel@tonic-gate * Try to get the page back immediately
21080Sstevel@tonic-gate * so that it can be collected.
21090Sstevel@tonic-gate */
21100Sstevel@tonic-gate pp = page_numtopp_nolock(pfn);
21110Sstevel@tonic-gate if (pp == NULL) {
21120Sstevel@tonic-gate MDSTAT_INCR(mhp, nnoreclaim);
21130Sstevel@tonic-gate /*
21140Sstevel@tonic-gate * This should not happen as this
21150Sstevel@tonic-gate * thread is deleting the page.
21160Sstevel@tonic-gate * If this code is generalized, this
21170Sstevel@tonic-gate * becomes a reality.
21180Sstevel@tonic-gate */
21190Sstevel@tonic-gate #ifdef DEBUG
21200Sstevel@tonic-gate cmn_err(CE_WARN,
21210Sstevel@tonic-gate "delete_memory_thread(0x%p) "
21220Sstevel@tonic-gate "pfn 0x%lx has no page_t",
21230Sstevel@tonic-gate (void *)mhp, pfn);
21240Sstevel@tonic-gate #endif /* DEBUG */
21250Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
21260Sstevel@tonic-gate continue;
21270Sstevel@tonic-gate }
21280Sstevel@tonic-gate if (page_try_reclaim_lock(pp, SE_EXCL,
2129917Selowe SE_EXCL_WANTED | SE_RETIRED)) {
21300Sstevel@tonic-gate if (PP_ISFREE(pp)) {
21310Sstevel@tonic-gate goto free_page_collect;
21320Sstevel@tonic-gate }
21330Sstevel@tonic-gate page_unlock(pp);
21340Sstevel@tonic-gate }
21350Sstevel@tonic-gate MDSTAT_INCR(mhp, nnoreclaim);
21360Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
21370Sstevel@tonic-gate continue;
21380Sstevel@tonic-gate
21390Sstevel@tonic-gate reloc:
21400Sstevel@tonic-gate /*
21410Sstevel@tonic-gate * Got some freemem and a target
21420Sstevel@tonic-gate * page, so move the data to avoid
21430Sstevel@tonic-gate * I/O and lock problems.
21440Sstevel@tonic-gate */
21450Sstevel@tonic-gate ASSERT(!page_iolock_assert(pp));
21460Sstevel@tonic-gate MDSTAT_INCR(mhp, nreloc);
21470Sstevel@tonic-gate /*
21480Sstevel@tonic-gate * page_relocate() will return pgcnt: the
21490Sstevel@tonic-gate * number of consecutive pages relocated.
21500Sstevel@tonic-gate * If it is successful, pp will be a
21510Sstevel@tonic-gate * linked list of the page structs that
21520Sstevel@tonic-gate * were relocated. If page_relocate() is
21530Sstevel@tonic-gate * unsuccessful, pp will be unmodified.
21540Sstevel@tonic-gate */
21550Sstevel@tonic-gate #ifdef MEM_DEL_STATS
21560Sstevel@tonic-gate start_pgrp = ddi_get_lbolt();
21570Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
21580Sstevel@tonic-gate result = page_relocate(&pp, &pp_targ, 0, 0,
21590Sstevel@tonic-gate &pgcnt, NULL);
21600Sstevel@tonic-gate #ifdef MEM_DEL_STATS
21610Sstevel@tonic-gate ntick_pgrp = (uint64_t)ddi_get_lbolt() -
21620Sstevel@tonic-gate start_pgrp;
21630Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
21640Sstevel@tonic-gate MDSTAT_PGRP(mhp, ntick_pgrp);
21650Sstevel@tonic-gate if (result != 0) {
21660Sstevel@tonic-gate MDSTAT_INCR(mhp, nrelocfail);
21670Sstevel@tonic-gate /*
21680Sstevel@tonic-gate * We did not succeed. We need
21690Sstevel@tonic-gate * to give the pp_targ pages back.
21700Sstevel@tonic-gate * page_free(pp_targ, 1) without
21710Sstevel@tonic-gate * the freemem accounting.
21720Sstevel@tonic-gate */
21730Sstevel@tonic-gate group_page_unlock(pp);
21740Sstevel@tonic-gate page_free_replacement_page(pp_targ);
21750Sstevel@tonic-gate page_unlock(pp);
21760Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
21770Sstevel@tonic-gate continue;
21780Sstevel@tonic-gate }
21790Sstevel@tonic-gate
21800Sstevel@tonic-gate /*
21810Sstevel@tonic-gate * We will then collect pgcnt pages.
21820Sstevel@tonic-gate */
21830Sstevel@tonic-gate ASSERT(pgcnt > 0);
21840Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
21850Sstevel@tonic-gate /*
21860Sstevel@tonic-gate * We need to make sure freemem_left is
21870Sstevel@tonic-gate * large enough.
21880Sstevel@tonic-gate */
21890Sstevel@tonic-gate while ((freemem_left < pgcnt) &&
21906242Smb158278 (!mhp->mh_cancel)) {
21910Sstevel@tonic-gate freemem_left +=
21926242Smb158278 delthr_get_freemem(mhp);
21930Sstevel@tonic-gate }
21940Sstevel@tonic-gate
21950Sstevel@tonic-gate /*
21960Sstevel@tonic-gate * Do not proceed if mh_cancel is set.
21970Sstevel@tonic-gate */
21980Sstevel@tonic-gate if (mhp->mh_cancel) {
21990Sstevel@tonic-gate while (pp_targ != NULL) {
22000Sstevel@tonic-gate /*
22010Sstevel@tonic-gate * Unlink and unlock each page.
22020Sstevel@tonic-gate */
22030Sstevel@tonic-gate tpp_targ = pp_targ;
22040Sstevel@tonic-gate page_sub(&pp_targ, tpp_targ);
22050Sstevel@tonic-gate page_unlock(tpp_targ);
22060Sstevel@tonic-gate }
22070Sstevel@tonic-gate /*
22080Sstevel@tonic-gate * We need to give the pp pages back.
22090Sstevel@tonic-gate * page_free(pp, 1) without the
22100Sstevel@tonic-gate * freemem accounting.
22110Sstevel@tonic-gate */
22120Sstevel@tonic-gate page_free_replacement_page(pp);
22130Sstevel@tonic-gate break;
22140Sstevel@tonic-gate }
22150Sstevel@tonic-gate
22160Sstevel@tonic-gate /* Now remove pgcnt from freemem_left */
22170Sstevel@tonic-gate freemem_left -= pgcnt;
22180Sstevel@tonic-gate ASSERT(freemem_left >= 0);
22190Sstevel@tonic-gate szc = pp->p_szc;
22200Sstevel@tonic-gate while (pp != NULL) {
22210Sstevel@tonic-gate /*
22220Sstevel@tonic-gate * pp and pp_targ were passed back as
22230Sstevel@tonic-gate * a linked list of pages.
22240Sstevel@tonic-gate * Unlink and unlock each page.
22250Sstevel@tonic-gate */
22260Sstevel@tonic-gate tpp_targ = pp_targ;
22270Sstevel@tonic-gate page_sub(&pp_targ, tpp_targ);
22280Sstevel@tonic-gate page_unlock(tpp_targ);
22290Sstevel@tonic-gate /*
22300Sstevel@tonic-gate * The original page is now free
22310Sstevel@tonic-gate * so remove it from the linked
22320Sstevel@tonic-gate * list and collect it.
22330Sstevel@tonic-gate */
22340Sstevel@tonic-gate tpp = pp;
22350Sstevel@tonic-gate page_sub(&pp, tpp);
22360Sstevel@tonic-gate pfn = page_pptonum(tpp);
22370Sstevel@tonic-gate collected++;
22380Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp));
22390Sstevel@tonic-gate ASSERT(tpp->p_vnode == NULL);
22400Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp));
22410Sstevel@tonic-gate ASSERT(tpp->p_szc == szc);
22420Sstevel@tonic-gate tpp->p_szc = 0;
22430Sstevel@tonic-gate page_delete_collect(tpp, mhp);
22440Sstevel@tonic-gate bit = pfn - mdsp->mds_base;
22450Sstevel@tonic-gate mdsp->mds_bitmap[bit / NBPBMW] |=
22466242Smb158278 (1 << (bit % NBPBMW));
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate ASSERT(pp_targ == NULL);
22490Sstevel@tonic-gate }
22500Sstevel@tonic-gate }
22510Sstevel@tonic-gate first_scan = 0;
22520Sstevel@tonic-gate if ((mhp->mh_cancel == 0) && (mhp->mh_hold_todo != 0) &&
22536242Smb158278 (collected == 0)) {
22540Sstevel@tonic-gate /*
22550Sstevel@tonic-gate * This code is needed as we cannot wait
22560Sstevel@tonic-gate * for a page to be locked OR the delete to
22570Sstevel@tonic-gate * be cancelled. Also, we must delay so
22580Sstevel@tonic-gate * that other threads get a chance to run
22590Sstevel@tonic-gate * on our cpu, otherwise page locks may be
22600Sstevel@tonic-gate * held indefinitely by those threads.
22610Sstevel@tonic-gate */
22620Sstevel@tonic-gate MDSTAT_INCR(mhp, ndelay);
22630Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
226411066Srafael.vanoni@sun.com (void) cv_reltimedwait(&mhp->mh_cv, &mhp->mh_mutex,
226511066Srafael.vanoni@sun.com DEL_BUSY_WAIT_TICKS, TR_CLOCK_TICK);
22660Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &mhp->mh_mutex);
22670Sstevel@tonic-gate }
22680Sstevel@tonic-gate }
22690Sstevel@tonic-gate /* stop the dr aio cleanup thread */
22700Sstevel@tonic-gate mhp->mh_dr_aio_cleanup_cancel = 1;
22710Sstevel@tonic-gate transit_list_collect(mhp, 0);
22720Sstevel@tonic-gate if (freemem_left != 0) {
22730Sstevel@tonic-gate /* Return any surplus. */
22740Sstevel@tonic-gate page_create_putback(freemem_left);
22750Sstevel@tonic-gate freemem_left = 0;
22760Sstevel@tonic-gate }
22770Sstevel@tonic-gate #ifdef MEM_DEL_STATS
22780Sstevel@tonic-gate ntick_total = (uint64_t)ddi_get_lbolt() - start_total;
22790Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
22800Sstevel@tonic-gate MDSTAT_TOTAL(mhp, ntick_total);
22810Sstevel@tonic-gate MDSTAT_PRINT(mhp);
22820Sstevel@tonic-gate
22830Sstevel@tonic-gate /*
22840Sstevel@tonic-gate * If the memory delete was cancelled, exclusive-wanted bits must
2285917Selowe * be cleared. If there are retired pages being deleted, they need
2286917Selowe * to be unretired.
22870Sstevel@tonic-gate */
22880Sstevel@tonic-gate for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
22890Sstevel@tonic-gate mdsp = mdsp->mds_next) {
22900Sstevel@tonic-gate pfn_t pfn, p_end;
22910Sstevel@tonic-gate
22920Sstevel@tonic-gate p_end = mdsp->mds_base + mdsp->mds_npgs;
22930Sstevel@tonic-gate for (pfn = mdsp->mds_base; pfn < p_end; pfn++) {
22940Sstevel@tonic-gate page_t *pp;
22950Sstevel@tonic-gate pgcnt_t bit;
22960Sstevel@tonic-gate
22970Sstevel@tonic-gate bit = pfn - mdsp->mds_base;
22980Sstevel@tonic-gate if (mhp->mh_cancel) {
22990Sstevel@tonic-gate pp = page_numtopp_nolock(pfn);
23000Sstevel@tonic-gate if (pp != NULL) {
23010Sstevel@tonic-gate if ((mdsp->mds_bitmap[bit / NBPBMW] &
23020Sstevel@tonic-gate (1 << (bit % NBPBMW))) == 0) {
23030Sstevel@tonic-gate page_lock_clr_exclwanted(pp);
23040Sstevel@tonic-gate }
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate } else {
23070Sstevel@tonic-gate pp = NULL;
23080Sstevel@tonic-gate }
23090Sstevel@tonic-gate if ((mdsp->mds_bitmap_retired[bit / NBPBMW] &
23100Sstevel@tonic-gate (1 << (bit % NBPBMW))) != 0) {
23110Sstevel@tonic-gate /* do we already have pp? */
23120Sstevel@tonic-gate if (pp == NULL) {
23130Sstevel@tonic-gate pp = page_numtopp_nolock(pfn);
23140Sstevel@tonic-gate }
23150Sstevel@tonic-gate ASSERT(pp != NULL);
2316917Selowe ASSERT(PP_RETIRED(pp));
23170Sstevel@tonic-gate if (mhp->mh_cancel != 0) {
2318917Selowe page_unlock(pp);
23190Sstevel@tonic-gate /*
23200Sstevel@tonic-gate * To satisfy ASSERT below in
23210Sstevel@tonic-gate * cancel code.
23220Sstevel@tonic-gate */
23230Sstevel@tonic-gate mhp->mh_hold_todo++;
23240Sstevel@tonic-gate } else {
23253253Smec (void) page_unretire_pp(pp,
23263253Smec PR_UNR_CLEAN);
23270Sstevel@tonic-gate }
23280Sstevel@tonic-gate }
23290Sstevel@tonic-gate }
23300Sstevel@tonic-gate }
23310Sstevel@tonic-gate /*
23320Sstevel@tonic-gate * Free retired page bitmap and collected page bitmap
23330Sstevel@tonic-gate */
23340Sstevel@tonic-gate for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
23350Sstevel@tonic-gate mdsp = mdsp->mds_next) {
23360Sstevel@tonic-gate ASSERT(mdsp->mds_bitmap_retired != NULL);
23370Sstevel@tonic-gate kmem_free(mdsp->mds_bitmap_retired, MDS_BITMAPBYTES(mdsp));
23380Sstevel@tonic-gate mdsp->mds_bitmap_retired = NULL; /* Paranoia. */
23390Sstevel@tonic-gate ASSERT(mdsp->mds_bitmap != NULL);
23400Sstevel@tonic-gate kmem_free(mdsp->mds_bitmap, MDS_BITMAPBYTES(mdsp));
23410Sstevel@tonic-gate mdsp->mds_bitmap = NULL; /* Paranoia. */
23420Sstevel@tonic-gate }
23430Sstevel@tonic-gate
23440Sstevel@tonic-gate /* wait for our dr aio cancel thread to exit */
23450Sstevel@tonic-gate while (!(mhp->mh_aio_cleanup_done)) {
23460Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
23470Sstevel@tonic-gate delay(drv_usectohz(DR_AIO_CLEANUP_DELAY));
23480Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &mhp->mh_mutex);
23490Sstevel@tonic-gate }
23500Sstevel@tonic-gate refused:
23510Sstevel@tonic-gate if (mhp->mh_cancel != 0) {
23520Sstevel@tonic-gate page_t *pp;
23530Sstevel@tonic-gate
23540Sstevel@tonic-gate comp_code = mhp->mh_cancel;
23550Sstevel@tonic-gate /*
23560Sstevel@tonic-gate * Go through list of deleted pages (mh_deleted) freeing
23570Sstevel@tonic-gate * them.
23580Sstevel@tonic-gate */
23590Sstevel@tonic-gate while ((pp = mhp->mh_deleted) != NULL) {
23600Sstevel@tonic-gate mhp->mh_deleted = pp->p_next;
23610Sstevel@tonic-gate mhp->mh_hold_todo++;
23620Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
23630Sstevel@tonic-gate /* Restore p_next. */
23640Sstevel@tonic-gate pp->p_next = pp->p_prev;
23650Sstevel@tonic-gate if (PP_ISFREE(pp)) {
23660Sstevel@tonic-gate cmn_err(CE_PANIC,
23670Sstevel@tonic-gate "page %p is free",
23680Sstevel@tonic-gate (void *)pp);
23690Sstevel@tonic-gate }
23700Sstevel@tonic-gate page_free(pp, 1);
23710Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
23720Sstevel@tonic-gate }
23730Sstevel@tonic-gate ASSERT(mhp->mh_hold_todo == mhp->mh_vm_pages);
23740Sstevel@tonic-gate
23750Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
23760Sstevel@tonic-gate put_availrmem(mhp->mh_vm_pages);
23770Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
23780Sstevel@tonic-gate
23790Sstevel@tonic-gate goto t_exit;
23800Sstevel@tonic-gate }
23810Sstevel@tonic-gate
23820Sstevel@tonic-gate /*
23830Sstevel@tonic-gate * All the pages are no longer in use and are exclusively locked.
23840Sstevel@tonic-gate */
23850Sstevel@tonic-gate
23860Sstevel@tonic-gate mhp->mh_deleted = NULL;
23870Sstevel@tonic-gate
23880Sstevel@tonic-gate kphysm_del_cleanup(mhp);
23890Sstevel@tonic-gate
23906242Smb158278 /*
239110106SJason.Beloro@Sun.COM * mem_node_del_range needs to be after kphysm_del_cleanup so
23926242Smb158278 * that the mem_node_config[] will remain intact for the cleanup.
23936242Smb158278 */
23946242Smb158278 for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
23956242Smb158278 mdsp = mdsp->mds_next) {
239610106SJason.Beloro@Sun.COM mem_node_del_range(mdsp->mds_base,
239710106SJason.Beloro@Sun.COM mdsp->mds_base + mdsp->mds_npgs - 1);
23986242Smb158278 }
239911185SSean.McEnroe@Sun.COM /* cleanup the page counters */
240011185SSean.McEnroe@Sun.COM page_ctrs_cleanup();
24016242Smb158278
24020Sstevel@tonic-gate comp_code = KPHYSM_OK;
24030Sstevel@tonic-gate
24040Sstevel@tonic-gate t_exit:
24050Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
24060Sstevel@tonic-gate kphysm_setup_post_del(mhp->mh_vm_pages,
24070Sstevel@tonic-gate (comp_code == KPHYSM_OK) ? 0 : 1);
24080Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
24090Sstevel@tonic-gate
24100Sstevel@tonic-gate early_exit:
24110Sstevel@tonic-gate /* mhp->mh_mutex exited by CALLB_CPR_EXIT() */
24120Sstevel@tonic-gate mhp->mh_state = MHND_DONE;
24130Sstevel@tonic-gate del_complete_funcp = mhp->mh_delete_complete;
24140Sstevel@tonic-gate del_complete_arg = mhp->mh_delete_complete_arg;
24150Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo);
24160Sstevel@tonic-gate (*del_complete_funcp)(del_complete_arg, comp_code);
24170Sstevel@tonic-gate thread_exit();
24180Sstevel@tonic-gate /*NOTREACHED*/
24190Sstevel@tonic-gate }
24200Sstevel@tonic-gate
24210Sstevel@tonic-gate /*
24220Sstevel@tonic-gate * Start the delete of the memory from the system.
24230Sstevel@tonic-gate */
24240Sstevel@tonic-gate int
kphysm_del_start(memhandle_t handle,void (* complete)(void *,int),void * complete_arg)24250Sstevel@tonic-gate kphysm_del_start(
24260Sstevel@tonic-gate memhandle_t handle,
24270Sstevel@tonic-gate void (*complete)(void *, int),
24280Sstevel@tonic-gate void *complete_arg)
24290Sstevel@tonic-gate {
24300Sstevel@tonic-gate struct mem_handle *mhp;
24310Sstevel@tonic-gate
24320Sstevel@tonic-gate mhp = kphysm_lookup_mem_handle(handle);
24330Sstevel@tonic-gate if (mhp == NULL) {
24340Sstevel@tonic-gate return (KPHYSM_EHANDLE);
24350Sstevel@tonic-gate }
24360Sstevel@tonic-gate switch (mhp->mh_state) {
24370Sstevel@tonic-gate case MHND_FREE:
24380Sstevel@tonic-gate ASSERT(mhp->mh_state != MHND_FREE);
24390Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
24400Sstevel@tonic-gate return (KPHYSM_EHANDLE);
24410Sstevel@tonic-gate case MHND_INIT:
24420Sstevel@tonic-gate break;
24430Sstevel@tonic-gate case MHND_STARTING:
24440Sstevel@tonic-gate case MHND_RUNNING:
24450Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
24460Sstevel@tonic-gate return (KPHYSM_ESEQUENCE);
24470Sstevel@tonic-gate case MHND_DONE:
24480Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
24490Sstevel@tonic-gate return (KPHYSM_ESEQUENCE);
24500Sstevel@tonic-gate case MHND_RELEASE:
24510Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
24520Sstevel@tonic-gate return (KPHYSM_ESEQUENCE);
24530Sstevel@tonic-gate default:
24540Sstevel@tonic-gate #ifdef DEBUG
24550Sstevel@tonic-gate cmn_err(CE_WARN, "kphysm_del_start(0x%p) state corrupt %d",
24560Sstevel@tonic-gate (void *)mhp, mhp->mh_state);
24570Sstevel@tonic-gate #endif /* DEBUG */
24580Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
24590Sstevel@tonic-gate return (KPHYSM_EHANDLE);
24600Sstevel@tonic-gate }
24610Sstevel@tonic-gate
24620Sstevel@tonic-gate if (mhp->mh_transit.trl_spans == NULL) {
24630Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
24640Sstevel@tonic-gate return (KPHYSM_ENOWORK);
24650Sstevel@tonic-gate }
24660Sstevel@tonic-gate
24670Sstevel@tonic-gate ASSERT(complete != NULL);
24680Sstevel@tonic-gate mhp->mh_delete_complete = complete;
24690Sstevel@tonic-gate mhp->mh_delete_complete_arg = complete_arg;
24700Sstevel@tonic-gate mhp->mh_state = MHND_STARTING;
24710Sstevel@tonic-gate /*
24720Sstevel@tonic-gate * Release the mutex in case thread_create sleeps.
24730Sstevel@tonic-gate */
24740Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
24750Sstevel@tonic-gate
24760Sstevel@tonic-gate /*
24770Sstevel@tonic-gate * The "obvious" process for this thread is pageout (proc_pageout)
24780Sstevel@tonic-gate * but this gives the thread too much power over freemem
24790Sstevel@tonic-gate * which results in freemem starvation.
24800Sstevel@tonic-gate */
24810Sstevel@tonic-gate (void) thread_create(NULL, 0, delete_memory_thread, mhp, 0, &p0,
24820Sstevel@tonic-gate TS_RUN, maxclsyspri - 1);
24830Sstevel@tonic-gate
24840Sstevel@tonic-gate return (KPHYSM_OK);
24850Sstevel@tonic-gate }
24860Sstevel@tonic-gate
24870Sstevel@tonic-gate static kmutex_t pp_dummy_lock; /* Protects init. of pp_dummy. */
24880Sstevel@tonic-gate static caddr_t pp_dummy;
24890Sstevel@tonic-gate static pgcnt_t pp_dummy_npages;
24900Sstevel@tonic-gate static pfn_t *pp_dummy_pfn; /* Array of dummy pfns. */
24910Sstevel@tonic-gate
24920Sstevel@tonic-gate static void
memseg_remap_init_pages(page_t * pages,page_t * epages)24930Sstevel@tonic-gate memseg_remap_init_pages(page_t *pages, page_t *epages)
24940Sstevel@tonic-gate {
24950Sstevel@tonic-gate page_t *pp;
24960Sstevel@tonic-gate
24970Sstevel@tonic-gate for (pp = pages; pp < epages; pp++) {
24980Sstevel@tonic-gate pp->p_pagenum = PFN_INVALID; /* XXXX */
24990Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1;
25000Sstevel@tonic-gate page_iolock_init(pp);
25010Sstevel@tonic-gate while (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_RECLAIM))
25020Sstevel@tonic-gate continue;
25030Sstevel@tonic-gate page_lock_delete(pp);
25040Sstevel@tonic-gate }
25050Sstevel@tonic-gate }
25060Sstevel@tonic-gate
25070Sstevel@tonic-gate void
memseg_remap_init()25080Sstevel@tonic-gate memseg_remap_init()
25090Sstevel@tonic-gate {
25100Sstevel@tonic-gate mutex_enter(&pp_dummy_lock);
25110Sstevel@tonic-gate if (pp_dummy == NULL) {
25120Sstevel@tonic-gate uint_t dpages;
25130Sstevel@tonic-gate int i;
25140Sstevel@tonic-gate
25150Sstevel@tonic-gate /*
25160Sstevel@tonic-gate * dpages starts off as the size of the structure and
25170Sstevel@tonic-gate * ends up as the minimum number of pages that will
25180Sstevel@tonic-gate * hold a whole number of page_t structures.
25190Sstevel@tonic-gate */
25200Sstevel@tonic-gate dpages = sizeof (page_t);
25210Sstevel@tonic-gate ASSERT(dpages != 0);
25220Sstevel@tonic-gate ASSERT(dpages <= MMU_PAGESIZE);
25230Sstevel@tonic-gate
25240Sstevel@tonic-gate while ((dpages & 1) == 0)
25250Sstevel@tonic-gate dpages >>= 1;
25260Sstevel@tonic-gate
25270Sstevel@tonic-gate pp_dummy_npages = dpages;
25280Sstevel@tonic-gate /*
25290Sstevel@tonic-gate * Allocate pp_dummy pages directly from static_arena,
25300Sstevel@tonic-gate * since these are whole page allocations and are
25310Sstevel@tonic-gate * referenced by physical address. This also has the
25320Sstevel@tonic-gate * nice fringe benefit of hiding the memory from
25330Sstevel@tonic-gate * ::findleaks since it doesn't deal well with allocated
25340Sstevel@tonic-gate * kernel heap memory that doesn't have any mappings.
25350Sstevel@tonic-gate */
25360Sstevel@tonic-gate pp_dummy = vmem_xalloc(static_arena, ptob(pp_dummy_npages),
25370Sstevel@tonic-gate PAGESIZE, 0, 0, NULL, NULL, VM_SLEEP);
25380Sstevel@tonic-gate bzero(pp_dummy, ptob(pp_dummy_npages));
25390Sstevel@tonic-gate ASSERT(((uintptr_t)pp_dummy & MMU_PAGEOFFSET) == 0);
25400Sstevel@tonic-gate pp_dummy_pfn = kmem_alloc(sizeof (*pp_dummy_pfn) *
25410Sstevel@tonic-gate pp_dummy_npages, KM_SLEEP);
25420Sstevel@tonic-gate for (i = 0; i < pp_dummy_npages; i++) {
25430Sstevel@tonic-gate pp_dummy_pfn[i] = hat_getpfnum(kas.a_hat,
25440Sstevel@tonic-gate &pp_dummy[MMU_PAGESIZE * i]);
25450Sstevel@tonic-gate ASSERT(pp_dummy_pfn[i] != PFN_INVALID);
25460Sstevel@tonic-gate }
25470Sstevel@tonic-gate /*
25480Sstevel@tonic-gate * Initialize the page_t's to a known 'deleted' state
25490Sstevel@tonic-gate * that matches the state of deleted pages.
25500Sstevel@tonic-gate */
25510Sstevel@tonic-gate memseg_remap_init_pages((page_t *)pp_dummy,
25526242Smb158278 (page_t *)(pp_dummy + ptob(pp_dummy_npages)));
25530Sstevel@tonic-gate /* Remove kmem mappings for the pages for safety. */
25540Sstevel@tonic-gate hat_unload(kas.a_hat, pp_dummy, ptob(pp_dummy_npages),
25550Sstevel@tonic-gate HAT_UNLOAD_UNLOCK);
25560Sstevel@tonic-gate /* Leave pp_dummy pointer set as flag that init is done. */
25570Sstevel@tonic-gate }
25580Sstevel@tonic-gate mutex_exit(&pp_dummy_lock);
25590Sstevel@tonic-gate }
25600Sstevel@tonic-gate
256110106SJason.Beloro@Sun.COM /*
256210106SJason.Beloro@Sun.COM * Remap a page-aglined range of page_t's to dummy pages.
256310106SJason.Beloro@Sun.COM */
256410106SJason.Beloro@Sun.COM void
remap_to_dummy(caddr_t va,pgcnt_t metapgs)256510106SJason.Beloro@Sun.COM remap_to_dummy(caddr_t va, pgcnt_t metapgs)
25660Sstevel@tonic-gate {
256710106SJason.Beloro@Sun.COM int phase;
256810106SJason.Beloro@Sun.COM
2569*12004Sjiang.liu@intel.com ASSERT(IS_P2ALIGNED((uint64_t)(uintptr_t)va, PAGESIZE));
257010106SJason.Beloro@Sun.COM
257110106SJason.Beloro@Sun.COM /*
257210106SJason.Beloro@Sun.COM * We may start remapping at a non-zero page offset
257310106SJason.Beloro@Sun.COM * within the dummy pages since the low/high ends
257410106SJason.Beloro@Sun.COM * of the outgoing pp's could be shared by other
257510106SJason.Beloro@Sun.COM * memsegs (see memseg_remap_meta).
257610106SJason.Beloro@Sun.COM */
2577*12004Sjiang.liu@intel.com phase = btop((uint64_t)(uintptr_t)va) % pp_dummy_npages;
2578*12004Sjiang.liu@intel.com /*CONSTCOND*/
257910106SJason.Beloro@Sun.COM ASSERT(PAGESIZE % sizeof (page_t) || phase == 0);
25800Sstevel@tonic-gate
25810Sstevel@tonic-gate while (metapgs != 0) {
25820Sstevel@tonic-gate pgcnt_t n;
258310106SJason.Beloro@Sun.COM int i, j;
25840Sstevel@tonic-gate
25850Sstevel@tonic-gate n = pp_dummy_npages;
25860Sstevel@tonic-gate if (n > metapgs)
25870Sstevel@tonic-gate n = metapgs;
25880Sstevel@tonic-gate for (i = 0; i < n; i++) {
258910106SJason.Beloro@Sun.COM j = (i + phase) % pp_dummy_npages;
259010106SJason.Beloro@Sun.COM hat_devload(kas.a_hat, va, ptob(1), pp_dummy_pfn[j],
25910Sstevel@tonic-gate PROT_READ,
25920Sstevel@tonic-gate HAT_LOAD | HAT_LOAD_NOCONSIST |
25930Sstevel@tonic-gate HAT_LOAD_REMAP);
259410106SJason.Beloro@Sun.COM va += ptob(1);
25950Sstevel@tonic-gate }
25960Sstevel@tonic-gate metapgs -= n;
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate }
25990Sstevel@tonic-gate
260010106SJason.Beloro@Sun.COM static void
memseg_remap_to_dummy(struct memseg * seg)260110106SJason.Beloro@Sun.COM memseg_remap_to_dummy(struct memseg *seg)
260210106SJason.Beloro@Sun.COM {
260310106SJason.Beloro@Sun.COM caddr_t pp;
260410106SJason.Beloro@Sun.COM pgcnt_t metapgs;
260510106SJason.Beloro@Sun.COM
260610106SJason.Beloro@Sun.COM ASSERT(memseg_is_dynamic(seg));
260710106SJason.Beloro@Sun.COM ASSERT(pp_dummy != NULL);
260810106SJason.Beloro@Sun.COM
260910106SJason.Beloro@Sun.COM
261010106SJason.Beloro@Sun.COM if (!memseg_includes_meta(seg)) {
261110106SJason.Beloro@Sun.COM memseg_remap_meta(seg);
261210106SJason.Beloro@Sun.COM return;
261310106SJason.Beloro@Sun.COM }
261410106SJason.Beloro@Sun.COM
261510106SJason.Beloro@Sun.COM pp = (caddr_t)seg->pages;
261610106SJason.Beloro@Sun.COM metapgs = seg->pages_base - memseg_get_start(seg);
261710106SJason.Beloro@Sun.COM ASSERT(metapgs != 0);
261810106SJason.Beloro@Sun.COM
261910106SJason.Beloro@Sun.COM seg->pages_end = seg->pages_base;
262010106SJason.Beloro@Sun.COM
262110106SJason.Beloro@Sun.COM remap_to_dummy(pp, metapgs);
262210106SJason.Beloro@Sun.COM }
262310106SJason.Beloro@Sun.COM
26240Sstevel@tonic-gate /*
26250Sstevel@tonic-gate * Transition all the deleted pages to the deleted state so that
26260Sstevel@tonic-gate * page_lock will not wait. The page_lock_delete call will
26270Sstevel@tonic-gate * also wake up any waiters.
26280Sstevel@tonic-gate */
26290Sstevel@tonic-gate static void
memseg_lock_delete_all(struct memseg * seg)26300Sstevel@tonic-gate memseg_lock_delete_all(struct memseg *seg)
26310Sstevel@tonic-gate {
26320Sstevel@tonic-gate page_t *pp;
26330Sstevel@tonic-gate
26340Sstevel@tonic-gate for (pp = seg->pages; pp < seg->epages; pp++) {
26350Sstevel@tonic-gate pp->p_pagenum = PFN_INVALID; /* XXXX */
26360Sstevel@tonic-gate page_lock_delete(pp);
26370Sstevel@tonic-gate }
26380Sstevel@tonic-gate }
26390Sstevel@tonic-gate
26400Sstevel@tonic-gate static void
kphysm_del_cleanup(struct mem_handle * mhp)26410Sstevel@tonic-gate kphysm_del_cleanup(struct mem_handle *mhp)
26420Sstevel@tonic-gate {
26430Sstevel@tonic-gate struct memdelspan *mdsp;
26440Sstevel@tonic-gate struct memseg *seg;
26450Sstevel@tonic-gate struct memseg **segpp;
26460Sstevel@tonic-gate struct memseg *seglist;
26470Sstevel@tonic-gate pfn_t p_end;
26480Sstevel@tonic-gate uint64_t avmem;
26490Sstevel@tonic-gate pgcnt_t avpgs;
26500Sstevel@tonic-gate pgcnt_t npgs;
26510Sstevel@tonic-gate
26520Sstevel@tonic-gate avpgs = mhp->mh_vm_pages;
26530Sstevel@tonic-gate
26540Sstevel@tonic-gate memsegs_lock(1);
26550Sstevel@tonic-gate
26560Sstevel@tonic-gate /*
26570Sstevel@tonic-gate * remove from main segment list.
26580Sstevel@tonic-gate */
26590Sstevel@tonic-gate npgs = 0;
26600Sstevel@tonic-gate seglist = NULL;
26610Sstevel@tonic-gate for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
26620Sstevel@tonic-gate mdsp = mdsp->mds_next) {
26630Sstevel@tonic-gate p_end = mdsp->mds_base + mdsp->mds_npgs;
26640Sstevel@tonic-gate for (segpp = &memsegs; (seg = *segpp) != NULL; ) {
26650Sstevel@tonic-gate if (seg->pages_base >= p_end ||
26660Sstevel@tonic-gate seg->pages_end <= mdsp->mds_base) {
26670Sstevel@tonic-gate /* Span and memseg don't overlap. */
26680Sstevel@tonic-gate segpp = &((*segpp)->next);
26690Sstevel@tonic-gate continue;
26700Sstevel@tonic-gate }
26710Sstevel@tonic-gate ASSERT(seg->pages_base >= mdsp->mds_base);
26720Sstevel@tonic-gate ASSERT(seg->pages_end <= p_end);
26730Sstevel@tonic-gate
26741373Skchow PLCNT_MODIFY_MAX(seg->pages_base,
26751373Skchow seg->pages_base - seg->pages_end);
26761373Skchow
26770Sstevel@tonic-gate /* Hide the memseg from future scans. */
26780Sstevel@tonic-gate hat_kpm_delmem_mseg_update(seg, segpp);
26790Sstevel@tonic-gate *segpp = seg->next;
26800Sstevel@tonic-gate membar_producer(); /* TODO: Needed? */
26810Sstevel@tonic-gate npgs += MSEG_NPAGES(seg);
26820Sstevel@tonic-gate
26830Sstevel@tonic-gate /*
26840Sstevel@tonic-gate * Leave the deleted segment's next pointer intact
26850Sstevel@tonic-gate * in case a memsegs scanning loop is walking this
26860Sstevel@tonic-gate * segment concurrently.
26870Sstevel@tonic-gate */
26880Sstevel@tonic-gate seg->lnext = seglist;
26890Sstevel@tonic-gate seglist = seg;
26900Sstevel@tonic-gate }
26910Sstevel@tonic-gate }
26920Sstevel@tonic-gate
26930Sstevel@tonic-gate build_pfn_hash();
26940Sstevel@tonic-gate
26950Sstevel@tonic-gate ASSERT(npgs < total_pages);
26960Sstevel@tonic-gate total_pages -= npgs;
26970Sstevel@tonic-gate
26980Sstevel@tonic-gate /*
26990Sstevel@tonic-gate * Recalculate the paging parameters now total_pages has changed.
27000Sstevel@tonic-gate * This will also cause the clock hands to be reset before next use.
27010Sstevel@tonic-gate */
27020Sstevel@tonic-gate setupclock(1);
27030Sstevel@tonic-gate
27040Sstevel@tonic-gate memsegs_unlock(1);
27050Sstevel@tonic-gate
27060Sstevel@tonic-gate mutex_exit(&mhp->mh_mutex);
27070Sstevel@tonic-gate
27080Sstevel@tonic-gate while ((seg = seglist) != NULL) {
27090Sstevel@tonic-gate pfn_t mseg_start;
27100Sstevel@tonic-gate pfn_t mseg_base, mseg_end;
27110Sstevel@tonic-gate pgcnt_t mseg_npgs;
27120Sstevel@tonic-gate int mlret;
27130Sstevel@tonic-gate
27140Sstevel@tonic-gate seglist = seg->lnext;
27150Sstevel@tonic-gate
27160Sstevel@tonic-gate /*
27170Sstevel@tonic-gate * Put the page_t's into the deleted state to stop
27180Sstevel@tonic-gate * cv_wait()s on the pages. When we remap, the dummy
27190Sstevel@tonic-gate * page_t's will be in the same state.
27200Sstevel@tonic-gate */
27210Sstevel@tonic-gate memseg_lock_delete_all(seg);
27220Sstevel@tonic-gate /*
27230Sstevel@tonic-gate * Collect up information based on pages_base and pages_end
27240Sstevel@tonic-gate * early so that we can flag early that the memseg has been
27250Sstevel@tonic-gate * deleted by setting pages_end == pages_base.
27260Sstevel@tonic-gate */
27270Sstevel@tonic-gate mseg_base = seg->pages_base;
27280Sstevel@tonic-gate mseg_end = seg->pages_end;
27290Sstevel@tonic-gate mseg_npgs = MSEG_NPAGES(seg);
273010106SJason.Beloro@Sun.COM mseg_start = memseg_get_start(seg);
273110106SJason.Beloro@Sun.COM
273210106SJason.Beloro@Sun.COM if (memseg_is_dynamic(seg)) {
27330Sstevel@tonic-gate /* Remap the meta data to our special dummy area. */
273410106SJason.Beloro@Sun.COM memseg_remap_to_dummy(seg);
27350Sstevel@tonic-gate
27360Sstevel@tonic-gate mutex_enter(&memseg_lists_lock);
27370Sstevel@tonic-gate seg->lnext = memseg_va_avail;
27380Sstevel@tonic-gate memseg_va_avail = seg;
27390Sstevel@tonic-gate mutex_exit(&memseg_lists_lock);
27400Sstevel@tonic-gate } else {
27410Sstevel@tonic-gate /*
27420Sstevel@tonic-gate * For memory whose page_ts were allocated
27430Sstevel@tonic-gate * at boot, we need to find a new use for
27440Sstevel@tonic-gate * the page_t memory.
27450Sstevel@tonic-gate * For the moment, just leak it.
27460Sstevel@tonic-gate * (It is held in the memseg_delete_junk list.)
27470Sstevel@tonic-gate */
274810106SJason.Beloro@Sun.COM seg->pages_end = seg->pages_base;
27490Sstevel@tonic-gate
27500Sstevel@tonic-gate mutex_enter(&memseg_lists_lock);
27510Sstevel@tonic-gate seg->lnext = memseg_delete_junk;
27520Sstevel@tonic-gate memseg_delete_junk = seg;
27530Sstevel@tonic-gate mutex_exit(&memseg_lists_lock);
27540Sstevel@tonic-gate }
27550Sstevel@tonic-gate
27560Sstevel@tonic-gate /* Must not use seg now as it could be re-used. */
27570Sstevel@tonic-gate
27580Sstevel@tonic-gate memlist_write_lock();
27590Sstevel@tonic-gate
27600Sstevel@tonic-gate mlret = memlist_delete_span(
27610Sstevel@tonic-gate (uint64_t)(mseg_base) << PAGESHIFT,
27620Sstevel@tonic-gate (uint64_t)(mseg_npgs) << PAGESHIFT,
27630Sstevel@tonic-gate &phys_avail);
27640Sstevel@tonic-gate ASSERT(mlret == MEML_SPANOP_OK);
27650Sstevel@tonic-gate
27660Sstevel@tonic-gate mlret = memlist_delete_span(
27670Sstevel@tonic-gate (uint64_t)(mseg_start) << PAGESHIFT,
27680Sstevel@tonic-gate (uint64_t)(mseg_end - mseg_start) <<
27690Sstevel@tonic-gate PAGESHIFT,
27700Sstevel@tonic-gate &phys_install);
27710Sstevel@tonic-gate ASSERT(mlret == MEML_SPANOP_OK);
27720Sstevel@tonic-gate phys_install_has_changed();
27730Sstevel@tonic-gate
27740Sstevel@tonic-gate memlist_write_unlock();
27750Sstevel@tonic-gate }
27760Sstevel@tonic-gate
27770Sstevel@tonic-gate memlist_read_lock();
27780Sstevel@tonic-gate installed_top_size(phys_install, &physmax, &physinstalled);
27790Sstevel@tonic-gate memlist_read_unlock();
27800Sstevel@tonic-gate
27810Sstevel@tonic-gate mutex_enter(&freemem_lock);
27820Sstevel@tonic-gate maxmem -= avpgs;
27830Sstevel@tonic-gate physmem -= avpgs;
27840Sstevel@tonic-gate /* availrmem is adjusted during the delete. */
27850Sstevel@tonic-gate availrmem_initial -= avpgs;
27860Sstevel@tonic-gate
27870Sstevel@tonic-gate mutex_exit(&freemem_lock);
27880Sstevel@tonic-gate
27890Sstevel@tonic-gate dump_resize();
27900Sstevel@tonic-gate
27910Sstevel@tonic-gate cmn_err(CE_CONT, "?kphysm_delete: mem = %ldK "
27920Sstevel@tonic-gate "(0x%" PRIx64 ")\n",
27930Sstevel@tonic-gate physinstalled << (PAGESHIFT - 10),
27940Sstevel@tonic-gate (uint64_t)physinstalled << PAGESHIFT);
27950Sstevel@tonic-gate
27960Sstevel@tonic-gate avmem = (uint64_t)freemem << PAGESHIFT;
27970Sstevel@tonic-gate cmn_err(CE_CONT, "?kphysm_delete: "
27980Sstevel@tonic-gate "avail mem = %" PRId64 "\n", avmem);
27990Sstevel@tonic-gate
28000Sstevel@tonic-gate /*
28010Sstevel@tonic-gate * Update lgroup generation number on single lgroup systems
28020Sstevel@tonic-gate */
28030Sstevel@tonic-gate if (nlgrps == 1)
28040Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_GEN_UPDATE, 0, 0);
28050Sstevel@tonic-gate
28060Sstevel@tonic-gate /* Successfully deleted system memory */
28070Sstevel@tonic-gate mutex_enter(&mhp->mh_mutex);
28080Sstevel@tonic-gate }
28090Sstevel@tonic-gate
28100Sstevel@tonic-gate static uint_t mdel_nullvp_waiter;
28110Sstevel@tonic-gate
28120Sstevel@tonic-gate static void
page_delete_collect(page_t * pp,struct mem_handle * mhp)28130Sstevel@tonic-gate page_delete_collect(
28140Sstevel@tonic-gate page_t *pp,
28150Sstevel@tonic-gate struct mem_handle *mhp)
28160Sstevel@tonic-gate {
28170Sstevel@tonic-gate if (pp->p_vnode) {
28180Sstevel@tonic-gate page_hashout(pp, (kmutex_t *)NULL);
28190Sstevel@tonic-gate /* do not do PP_SETAGED(pp); */
28200Sstevel@tonic-gate } else {
28210Sstevel@tonic-gate kmutex_t *sep;
28220Sstevel@tonic-gate
28230Sstevel@tonic-gate sep = page_se_mutex(pp);
28240Sstevel@tonic-gate mutex_enter(sep);
28250Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) {
28260Sstevel@tonic-gate mdel_nullvp_waiter++;
28270Sstevel@tonic-gate cv_broadcast(&pp->p_cv);
28280Sstevel@tonic-gate }
28290Sstevel@tonic-gate mutex_exit(sep);
28300Sstevel@tonic-gate }
28310Sstevel@tonic-gate ASSERT(pp->p_next == pp->p_prev);
28320Sstevel@tonic-gate ASSERT(pp->p_next == NULL || pp->p_next == pp);
28330Sstevel@tonic-gate pp->p_next = mhp->mh_deleted;
28340Sstevel@tonic-gate mhp->mh_deleted = pp;
28350Sstevel@tonic-gate ASSERT(mhp->mh_hold_todo != 0);
28360Sstevel@tonic-gate mhp->mh_hold_todo--;
28370Sstevel@tonic-gate }
28380Sstevel@tonic-gate
28390Sstevel@tonic-gate static void
transit_list_collect(struct mem_handle * mhp,int v)28400Sstevel@tonic-gate transit_list_collect(struct mem_handle *mhp, int v)
28410Sstevel@tonic-gate {
28420Sstevel@tonic-gate struct transit_list_head *trh;
28430Sstevel@tonic-gate
28440Sstevel@tonic-gate trh = &transit_list_head;
28450Sstevel@tonic-gate mutex_enter(&trh->trh_lock);
28460Sstevel@tonic-gate mhp->mh_transit.trl_collect = v;
28470Sstevel@tonic-gate mutex_exit(&trh->trh_lock);
28480Sstevel@tonic-gate }
28490Sstevel@tonic-gate
28500Sstevel@tonic-gate static void
transit_list_insert(struct transit_list * tlp)28510Sstevel@tonic-gate transit_list_insert(struct transit_list *tlp)
28520Sstevel@tonic-gate {
28530Sstevel@tonic-gate struct transit_list_head *trh;
28540Sstevel@tonic-gate
28550Sstevel@tonic-gate trh = &transit_list_head;
28560Sstevel@tonic-gate ASSERT(MUTEX_HELD(&trh->trh_lock));
28570Sstevel@tonic-gate tlp->trl_next = trh->trh_head;
28580Sstevel@tonic-gate trh->trh_head = tlp;
28590Sstevel@tonic-gate }
28600Sstevel@tonic-gate
28610Sstevel@tonic-gate static void
transit_list_remove(struct transit_list * tlp)28620Sstevel@tonic-gate transit_list_remove(struct transit_list *tlp)
28630Sstevel@tonic-gate {
28640Sstevel@tonic-gate struct transit_list_head *trh;
28650Sstevel@tonic-gate struct transit_list **tlpp;
28660Sstevel@tonic-gate
28670Sstevel@tonic-gate trh = &transit_list_head;
28680Sstevel@tonic-gate tlpp = &trh->trh_head;
28690Sstevel@tonic-gate ASSERT(MUTEX_HELD(&trh->trh_lock));
28700Sstevel@tonic-gate while (*tlpp != NULL && *tlpp != tlp)
28710Sstevel@tonic-gate tlpp = &(*tlpp)->trl_next;
28720Sstevel@tonic-gate ASSERT(*tlpp != NULL);
28730Sstevel@tonic-gate if (*tlpp == tlp)
28740Sstevel@tonic-gate *tlpp = tlp->trl_next;
28750Sstevel@tonic-gate tlp->trl_next = NULL;
28760Sstevel@tonic-gate }
28770Sstevel@tonic-gate
28780Sstevel@tonic-gate static struct transit_list *
pfnum_to_transit_list(struct transit_list_head * trh,pfn_t pfnum)28790Sstevel@tonic-gate pfnum_to_transit_list(struct transit_list_head *trh, pfn_t pfnum)
28800Sstevel@tonic-gate {
28810Sstevel@tonic-gate struct transit_list *tlp;
28820Sstevel@tonic-gate
28830Sstevel@tonic-gate for (tlp = trh->trh_head; tlp != NULL; tlp = tlp->trl_next) {
28840Sstevel@tonic-gate struct memdelspan *mdsp;
28850Sstevel@tonic-gate
28860Sstevel@tonic-gate for (mdsp = tlp->trl_spans; mdsp != NULL;
28870Sstevel@tonic-gate mdsp = mdsp->mds_next) {
28880Sstevel@tonic-gate if (pfnum >= mdsp->mds_base &&
28890Sstevel@tonic-gate pfnum < (mdsp->mds_base + mdsp->mds_npgs)) {
28900Sstevel@tonic-gate return (tlp);
28910Sstevel@tonic-gate }
28920Sstevel@tonic-gate }
28930Sstevel@tonic-gate }
28940Sstevel@tonic-gate return (NULL);
28950Sstevel@tonic-gate }
28960Sstevel@tonic-gate
28970Sstevel@tonic-gate int
pfn_is_being_deleted(pfn_t pfnum)28980Sstevel@tonic-gate pfn_is_being_deleted(pfn_t pfnum)
28990Sstevel@tonic-gate {
29000Sstevel@tonic-gate struct transit_list_head *trh;
29010Sstevel@tonic-gate struct transit_list *tlp;
29020Sstevel@tonic-gate int ret;
29030Sstevel@tonic-gate
29040Sstevel@tonic-gate trh = &transit_list_head;
29050Sstevel@tonic-gate if (trh->trh_head == NULL)
29060Sstevel@tonic-gate return (0);
29070Sstevel@tonic-gate
29080Sstevel@tonic-gate mutex_enter(&trh->trh_lock);
29090Sstevel@tonic-gate tlp = pfnum_to_transit_list(trh, pfnum);
29100Sstevel@tonic-gate ret = (tlp != NULL && tlp->trl_collect);
29110Sstevel@tonic-gate mutex_exit(&trh->trh_lock);
29120Sstevel@tonic-gate
29130Sstevel@tonic-gate return (ret);
29140Sstevel@tonic-gate }
29150Sstevel@tonic-gate
29160Sstevel@tonic-gate #ifdef MEM_DEL_STATS
29170Sstevel@tonic-gate extern int hz;
29180Sstevel@tonic-gate static void
mem_del_stat_print_func(struct mem_handle * mhp)29190Sstevel@tonic-gate mem_del_stat_print_func(struct mem_handle *mhp)
29200Sstevel@tonic-gate {
29210Sstevel@tonic-gate uint64_t tmp;
29220Sstevel@tonic-gate
29230Sstevel@tonic-gate if (mem_del_stat_print) {
29240Sstevel@tonic-gate printf("memory delete loop %x/%x, statistics%s\n",
29250Sstevel@tonic-gate (uint_t)mhp->mh_transit.trl_spans->mds_base,
29260Sstevel@tonic-gate (uint_t)mhp->mh_transit.trl_spans->mds_npgs,
29270Sstevel@tonic-gate (mhp->mh_cancel ? " (cancelled)" : ""));
29280Sstevel@tonic-gate printf("\t%8u nloop\n", mhp->mh_delstat.nloop);
29290Sstevel@tonic-gate printf("\t%8u need_free\n", mhp->mh_delstat.need_free);
29300Sstevel@tonic-gate printf("\t%8u free_loop\n", mhp->mh_delstat.free_loop);
29310Sstevel@tonic-gate printf("\t%8u free_low\n", mhp->mh_delstat.free_low);
29320Sstevel@tonic-gate printf("\t%8u free_failed\n", mhp->mh_delstat.free_failed);
29330Sstevel@tonic-gate printf("\t%8u ncheck\n", mhp->mh_delstat.ncheck);
29340Sstevel@tonic-gate printf("\t%8u nopaget\n", mhp->mh_delstat.nopaget);
29350Sstevel@tonic-gate printf("\t%8u lockfail\n", mhp->mh_delstat.lockfail);
29360Sstevel@tonic-gate printf("\t%8u nfree\n", mhp->mh_delstat.nfree);
29370Sstevel@tonic-gate printf("\t%8u nreloc\n", mhp->mh_delstat.nreloc);
29380Sstevel@tonic-gate printf("\t%8u nrelocfail\n", mhp->mh_delstat.nrelocfail);
29390Sstevel@tonic-gate printf("\t%8u already_done\n", mhp->mh_delstat.already_done);
29400Sstevel@tonic-gate printf("\t%8u first_notfree\n", mhp->mh_delstat.first_notfree);
29410Sstevel@tonic-gate printf("\t%8u npplocked\n", mhp->mh_delstat.npplocked);
29420Sstevel@tonic-gate printf("\t%8u nlockreloc\n", mhp->mh_delstat.nlockreloc);
29430Sstevel@tonic-gate printf("\t%8u nnorepl\n", mhp->mh_delstat.nnorepl);
29440Sstevel@tonic-gate printf("\t%8u nmodreloc\n", mhp->mh_delstat.nmodreloc);
29450Sstevel@tonic-gate printf("\t%8u ndestroy\n", mhp->mh_delstat.ndestroy);
29460Sstevel@tonic-gate printf("\t%8u nputpage\n", mhp->mh_delstat.nputpage);
29470Sstevel@tonic-gate printf("\t%8u nnoreclaim\n", mhp->mh_delstat.nnoreclaim);
29480Sstevel@tonic-gate printf("\t%8u ndelay\n", mhp->mh_delstat.ndelay);
29490Sstevel@tonic-gate printf("\t%8u demotefail\n", mhp->mh_delstat.demotefail);
29500Sstevel@tonic-gate printf("\t%8u retired\n", mhp->mh_delstat.retired);
29510Sstevel@tonic-gate printf("\t%8u toxic\n", mhp->mh_delstat.toxic);
29520Sstevel@tonic-gate printf("\t%8u failing\n", mhp->mh_delstat.failing);
29530Sstevel@tonic-gate printf("\t%8u modtoxic\n", mhp->mh_delstat.modtoxic);
29540Sstevel@tonic-gate printf("\t%8u npplkdtoxic\n", mhp->mh_delstat.npplkdtoxic);
29550Sstevel@tonic-gate printf("\t%8u gptlmodfail\n", mhp->mh_delstat.gptlmodfail);
29560Sstevel@tonic-gate printf("\t%8u gptllckfail\n", mhp->mh_delstat.gptllckfail);
29570Sstevel@tonic-gate tmp = mhp->mh_delstat.nticks_total / hz; /* seconds */
29580Sstevel@tonic-gate printf(
29590Sstevel@tonic-gate "\t%"PRIu64" nticks_total - %"PRIu64" min %"PRIu64" sec\n",
29600Sstevel@tonic-gate mhp->mh_delstat.nticks_total, tmp / 60, tmp % 60);
29610Sstevel@tonic-gate
29620Sstevel@tonic-gate tmp = mhp->mh_delstat.nticks_pgrp / hz; /* seconds */
29630Sstevel@tonic-gate printf(
29640Sstevel@tonic-gate "\t%"PRIu64" nticks_pgrp - %"PRIu64" min %"PRIu64" sec\n",
29650Sstevel@tonic-gate mhp->mh_delstat.nticks_pgrp, tmp / 60, tmp % 60);
29660Sstevel@tonic-gate }
29670Sstevel@tonic-gate }
29680Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
29690Sstevel@tonic-gate
29700Sstevel@tonic-gate struct mem_callback {
29710Sstevel@tonic-gate kphysm_setup_vector_t *vec;
29720Sstevel@tonic-gate void *arg;
29730Sstevel@tonic-gate };
29740Sstevel@tonic-gate
29750Sstevel@tonic-gate #define NMEMCALLBACKS 100
29760Sstevel@tonic-gate
29770Sstevel@tonic-gate static struct mem_callback mem_callbacks[NMEMCALLBACKS];
29780Sstevel@tonic-gate static uint_t nmemcallbacks;
29790Sstevel@tonic-gate static krwlock_t mem_callback_rwlock;
29800Sstevel@tonic-gate
29810Sstevel@tonic-gate int
kphysm_setup_func_register(kphysm_setup_vector_t * vec,void * arg)29820Sstevel@tonic-gate kphysm_setup_func_register(kphysm_setup_vector_t *vec, void *arg)
29830Sstevel@tonic-gate {
29840Sstevel@tonic-gate uint_t i, found;
29850Sstevel@tonic-gate
29860Sstevel@tonic-gate /*
29870Sstevel@tonic-gate * This test will become more complicated when the version must
29880Sstevel@tonic-gate * change.
29890Sstevel@tonic-gate */
29900Sstevel@tonic-gate if (vec->version != KPHYSM_SETUP_VECTOR_VERSION)
29910Sstevel@tonic-gate return (EINVAL);
29920Sstevel@tonic-gate
29930Sstevel@tonic-gate if (vec->post_add == NULL || vec->pre_del == NULL ||
29940Sstevel@tonic-gate vec->post_del == NULL)
29950Sstevel@tonic-gate return (EINVAL);
29960Sstevel@tonic-gate
29970Sstevel@tonic-gate rw_enter(&mem_callback_rwlock, RW_WRITER);
29980Sstevel@tonic-gate for (i = 0, found = 0; i < nmemcallbacks; i++) {
29990Sstevel@tonic-gate if (mem_callbacks[i].vec == NULL && found == 0)
30000Sstevel@tonic-gate found = i + 1;
30010Sstevel@tonic-gate if (mem_callbacks[i].vec == vec &&
30020Sstevel@tonic-gate mem_callbacks[i].arg == arg) {
30030Sstevel@tonic-gate #ifdef DEBUG
30040Sstevel@tonic-gate /* Catch this in DEBUG kernels. */
30050Sstevel@tonic-gate cmn_err(CE_WARN, "kphysm_setup_func_register"
30060Sstevel@tonic-gate "(0x%p, 0x%p) duplicate registration from 0x%p",
30070Sstevel@tonic-gate (void *)vec, arg, (void *)caller());
30080Sstevel@tonic-gate #endif /* DEBUG */
30090Sstevel@tonic-gate rw_exit(&mem_callback_rwlock);
30100Sstevel@tonic-gate return (EEXIST);
30110Sstevel@tonic-gate }
30120Sstevel@tonic-gate }
30130Sstevel@tonic-gate if (found != 0) {
30140Sstevel@tonic-gate i = found - 1;
30150Sstevel@tonic-gate } else {
30160Sstevel@tonic-gate ASSERT(nmemcallbacks < NMEMCALLBACKS);
30170Sstevel@tonic-gate if (nmemcallbacks == NMEMCALLBACKS) {
30180Sstevel@tonic-gate rw_exit(&mem_callback_rwlock);
30190Sstevel@tonic-gate return (ENOMEM);
30200Sstevel@tonic-gate }
30210Sstevel@tonic-gate i = nmemcallbacks++;
30220Sstevel@tonic-gate }
30230Sstevel@tonic-gate mem_callbacks[i].vec = vec;
30240Sstevel@tonic-gate mem_callbacks[i].arg = arg;
30250Sstevel@tonic-gate rw_exit(&mem_callback_rwlock);
30260Sstevel@tonic-gate return (0);
30270Sstevel@tonic-gate }
30280Sstevel@tonic-gate
30290Sstevel@tonic-gate void
kphysm_setup_func_unregister(kphysm_setup_vector_t * vec,void * arg)30300Sstevel@tonic-gate kphysm_setup_func_unregister(kphysm_setup_vector_t *vec, void *arg)
30310Sstevel@tonic-gate {
30320Sstevel@tonic-gate uint_t i;
30330Sstevel@tonic-gate
30340Sstevel@tonic-gate rw_enter(&mem_callback_rwlock, RW_WRITER);
30350Sstevel@tonic-gate for (i = 0; i < nmemcallbacks; i++) {
30360Sstevel@tonic-gate if (mem_callbacks[i].vec == vec &&
30370Sstevel@tonic-gate mem_callbacks[i].arg == arg) {
30380Sstevel@tonic-gate mem_callbacks[i].vec = NULL;
30390Sstevel@tonic-gate mem_callbacks[i].arg = NULL;
30400Sstevel@tonic-gate if (i == (nmemcallbacks - 1))
30410Sstevel@tonic-gate nmemcallbacks--;
30420Sstevel@tonic-gate break;
30430Sstevel@tonic-gate }
30440Sstevel@tonic-gate }
30450Sstevel@tonic-gate rw_exit(&mem_callback_rwlock);
30460Sstevel@tonic-gate }
30470Sstevel@tonic-gate
30480Sstevel@tonic-gate static void
kphysm_setup_post_add(pgcnt_t delta_pages)30490Sstevel@tonic-gate kphysm_setup_post_add(pgcnt_t delta_pages)
30500Sstevel@tonic-gate {
30510Sstevel@tonic-gate uint_t i;
30520Sstevel@tonic-gate
30530Sstevel@tonic-gate rw_enter(&mem_callback_rwlock, RW_READER);
30540Sstevel@tonic-gate for (i = 0; i < nmemcallbacks; i++) {
30550Sstevel@tonic-gate if (mem_callbacks[i].vec != NULL) {
30560Sstevel@tonic-gate (*mem_callbacks[i].vec->post_add)
30570Sstevel@tonic-gate (mem_callbacks[i].arg, delta_pages);
30580Sstevel@tonic-gate }
30590Sstevel@tonic-gate }
30600Sstevel@tonic-gate rw_exit(&mem_callback_rwlock);
30610Sstevel@tonic-gate }
30620Sstevel@tonic-gate
30630Sstevel@tonic-gate /*
30640Sstevel@tonic-gate * Note the locking between pre_del and post_del: The reader lock is held
30650Sstevel@tonic-gate * between the two calls to stop the set of functions from changing.
30660Sstevel@tonic-gate */
30670Sstevel@tonic-gate
30680Sstevel@tonic-gate static int
kphysm_setup_pre_del(pgcnt_t delta_pages)30690Sstevel@tonic-gate kphysm_setup_pre_del(pgcnt_t delta_pages)
30700Sstevel@tonic-gate {
30710Sstevel@tonic-gate uint_t i;
30720Sstevel@tonic-gate int ret;
30730Sstevel@tonic-gate int aret;
30740Sstevel@tonic-gate
30750Sstevel@tonic-gate ret = 0;
30760Sstevel@tonic-gate rw_enter(&mem_callback_rwlock, RW_READER);
30770Sstevel@tonic-gate for (i = 0; i < nmemcallbacks; i++) {
30780Sstevel@tonic-gate if (mem_callbacks[i].vec != NULL) {
30790Sstevel@tonic-gate aret = (*mem_callbacks[i].vec->pre_del)
30800Sstevel@tonic-gate (mem_callbacks[i].arg, delta_pages);
30810Sstevel@tonic-gate ret |= aret;
30820Sstevel@tonic-gate }
30830Sstevel@tonic-gate }
30840Sstevel@tonic-gate
30850Sstevel@tonic-gate return (ret);
30860Sstevel@tonic-gate }
30870Sstevel@tonic-gate
30880Sstevel@tonic-gate static void
kphysm_setup_post_del(pgcnt_t delta_pages,int cancelled)30890Sstevel@tonic-gate kphysm_setup_post_del(pgcnt_t delta_pages, int cancelled)
30900Sstevel@tonic-gate {
30910Sstevel@tonic-gate uint_t i;
30920Sstevel@tonic-gate
30930Sstevel@tonic-gate for (i = 0; i < nmemcallbacks; i++) {
30940Sstevel@tonic-gate if (mem_callbacks[i].vec != NULL) {
30950Sstevel@tonic-gate (*mem_callbacks[i].vec->post_del)
30960Sstevel@tonic-gate (mem_callbacks[i].arg, delta_pages, cancelled);
30970Sstevel@tonic-gate }
30980Sstevel@tonic-gate }
30990Sstevel@tonic-gate rw_exit(&mem_callback_rwlock);
31000Sstevel@tonic-gate }
31010Sstevel@tonic-gate
31020Sstevel@tonic-gate static int
kphysm_split_memseg(pfn_t base,pgcnt_t npgs)31030Sstevel@tonic-gate kphysm_split_memseg(
31040Sstevel@tonic-gate pfn_t base,
31050Sstevel@tonic-gate pgcnt_t npgs)
31060Sstevel@tonic-gate {
31070Sstevel@tonic-gate struct memseg *seg;
31080Sstevel@tonic-gate struct memseg **segpp;
31090Sstevel@tonic-gate pgcnt_t size_low, size_high;
31100Sstevel@tonic-gate struct memseg *seg_low, *seg_mid, *seg_high;
31110Sstevel@tonic-gate
31120Sstevel@tonic-gate /*
31130Sstevel@tonic-gate * Lock the memsegs list against other updates now
31140Sstevel@tonic-gate */
31150Sstevel@tonic-gate memsegs_lock(1);
31160Sstevel@tonic-gate
31170Sstevel@tonic-gate /*
31180Sstevel@tonic-gate * Find boot time memseg that wholly covers this area.
31190Sstevel@tonic-gate */
31200Sstevel@tonic-gate
31210Sstevel@tonic-gate /* First find the memseg with page 'base' in it. */
31220Sstevel@tonic-gate for (segpp = &memsegs; (seg = *segpp) != NULL;
31230Sstevel@tonic-gate segpp = &((*segpp)->next)) {
31240Sstevel@tonic-gate if (base >= seg->pages_base && base < seg->pages_end)
31250Sstevel@tonic-gate break;
31260Sstevel@tonic-gate }
31270Sstevel@tonic-gate if (seg == NULL) {
31280Sstevel@tonic-gate memsegs_unlock(1);
31290Sstevel@tonic-gate return (0);
31300Sstevel@tonic-gate }
313110106SJason.Beloro@Sun.COM if (memseg_includes_meta(seg)) {
31320Sstevel@tonic-gate memsegs_unlock(1);
31330Sstevel@tonic-gate return (0);
31340Sstevel@tonic-gate }
31350Sstevel@tonic-gate if ((base + npgs) > seg->pages_end) {
31360Sstevel@tonic-gate memsegs_unlock(1);
31370Sstevel@tonic-gate return (0);
31380Sstevel@tonic-gate }
31390Sstevel@tonic-gate
31400Sstevel@tonic-gate /*
31410Sstevel@tonic-gate * Work out the size of the two segments that will
31420Sstevel@tonic-gate * surround the new segment, one for low address
31430Sstevel@tonic-gate * and one for high.
31440Sstevel@tonic-gate */
31450Sstevel@tonic-gate ASSERT(base >= seg->pages_base);
31460Sstevel@tonic-gate size_low = base - seg->pages_base;
31470Sstevel@tonic-gate ASSERT(seg->pages_end >= (base + npgs));
31480Sstevel@tonic-gate size_high = seg->pages_end - (base + npgs);
31490Sstevel@tonic-gate
31500Sstevel@tonic-gate /*
31510Sstevel@tonic-gate * Sanity check.
31520Sstevel@tonic-gate */
31530Sstevel@tonic-gate if ((size_low + size_high) == 0) {
31540Sstevel@tonic-gate memsegs_unlock(1);
31550Sstevel@tonic-gate return (0);
31560Sstevel@tonic-gate }
31570Sstevel@tonic-gate
31580Sstevel@tonic-gate /*
31590Sstevel@tonic-gate * Allocate the new structures. The old memseg will not be freed
31600Sstevel@tonic-gate * as there may be a reference to it.
31610Sstevel@tonic-gate */
31620Sstevel@tonic-gate seg_low = NULL;
31630Sstevel@tonic-gate seg_high = NULL;
31640Sstevel@tonic-gate
316510106SJason.Beloro@Sun.COM if (size_low != 0)
316610106SJason.Beloro@Sun.COM seg_low = memseg_alloc();
316710106SJason.Beloro@Sun.COM
316810106SJason.Beloro@Sun.COM seg_mid = memseg_alloc();
316910106SJason.Beloro@Sun.COM
317010106SJason.Beloro@Sun.COM if (size_high != 0)
317110106SJason.Beloro@Sun.COM seg_high = memseg_alloc();
31720Sstevel@tonic-gate
31730Sstevel@tonic-gate /*
31740Sstevel@tonic-gate * All allocation done now.
31750Sstevel@tonic-gate */
31760Sstevel@tonic-gate if (size_low != 0) {
31770Sstevel@tonic-gate seg_low->pages = seg->pages;
31780Sstevel@tonic-gate seg_low->epages = seg_low->pages + size_low;
31790Sstevel@tonic-gate seg_low->pages_base = seg->pages_base;
31800Sstevel@tonic-gate seg_low->pages_end = seg_low->pages_base + size_low;
31810Sstevel@tonic-gate seg_low->next = seg_mid;
318210106SJason.Beloro@Sun.COM seg_low->msegflags = seg->msegflags;
31830Sstevel@tonic-gate }
31840Sstevel@tonic-gate if (size_high != 0) {
31850Sstevel@tonic-gate seg_high->pages = seg->epages - size_high;
31860Sstevel@tonic-gate seg_high->epages = seg_high->pages + size_high;
31870Sstevel@tonic-gate seg_high->pages_base = seg->pages_end - size_high;
31880Sstevel@tonic-gate seg_high->pages_end = seg_high->pages_base + size_high;
31890Sstevel@tonic-gate seg_high->next = seg->next;
319010106SJason.Beloro@Sun.COM seg_high->msegflags = seg->msegflags;
31910Sstevel@tonic-gate }
31920Sstevel@tonic-gate
31930Sstevel@tonic-gate seg_mid->pages = seg->pages + size_low;
31940Sstevel@tonic-gate seg_mid->pages_base = seg->pages_base + size_low;
31950Sstevel@tonic-gate seg_mid->epages = seg->epages - size_high;
31960Sstevel@tonic-gate seg_mid->pages_end = seg->pages_end - size_high;
31970Sstevel@tonic-gate seg_mid->next = (seg_high != NULL) ? seg_high : seg->next;
319810106SJason.Beloro@Sun.COM seg_mid->msegflags = seg->msegflags;
31990Sstevel@tonic-gate
32000Sstevel@tonic-gate /*
32010Sstevel@tonic-gate * Update hat_kpm specific info of all involved memsegs and
32020Sstevel@tonic-gate * allow hat_kpm specific global chain updates.
32030Sstevel@tonic-gate */
32040Sstevel@tonic-gate hat_kpm_split_mseg_update(seg, segpp, seg_low, seg_mid, seg_high);
32050Sstevel@tonic-gate
32060Sstevel@tonic-gate /*
32070Sstevel@tonic-gate * At this point we have two equivalent memseg sub-chains,
32080Sstevel@tonic-gate * seg and seg_low/seg_mid/seg_high, which both chain on to
32090Sstevel@tonic-gate * the same place in the global chain. By re-writing the pointer
32100Sstevel@tonic-gate * in the previous element we switch atomically from using the old
32110Sstevel@tonic-gate * (seg) to the new.
32120Sstevel@tonic-gate */
32130Sstevel@tonic-gate *segpp = (seg_low != NULL) ? seg_low : seg_mid;
32140Sstevel@tonic-gate
32150Sstevel@tonic-gate membar_enter();
32160Sstevel@tonic-gate
32170Sstevel@tonic-gate build_pfn_hash();
32180Sstevel@tonic-gate memsegs_unlock(1);
32190Sstevel@tonic-gate
32200Sstevel@tonic-gate /*
32210Sstevel@tonic-gate * We leave the old segment, 'seg', intact as there may be
32220Sstevel@tonic-gate * references to it. Also, as the value of total_pages has not
32230Sstevel@tonic-gate * changed and the memsegs list is effectively the same when
32240Sstevel@tonic-gate * accessed via the old or the new pointer, we do not have to
32250Sstevel@tonic-gate * cause pageout_scanner() to re-evaluate its hand pointers.
32260Sstevel@tonic-gate *
32270Sstevel@tonic-gate * We currently do not re-use or reclaim the page_t memory.
32280Sstevel@tonic-gate * If we do, then this may have to change.
32290Sstevel@tonic-gate */
32300Sstevel@tonic-gate
32310Sstevel@tonic-gate mutex_enter(&memseg_lists_lock);
32320Sstevel@tonic-gate seg->lnext = memseg_edit_junk;
32330Sstevel@tonic-gate memseg_edit_junk = seg;
32340Sstevel@tonic-gate mutex_exit(&memseg_lists_lock);
32350Sstevel@tonic-gate
32360Sstevel@tonic-gate return (1);
32370Sstevel@tonic-gate }
32380Sstevel@tonic-gate
32390Sstevel@tonic-gate /*
32400Sstevel@tonic-gate * The sfmmu hat layer (e.g.) accesses some parts of the memseg
32410Sstevel@tonic-gate * structure using physical addresses. Therefore a kmem_cache is
32420Sstevel@tonic-gate * used with KMC_NOHASH to avoid page crossings within a memseg
32430Sstevel@tonic-gate * structure. KMC_NOHASH requires that no external (outside of
32440Sstevel@tonic-gate * slab) information is allowed. This, in turn, implies that the
32450Sstevel@tonic-gate * cache's slabsize must be exactly a single page, since per-slab
32460Sstevel@tonic-gate * information (e.g. the freelist for the slab) is kept at the
32470Sstevel@tonic-gate * end of the slab, where it is easy to locate. Should be changed
32480Sstevel@tonic-gate * when a more obvious kmem_cache interface/flag will become
32490Sstevel@tonic-gate * available.
32500Sstevel@tonic-gate */
32510Sstevel@tonic-gate void
mem_config_init()32520Sstevel@tonic-gate mem_config_init()
32530Sstevel@tonic-gate {
32540Sstevel@tonic-gate memseg_cache = kmem_cache_create("memseg_cache", sizeof (struct memseg),
32556242Smb158278 0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
32560Sstevel@tonic-gate }
325710106SJason.Beloro@Sun.COM
325810106SJason.Beloro@Sun.COM struct memseg *
memseg_alloc()325910106SJason.Beloro@Sun.COM memseg_alloc()
326010106SJason.Beloro@Sun.COM {
326110106SJason.Beloro@Sun.COM struct memseg *seg;
326210106SJason.Beloro@Sun.COM
326310106SJason.Beloro@Sun.COM seg = kmem_cache_alloc(memseg_cache, KM_SLEEP);
326410106SJason.Beloro@Sun.COM bzero(seg, sizeof (struct memseg));
326510106SJason.Beloro@Sun.COM
326610106SJason.Beloro@Sun.COM return (seg);
326710106SJason.Beloro@Sun.COM }
326810106SJason.Beloro@Sun.COM
326910106SJason.Beloro@Sun.COM /*
327010106SJason.Beloro@Sun.COM * Return whether the page_t memory for this memseg
327110106SJason.Beloro@Sun.COM * is included in the memseg itself.
327210106SJason.Beloro@Sun.COM */
327310106SJason.Beloro@Sun.COM static int
memseg_includes_meta(struct memseg * seg)327410106SJason.Beloro@Sun.COM memseg_includes_meta(struct memseg *seg)
327510106SJason.Beloro@Sun.COM {
327610106SJason.Beloro@Sun.COM return (seg->msegflags & MEMSEG_META_INCL);
327710106SJason.Beloro@Sun.COM }
327810106SJason.Beloro@Sun.COM
327910106SJason.Beloro@Sun.COM pfn_t
memseg_get_start(struct memseg * seg)328010106SJason.Beloro@Sun.COM memseg_get_start(struct memseg *seg)
328110106SJason.Beloro@Sun.COM {
328210106SJason.Beloro@Sun.COM pfn_t pt_start;
328310106SJason.Beloro@Sun.COM
328410106SJason.Beloro@Sun.COM if (memseg_includes_meta(seg)) {
328510106SJason.Beloro@Sun.COM pt_start = hat_getpfnum(kas.a_hat, (caddr_t)seg->pages);
328610106SJason.Beloro@Sun.COM
328710106SJason.Beloro@Sun.COM /* Meta data is required to be at the beginning */
328810106SJason.Beloro@Sun.COM ASSERT(pt_start < seg->pages_base);
328910106SJason.Beloro@Sun.COM } else
329010106SJason.Beloro@Sun.COM pt_start = seg->pages_base;
329110106SJason.Beloro@Sun.COM
329210106SJason.Beloro@Sun.COM return (pt_start);
329310106SJason.Beloro@Sun.COM }
329410106SJason.Beloro@Sun.COM
329510106SJason.Beloro@Sun.COM /*
329610106SJason.Beloro@Sun.COM * Invalidate memseg pointers in cpu private vm data caches.
329710106SJason.Beloro@Sun.COM */
329810106SJason.Beloro@Sun.COM static void
memseg_cpu_vm_flush()329910106SJason.Beloro@Sun.COM memseg_cpu_vm_flush()
330010106SJason.Beloro@Sun.COM {
330110106SJason.Beloro@Sun.COM cpu_t *cp;
330210106SJason.Beloro@Sun.COM vm_cpu_data_t *vc;
330310106SJason.Beloro@Sun.COM
330410106SJason.Beloro@Sun.COM mutex_enter(&cpu_lock);
330510106SJason.Beloro@Sun.COM pause_cpus(NULL);
330610106SJason.Beloro@Sun.COM
330710106SJason.Beloro@Sun.COM cp = cpu_list;
330810106SJason.Beloro@Sun.COM do {
330910106SJason.Beloro@Sun.COM vc = cp->cpu_vm_data;
331010106SJason.Beloro@Sun.COM vc->vc_pnum_memseg = NULL;
331110106SJason.Beloro@Sun.COM vc->vc_pnext_memseg = NULL;
331210106SJason.Beloro@Sun.COM
331310106SJason.Beloro@Sun.COM } while ((cp = cp->cpu_next) != cpu_list);
331410106SJason.Beloro@Sun.COM
331510106SJason.Beloro@Sun.COM start_cpus();
331610106SJason.Beloro@Sun.COM mutex_exit(&cpu_lock);
331710106SJason.Beloro@Sun.COM }
3318