4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2015, Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
24 * Copyright (c) 2015, 2016 by Delphix. All rights reserved.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
41 * VM - physical page management.
44 #include <sys/types.h>
45 #include <sys/t_lock.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
50 #include <sys/vnode.h>
52 #include <sys/vtrace.h>
54 #include <sys/cmn_err.h>
55 #include <sys/tuneable.h>
56 #include <sys/sysmacros.h>
57 #include <sys/cpuvar.h>
58 #include <sys/callb.h>
59 #include <sys/debug.h>
60 #include <sys/tnf_probe.h>
61 #include <sys/condvar_impl.h>
62 #include <sys/mem_config.h>
63 #include <sys/mem_cage.h>
65 #include <sys/atomic.h>
66 #include <sys/strlog.h>
68 #include <sys/ontrap.h>
77 #include <vm/seg_kmem.h>
78 #include <vm/vm_dep.h>
79 #include <sys/vm_usage.h>
80 #include <sys/fs_subr.h>
82 #include <sys/modctl.h>
84 static pgcnt_t max_page_get
; /* max page_get request size in pages */
85 pgcnt_t total_pages
= 0; /* total number of pages (used by /proc) */
88 * freemem_lock protects all freemem variables:
89 * availrmem. Also this lock protects the globals which track the
90 * availrmem changes for accurate kernel footprint calculation.
91 * See below for an explanation of these
94 kmutex_t freemem_lock
;
96 pgcnt_t availrmem_initial
;
99 * These globals track availrmem changes to get a more accurate
100 * estimate of tke kernel size. Historically pp_kernel is used for
101 * kernel size and is based on availrmem. But availrmem is adjusted for
102 * locked pages in the system not just for kernel locked pages.
103 * These new counters will track the pages locked through segvn and
104 * by explicit user locking.
106 * pages_locked : How many pages are locked because of user specified
107 * locking through mlock or plock.
109 * pages_useclaim,pages_claimed : These two variables track the
110 * claim adjustments because of the protection changes on a segvn segment.
112 * All these globals are protected by the same lock which protects availrmem.
114 pgcnt_t pages_locked
= 0;
115 pgcnt_t pages_useclaim
= 0;
116 pgcnt_t pages_claimed
= 0;
120 * new_freemem_lock protects freemem, freemem_wait & freemem_cv.
122 static kmutex_t new_freemem_lock
;
123 static uint_t freemem_wait
; /* someone waiting for freemem */
124 static kcondvar_t freemem_cv
;
127 * The logical page free list is maintained as two lists, the 'free'
128 * and the 'cache' lists.
129 * The free list contains those pages that should be reused first.
131 * The implementation of the lists is machine dependent.
132 * page_get_freelist(), page_get_cachelist(),
133 * page_list_sub(), and page_list_add()
134 * form the interface to the machine dependent implementation.
136 * Pages with p_free set are on the cache list.
137 * Pages with p_free and p_age set are on the free list,
139 * A page may be locked while on either list.
143 * free list accounting stuff.
146 * Spread out the value for the number of pages on the
147 * page free and page cache lists. If there is just one
148 * value, then it must be under just one lock.
149 * The lock contention and cache traffic are a real bother.
151 * When we acquire and then drop a single pcf lock
152 * we can start in the middle of the array of pcf structures.
153 * If we acquire more than one pcf lock at a time, we need to
154 * start at the front to avoid deadlocking.
156 * pcf_count holds the number of pages in each pool.
158 * pcf_block is set when page_create_get_something() has asked the
159 * PSM page freelist and page cachelist routines without specifying
160 * a color and nothing came back. This is used to block anything
161 * else from moving pages from one list to the other while the
162 * lists are searched again. If a page is freeed while pcf_block is
163 * set, then pcf_reserve is incremented. pcgs_unblock() takes care
164 * of clearning pcf_block, doing the wakeups, etc.
167 #define MAX_PCF_FANOUT NCPU
168 static uint_t pcf_fanout
= 1; /* Will get changed at boot time */
169 static uint_t pcf_fanout_mask
= 0;
172 kmutex_t pcf_lock
; /* protects the structure */
173 uint_t pcf_count
; /* page count */
174 uint_t pcf_wait
; /* number of waiters */
175 uint_t pcf_block
; /* pcgs flag to page_free() */
176 uint_t pcf_reserve
; /* pages freed after pcf_block set */
177 uint_t pcf_fill
[10]; /* to line up on the caches */
181 * PCF_INDEX hash needs to be dynamic (every so often the hash changes where
182 * it will hash the cpu to). This is done to prevent a drain condition
183 * from happening. This drain condition will occur when pcf_count decrement
184 * occurs on cpu A and the increment of pcf_count always occurs on cpu B. An
185 * example of this shows up with device interrupts. The dma buffer is allocated
186 * by the cpu requesting the IO thus the pcf_count is decremented based on that.
187 * When the memory is returned by the interrupt thread, the pcf_count will be
188 * incremented based on the cpu servicing the interrupt.
190 static struct pcf pcf
[MAX_PCF_FANOUT
];
191 #define PCF_INDEX() ((int)(((long)CPU->cpu_seqid) + \
192 (randtick() >> 24)) & (pcf_fanout_mask))
194 static int pcf_decrement_bucket(pgcnt_t
);
195 static int pcf_decrement_multiple(pgcnt_t
*, pgcnt_t
, int);
197 kmutex_t pcgs_lock
; /* serializes page_create_get_ */
198 kmutex_t pcgs_cagelock
; /* serializes NOSLEEP cage allocs */
199 kmutex_t pcgs_wait_lock
; /* used for delay in pcgs */
200 static kcondvar_t pcgs_cv
; /* cv for delay in pcgs */
205 * No locks, but so what, they are only statistics.
208 static struct page_tcnt
{
209 int pc_free_cache
; /* free's into cache list */
210 int pc_free_dontneed
; /* free's with dontneed */
211 int pc_free_pageout
; /* free's from pageout */
212 int pc_free_free
; /* free's into free list */
213 int pc_free_pages
; /* free's into large page free list */
214 int pc_destroy_pages
; /* large page destroy's */
215 int pc_get_cache
; /* get's from cache list */
216 int pc_get_free
; /* get's from free list */
217 int pc_reclaim
; /* reclaim's */
218 int pc_abortfree
; /* abort's of free pages */
219 int pc_find_hit
; /* find's that find page */
220 int pc_find_miss
; /* find's that don't find page */
221 int pc_destroy_free
; /* # of free pages destroyed */
222 int pc_addclaim_pages
;
223 int pc_subclaim_pages
;
224 int pc_free_replacement_page
[2];
225 int pc_try_demote_pages
[6];
226 int pc_demote_pages
[2];
230 uint_t hashin_not_held
;
231 uint_t hashin_already
;
233 uint_t hashout_count
;
234 uint_t hashout_not_held
;
236 uint_t page_create_count
;
237 uint_t page_create_not_enough
;
238 uint_t page_create_not_enough_again
;
239 uint_t page_create_zero
;
240 uint_t page_create_hashout
;
241 uint_t page_create_page_lock_failed
;
242 uint_t page_create_trylock_failed
;
243 uint_t page_create_found_one
;
244 uint_t page_create_hashin_failed
;
245 uint_t page_create_dropped_phm
;
247 uint_t page_create_new
;
248 uint_t page_create_exists
;
249 uint_t page_create_putbacks
;
250 uint_t page_create_overshoot
;
252 uint_t page_reclaim_zero
;
253 uint_t page_reclaim_zero_locked
;
255 uint_t page_rename_exists
;
256 uint_t page_rename_count
;
258 uint_t page_lookup_cnt
[20];
259 uint_t page_lookup_nowait_cnt
[10];
260 uint_t page_find_cnt
;
261 uint_t page_exists_cnt
;
262 uint_t page_exists_forreal_cnt
;
263 uint_t page_lookup_dev_cnt
;
264 uint_t get_cachelist_cnt
;
265 uint_t page_create_cnt
[10];
266 uint_t alloc_pages
[9];
267 uint_t page_exphcontg
[19];
268 uint_t page_create_large_cnt
[10];
272 static inline page_t
*
273 find_page(vnode_t
*vnode
, uoff_t off
)
281 page
= avl_find(&vnode
->v_pagecache
, &key
, NULL
);
285 pagecnt
.pc_find_hit
++;
287 pagecnt
.pc_find_miss
++;
295 #define MEMSEG_SEARCH_STATS
298 #ifdef MEMSEG_SEARCH_STATS
299 struct memseg_stats
{
306 #define MEMSEG_STAT_INCR(v) \
307 atomic_inc_32(&memseg_stats.v)
309 #define MEMSEG_STAT_INCR(x)
312 struct memseg
*memsegs
; /* list of memory segments */
315 * /etc/system tunable to control large page allocation hueristic.
317 * Setting to LPAP_LOCAL will heavily prefer the local lgroup over remote lgroup
318 * for large page allocation requests. If a large page is not readily
319 * avaliable on the local freelists we will go through additional effort
320 * to create a large page, potentially moving smaller pages around to coalesce
321 * larger pages in the local lgroup.
322 * Default value of LPAP_DEFAULT will go to remote freelists if large pages
323 * are not readily available in the local lgroup.
326 LPAP_DEFAULT
, /* default large page allocation policy */
327 LPAP_LOCAL
/* local large page allocation policy */
330 enum lpap lpg_alloc_prefer
= LPAP_DEFAULT
;
332 static void page_init_mem_config(void);
333 static int page_do_hashin(page_t
*, vnode_t
*, uoff_t
);
334 static void page_do_hashout(page_t
*);
335 static void page_capture_init();
336 int page_capture_take_action(page_t
*, uint_t
, void *);
338 static void page_demote_vp_pages(page_t
*);
344 if (boot_ncpus
!= -1) {
345 pcf_fanout
= boot_ncpus
;
347 pcf_fanout
= max_ncpus
;
351 * Force at least 4 buckets if possible for sun4v.
353 pcf_fanout
= MAX(pcf_fanout
, 4);
357 * Round up to the nearest power of 2.
359 pcf_fanout
= MIN(pcf_fanout
, MAX_PCF_FANOUT
);
360 if (!ISP2(pcf_fanout
)) {
361 pcf_fanout
= 1 << highbit(pcf_fanout
);
363 if (pcf_fanout
> MAX_PCF_FANOUT
) {
364 pcf_fanout
= 1 << (highbit(MAX_PCF_FANOUT
) - 1);
367 pcf_fanout_mask
= pcf_fanout
- 1;
371 * vm subsystem related initialization
376 boolean_t
callb_vm_cpr(void *, int);
378 (void) callb_add(callb_vm_cpr
, 0, CB_CL_CPR_VM
, "vm");
379 page_init_mem_config();
386 * This function is called at startup and when memory is added or deleted.
389 init_pages_pp_maximum()
391 static pgcnt_t p_min
;
392 static pgcnt_t pages_pp_maximum_startup
;
393 static pgcnt_t avrmem_delta
;
394 static int init_done
;
395 static int user_set
; /* true if set in /etc/system */
397 if (init_done
== 0) {
399 /* If the user specified a value, save it */
400 if (pages_pp_maximum
!= 0) {
402 pages_pp_maximum_startup
= pages_pp_maximum
;
406 * Setting of pages_pp_maximum is based first time
407 * on the value of availrmem just after the start-up
408 * allocations. To preserve this relationship at run
409 * time, use a delta from availrmem_initial.
411 ASSERT(availrmem_initial
>= availrmem
);
412 avrmem_delta
= availrmem_initial
- availrmem
;
414 /* The allowable floor of pages_pp_maximum */
415 p_min
= tune
.t_minarmem
+ 100;
417 /* Make sure we don't come through here again. */
421 * Determine pages_pp_maximum, the number of currently available
422 * pages (availrmem) that can't be `locked'. If not set by
423 * the user, we set it to 4% of the currently available memory
425 * But we also insist that it be greater than tune.t_minarmem;
426 * otherwise a process could lock down a lot of memory, get swapped
427 * out, and never have enough to get swapped back in.
430 pages_pp_maximum
= pages_pp_maximum_startup
;
432 pages_pp_maximum
= ((availrmem_initial
- avrmem_delta
) / 25)
433 + btop(4 * 1024 * 1024);
435 if (pages_pp_maximum
<= p_min
) {
436 pages_pp_maximum
= p_min
;
441 set_max_page_get(pgcnt_t target_total_pages
)
443 max_page_get
= target_total_pages
/ 2;
446 static pgcnt_t pending_delete
;
450 page_mem_config_post_add(
454 set_max_page_get(total_pages
- pending_delete
);
455 init_pages_pp_maximum();
460 page_mem_config_pre_del(
466 nv
= atomic_add_long_nv(&pending_delete
, (spgcnt_t
)delta_pages
);
467 set_max_page_get(total_pages
- nv
);
473 page_mem_config_post_del(
480 nv
= atomic_add_long_nv(&pending_delete
, -(spgcnt_t
)delta_pages
);
481 set_max_page_get(total_pages
- nv
);
483 init_pages_pp_maximum();
486 static kphysm_setup_vector_t page_mem_config_vec
= {
487 KPHYSM_SETUP_VECTOR_VERSION
,
488 page_mem_config_post_add
,
489 page_mem_config_pre_del
,
490 page_mem_config_post_del
,
494 page_init_mem_config(void)
498 ret
= kphysm_setup_func_register(&page_mem_config_vec
, NULL
);
503 * Evenly spread out the PCF counters for large free pages
506 page_free_large_ctr(pgcnt_t npages
)
508 static struct pcf
*p
= pcf
;
513 lump
= roundup(npages
, pcf_fanout
) / pcf_fanout
;
517 ASSERT(!p
->pcf_block
);
520 p
->pcf_count
+= (uint_t
)lump
;
523 p
->pcf_count
+= (uint_t
)npages
;
527 ASSERT(!p
->pcf_wait
);
529 if (++p
> &pcf
[pcf_fanout
- 1])
537 * Add a physical chunk of memory to the system free lists during startup.
538 * Platform specific startup() allocates the memory for the page structs.
540 * num - number of page structures
541 * base - page number (pfn) to be associated with the first page.
543 * Since we are doing this during startup (ie. single threaded), we will
544 * use shortcut routines to avoid any locking overhead while putting all
545 * these pages on the freelists.
547 * NOTE: Any changes performed to page_free(), must also be performed to
548 * add_physmem() since this is how we initialize all page_t's at
558 uint_t szc
= page_num_pagesizes() - 1;
559 pgcnt_t large
= page_get_pagecnt(szc
);
563 * Arbitrarily limit the max page_get request
564 * to 1/2 of the page structs we have.
567 set_max_page_get(total_pages
);
569 PLCNT_MODIFY_MAX(pnum
, (long)num
);
572 * The physical space for the pages array
573 * representing ram pages has already been
574 * allocated. Here we initialize each lock
575 * in the page structure, and put each on
578 for (; num
; pp
++, pnum
++, num
--) {
581 * this needs to fill in the page number
582 * and do any other arch specific initialization
584 add_physmem_cb(pp
, pnum
);
591 * Initialize the page lock as unlocked, since nobody
592 * can see or access this page yet.
599 page_iolock_init(pp
);
602 * initialize other fields in the page_t
605 page_clr_all_props(pp
);
607 pp
->p_offset
= (uoff_t
)-1;
612 * Simple case: System doesn't support large pages.
616 page_free_at_startup(pp
);
621 * Handle unaligned pages, we collect them up onto
622 * the root page until we have a full large page.
624 if (!IS_P2ALIGNED(pnum
, large
)) {
627 * If not in a large page,
628 * just free as small page.
632 page_free_at_startup(pp
);
637 * Link a constituent page into the large page.
640 page_list_concat(&root
, &pp
);
643 * When large page is fully formed, free it.
645 if (++cnt
== large
) {
646 page_free_large_ctr(cnt
);
647 page_list_add_pages(root
, PG_LIST_ISINIT
);
655 * At this point we have a page number which
656 * is aligned. We assert that we aren't already
657 * in a different large page.
659 ASSERT(IS_P2ALIGNED(pnum
, large
));
660 ASSERT(root
== NULL
&& cnt
== 0);
663 * If insufficient number of pages left to form
664 * a large page, just free the small page.
668 page_free_at_startup(pp
);
673 * Otherwise start a new large page.
679 ASSERT(root
== NULL
&& cnt
== 0);
683 * Find a page representing the specified [vp, offset].
684 * If we find the page but it is intransit coming in,
685 * it will have an "exclusive" lock and we wait for
686 * the i/o to complete. A page found on the free list
687 * is always reclaimed and then locked. On success, the page
688 * is locked, its data is valid and it isn't on the free
689 * list, while a NULL is returned if the page doesn't exist.
692 page_lookup(vnode_t
*vp
, uoff_t off
, se_t se
)
694 return (page_lookup_create(vp
, off
, se
, NULL
, NULL
, 0));
698 * Find a page representing the specified [vp, offset].
699 * We either return the one we found or, if passed in,
700 * create one with identity of [vp, offset] of the
701 * pre-allocated page. If we find existing page but it is
702 * intransit coming in, it will have an "exclusive" lock
703 * and we wait for the i/o to complete. A page found on
704 * the free list is always reclaimed and then locked.
705 * On success, the page is locked, its data is valid and
706 * it isn't on the free list, while a NULL is returned
707 * if the page doesn't exist and newpp is NULL;
723 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp
)));
724 VM_STAT_ADD(page_lookup_cnt
[0]);
725 ASSERT(newpp
? PAGE_EXCL(newpp
) : 1);
727 mutex_enter(page_vnode_mutex(vp
));
729 pp
= find_page(vp
, off
);
732 VM_STAT_ADD(page_lookup_cnt
[1]);
733 es
= (newpp
!= NULL
) ? 1 : 0;
736 VM_STAT_ADD(page_lookup_cnt
[4]);
737 if (!page_lock_es(pp
, se
, vp
, P_RECLAIM
, es
)) {
738 VM_STAT_ADD(page_lookup_cnt
[5]);
742 VM_STAT_ADD(page_lookup_cnt
[6]);
744 mutex_exit(page_vnode_mutex(vp
));
746 if (newpp
!= NULL
&& pp
->p_szc
< newpp
->p_szc
&&
747 PAGE_EXCL(pp
) && nrelocp
!= NULL
) {
748 ASSERT(nrelocp
!= NULL
);
749 (void) page_relocate(&pp
, &newpp
, 1, 1, nrelocp
,
752 VM_STAT_COND_ADD(*nrelocp
== 1,
753 page_lookup_cnt
[11]);
754 VM_STAT_COND_ADD(*nrelocp
> 1,
755 page_lookup_cnt
[12]);
759 if (se
== SE_SHARED
) {
762 VM_STAT_ADD(page_lookup_cnt
[13]);
764 } else if (newpp
!= NULL
&& nrelocp
!= NULL
) {
765 if (PAGE_EXCL(pp
) && se
== SE_SHARED
) {
768 VM_STAT_COND_ADD(pp
->p_szc
< newpp
->p_szc
,
769 page_lookup_cnt
[14]);
770 VM_STAT_COND_ADD(pp
->p_szc
== newpp
->p_szc
,
771 page_lookup_cnt
[15]);
772 VM_STAT_COND_ADD(pp
->p_szc
> newpp
->p_szc
,
773 page_lookup_cnt
[16]);
774 } else if (newpp
!= NULL
&& PAGE_EXCL(pp
)) {
777 } else if (newpp
!= NULL
) {
779 * If we have a preallocated page then
780 * insert it now and basically behave like
783 VM_STAT_ADD(page_lookup_cnt
[18]);
785 * Since we hold the page hash mutex and
786 * just searched for this page, page_hashin
787 * had better not fail. If it does, that
788 * means some thread did not follow the
789 * page hash mutex rules. Panic now and
790 * get it over with. As usual, go down
791 * holding all the locks.
793 if (!page_hashin(newpp
, vp
, off
, true)) {
794 ASSERT(MUTEX_HELD(page_vnode_mutex(vp
)));
795 panic("page_lookup_create: hashin failed %p %p %llx",
796 (void *)newpp
, (void *)vp
, off
);
799 ASSERT(MUTEX_HELD(page_vnode_mutex(vp
)));
800 mutex_exit(page_vnode_mutex(vp
));
801 page_set_props(newpp
, P_REF
);
806 VM_STAT_ADD(page_lookup_cnt
[19]);
807 mutex_exit(page_vnode_mutex(vp
));
810 ASSERT(pp
? PAGE_LOCKED_SE(pp
, se
) : 1);
812 ASSERT(pp
? ((PP_ISFREE(pp
) == 0) && (PP_ISAGED(pp
) == 0)) : 1);
818 * Search the hash list for the page representing the
819 * specified [vp, offset] and return it locked. Skip
820 * free pages and pages that cannot be locked as requested.
821 * Used while attempting to kluster pages.
824 page_lookup_nowait(vnode_t
*vp
, uoff_t off
, se_t se
)
828 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp
)));
829 VM_STAT_ADD(page_lookup_nowait_cnt
[0]);
831 mutex_enter(page_vnode_mutex(vp
));
832 pp
= find_page(vp
, off
);
834 if (pp
== NULL
|| PP_ISFREE(pp
)) {
835 VM_STAT_ADD(page_lookup_nowait_cnt
[2]);
838 if (!page_trylock(pp
, se
)) {
839 VM_STAT_ADD(page_lookup_nowait_cnt
[3]);
842 VM_STAT_ADD(page_lookup_nowait_cnt
[4]);
844 VM_STAT_ADD(page_lookup_nowait_cnt
[6]);
851 mutex_exit(page_vnode_mutex(vp
));
853 ASSERT(pp
? PAGE_LOCKED_SE(pp
, se
) : 1);
859 * Search the hash list for a page with the specified [vp, off]
860 * that is known to exist and is already locked. This routine
861 * is typically used by segment SOFTUNLOCK routines.
864 page_find(vnode_t
*vp
, uoff_t off
)
868 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp
)));
869 VM_STAT_ADD(page_find_cnt
);
871 mutex_enter(page_vnode_mutex(vp
));
872 pp
= find_page(vp
, off
);
873 mutex_exit(page_vnode_mutex(vp
));
875 ASSERT(pp
== NULL
|| PAGE_LOCKED(pp
) || panicstr
);
880 * Determine whether a page with the specified [vp, off]
881 * currently exists in the system. Obviously this should
882 * only be considered as a hint since nothing prevents the
883 * page from disappearing or appearing immediately after
884 * the return from this routine.
886 * Note: This is virtually identical to page_find. Can we combine them?
889 page_exists(vnode_t
*vp
, uoff_t off
)
893 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp
)));
894 VM_STAT_ADD(page_exists_cnt
);
896 mutex_enter(page_vnode_mutex(vp
));
897 page
= find_page(vp
, off
);
898 mutex_exit(page_vnode_mutex(vp
));
904 * Determine if physically contiguous pages exist for [vp, off] - [vp, off +
905 * page_size(szc)) range. if they exist and ppa is not NULL fill ppa array
906 * with these pages locked SHARED. If necessary reclaim pages from
907 * freelist. Return 1 if contiguous pages exist and 0 otherwise.
909 * If we fail to lock pages still return 1 if pages exist and contiguous.
910 * But in this case return value is just a hint. ppa array won't be filled.
911 * Caller should initialize ppa[0] as NULL to distinguish return value.
913 * Returns 0 if pages don't exist or not physically contiguous.
915 * This routine doesn't work for anonymous(swapfs) pages.
918 page_exists_physcontig(vnode_t
*vp
, uoff_t off
, uint_t szc
, page_t
**ppa
)
925 uoff_t save_off
= off
;
932 ASSERT(!IS_SWAPFSVP(vp
));
933 ASSERT(!VN_ISKAS(vp
));
937 VM_STAT_ADD(page_exphcontg
[0]);
941 mutex_enter(page_vnode_mutex(vp
));
942 pp
= find_page(vp
, off
);
943 mutex_exit(page_vnode_mutex(vp
));
945 VM_STAT_ADD(page_exphcontg
[1]);
948 VM_STAT_ADD(page_exphcontg
[2]);
952 pages
= page_get_pagecnt(szc
);
954 pfn
= rootpp
->p_pagenum
;
956 if ((pszc
= pp
->p_szc
) >= szc
&& ppa
!= NULL
) {
957 VM_STAT_ADD(page_exphcontg
[3]);
958 if (!page_trylock(pp
, SE_SHARED
)) {
959 VM_STAT_ADD(page_exphcontg
[4]);
963 * Also check whether p_pagenum was modified by DR.
965 if (pp
->p_szc
!= pszc
|| pp
->p_vnode
!= vp
||
966 pp
->p_offset
!= off
|| pp
->p_pagenum
!= pfn
) {
967 VM_STAT_ADD(page_exphcontg
[5]);
973 * szc was non zero and vnode and offset matched after we
974 * locked the page it means it can't become free on us.
976 ASSERT(!PP_ISFREE(pp
));
977 if (!IS_P2ALIGNED(pfn
, pages
)) {
985 for (i
= 1; i
< pages
; i
++, pp
++, off
+= PAGESIZE
, pfn
++) {
986 if (!page_trylock(pp
, SE_SHARED
)) {
987 VM_STAT_ADD(page_exphcontg
[6]);
996 if (pp
->p_szc
!= pszc
) {
997 VM_STAT_ADD(page_exphcontg
[7]);
1009 * szc the same as for previous already locked pages
1010 * with right identity. Since this page had correct
1011 * szc after we locked it can't get freed or destroyed
1012 * and therefore must have the expected identity.
1014 ASSERT(!PP_ISFREE(pp
));
1015 if (pp
->p_vnode
!= vp
||
1016 pp
->p_offset
!= off
) {
1017 panic("page_exists_physcontig: "
1018 "large page identity doesn't match");
1021 ASSERT(pp
->p_pagenum
== pfn
);
1023 VM_STAT_ADD(page_exphcontg
[8]);
1026 } else if (pszc
>= szc
) {
1027 VM_STAT_ADD(page_exphcontg
[9]);
1028 if (!IS_P2ALIGNED(pfn
, pages
)) {
1034 if (!IS_P2ALIGNED(pfn
, pages
)) {
1035 VM_STAT_ADD(page_exphcontg
[10]);
1039 if (page_numtomemseg_nolock(pfn
) !=
1040 page_numtomemseg_nolock(pfn
+ pages
- 1)) {
1041 VM_STAT_ADD(page_exphcontg
[11]);
1046 * We loop up 4 times across pages to promote page size.
1047 * We're extra cautious to promote page size atomically with respect
1048 * to everybody else. But we can probably optimize into 1 loop if
1049 * this becomes an issue.
1052 for (i
= 0; i
< pages
; i
++, pp
++, off
+= PAGESIZE
, pfn
++) {
1053 if (!page_trylock(pp
, SE_EXCL
)) {
1054 VM_STAT_ADD(page_exphcontg
[12]);
1058 * Check whether p_pagenum was modified by DR.
1060 if (pp
->p_pagenum
!= pfn
) {
1064 if (pp
->p_vnode
!= vp
||
1065 pp
->p_offset
!= off
) {
1066 VM_STAT_ADD(page_exphcontg
[13]);
1070 if (pp
->p_szc
>= szc
) {
1079 VM_STAT_ADD(page_exphcontg
[14]);
1089 for (i
= 0; i
< pages
; i
++, pp
++) {
1090 if (PP_ISFREE(pp
)) {
1091 VM_STAT_ADD(page_exphcontg
[15]);
1092 ASSERT(!PP_ISAGED(pp
));
1093 ASSERT(pp
->p_szc
== 0);
1094 if (!page_reclaim(pp
, NULL
)) {
1098 ASSERT(pp
->p_szc
< szc
);
1099 VM_STAT_ADD(page_exphcontg
[16]);
1100 (void) hat_pageunload(pp
, HAT_FORCE_PGUNLOAD
);
1104 VM_STAT_ADD(page_exphcontg
[17]);
1106 * page_reclaim failed because we were out of memory.
1107 * drop the rest of the locks and return because this page
1108 * must be already reallocated anyway.
1111 for (j
= 0; j
< pages
; j
++, pp
++) {
1121 for (i
= 0; i
< pages
; i
++, pp
++, off
+= PAGESIZE
) {
1122 ASSERT(PAGE_EXCL(pp
));
1123 ASSERT(!PP_ISFREE(pp
));
1124 ASSERT(!hat_page_is_mapped(pp
));
1125 ASSERT(pp
->p_vnode
== vp
);
1126 ASSERT(pp
->p_offset
== off
);
1130 for (i
= 0; i
< pages
; i
++, pp
++) {
1135 page_downgrade(ppa
[i
]);
1141 VM_STAT_ADD(page_exphcontg
[18]);
1142 ASSERT(vn_has_cached_data(vp
));
1147 * Determine whether a page with the specified [vp, off]
1148 * currently exists in the system and if so return its
1149 * size code. Obviously this should only be considered as
1150 * a hint since nothing prevents the page from disappearing
1151 * or appearing immediately after the return from this routine.
1154 page_exists_forreal(vnode_t
*vp
, uoff_t off
, uint_t
*szc
)
1159 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp
)));
1160 ASSERT(szc
!= NULL
);
1161 VM_STAT_ADD(page_exists_forreal_cnt
);
1163 mutex_enter(page_vnode_mutex(vp
));
1164 pp
= find_page(vp
, off
);
1169 mutex_exit(page_vnode_mutex(vp
));
1173 /* wakeup threads waiting for pages in page_create_get_something() */
1177 if (!CV_HAS_WAITERS(&pcgs_cv
))
1179 cv_broadcast(&pcgs_cv
);
1183 * 'freemem' is used all over the kernel as an indication of how many
1184 * pages are free (either on the cache list or on the free page list)
1185 * in the system. In very few places is a really accurate 'freemem'
1186 * needed. To avoid contention of the lock protecting a the
1187 * single freemem, it was spread out into NCPU buckets. Set_freemem
1188 * sets freemem to the total of all NCPU buckets. It is called from
1189 * clock() on each TICK.
1200 for (i
= 0; i
< pcf_fanout
; i
++) {
1207 * Don't worry about grabbing mutex. It's not that
1208 * critical if we miss a tick or two. This is
1209 * where we wakeup possible delayers in
1210 * page_create_get_something().
1224 for (i
= 0; i
< pcf_fanout
; i
++) {
1229 * We just calculated it, might as well set it.
1236 * Acquire all of the page cache & free (pcf) locks.
1245 for (i
= 0; i
< pcf_fanout
; i
++) {
1246 mutex_enter(&p
->pcf_lock
);
1252 * Release all the pcf_locks.
1261 for (i
= 0; i
< pcf_fanout
; i
++) {
1262 mutex_exit(&p
->pcf_lock
);
1268 * Inform the VM system that we need some pages freed up.
1269 * Calls must be symmetric, e.g.:
1271 * page_needfree(100);
1273 * page_needfree(-100);
1276 page_needfree(spgcnt_t npages
)
1278 mutex_enter(&new_freemem_lock
);
1280 mutex_exit(&new_freemem_lock
);
1284 * Throttle for page_create(): try to prevent freemem from dropping
1285 * below throttlefree. We can't provide a 100% guarantee because
1286 * KM_NOSLEEP allocations, page_reclaim(), and various other things
1287 * nibble away at the freelist. However, we can block all PG_WAIT
1288 * allocations until memory becomes available. The motivation is
1289 * that several things can fall apart when there's no free memory:
1291 * (1) If pageout() needs memory to push a page, the system deadlocks.
1293 * (2) By (broken) specification, timeout(9F) can neither fail nor
1294 * block, so it has no choice but to panic the system if it
1295 * cannot allocate a callout structure.
1297 * (3) Like timeout(), ddi_set_callback() cannot fail and cannot block;
1298 * it panics if it cannot allocate a callback structure.
1300 * (4) Untold numbers of third-party drivers have not yet been hardened
1301 * against KM_NOSLEEP and/or allocb() failures; they simply assume
1302 * success and panic the system with a data fault on failure.
1303 * (The long-term solution to this particular problem is to ship
1304 * hostile fault-injecting DEBUG kernels with the DDK.)
1306 * It is theoretically impossible to guarantee success of non-blocking
1307 * allocations, but in practice, this throttle is very hard to break.
1310 page_create_throttle(pgcnt_t npages
, int flags
)
1314 pgcnt_t tf
; /* effective value of throttlefree */
1317 * Normal priority allocations.
1319 if ((flags
& (PG_WAIT
| PG_NORMALPRI
)) == PG_NORMALPRI
) {
1320 ASSERT(!(flags
& (PG_PANIC
| PG_PUSHPAGE
)));
1321 return (freemem
>= npages
+ throttlefree
);
1325 * Never deny pages when:
1326 * - it's a thread that cannot block [NOMEMWAIT()]
1327 * - the allocation cannot block and must not fail
1328 * - the allocation cannot block and is pageout dispensated
1331 ((flags
& (PG_WAIT
| PG_PANIC
)) == PG_PANIC
) ||
1332 ((flags
& (PG_WAIT
| PG_PUSHPAGE
)) == PG_PUSHPAGE
))
1336 * If the allocation can't block, we look favorably upon it
1337 * unless we're below pageout_reserve. In that case we fail
1338 * the allocation because we want to make sure there are a few
1339 * pages available for pageout.
1341 if ((flags
& PG_WAIT
) == 0)
1342 return (freemem
>= npages
+ pageout_reserve
);
1344 /* Calculate the effective throttlefree value */
1346 ((flags
& PG_PUSHPAGE
) ? pageout_reserve
: 0);
1348 cv_signal(&proc_pageout
->p_cv
);
1353 mutex_enter(&new_freemem_lock
);
1354 for (i
= 0; i
< pcf_fanout
; i
++) {
1355 fm
+= pcf
[i
].pcf_count
;
1357 mutex_exit(&pcf
[i
].pcf_lock
);
1360 if (freemem
>= npages
+ tf
) {
1361 mutex_exit(&new_freemem_lock
);
1366 cv_wait(&freemem_cv
, &new_freemem_lock
);
1369 mutex_exit(&new_freemem_lock
);
1375 * page_create_wait() is called to either coalesce pages from the
1376 * different pcf buckets or to wait because there simply are not
1377 * enough pages to satisfy the caller's request.
1379 * Sadly, this is called from platform/vm/vm_machdep.c
1382 page_create_wait(pgcnt_t npages
, uint_t flags
)
1389 * Wait until there are enough free pages to satisfy our
1391 * We set needfree += npages before prodding pageout, to make sure
1392 * it does real work when npages > lotsfree > freemem.
1394 VM_STAT_ADD(page_create_not_enough
);
1396 ASSERT(!kcage_on
? !(flags
& PG_NORELOC
) : 1);
1398 if ((flags
& PG_NORELOC
) &&
1399 kcage_freemem
< kcage_throttlefree
+ npages
)
1400 (void) kcage_create_throttle(npages
, flags
);
1402 if (freemem
< npages
+ throttlefree
)
1403 if (!page_create_throttle(npages
, flags
))
1406 if (pcf_decrement_bucket(npages
) ||
1407 pcf_decrement_multiple(&total
, npages
, 0))
1411 * All of the pcf locks are held, there are not enough pages
1412 * to satisfy the request (npages < total).
1413 * Be sure to acquire the new_freemem_lock before dropping
1414 * the pcf locks. This prevents dropping wakeups in page_free().
1415 * The order is always pcf_lock then new_freemem_lock.
1417 * Since we hold all the pcf locks, it is a good time to set freemem.
1419 * If the caller does not want to wait, return now.
1420 * Else turn the pageout daemon loose to find something
1421 * and wait till it does.
1426 if ((flags
& PG_WAIT
) == 0) {
1432 ASSERT(proc_pageout
!= NULL
);
1433 cv_signal(&proc_pageout
->p_cv
);
1436 * We are going to wait.
1437 * We currently hold all of the pcf_locks,
1438 * get the new_freemem_lock (it protects freemem_wait),
1439 * before dropping the pcf_locks.
1441 mutex_enter(&new_freemem_lock
);
1444 for (i
= 0; i
< pcf_fanout
; i
++) {
1446 mutex_exit(&p
->pcf_lock
);
1453 cv_wait(&freemem_cv
, &new_freemem_lock
);
1458 mutex_exit(&new_freemem_lock
);
1460 VM_STAT_ADD(page_create_not_enough_again
);
1464 * A routine to do the opposite of page_create_wait().
1467 page_create_putback(spgcnt_t npages
)
1474 * When a contiguous lump is broken up, we have to
1475 * deal with lots of pages (min 64) so lets spread
1476 * the wealth around.
1478 lump
= roundup(npages
, pcf_fanout
) / pcf_fanout
;
1481 for (p
= pcf
; (npages
> 0) && (p
< &pcf
[pcf_fanout
]); p
++) {
1482 which
= &p
->pcf_count
;
1484 mutex_enter(&p
->pcf_lock
);
1487 which
= &p
->pcf_reserve
;
1490 if (lump
< npages
) {
1491 *which
+= (uint_t
)lump
;
1494 *which
+= (uint_t
)npages
;
1499 mutex_enter(&new_freemem_lock
);
1501 * Check to see if some other thread
1502 * is actually waiting. Another bucket
1503 * may have woken it up by now. If there
1504 * are no waiters, then set our pcf_wait
1505 * count to zero to avoid coming in here
1510 cv_broadcast(&freemem_cv
);
1512 cv_signal(&freemem_cv
);
1518 mutex_exit(&new_freemem_lock
);
1520 mutex_exit(&p
->pcf_lock
);
1522 ASSERT(npages
== 0);
1526 * A helper routine for page_create_get_something.
1527 * The indenting got to deep down there.
1528 * Unblock the pcf counters. Any pages freed after
1529 * pcf_block got set are moved to pcf_count and
1530 * wakeups (cv_broadcast() or cv_signal()) are done as needed.
1538 /* Update freemem while we're here. */
1541 for (i
= 0; i
< pcf_fanout
; i
++) {
1542 mutex_enter(&p
->pcf_lock
);
1543 ASSERT(p
->pcf_count
== 0);
1544 p
->pcf_count
= p
->pcf_reserve
;
1546 freemem
+= p
->pcf_count
;
1548 mutex_enter(&new_freemem_lock
);
1550 if (p
->pcf_reserve
> 1) {
1551 cv_broadcast(&freemem_cv
);
1554 cv_signal(&freemem_cv
);
1560 mutex_exit(&new_freemem_lock
);
1563 mutex_exit(&p
->pcf_lock
);
1569 * Called from page_create_va() when both the cache and free lists
1570 * have been checked once.
1572 * Either returns a page or panics since the accounting was done
1573 * way before we got here.
1575 * We don't come here often, so leave the accounting on permanently.
1578 #define MAX_PCGS 100
1581 #define PCGS_TRIES 100
1583 #define PCGS_TRIES 10
1587 uint_t pcgs_counts
[PCGS_TRIES
];
1588 uint_t pcgs_too_many
;
1589 uint_t pcgs_entered
;
1590 uint_t pcgs_entered_noreloc
;
1592 uint_t pcgs_cagelocked
;
1593 #endif /* VM_STATS */
1596 page_create_get_something(vnode_t
*vp
, uoff_t off
, struct seg
*seg
,
1597 caddr_t vaddr
, uint_t flags
)
1606 VM_STAT_ADD(pcgs_entered
);
1609 * Tap any reserve freelists: if we fail now, we'll die
1610 * since the page(s) we're looking for have already been
1615 if ((flags
& PG_NORELOC
) != 0) {
1616 VM_STAT_ADD(pcgs_entered_noreloc
);
1618 * Requests for free pages from critical threads
1619 * such as pageout still won't throttle here, but
1620 * we must try again, to give the cageout thread
1621 * another chance to catch up. Since we already
1622 * accounted for the pages, we had better get them
1625 * N.B. All non-critical threads acquire the pcgs_cagelock
1626 * to serialize access to the freelists. This implements a
1627 * turnstile-type synchornization to avoid starvation of
1628 * critical requests for PG_NORELOC memory by non-critical
1629 * threads: all non-critical threads must acquire a 'ticket'
1630 * before passing through, which entails making sure
1631 * kcage_freemem won't fall below minfree prior to grabbing
1632 * pages from the freelists.
1634 if (kcage_create_throttle(1, flags
) == KCT_NONCRIT
) {
1635 mutex_enter(&pcgs_cagelock
);
1637 VM_STAT_ADD(pcgs_cagelocked
);
1642 * Time to get serious.
1643 * We failed to get a `correctly colored' page from both the
1644 * free and cache lists.
1645 * We escalate in stage.
1647 * First try both lists without worring about color.
1649 * Then, grab all page accounting locks (ie. pcf[]) and
1650 * steal any pages that they have and set the pcf_block flag to
1651 * stop deletions from the lists. This will help because
1652 * a page can get added to the free list while we are looking
1653 * at the cache list, then another page could be added to the cache
1654 * list allowing the page on the free list to be removed as we
1655 * move from looking at the cache list to the free list. This
1656 * could happen over and over. We would never find the page
1657 * we have accounted for.
1659 * Noreloc pages are a subset of the global (relocatable) page pool.
1660 * They are not tracked separately in the pcf bins, so it is
1661 * impossible to know when doing pcf accounting if the available
1662 * page(s) are noreloc pages or not. When looking for a noreloc page
1663 * it is quite easy to end up here even if the global (relocatable)
1664 * page pool has plenty of free pages but the noreloc pool is empty.
1666 * When the noreloc pool is empty (or low), additional noreloc pages
1667 * are created by converting pages from the global page pool. This
1668 * process will stall during pcf accounting if the pcf bins are
1669 * already locked. Such is the case when a noreloc allocation is
1670 * looping here in page_create_get_something waiting for more noreloc
1673 * Short of adding a new field to the pcf bins to accurately track
1674 * the number of free noreloc pages, we instead do not grab the
1675 * pcgs_lock, do not set the pcf blocks and do not timeout when
1676 * allocating a noreloc page. This allows noreloc allocations to
1677 * loop without blocking global page pool allocations.
1679 * NOTE: the behaviour of page_create_get_something has not changed
1680 * for the case of global page pool allocations.
1683 flags
&= ~PG_MATCH_COLOR
;
1685 #if defined(__i386) || defined(__amd64)
1686 flags
= page_create_update_flags_x86(flags
);
1689 lgrp
= lgrp_mem_choose(seg
, vaddr
, PAGESIZE
);
1691 for (count
= 0; kcage_on
|| count
< MAX_PCGS
; count
++) {
1692 pp
= page_get_freelist(vp
, off
, seg
, vaddr
, PAGESIZE
,
1695 pp
= page_get_cachelist(vp
, off
, seg
, vaddr
,
1700 * Serialize. Don't fight with other pcgs().
1702 if (!locked
&& (!kcage_on
|| !(flags
& PG_NORELOC
))) {
1703 mutex_enter(&pcgs_lock
);
1704 VM_STAT_ADD(pcgs_locked
);
1707 for (i
= 0; i
< pcf_fanout
; i
++) {
1708 mutex_enter(&p
->pcf_lock
);
1709 ASSERT(p
->pcf_block
== 0);
1711 p
->pcf_reserve
= p
->pcf_count
;
1713 mutex_exit(&p
->pcf_lock
);
1721 * Since page_free() puts pages on
1722 * a list then accounts for it, we
1723 * just have to wait for page_free()
1724 * to unlock any page it was working
1725 * with. The page_lock()-page_reclaim()
1726 * path falls in the same boat.
1728 * We don't need to check on the
1729 * PG_WAIT flag, we have already
1730 * accounted for the page we are
1731 * looking for in page_create_va().
1733 * We just wait a moment to let any
1734 * locked pages on the lists free up,
1735 * then continue around and try again.
1737 * Will be awakened by set_freemem().
1739 mutex_enter(&pcgs_wait_lock
);
1740 cv_wait(&pcgs_cv
, &pcgs_wait_lock
);
1741 mutex_exit(&pcgs_wait_lock
);
1745 if (count
>= PCGS_TRIES
) {
1746 VM_STAT_ADD(pcgs_too_many
);
1748 VM_STAT_ADD(pcgs_counts
[count
]);
1753 mutex_exit(&pcgs_lock
);
1756 mutex_exit(&pcgs_cagelock
);
1761 * we go down holding the pcf locks.
1763 panic("no %spage found %d",
1764 ((flags
& PG_NORELOC
) ? "non-reloc " : ""), count
);
1769 * Create enough pages for "bytes" worth of data starting at
1772 * Where flag must be one of:
1774 * PG_EXCL: Exclusive create (fail if any page already
1775 * exists in the page cache) which does not
1776 * wait for memory to become available.
1778 * PG_WAIT: Non-exclusive create which can wait for
1779 * memory to become available.
1781 * PG_PHYSCONTIG: Allocate physically contiguous pages.
1784 * A doubly linked list of pages is returned to the caller. Each page
1785 * on the list has the "exclusive" (p_selock) lock and "iolock" (p_iolock)
1788 * Unable to change the parameters to page_create() in a minor release,
1789 * we renamed page_create() to page_create_va(), changed all known calls
1790 * from page_create() to page_create_va(), and created this wrapper.
1792 * Upon a major release, we should break compatibility by deleting this
1793 * wrapper, and replacing all the strings "page_create_va", with "page_create".
1795 * NOTE: There is a copy of this interface as page_create_io() in
1796 * i86/vm/vm_machdep.c. Any bugs fixed here should be applied
1800 page_create(vnode_t
*vp
, uoff_t off
, size_t bytes
, uint_t flags
)
1802 caddr_t random_vaddr
;
1806 cmn_err(CE_WARN
, "Using deprecated interface page_create: caller %p",
1810 random_vaddr
= (caddr_t
)(((uintptr_t)vp
>> 7) ^
1811 (uintptr_t)(off
>> PAGESHIFT
));
1814 return (page_create_va(vp
, off
, bytes
, flags
, &kseg
, random_vaddr
));
1818 uint32_t pg_alloc_pgs_mtbf
= 0;
1822 * Used for large page support. It will attempt to allocate
1823 * a large page(s) off the freelist.
1825 * Returns non zero on failure.
1828 page_alloc_pages(struct vnode
*vp
, struct seg
*seg
, caddr_t addr
,
1829 page_t
**basepp
, page_t
*ppa
[], uint_t szc
, int anypgsz
, int pgflags
)
1831 pgcnt_t npgs
, curnpgs
, totpgs
;
1833 page_t
*pplist
= NULL
, *pp
;
1837 ASSERT(szc
!= 0 && szc
<= (page_num_pagesizes() - 1));
1838 ASSERT(pgflags
== 0 || pgflags
== PG_LOCAL
);
1841 * Check if system heavily prefers local large pages over remote
1842 * on systems with multiple lgroups.
1844 if (lpg_alloc_prefer
== LPAP_LOCAL
&& nlgrps
> 1) {
1848 VM_STAT_ADD(alloc_pages
[0]);
1851 if (pg_alloc_pgs_mtbf
&& !(gethrtime() % pg_alloc_pgs_mtbf
)) {
1857 * One must be NULL but not both.
1858 * And one must be non NULL but not both.
1860 ASSERT(basepp
!= NULL
|| ppa
!= NULL
);
1861 ASSERT(basepp
== NULL
|| ppa
== NULL
);
1863 #if defined(__i386) || defined(__amd64)
1864 while (page_chk_freelist(szc
) == 0) {
1865 VM_STAT_ADD(alloc_pages
[8]);
1866 if (anypgsz
== 0 || --szc
== 0)
1871 pgsz
= page_get_pagesize(szc
);
1872 totpgs
= curnpgs
= npgs
= pgsz
>> PAGESHIFT
;
1874 ASSERT(((uintptr_t)addr
& (pgsz
- 1)) == 0);
1876 (void) page_create_wait(npgs
, PG_WAIT
);
1878 while (npgs
&& szc
) {
1879 lgrp
= lgrp_mem_choose(seg
, addr
, pgsz
);
1880 if (pgflags
== PG_LOCAL
) {
1881 pp
= page_get_freelist(vp
, 0, seg
, addr
, pgsz
,
1884 pp
= page_get_freelist(vp
, 0, seg
, addr
, pgsz
,
1888 pp
= page_get_freelist(vp
, 0, seg
, addr
, pgsz
,
1892 VM_STAT_ADD(alloc_pages
[1]);
1893 page_list_concat(&pplist
, &pp
);
1894 ASSERT(npgs
>= curnpgs
);
1896 } else if (anypgsz
) {
1897 VM_STAT_ADD(alloc_pages
[2]);
1899 pgsz
= page_get_pagesize(szc
);
1900 curnpgs
= pgsz
>> PAGESHIFT
;
1902 VM_STAT_ADD(alloc_pages
[3]);
1903 ASSERT(npgs
== totpgs
);
1904 page_create_putback(npgs
);
1909 VM_STAT_ADD(alloc_pages
[4]);
1911 page_create_putback(npgs
);
1913 } else if (basepp
!= NULL
) {
1915 ASSERT(ppa
== NULL
);
1919 npgs
= totpgs
- npgs
;
1923 * Clear the free and age bits. Also if we were passed in a ppa then
1924 * fill it in with all the constituent pages from the large page. But
1925 * if we failed to allocate all the pages just free what we got.
1928 ASSERT(PP_ISFREE(pp
));
1929 ASSERT(PP_ISAGED(pp
));
1930 if (ppa
!= NULL
|| err
!= 0) {
1932 VM_STAT_ADD(alloc_pages
[5]);
1935 page_sub(&pplist
, pp
);
1939 VM_STAT_ADD(alloc_pages
[6]);
1940 ASSERT(pp
->p_szc
!= 0);
1941 curnpgs
= page_get_pagecnt(pp
->p_szc
);
1942 page_list_break(&pp
, &pplist
, curnpgs
);
1943 page_list_add_pages(pp
, 0);
1944 page_create_putback(curnpgs
);
1945 ASSERT(npgs
>= curnpgs
);
1950 VM_STAT_ADD(alloc_pages
[7]);
1961 * Get a single large page off of the freelists, and set it up for use.
1962 * Number of bytes requested must be a supported page size.
1964 * Note that this call may fail even if there is sufficient
1965 * memory available or PG_WAIT is set, so the caller must
1966 * be willing to fallback on page_create_va(), block and retry,
1967 * or fail the requester.
1970 page_create_va_large(vnode_t
*vp
, uoff_t off
, size_t bytes
, uint_t flags
,
1971 struct seg
*seg
, caddr_t vaddr
, void *arg
)
1977 lgrp_id_t
*lgrpid
= (lgrp_id_t
*)arg
;
1981 ASSERT((flags
& ~(PG_EXCL
| PG_WAIT
|
1982 PG_NORELOC
| PG_PANIC
| PG_PUSHPAGE
| PG_NORMALPRI
)) == 0);
1985 ASSERT((flags
& PG_EXCL
) == PG_EXCL
);
1987 npages
= btop(bytes
);
1989 if (!kcage_on
|| panicstr
) {
1991 * Cage is OFF, or we are single threaded in
1992 * panic, so make everything a RELOC request.
1994 flags
&= ~PG_NORELOC
;
1998 * Make sure there's adequate physical memory available.
1999 * Note: PG_WAIT is ignored here.
2001 if (freemem
<= throttlefree
+ npages
) {
2002 VM_STAT_ADD(page_create_large_cnt
[1]);
2007 * If cage is on, dampen draw from cage when available
2008 * cage space is low.
2010 if ((flags
& (PG_NORELOC
| PG_WAIT
)) == (PG_NORELOC
| PG_WAIT
) &&
2011 kcage_freemem
< kcage_throttlefree
+ npages
) {
2014 * The cage is on, the caller wants PG_NORELOC
2015 * pages and available cage memory is very low.
2016 * Call kcage_create_throttle() to attempt to
2017 * control demand on the cage.
2019 if (kcage_create_throttle(npages
, flags
) == KCT_FAILURE
) {
2020 VM_STAT_ADD(page_create_large_cnt
[2]);
2025 if (!pcf_decrement_bucket(npages
) &&
2026 !pcf_decrement_multiple(NULL
, npages
, 1)) {
2027 VM_STAT_ADD(page_create_large_cnt
[4]);
2032 * This is where this function behaves fundamentally differently
2033 * than page_create_va(); since we're intending to map the page
2034 * with a single TTE, we have to get it as a physically contiguous
2035 * hardware pagesize chunk. If we can't, we fail.
2037 if (lgrpid
!= NULL
&& *lgrpid
>= 0 && *lgrpid
<= lgrp_alloc_max
&&
2038 LGRP_EXISTS(lgrp_table
[*lgrpid
]))
2039 lgrp
= lgrp_table
[*lgrpid
];
2041 lgrp
= lgrp_mem_choose(seg
, vaddr
, bytes
);
2043 if ((rootpp
= page_get_freelist(&kvp
, off
, seg
, vaddr
,
2044 bytes
, flags
& ~PG_MATCH_COLOR
, lgrp
)) == NULL
) {
2045 page_create_putback(npages
);
2046 VM_STAT_ADD(page_create_large_cnt
[5]);
2051 * if we got the page with the wrong mtype give it back this is a
2052 * workaround for CR 6249718. When CR 6249718 is fixed we never get
2053 * inside "if" and the workaround becomes just a nop
2055 if (kcage_on
&& (flags
& PG_NORELOC
) && !PP_ISNORELOC(rootpp
)) {
2056 page_list_add_pages(rootpp
, 0);
2057 page_create_putback(npages
);
2058 VM_STAT_ADD(page_create_large_cnt
[6]);
2063 * If satisfying this request has left us with too little
2064 * memory, start the wheels turning to get some back. The
2065 * first clause of the test prevents waking up the pageout
2066 * daemon in situations where it would decide that there's
2069 if (nscan
< desscan
&& freemem
< minfree
) {
2070 cv_signal(&proc_pageout
->p_cv
);
2075 ASSERT(PAGE_EXCL(pp
));
2076 ASSERT(pp
->p_vnode
== NULL
);
2077 ASSERT(!hat_page_is_mapped(pp
));
2080 if (!page_hashin(pp
, vp
, off
, false))
2081 panic("page_create_large: hashin failed: page %p",
2088 VM_STAT_ADD(page_create_large_cnt
[0]);
2093 page_create_va(vnode_t
*vp
, uoff_t off
, size_t bytes
, uint_t flags
,
2094 struct seg
*seg
, caddr_t vaddr
)
2096 page_t
*plist
= NULL
;
2098 pgcnt_t found_on_free
= 0;
2104 ASSERT(bytes
!= 0 && vp
!= NULL
);
2106 if ((flags
& PG_EXCL
) == 0 && (flags
& PG_WAIT
) == 0) {
2107 panic("page_create: invalid flags");
2110 ASSERT((flags
& ~(PG_EXCL
| PG_WAIT
|
2111 PG_NORELOC
| PG_PANIC
| PG_PUSHPAGE
| PG_NORMALPRI
)) == 0);
2114 pages_req
= npages
= btopr(bytes
);
2116 * Try to see whether request is too large to *ever* be
2117 * satisfied, in order to prevent deadlock. We arbitrarily
2118 * decide to limit maximum size requests to max_page_get.
2120 if (npages
>= max_page_get
) {
2121 if ((flags
& PG_WAIT
) == 0) {
2125 "Request for too much kernel memory "
2126 "(%lu bytes), will hang forever", bytes
);
2132 if (!kcage_on
|| panicstr
) {
2134 * Cage is OFF, or we are single threaded in
2135 * panic, so make everything a RELOC request.
2137 flags
&= ~PG_NORELOC
;
2140 if (freemem
<= throttlefree
+ npages
)
2141 if (!page_create_throttle(npages
, flags
))
2145 * If cage is on, dampen draw from cage when available
2146 * cage space is low.
2148 if ((flags
& PG_NORELOC
) &&
2149 kcage_freemem
< kcage_throttlefree
+ npages
) {
2152 * The cage is on, the caller wants PG_NORELOC
2153 * pages and available cage memory is very low.
2154 * Call kcage_create_throttle() to attempt to
2155 * control demand on the cage.
2157 if (kcage_create_throttle(npages
, flags
) == KCT_FAILURE
)
2161 VM_STAT_ADD(page_create_cnt
[0]);
2163 if (!pcf_decrement_bucket(npages
)) {
2165 * Have to look harder. If npages is greater than
2166 * one, then we might have to coalesce the counters.
2168 * Go wait. We come back having accounted
2171 VM_STAT_ADD(page_create_cnt
[1]);
2172 if (!page_create_wait(npages
, flags
)) {
2173 VM_STAT_ADD(page_create_cnt
[2]);
2179 * If satisfying this request has left us with too little
2180 * memory, start the wheels turning to get some back. The
2181 * first clause of the test prevents waking up the pageout
2182 * daemon in situations where it would decide that there's
2185 if (nscan
< desscan
&& freemem
< minfree
) {
2186 cv_signal(&proc_pageout
->p_cv
);
2190 * Loop around collecting the requested number of pages.
2191 * Most of the time, we have to `create' a new page. With
2192 * this in mind, pull the page off the free list before
2193 * getting the hash lock. This will minimize the hash
2194 * lock hold time, nesting, and the like. If it turns
2195 * out we don't need the page, we put it back at the end.
2201 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp
)));
2205 * Try to get a page from the freelist (ie,
2206 * a page with no [vp, off] tag). If that
2207 * fails, use the cachelist.
2209 * During the first attempt at both the free
2210 * and cache lists we try for the correct color.
2213 * XXXX-how do we deal with virtual indexed
2214 * caches and and colors?
2216 VM_STAT_ADD(page_create_cnt
[4]);
2218 * Get lgroup to allocate next page of shared memory
2219 * from and use it to specify where to allocate
2220 * the physical memory
2222 lgrp
= lgrp_mem_choose(seg
, vaddr
, PAGESIZE
);
2223 npp
= page_get_freelist(vp
, off
, seg
, vaddr
, PAGESIZE
,
2224 flags
| PG_MATCH_COLOR
, lgrp
);
2226 npp
= page_get_cachelist(vp
, off
, seg
,
2227 vaddr
, flags
| PG_MATCH_COLOR
, lgrp
);
2229 npp
= page_create_get_something(vp
,
2231 flags
& ~PG_MATCH_COLOR
);
2234 if (PP_ISAGED(npp
) == 0) {
2236 * Since this page came from the
2237 * cachelist, we must destroy the
2238 * old vnode association.
2240 page_hashout(npp
, false);
2248 ASSERT(PAGE_EXCL(npp
));
2249 ASSERT(npp
->p_vnode
== NULL
);
2250 ASSERT(!hat_page_is_mapped(npp
));
2255 * Here we have a page in our hot little mits and are
2256 * just waiting to stuff it on the appropriate lists.
2257 * Get the mutex and check to see if it really does
2260 mutex_enter(page_vnode_mutex(vp
));
2261 pp
= find_page(vp
, off
);
2263 VM_STAT_ADD(page_create_new
);
2266 if (!page_hashin(pp
, vp
, off
, true)) {
2268 * Since we hold the page vnode page cache
2269 * mutex and just searched for this page,
2270 * page_hashin had better not fail. If it
2271 * does, that means some thread did not
2272 * follow the page hash mutex rules. Panic
2273 * now and get it over with. As usual, go
2274 * down holding all the locks.
2276 ASSERT(MUTEX_HELD(page_vnode_mutex(vp
)));
2277 panic("page_create: "
2278 "hashin failed %p %p %llx",
2279 (void *)pp
, (void *)vp
, off
);
2282 ASSERT(MUTEX_HELD(page_vnode_mutex(vp
)));
2283 mutex_exit(page_vnode_mutex(vp
));
2286 * Hat layer locking need not be done to set
2287 * the following bits since the page is not hashed
2288 * and was on the free list (i.e., had no mappings).
2290 * Set the reference bit to protect
2291 * against immediate pageout
2293 * XXXmh modify freelist code to set reference
2294 * bit so we don't have to do it here.
2296 page_set_props(pp
, P_REF
);
2299 VM_STAT_ADD(page_create_exists
);
2300 if (flags
& PG_EXCL
) {
2302 * Found an existing page, and the caller
2303 * wanted all new pages. Undo all of the work
2306 mutex_exit(page_vnode_mutex(vp
));
2307 while (plist
!= NULL
) {
2309 page_sub(&plist
, pp
);
2311 /* large pages should not end up here */
2312 ASSERT(pp
->p_szc
== 0);
2314 VN_DISPOSE(pp
, B_INVAL
, 0, kcred
);
2316 VM_STAT_ADD(page_create_found_one
);
2319 ASSERT(flags
& PG_WAIT
);
2320 if (!page_lock(pp
, SE_EXCL
, vp
, P_NO_RECLAIM
)) {
2322 * Start all over again if we blocked trying
2325 mutex_exit(page_vnode_mutex(vp
));
2326 VM_STAT_ADD(page_create_page_lock_failed
);
2329 mutex_exit(page_vnode_mutex(vp
));
2331 if (PP_ISFREE(pp
)) {
2332 ASSERT(PP_ISAGED(pp
) == 0);
2333 VM_STAT_ADD(pagecnt
.pc_get_cache
);
2334 page_list_sub(pp
, PG_CACHE_LIST
);
2341 * Got a page! It is locked. Acquire the i/o
2342 * lock since we are going to use the p_next and
2343 * p_prev fields to link the requested pages together.
2346 page_add(&plist
, pp
);
2347 plist
= plist
->p_next
;
2352 ASSERT((flags
& PG_EXCL
) ? (found_on_free
== pages_req
) : 1);
2356 * Did not need this page after all.
2357 * Put it back on the free list.
2359 VM_STAT_ADD(page_create_putbacks
);
2362 npp
->p_offset
= (uoff_t
)-1;
2363 page_list_add(npp
, PG_FREE_LIST
| PG_LIST_TAIL
);
2367 ASSERT(pages_req
>= found_on_free
);
2370 uint_t overshoot
= (uint_t
)(pages_req
- found_on_free
);
2373 VM_STAT_ADD(page_create_overshoot
);
2374 p
= &pcf
[PCF_INDEX()];
2375 mutex_enter(&p
->pcf_lock
);
2377 p
->pcf_reserve
+= overshoot
;
2379 p
->pcf_count
+= overshoot
;
2381 mutex_enter(&new_freemem_lock
);
2383 cv_signal(&freemem_cv
);
2388 mutex_exit(&new_freemem_lock
);
2391 mutex_exit(&p
->pcf_lock
);
2392 /* freemem is approximate, so this test OK */
2394 freemem
+= overshoot
;
2402 * One or more constituent pages of this large page has been marked
2403 * toxic. Simply demote the large page to PAGESIZE pages and let
2404 * page_free() handle it. This routine should only be called by
2405 * large page free routines (page_free_pages() and page_destroy_pages().
2406 * All pages are locked SE_EXCL and have already been marked free.
2409 page_free_toxic_pages(page_t
*rootpp
)
2412 pgcnt_t i
, pgcnt
= page_get_pagecnt(rootpp
->p_szc
);
2413 uint_t szc
= rootpp
->p_szc
;
2415 for (i
= 0, tpp
= rootpp
; i
< pgcnt
; i
++, tpp
= tpp
->p_next
) {
2416 ASSERT(tpp
->p_szc
== szc
);
2417 ASSERT((PAGE_EXCL(tpp
) &&
2418 !page_iolock_assert(tpp
)) || panicstr
);
2422 while (rootpp
!= NULL
) {
2424 page_sub(&rootpp
, tpp
);
2425 ASSERT(PP_ISFREE(tpp
));
2432 * Put page on the "free" list.
2433 * The free list is really two lists maintained by
2434 * the PSM of whatever machine we happen to be on.
2437 page_free(page_t
*pp
, int dontneed
)
2442 ASSERT((PAGE_EXCL(pp
) &&
2443 !page_iolock_assert(pp
)) || panicstr
);
2445 if (PP_ISFREE(pp
)) {
2446 panic("page_free: page %p is free", (void *)pp
);
2449 if (pp
->p_szc
!= 0) {
2450 if (pp
->p_vnode
== NULL
|| IS_SWAPFSVP(pp
->p_vnode
) ||
2452 panic("page_free: anon or kernel "
2453 "or no vnode large page %p", (void *)pp
);
2455 page_demote_vp_pages(pp
);
2456 ASSERT(pp
->p_szc
== 0);
2460 * The page_struct_lock need not be acquired to examine these
2461 * fields since the page has an "exclusive" lock.
2463 if (hat_page_is_mapped(pp
) || pp
->p_lckcnt
!= 0 || pp
->p_cowcnt
!= 0 ||
2464 pp
->p_slckcnt
!= 0) {
2465 panic("page_free pp=%p, pfn=%lx, lckcnt=%d, cowcnt=%d "
2466 "slckcnt = %d", (void *)pp
, page_pptonum(pp
), pp
->p_lckcnt
,
2467 pp
->p_cowcnt
, pp
->p_slckcnt
);
2471 ASSERT(!hat_page_getshare(pp
));
2474 ASSERT(pp
->p_vnode
== NULL
|| !IS_VMODSORT(pp
->p_vnode
) ||
2476 page_clr_all_props(pp
);
2477 ASSERT(!hat_page_getshare(pp
));
2480 * Now we add the page to the head of the free list.
2481 * But if this page is associated with a paged vnode
2482 * then we adjust the head forward so that the page is
2483 * effectively at the end of the list.
2485 if (pp
->p_vnode
== NULL
) {
2487 * Page has no identity, put it on the free list.
2490 pp
->p_offset
= (uoff_t
)-1;
2491 page_list_add(pp
, PG_FREE_LIST
| PG_LIST_TAIL
);
2492 VM_STAT_ADD(pagecnt
.pc_free_free
);
2497 /* move it to the tail of the list */
2498 page_list_add(pp
, PG_CACHE_LIST
| PG_LIST_TAIL
);
2500 VM_STAT_ADD(pagecnt
.pc_free_cache
);
2502 page_list_add(pp
, PG_CACHE_LIST
| PG_LIST_HEAD
);
2504 VM_STAT_ADD(pagecnt
.pc_free_dontneed
);
2510 * Now do the `freemem' accounting.
2512 pcf_index
= PCF_INDEX();
2513 p
= &pcf
[pcf_index
];
2515 mutex_enter(&p
->pcf_lock
);
2517 p
->pcf_reserve
+= 1;
2521 mutex_enter(&new_freemem_lock
);
2523 * Check to see if some other thread
2524 * is actually waiting. Another bucket
2525 * may have woken it up by now. If there
2526 * are no waiters, then set our pcf_wait
2527 * count to zero to avoid coming in here
2528 * next time. Also, since only one page
2529 * was put on the free list, just wake
2533 cv_signal(&freemem_cv
);
2538 mutex_exit(&new_freemem_lock
);
2541 mutex_exit(&p
->pcf_lock
);
2543 /* freemem is approximate, so this test OK */
2549 * Put page on the "free" list during intial startup.
2550 * This happens during initial single threaded execution.
2553 page_free_at_startup(page_t
*pp
)
2558 page_list_add(pp
, PG_FREE_LIST
| PG_LIST_HEAD
| PG_LIST_ISINIT
);
2559 VM_STAT_ADD(pagecnt
.pc_free_free
);
2562 * Now do the `freemem' accounting.
2564 pcf_index
= PCF_INDEX();
2565 p
= &pcf
[pcf_index
];
2567 ASSERT(p
->pcf_block
== 0);
2568 ASSERT(p
->pcf_wait
== 0);
2571 /* freemem is approximate, so this is OK */
2576 page_free_pages(page_t
*pp
)
2578 page_t
*tpp
, *rootpp
= NULL
;
2579 pgcnt_t pgcnt
= page_get_pagecnt(pp
->p_szc
);
2581 uint_t szc
= pp
->p_szc
;
2583 VM_STAT_ADD(pagecnt
.pc_free_pages
);
2585 ASSERT(pp
->p_szc
!= 0 && pp
->p_szc
< page_num_pagesizes());
2586 if ((page_pptonum(pp
) & (pgcnt
- 1)) != 0) {
2587 panic("page_free_pages: not root page %p", (void *)pp
);
2591 for (i
= 0, tpp
= pp
; i
< pgcnt
; i
++, tpp
++) {
2592 ASSERT((PAGE_EXCL(tpp
) &&
2593 !page_iolock_assert(tpp
)) || panicstr
);
2594 if (PP_ISFREE(tpp
)) {
2595 panic("page_free_pages: page %p is free", (void *)tpp
);
2598 if (hat_page_is_mapped(tpp
) || tpp
->p_lckcnt
!= 0 ||
2599 tpp
->p_cowcnt
!= 0 || tpp
->p_slckcnt
!= 0) {
2600 panic("page_free_pages %p", (void *)tpp
);
2604 ASSERT(!hat_page_getshare(tpp
));
2605 ASSERT(tpp
->p_vnode
== NULL
);
2606 ASSERT(tpp
->p_szc
== szc
);
2609 page_clr_all_props(tpp
);
2611 tpp
->p_offset
= (uoff_t
)-1;
2612 ASSERT(tpp
->p_next
== tpp
);
2613 ASSERT(tpp
->p_prev
== tpp
);
2614 page_list_concat(&rootpp
, &tpp
);
2616 ASSERT(rootpp
== pp
);
2618 page_list_add_pages(rootpp
, 0);
2619 page_create_putback(pgcnt
);
2625 * This routine attempts to return pages to the cachelist via page_release().
2626 * It does not *have* to be successful in all cases, since the pageout scanner
2627 * will catch any pages it misses. It does need to be fast and not introduce
2628 * too much overhead.
2630 * If a page isn't found on the unlocked sweep of the page_hash bucket, we
2631 * don't lock and retry. This is ok, since the page scanner will eventually
2632 * find any page we miss in free_vp_pages().
2635 free_vp_pages(vnode_t
*vp
, uoff_t off
, size_t len
)
2639 extern int swap_in_range(vnode_t
*, uoff_t
, size_t);
2643 if (free_pages
== 0)
2645 if (swap_in_range(vp
, off
, len
))
2648 for (; off
< eoff
; off
+= PAGESIZE
) {
2651 * find the page using a fast, but inexact search. It'll be OK
2652 * if a few pages slip through the cracks here.
2654 pp
= page_exists(vp
, off
);
2657 * If we didn't find the page (it may not exist), the page
2658 * is free, looks still in use (shared), or we can't lock it,
2663 page_share_cnt(pp
) > 0 ||
2664 !page_trylock(pp
, SE_EXCL
))
2668 * Once we have locked pp, verify that it's still the
2669 * correct page and not already free
2671 ASSERT(PAGE_LOCKED_SE(pp
, SE_EXCL
));
2672 if (pp
->p_vnode
!= vp
|| pp
->p_offset
!= off
|| PP_ISFREE(pp
)) {
2678 * try to release the page...
2680 (void) page_release(pp
, 1);
2685 * Reclaim the given page from the free list.
2686 * If pp is part of a large pages, only the given constituent page is reclaimed
2687 * and the large page it belonged to will be demoted. This can only happen
2688 * if the page is not on the cachelist.
2690 * Returns 1 on success or 0 on failure.
2692 * The page is unlocked if it can't be reclaimed (when freemem == 0).
2693 * If `lock' is non-null, it will be dropped and re-acquired if
2694 * the routine must wait while freemem is 0.
2696 * As it turns out, boot_getpages() does this. It picks a page,
2697 * based on where OBP mapped in some address, gets its pfn, searches
2698 * the memsegs, locks the page, then pulls it off the free list!
2701 page_reclaim(page_t
*pp
, vnode_t
*vnode
)
2708 ASSERT(vnode
!= NULL
? MUTEX_HELD(page_vnode_mutex(vnode
)) : 1);
2709 ASSERT(PAGE_EXCL(pp
) && PP_ISFREE(pp
));
2712 * If `freemem' is 0, we cannot reclaim this page from the
2713 * freelist, so release every lock we might hold: the page,
2714 * and the vnode page lock before blocking.
2716 * The only way `freemem' can become 0 while there are pages
2717 * marked free (have their p->p_free bit set) is when the
2718 * system is low on memory and doing a page_create(). In
2719 * order to guarantee that once page_create() starts acquiring
2720 * pages it will be able to get all that it needs since `freemem'
2721 * was decreased by the requested amount. So, we need to release
2722 * this page, and let page_create() have it.
2724 * Since `freemem' being zero is not supposed to happen, just
2725 * use the usual hash stuff as a starting point. If that bucket
2726 * is empty, then assume the worst, and start at the beginning
2727 * of the pcf array. If we always start at the beginning
2728 * when acquiring more than one pcf lock, there won't be any
2729 * deadlock problems.
2732 /* TODO: Do we need to test kcage_freemem if PG_NORELOC(pp)? */
2734 if (freemem
<= throttlefree
&& !page_create_throttle(1l, 0)) {
2736 goto page_reclaim_nomem
;
2739 enough
= pcf_decrement_bucket(1);
2742 VM_STAT_ADD(page_reclaim_zero
);
2744 * Check again. Its possible that some other thread
2745 * could have been right behind us, and added one
2746 * to a list somewhere. Acquire each of the pcf locks
2747 * until we find a page.
2750 for (i
= 0; i
< pcf_fanout
; i
++) {
2751 mutex_enter(&p
->pcf_lock
);
2752 if (p
->pcf_count
>= 1) {
2755 * freemem is not protected by any lock. Thus,
2756 * we cannot have any assertion containing
2769 * We really can't have page `pp'.
2770 * Time for the no-memory dance with
2771 * page_free(). This is just like
2772 * page_create_wait(). Plus the added
2773 * attraction of releasing the vnode page lock.
2774 * Page_unlock() will wakeup any thread
2775 * waiting around for this page.
2777 if (vnode
!= NULL
) {
2778 VM_STAT_ADD(page_reclaim_zero_locked
);
2779 mutex_exit(page_vnode_mutex(vnode
));
2784 * get this before we drop all the pcf locks.
2786 mutex_enter(&new_freemem_lock
);
2789 for (i
= 0; i
< pcf_fanout
; i
++) {
2791 mutex_exit(&p
->pcf_lock
);
2796 cv_wait(&freemem_cv
, &new_freemem_lock
);
2799 mutex_exit(&new_freemem_lock
);
2802 mutex_enter(page_vnode_mutex(vnode
));
2808 * The pcf accounting has been done,
2809 * though none of the pcf_wait flags have been set,
2810 * drop the locks and continue on.
2813 mutex_exit(&p
->pcf_lock
);
2819 VM_STAT_ADD(pagecnt
.pc_reclaim
);
2822 * page_list_sub will handle the case where pp is a large page.
2823 * It's possible that the page was promoted while on the freelist
2825 if (PP_ISAGED(pp
)) {
2826 page_list_sub(pp
, PG_FREE_LIST
);
2828 page_list_sub(pp
, PG_CACHE_LIST
);
2832 * clear the p_free & p_age bits since this page is no longer
2833 * on the free list. Notice that there was a brief time where
2834 * a page is marked as free, but is not on the list.
2836 * Set the reference bit to protect against immediate pageout.
2840 page_set_props(pp
, P_REF
);
2842 CPU_STATS_ENTER_K();
2843 cpup
= CPU
; /* get cpup now that CPU cannot change */
2844 CPU_STATS_ADDQ(cpup
, vm
, pgrec
, 1);
2845 CPU_STATS_ADDQ(cpup
, vm
, pgfrec
, 1);
2847 ASSERT(pp
->p_szc
== 0);
2853 * Destroy identity of the page and put it back on
2854 * the page free list. Assumes that the caller has
2855 * acquired the "exclusive" lock on the page.
2858 page_destroy(page_t
*pp
, int dontfree
)
2860 ASSERT((PAGE_EXCL(pp
) &&
2861 !page_iolock_assert(pp
)) || panicstr
);
2862 ASSERT(pp
->p_slckcnt
== 0 || panicstr
);
2864 if (pp
->p_szc
!= 0) {
2865 if (pp
->p_vnode
== NULL
|| IS_SWAPFSVP(pp
->p_vnode
) ||
2867 panic("page_destroy: anon or kernel or no vnode "
2868 "large page %p", (void *)pp
);
2870 page_demote_vp_pages(pp
);
2871 ASSERT(pp
->p_szc
== 0);
2875 * Unload translations, if any, then hash out the
2876 * page to erase its identity.
2878 (void) hat_pageunload(pp
, HAT_FORCE_PGUNLOAD
);
2879 page_hashout(pp
, false);
2883 * Acquire the "freemem_lock" for availrmem.
2884 * The page_struct_lock need not be acquired for lckcnt
2885 * and cowcnt since the page has an "exclusive" lock.
2886 * We are doing a modified version of page_pp_unlock here.
2888 if ((pp
->p_lckcnt
!= 0) || (pp
->p_cowcnt
!= 0)) {
2889 mutex_enter(&freemem_lock
);
2890 if (pp
->p_lckcnt
!= 0) {
2895 if (pp
->p_cowcnt
!= 0) {
2896 availrmem
+= pp
->p_cowcnt
;
2897 pages_locked
-= pp
->p_cowcnt
;
2900 mutex_exit(&freemem_lock
);
2903 * Put the page on the "free" list.
2910 page_destroy_pages(page_t
*pp
)
2913 page_t
*tpp
, *rootpp
= NULL
;
2914 pgcnt_t pgcnt
= page_get_pagecnt(pp
->p_szc
);
2915 pgcnt_t i
, pglcks
= 0;
2916 uint_t szc
= pp
->p_szc
;
2918 ASSERT(pp
->p_szc
!= 0 && pp
->p_szc
< page_num_pagesizes());
2920 VM_STAT_ADD(pagecnt
.pc_destroy_pages
);
2922 if ((page_pptonum(pp
) & (pgcnt
- 1)) != 0) {
2923 panic("page_destroy_pages: not root page %p", (void *)pp
);
2927 for (i
= 0, tpp
= pp
; i
< pgcnt
; i
++, tpp
++) {
2928 ASSERT((PAGE_EXCL(tpp
) &&
2929 !page_iolock_assert(tpp
)) || panicstr
);
2930 ASSERT(tpp
->p_slckcnt
== 0 || panicstr
);
2931 (void) hat_pageunload(tpp
, HAT_FORCE_PGUNLOAD
);
2932 page_hashout(tpp
, false);
2933 ASSERT(tpp
->p_offset
== (uoff_t
)-1);
2934 if (tpp
->p_lckcnt
!= 0) {
2937 } else if (tpp
->p_cowcnt
!= 0) {
2938 pglcks
+= tpp
->p_cowcnt
;
2941 ASSERT(!hat_page_getshare(tpp
));
2942 ASSERT(tpp
->p_vnode
== NULL
);
2943 ASSERT(tpp
->p_szc
== szc
);
2946 page_clr_all_props(tpp
);
2948 ASSERT(tpp
->p_next
== tpp
);
2949 ASSERT(tpp
->p_prev
== tpp
);
2950 page_list_concat(&rootpp
, &tpp
);
2953 ASSERT(rootpp
== pp
);
2955 mutex_enter(&freemem_lock
);
2956 availrmem
+= pglcks
;
2957 mutex_exit(&freemem_lock
);
2960 page_list_add_pages(rootpp
, 0);
2961 page_create_putback(pgcnt
);
2965 * Similar to page_destroy(), but destroys pages which are
2966 * locked and known to be on the page free list. Since
2967 * the page is known to be free and locked, no one can access
2970 * Also, the number of free pages does not change.
2973 page_destroy_free(page_t
*pp
)
2975 ASSERT(PAGE_EXCL(pp
));
2976 ASSERT(PP_ISFREE(pp
));
2977 ASSERT(pp
->p_vnode
);
2978 ASSERT(hat_page_getattr(pp
, P_MOD
| P_REF
| P_RO
) == 0);
2979 ASSERT(!hat_page_is_mapped(pp
));
2980 ASSERT(PP_ISAGED(pp
) == 0);
2981 ASSERT(pp
->p_szc
== 0);
2983 VM_STAT_ADD(pagecnt
.pc_destroy_free
);
2984 page_list_sub(pp
, PG_CACHE_LIST
);
2986 page_hashout(pp
, false);
2987 ASSERT(pp
->p_vnode
== NULL
);
2988 ASSERT(pp
->p_offset
== (uoff_t
)-1);
2991 page_list_add(pp
, PG_FREE_LIST
| PG_LIST_TAIL
);
2994 mutex_enter(&new_freemem_lock
);
2996 cv_signal(&freemem_cv
);
2998 mutex_exit(&new_freemem_lock
);
3002 * Rename the page "opp" to have an identity specified
3003 * by [vp, off]. If a page already exists with this name
3004 * it is locked and destroyed. Note that the page's
3005 * translations are not unloaded during the rename.
3007 * This routine is used by the anon layer to "steal" the
3008 * original page and is not unlike destroying a page and
3009 * creating a new page using the same page frame.
3011 * XXX -- Could deadlock if caller 1 tries to rename A to B while
3012 * caller 2 tries to rename B to A.
3015 page_rename(page_t
*opp
, vnode_t
*vp
, uoff_t off
)
3021 ASSERT(PAGE_EXCL(opp
) && !page_iolock_assert(opp
));
3022 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp
)));
3023 ASSERT(PP_ISFREE(opp
) == 0);
3025 VM_STAT_ADD(page_rename_count
);
3028 * CacheFS may call page_rename for a large NFS page
3029 * when both CacheFS and NFS mount points are used
3030 * by applications. Demote this large page before
3031 * renaming it, to ensure that there are no "partial"
3032 * large pages left lying around.
3034 if (opp
->p_szc
!= 0) {
3035 vnode_t
*ovp
= opp
->p_vnode
;
3036 ASSERT(ovp
!= NULL
);
3037 ASSERT(!IS_SWAPFSVP(ovp
));
3038 ASSERT(!VN_ISKAS(ovp
));
3039 page_demote_vp_pages(opp
);
3040 ASSERT(opp
->p_szc
== 0);
3043 page_hashout(opp
, false);
3046 mutex_enter(page_vnode_mutex(vp
));
3049 * Look for an existing page with this name and destroy it if found.
3050 * By holding the page hash lock all the way to the page_hashin()
3051 * call, we are assured that no page can be created with this
3052 * identity. In the case when the phm lock is dropped to undo any
3053 * hat layer mappings, the existing page is held with an "exclusive"
3054 * lock, again preventing another page from being created with
3057 pp
= find_page(vp
, off
);
3059 VM_STAT_ADD(page_rename_exists
);
3062 * As it turns out, this is one of only two places where
3063 * page_lock() needs to hold the passed in lock in the
3064 * successful case. In all of the others, the lock could
3065 * be dropped as soon as the attempt is made to lock
3066 * the page. It is tempting to add yet another arguement,
3067 * PL_KEEP or PL_DROP, to let page_lock know what to do.
3069 if (!page_lock(pp
, SE_EXCL
, vp
, P_RECLAIM
)) {
3071 * Went to sleep because the page could not
3072 * be locked. We were woken up when the page
3073 * was unlocked, or when the page was destroyed.
3074 * In either case, `phm' was dropped while we
3075 * slept. Hence we should not just roar through
3082 * If an existing page is a large page, then demote
3083 * it to ensure that no "partial" large pages are
3084 * "created" after page_rename. An existing page
3085 * can be a CacheFS page, and can't belong to swapfs.
3087 if (hat_page_is_mapped(pp
)) {
3089 * Unload translations. Since we hold the
3090 * exclusive lock on this page, the page
3091 * can not be changed while we drop phm.
3092 * This is also not a lock protocol violation,
3093 * but rather the proper way to do things.
3095 mutex_exit(page_vnode_mutex(vp
));
3096 (void) hat_pageunload(pp
, HAT_FORCE_PGUNLOAD
);
3097 if (pp
->p_szc
!= 0) {
3098 ASSERT(!IS_SWAPFSVP(vp
));
3099 ASSERT(!VN_ISKAS(vp
));
3100 page_demote_vp_pages(pp
);
3101 ASSERT(pp
->p_szc
== 0);
3103 mutex_enter(page_vnode_mutex(vp
));
3104 } else if (pp
->p_szc
!= 0) {
3105 ASSERT(!IS_SWAPFSVP(vp
));
3106 ASSERT(!VN_ISKAS(vp
));
3107 mutex_exit(page_vnode_mutex(vp
));
3108 page_demote_vp_pages(pp
);
3109 ASSERT(pp
->p_szc
== 0);
3110 mutex_enter(page_vnode_mutex(vp
));
3112 page_hashout(pp
, true);
3115 * Hash in the page with the new identity.
3117 if (!page_hashin(opp
, vp
, off
, true)) {
3119 * We were holding phm while we searched for [vp, off]
3120 * and only dropped phm if we found and locked a page.
3121 * If we can't create this page now, then some thing
3124 panic("page_rename: Can't hash in page: %p", (void *)pp
);
3128 ASSERT(MUTEX_HELD(page_vnode_mutex(vp
)));
3129 mutex_exit(page_vnode_mutex(vp
));
3132 * Now that we have dropped phm, lets get around to finishing up
3136 ASSERT(!hat_page_is_mapped(pp
));
3137 /* for now large pages should not end up here */
3138 ASSERT(pp
->p_szc
== 0);
3140 * Save the locks for transfer to the new page and then
3141 * clear them so page_free doesn't think they're important.
3142 * The page_struct_lock need not be acquired for lckcnt and
3143 * cowcnt since the page has an "exclusive" lock.
3145 olckcnt
= pp
->p_lckcnt
;
3146 ocowcnt
= pp
->p_cowcnt
;
3147 pp
->p_lckcnt
= pp
->p_cowcnt
= 0;
3150 * Put the page on the "free" list after we drop
3151 * the lock. The less work under the lock the better.
3153 VN_DISPOSE(pp
, B_FREE
, 0, kcred
);
3157 * Transfer the lock count from the old page (if any).
3158 * The page_struct_lock need not be acquired for lckcnt and
3159 * cowcnt since the page has an "exclusive" lock.
3161 opp
->p_lckcnt
+= olckcnt
;
3162 opp
->p_cowcnt
+= ocowcnt
;
3166 * low level routine to add page `page' to the AVL tree and vnode chains for
3169 * Pages are normally inserted at the start of a vnode's v_pagecache_list.
3170 * If the vnode is VMODSORT and the page is modified, it goes at the end.
3171 * This can happen when a modified page is relocated for DR.
3173 * Returns 1 on success and 0 on failure.
3176 page_do_hashin(page_t
*page
, vnode_t
*vnode
, uoff_t offset
)
3181 ASSERT(PAGE_EXCL(page
));
3182 ASSERT(vnode
!= NULL
);
3183 ASSERT(MUTEX_HELD(page_vnode_mutex(vnode
)));
3186 * Be sure to set these up before the page is inserted into the AVL
3187 * tree. As soon as the page is placed on the list some other
3188 * thread might get confused and wonder how this page could
3189 * possibly hash to this list.
3191 page
->p_vnode
= vnode
;
3192 page
->p_offset
= offset
;
3195 * record if this page is on a swap vnode
3197 if ((vnode
->v_flag
& VISSWAP
) != 0)
3201 * Duplicates are not allowed - fail to insert if we already have a
3202 * page with this identity.
3204 if (avl_find(&vnode
->v_pagecache
, page
, &where
) != NULL
) {
3205 page
->p_vnode
= NULL
;
3206 page
->p_offset
= (uoff_t
)(-1);
3210 avl_insert(&vnode
->v_pagecache
, page
, where
);
3213 * Add the page to the vnode's list of pages
3215 if (IS_VMODSORT(vnode
) && hat_ismod(page
))
3216 vnode_add_page_tail(vnode
, page
);
3218 vnode_add_page_head(vnode
, page
);
3224 * Add page `pp' to both the hash and vp chains for [vp, offset].
3226 * Returns 1 on success and 0 on failure.
3227 * If `locked` is true, we do *not* attempt to lock the vnode's page mutex.
3230 page_hashin(page_t
*pp
, vnode_t
*vp
, uoff_t offset
, bool locked
)
3234 ASSERT(pp
->p_fsdata
== 0 || panicstr
);
3236 VM_STAT_ADD(hashin_count
);
3239 VM_STAT_ADD(hashin_not_held
);
3240 mutex_enter(page_vnode_mutex(vp
));
3243 rc
= page_do_hashin(pp
, vp
, offset
);
3246 mutex_exit(page_vnode_mutex(vp
));
3249 VM_STAT_ADD(hashin_already
);
3255 * Remove page `page' from the AVL tree and vnode chains and remove its
3256 * vnode association. All mutexes must be held
3259 page_do_hashout(page_t
*page
)
3263 vnode_t
*vnode
= page
->p_vnode
;
3265 ASSERT(vnode
!= NULL
);
3266 ASSERT(MUTEX_HELD(page_vnode_mutex(vnode
)));
3268 avl_remove(&vnode
->v_pagecache
, page
);
3270 vnode_remove_page(vnode
, page
);
3272 page_clr_all_props(page
);
3274 page
->p_vnode
= NULL
;
3275 page
->p_offset
= (uoff_t
)-1;
3280 * Remove page `page' from the AVL tree and vnode chains and remove vnode
3283 * When `locked` is true, we do *not* attempt to lock the vnode's page
3287 page_hashout(page_t
*pp
, bool locked
)
3293 ASSERT(hold
!= NULL
? MUTEX_HELD(hold
) : 1);
3294 ASSERT(pp
->p_vnode
!= NULL
);
3295 ASSERT((PAGE_EXCL(pp
) && !page_iolock_assert(pp
)) || panicstr
);
3300 VM_STAT_ADD(hashout_not_held
);
3301 mutex_enter(page_vnode_mutex(vp
));
3304 page_do_hashout(pp
);
3307 mutex_exit(page_vnode_mutex(vp
));
3310 * Wake up processes waiting for this page. The page's
3311 * identity has been changed, and is probably not the
3312 * desired page any longer.
3314 sep
= page_se_mutex(pp
);
3316 pp
->p_selock
&= ~SE_EWANTED
;
3317 if (CV_HAS_WAITERS(&pp
->p_cv
))
3318 cv_broadcast(&pp
->p_cv
);
3323 * Add the page to the front of a linked list of pages
3324 * using the p_next & p_prev pointers for the list.
3325 * The caller is responsible for protecting the list pointers.
3328 page_add(page_t
**ppp
, page_t
*pp
)
3330 ASSERT(PAGE_EXCL(pp
) || (PAGE_SHARED(pp
) && page_iolock_assert(pp
)));
3332 page_add_common(ppp
, pp
);
3338 * Common code for page_add() and mach_page_add()
3341 page_add_common(page_t
**ppp
, page_t
*pp
)
3344 pp
->p_next
= pp
->p_prev
= pp
;
3347 pp
->p_prev
= (*ppp
)->p_prev
;
3348 (*ppp
)->p_prev
= pp
;
3349 pp
->p_prev
->p_next
= pp
;
3356 * Remove this page from a linked list of pages
3357 * using the p_next & p_prev pointers for the list.
3359 * The caller is responsible for protecting the list pointers.
3362 page_sub(page_t
**ppp
, page_t
*pp
)
3364 ASSERT((PP_ISFREE(pp
)) ? 1 :
3365 (PAGE_EXCL(pp
)) || (PAGE_SHARED(pp
) && page_iolock_assert(pp
)));
3367 if (*ppp
== NULL
|| pp
== NULL
) {
3368 panic("page_sub: bad arg(s): pp %p, *ppp %p",
3369 (void *)pp
, (void *)(*ppp
));
3373 page_sub_common(ppp
, pp
);
3378 * Common code for page_sub() and mach_page_sub()
3381 page_sub_common(page_t
**ppp
, page_t
*pp
)
3384 *ppp
= pp
->p_next
; /* go to next page */
3387 *ppp
= NULL
; /* page list is gone */
3389 pp
->p_prev
->p_next
= pp
->p_next
;
3390 pp
->p_next
->p_prev
= pp
->p_prev
;
3392 pp
->p_prev
= pp
->p_next
= pp
; /* make pp a list of one */
3397 * Break page list cppp into two lists with npages in the first list.
3398 * The tail is returned in nppp.
3401 page_list_break(page_t
**oppp
, page_t
**nppp
, pgcnt_t npages
)
3403 page_t
*s1pp
= *oppp
;
3405 page_t
*e1pp
, *e2pp
;
3417 for (n
= 0, s2pp
= *oppp
; n
< npages
; n
++) {
3418 s2pp
= s2pp
->p_next
;
3420 /* Fix head and tail of new lists */
3421 e1pp
= s2pp
->p_prev
;
3422 e2pp
= s1pp
->p_prev
;
3423 s1pp
->p_prev
= e1pp
;
3424 e1pp
->p_next
= s1pp
;
3425 s2pp
->p_prev
= e2pp
;
3426 e2pp
->p_next
= s2pp
;
3428 /* second list empty */
3439 * Concatenate page list nppp onto the end of list ppp.
3442 page_list_concat(page_t
**ppp
, page_t
**nppp
)
3444 page_t
*s1pp
, *s2pp
, *e1pp
, *e2pp
;
3446 if (*nppp
== NULL
) {
3454 e1pp
= s1pp
->p_prev
;
3456 e2pp
= s2pp
->p_prev
;
3457 s1pp
->p_prev
= e2pp
;
3458 e2pp
->p_next
= s1pp
;
3459 e1pp
->p_next
= s2pp
;
3460 s2pp
->p_prev
= e1pp
;
3464 * return the next page in the page list
3467 page_list_next(page_t
*pp
)
3469 return (pp
->p_next
);
3474 * Add the page to the front of the linked list of pages
3475 * using p_list.vnode for the list.
3477 * The caller is responsible for protecting the lists.
3480 page_vpadd(page_t
**ppp
, page_t
*pp
)
3482 panic("%s should not be used", __func__
);
3486 page_lpadd(page_t
**ppp
, page_t
*pp
)
3489 pp
->p_list
.largepg
.next
= pp
->p_list
.largepg
.prev
= pp
;
3491 pp
->p_list
.largepg
.next
= *ppp
;
3492 pp
->p_list
.largepg
.prev
= (*ppp
)->p_list
.largepg
.prev
;
3493 (*ppp
)->p_list
.largepg
.prev
= pp
;
3494 pp
->p_list
.largepg
.prev
->p_list
.largepg
.next
= pp
;
3500 * Remove this page from the linked list of pages
3501 * using p_list.vnode for the list.
3503 * The caller is responsible for protecting the lists.
3506 page_vpsub(page_t
**ppp
, page_t
*pp
)
3508 panic("%s should not be used", __func__
);
3512 page_lpsub(page_t
**ppp
, page_t
*pp
)
3514 if (*ppp
== NULL
|| pp
== NULL
) {
3515 panic("page_vpsub: bad arg(s): pp %p, *ppp %p",
3516 (void *)pp
, (void *)(*ppp
));
3521 *ppp
= pp
->p_list
.largepg
.next
; /* go to next page */
3524 *ppp
= NULL
; /* page list is gone */
3526 pp
->p_list
.largepg
.prev
->p_list
.largepg
.next
= pp
->p_list
.largepg
.next
;
3527 pp
->p_list
.largepg
.next
->p_list
.largepg
.prev
= pp
->p_list
.largepg
.prev
;
3529 pp
->p_list
.largepg
.prev
= pp
->p_list
.largepg
.next
= pp
; /* make pp a list of one */
3533 * Lock a physical page into memory "long term". Used to support "lock
3534 * in memory" functions. Accepts the page to be locked, and a cow variable
3535 * to indicate whether a the lock will travel to the new page during
3536 * a potential copy-on-write.
3540 page_t
*pp
, /* page to be locked */
3541 int cow
, /* cow lock */
3542 int kernel
) /* must succeed -- ignore checking */
3544 int r
= 0; /* result -- assume failure */
3546 ASSERT(PAGE_LOCKED(pp
));
3548 page_struct_lock(pp
);
3550 * Acquire the "freemem_lock" for availrmem.
3553 mutex_enter(&freemem_lock
);
3554 if ((availrmem
> pages_pp_maximum
) &&
3555 (pp
->p_cowcnt
< (ushort_t
)PAGE_LOCK_MAXIMUM
)) {
3558 mutex_exit(&freemem_lock
);
3560 if (++pp
->p_cowcnt
== (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3562 "COW lock limit reached on pfn 0x%lx",
3566 mutex_exit(&freemem_lock
);
3569 if (pp
->p_lckcnt
< (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3571 if (++pp
->p_lckcnt
==
3572 (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3573 cmn_err(CE_WARN
, "Page lock limit "
3574 "reached on pfn 0x%lx",
3580 /* availrmem accounting done by caller */
3584 mutex_enter(&freemem_lock
);
3585 if (availrmem
> pages_pp_maximum
) {
3591 mutex_exit(&freemem_lock
);
3595 page_struct_unlock(pp
);
3600 * Decommit a lock on a physical page frame. Account for cow locks if
3605 page_t
*pp
, /* page to be unlocked */
3606 int cow
, /* expect cow lock */
3607 int kernel
) /* this was a kernel lock */
3609 ASSERT(PAGE_LOCKED(pp
));
3611 page_struct_lock(pp
);
3613 * Acquire the "freemem_lock" for availrmem.
3614 * If cowcnt or lcknt is already 0 do nothing; i.e., we
3615 * could be called to unlock even if nothing is locked. This could
3616 * happen if locked file pages were truncated (removing the lock)
3617 * and the file was grown again and new pages faulted in; the new
3618 * pages are unlocked but the segment still thinks they're locked.
3622 mutex_enter(&freemem_lock
);
3626 mutex_exit(&freemem_lock
);
3629 if (pp
->p_lckcnt
&& --pp
->p_lckcnt
== 0) {
3631 mutex_enter(&freemem_lock
);
3634 mutex_exit(&freemem_lock
);
3638 page_struct_unlock(pp
);
3642 * This routine reserves availrmem for npages;
3643 * flags: KM_NOSLEEP or KM_SLEEP
3644 * returns 1 on success or 0 on failure
3647 page_resv(pgcnt_t npages
, uint_t flags
)
3649 mutex_enter(&freemem_lock
);
3650 while (availrmem
< tune
.t_minarmem
+ npages
) {
3651 if (flags
& KM_NOSLEEP
) {
3652 mutex_exit(&freemem_lock
);
3655 mutex_exit(&freemem_lock
);
3656 page_needfree(npages
);
3659 page_needfree(-(spgcnt_t
)npages
);
3660 mutex_enter(&freemem_lock
);
3662 availrmem
-= npages
;
3663 mutex_exit(&freemem_lock
);
3668 * This routine unreserves availrmem for npages;
3671 page_unresv(pgcnt_t npages
)
3673 mutex_enter(&freemem_lock
);
3674 availrmem
+= npages
;
3675 mutex_exit(&freemem_lock
);
3679 * See Statement at the beginning of segvn_lockop() regarding
3680 * the way we handle cowcnts and lckcnts.
3682 * Transfer cowcnt on 'opp' to cowcnt on 'npp' if the vpage
3683 * that breaks COW has PROT_WRITE.
3685 * Note that, we may also break COW in case we are softlocking
3686 * on read access during physio;
3687 * in this softlock case, the vpage may not have PROT_WRITE.
3688 * So, we need to transfer lckcnt on 'opp' to lckcnt on 'npp'
3689 * if the vpage doesn't have PROT_WRITE.
3691 * This routine is never called if we are stealing a page
3694 * The caller subtracted from availrmem for read only mapping.
3695 * if lckcnt is 1 increment availrmem.
3699 page_t
*opp
, /* original page frame losing lock */
3700 page_t
*npp
, /* new page frame gaining lock */
3701 uint_t write_perm
) /* set if vpage has PROT_WRITE */
3706 ASSERT(PAGE_LOCKED(opp
));
3707 ASSERT(PAGE_LOCKED(npp
));
3710 * Since we have two pages we probably have two locks. We need to take
3711 * them in a defined order to avoid deadlocks. It's also possible they
3712 * both hash to the same lock in which case this is a non-issue.
3714 nidx
= PAGE_LLOCK_HASH(PP_PAGEROOT(npp
));
3715 oidx
= PAGE_LLOCK_HASH(PP_PAGEROOT(opp
));
3717 page_struct_lock(npp
);
3718 page_struct_lock(opp
);
3719 } else if (oidx
< nidx
) {
3720 page_struct_lock(opp
);
3721 page_struct_lock(npp
);
3722 } else { /* The pages hash to the same lock */
3723 page_struct_lock(npp
);
3726 ASSERT(npp
->p_cowcnt
== 0);
3727 ASSERT(npp
->p_lckcnt
== 0);
3729 /* Don't use claim if nothing is locked (see page_pp_unlock above) */
3730 if ((write_perm
&& opp
->p_cowcnt
!= 0) ||
3731 (!write_perm
&& opp
->p_lckcnt
!= 0)) {
3735 ASSERT(opp
->p_cowcnt
!= 0);
3739 ASSERT(opp
->p_lckcnt
!= 0);
3742 * We didn't need availrmem decremented if p_lckcnt on
3743 * original page is 1. Here, we are unlocking
3744 * read-only copy belonging to original page and
3745 * are locking a copy belonging to new page.
3747 if (opp
->p_lckcnt
== 1)
3755 mutex_enter(&freemem_lock
);
3758 mutex_exit(&freemem_lock
);
3762 page_struct_unlock(opp
);
3763 page_struct_unlock(npp
);
3764 } else if (oidx
< nidx
) {
3765 page_struct_unlock(npp
);
3766 page_struct_unlock(opp
);
3767 } else { /* The pages hash to the same lock */
3768 page_struct_unlock(npp
);
3773 * Simple claim adjust functions -- used to support changes in
3774 * claims due to changes in access permissions. Used by segvn_setprot().
3777 page_addclaim(page_t
*pp
)
3779 int r
= 0; /* result */
3781 ASSERT(PAGE_LOCKED(pp
));
3783 page_struct_lock(pp
);
3784 ASSERT(pp
->p_lckcnt
!= 0);
3786 if (pp
->p_lckcnt
== 1) {
3787 if (pp
->p_cowcnt
< (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3790 if (++pp
->p_cowcnt
== (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3792 "COW lock limit reached on pfn 0x%lx",
3797 mutex_enter(&freemem_lock
);
3798 if ((availrmem
> pages_pp_maximum
) &&
3799 (pp
->p_cowcnt
< (ushort_t
)PAGE_LOCK_MAXIMUM
)) {
3802 mutex_exit(&freemem_lock
);
3805 if (++pp
->p_cowcnt
== (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3807 "COW lock limit reached on pfn 0x%lx",
3811 mutex_exit(&freemem_lock
);
3813 page_struct_unlock(pp
);
3818 page_subclaim(page_t
*pp
)
3822 ASSERT(PAGE_LOCKED(pp
));
3824 page_struct_lock(pp
);
3825 ASSERT(pp
->p_cowcnt
!= 0);
3828 if (pp
->p_lckcnt
< (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3833 mutex_enter(&freemem_lock
);
3836 mutex_exit(&freemem_lock
);
3840 if (++pp
->p_lckcnt
== (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3842 "Page lock limit reached on pfn 0x%lx",
3851 page_struct_unlock(pp
);
3856 * Variant of page_addclaim(), where ppa[] contains the pages of a single large
3860 page_addclaim_pages(page_t
**ppa
)
3862 pgcnt_t lckpgs
= 0, pg_idx
;
3864 VM_STAT_ADD(pagecnt
.pc_addclaim_pages
);
3867 * Only need to take the page struct lock on the large page root.
3869 page_struct_lock(ppa
[0]);
3870 for (pg_idx
= 0; ppa
[pg_idx
] != NULL
; pg_idx
++) {
3872 ASSERT(PAGE_LOCKED(ppa
[pg_idx
]));
3873 ASSERT(ppa
[pg_idx
]->p_lckcnt
!= 0);
3874 if (ppa
[pg_idx
]->p_cowcnt
== (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3875 page_struct_unlock(ppa
[0]);
3878 if (ppa
[pg_idx
]->p_lckcnt
> 1)
3883 mutex_enter(&freemem_lock
);
3884 if (availrmem
>= pages_pp_maximum
+ lckpgs
) {
3885 availrmem
-= lckpgs
;
3886 pages_claimed
+= lckpgs
;
3888 mutex_exit(&freemem_lock
);
3889 page_struct_unlock(ppa
[0]);
3892 mutex_exit(&freemem_lock
);
3895 for (pg_idx
= 0; ppa
[pg_idx
] != NULL
; pg_idx
++) {
3896 ppa
[pg_idx
]->p_lckcnt
--;
3897 ppa
[pg_idx
]->p_cowcnt
++;
3899 page_struct_unlock(ppa
[0]);
3904 * Variant of page_subclaim(), where ppa[] contains the pages of a single large
3908 page_subclaim_pages(page_t
**ppa
)
3910 pgcnt_t ulckpgs
= 0, pg_idx
;
3912 VM_STAT_ADD(pagecnt
.pc_subclaim_pages
);
3915 * Only need to take the page struct lock on the large page root.
3917 page_struct_lock(ppa
[0]);
3918 for (pg_idx
= 0; ppa
[pg_idx
] != NULL
; pg_idx
++) {
3920 ASSERT(PAGE_LOCKED(ppa
[pg_idx
]));
3921 ASSERT(ppa
[pg_idx
]->p_cowcnt
!= 0);
3922 if (ppa
[pg_idx
]->p_lckcnt
== (ushort_t
)PAGE_LOCK_MAXIMUM
) {
3923 page_struct_unlock(ppa
[0]);
3926 if (ppa
[pg_idx
]->p_lckcnt
!= 0)
3931 mutex_enter(&freemem_lock
);
3932 availrmem
+= ulckpgs
;
3933 pages_claimed
-= ulckpgs
;
3934 mutex_exit(&freemem_lock
);
3937 for (pg_idx
= 0; ppa
[pg_idx
] != NULL
; pg_idx
++) {
3938 ppa
[pg_idx
]->p_cowcnt
--;
3939 ppa
[pg_idx
]->p_lckcnt
++;
3942 page_struct_unlock(ppa
[0]);
3947 page_numtopp(pfn_t pfnum
, se_t se
)
3952 pp
= page_numtopp_nolock(pfnum
);
3958 * Acquire the appropriate lock on the page.
3960 while (!page_lock(pp
, se
, NULL
, P_RECLAIM
)) {
3961 if (page_pptonum(pp
) != pfnum
)
3966 if (page_pptonum(pp
) != pfnum
) {
3975 page_numtopp_noreclaim(pfn_t pfnum
, se_t se
)
3980 pp
= page_numtopp_nolock(pfnum
);
3986 * Acquire the appropriate lock on the page.
3988 while (!page_lock(pp
, se
, NULL
, P_NO_RECLAIM
)) {
3989 if (page_pptonum(pp
) != pfnum
)
3994 if (page_pptonum(pp
) != pfnum
) {
4003 * This routine is like page_numtopp, but will only return page structs
4004 * for pages which are ok for loading into hardware using the page struct.
4007 page_numtopp_nowait(pfn_t pfnum
, se_t se
)
4012 pp
= page_numtopp_nolock(pfnum
);
4018 * Try to acquire the appropriate lock on the page.
4023 if (!page_trylock(pp
, se
))
4026 if (page_pptonum(pp
) != pfnum
) {
4030 if (PP_ISFREE(pp
)) {
4040 * Returns a count of dirty pages that are in the process
4041 * of being written out. If 'cleanit' is set, try to push the page.
4044 page_busy(int cleanit
)
4046 page_t
*page0
= page_first();
4048 pgcnt_t nppbusy
= 0;
4052 vnode_t
*vp
= pp
->p_vnode
;
4054 * A page is a candidate for syncing if it is:
4056 * (a) On neither the freelist nor the cachelist
4057 * (b) Hashed onto a vnode
4058 * (c) Not a kernel page
4060 * (e) Not part of a swapfile
4061 * (f) a page which belongs to a real vnode; eg has a non-null
4063 * (g) Backed by a filesystem which doesn't have a
4064 * stubbed-out sync operation
4066 if (!PP_ISFREE(pp
) && vp
!= NULL
&& !VN_ISKAS(vp
) &&
4067 hat_ismod(pp
) && !IS_SWAPVP(vp
) && vp
->v_vfsp
!= NULL
&&
4068 vfs_can_sync(vp
->v_vfsp
)) {
4073 if (!page_trylock(pp
, SE_EXCL
))
4076 if (PP_ISFREE(pp
) || vp
== NULL
|| IS_SWAPVP(vp
) ||
4077 pp
->p_lckcnt
!= 0 || pp
->p_cowcnt
!= 0 ||
4079 HAT_SYNC_DONTZERO
| HAT_SYNC_STOPON_MOD
) & P_MOD
)) {
4086 (void) fop_putpage(vp
, off
, PAGESIZE
,
4087 B_ASYNC
| B_FREE
, kcred
, NULL
);
4090 } while ((pp
= page_next(pp
)) != page0
);
4095 void page_invalidate_pages(void);
4098 * callback handler to vm sub-system
4100 * callers make sure no recursive entries to this func.
4104 callb_vm_cpr(void *arg
, int code
)
4106 if (code
== CB_CODE_CPR_CHKPT
)
4107 page_invalidate_pages();
4112 * Invalidate all pages of the system.
4113 * It shouldn't be called until all user page activities are all stopped.
4116 page_invalidate_pages()
4122 const int MAXRETRIES
= 4;
4125 * Flush dirty pages and destroy the clean ones.
4129 pp
= page0
= page_first();
4136 * skip the page if it has no vnode or the page associated
4137 * with the kernel vnode or prom allocated kernel mem.
4139 if ((vp
= pp
->p_vnode
) == NULL
|| VN_ISKAS(vp
))
4143 * skip the page which is already free invalidated.
4145 if (PP_ISFREE(pp
) && PP_ISAGED(pp
))
4149 * skip pages that are already locked or can't be "exclusively"
4150 * locked or are already free. After we lock the page, check
4151 * the free and age bits again to be sure it's not destroyed
4153 * To achieve max. parallelization, we use page_trylock instead
4154 * of page_lock so that we don't get block on individual pages
4155 * while we have thousands of other pages to process.
4157 if (!page_trylock(pp
, SE_EXCL
)) {
4160 } else if (PP_ISFREE(pp
)) {
4161 if (!PP_ISAGED(pp
)) {
4162 page_destroy_free(pp
);
4169 * Is this page involved in some I/O? shared?
4171 * The page_struct_lock need not be acquired to
4172 * examine these fields since the page has an
4175 if (pp
->p_lckcnt
!= 0 || pp
->p_cowcnt
!= 0) {
4180 if (vp
->v_type
== VCHR
) {
4181 panic("vp->v_type == VCHR");
4185 if (!page_try_demote_pages(pp
)) {
4191 * Check the modified bit. Leave the bits alone in hardware
4192 * (they will be modified if we do the putpage).
4194 mod
= (hat_pagesync(pp
, HAT_SYNC_DONTZERO
| HAT_SYNC_STOPON_MOD
)
4197 offset
= pp
->p_offset
;
4199 * Hold the vnode before releasing the page lock
4200 * to prevent it from being freed and re-used by
4201 * some other thread.
4206 * No error return is checked here. Callers such as
4207 * cpr deals with the dirty pages at the dump time
4208 * if this putpage fails.
4210 (void) fop_putpage(vp
, offset
, PAGESIZE
, B_INVAL
,
4214 VN_DISPOSE(pp
, B_INVAL
, 0, kcred
);
4216 } while ((pp
= page_next(pp
)) != page0
);
4217 if (nbusypages
&& retry
++ < MAXRETRIES
) {
4224 * Replace the page "old" with the page "new" on the page hash and vnode lists
4226 * the replacement must be done in place, ie the equivalent sequence:
4228 * vp = old->p_vnode;
4229 * off = old->p_offset;
4230 * page_do_hashout(old)
4231 * page_do_hashin(new, vp, off)
4233 * doesn't work, since
4234 * 1) if old is the only page on the vnode, the v_pagecache_list has a window
4235 * where it looks empty. This will break file system assumptions.
4237 * 2) pvn_vplist_dirty() can't deal with pages moving on the v_pagecache_list.
4240 page_do_relocate_hash(page_t
*new, page_t
*old
)
4243 vnode_t
*vp
= old
->p_vnode
;
4246 ASSERT(PAGE_EXCL(old
));
4247 ASSERT(PAGE_EXCL(new));
4249 ASSERT(MUTEX_HELD(page_vnode_mutex(vp
)));
4252 * update new and replace old with new on the page hash list
4254 new->p_vnode
= old
->p_vnode
;
4255 new->p_offset
= old
->p_offset
;
4257 avl_remove(&vp
->v_pagecache
, old
);
4258 avl_add(&vp
->v_pagecache
, new);
4260 if ((new->p_vnode
->v_flag
& VISSWAP
) != 0)
4264 * replace old with new on the vnode's page list
4266 list_insert_before(&vp
->v_pagecache_list
, old
, new);
4267 list_remove(&vp
->v_pagecache_list
, old
);
4270 * clear out the old page
4272 old
->p_vnode
= NULL
;
4274 old
->p_offset
= (uoff_t
)-1;
4275 page_clr_all_props(old
);
4278 * Wake up processes waiting for this page. The page's
4279 * identity has been changed, and is probably not the
4280 * desired page any longer.
4282 sep
= page_se_mutex(old
);
4284 old
->p_selock
&= ~SE_EWANTED
;
4285 if (CV_HAS_WAITERS(&old
->p_cv
))
4286 cv_broadcast(&old
->p_cv
);
4291 * This function moves the identity of page "pp_old" to page "pp_new".
4292 * Both pages must be locked on entry. "pp_new" is free, has no identity,
4293 * and need not be hashed out from anywhere.
4296 page_relocate_hash(page_t
*pp_new
, page_t
*pp_old
)
4298 vnode_t
*vp
= pp_old
->p_vnode
;
4299 uoff_t off
= pp_old
->p_offset
;
4304 ASSERT(PAGE_EXCL(pp_old
));
4305 ASSERT(PAGE_EXCL(pp_new
));
4307 ASSERT(pp_new
->p_vnode
== NULL
);
4309 mutex_enter(page_vnode_mutex(vp
));
4311 page_do_relocate_hash(pp_new
, pp_old
);
4312 pp_new
->p_fsdata
= pp_old
->p_fsdata
;
4313 pp_old
->p_fsdata
= 0;
4315 mutex_exit(page_vnode_mutex(vp
));
4318 * The page_struct_lock need not be acquired for lckcnt and
4319 * cowcnt since the page has an "exclusive" lock.
4321 ASSERT(pp_new
->p_lckcnt
== 0);
4322 ASSERT(pp_new
->p_cowcnt
== 0);
4323 pp_new
->p_lckcnt
= pp_old
->p_lckcnt
;
4324 pp_new
->p_cowcnt
= pp_old
->p_cowcnt
;
4325 pp_old
->p_lckcnt
= pp_old
->p_cowcnt
= 0;
4329 * Helper routine used to lock all remaining members of a
4330 * large page. The caller is responsible for passing in a locked
4331 * pp. If pp is a large page, then it succeeds in locking all the
4332 * remaining constituent pages or it returns with only the
4333 * original page locked.
4335 * Returns 1 on success, 0 on failure.
4337 * If success is returned this routine guarantees p_szc for all constituent
4338 * pages of a large page pp belongs to can't change. To achieve this we
4339 * recheck szc of pp after locking all constituent pages and retry if szc
4340 * changed (it could only decrease). Since hat_page_demote() needs an EXCL
4341 * lock on one of constituent pages it can't be running after all constituent
4342 * pages are locked. hat_page_demote() with a lock on a constituent page
4343 * outside of this large page (i.e. pp belonged to a larger large page) is
4344 * already done with all constituent pages of pp since the root's p_szc is
4345 * changed last. Therefore no need to synchronize with hat_page_demote() that
4346 * locked a constituent page outside of pp's current large page.
4349 uint32_t gpg_trylock_mtbf
= 0;
4353 group_page_trylock(page_t
*pp
, se_t se
)
4357 uint_t pszc
= pp
->p_szc
;
4360 if (gpg_trylock_mtbf
&& !(gethrtime() % gpg_trylock_mtbf
)) {
4365 if (pp
!= PP_GROUPLEADER(pp
, pszc
)) {
4370 ASSERT(PAGE_LOCKED_SE(pp
, se
));
4371 ASSERT(!PP_ISFREE(pp
));
4375 npgs
= page_get_pagecnt(pszc
);
4377 for (i
= 1; i
< npgs
; i
++, tpp
++) {
4378 if (!page_trylock(tpp
, se
)) {
4380 for (j
= 1; j
< i
; j
++, tpp
++) {
4386 if (pp
->p_szc
!= pszc
) {
4387 ASSERT(pp
->p_szc
< pszc
);
4388 ASSERT(pp
->p_vnode
!= NULL
&& !PP_ISKAS(pp
) &&
4389 !IS_SWAPFSVP(pp
->p_vnode
));
4391 for (i
= 1; i
< npgs
; i
++, tpp
++) {
4401 group_page_unlock(page_t
*pp
)
4406 ASSERT(PAGE_LOCKED(pp
));
4407 ASSERT(!PP_ISFREE(pp
));
4408 ASSERT(pp
== PP_PAGEROOT(pp
));
4409 npgs
= page_get_pagecnt(pp
->p_szc
);
4410 for (i
= 1, tpp
= pp
+ 1; i
< npgs
; i
++, tpp
++) {
4417 * 0 : on success and *nrelocp is number of relocated PAGESIZE pages
4418 * ERANGE : this is not a base page
4419 * EBUSY : failure to get locks on the page/pages
4420 * ENOMEM : failure to obtain replacement pages
4421 * EAGAIN : OBP has not yet completed its boot-time handoff to the kernel
4422 * EIO : An error occurred while trying to copy the page data
4424 * Return with all constituent members of target and replacement
4425 * SE_EXCL locked. It is the callers responsibility to drop the
4431 page_t
**replacement
,
4441 pfn_t pfn
, repl_pfn
;
4444 int repl_contig
= 0;
4446 spgcnt_t dofree
= 0;
4450 #if defined(__sparc)
4452 * We need to wait till OBP has completed
4453 * its boot-time handoff of its resources to the kernel
4454 * before we allow page relocation
4456 if (page_relocate_ready
== 0) {
4462 * If this is not a base page,
4463 * just return with 0x0 pages relocated.
4466 ASSERT(PAGE_EXCL(targ
));
4467 ASSERT(!PP_ISFREE(targ
));
4469 ASSERT(szc
< mmu_page_sizes
);
4470 VM_STAT_ADD(vmm_vmstats
.ppr_reloc
[szc
]);
4471 pfn
= targ
->p_pagenum
;
4472 if (pfn
!= PFN_BASE(pfn
, szc
)) {
4473 VM_STAT_ADD(vmm_vmstats
.ppr_relocnoroot
[szc
]);
4477 if ((repl
= *replacement
) != NULL
&& repl
->p_szc
>= szc
) {
4478 repl_pfn
= repl
->p_pagenum
;
4479 if (repl_pfn
!= PFN_BASE(repl_pfn
, szc
)) {
4480 VM_STAT_ADD(vmm_vmstats
.ppr_reloc_replnoroot
[szc
]);
4487 * We must lock all members of this large page or we cannot
4488 * relocate any part of it.
4490 if (grouplock
!= 0 && !group_page_trylock(targ
, SE_EXCL
)) {
4491 VM_STAT_ADD(vmm_vmstats
.ppr_relocnolock
[targ
->p_szc
]);
4496 * reread szc it could have been decreased before
4497 * group_page_trylock() was done.
4500 ASSERT(szc
< mmu_page_sizes
);
4501 VM_STAT_ADD(vmm_vmstats
.ppr_reloc
[szc
]);
4502 ASSERT(pfn
== PFN_BASE(pfn
, szc
));
4504 npgs
= page_get_pagecnt(targ
->p_szc
);
4507 dofree
= npgs
; /* Size of target page in MMU pages */
4508 if (!page_create_wait(dofree
, 0)) {
4509 if (grouplock
!= 0) {
4510 group_page_unlock(targ
);
4512 VM_STAT_ADD(vmm_vmstats
.ppr_relocnomem
[szc
]);
4517 * seg kmem pages require that the target and replacement
4518 * page be the same pagesize.
4520 flags
= (VN_ISKAS(targ
->p_vnode
)) ? PGR_SAMESZC
: 0;
4521 repl
= page_get_replacement_page(targ
, lgrp
, flags
);
4523 if (grouplock
!= 0) {
4524 group_page_unlock(targ
);
4526 page_create_putback(dofree
);
4527 VM_STAT_ADD(vmm_vmstats
.ppr_relocnomem
[szc
]);
4533 ASSERT(PAGE_LOCKED(repl
));
4537 #if defined(__sparc)
4539 * Let hat_page_relocate() complete the relocation if it's kernel page
4541 if (VN_ISKAS(targ
->p_vnode
)) {
4542 *replacement
= repl
;
4543 if (hat_page_relocate(target
, replacement
, nrelocp
) != 0) {
4544 if (grouplock
!= 0) {
4545 group_page_unlock(targ
);
4548 *replacement
= NULL
;
4549 page_free_replacement_page(repl
);
4550 page_create_putback(dofree
);
4552 VM_STAT_ADD(vmm_vmstats
.ppr_krelocfail
[szc
]);
4555 VM_STAT_ADD(vmm_vmstats
.ppr_relocok
[szc
]);
4562 for (i
= 0; i
< npgs
; i
++) {
4563 ASSERT(PAGE_EXCL(targ
));
4564 ASSERT(targ
->p_slckcnt
== 0);
4565 ASSERT(repl
->p_slckcnt
== 0);
4567 (void) hat_pageunload(targ
, HAT_FORCE_PGUNLOAD
);
4569 ASSERT(hat_page_getshare(targ
) == 0);
4570 ASSERT(!PP_ISFREE(targ
));
4571 ASSERT(targ
->p_pagenum
== (pfn
+ i
));
4572 ASSERT(repl_contig
== 0 ||
4573 repl
->p_pagenum
== (repl_pfn
+ i
));
4576 * Copy the page contents and attributes then
4577 * relocate the page in the page hash.
4579 if (ppcopy(targ
, repl
) == 0) {
4582 VM_STAT_ADD(vmm_vmstats
.ppr_copyfail
);
4583 if (grouplock
!= 0) {
4584 group_page_unlock(targ
);
4587 *replacement
= NULL
;
4588 page_free_replacement_page(repl
);
4589 page_create_putback(dofree
);
4595 if (repl_contig
!= 0) {
4598 repl
= repl
->p_next
;
4605 for (i
= 0; i
< npgs
; i
++) {
4606 ppattr
= hat_page_getattr(targ
, (P_MOD
| P_REF
| P_RO
));
4607 page_clr_all_props(repl
);
4608 page_set_props(repl
, ppattr
);
4609 page_relocate_hash(repl
, targ
);
4611 ASSERT(hat_page_getshare(targ
) == 0);
4612 ASSERT(hat_page_getshare(repl
) == 0);
4614 * Now clear the props on targ, after the
4615 * page_relocate_hash(), they no longer
4618 page_clr_all_props(targ
);
4619 ASSERT(targ
->p_next
== targ
);
4620 ASSERT(targ
->p_prev
== targ
);
4621 page_list_concat(&pl
, &targ
);
4624 if (repl_contig
!= 0) {
4627 repl
= repl
->p_next
;
4630 /* assert that we have come full circle with repl */
4631 ASSERT(repl_contig
== 1 || first_repl
== repl
);
4634 if (*replacement
== NULL
) {
4635 ASSERT(first_repl
== repl
);
4636 *replacement
= repl
;
4638 VM_STAT_ADD(vmm_vmstats
.ppr_relocok
[szc
]);
4643 * On success returns 0 and *nrelocp the number of PAGESIZE pages relocated.
4648 page_t
**replacement
,
4656 /* do_page_relocate returns 0 on success or errno value */
4657 ret
= do_page_relocate(target
, replacement
, grouplock
, nrelocp
, lgrp
);
4659 if (ret
!= 0 || freetarget
== 0) {
4662 if (*nrelocp
== 1) {
4663 ASSERT(*target
!= NULL
);
4664 page_free(*target
, 1);
4666 page_t
*tpp
= *target
;
4667 uint_t szc
= tpp
->p_szc
;
4668 pgcnt_t npgs
= page_get_pagecnt(szc
);
4672 ASSERT(PAGE_EXCL(tpp
));
4673 ASSERT(!hat_page_is_mapped(tpp
));
4674 ASSERT(tpp
->p_szc
== szc
);
4678 } while ((tpp
= tpp
->p_next
) != *target
);
4680 page_list_add_pages(*target
, 0);
4681 npgs
= page_get_pagecnt(szc
);
4682 page_create_putback(npgs
);
4688 * it is up to the caller to deal with pcf accounting.
4691 page_free_replacement_page(page_t
*pplist
)
4695 while (pplist
!= NULL
) {
4697 * pp_targ is a linked list.
4700 if (pp
->p_szc
== 0) {
4701 page_sub(&pplist
, pp
);
4702 page_clr_all_props(pp
);
4705 page_list_add(pp
, PG_FREE_LIST
| PG_LIST_TAIL
);
4707 VM_STAT_ADD(pagecnt
.pc_free_replacement_page
[0]);
4709 spgcnt_t curnpgs
= page_get_pagecnt(pp
->p_szc
);
4711 page_list_break(&pp
, &pplist
, curnpgs
);
4714 ASSERT(PAGE_EXCL(tpp
));
4715 ASSERT(!hat_page_is_mapped(tpp
));
4716 page_clr_all_props(tpp
);
4719 } while ((tpp
= tpp
->p_next
) != pp
);
4720 page_list_add_pages(pp
, 0);
4721 VM_STAT_ADD(pagecnt
.pc_free_replacement_page
[1]);
4727 * Relocate target to non-relocatable replacement page.
4730 page_relocate_cage(page_t
**target
, page_t
**replacement
)
4733 spgcnt_t pgcnt
, npgs
;
4738 ASSERT(PAGE_EXCL(tpp
));
4739 ASSERT(tpp
->p_szc
== 0);
4741 pgcnt
= btop(page_get_pagesize(tpp
->p_szc
));
4744 (void) page_create_wait(pgcnt
, PG_WAIT
| PG_NORELOC
);
4745 rpp
= page_get_replacement_page(tpp
, NULL
, PGR_NORELOC
);
4747 page_create_putback(pgcnt
);
4748 kcage_cageout_wakeup();
4750 } while (rpp
== NULL
);
4752 ASSERT(PP_ISNORELOC(rpp
));
4754 result
= page_relocate(&tpp
, &rpp
, 0, 1, &npgs
, NULL
);
4759 panic("page_relocate_cage: partial relocation");
4766 * Release the page lock on a page, place on cachelist
4767 * tail if no longer mapped. Caller can let us know if
4768 * the page is known to be clean.
4771 page_release(page_t
*pp
, int checkmod
)
4775 ASSERT(PAGE_LOCKED(pp
) && !PP_ISFREE(pp
) &&
4776 (pp
->p_vnode
!= NULL
));
4778 if (!hat_page_is_mapped(pp
) && !IS_SWAPVP(pp
->p_vnode
) &&
4779 ((PAGE_SHARED(pp
) && page_tryupgrade(pp
)) || PAGE_EXCL(pp
)) &&
4780 pp
->p_lckcnt
== 0 && pp
->p_cowcnt
== 0 &&
4781 !hat_page_is_mapped(pp
)) {
4784 * If page is modified, unlock it
4786 * (p_nrm & P_MOD) bit has the latest stuff because:
4787 * (1) We found that this page doesn't have any mappings
4788 * _after_ holding SE_EXCL and
4789 * (2) We didn't drop SE_EXCL lock after the check in (1)
4791 if (checkmod
&& hat_ismod(pp
)) {
4795 VN_DISPOSE(pp
, B_FREE
, 0, kcred
);
4796 status
= PGREL_CLEAN
;
4800 status
= PGREL_NOTREL
;
4806 * Given a constituent page, try to demote the large page on the freelist.
4808 * Returns nonzero if the page could be demoted successfully. Returns with
4809 * the constituent page still locked.
4812 page_try_demote_free_pages(page_t
*pp
)
4814 page_t
*rootpp
= pp
;
4815 pfn_t pfn
= page_pptonum(pp
);
4817 uint_t szc
= pp
->p_szc
;
4819 ASSERT(PP_ISFREE(pp
));
4820 ASSERT(PAGE_EXCL(pp
));
4823 * Adjust rootpp and lock it, if `pp' is not the base
4826 npgs
= page_get_pagecnt(pp
->p_szc
);
4831 if (!IS_P2ALIGNED(pfn
, npgs
)) {
4832 pfn
= P2ALIGN(pfn
, npgs
);
4833 rootpp
= page_numtopp_nolock(pfn
);
4836 if (pp
!= rootpp
&& !page_trylock(rootpp
, SE_EXCL
)) {
4840 if (rootpp
->p_szc
!= szc
) {
4842 page_unlock(rootpp
);
4846 page_demote_free_pages(rootpp
);
4849 page_unlock(rootpp
);
4851 ASSERT(PP_ISFREE(pp
));
4852 ASSERT(PAGE_EXCL(pp
));
4857 * Given a constituent page, try to demote the large page.
4859 * Returns nonzero if the page could be demoted successfully. Returns with
4860 * the constituent page still locked.
4863 page_try_demote_pages(page_t
*pp
)
4865 page_t
*tpp
, *rootpp
= pp
;
4866 pfn_t pfn
= page_pptonum(pp
);
4868 uint_t szc
= pp
->p_szc
;
4869 vnode_t
*vp
= pp
->p_vnode
;
4871 ASSERT(PAGE_EXCL(pp
));
4873 VM_STAT_ADD(pagecnt
.pc_try_demote_pages
[0]);
4875 if (pp
->p_szc
== 0) {
4876 VM_STAT_ADD(pagecnt
.pc_try_demote_pages
[1]);
4880 if (vp
!= NULL
&& !IS_SWAPFSVP(vp
) && !VN_ISKAS(vp
)) {
4881 VM_STAT_ADD(pagecnt
.pc_try_demote_pages
[2]);
4882 page_demote_vp_pages(pp
);
4883 ASSERT(pp
->p_szc
== 0);
4888 * Adjust rootpp if passed in is not the base
4891 npgs
= page_get_pagecnt(pp
->p_szc
);
4893 if (!IS_P2ALIGNED(pfn
, npgs
)) {
4894 pfn
= P2ALIGN(pfn
, npgs
);
4895 rootpp
= page_numtopp_nolock(pfn
);
4896 VM_STAT_ADD(pagecnt
.pc_try_demote_pages
[3]);
4897 ASSERT(rootpp
->p_vnode
!= NULL
);
4898 ASSERT(rootpp
->p_szc
== szc
);
4902 * We can't demote kernel pages since we can't hat_unload()
4905 if (VN_ISKAS(rootpp
->p_vnode
))
4909 * Attempt to lock all constituent pages except the page passed
4910 * in since it's already locked.
4912 for (tpp
= rootpp
, i
= 0; i
< npgs
; i
++, tpp
++) {
4913 ASSERT(!PP_ISFREE(tpp
));
4914 ASSERT(tpp
->p_vnode
!= NULL
);
4916 if (tpp
!= pp
&& !page_trylock(tpp
, SE_EXCL
))
4918 ASSERT(tpp
->p_szc
== rootpp
->p_szc
);
4919 ASSERT(page_pptonum(tpp
) == page_pptonum(rootpp
) + i
);
4923 * If we failed to lock them all then unlock what we have
4924 * locked so far and bail.
4933 VM_STAT_ADD(pagecnt
.pc_try_demote_pages
[4]);
4937 for (tpp
= rootpp
, i
= 0; i
< npgs
; i
++, tpp
++) {
4938 ASSERT(PAGE_EXCL(tpp
));
4939 ASSERT(tpp
->p_slckcnt
== 0);
4940 (void) hat_pageunload(tpp
, HAT_FORCE_PGUNLOAD
);
4945 * Unlock all pages except the page passed in.
4947 for (tpp
= rootpp
, i
= 0; i
< npgs
; i
++, tpp
++) {
4948 ASSERT(!hat_page_is_mapped(tpp
));
4953 VM_STAT_ADD(pagecnt
.pc_try_demote_pages
[5]);
4958 * Called by page_free() and page_destroy() to demote the page size code
4959 * (p_szc) to 0 (since we can't just put a single PAGESIZE page with non zero
4960 * p_szc on free list, neither can we just clear p_szc of a single page_t
4961 * within a large page since it will break other code that relies on p_szc
4962 * being the same for all page_t's of a large page). Anonymous pages should
4963 * never end up here because anon_map_getpages() cannot deal with p_szc
4964 * changes after a single constituent page is locked. While anonymous or
4965 * kernel large pages are demoted or freed the entire large page at a time
4966 * with all constituent pages locked EXCL for the file system pages we
4967 * have to be able to demote a large page (i.e. decrease all constituent pages
4968 * p_szc) with only just an EXCL lock on one of constituent pages. The reason
4969 * we can easily deal with anonymous page demotion the entire large page at a
4970 * time is that those operation originate at address space level and concern
4971 * the entire large page region with actual demotion only done when pages are
4972 * not shared with any other processes (therefore we can always get EXCL lock
4973 * on all anonymous constituent pages after clearing segment page
4974 * cache). However file system pages can be truncated or invalidated at a
4975 * PAGESIZE level from the file system side and end up in page_free() or
4976 * page_destroy() (we also allow only part of the large page to be SOFTLOCKed
4977 * and therefore pageout should be able to demote a large page by EXCL locking
4978 * any constituent page that is not under SOFTLOCK). In those cases we cannot
4979 * rely on being able to lock EXCL all constituent pages.
4981 * To prevent szc changes on file system pages one has to lock all constituent
4982 * pages at least SHARED (or call page_szc_lock()). The only subsystem that
4983 * doesn't rely on locking all constituent pages (or using page_szc_lock()) to
4984 * prevent szc changes is hat layer that uses its own page level mlist
4985 * locks. hat assumes that szc doesn't change after mlist lock for a page is
4986 * taken. Therefore we need to change szc under hat level locks if we only
4987 * have an EXCL lock on a single constituent page and hat still references any
4988 * of constituent pages. (Note we can't "ignore" hat layer by simply
4989 * hat_pageunload() all constituent pages without having EXCL locks on all of
4990 * constituent pages). We use hat_page_demote() call to safely demote szc of
4991 * all constituent pages under hat locks when we only have an EXCL lock on one
4992 * of constituent pages.
4994 * This routine calls page_szc_lock() before calling hat_page_demote() to
4995 * allow segvn in one special case not to lock all constituent pages SHARED
4996 * before calling hat_memload_array() that relies on p_szc not changing even
4997 * before hat level mlist lock is taken. In that case segvn uses
4998 * page_szc_lock() to prevent hat_page_demote() changing p_szc values.
5000 * Anonymous or kernel page demotion still has to lock all pages exclusively
5001 * and do hat_pageunload() on all constituent pages before demoting the page
5002 * therefore there's no need for anonymous or kernel page demotion to use
5003 * hat_page_demote() mechanism.
5005 * hat_page_demote() removes all large mappings that map pp and then decreases
5006 * p_szc starting from the last constituent page of the large page. By working
5007 * from the tail of a large page in pfn decreasing order allows one looking at
5008 * the root page to know that hat_page_demote() is done for root's szc area.
5009 * e.g. if a root page has szc 1 one knows it only has to lock all constituent
5010 * pages within szc 1 area to prevent szc changes because hat_page_demote()
5011 * that started on this page when it had szc > 1 is done for this szc 1 area.
5013 * We are guaranteed that all constituent pages of pp's large page belong to
5014 * the same vnode with the consecutive offsets increasing in the direction of
5015 * the pfn i.e. the identity of constituent pages can't change until their
5016 * p_szc is decreased. Therefore it's safe for hat_page_demote() to remove
5017 * large mappings to pp even though we don't lock any constituent page except
5018 * pp (i.e. we won't unload e.g. kernel locked page).
5021 page_demote_vp_pages(page_t
*pp
)
5025 ASSERT(PAGE_EXCL(pp
));
5026 ASSERT(!PP_ISFREE(pp
));
5027 ASSERT(pp
->p_vnode
!= NULL
);
5028 ASSERT(!IS_SWAPFSVP(pp
->p_vnode
));
5029 ASSERT(!PP_ISKAS(pp
));
5031 VM_STAT_ADD(pagecnt
.pc_demote_pages
[0]);
5033 mtx
= page_szc_lock(pp
);
5035 hat_page_demote(pp
);
5038 ASSERT(pp
->p_szc
== 0);
5042 * Mark any existing pages for migration in the given range
5045 page_mark_migrate(struct seg
*seg
, caddr_t addr
, size_t len
,
5046 struct anon_map
*amp
, ulong_t anon_index
, vnode_t
*vp
,
5047 uoff_t vnoff
, int rflag
)
5062 anon_sync_obj_t cookie
;
5064 ASSERT(seg
->s_as
&& AS_LOCK_HELD(seg
->s_as
));
5067 * Don't do anything if don't need to do lgroup optimizations
5070 if (!lgrp_optimizations())
5074 * Align address and length to (potentially large) page boundary
5076 segpgsz
= page_get_pagesize(seg
->s_szc
);
5077 addr
= (caddr_t
)P2ALIGN((uintptr_t)addr
, segpgsz
);
5079 len
= P2ROUNDUP(len
, segpgsz
);
5082 * Do one (large) page at a time
5085 while (va
< addr
+ len
) {
5087 * Lookup (root) page for vnode and offset corresponding to
5088 * this virtual address
5089 * Try anonmap first since there may be copy-on-write
5090 * pages, but initialize vnode pointer and offset using
5091 * vnode arguments just in case there isn't an amp.
5094 off
= vnoff
+ va
- seg
->s_base
;
5096 ANON_LOCK_ENTER(&
->a_rwlock
, RW_READER
);
5097 an_idx
= anon_index
+ seg_page(seg
, va
);
5098 anon_array_enter(amp
, an_idx
, &cookie
);
5099 ap
= anon_get_ptr(amp
->ahp
, an_idx
);
5101 swap_xlate(ap
, &curvp
, &off
);
5102 anon_array_exit(&cookie
);
5103 ANON_LOCK_EXIT(&
->a_rwlock
);
5108 pp
= page_lookup(curvp
, off
, SE_SHARED
);
5111 * If there isn't a page at this virtual address,
5120 * Figure out which lgroup this page is in for kstats
5122 pfn
= page_pptonum(pp
);
5123 from
= lgrp_pfn_to_lgrp(pfn
);
5126 * Get page size, and round up and skip to next page boundary
5127 * if unaligned address
5130 pgsz
= page_get_pagesize(pszc
);
5132 if (!IS_P2ALIGNED(va
, pgsz
) ||
5133 !IS_P2ALIGNED(pfn
, pages
) ||
5135 pgsz
= MIN(pgsz
, segpgsz
);
5137 pages
= btop(P2END((uintptr_t)va
, pgsz
) -
5139 va
= (caddr_t
)P2END((uintptr_t)va
, pgsz
);
5140 lgrp_stat_add(from
->lgrp_id
, LGRP_PMM_FAIL_PGS
, pages
);
5145 * Upgrade to exclusive lock on page
5147 if (!page_tryupgrade(pp
)) {
5150 lgrp_stat_add(from
->lgrp_id
, LGRP_PMM_FAIL_PGS
,
5159 * Lock constituent pages if this is large page
5163 * Lock all constituents except root page, since it
5164 * should be locked already.
5166 for (; nlocked
< pages
; nlocked
++) {
5167 if (!page_trylock(pp
, SE_EXCL
)) {
5170 if (PP_ISFREE(pp
) ||
5171 pp
->p_szc
!= pszc
) {
5173 * hat_page_demote() raced in with us.
5175 ASSERT(!IS_SWAPFSVP(curvp
));
5184 * If all constituent pages couldn't be locked,
5185 * unlock pages locked so far and skip to next page.
5187 if (nlocked
< pages
) {
5192 lgrp_stat_add(from
->lgrp_id
, LGRP_PMM_FAIL_PGS
,
5198 * hat_page_demote() can no longer happen
5199 * since last cons page had the right p_szc after
5200 * all cons pages were locked. all cons pages
5201 * should now have the same p_szc.
5205 * All constituent pages locked successfully, so mark
5206 * large page for migration and unload the mappings of
5207 * constituent pages, so a fault will occur on any part of the
5212 (void) hat_pageunload(pp0
, HAT_FORCE_PGUNLOAD
);
5213 ASSERT(hat_page_getshare(pp0
) == 0);
5216 lgrp_stat_add(from
->lgrp_id
, LGRP_PMM_PGS
, nlocked
);
5223 * Migrate any pages that have been marked for migration in the given range
5242 ASSERT(seg
->s_as
&& AS_LOCK_HELD(seg
->s_as
));
5244 while (npages
> 0) {
5247 pgsz
= page_get_pagesize(pszc
);
5248 page_cnt
= btop(pgsz
);
5251 * Check to see whether this page is marked for migration
5253 * Assume that root page of large page is marked for
5254 * migration and none of the other constituent pages
5255 * are marked. This really simplifies clearing the
5256 * migrate bit by not having to clear it from each
5259 * note we don't want to relocate an entire large page if
5260 * someone is only using one subpage.
5262 if (npages
< page_cnt
)
5266 * Is it marked for migration?
5268 if (!PP_ISMIGRATE(pp
))
5272 * Determine lgroups that page is being migrated between
5274 pfn
= page_pptonum(pp
);
5275 if (!IS_P2ALIGNED(pfn
, page_cnt
)) {
5278 from
= lgrp_pfn_to_lgrp(pfn
);
5279 to
= lgrp_mem_choose(seg
, addr
, pgsz
);
5282 * Need to get exclusive lock's to migrate
5284 for (i
= 0; i
< page_cnt
; i
++) {
5285 ASSERT(PAGE_LOCKED(ppa
[i
]));
5286 if (page_pptonum(ppa
[i
]) != pfn
+ i
||
5287 ppa
[i
]->p_szc
!= pszc
) {
5290 if (!page_tryupgrade(ppa
[i
])) {
5291 lgrp_stat_add(from
->lgrp_id
,
5292 LGRP_PM_FAIL_LOCK_PGS
,
5298 * Check to see whether we are trying to migrate
5299 * page to lgroup where it is allocated already.
5300 * If so, clear the migrate bit and skip to next
5303 if (i
== 0 && to
== from
) {
5304 PP_CLRMIGRATE(ppa
[0]);
5305 page_downgrade(ppa
[0]);
5311 * If all constituent pages couldn't be locked,
5312 * unlock pages locked so far and skip to next page.
5314 if (i
!= page_cnt
) {
5316 page_downgrade(ppa
[i
]);
5321 (void) page_create_wait(page_cnt
, PG_WAIT
);
5322 newpp
= page_get_replacement_page(pp
, to
, PGR_SAMESZC
);
5323 if (newpp
== NULL
) {
5324 page_create_putback(page_cnt
);
5325 for (i
= 0; i
< page_cnt
; i
++) {
5326 page_downgrade(ppa
[i
]);
5328 lgrp_stat_add(to
->lgrp_id
, LGRP_PM_FAIL_ALLOC_PGS
,
5332 ASSERT(newpp
->p_szc
== pszc
);
5334 * Clear migrate bit and relocate page
5337 if (page_relocate(&pp
, &newpp
, 0, 1, &page_cnt
, to
)) {
5338 panic("page_migrate: page_relocate failed");
5340 ASSERT(page_cnt
* PAGESIZE
== pgsz
);
5343 * Keep stats for number of pages migrated from and to
5346 lgrp_stat_add(from
->lgrp_id
, LGRP_PM_SRC_PGS
, page_cnt
);
5347 lgrp_stat_add(to
->lgrp_id
, LGRP_PM_DEST_PGS
, page_cnt
);
5349 * update the page_t array we were passed in and
5350 * unlink constituent pages of a large page.
5352 for (i
= 0; i
< page_cnt
; ++i
, ++pp
) {
5353 ASSERT(PAGE_EXCL(newpp
));
5354 ASSERT(newpp
->p_szc
== pszc
);
5357 page_sub(&newpp
, pp
);
5360 ASSERT(newpp
== NULL
);
5368 uint_t page_reclaim_maxcnt
= 60; /* max total iterations */
5369 uint_t page_reclaim_nofree_maxcnt
= 3; /* max iterations without progress */
5371 * Reclaim/reserve availrmem for npages.
5372 * If there is not enough memory start reaping seg, kmem caches.
5373 * Start pageout scanner (via page_needfree()).
5374 * Exit after ~ MAX_CNT s regardless of how much memory has been released.
5375 * Note: There is no guarantee that any availrmem will be freed as
5376 * this memory typically is locked (kernel heap) or reserved for swap.
5377 * Also due to memory fragmentation kmem allocator may not be able
5378 * to free any memory (single user allocated buffer will prevent
5379 * freeing slab or a page).
5382 page_reclaim_mem(pgcnt_t npages
, pgcnt_t epages
, int adjust
)
5388 pgcnt_t old_availrmem
= 0;
5390 mutex_enter(&freemem_lock
);
5391 while (availrmem
< tune
.t_minarmem
+ npages
+ epages
&&
5392 i
++ < page_reclaim_maxcnt
) {
5393 /* ensure we made some progress in the last few iterations */
5394 if (old_availrmem
< availrmem
) {
5395 old_availrmem
= availrmem
;
5397 } else if (i_nofree
++ >= page_reclaim_nofree_maxcnt
) {
5401 deficit
= tune
.t_minarmem
+ npages
+ epages
- availrmem
;
5402 mutex_exit(&freemem_lock
);
5403 page_needfree(deficit
);
5406 page_needfree(-(spgcnt_t
)deficit
);
5407 mutex_enter(&freemem_lock
);
5410 if (adjust
&& (availrmem
>= tune
.t_minarmem
+ npages
+ epages
)) {
5411 availrmem
-= npages
;
5415 mutex_exit(&freemem_lock
);
5421 * Search the memory segments to locate the desired page. Within a
5422 * segment, pages increase linearly with one page structure per
5423 * physical page frame (size PAGESIZE). The search begins
5424 * with the segment that was accessed last, to take advantage of locality.
5425 * If the hint misses, we start from the beginning of the sorted memseg list
5430 * Some data structures for pfn to pp lookup.
5432 ulong_t mhash_per_slot
;
5433 struct memseg
*memseg_hash
[N_MEM_SLOTS
];
5436 page_numtopp_nolock(pfn_t pfnum
)
5443 * We need to disable kernel preemption while referencing the
5444 * cpu_vm_data field in order to prevent us from being switched to
5445 * another cpu and trying to reference it after it has been freed.
5446 * This will keep us on cpu and prevent it from being removed while
5447 * we are still on it.
5449 * We may be caching a memseg in vc_pnum_memseg/vc_pnext_memseg
5450 * which is being resued by DR who will flush those references
5451 * before modifying the reused memseg. See memseg_cpu_vm_flush().
5454 vc
= CPU
->cpu_vm_data
;
5457 MEMSEG_STAT_INCR(nsearch
);
5459 /* Try last winner first */
5460 if (((seg
= vc
->vc_pnum_memseg
) != NULL
) &&
5461 (pfnum
>= seg
->pages_base
) && (pfnum
< seg
->pages_end
)) {
5462 MEMSEG_STAT_INCR(nlastwon
);
5463 pp
= seg
->pages
+ (pfnum
- seg
->pages_base
);
5464 if (pp
->p_pagenum
== pfnum
) {
5466 return ((page_t
*)pp
);
5471 if (((seg
= memseg_hash
[MEMSEG_PFN_HASH(pfnum
)]) != NULL
) &&
5472 (pfnum
>= seg
->pages_base
) && (pfnum
< seg
->pages_end
)) {
5473 MEMSEG_STAT_INCR(nhashwon
);
5474 vc
->vc_pnum_memseg
= seg
;
5475 pp
= seg
->pages
+ (pfnum
- seg
->pages_base
);
5476 if (pp
->p_pagenum
== pfnum
) {
5478 return ((page_t
*)pp
);
5482 /* Else Brute force */
5483 for (seg
= memsegs
; seg
!= NULL
; seg
= seg
->next
) {
5484 if (pfnum
>= seg
->pages_base
&& pfnum
< seg
->pages_end
) {
5485 vc
->vc_pnum_memseg
= seg
;
5486 pp
= seg
->pages
+ (pfnum
- seg
->pages_base
);
5487 if (pp
->p_pagenum
== pfnum
) {
5489 return ((page_t
*)pp
);
5493 vc
->vc_pnum_memseg
= NULL
;
5495 MEMSEG_STAT_INCR(nnotfound
);
5501 page_numtomemseg_nolock(pfn_t pfnum
)
5507 * We may be caching a memseg in vc_pnum_memseg/vc_pnext_memseg
5508 * which is being resued by DR who will flush those references
5509 * before modifying the reused memseg. See memseg_cpu_vm_flush().
5513 if (((seg
= memseg_hash
[MEMSEG_PFN_HASH(pfnum
)]) != NULL
) &&
5514 (pfnum
>= seg
->pages_base
) && (pfnum
< seg
->pages_end
)) {
5515 pp
= seg
->pages
+ (pfnum
- seg
->pages_base
);
5516 if (pp
->p_pagenum
== pfnum
) {
5522 /* Else Brute force */
5523 for (seg
= memsegs
; seg
!= NULL
; seg
= seg
->next
) {
5524 if (pfnum
>= seg
->pages_base
&& pfnum
< seg
->pages_end
) {
5525 pp
= seg
->pages
+ (pfnum
- seg
->pages_base
);
5526 if (pp
->p_pagenum
== pfnum
) {
5537 * Given a page and a count return the page struct that is
5538 * n structs away from the current one in the global page
5541 * This function wraps to the first page upon
5542 * reaching the end of the memseg list.
5545 page_nextn(page_t
*pp
, ulong_t n
)
5552 * We need to disable kernel preemption while referencing the
5553 * cpu_vm_data field in order to prevent us from being switched to
5554 * another cpu and trying to reference it after it has been freed.
5555 * This will keep us on cpu and prevent it from being removed while
5556 * we are still on it.
5558 * We may be caching a memseg in vc_pnum_memseg/vc_pnext_memseg
5559 * which is being resued by DR who will flush those references
5560 * before modifying the reused memseg. See memseg_cpu_vm_flush().
5563 vc
= (vm_cpu_data_t
*)CPU
->cpu_vm_data
;
5567 if (((seg
= vc
->vc_pnext_memseg
) == NULL
) ||
5568 (seg
->pages_base
== seg
->pages_end
) ||
5569 !(pp
>= seg
->pages
&& pp
< seg
->epages
)) {
5571 for (seg
= memsegs
; seg
; seg
= seg
->next
) {
5572 if (pp
>= seg
->pages
&& pp
< seg
->epages
)
5577 /* Memory delete got in, return something valid. */
5584 /* check for wraparound - possible if n is large */
5585 while ((ppn
= (pp
+ n
)) >= seg
->epages
|| ppn
< pp
) {
5586 n
-= seg
->epages
- pp
;
5592 vc
->vc_pnext_memseg
= seg
;
5598 * Initialize for a loop using page_next_scan_large().
5601 page_next_scan_init(void **cookie
)
5603 ASSERT(cookie
!= NULL
);
5604 *cookie
= (void *)memsegs
;
5605 return ((page_t
*)memsegs
->pages
);
5609 * Return the next page in a scan of page_t's, assuming we want
5610 * to skip over sub-pages within larger page sizes.
5612 * The cookie is used to keep track of the current memseg.
5615 page_next_scan_large(
5620 struct memseg
*seg
= (struct memseg
*)*cookie
;
5627 * get the count of page_t's to skip based on the page size
5630 if (pp
->p_szc
== 0) {
5633 pfn
= page_pptonum(pp
);
5634 cnt
= page_get_pagecnt(pp
->p_szc
);
5635 cnt
-= pfn
& (cnt
- 1);
5641 * Catch if we went past the end of the current memory segment. If so,
5642 * just move to the next segment with pages.
5644 if (new_pp
>= seg
->epages
|| seg
->pages_base
== seg
->pages_end
) {
5649 } while (seg
->pages_base
== seg
->pages_end
);
5650 new_pp
= seg
->pages
;
5651 *cookie
= (void *)seg
;
5659 * Returns next page in list. Note: this function wraps
5660 * to the first page in the list upon reaching the end
5661 * of the list. Callers should be aware of this fact.
5664 /* We should change this be a #define */
5667 page_next(page_t
*pp
)
5669 return (page_nextn(pp
, 1));
5675 return ((page_t
*)memsegs
->pages
);
5680 * This routine is called at boot with the initial memory configuration
5681 * and when memory is added or removed.
5688 struct memseg
*pseg
;
5692 * Clear memseg_hash array.
5693 * Since memory add/delete is designed to operate concurrently
5694 * with normal operation, the hash rebuild must be able to run
5695 * concurrently with page_numtopp_nolock(). To support this
5696 * functionality, assignments to memseg_hash array members must
5697 * be done atomically.
5699 * NOTE: bzero() does not currently guarantee this for kernel
5700 * threads, and cannot be used here.
5702 for (i
= 0; i
< N_MEM_SLOTS
; i
++)
5703 memseg_hash
[i
] = NULL
;
5705 hat_kpm_mseghash_clear(N_MEM_SLOTS
);
5708 * Physmax is the last valid pfn.
5710 mhash_per_slot
= (physmax
+ 1) >> MEM_HASH_SHIFT
;
5711 for (pseg
= memsegs
; pseg
!= NULL
; pseg
= pseg
->next
) {
5712 index
= MEMSEG_PFN_HASH(pseg
->pages_base
);
5713 cur
= pseg
->pages_base
;
5715 if (index
>= N_MEM_SLOTS
)
5716 index
= MEMSEG_PFN_HASH(cur
);
5718 if (memseg_hash
[index
] == NULL
||
5719 memseg_hash
[index
]->pages_base
> pseg
->pages_base
) {
5720 memseg_hash
[index
] = pseg
;
5721 hat_kpm_mseghash_update(index
, pseg
);
5723 cur
+= mhash_per_slot
;
5725 } while (cur
< pseg
->pages_end
);
5730 * Return the pagenum for the pp
5733 page_pptonum(page_t
*pp
)
5735 return (pp
->p_pagenum
);
5739 * interface to the referenced and modified etc bits
5740 * in the PSM part of the page struct
5741 * when no locking is desired.
5744 page_set_props(page_t
*pp
, uint_t flags
)
5746 ASSERT((flags
& ~(P_MOD
| P_REF
| P_RO
)) == 0);
5747 pp
->p_nrm
|= (uchar_t
)flags
;
5751 page_clr_all_props(page_t
*pp
)
5757 * Clear p_lckcnt and p_cowcnt, adjusting freemem if required.
5760 page_clear_lck_cow(page_t
*pp
, int adjust
)
5764 ASSERT(PAGE_EXCL(pp
));
5767 * The page_struct_lock need not be acquired here since
5768 * we require the caller hold the page exclusively locked.
5776 f_amount
+= pp
->p_cowcnt
;
5780 if (adjust
&& f_amount
) {
5781 mutex_enter(&freemem_lock
);
5782 availrmem
+= f_amount
;
5783 mutex_exit(&freemem_lock
);
5790 * The following functions is called from free_vp_pages()
5791 * for an inexact estimate of a newly free'd page...
5794 page_share_cnt(page_t
*pp
)
5796 return (hat_page_getshare(pp
));
5800 page_isshared(page_t
*pp
)
5802 return (hat_page_checkshare(pp
, 1));
5806 page_isfree(page_t
*pp
)
5808 return (PP_ISFREE(pp
));
5812 page_isref(page_t
*pp
)
5814 return (hat_page_getattr(pp
, P_REF
));
5818 page_ismod(page_t
*pp
)
5820 return (hat_page_getattr(pp
, P_MOD
));
5824 * The following code all currently relates to the page capture logic:
5826 * This logic is used for cases where there is a desire to claim a certain
5827 * physical page in the system for the caller. As it may not be possible
5828 * to capture the page immediately, the p_toxic bits are used in the page
5829 * structure to indicate that someone wants to capture this page. When the
5830 * page gets unlocked, the toxic flag will be noted and an attempt to capture
5831 * the page will be made. If it is successful, the original callers callback
5832 * will be called with the page to do with it what they please.
5834 * There is also an async thread which wakes up to attempt to capture
5835 * pages occasionally which have the capture bit set. All of the pages which
5836 * need to be captured asynchronously have been inserted into the
5837 * page_capture_hash and thus this thread walks that hash list. Items in the
5838 * hash have an expiration time so this thread handles that as well by removing
5839 * the item from the hash if it has expired.
5841 * Some important things to note are:
5842 * - if the PR_CAPTURE bit is set on a page, then the page is in the
5843 * page_capture_hash. The page_capture_hash_head.pchh_mutex is needed
5844 * to set and clear this bit, and while the lock is held is the only time
5845 * you can add or remove an entry from the hash.
5846 * - the PR_CAPTURE bit can only be set and cleared while holding the
5847 * page_capture_hash_head.pchh_mutex
5848 * - the t_flag field of the thread struct is used with the T_CAPTURING
5849 * flag to prevent recursion while dealing with large pages.
5850 * - pages which need to be retired never expire on the page_capture_hash.
5853 static void page_capture_thread(void);
5854 static kthread_t
*pc_thread_id
;
5856 static kmutex_t pc_thread_mutex
;
5857 static clock_t pc_thread_shortwait
;
5858 static clock_t pc_thread_longwait
;
5859 static int pc_thread_retry
;
5861 struct page_capture_callback pc_cb
[PC_NUM_CALLBACKS
];
5863 /* Note that this is a circular linked list */
5864 typedef struct page_capture_hash_bucket
{
5869 clock_t expires
; /* lbolt at which this request expires. */
5870 void *datap
; /* Cached data passed in for callback */
5871 struct page_capture_hash_bucket
*next
;
5872 struct page_capture_hash_bucket
*prev
;
5873 } page_capture_hash_bucket_t
;
5875 #define PC_PRI_HI 0 /* capture now */
5876 #define PC_PRI_LO 1 /* capture later */
5877 #define PC_NUM_PRI 2
5879 #define PAGE_CAPTURE_PRIO(pp) (PP_ISRAF(pp) ? PC_PRI_LO : PC_PRI_HI)
5883 * Each hash bucket will have it's own mutex and two lists which are:
5884 * active (0): represents requests which have not been processed by
5885 * the page_capture async thread yet.
5886 * walked (1): represents requests which have been processed by the
5887 * page_capture async thread within it's given walk of this bucket.
5889 * These are all needed so that we can synchronize all async page_capture
5890 * events. When the async thread moves to a new bucket, it will append the
5891 * walked list to the active list and walk each item one at a time, moving it
5892 * from the active list to the walked list. Thus if there is an async request
5893 * outstanding for a given page, it will always be in one of the two lists.
5894 * New requests will always be added to the active list.
5895 * If we were not able to capture a page before the request expired, we'd free
5896 * up the request structure which would indicate to page_capture that there is
5897 * no longer a need for the given page, and clear the PR_CAPTURE flag if
5900 typedef struct page_capture_hash_head
{
5901 kmutex_t pchh_mutex
;
5902 uint_t num_pages
[PC_NUM_PRI
];
5903 page_capture_hash_bucket_t lists
[2]; /* sentinel nodes */
5904 } page_capture_hash_head_t
;
5907 #define NUM_PAGE_CAPTURE_BUCKETS 4
5909 #define NUM_PAGE_CAPTURE_BUCKETS 64
5912 page_capture_hash_head_t page_capture_hash
[NUM_PAGE_CAPTURE_BUCKETS
];
5914 /* for now use a very simple hash based upon the size of a page struct */
5915 #define PAGE_CAPTURE_HASH(pp) \
5916 ((int)(((uintptr_t)pp >> 7) & (NUM_PAGE_CAPTURE_BUCKETS - 1)))
5918 extern pgcnt_t swapfs_minfree
;
5920 int page_trycapture(page_t
*pp
, uint_t szc
, uint_t flags
, void *datap
);
5923 * a callback function is required for page capture requests.
5926 page_capture_register_callback(uint_t index
, clock_t duration
,
5927 int (*cb_func
)(page_t
*, void *, uint_t
))
5929 ASSERT(pc_cb
[index
].cb_active
== 0);
5930 ASSERT(cb_func
!= NULL
);
5931 rw_enter(&pc_cb
[index
].cb_rwlock
, RW_WRITER
);
5932 pc_cb
[index
].duration
= duration
;
5933 pc_cb
[index
].cb_func
= cb_func
;
5934 pc_cb
[index
].cb_active
= 1;
5935 rw_exit(&pc_cb
[index
].cb_rwlock
);
5939 page_capture_unregister_callback(uint_t index
)
5942 struct page_capture_hash_bucket
*bp1
;
5943 struct page_capture_hash_bucket
*bp2
;
5944 struct page_capture_hash_bucket
*head
= NULL
;
5945 uint_t flags
= (1 << index
);
5947 rw_enter(&pc_cb
[index
].cb_rwlock
, RW_WRITER
);
5948 ASSERT(pc_cb
[index
].cb_active
== 1);
5949 pc_cb
[index
].duration
= 0; /* Paranoia */
5950 pc_cb
[index
].cb_func
= NULL
; /* Paranoia */
5951 pc_cb
[index
].cb_active
= 0;
5952 rw_exit(&pc_cb
[index
].cb_rwlock
);
5955 * Just move all the entries to a private list which we can walk
5956 * through without the need to hold any locks.
5957 * No more requests can get added to the hash lists for this consumer
5958 * as the cb_active field for the callback has been cleared.
5960 for (i
= 0; i
< NUM_PAGE_CAPTURE_BUCKETS
; i
++) {
5961 mutex_enter(&page_capture_hash
[i
].pchh_mutex
);
5962 for (j
= 0; j
< 2; j
++) {
5963 bp1
= page_capture_hash
[i
].lists
[j
].next
;
5964 /* walk through all but first (sentinel) element */
5965 while (bp1
!= &page_capture_hash
[i
].lists
[j
]) {
5967 if (bp2
->flags
& flags
) {
5969 bp1
->prev
= bp2
->prev
;
5970 bp2
->prev
->next
= bp1
;
5974 * Clear the PR_CAPTURE bit as we
5975 * hold appropriate locks here.
5977 page_clrtoxic(head
->pp
, PR_CAPTURE
);
5978 page_capture_hash
[i
].
5979 num_pages
[bp2
->pri
]--;
5985 mutex_exit(&page_capture_hash
[i
].pchh_mutex
);
5988 while (head
!= NULL
) {
5991 kmem_free(bp1
, sizeof (*bp1
));
5997 * Find pp in the active list and move it to the walked list if it
5999 * Note that most often pp should be at the front of the active list
6000 * as it is currently used and thus there is no other sort of optimization
6001 * being done here as this is a linked list data structure.
6002 * Returns 1 on successful move or 0 if page could not be found.
6005 page_capture_move_to_walked(page_t
*pp
)
6007 page_capture_hash_bucket_t
*bp
;
6010 index
= PAGE_CAPTURE_HASH(pp
);
6012 mutex_enter(&page_capture_hash
[index
].pchh_mutex
);
6013 bp
= page_capture_hash
[index
].lists
[0].next
;
6014 while (bp
!= &page_capture_hash
[index
].lists
[0]) {
6016 /* Remove from old list */
6017 bp
->next
->prev
= bp
->prev
;
6018 bp
->prev
->next
= bp
->next
;
6020 /* Add to new list */
6021 bp
->next
= page_capture_hash
[index
].lists
[1].next
;
6022 bp
->prev
= &page_capture_hash
[index
].lists
[1];
6023 page_capture_hash
[index
].lists
[1].next
= bp
;
6024 bp
->next
->prev
= bp
;
6027 * There is a small probability of page on a free
6028 * list being retired while being allocated
6029 * and before P_RAF is set on it. The page may
6030 * end up marked as high priority request instead
6031 * of low priority request.
6032 * If P_RAF page is not marked as low priority request
6033 * change it to low priority request.
6035 page_capture_hash
[index
].num_pages
[bp
->pri
]--;
6036 bp
->pri
= PAGE_CAPTURE_PRIO(pp
);
6037 page_capture_hash
[index
].num_pages
[bp
->pri
]++;
6038 mutex_exit(&page_capture_hash
[index
].pchh_mutex
);
6043 mutex_exit(&page_capture_hash
[index
].pchh_mutex
);
6048 * Add a new entry to the page capture hash. The only case where a new
6049 * entry is not added is when the page capture consumer is no longer registered.
6050 * In this case, we'll silently not add the page to the hash. We know that
6051 * page retire will always be registered for the case where we are currently
6052 * unretiring a page and thus there are no conflicts.
6055 page_capture_add_hash(page_t
*pp
, uint_t szc
, uint_t flags
, void *datap
)
6057 page_capture_hash_bucket_t
*bp1
;
6058 page_capture_hash_bucket_t
*bp2
;
6064 page_capture_hash_bucket_t
*tp1
;
6068 ASSERT(!(flags
& CAPTURE_ASYNC
));
6070 bp1
= kmem_alloc(sizeof (struct page_capture_hash_bucket
), KM_SLEEP
);
6077 for (cb_index
= 0; cb_index
< PC_NUM_CALLBACKS
; cb_index
++) {
6078 if ((flags
>> cb_index
) & 1) {
6083 ASSERT(cb_index
!= PC_NUM_CALLBACKS
);
6085 rw_enter(&pc_cb
[cb_index
].cb_rwlock
, RW_READER
);
6086 if (pc_cb
[cb_index
].cb_active
) {
6087 if (pc_cb
[cb_index
].duration
== -1) {
6088 bp1
->expires
= (clock_t)-1;
6090 bp1
->expires
= ddi_get_lbolt() +
6091 pc_cb
[cb_index
].duration
;
6094 /* There's no callback registered so don't add to the hash */
6095 rw_exit(&pc_cb
[cb_index
].cb_rwlock
);
6096 kmem_free(bp1
, sizeof (*bp1
));
6100 index
= PAGE_CAPTURE_HASH(pp
);
6103 * Only allow capture flag to be modified under this mutex.
6104 * Prevents multiple entries for same page getting added.
6106 mutex_enter(&page_capture_hash
[index
].pchh_mutex
);
6109 * if not already on the hash, set capture bit and add to the hash
6111 if (!(pp
->p_toxic
& PR_CAPTURE
)) {
6113 /* Check for duplicate entries */
6114 for (l
= 0; l
< 2; l
++) {
6115 tp1
= page_capture_hash
[index
].lists
[l
].next
;
6116 while (tp1
!= &page_capture_hash
[index
].lists
[l
]) {
6117 if (tp1
->pp
== pp
) {
6118 panic("page pp 0x%p already on hash "
6120 (void *)pp
, (void *)tp1
);
6127 page_settoxic(pp
, PR_CAPTURE
);
6128 pri
= PAGE_CAPTURE_PRIO(pp
);
6130 bp1
->next
= page_capture_hash
[index
].lists
[0].next
;
6131 bp1
->prev
= &page_capture_hash
[index
].lists
[0];
6132 bp1
->next
->prev
= bp1
;
6133 page_capture_hash
[index
].lists
[0].next
= bp1
;
6134 page_capture_hash
[index
].num_pages
[pri
]++;
6135 if (flags
& CAPTURE_RETIRE
) {
6136 page_retire_incr_pend_count(datap
);
6138 mutex_exit(&page_capture_hash
[index
].pchh_mutex
);
6139 rw_exit(&pc_cb
[cb_index
].cb_rwlock
);
6145 * A page retire request will replace any other request.
6146 * A second physmem request which is for a different process than
6147 * the currently registered one will be dropped as there is
6148 * no way to hold the private data for both calls.
6149 * In the future, once there are more callers, this will have to
6150 * be worked out better as there needs to be private storage for
6151 * at least each type of caller (maybe have datap be an array of
6152 * *void's so that we can index based upon callers index).
6155 /* walk hash list to update expire time */
6156 for (i
= 0; i
< 2; i
++) {
6157 bp2
= page_capture_hash
[index
].lists
[i
].next
;
6158 while (bp2
!= &page_capture_hash
[index
].lists
[i
]) {
6159 if (bp2
->pp
== pp
) {
6160 if (flags
& CAPTURE_RETIRE
) {
6161 if (!(bp2
->flags
& CAPTURE_RETIRE
)) {
6162 page_retire_incr_pend_count(
6165 bp2
->expires
= bp1
->expires
;
6169 ASSERT(flags
& CAPTURE_PHYSMEM
);
6170 if (!(bp2
->flags
& CAPTURE_RETIRE
) &&
6171 (datap
== bp2
->datap
)) {
6172 bp2
->expires
= bp1
->expires
;
6175 mutex_exit(&page_capture_hash
[index
].
6177 rw_exit(&pc_cb
[cb_index
].cb_rwlock
);
6178 kmem_free(bp1
, sizeof (*bp1
));
6186 * the PR_CAPTURE flag is protected by the page_capture_hash mutexes
6187 * and thus it either has to be set or not set and can't change
6188 * while holding the mutex above.
6190 panic("page_capture_add_hash, PR_CAPTURE flag set on pp %p\n",
6195 * We have a page in our hands, lets try and make it ours by turning
6196 * it into a clean page like it had just come off the freelists.
6198 * Returns 0 on success, with the page still EXCL locked.
6199 * On failure, the page will be unlocked, and returns EAGAIN
6202 page_capture_clean_page(page_t
*pp
)
6205 int skip_unlock
= 0;
6211 ASSERT(PAGE_EXCL(pp
));
6212 ASSERT(!PP_RETIRED(pp
));
6213 ASSERT(curthread
->t_flag
& T_CAPTURING
);
6215 if (PP_ISFREE(pp
)) {
6216 if (!page_reclaim(pp
, NULL
)) {
6221 ASSERT(pp
->p_szc
== 0);
6222 if (pp
->p_vnode
!= NULL
) {
6224 * Since this page came from the
6225 * cachelist, we must destroy the
6226 * old vnode association.
6228 page_hashout(pp
, false);
6234 * If we know page_relocate will fail, skip it
6235 * It could still fail due to a UE on another page but we
6236 * can't do anything about that.
6238 if (pp
->p_toxic
& PR_UE
) {
6243 * It's possible that pages can not have a vnode as fsflush comes
6244 * through and cleans up these pages. It's ugly but that's how it is.
6246 if (pp
->p_vnode
== NULL
) {
6251 * Page was not free, so lets try to relocate it.
6252 * page_relocate only works with root pages, so if this is not a root
6253 * page, we need to demote it to try and relocate it.
6254 * Unfortunately this is the best we can do right now.
6257 if ((pp
->p_szc
> 0) && (pp
!= PP_PAGEROOT(pp
))) {
6258 if (page_try_demote_pages(pp
) == 0) {
6263 ret
= page_relocate(&pp
, &newpp
, 1, 0, &count
, NULL
);
6266 /* unlock the new page(s) */
6267 while (count
-- > 0) {
6268 ASSERT(newpp
!= NULL
);
6270 page_sub(&newpp
, npp
);
6273 ASSERT(newpp
== NULL
);
6275 * Check to see if the page we have is too large.
6276 * If so, demote it freeing up the extra pages.
6278 if (pp
->p_szc
> 0) {
6279 /* For now demote extra pages to szc == 0 */
6280 extra
= page_get_pagecnt(pp
->p_szc
) - 1;
6288 /* Make sure to set our page to szc 0 as well */
6289 ASSERT(pp
->p_next
== pp
&& pp
->p_prev
== pp
);
6293 } else if (ret
== EIO
) {
6298 * Need to reset return type as we failed to relocate the page
6299 * but that does not mean that some of the next steps will not
6307 if (pp
->p_szc
> 0) {
6308 if (page_try_demote_pages(pp
) == 0) {
6314 ASSERT(pp
->p_szc
== 0);
6316 if (hat_ismod(pp
)) {
6324 if (pp
->p_lckcnt
|| pp
->p_cowcnt
) {
6329 (void) hat_pageunload(pp
, HAT_FORCE_PGUNLOAD
);
6330 ASSERT(!hat_page_is_mapped(pp
));
6332 if (hat_ismod(pp
)) {
6334 * This is a semi-odd case as the page is now modified but not
6335 * mapped as we just unloaded the mappings above.
6340 if (pp
->p_vnode
!= NULL
) {
6341 page_hashout(pp
, false);
6345 * At this point, the page should be in a clean state and
6346 * we can do whatever we want with it.
6355 ASSERT(pp
->p_szc
== 0);
6356 ASSERT(PAGE_EXCL(pp
));
6365 * Various callers of page_trycapture() can have different restrictions upon
6366 * what memory they have access to.
6367 * Returns 0 on success, with the following error codes on failure:
6368 * EPERM - The requested page is long term locked, and thus repeated
6369 * requests to capture this page will likely fail.
6370 * ENOMEM - There was not enough free memory in the system to safely
6371 * map the requested page.
6372 * ENOENT - The requested page was inside the kernel cage, and the
6373 * PHYSMEM_CAGE flag was not set.
6376 page_capture_pre_checks(page_t
*pp
, uint_t flags
)
6380 #if defined(__sparc)
6381 if (pp
->p_vnode
== &promvp
) {
6385 if (PP_ISNORELOC(pp
) && !(flags
& CAPTURE_GET_CAGE
) &&
6386 (flags
& CAPTURE_PHYSMEM
)) {
6390 if (PP_ISNORELOCKERNEL(pp
)) {
6397 #endif /* __sparc */
6399 /* only physmem currently has the restrictions checked below */
6400 if (!(flags
& CAPTURE_PHYSMEM
)) {
6404 if (availrmem
< swapfs_minfree
) {
6406 * We won't try to capture this page as we are
6407 * running low on memory.
6415 * Once we have a page in our mits, go ahead and complete the capture
6417 * Returns 1 on failure where page is no longer needed
6418 * Returns 0 on success
6419 * Returns -1 if there was a transient failure.
6420 * Failure cases must release the SE_EXCL lock on pp (usually via page_free).
6423 page_capture_take_action(page_t
*pp
, uint_t flags
, void *datap
)
6427 page_capture_hash_bucket_t
*bp1
;
6428 page_capture_hash_bucket_t
*bp2
;
6433 ASSERT(PAGE_EXCL(pp
));
6434 ASSERT(curthread
->t_flag
& T_CAPTURING
);
6436 for (cb_index
= 0; cb_index
< PC_NUM_CALLBACKS
; cb_index
++) {
6437 if ((flags
>> cb_index
) & 1) {
6441 ASSERT(cb_index
< PC_NUM_CALLBACKS
);
6444 * Remove the entry from the page_capture hash, but don't free it yet
6445 * as we may need to put it back.
6446 * Since we own the page at this point in time, we should find it
6447 * in the hash if this is an ASYNC call. If we don't it's likely
6448 * that the page_capture_async() thread decided that this request
6449 * had expired, in which case we just continue on.
6451 if (flags
& CAPTURE_ASYNC
) {
6453 index
= PAGE_CAPTURE_HASH(pp
);
6455 mutex_enter(&page_capture_hash
[index
].pchh_mutex
);
6456 for (i
= 0; i
< 2 && !found
; i
++) {
6457 bp1
= page_capture_hash
[index
].lists
[i
].next
;
6458 while (bp1
!= &page_capture_hash
[index
].lists
[i
]) {
6459 if (bp1
->pp
== pp
) {
6460 bp1
->next
->prev
= bp1
->prev
;
6461 bp1
->prev
->next
= bp1
->next
;
6462 page_capture_hash
[index
].
6463 num_pages
[bp1
->pri
]--;
6464 page_clrtoxic(pp
, PR_CAPTURE
);
6471 mutex_exit(&page_capture_hash
[index
].pchh_mutex
);
6474 /* Synchronize with the unregister func. */
6475 rw_enter(&pc_cb
[cb_index
].cb_rwlock
, RW_READER
);
6476 if (!pc_cb
[cb_index
].cb_active
) {
6478 rw_exit(&pc_cb
[cb_index
].cb_rwlock
);
6480 kmem_free(bp1
, sizeof (*bp1
));
6486 * We need to remove the entry from the page capture hash and turn off
6487 * the PR_CAPTURE bit before calling the callback. We'll need to cache
6488 * the entry here, and then based upon the return value, cleanup
6489 * appropriately or re-add it to the hash, making sure that someone else
6490 * hasn't already done so.
6491 * It should be rare for the callback to fail and thus it's ok for
6492 * the failure path to be a bit complicated as the success path is
6493 * cleaner and the locking rules are easier to follow.
6496 ret
= pc_cb
[cb_index
].cb_func(pp
, datap
, flags
);
6498 rw_exit(&pc_cb
[cb_index
].cb_rwlock
);
6501 * If this was an ASYNC request, we need to cleanup the hash if the
6502 * callback was successful or if the request was no longer valid.
6503 * For non-ASYNC requests, we return failure to map and the caller
6504 * will take care of adding the request to the hash.
6505 * Note also that the callback itself is responsible for the page
6506 * at this point in time in terms of locking ... The most common
6507 * case for the failure path should just be a page_free.
6511 if (bp1
->flags
& CAPTURE_RETIRE
) {
6512 page_retire_decr_pend_count(datap
);
6514 kmem_free(bp1
, sizeof (*bp1
));
6522 ASSERT(flags
& CAPTURE_ASYNC
);
6525 * Check for expiration time first as we can just free it up if it's
6528 if (ddi_get_lbolt() > bp1
->expires
&& bp1
->expires
!= -1) {
6529 kmem_free(bp1
, sizeof (*bp1
));
6534 * The callback failed and there used to be an entry in the hash for
6535 * this page, so we need to add it back to the hash.
6537 mutex_enter(&page_capture_hash
[index
].pchh_mutex
);
6538 if (!(pp
->p_toxic
& PR_CAPTURE
)) {
6539 /* just add bp1 back to head of walked list */
6540 page_settoxic(pp
, PR_CAPTURE
);
6541 bp1
->next
= page_capture_hash
[index
].lists
[1].next
;
6542 bp1
->prev
= &page_capture_hash
[index
].lists
[1];
6543 bp1
->next
->prev
= bp1
;
6544 bp1
->pri
= PAGE_CAPTURE_PRIO(pp
);
6545 page_capture_hash
[index
].lists
[1].next
= bp1
;
6546 page_capture_hash
[index
].num_pages
[bp1
->pri
]++;
6547 mutex_exit(&page_capture_hash
[index
].pchh_mutex
);
6552 * Otherwise there was a new capture request added to list
6553 * Need to make sure that our original data is represented if
6556 for (i
= 0; i
< 2; i
++) {
6557 bp2
= page_capture_hash
[index
].lists
[i
].next
;
6558 while (bp2
!= &page_capture_hash
[index
].lists
[i
]) {
6559 if (bp2
->pp
== pp
) {
6560 if (bp1
->flags
& CAPTURE_RETIRE
) {
6561 if (!(bp2
->flags
& CAPTURE_RETIRE
)) {
6562 bp2
->szc
= bp1
->szc
;
6563 bp2
->flags
= bp1
->flags
;
6564 bp2
->expires
= bp1
->expires
;
6565 bp2
->datap
= bp1
->datap
;
6568 ASSERT(bp1
->flags
& CAPTURE_PHYSMEM
);
6569 if (!(bp2
->flags
& CAPTURE_RETIRE
)) {
6570 bp2
->szc
= bp1
->szc
;
6571 bp2
->flags
= bp1
->flags
;
6572 bp2
->expires
= bp1
->expires
;
6573 bp2
->datap
= bp1
->datap
;
6576 page_capture_hash
[index
].num_pages
[bp2
->pri
]--;
6577 bp2
->pri
= PAGE_CAPTURE_PRIO(pp
);
6578 page_capture_hash
[index
].num_pages
[bp2
->pri
]++;
6579 mutex_exit(&page_capture_hash
[index
].
6581 kmem_free(bp1
, sizeof (*bp1
));
6587 panic("PR_CAPTURE set but not on hash for pp 0x%p\n", (void *)pp
);
6592 * Try to capture the given page for the caller specified in the flags
6593 * parameter. The page will either be captured and handed over to the
6594 * appropriate callback, or will be queued up in the page capture hash
6595 * to be captured asynchronously.
6596 * If the current request is due to an async capture, the page must be
6597 * exclusively locked before calling this function.
6598 * Currently szc must be 0 but in the future this should be expandable to
6600 * Returns 0 on success, with the following error codes on failure:
6601 * EPERM - The requested page is long term locked, and thus repeated
6602 * requests to capture this page will likely fail.
6603 * ENOMEM - There was not enough free memory in the system to safely
6604 * map the requested page.
6605 * ENOENT - The requested page was inside the kernel cage, and the
6606 * CAPTURE_GET_CAGE flag was not set.
6607 * EAGAIN - The requested page could not be capturead at this point in
6608 * time but future requests will likely work.
6609 * EBUSY - The requested page is retired and the CAPTURE_GET_RETIRED flag
6613 page_itrycapture(page_t
*pp
, uint_t szc
, uint_t flags
, void *datap
)
6618 if (flags
& CAPTURE_ASYNC
) {
6619 ASSERT(PAGE_EXCL(pp
));
6623 /* Make sure there's enough availrmem ... */
6624 ret
= page_capture_pre_checks(pp
, flags
);
6629 if (!page_trylock(pp
, SE_EXCL
)) {
6630 for (cb_index
= 0; cb_index
< PC_NUM_CALLBACKS
; cb_index
++) {
6631 if ((flags
>> cb_index
) & 1) {
6635 ASSERT(cb_index
< PC_NUM_CALLBACKS
);
6637 /* Special case for retired pages */
6638 if (PP_RETIRED(pp
)) {
6639 if (flags
& CAPTURE_GET_RETIRED
) {
6640 if (!page_unretire_pp(pp
, PR_UNR_TEMP
)) {
6642 * Need to set capture bit and add to
6643 * hash so that the page will be
6644 * retired when freed.
6646 page_capture_add_hash(pp
, szc
,
6647 CAPTURE_RETIRE
, NULL
);
6655 page_capture_add_hash(pp
, szc
, flags
, datap
);
6660 ASSERT(PAGE_EXCL(pp
));
6662 /* Need to check for physmem async requests that availrmem is sane */
6663 if ((flags
& (CAPTURE_ASYNC
| CAPTURE_PHYSMEM
)) ==
6664 (CAPTURE_ASYNC
| CAPTURE_PHYSMEM
) &&
6665 (availrmem
< swapfs_minfree
)) {
6670 ret
= page_capture_clean_page(pp
);
6673 /* We failed to get the page, so lets add it to the hash */
6674 if (!(flags
& CAPTURE_ASYNC
)) {
6675 page_capture_add_hash(pp
, szc
, flags
, datap
);
6681 ASSERT(PAGE_EXCL(pp
));
6682 ASSERT(pp
->p_szc
== 0);
6684 /* Call the callback */
6685 ret
= page_capture_take_action(pp
, flags
, datap
);
6692 * Note that in the failure cases from page_capture_take_action, the
6693 * EXCL lock will have already been dropped.
6695 if ((ret
== -1) && (!(flags
& CAPTURE_ASYNC
))) {
6696 page_capture_add_hash(pp
, szc
, flags
, datap
);
6702 page_trycapture(page_t
*pp
, uint_t szc
, uint_t flags
, void *datap
)
6706 curthread
->t_flag
|= T_CAPTURING
;
6707 ret
= page_itrycapture(pp
, szc
, flags
, datap
);
6708 curthread
->t_flag
&= ~T_CAPTURING
; /* xor works as we know its set */
6713 * When unlocking a page which has the PR_CAPTURE bit set, this routine
6714 * gets called to try and capture the page.
6717 page_unlock_capture(page_t
*pp
)
6719 page_capture_hash_bucket_t
*bp
;
6726 extern vnode_t retired_pages
;
6729 * We need to protect against a possible deadlock here where we own
6730 * the vnode page hash mutex and want to acquire it again as there
6731 * are locations in the code, where we unlock a page while holding
6732 * the mutex which can lead to the page being captured and eventually
6733 * end up here. As we may be hashing out the old page and hashing into
6734 * the retire vnode, we need to make sure we don't own them.
6735 * Other callbacks who do hash operations also need to make sure that
6736 * before they hashin to a vnode that they do not currently own the
6737 * vphm mutex otherwise there will be a panic.
6739 if (mutex_owned(page_vnode_mutex(&retired_pages
))) {
6740 page_unlock_nocapture(pp
);
6743 if (pp
->p_vnode
!= NULL
&& mutex_owned(page_vnode_mutex(pp
->p_vnode
))) {
6744 page_unlock_nocapture(pp
);
6748 index
= PAGE_CAPTURE_HASH(pp
);
6750 mp
= &page_capture_hash
[index
].pchh_mutex
;
6752 for (i
= 0; i
< 2; i
++) {
6753 bp
= page_capture_hash
[index
].lists
[i
].next
;
6754 while (bp
!= &page_capture_hash
[index
].lists
[i
]) {
6757 flags
= bp
->flags
| CAPTURE_ASYNC
;
6760 (void) page_trycapture(pp
, szc
, flags
, datap
);
6767 /* Failed to find page in hash so clear flags and unlock it. */
6768 page_clrtoxic(pp
, PR_CAPTURE
);
6778 for (i
= 0; i
< NUM_PAGE_CAPTURE_BUCKETS
; i
++) {
6779 page_capture_hash
[i
].lists
[0].next
=
6780 &page_capture_hash
[i
].lists
[0];
6781 page_capture_hash
[i
].lists
[0].prev
=
6782 &page_capture_hash
[i
].lists
[0];
6783 page_capture_hash
[i
].lists
[1].next
=
6784 &page_capture_hash
[i
].lists
[1];
6785 page_capture_hash
[i
].lists
[1].prev
=
6786 &page_capture_hash
[i
].lists
[1];
6789 pc_thread_shortwait
= 23 * hz
;
6790 pc_thread_longwait
= 1201 * hz
;
6791 pc_thread_retry
= 3;
6792 mutex_init(&pc_thread_mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
6793 cv_init(&pc_cv
, NULL
, CV_DEFAULT
, NULL
);
6794 pc_thread_id
= thread_create(NULL
, 0, page_capture_thread
, NULL
, 0, &p0
,
6795 TS_RUN
, minclsyspri
);
6799 * It is necessary to scrub any failing pages prior to reboot in order to
6800 * prevent a latent error trap from occurring on the next boot.
6803 page_retire_mdboot()
6807 page_capture_hash_bucket_t
*bp
;
6810 /* walk lists looking for pages to scrub */
6811 for (i
= 0; i
< NUM_PAGE_CAPTURE_BUCKETS
; i
++) {
6812 for (pri
= 0; pri
< PC_NUM_PRI
; pri
++) {
6813 if (page_capture_hash
[i
].num_pages
[pri
] != 0) {
6817 if (pri
== PC_NUM_PRI
)
6820 mutex_enter(&page_capture_hash
[i
].pchh_mutex
);
6822 for (j
= 0; j
< 2; j
++) {
6823 bp
= page_capture_hash
[i
].lists
[j
].next
;
6824 while (bp
!= &page_capture_hash
[i
].lists
[j
]) {
6827 if (page_trylock(pp
, SE_EXCL
)) {
6829 pagescrub(pp
, 0, PAGESIZE
);
6836 mutex_exit(&page_capture_hash
[i
].pchh_mutex
);
6841 * Walk the page_capture_hash trying to capture pages and also cleanup old
6842 * entries which have expired.
6845 page_capture_async()
6850 page_capture_hash_bucket_t
*bp1
, *bp2
;
6856 /* If there are outstanding pages to be captured, get to work */
6857 for (i
= 0; i
< NUM_PAGE_CAPTURE_BUCKETS
; i
++) {
6858 for (pri
= 0; pri
< PC_NUM_PRI
; pri
++) {
6859 if (page_capture_hash
[i
].num_pages
[pri
] != 0)
6862 if (pri
== PC_NUM_PRI
)
6865 /* Append list 1 to list 0 and then walk through list 0 */
6866 mutex_enter(&page_capture_hash
[i
].pchh_mutex
);
6867 bp1
= &page_capture_hash
[i
].lists
[1];
6870 bp1
->prev
->next
= page_capture_hash
[i
].lists
[0].next
;
6871 bp2
->prev
= &page_capture_hash
[i
].lists
[0];
6872 page_capture_hash
[i
].lists
[0].next
->prev
= bp1
->prev
;
6873 page_capture_hash
[i
].lists
[0].next
= bp2
;
6878 /* list[1] will be empty now */
6880 bp1
= page_capture_hash
[i
].lists
[0].next
;
6881 while (bp1
!= &page_capture_hash
[i
].lists
[0]) {
6882 /* Check expiration time */
6883 if ((ddi_get_lbolt() > bp1
->expires
&&
6884 bp1
->expires
!= -1) ||
6885 page_deleted(bp1
->pp
)) {
6886 page_capture_hash
[i
].lists
[0].next
= bp1
->next
;
6888 &page_capture_hash
[i
].lists
[0];
6889 page_capture_hash
[i
].num_pages
[bp1
->pri
]--;
6892 * We can safely remove the PR_CAPTURE bit
6893 * without holding the EXCL lock on the page
6894 * as the PR_CAPTURE bit requres that the
6895 * page_capture_hash[].pchh_mutex be held
6898 page_clrtoxic(bp1
->pp
, PR_CAPTURE
);
6899 mutex_exit(&page_capture_hash
[i
].pchh_mutex
);
6900 kmem_free(bp1
, sizeof (*bp1
));
6901 mutex_enter(&page_capture_hash
[i
].pchh_mutex
);
6902 bp1
= page_capture_hash
[i
].lists
[0].next
;
6909 mutex_exit(&page_capture_hash
[i
].pchh_mutex
);
6910 if (page_trylock(pp
, SE_EXCL
)) {
6911 ret
= page_trycapture(pp
, szc
,
6912 flags
| CAPTURE_ASYNC
, datap
);
6914 ret
= 1; /* move to walked hash */
6918 /* Move to walked hash */
6919 (void) page_capture_move_to_walked(pp
);
6921 mutex_enter(&page_capture_hash
[i
].pchh_mutex
);
6922 bp1
= page_capture_hash
[i
].lists
[0].next
;
6925 mutex_exit(&page_capture_hash
[i
].pchh_mutex
);
6930 * This function is called by the page_capture_thread, and is needed in
6931 * in order to initiate aio cleanup, so that pages used in aio
6932 * will be unlocked and subsequently retired by page_capture_thread.
6935 do_aio_cleanup(void)
6938 int (*aio_cleanup_dr_delete_memory
)(proc_t
*);
6941 if (modload("sys", "kaio") == -1) {
6942 cmn_err(CE_WARN
, "do_aio_cleanup: cannot load kaio");
6946 * We use the aio_cleanup_dr_delete_memory function to
6947 * initiate the actual clean up; this function will wake
6948 * up the per-process aio_cleanup_thread.
6950 aio_cleanup_dr_delete_memory
= (int (*)(proc_t
*))
6951 modgetsymvalue("aio_cleanup_dr_delete_memory", 0);
6952 if (aio_cleanup_dr_delete_memory
== NULL
) {
6954 "aio_cleanup_dr_delete_memory not found in kaio");
6957 mutex_enter(&pidlock
);
6958 for (procp
= practive
; (procp
!= NULL
); procp
= procp
->p_next
) {
6959 mutex_enter(&procp
->p_lock
);
6960 if (procp
->p_aio
!= NULL
) {
6961 /* cleanup proc's outstanding kaio */
6962 cleaned
+= (*aio_cleanup_dr_delete_memory
)(procp
);
6964 mutex_exit(&procp
->p_lock
);
6966 mutex_exit(&pidlock
);
6971 * helper function for page_capture_thread
6974 page_capture_handle_outstanding(void)
6978 /* Reap pages before attempting capture pages */
6981 if ((page_retire_pend_count() > page_retire_pend_kas_count()) &&
6982 hat_supported(HAT_DYNAMIC_ISM_UNMAP
, NULL
)) {
6984 * Note: Purging only for platforms that support
6985 * ISM hat_pageunload() - mainly SPARC. On x86/x64
6986 * platforms ISM pages SE_SHARED locked until destroyed.
6989 /* disable and purge seg_pcache */
6990 (void) seg_p_disable();
6991 for (ntry
= 0; ntry
< pc_thread_retry
; ntry
++) {
6992 if (!page_retire_pend_count())
6994 if (do_aio_cleanup()) {
6996 * allow the apps cleanup threads
6999 delay(pc_thread_shortwait
);
7001 page_capture_async();
7003 /* reenable seg_pcache */
7006 /* completed what can be done. break out */
7011 * For kernel pages and/or unsupported HAT_DYNAMIC_ISM_UNMAP, reap
7012 * and then attempt to capture.
7015 page_capture_async();
7019 * The page_capture_thread loops forever, looking to see if there are
7020 * pages still waiting to be captured.
7023 page_capture_thread(void)
7031 CALLB_CPR_INIT(&c
, &pc_thread_mutex
, callb_generic_cpr
, "page_capture");
7033 mutex_enter(&pc_thread_mutex
);
7037 for (i
= 0; i
< NUM_PAGE_CAPTURE_BUCKETS
; i
++) {
7039 page_capture_hash
[i
].num_pages
[PC_PRI_HI
];
7041 page_capture_hash
[i
].num_pages
[PC_PRI_LO
];
7044 timeout
= pc_thread_longwait
;
7045 if (high_pri_pages
!= 0) {
7046 timeout
= pc_thread_shortwait
;
7047 page_capture_handle_outstanding();
7048 } else if (low_pri_pages
!= 0) {
7049 page_capture_async();
7051 CALLB_CPR_SAFE_BEGIN(&c
);
7052 (void) cv_reltimedwait(&pc_cv
, &pc_thread_mutex
,
7053 timeout
, TR_CLOCK_TICK
);
7054 CALLB_CPR_SAFE_END(&c
, &pc_thread_mutex
);
7059 * Attempt to locate a bucket that has enough pages to satisfy the request.
7060 * The initial check is done without the lock to avoid unneeded contention.
7061 * The function returns 1 if enough pages were found, else 0 if it could not
7062 * find enough pages in a bucket.
7065 pcf_decrement_bucket(pgcnt_t npages
)
7071 p
= &pcf
[PCF_INDEX()];
7072 q
= &pcf
[pcf_fanout
];
7073 for (i
= 0; i
< pcf_fanout
; i
++) {
7074 if (p
->pcf_count
> npages
) {
7076 * a good one to try.
7078 mutex_enter(&p
->pcf_lock
);
7079 if (p
->pcf_count
> npages
) {
7080 p
->pcf_count
-= (uint_t
)npages
;
7082 * freemem is not protected by any lock.
7083 * Thus, we cannot have any assertion
7084 * containing freemem here.
7087 mutex_exit(&p
->pcf_lock
);
7090 mutex_exit(&p
->pcf_lock
);
7102 * pcftotal_ret: If the value is not NULL and we have walked all the
7103 * buckets but did not find enough pages then it will
7104 * be set to the total number of pages in all the pcf
7106 * npages: Is the number of pages we have been requested to
7108 * unlock: If set to 0 we will leave the buckets locked if the
7109 * requested number of pages are not found.
7111 * Go and try to satisfy the page request from any number of buckets.
7112 * This can be a very expensive operation as we have to lock the buckets
7113 * we are checking (and keep them locked), starting at bucket 0.
7115 * The function returns 1 if enough pages were found, else 0 if it could not
7116 * find enough pages in the buckets.
7120 pcf_decrement_multiple(pgcnt_t
*pcftotal_ret
, pgcnt_t npages
, int unlock
)
7127 /* try to collect pages from several pcf bins */
7128 for (pcftotal
= 0, i
= 0; i
< pcf_fanout
; i
++) {
7129 mutex_enter(&p
->pcf_lock
);
7130 pcftotal
+= p
->pcf_count
;
7131 if (pcftotal
>= npages
) {
7133 * Wow! There are enough pages laying around
7134 * to satisfy the request. Do the accounting,
7135 * drop the locks we acquired, and go back.
7137 * freemem is not protected by any lock. So,
7138 * we cannot have any assertion containing
7143 if (p
->pcf_count
<= npages
) {
7144 npages
-= p
->pcf_count
;
7147 p
->pcf_count
-= (uint_t
)npages
;
7150 mutex_exit(&p
->pcf_lock
);
7153 ASSERT(npages
== 0);
7159 /* failed to collect pages - release the locks */
7160 while (--p
>= pcf
) {
7161 mutex_exit(&p
->pcf_lock
);
7164 if (pcftotal_ret
!= NULL
)
7165 *pcftotal_ret
= pcftotal
;
7170 pagecache_cmp(const void *va
, const void *vb
)
7172 const page_t
*a
= va
;
7173 const page_t
*b
= vb
;
7175 if (a
->p_offset
> b
->p_offset
)
7177 if (a
->p_offset
< b
->p_offset
)
7183 pagecache_init(struct vnode
*vnode
)
7185 avl_create(&vnode
->v_pagecache
, pagecache_cmp
, sizeof (struct page
),
7186 offsetof(struct page
, p_pagecache
));
7187 list_create(&vnode
->v_pagecache_list
, sizeof (struct page
),
7188 offsetof(struct page
, p_list
.vnode
));
7189 mutex_init(&vnode
->v_pagecache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7193 pagecache_fini(struct vnode
*vnode
)
7195 mutex_destroy(&vnode
->v_pagecache_lock
);
7196 list_destroy(&vnode
->v_pagecache_list
);
7197 avl_destroy(&vnode
->v_pagecache
);