1*74c51b5dSchs /* $NetBSD: uvm_anon.c,v 1.80 2020/10/25 00:05:26 chs Exp $ */
244f5fc28Schuck
344f5fc28Schuck /*
444f5fc28Schuck * Copyright (c) 1997 Charles D. Cranor and Washington University.
544f5fc28Schuck * All rights reserved.
644f5fc28Schuck *
744f5fc28Schuck * Redistribution and use in source and binary forms, with or without
844f5fc28Schuck * modification, are permitted provided that the following conditions
944f5fc28Schuck * are met:
1044f5fc28Schuck * 1. Redistributions of source code must retain the above copyright
1144f5fc28Schuck * notice, this list of conditions and the following disclaimer.
1244f5fc28Schuck * 2. Redistributions in binary form must reproduce the above copyright
1344f5fc28Schuck * notice, this list of conditions and the following disclaimer in the
1444f5fc28Schuck * documentation and/or other materials provided with the distribution.
1544f5fc28Schuck *
1644f5fc28Schuck * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1744f5fc28Schuck * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1844f5fc28Schuck * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1944f5fc28Schuck * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2044f5fc28Schuck * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2144f5fc28Schuck * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2244f5fc28Schuck * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2344f5fc28Schuck * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2444f5fc28Schuck * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2544f5fc28Schuck * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2644f5fc28Schuck */
2744f5fc28Schuck
2844f5fc28Schuck /*
2944f5fc28Schuck * uvm_anon.c: uvm anon ops
3044f5fc28Schuck */
3144f5fc28Schuck
32b616d1caSlukem #include <sys/cdefs.h>
33*74c51b5dSchs __KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.80 2020/10/25 00:05:26 chs Exp $");
34b616d1caSlukem
3544f5fc28Schuck #include "opt_uvmhist.h"
3644f5fc28Schuck
3744f5fc28Schuck #include <sys/param.h>
3844f5fc28Schuck #include <sys/systm.h>
3944f5fc28Schuck #include <sys/pool.h>
4016f0ca36Schs #include <sys/kernel.h>
4145b09b5dSuwe #include <sys/atomic.h>
4244f5fc28Schuck
4344f5fc28Schuck #include <uvm/uvm.h>
4444f5fc28Schuck #include <uvm/uvm_swap.h>
459d3e3eabSyamt #include <uvm/uvm_pdpolicy.h>
4644f5fc28Schuck
47d18c6ca4Sad static struct pool_cache uvm_anon_cache;
4816f0ca36Schs
49662ada8fSyamt static int uvm_anon_ctor(void *, void *, int);
5016f0ca36Schs
5144f5fc28Schuck void
uvm_anon_init(void)52e569faccSthorpej uvm_anon_init(void)
5344f5fc28Schuck {
5416f0ca36Schs
559aef0fdbSad pool_cache_bootstrap(&uvm_anon_cache, sizeof(struct vm_anon), 0, 0,
569aef0fdbSad PR_LARGECACHE, "anonpl", NULL, IPL_NONE, uvm_anon_ctor,
573aad734eSrmind NULL, NULL);
5844f5fc28Schuck }
5944f5fc28Schuck
60662ada8fSyamt static int
uvm_anon_ctor(void * arg,void * object,int flags)611a7bc55dSyamt uvm_anon_ctor(void *arg, void *object, int flags)
6244f5fc28Schuck {
63662ada8fSyamt struct vm_anon *anon = object;
6444f5fc28Schuck
65662ada8fSyamt anon->an_ref = 0;
6639445b05Srmind anon->an_lock = NULL;
67662ada8fSyamt anon->an_page = NULL;
686fbf5bf6Syamt #if defined(VMSWAP)
69662ada8fSyamt anon->an_swslot = 0;
703aad734eSrmind #endif
7189b005fcSchs return 0;
7216f0ca36Schs }
7316f0ca36Schs
7416f0ca36Schs /*
753aad734eSrmind * uvm_analloc: allocate a new anon.
76ad7259d7Sthorpej *
773aad734eSrmind * => anon will have no lock associated.
7844f5fc28Schuck */
7944f5fc28Schuck struct vm_anon *
uvm_analloc(void)80e569faccSthorpej uvm_analloc(void)
8144f5fc28Schuck {
82662ada8fSyamt struct vm_anon *anon;
8344f5fc28Schuck
84d18c6ca4Sad anon = pool_cache_get(&uvm_anon_cache, PR_NOWAIT);
85662ada8fSyamt if (anon) {
86662ada8fSyamt KASSERT(anon->an_ref == 0);
8739445b05Srmind KASSERT(anon->an_lock == NULL);
88662ada8fSyamt KASSERT(anon->an_page == NULL);
896fbf5bf6Syamt #if defined(VMSWAP)
90662ada8fSyamt KASSERT(anon->an_swslot == 0);
913aad734eSrmind #endif
92662ada8fSyamt anon->an_ref = 1;
9344f5fc28Schuck }
94662ada8fSyamt return anon;
9544f5fc28Schuck }
9644f5fc28Schuck
9744f5fc28Schuck /*
980622217aSad * uvm_anfree: free a single anon structure
9944f5fc28Schuck *
1003aad734eSrmind * => anon must be removed from the amap (if anon was in an amap).
1010622217aSad * => amap must be locked, if anon was owned by amap.
1020622217aSad * => we may drop and re-acquire the lock here (to break loans).
10344f5fc28Schuck */
1040622217aSad void
uvm_anfree(struct vm_anon * anon)1050622217aSad uvm_anfree(struct vm_anon *anon)
10644f5fc28Schuck {
1070622217aSad struct vm_page *pg = anon->an_page, *pg2 __diagused;
1083aad734eSrmind
109f3bd60e2Sskrll UVMHIST_FUNC(__func__);
110f3bd60e2Sskrll UVMHIST_CALLARGS(maphist,"(anon=%#jx)", (uintptr_t)anon, 0,0,0);
11144f5fc28Schuck
1120622217aSad KASSERT(anon->an_lock == NULL || rw_write_held(anon->an_lock));
1130622217aSad KASSERT(anon->an_ref == 0);
11413759f53Sthorpej
11544f5fc28Schuck /*
1160622217aSad * Dispose of the page, if it is resident.
11744f5fc28Schuck */
11844f5fc28Schuck
1190622217aSad if (__predict_true(pg != NULL)) {
120e225b7bdSrmind KASSERT(anon->an_lock != NULL);
12144f5fc28Schuck
12244f5fc28Schuck /*
123b33d8c36Sad * If there is a resident page and it is loaned, then anon
124b33d8c36Sad * may not own it. Call out to uvm_anon_lockloanpg() to
125b33d8c36Sad * identify and lock the real owner of the page.
126b33d8c36Sad */
127b33d8c36Sad
1280622217aSad if (__predict_false(pg->loan_count != 0)) {
1290622217aSad pg2 = uvm_anon_lockloanpg(anon);
1300622217aSad KASSERT(pg2 == pg);
131b33d8c36Sad }
132b33d8c36Sad
133b33d8c36Sad /*
1343aad734eSrmind * If the page is owned by a UVM object (now locked),
1353aad734eSrmind * then kill the loan on the page rather than free it,
1363aad734eSrmind * and release the object lock.
13744f5fc28Schuck */
13844f5fc28Schuck
1390622217aSad if (__predict_false(pg->uobject != NULL)) {
1405978ddc6Sad mutex_enter(&pg->interlock);
1412ed28d2cSchs KASSERT(pg->loan_count > 0);
14244f5fc28Schuck pg->loan_count--;
14344f5fc28Schuck pg->uanon = NULL;
1445978ddc6Sad mutex_exit(&pg->interlock);
145d2a0ebb6Sad rw_exit(pg->uobject->vmobjlock);
14644f5fc28Schuck } else {
14744f5fc28Schuck
14844f5fc28Schuck /*
1493aad734eSrmind * If page has no UVM object, then anon is the owner,
1503aad734eSrmind * and it is already locked.
15144f5fc28Schuck */
15244f5fc28Schuck
15364c6d1d2Schs KASSERT((pg->flags & PG_RELEASED) == 0);
154f3a668edSchs pmap_page_protect(pg, VM_PROT_NONE);
1558368dac6Syamt
1568368dac6Syamt /*
1573aad734eSrmind * If the page is busy, mark it as PG_RELEASED, so
1583aad734eSrmind * that uvm_anon_release(9) would release it later.
1598368dac6Syamt */
1608368dac6Syamt
1610622217aSad if (__predict_false((pg->flags & PG_BUSY) != 0)) {
1628368dac6Syamt pg->flags |= PG_RELEASED;
163d2a0ebb6Sad rw_obj_hold(anon->an_lock);
1640622217aSad return;
16564c6d1d2Schs }
1665978ddc6Sad uvm_pagefree(pg);
1674c1762c6Srin UVMHIST_LOG(maphist, "anon %#jx, page %#jx: "
1685978ddc6Sad "freed now!", (uintptr_t)anon, (uintptr_t)pg,
1695978ddc6Sad 0, 0);
17044f5fc28Schuck }
1710622217aSad } else {
1726fbf5bf6Syamt #if defined(VMSWAP)
1730622217aSad if (anon->an_swslot > 0) {
1743aad734eSrmind /* This page is no longer only in swap. */
1752ed88fe0Schs KASSERT(uvmexp.swpgonly > 0);
176221d5f98Sad atomic_dec_uint(&uvmexp.swpgonly);
1772ed88fe0Schs }
1783aad734eSrmind #endif
1790622217aSad }
1800622217aSad anon->an_lock = NULL;
18144f5fc28Schuck
1825978ddc6Sad /*
1835978ddc6Sad * Free any swap resources, leave a page replacement hint.
1845978ddc6Sad */
1855978ddc6Sad
1865978ddc6Sad uvm_anon_dropswap(anon);
1875978ddc6Sad uvmpdpol_anfree(anon);
18844f5fc28Schuck UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
18939445b05Srmind pool_cache_put(&uvm_anon_cache, anon);
19039445b05Srmind }
19139445b05Srmind
19239445b05Srmind /*
1933aad734eSrmind * uvm_anon_lockloanpg: given a locked anon, lock its resident page owner.
19444f5fc28Schuck *
19544f5fc28Schuck * => anon is locked by caller
19644f5fc28Schuck * => on return: anon is locked
19744f5fc28Schuck * if there is a resident page:
19844f5fc28Schuck * if it has a uobject, it is locked by us
19944f5fc28Schuck * if it is ownerless, we take over as owner
20044f5fc28Schuck * we return the resident page (it can change during
20144f5fc28Schuck * this function)
20244f5fc28Schuck * => note that the only time an anon has an ownerless resident page
20344f5fc28Schuck * is if the page was loaned from a uvm_object and the uvm_object
20444f5fc28Schuck * disowned it
20544f5fc28Schuck * => this only needs to be called when you want to do an operation
20644f5fc28Schuck * on an anon's resident page and that page has a non-zero loan
20744f5fc28Schuck * count.
20844f5fc28Schuck */
20944f5fc28Schuck struct vm_page *
uvm_anon_lockloanpg(struct vm_anon * anon)210e569faccSthorpej uvm_anon_lockloanpg(struct vm_anon *anon)
21144f5fc28Schuck {
21244f5fc28Schuck struct vm_page *pg;
213d2a0ebb6Sad krw_t op;
21444f5fc28Schuck
215d2a0ebb6Sad KASSERT(rw_lock_held(anon->an_lock));
21613759f53Sthorpej
21744f5fc28Schuck /*
21844f5fc28Schuck * loop while we have a resident page that has a non-zero loan count.
21944f5fc28Schuck * if we successfully get our lock, we will "break" the loop.
22044f5fc28Schuck * note that the test for pg->loan_count is not protected -- this
22144f5fc28Schuck * may produce false positive results. note that a false positive
22244f5fc28Schuck * result may cause us to do more work than we need to, but it will
22344f5fc28Schuck * not produce an incorrect result.
22444f5fc28Schuck */
22544f5fc28Schuck
226662ada8fSyamt while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
2275978ddc6Sad mutex_enter(&pg->interlock);
22844f5fc28Schuck if (pg->uobject) {
22944f5fc28Schuck /*
23044f5fc28Schuck * if we didn't get a lock (try lock failed), then we
23144f5fc28Schuck * toggle our anon lock and try again
23244f5fc28Schuck */
23344f5fc28Schuck
234d2a0ebb6Sad if (!rw_tryenter(pg->uobject->vmobjlock, RW_WRITER)) {
23544f5fc28Schuck /*
23644f5fc28Schuck * someone locking the object has a chance to
23744f5fc28Schuck * lock us right now
238e225b7bdSrmind *
239e225b7bdSrmind * XXX Better than yielding but inadequate.
24044f5fc28Schuck */
2415978ddc6Sad mutex_exit(&pg->interlock);
242bcbc27bbSad op = rw_lock_op(anon->an_lock);
243d2a0ebb6Sad rw_exit(anon->an_lock);
244d2a0ebb6Sad kpause("lkloanpg", false, 1, NULL);
245d2a0ebb6Sad rw_enter(anon->an_lock, op);
2462ed28d2cSchs continue;
24744f5fc28Schuck }
24844f5fc28Schuck }
24944f5fc28Schuck
25044f5fc28Schuck /*
2513aad734eSrmind * If page is un-owned i.e. the object dropped its ownership,
2523aad734eSrmind * then we have to take the ownership.
25344f5fc28Schuck */
25444f5fc28Schuck
2555978ddc6Sad if (pg->uobject == NULL && (pg->flags & PG_ANON) == 0) {
2565978ddc6Sad pg->flags |= PG_ANON;
25764c6d1d2Schs pg->loan_count--;
25844f5fc28Schuck }
2595978ddc6Sad mutex_exit(&pg->interlock);
26044f5fc28Schuck break;
26144f5fc28Schuck }
2623aad734eSrmind return pg;
26344f5fc28Schuck }
26416f0ca36Schs
2656fbf5bf6Syamt #if defined(VMSWAP)
2666fbf5bf6Syamt
26716f0ca36Schs /*
2683aad734eSrmind * uvm_anon_pagein: fetch an anon's page.
26916f0ca36Schs *
27016f0ca36Schs * => anon must be locked, and is unlocked upon return.
271b3667adaSthorpej * => returns true if pagein was aborted due to lack of memory.
27216f0ca36Schs */
27316f0ca36Schs
274712239e3Sthorpej bool
uvm_anon_pagein(struct vm_amap * amap,struct vm_anon * anon)2757be1d602Srmind uvm_anon_pagein(struct vm_amap *amap, struct vm_anon *anon)
27616f0ca36Schs {
27716f0ca36Schs struct vm_page *pg;
27816f0ca36Schs struct uvm_object *uobj;
27916f0ca36Schs
280d2a0ebb6Sad KASSERT(rw_write_held(anon->an_lock));
2817be1d602Srmind KASSERT(anon->an_lock == amap->am_lock);
28213759f53Sthorpej
283504f8894Sthorpej /*
2843aad734eSrmind * Get the page of the anon.
285504f8894Sthorpej */
28616f0ca36Schs
2877be1d602Srmind switch (uvmfault_anonget(NULL, amap, anon)) {
288dd82ad8eSchs case 0:
2893aad734eSrmind /* Success - we have the page. */
290d2a0ebb6Sad KASSERT(rw_write_held(anon->an_lock));
29116f0ca36Schs break;
292dd82ad8eSchs case EIO:
293dd82ad8eSchs case ERESTART:
29416f0ca36Schs /*
2953aad734eSrmind * Nothing more to do on errors. ERESTART means that the
2963aad734eSrmind * anon was freed.
29716f0ca36Schs */
298b3667adaSthorpej return false;
2991d7848adSad case ENOLCK:
3001d7848adSad panic("uvm_anon_pagein");
301d022b5caSpk default:
302b3667adaSthorpej return true;
30316f0ca36Schs }
30416f0ca36Schs
30516f0ca36Schs /*
3065972ba16Sad * Mark the page as dirty and clear its swslot.
30716f0ca36Schs */
30816f0ca36Schs
309662ada8fSyamt pg = anon->an_page;
31016f0ca36Schs uobj = pg->uobject;
3113aad734eSrmind if (anon->an_swslot > 0) {
31216f0ca36Schs uvm_swap_free(anon->an_swslot, 1);
3133aad734eSrmind }
31416f0ca36Schs anon->an_swslot = 0;
31505a3457eSad uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
31616f0ca36Schs
31716f0ca36Schs /*
3183aad734eSrmind * Deactivate the page (to put it on a page queue).
31916f0ca36Schs */
32016f0ca36Schs
32194843b13Sad uvm_pagelock(pg);
32216f0ca36Schs uvm_pagedeactivate(pg);
32394843b13Sad uvm_pageunlock(pg);
324d2a0ebb6Sad rw_exit(anon->an_lock);
32516f0ca36Schs if (uobj) {
326d2a0ebb6Sad rw_exit(uobj->vmobjlock);
32716f0ca36Schs }
328b3667adaSthorpej return false;
32916f0ca36Schs }
3308368dac6Syamt
3313aad734eSrmind /*
3323aad734eSrmind * uvm_anon_dropswap: release any swap resources from this anon.
3333aad734eSrmind *
3343aad734eSrmind * => anon must be locked or have a reference count of 0.
3353aad734eSrmind */
3363aad734eSrmind void
uvm_anon_dropswap(struct vm_anon * anon)3373aad734eSrmind uvm_anon_dropswap(struct vm_anon *anon)
3383aad734eSrmind {
339f3bd60e2Sskrll UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
3403aad734eSrmind
3413aad734eSrmind if (anon->an_swslot == 0)
3423aad734eSrmind return;
3433aad734eSrmind
3444c1762c6Srin UVMHIST_LOG(maphist,"freeing swap for anon %#jx, paged to swslot %#jx",
345cb32a134Spgoyette (uintptr_t)anon, anon->an_swslot, 0, 0);
3463aad734eSrmind uvm_swap_free(anon->an_swslot, 1);
3473aad734eSrmind anon->an_swslot = 0;
3483aad734eSrmind }
3493aad734eSrmind
3503aad734eSrmind #endif
3516fbf5bf6Syamt
3528368dac6Syamt /*
3538368dac6Syamt * uvm_anon_release: release an anon and its page.
3548368dac6Syamt *
3553aad734eSrmind * => anon should not have any references.
3563aad734eSrmind * => anon must be locked.
3578368dac6Syamt */
3588368dac6Syamt
3598368dac6Syamt void
uvm_anon_release(struct vm_anon * anon)360e569faccSthorpej uvm_anon_release(struct vm_anon *anon)
3618368dac6Syamt {
362662ada8fSyamt struct vm_page *pg = anon->an_page;
3630622217aSad krwlock_t *lock;
3648368dac6Syamt
365d2a0ebb6Sad KASSERT(rw_write_held(anon->an_lock));
3668368dac6Syamt KASSERT(pg != NULL);
3678368dac6Syamt KASSERT((pg->flags & PG_RELEASED) != 0);
3688368dac6Syamt KASSERT((pg->flags & PG_BUSY) != 0);
3698368dac6Syamt KASSERT(pg->uobject == NULL);
3708368dac6Syamt KASSERT(pg->uanon == anon);
3718368dac6Syamt KASSERT(pg->loan_count == 0);
3728368dac6Syamt KASSERT(anon->an_ref == 0);
3738368dac6Syamt
374*74c51b5dSchs if ((pg->flags & PG_PAGEOUT) != 0) {
375*74c51b5dSchs pg->flags &= ~PG_PAGEOUT;
376*74c51b5dSchs uvm_pageout_done(1);
377*74c51b5dSchs }
378*74c51b5dSchs
3798368dac6Syamt uvm_pagefree(pg);
380662ada8fSyamt KASSERT(anon->an_page == NULL);
3810622217aSad lock = anon->an_lock;
3820622217aSad uvm_anfree(anon);
3830622217aSad rw_exit(lock);
38439445b05Srmind /* Note: extra reference is held for PG_RELEASED case. */
3850622217aSad rw_obj_free(lock);
3868368dac6Syamt }
387