xref: /titanic_53/usr/src/uts/i86pc/os/pmem.c (revision 7b93957c241820af312fd1aad1e23e305a8dabbf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7b93957cSeota  * Common Development and Distribution License (the "License").
6*7b93957cSeota  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*7b93957cSeota  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * PMEM - Direct mapping physical memory pages to userland process
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Provide functions used for directly (w/o occupying kernel virtual address
327c478bd9Sstevel@tonic-gate  * space) allocating and exporting physical memory pages to userland.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/ddidevmap.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <sys/project.h>
427c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
437c478bd9Sstevel@tonic-gate #include <sys/pmem.h>
447c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
457c478bd9Sstevel@tonic-gate #include <sys/task.h>
467c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * The routines in this file allocate memory which will be accessed through
507c478bd9Sstevel@tonic-gate  * the AGP GART hardware.  The GART is programmed with the PFNs for this
517c478bd9Sstevel@tonic-gate  * memory, and the only mechanism for removing these entries is by an
527c478bd9Sstevel@tonic-gate  * explicit process operation (ioctl/close of the driver, or process exit).
537c478bd9Sstevel@tonic-gate  * As such, the pages need to remain locked to ensure that they won't be
547c478bd9Sstevel@tonic-gate  * relocated or paged out.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * To prevent these locked pages from getting in the way of page
577c478bd9Sstevel@tonic-gate  * coalescing, we try to allocate large pages from the system, and carve
587c478bd9Sstevel@tonic-gate  * them up to satisfy pmem allocation requests.  This will keep the locked
597c478bd9Sstevel@tonic-gate  * pages within a constrained area of physical memory, limiting the number
607c478bd9Sstevel@tonic-gate  * of large pages that would be pinned by our locked pages.  This is, of
617c478bd9Sstevel@tonic-gate  * course, another take on the infamous kernel cage, and it has many of the
627c478bd9Sstevel@tonic-gate  * downsides of the original cage.  It also interferes with system-wide
637c478bd9Sstevel@tonic-gate  * resource management decisions, as it maintains its own pool of unused
647c478bd9Sstevel@tonic-gate  * pages which can't be easily reclaimed and used during low-memory
657c478bd9Sstevel@tonic-gate  * situations.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * The right solution is for pmem to register a callback that the VM system
687c478bd9Sstevel@tonic-gate  * could call, which would temporarily remove any GART entries for pages
697c478bd9Sstevel@tonic-gate  * that were being relocated.  This would let us leave the pages unlocked,
707c478bd9Sstevel@tonic-gate  * which would remove the need for using large pages, which would simplify
717c478bd9Sstevel@tonic-gate  * this code a great deal.  Unfortunately, the support for these callbacks
727c478bd9Sstevel@tonic-gate  * only exists on some SPARC platforms right now.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * Note that this is the *only* reason that large pages are used here.  The
757c478bd9Sstevel@tonic-gate  * GART can't perform large-page translations, and the code appropriately
767c478bd9Sstevel@tonic-gate  * falls back to using small pages if page_create_va_large() fails.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	HOLD_DHP_LOCK(dhp)  if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) \
807c478bd9Sstevel@tonic-gate 			{ mutex_enter(&dhp->dh_lock); }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	RELE_DHP_LOCK(dhp) if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) \
837c478bd9Sstevel@tonic-gate 			{ mutex_exit(&dhp->dh_lock); }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	FROM_LPG(pp) (pp->p_szc != 0)
867c478bd9Sstevel@tonic-gate #define	PFIND(pp) (page_pptonum(pp) & (pmem_pgcnt - 1))
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Structs and static variables used for pmem only.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate typedef struct pmem_lpg {
927c478bd9Sstevel@tonic-gate 	page_t	*pl_pp;		/* start pp */
937c478bd9Sstevel@tonic-gate 	ulong_t	*pl_bitmap;	/* allocation status for each page */
947c478bd9Sstevel@tonic-gate 	ushort_t pl_pfree;	/* this large page might be fully freed */
957c478bd9Sstevel@tonic-gate 	struct pmem_lpg *pl_next;
967c478bd9Sstevel@tonic-gate 	struct pmem_lpg *pl_prev;
977c478bd9Sstevel@tonic-gate } pmem_lpg_t;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static size_t	pmem_lpgsize;	/* the size of one large page */
1007c478bd9Sstevel@tonic-gate static pgcnt_t	pmem_pgcnt;	/* the number of small pages in a large page */
1017c478bd9Sstevel@tonic-gate static uint_t	pmem_lszc;	/* page size code of the large page */
1027c478bd9Sstevel@tonic-gate /* The segment to be associated with all the allocated pages. */
1037c478bd9Sstevel@tonic-gate static struct seg	pmem_seg;
1047c478bd9Sstevel@tonic-gate /* Fully occupied large pages allocated for pmem. */
1057c478bd9Sstevel@tonic-gate static pmem_lpg_t *pmem_occ_lpgs;
1067c478bd9Sstevel@tonic-gate /* Memory pool to store residual small pages from large pages. */
1077c478bd9Sstevel@tonic-gate static page_t	*pmem_mpool = NULL;
1087c478bd9Sstevel@tonic-gate /* Number of small pages reside in pmem_mpool currently. */
1097c478bd9Sstevel@tonic-gate static pgcnt_t	pmem_nmpages = 0;
1107c478bd9Sstevel@tonic-gate /* To protect pmem_nmpages, pmem_mpool and pmem_occ_lpgs. */
1117c478bd9Sstevel@tonic-gate kmutex_t	pmem_mutex;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static int lpg_isfree(pmem_lpg_t *);
1147c478bd9Sstevel@tonic-gate static void pmem_lpg_sub(pmem_lpg_t **, pmem_lpg_t *);
1157c478bd9Sstevel@tonic-gate static void pmem_lpg_concat(pmem_lpg_t **, pmem_lpg_t **);
1167c478bd9Sstevel@tonic-gate static pmem_lpg_t *pmem_lpg_get(pmem_lpg_t *, page_t *, pmem_lpg_t **);
1177c478bd9Sstevel@tonic-gate static pmem_lpg_t *pmem_lpg_alloc(uint_t);
1187c478bd9Sstevel@tonic-gate static void pmem_lpg_free(pmem_lpg_t **, pmem_lpg_t *);
1197c478bd9Sstevel@tonic-gate static void lpg_free(page_t *spp);
1207c478bd9Sstevel@tonic-gate static pgcnt_t mpool_break(page_t **, pgcnt_t);
1217c478bd9Sstevel@tonic-gate static void mpool_append(page_t **, pgcnt_t);
1227c478bd9Sstevel@tonic-gate static void lpp_break(page_t **, pgcnt_t, pgcnt_t, pmem_lpg_t *);
1237c478bd9Sstevel@tonic-gate static void lpp_free(page_t *, pgcnt_t, pmem_lpg_t **);
1247c478bd9Sstevel@tonic-gate static int lpp_create(page_t **, pgcnt_t, pgcnt_t *, pmem_lpg_t **,
1257c478bd9Sstevel@tonic-gate     vnode_t *, u_offset_t *, uint_t);
1267c478bd9Sstevel@tonic-gate static void tlist_in(page_t *, pgcnt_t, vnode_t *, u_offset_t *);
1277c478bd9Sstevel@tonic-gate static void tlist_out(page_t *, pgcnt_t);
1287c478bd9Sstevel@tonic-gate static int pmem_cookie_alloc(struct devmap_pmem_cookie **, pgcnt_t, uint_t);
1297c478bd9Sstevel@tonic-gate static int pmem_lock(pgcnt_t, kproject_t **);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Called by driver devmap routine to pass physical memory mapping info to
1337c478bd9Sstevel@tonic-gate  * seg_dev framework, used only for physical memory allocated from
1347c478bd9Sstevel@tonic-gate  * devmap_pmem_alloc().
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate /* ARGSUSED */
1377c478bd9Sstevel@tonic-gate int
1387c478bd9Sstevel@tonic-gate devmap_pmem_setup(devmap_cookie_t dhc, dev_info_t *dip,
1397c478bd9Sstevel@tonic-gate     struct devmap_callback_ctl *callbackops, devmap_pmem_cookie_t cookie,
1407c478bd9Sstevel@tonic-gate     offset_t off, size_t len, uint_t maxprot, uint_t flags,
1417c478bd9Sstevel@tonic-gate     ddi_device_acc_attr_t *accattrp)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
1447c478bd9Sstevel@tonic-gate 	struct devmap_pmem_cookie *pcp = (struct devmap_pmem_cookie *)cookie;
145*7b93957cSeota 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (pcp == NULL || (off + len) > ptob(pcp->dp_npages))
1487c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/*
1517c478bd9Sstevel@tonic-gate 	 * First to check if this function has been called for this dhp.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_SETUP_DONE)
1547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
1577c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1587c478bd9Sstevel@tonic-gate 
159*7b93957cSeota 	/*
160*7b93957cSeota 	 * Check if the cache attributes are supported. Need to pay
161*7b93957cSeota 	 * attention that only uncachable or write-combining is
162*7b93957cSeota 	 * permitted for pmem.
163*7b93957cSeota 	 */
164*7b93957cSeota 	if (i_ddi_check_cache_attr(flags) == B_FALSE ||
165*7b93957cSeota 	    (cache_attr & (IOMEM_DATA_UNCACHED|IOMEM_DATA_UC_WR_COMBINE)) == 0)
166*7b93957cSeota 		return (DDI_FAILURE);
167*7b93957cSeota 
1687c478bd9Sstevel@tonic-gate 	if (flags & DEVMAP_MAPPING_INVALID) {
1697c478bd9Sstevel@tonic-gate 		/*
1707c478bd9Sstevel@tonic-gate 		 * If DEVMAP_MAPPING_INVALID is specified, we have to grant
1717c478bd9Sstevel@tonic-gate 		 * remap permission.
1727c478bd9Sstevel@tonic-gate 		 */
1737c478bd9Sstevel@tonic-gate 		if (!(flags & DEVMAP_ALLOW_REMAP))
1747c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1757c478bd9Sstevel@tonic-gate 	} else {
1767c478bd9Sstevel@tonic-gate 		dhp->dh_pcookie = (devmap_pmem_cookie_t)pcp;
1777c478bd9Sstevel@tonic-gate 		/* dh_roff is the offset inside the dh_pcookie. */
1787c478bd9Sstevel@tonic-gate 		dhp->dh_roff = ptob(btop(off));
179*7b93957cSeota 		/* Set the cache attributes correctly */
180*7b93957cSeota 		i_ddi_cacheattr_to_hatacc(cache_attr, &dhp->dh_hat_attr);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	dhp->dh_cookie = DEVMAP_PMEM_COOKIE;
1847c478bd9Sstevel@tonic-gate 	dhp->dh_flags |= (flags & DEVMAP_SETUP_FLAGS);
1857c478bd9Sstevel@tonic-gate 	dhp->dh_len = ptob(btopr(len));
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
1887c478bd9Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (callbackops != NULL) {
1917c478bd9Sstevel@tonic-gate 		bcopy(callbackops, &dhp->dh_callbackops,
1927c478bd9Sstevel@tonic-gate 		    sizeof (struct devmap_callback_ctl));
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * Initialize dh_lock if we want to do remap.
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) {
1997c478bd9Sstevel@tonic-gate 		mutex_init(&dhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
2007c478bd9Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_LOCK_INITED;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	dhp->dh_flags |= DEVMAP_SETUP_DONE;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Replace existing mapping using a new cookie, mainly gets called when doing
2107c478bd9Sstevel@tonic-gate  * fork(). Should be called in associated devmap_dup(9E).
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate /* ARGSUSED */
2137c478bd9Sstevel@tonic-gate int
2147c478bd9Sstevel@tonic-gate devmap_pmem_remap(devmap_cookie_t dhc, dev_info_t *dip,
2157c478bd9Sstevel@tonic-gate     devmap_pmem_cookie_t cookie, offset_t off, size_t len, uint_t maxprot,
2167c478bd9Sstevel@tonic-gate     uint_t flags, ddi_device_acc_attr_t *accattrp)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
2197c478bd9Sstevel@tonic-gate 	struct devmap_pmem_cookie *pcp = (struct devmap_pmem_cookie *)cookie;
220*7b93957cSeota 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * Reture failure if setup has not been done or no remap permission
2247c478bd9Sstevel@tonic-gate 	 * has been granted during the setup.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	if ((dhp->dh_flags & DEVMAP_SETUP_DONE) == 0 ||
2277c478bd9Sstevel@tonic-gate 	    (dhp->dh_flags & DEVMAP_ALLOW_REMAP) == 0)
2287c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/* No flags supported for remap yet. */
2317c478bd9Sstevel@tonic-gate 	if (flags != 0)
2327c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
2357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (pcp == NULL || (off + len) > ptob(pcp->dp_npages))
2387c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2397c478bd9Sstevel@tonic-gate 
240*7b93957cSeota 	/*
241*7b93957cSeota 	 * Check if the cache attributes are supported. Need to pay
242*7b93957cSeota 	 * attention that only uncachable or write-combining is
243*7b93957cSeota 	 * permitted for pmem.
244*7b93957cSeota 	 */
245*7b93957cSeota 	if (i_ddi_check_cache_attr(flags) == B_FALSE ||
246*7b93957cSeota 	    (cache_attr & (IOMEM_DATA_UNCACHED|IOMEM_DATA_UC_WR_COMBINE)) == 0)
247*7b93957cSeota 		return (DDI_FAILURE);
248*7b93957cSeota 
2497c478bd9Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * Unload the old mapping of pages reloated with this dhp, so next
2527c478bd9Sstevel@tonic-gate 	 * fault will setup the new mappings. It is in segdev_faultpage that
2537c478bd9Sstevel@tonic-gate 	 * calls hat_devload to establish the mapping. Do this while holding
2547c478bd9Sstevel@tonic-gate 	 * the dhp lock so other faults dont reestablish the mappings.
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
2577c478bd9Sstevel@tonic-gate 	    dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
2587c478bd9Sstevel@tonic-gate 
259*7b93957cSeota 	/* Set the cache attributes correctly */
260*7b93957cSeota 	i_ddi_cacheattr_to_hatacc(cache_attr, &dhp->dh_hat_attr);
261*7b93957cSeota 
2627c478bd9Sstevel@tonic-gate 	dhp->dh_pcookie = cookie;
2637c478bd9Sstevel@tonic-gate 	dhp->dh_roff = ptob(btop(off));
2647c478bd9Sstevel@tonic-gate 	dhp->dh_len = ptob(btopr(len));
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/* Clear the large page size flag. */
2677c478bd9Sstevel@tonic-gate 	dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
2707c478bd9Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
2717c478bd9Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
2727c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Directly (i.e., without occupying kernel virtual address space) allocate
2777c478bd9Sstevel@tonic-gate  * 'npages' physical memory pages for exporting to user land. The allocated
2787c478bd9Sstevel@tonic-gate  * page_t pointer will be recorded in cookie.
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate int
2817c478bd9Sstevel@tonic-gate devmap_pmem_alloc(size_t size, uint_t flags, devmap_pmem_cookie_t *cookiep)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	u_offset_t	pmem_off = 0;
2847c478bd9Sstevel@tonic-gate 	page_t		*pp = NULL;
2857c478bd9Sstevel@tonic-gate 	page_t		*lpp = NULL;
2867c478bd9Sstevel@tonic-gate 	page_t		*tlist = NULL;
2877c478bd9Sstevel@tonic-gate 	pgcnt_t		i = 0;
2887c478bd9Sstevel@tonic-gate 	pgcnt_t		rpages = 0;
2897c478bd9Sstevel@tonic-gate 	pgcnt_t		lpages = 0;
2907c478bd9Sstevel@tonic-gate 	pgcnt_t		tpages = 0;
2917c478bd9Sstevel@tonic-gate 	pgcnt_t		npages = btopr(size);
2927c478bd9Sstevel@tonic-gate 	pmem_lpg_t	*plp = NULL;
2937c478bd9Sstevel@tonic-gate 	struct devmap_pmem_cookie	*pcp;
2947c478bd9Sstevel@tonic-gate 	uint_t		reserved = 0;
2957c478bd9Sstevel@tonic-gate 	uint_t		locked = 0;
2967c478bd9Sstevel@tonic-gate 	uint_t		pflags, kflags;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	*cookiep = NULL;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
3017c478bd9Sstevel@tonic-gate 	 * Number larger than this will cause page_create_va() to loop
3027c478bd9Sstevel@tonic-gate 	 * infinitely.
3037c478bd9Sstevel@tonic-gate 	 */
3047c478bd9Sstevel@tonic-gate 	if (npages == 0 || npages >= total_pages / 2)
3057c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3067c478bd9Sstevel@tonic-gate 	if ((flags & (PMEM_SLEEP | PMEM_NOSLEEP)) == 0)
3077c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3087c478bd9Sstevel@tonic-gate 	pflags = flags & PMEM_NOSLEEP ? PG_EXCL : PG_WAIT;
3097c478bd9Sstevel@tonic-gate 	kflags = flags & PMEM_NOSLEEP ? KM_NOSLEEP : KM_SLEEP;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/* Allocate pmem cookie. */
3127c478bd9Sstevel@tonic-gate 	if (pmem_cookie_alloc(&pcp, npages, kflags) == DDI_FAILURE)
3137c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3147c478bd9Sstevel@tonic-gate 	pcp->dp_npages = npages;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/*
3177c478bd9Sstevel@tonic-gate 	 * See if the requested memory can be locked. Currently we do resource
3187c478bd9Sstevel@tonic-gate 	 * controls on the project levlel only.
3197c478bd9Sstevel@tonic-gate 	 */
3207c478bd9Sstevel@tonic-gate 	if (pmem_lock(npages, &(pcp->dp_projp)) == DDI_FAILURE)
3217c478bd9Sstevel@tonic-gate 		goto alloc_fail;
3227c478bd9Sstevel@tonic-gate 	locked = 1;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 * First, grab as many as possible from pmem_mpool. If pages in
3267c478bd9Sstevel@tonic-gate 	 * pmem_mpool are enough for this request, we are done.
3277c478bd9Sstevel@tonic-gate 	 */
3287c478bd9Sstevel@tonic-gate 	mutex_enter(&pmem_mutex);
3297c478bd9Sstevel@tonic-gate 	tpages = mpool_break(&tlist, npages);
3307c478bd9Sstevel@tonic-gate 	/* IOlock and hashin them into the new offset. */
3317c478bd9Sstevel@tonic-gate 	if (tpages)
3327c478bd9Sstevel@tonic-gate 		tlist_in(tlist, tpages, pcp->dp_vnp, &pmem_off);
3337c478bd9Sstevel@tonic-gate 	mutex_exit(&pmem_mutex);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (tpages == npages)
3367c478bd9Sstevel@tonic-gate 		goto done;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	rpages = npages - tpages;
3397c478bd9Sstevel@tonic-gate 	/* Quit now if memory cannot be reserved. */
3407c478bd9Sstevel@tonic-gate 	if (!page_resv(rpages, kflags))
3417c478bd9Sstevel@tonic-gate 		goto alloc_fail;
3427c478bd9Sstevel@tonic-gate 	reserved = 1;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/* Try to allocate large pages first to decrease fragmentation. */
3457c478bd9Sstevel@tonic-gate 	i = (rpages + (pmem_pgcnt - 1)) / pmem_pgcnt;
3467c478bd9Sstevel@tonic-gate 	if (lpp_create(&lpp, i, &lpages, &plp, pcp->dp_vnp, &pmem_off,
3477c478bd9Sstevel@tonic-gate 	    kflags) == DDI_FAILURE)
3487c478bd9Sstevel@tonic-gate 		goto alloc_fail;
3497c478bd9Sstevel@tonic-gate 	ASSERT(lpages == 0 ? lpp == NULL : 1);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/*
3527c478bd9Sstevel@tonic-gate 	 * Pages in large pages is more than the request, put the residual
3537c478bd9Sstevel@tonic-gate 	 * pages into pmem_mpool.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	if (lpages >= rpages) {
3567c478bd9Sstevel@tonic-gate 		lpp_break(&lpp, lpages, lpages - rpages, plp);
3577c478bd9Sstevel@tonic-gate 		goto done;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/* Allocate small pages if lpp+tlist cannot satisfy the request. */
3617c478bd9Sstevel@tonic-gate 	i =  rpages - lpages;
3627c478bd9Sstevel@tonic-gate 	if ((pp = page_create_va(pcp->dp_vnp, pmem_off, ptob(i),
36302bbca18Sms148562 	    pflags, &pmem_seg, (caddr_t)(uintptr_t)pmem_off)) == NULL)
3647c478bd9Sstevel@tonic-gate 		goto alloc_fail;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate done:
3677c478bd9Sstevel@tonic-gate 	page_list_concat(&tlist, &lpp);
3687c478bd9Sstevel@tonic-gate 	page_list_concat(&tlist, &pp);
3697c478bd9Sstevel@tonic-gate 	/* Set those small pages from large pages as allocated. */
3707c478bd9Sstevel@tonic-gate 	mutex_enter(&pmem_mutex);
3717c478bd9Sstevel@tonic-gate 	pmem_lpg_concat(&pmem_occ_lpgs, &plp);
3727c478bd9Sstevel@tonic-gate 	mutex_exit(&pmem_mutex);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * Now tlist holds all the pages for this cookie. Record these pages in
3767c478bd9Sstevel@tonic-gate 	 * pmem cookie.
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 	for (pp = tlist, i = 0; i < npages; i++) {
3797c478bd9Sstevel@tonic-gate 		pcp->dp_pparray[i] = pp;
3807c478bd9Sstevel@tonic-gate 		page_io_unlock(pp);
3817c478bd9Sstevel@tonic-gate 		pp = pp->p_next;
3827c478bd9Sstevel@tonic-gate 		page_sub(&tlist, pp->p_prev);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 	ASSERT(tlist == NULL);
3857c478bd9Sstevel@tonic-gate 	*cookiep = (devmap_pmem_cookie_t)pcp;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate alloc_fail:
3907c478bd9Sstevel@tonic-gate 	DTRACE_PROBE(pmem__alloc__fail);
3917c478bd9Sstevel@tonic-gate 	/* Free large pages and the associated allocation records. */
3927c478bd9Sstevel@tonic-gate 	if (lpp)
3937c478bd9Sstevel@tonic-gate 		lpp_free(lpp, lpages / pmem_pgcnt, &plp);
3947c478bd9Sstevel@tonic-gate 	if (reserved == 1)
3957c478bd9Sstevel@tonic-gate 		page_unresv(rpages);
3967c478bd9Sstevel@tonic-gate 	/* Put those pages in tlist back into pmem_mpool. */
3977c478bd9Sstevel@tonic-gate 	if (tpages != 0) {
3987c478bd9Sstevel@tonic-gate 		mutex_enter(&pmem_mutex);
3997c478bd9Sstevel@tonic-gate 		/* IOunlock, hashout and update the allocation records. */
4007c478bd9Sstevel@tonic-gate 		tlist_out(tlist, tpages);
4017c478bd9Sstevel@tonic-gate 		mpool_append(&tlist, tpages);
4027c478bd9Sstevel@tonic-gate 		mutex_exit(&pmem_mutex);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	if (locked == 1)
4057c478bd9Sstevel@tonic-gate 		i_ddi_decr_locked_memory(NULL, NULL, pcp->dp_projp, NULL,
4067c478bd9Sstevel@tonic-gate 		    ptob(pcp->dp_npages));
4077c478bd9Sstevel@tonic-gate 	/* Freeing pmem_cookie. */
4087c478bd9Sstevel@tonic-gate 	kmem_free(pcp->dp_vnp, sizeof (vnode_t));
4097c478bd9Sstevel@tonic-gate 	kmem_free(pcp->dp_pparray, npages * sizeof (page_t *));
4107c478bd9Sstevel@tonic-gate 	kmem_free(pcp, sizeof (struct devmap_pmem_cookie));
4117c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * Free all small pages inside cookie, and return pages from large pages into
4167c478bd9Sstevel@tonic-gate  * mpool, if all the pages from one large page is in mpool, free it as a whole.
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate void
4197c478bd9Sstevel@tonic-gate devmap_pmem_free(devmap_pmem_cookie_t cookie)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	struct	devmap_pmem_cookie *pcp = (struct devmap_pmem_cookie *)cookie;
4227c478bd9Sstevel@tonic-gate 	pgcnt_t		i;
4237c478bd9Sstevel@tonic-gate 	pgcnt_t		tpages = 0;
4247c478bd9Sstevel@tonic-gate 	page_t		*pp;
4257c478bd9Sstevel@tonic-gate 	pmem_lpg_t 	*pl1, *plp;
4267c478bd9Sstevel@tonic-gate 	pmem_lpg_t	*pf_lpgs = NULL;
4277c478bd9Sstevel@tonic-gate 	uint_t		npls = 0;
4287c478bd9Sstevel@tonic-gate 	pmem_lpg_t *last_pl = NULL;
4297c478bd9Sstevel@tonic-gate 	pmem_lpg_t *plast_pl = NULL;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	ASSERT(pcp);
4327c478bd9Sstevel@tonic-gate 	mutex_enter(&pmem_mutex);
4337c478bd9Sstevel@tonic-gate 	/* Free small pages and return them to memory pool. */
4347c478bd9Sstevel@tonic-gate 	for (i = pcp->dp_npages; i > 0; i--) {
4357c478bd9Sstevel@tonic-gate 		pp = pcp->dp_pparray[i - 1];
4367c478bd9Sstevel@tonic-gate 		page_hashout(pp, NULL);
4377c478bd9Sstevel@tonic-gate 		/*
4387c478bd9Sstevel@tonic-gate 		 * Remove the mapping of this single page, this mapping is
4397c478bd9Sstevel@tonic-gate 		 * created using hat_devload() in segdev_faultpage().
4407c478bd9Sstevel@tonic-gate 		 */
4417c478bd9Sstevel@tonic-gate 		(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
4427c478bd9Sstevel@tonic-gate 		if (!FROM_LPG(pp)) {
4437c478bd9Sstevel@tonic-gate 			/* Normal small page. */
4447c478bd9Sstevel@tonic-gate 			page_free(pp, 1);
4457c478bd9Sstevel@tonic-gate 			page_unresv(1);
4467c478bd9Sstevel@tonic-gate 		} else {
4477c478bd9Sstevel@tonic-gate 			/* Small page from large pages. */
4487c478bd9Sstevel@tonic-gate 			plp = pmem_lpg_get(pmem_occ_lpgs, pp, &last_pl);
4497c478bd9Sstevel@tonic-gate 			if (plp && !(plp->pl_pfree)) {
4507c478bd9Sstevel@tonic-gate 				/*
4517c478bd9Sstevel@tonic-gate 				 * Move this record to pf_lpgs list, this large
4527c478bd9Sstevel@tonic-gate 				 * page may be able to be freed as a whole.
4537c478bd9Sstevel@tonic-gate 				 */
4547c478bd9Sstevel@tonic-gate 				pmem_lpg_sub(&pmem_occ_lpgs, plp);
4557c478bd9Sstevel@tonic-gate 				pmem_lpg_concat(&pf_lpgs, &plp);
4567c478bd9Sstevel@tonic-gate 				plp->pl_pfree = 1;
4577c478bd9Sstevel@tonic-gate 				npls++;
4587c478bd9Sstevel@tonic-gate 				last_pl = NULL;
4597c478bd9Sstevel@tonic-gate 			} else {
4607c478bd9Sstevel@tonic-gate 				/* Search in pf_lpgs list. */
4617c478bd9Sstevel@tonic-gate 				plp = pmem_lpg_get(pf_lpgs, pp, &plast_pl);
4627c478bd9Sstevel@tonic-gate 			}
4637c478bd9Sstevel@tonic-gate 			ASSERT(plp);
4647c478bd9Sstevel@tonic-gate 			/* Mark this page as free. */
4657c478bd9Sstevel@tonic-gate 			BT_SET(plp->pl_bitmap, PFIND(pp));
4667c478bd9Sstevel@tonic-gate 			/* Record this page in pmem_mpool. */
4677c478bd9Sstevel@tonic-gate 			mpool_append(&pp, 1);
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * Find out the large pages whose pages have been freed, remove them
4737c478bd9Sstevel@tonic-gate 	 * from plp list, free them and the associated pmem_lpg struct.
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	for (plp = pf_lpgs; npls != 0; npls--) {
4767c478bd9Sstevel@tonic-gate 		pl1 = plp;
4777c478bd9Sstevel@tonic-gate 		plp = plp->pl_next;
4787c478bd9Sstevel@tonic-gate 		if (lpg_isfree(pl1)) {
4797c478bd9Sstevel@tonic-gate 			/*
4807c478bd9Sstevel@tonic-gate 			 * Get one free large page.  Find all pages in this
4817c478bd9Sstevel@tonic-gate 			 * large page and remove them from pmem_mpool.
4827c478bd9Sstevel@tonic-gate 			 */
4837c478bd9Sstevel@tonic-gate 			lpg_free(pl1->pl_pp);
4847c478bd9Sstevel@tonic-gate 			/* Remove associated allocation records. */
4857c478bd9Sstevel@tonic-gate 			pmem_lpg_sub(&pf_lpgs, pl1);
4867c478bd9Sstevel@tonic-gate 			pmem_lpg_free(&pf_lpgs, pl1);
4877c478bd9Sstevel@tonic-gate 			tpages -= pmem_pgcnt;
4887c478bd9Sstevel@tonic-gate 		} else
4897c478bd9Sstevel@tonic-gate 			pl1->pl_pfree = 0;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 	/* Update allocation records accordingly. */
4927c478bd9Sstevel@tonic-gate 	pmem_lpg_concat(&pmem_occ_lpgs, &pf_lpgs);
4937c478bd9Sstevel@tonic-gate 	mutex_exit(&pmem_mutex);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	i_ddi_decr_locked_memory(NULL, NULL, (kproject_t *)pcp->dp_projp, NULL,
4967c478bd9Sstevel@tonic-gate 	    ptob(pcp->dp_npages));
4977c478bd9Sstevel@tonic-gate 	kmem_free(pcp->dp_vnp, sizeof (vnode_t));
4987c478bd9Sstevel@tonic-gate 	kmem_free(pcp->dp_pparray, pcp->dp_npages * sizeof (page_t *));
4997c478bd9Sstevel@tonic-gate 	kmem_free(pcp, sizeof (struct devmap_pmem_cookie));
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * To extract page frame number from specified range in a cookie.
5047c478bd9Sstevel@tonic-gate  */
5057c478bd9Sstevel@tonic-gate int
5067c478bd9Sstevel@tonic-gate devmap_pmem_getpfns(devmap_pmem_cookie_t cookie, uint_t start, pgcnt_t npages,
5077c478bd9Sstevel@tonic-gate     pfn_t *pfnarray)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	struct devmap_pmem_cookie *pcp = (struct devmap_pmem_cookie *)cookie;
5107c478bd9Sstevel@tonic-gate 	pgcnt_t i;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (pcp == NULL || start + npages > pcp->dp_npages)
5137c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	for (i = start; i < start + npages; i++)
5167c478bd9Sstevel@tonic-gate 		pfnarray[i - start] = pcp->dp_pparray[i]->p_pagenum;
5177c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate void
5217c478bd9Sstevel@tonic-gate pmem_init()
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	mutex_init(&pmem_mutex, NULL, MUTEX_DEFAULT, NULL);
5247c478bd9Sstevel@tonic-gate 	pmem_lszc = MIN(1, page_num_pagesizes() - 1);
5257c478bd9Sstevel@tonic-gate 	pmem_lpgsize = page_get_pagesize(pmem_lszc);
5267c478bd9Sstevel@tonic-gate 	pmem_pgcnt = pmem_lpgsize >> PAGESHIFT;
5277c478bd9Sstevel@tonic-gate 	bzero(&pmem_seg, sizeof (struct seg));
5287c478bd9Sstevel@tonic-gate 	pmem_seg.s_as = &kas;
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /* Allocate kernel memory for one pmem cookie with n pages. */
5327c478bd9Sstevel@tonic-gate static int
5337c478bd9Sstevel@tonic-gate pmem_cookie_alloc(struct devmap_pmem_cookie **pcpp, pgcnt_t n, uint_t kflags)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	struct devmap_pmem_cookie *pcp;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if ((*pcpp = kmem_zalloc(sizeof (struct devmap_pmem_cookie),
5387c478bd9Sstevel@tonic-gate 	    kflags)) == NULL)
5397c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5407c478bd9Sstevel@tonic-gate 	pcp = *pcpp;
5417c478bd9Sstevel@tonic-gate 	if ((pcp->dp_vnp =
5427c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sizeof (vnode_t), kflags)) == NULL) {
5437c478bd9Sstevel@tonic-gate 		kmem_free(pcp, sizeof (struct devmap_pmem_cookie));
5447c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	if ((pcp->dp_pparray =
5477c478bd9Sstevel@tonic-gate 	    kmem_zalloc(n * sizeof (page_t *), kflags)) == NULL) {
5487c478bd9Sstevel@tonic-gate 		kmem_free(pcp->dp_vnp, sizeof (vnode_t));
5497c478bd9Sstevel@tonic-gate 		kmem_free(pcp, sizeof (struct devmap_pmem_cookie));
5507c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /* Try to lock down n pages resource for current project. */
5567c478bd9Sstevel@tonic-gate static int
5577c478bd9Sstevel@tonic-gate pmem_lock(pgcnt_t n, kproject_t **prjpp)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
5607c478bd9Sstevel@tonic-gate 	if (i_ddi_incr_locked_memory(curproc, NULL, NULL, NULL,
5617c478bd9Sstevel@tonic-gate 	    ptob(n)) != 0) {
5627c478bd9Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
5637c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 	/* Store this project in cookie for later lock/unlock. */
5667c478bd9Sstevel@tonic-gate 	*prjpp = curproc->p_task->tk_proj;
5677c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
5687c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /* To check if all the pages in a large page are freed. */
5727c478bd9Sstevel@tonic-gate static int
5737c478bd9Sstevel@tonic-gate lpg_isfree(pmem_lpg_t *plp)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	uint_t i;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	for (i = 0; i < BT_BITOUL(pmem_pgcnt); i++)
5787c478bd9Sstevel@tonic-gate 		if (plp->pl_bitmap[i] != BT_ULMAXMASK)
5797c478bd9Sstevel@tonic-gate 			return (0);
5807c478bd9Sstevel@tonic-gate 	/* All 1 means all pages are freed. */
5817c478bd9Sstevel@tonic-gate 	return (1);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Using pp to get the associated large page allocation record, searching in
5867c478bd9Sstevel@tonic-gate  * the splp linked list with *last as the heuristic pointer. Return NULL if
5877c478bd9Sstevel@tonic-gate  * not found.
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate static pmem_lpg_t *
5907c478bd9Sstevel@tonic-gate pmem_lpg_get(pmem_lpg_t *splp, page_t *pp, pmem_lpg_t **last)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	pmem_lpg_t *plp;
5937c478bd9Sstevel@tonic-gate 	pgcnt_t root_pfn;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	ASSERT(pp);
5967c478bd9Sstevel@tonic-gate 	if (splp == NULL)
5977c478bd9Sstevel@tonic-gate 		return (NULL);
5987c478bd9Sstevel@tonic-gate 	root_pfn = page_pptonum(pp) & ~(pmem_pgcnt - 1);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/* Try last winner first. */
6017c478bd9Sstevel@tonic-gate 	if (*last && root_pfn == page_pptonum((*last)->pl_pp))
6027c478bd9Sstevel@tonic-gate 		goto pl_found;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/* Else search the whole pmem_lpg list. */
6057c478bd9Sstevel@tonic-gate 	for (plp = splp; root_pfn != page_pptonum(plp->pl_pp); ) {
6067c478bd9Sstevel@tonic-gate 		plp = plp->pl_next;
6077c478bd9Sstevel@tonic-gate 		if (plp == splp) {
6087c478bd9Sstevel@tonic-gate 			plp = NULL;
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		ASSERT(plp->pl_pp);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	*last = plp;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate pl_found:
6177c478bd9Sstevel@tonic-gate 	return (*last);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate /*
6217c478bd9Sstevel@tonic-gate  *  Remove one pmem_lpg plp from the oplpp list.
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate static void
6247c478bd9Sstevel@tonic-gate pmem_lpg_sub(pmem_lpg_t **oplpp, pmem_lpg_t *plp)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	if (*oplpp == plp)
6277c478bd9Sstevel@tonic-gate 		*oplpp = plp->pl_next;		/* go to next pmem_lpg */
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (*oplpp == plp)
6307c478bd9Sstevel@tonic-gate 		*oplpp = NULL;			/* pmem_lpg list is gone */
6317c478bd9Sstevel@tonic-gate 	else {
6327c478bd9Sstevel@tonic-gate 		plp->pl_prev->pl_next = plp->pl_next;
6337c478bd9Sstevel@tonic-gate 		plp->pl_next->pl_prev = plp->pl_prev;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	plp->pl_prev = plp->pl_next = plp;	/* make plp a list of one */
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * Concatenate page list nplpp onto the end of list plpp.
6407c478bd9Sstevel@tonic-gate  */
6417c478bd9Sstevel@tonic-gate static void
6427c478bd9Sstevel@tonic-gate pmem_lpg_concat(pmem_lpg_t **plpp, pmem_lpg_t **nplpp)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	pmem_lpg_t *s1p, *s2p, *e1p, *e2p;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if (*nplpp == NULL) {
6477c478bd9Sstevel@tonic-gate 		return;
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	if (*plpp == NULL) {
6507c478bd9Sstevel@tonic-gate 		*plpp = *nplpp;
6517c478bd9Sstevel@tonic-gate 		return;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 	s1p = *plpp;
6547c478bd9Sstevel@tonic-gate 	e1p =  s1p->pl_prev;
6557c478bd9Sstevel@tonic-gate 	s2p = *nplpp;
6567c478bd9Sstevel@tonic-gate 	e2p = s2p->pl_prev;
6577c478bd9Sstevel@tonic-gate 	s1p->pl_prev = e2p;
6587c478bd9Sstevel@tonic-gate 	e2p->pl_next = s1p;
6597c478bd9Sstevel@tonic-gate 	e1p->pl_next = s2p;
6607c478bd9Sstevel@tonic-gate 	s2p->pl_prev = e1p;
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  * Allocate and initialize the allocation record of one large page, the init
6657c478bd9Sstevel@tonic-gate  * value is 'allocated'.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate static pmem_lpg_t *
6687c478bd9Sstevel@tonic-gate pmem_lpg_alloc(uint_t kflags)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	pmem_lpg_t *plp;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	ASSERT(pmem_pgcnt % BT_NBIPUL == 0);
6737c478bd9Sstevel@tonic-gate 	plp = kmem_zalloc(sizeof (pmem_lpg_t), kflags);
6747c478bd9Sstevel@tonic-gate 	if (plp == NULL)
6757c478bd9Sstevel@tonic-gate 		return (NULL);
6767c478bd9Sstevel@tonic-gate 	plp->pl_bitmap = kmem_zalloc(BT_SIZEOFMAP(pmem_pgcnt), kflags);
6777c478bd9Sstevel@tonic-gate 	if (plp->pl_bitmap == NULL) {
6787c478bd9Sstevel@tonic-gate 		kmem_free(plp, sizeof (*plp));
6797c478bd9Sstevel@tonic-gate 		return (NULL);
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 	plp->pl_next = plp->pl_prev = plp;
6827c478bd9Sstevel@tonic-gate 	return (plp);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate /* Free one allocation record pointed by oplp. */
6867c478bd9Sstevel@tonic-gate static void
6877c478bd9Sstevel@tonic-gate pmem_lpg_free(pmem_lpg_t **headp, pmem_lpg_t *plp)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	if (*headp == plp)
6907c478bd9Sstevel@tonic-gate 		*headp = plp->pl_next;		/* go to next pmem_lpg_t */
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (*headp == plp)
6937c478bd9Sstevel@tonic-gate 		*headp = NULL;			/* this list is gone */
6947c478bd9Sstevel@tonic-gate 	else {
6957c478bd9Sstevel@tonic-gate 		plp->pl_prev->pl_next = plp->pl_next;
6967c478bd9Sstevel@tonic-gate 		plp->pl_next->pl_prev = plp->pl_prev;
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 	kmem_free(plp->pl_bitmap, BT_SIZEOFMAP(pmem_pgcnt));
6997c478bd9Sstevel@tonic-gate 	kmem_free(plp, sizeof (*plp));
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /* Free one large page headed by spp from pmem_mpool. */
7037c478bd9Sstevel@tonic-gate static void
7047c478bd9Sstevel@tonic-gate lpg_free(page_t *spp)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	page_t *pp1 = spp;
7077c478bd9Sstevel@tonic-gate 	uint_t i;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pmem_mutex));
7107c478bd9Sstevel@tonic-gate 	for (i = 0; i < pmem_pgcnt; i++) {
7117c478bd9Sstevel@tonic-gate 		/* Break pp1 from pmem_mpool. */
7127c478bd9Sstevel@tonic-gate 		page_sub(&pmem_mpool, pp1);
7137c478bd9Sstevel@tonic-gate 		pp1++;
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 	/* Free pages in this large page. */
7167c478bd9Sstevel@tonic-gate 	page_free_pages(spp);
7177c478bd9Sstevel@tonic-gate 	page_unresv(pmem_pgcnt);
7187c478bd9Sstevel@tonic-gate 	pmem_nmpages -= pmem_pgcnt;
7197c478bd9Sstevel@tonic-gate 	ASSERT((pmem_nmpages && pmem_mpool) || (!pmem_nmpages && !pmem_mpool));
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate /* Put n pages in *ppp list back into pmem_mpool. */
7237c478bd9Sstevel@tonic-gate static void
7247c478bd9Sstevel@tonic-gate mpool_append(page_t **ppp, pgcnt_t n)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pmem_mutex));
7277c478bd9Sstevel@tonic-gate 	/* Put back pages. */
7287c478bd9Sstevel@tonic-gate 	page_list_concat(&pmem_mpool, ppp);
7297c478bd9Sstevel@tonic-gate 	pmem_nmpages += n;
7307c478bd9Sstevel@tonic-gate 	ASSERT((pmem_nmpages && pmem_mpool) || (!pmem_nmpages && !pmem_mpool));
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate  * Try to grab MIN(pmem_nmpages, n) pages from pmem_mpool, put them into *ppp
7357c478bd9Sstevel@tonic-gate  * list, and return the number of grabbed pages.
7367c478bd9Sstevel@tonic-gate  */
7377c478bd9Sstevel@tonic-gate static pgcnt_t
7387c478bd9Sstevel@tonic-gate mpool_break(page_t **ppp, pgcnt_t n)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate 	pgcnt_t i;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pmem_mutex));
7437c478bd9Sstevel@tonic-gate 	/* Grab the pages. */
7447c478bd9Sstevel@tonic-gate 	i = MIN(pmem_nmpages, n);
7457c478bd9Sstevel@tonic-gate 	*ppp = pmem_mpool;
7467c478bd9Sstevel@tonic-gate 	page_list_break(ppp, &pmem_mpool, i);
7477c478bd9Sstevel@tonic-gate 	pmem_nmpages -= i;
7487c478bd9Sstevel@tonic-gate 	ASSERT((pmem_nmpages && pmem_mpool) || (!pmem_nmpages && !pmem_mpool));
7497c478bd9Sstevel@tonic-gate 	return (i);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * Create n large pages, lpages and plpp contains the number of small pages and
7547c478bd9Sstevel@tonic-gate  * allocation records list respectively.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate static int
7577c478bd9Sstevel@tonic-gate lpp_create(page_t **lppp, pgcnt_t n, pgcnt_t *lpages, pmem_lpg_t **plpp,
7587c478bd9Sstevel@tonic-gate     vnode_t *vnp, u_offset_t *offp, uint_t kflags)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	pgcnt_t i;
7617c478bd9Sstevel@tonic-gate 	pmem_lpg_t *plp;
7627c478bd9Sstevel@tonic-gate 	page_t *pp;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	for (i = 0, *lpages = 0; i < n; i++) {
7657c478bd9Sstevel@tonic-gate 		/* Allocte one large page each time. */
7667c478bd9Sstevel@tonic-gate 		pp = page_create_va_large(vnp, *offp, pmem_lpgsize,
76702bbca18Sms148562 		    PG_EXCL, &pmem_seg, (caddr_t)(uintptr_t)*offp, NULL);
7687c478bd9Sstevel@tonic-gate 		if (pp == NULL)
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 		*offp += pmem_lpgsize;
7717c478bd9Sstevel@tonic-gate 		page_list_concat(lppp, &pp);
7727c478bd9Sstevel@tonic-gate 		*lpages += pmem_pgcnt;
7737c478bd9Sstevel@tonic-gate 		/* Add one allocation record for this large page. */
7747c478bd9Sstevel@tonic-gate 		if ((plp = pmem_lpg_alloc(kflags)) == NULL)
7757c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7767c478bd9Sstevel@tonic-gate 		plp->pl_pp = pp;
7777c478bd9Sstevel@tonic-gate 		pmem_lpg_concat(plpp, &plp);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate  * Break the last r small pages from the large page list *lppp (with totally n
7847c478bd9Sstevel@tonic-gate  * small pages) and put them into pmem_mpool.
7857c478bd9Sstevel@tonic-gate  */
7867c478bd9Sstevel@tonic-gate static void
7877c478bd9Sstevel@tonic-gate lpp_break(page_t **lppp, pgcnt_t n, pgcnt_t r, pmem_lpg_t *oplp)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate 	page_t *pp, *pp1;
7907c478bd9Sstevel@tonic-gate 	pgcnt_t i;
7917c478bd9Sstevel@tonic-gate 	pmem_lpg_t *plp;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (r == 0)
7947c478bd9Sstevel@tonic-gate 		return;
7957c478bd9Sstevel@tonic-gate 	ASSERT(*lppp != NULL && r < pmem_pgcnt);
7967c478bd9Sstevel@tonic-gate 	page_list_break(lppp, &pp, n - r);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	/* The residual should reside in the last large page.  */
7997c478bd9Sstevel@tonic-gate 	plp = oplp->pl_prev;
8007c478bd9Sstevel@tonic-gate 	/* IOunlock and hashout the residual pages. */
8017c478bd9Sstevel@tonic-gate 	for (pp1 = pp, i = 0; i < r; i++) {
8027c478bd9Sstevel@tonic-gate 		page_io_unlock(pp1);
8037c478bd9Sstevel@tonic-gate 		page_hashout(pp1, NULL);
8047c478bd9Sstevel@tonic-gate 		/* Mark this page as free. */
8057c478bd9Sstevel@tonic-gate 		BT_SET(plp->pl_bitmap, PFIND(pp1));
8067c478bd9Sstevel@tonic-gate 		pp1 = pp1->p_next;
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 	ASSERT(pp1 == pp);
8097c478bd9Sstevel@tonic-gate 	/* Put these residual pages into memory pool. */
8107c478bd9Sstevel@tonic-gate 	mutex_enter(&pmem_mutex);
8117c478bd9Sstevel@tonic-gate 	mpool_append(&pp, r);
8127c478bd9Sstevel@tonic-gate 	mutex_exit(&pmem_mutex);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /* Freeing large pages in lpp and the associated allocation records in plp. */
8167c478bd9Sstevel@tonic-gate static void
8177c478bd9Sstevel@tonic-gate lpp_free(page_t *lpp, pgcnt_t lpgs, pmem_lpg_t **plpp)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	pgcnt_t i, j;
8207c478bd9Sstevel@tonic-gate 	page_t *pp = lpp, *pp1;
8217c478bd9Sstevel@tonic-gate 	pmem_lpg_t *plp1, *plp2;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	for (i = 0; i < lpgs; i++) {
8247c478bd9Sstevel@tonic-gate 		for (j = 0; j < pmem_pgcnt; j++) {
8257c478bd9Sstevel@tonic-gate 			/* IO unlock and hashout this small page. */
8267c478bd9Sstevel@tonic-gate 			page_io_unlock(pp);
8277c478bd9Sstevel@tonic-gate 			page_hashout(pp, NULL);
8287c478bd9Sstevel@tonic-gate 			pp1 = pp->p_next;
8297c478bd9Sstevel@tonic-gate 			pp->p_prev = pp->p_next = pp;
8307c478bd9Sstevel@tonic-gate 			pp = pp1;
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 		/* Free one large page at one time. */
8337c478bd9Sstevel@tonic-gate 		page_free_pages(lpp);
8347c478bd9Sstevel@tonic-gate 		lpp = pp;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	/* Free associate pmem large page allocation records. */
8377c478bd9Sstevel@tonic-gate 	for (plp1 = *plpp; *plpp; plp1 = plp2) {
8387c478bd9Sstevel@tonic-gate 		plp2 = plp1->pl_next;
8397c478bd9Sstevel@tonic-gate 		pmem_lpg_free(plpp, plp1);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * IOlock and hashin all pages in tlist, associate them with vnode *pvnp
8457c478bd9Sstevel@tonic-gate  * and offset starting with *poffp. Update allocation records accordingly at
8467c478bd9Sstevel@tonic-gate  * the same time.
8477c478bd9Sstevel@tonic-gate  */
8487c478bd9Sstevel@tonic-gate static void
8497c478bd9Sstevel@tonic-gate tlist_in(page_t *tlist, pgcnt_t tpages, vnode_t *pvnp, u_offset_t *poffp)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	page_t *pp;
8527c478bd9Sstevel@tonic-gate 	pgcnt_t i = 0;
8537c478bd9Sstevel@tonic-gate 	pmem_lpg_t *plp, *last_pl = NULL;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pmem_mutex));
8567c478bd9Sstevel@tonic-gate 	for (pp = tlist; i < tpages; i++) {
8577c478bd9Sstevel@tonic-gate 		ASSERT(FROM_LPG(pp));
8587c478bd9Sstevel@tonic-gate 		page_io_lock(pp);
8597c478bd9Sstevel@tonic-gate 		(void) page_hashin(pp, pvnp, *poffp, NULL);
8607c478bd9Sstevel@tonic-gate 		plp = pmem_lpg_get(pmem_occ_lpgs, pp, &last_pl);
8617c478bd9Sstevel@tonic-gate 		/* Mark this page as allocated. */
8627c478bd9Sstevel@tonic-gate 		BT_CLEAR(plp->pl_bitmap, PFIND(pp));
8637c478bd9Sstevel@tonic-gate 		*poffp += PAGESIZE;
8647c478bd9Sstevel@tonic-gate 		pp = pp->p_next;
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	ASSERT(pp == tlist);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * IOunlock and hashout all pages in tlist, update allocation records
8717c478bd9Sstevel@tonic-gate  * accordingly at the same time.
8727c478bd9Sstevel@tonic-gate  */
8737c478bd9Sstevel@tonic-gate static void
8747c478bd9Sstevel@tonic-gate tlist_out(page_t *tlist, pgcnt_t tpages)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	page_t *pp;
8777c478bd9Sstevel@tonic-gate 	pgcnt_t i = 0;
8787c478bd9Sstevel@tonic-gate 	pmem_lpg_t *plp, *last_pl = NULL;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pmem_mutex));
8817c478bd9Sstevel@tonic-gate 	for (pp = tlist; i < tpages; i++) {
8827c478bd9Sstevel@tonic-gate 		ASSERT(FROM_LPG(pp));
8837c478bd9Sstevel@tonic-gate 		page_io_unlock(pp);
8847c478bd9Sstevel@tonic-gate 		page_hashout(pp, NULL);
8857c478bd9Sstevel@tonic-gate 		plp = pmem_lpg_get(pmem_occ_lpgs, pp, &last_pl);
8867c478bd9Sstevel@tonic-gate 		/* Mark this page as free. */
8877c478bd9Sstevel@tonic-gate 		BT_SET(plp->pl_bitmap, PFIND(pp));
8887c478bd9Sstevel@tonic-gate 		pp = pp->p_next;
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	ASSERT(pp == tlist);
8917c478bd9Sstevel@tonic-gate }
892