xref: /freebsd/sys/vm/sg_pager.c (revision e60f608eb9cf3b38099948545934d699de9bbcea)
101381811SJohn Baldwin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4179fa75eSJohn Baldwin  * Copyright (c) 2009 Hudson River Trading LLC
501381811SJohn Baldwin  * Written by: John H. Baldwin <jhb@FreeBSD.org>
601381811SJohn Baldwin  * All rights reserved.
701381811SJohn Baldwin  *
801381811SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
901381811SJohn Baldwin  * modification, are permitted provided that the following conditions
1001381811SJohn Baldwin  * are met:
1101381811SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
1201381811SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
1301381811SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
1401381811SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
1501381811SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
1601381811SJohn Baldwin  *
1701381811SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1801381811SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1901381811SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2001381811SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2101381811SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2201381811SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2301381811SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2401381811SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2501381811SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2601381811SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2701381811SJohn Baldwin  * SUCH DAMAGE.
2801381811SJohn Baldwin  */
2901381811SJohn Baldwin 
3001381811SJohn Baldwin #include <sys/cdefs.h>
3101381811SJohn Baldwin /*
3201381811SJohn Baldwin  * This pager manages OBJT_SG objects.  These objects are backed by
3301381811SJohn Baldwin  * a scatter/gather list of physical address ranges.
3401381811SJohn Baldwin  */
3501381811SJohn Baldwin 
3601381811SJohn Baldwin #include <sys/param.h>
37*e60f608eSKonstantin Belousov #include <sys/event.h>
3801381811SJohn Baldwin #include <sys/lock.h>
3901381811SJohn Baldwin #include <sys/mutex.h>
4089f6b863SAttilio Rao #include <sys/rwlock.h>
4101381811SJohn Baldwin #include <sys/sglist.h>
4200a3fe96SKonstantin Belousov #include <sys/user.h>
439ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
449ed01c32SGleb Smirnoff 
4501381811SJohn Baldwin #include <vm/vm.h>
461c771f92SKonstantin Belousov #include <vm/vm_param.h>
4701381811SJohn Baldwin #include <vm/vm_object.h>
4801381811SJohn Baldwin #include <vm/vm_page.h>
4901381811SJohn Baldwin #include <vm/vm_pager.h>
5043f48b65SKonstantin Belousov #include <vm/vm_phys.h>
5101381811SJohn Baldwin #include <vm/uma.h>
5201381811SJohn Baldwin 
5301381811SJohn Baldwin static vm_object_t sg_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
5401381811SJohn Baldwin     vm_ooffset_t, struct ucred *);
5501381811SJohn Baldwin static void sg_pager_dealloc(vm_object_t);
56b0cd2017SGleb Smirnoff static int sg_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
5701381811SJohn Baldwin static void sg_pager_putpages(vm_object_t, vm_page_t *, int,
58f74be55eSDimitry Andric 		int, int *);
5901381811SJohn Baldwin static boolean_t sg_pager_haspage(vm_object_t, vm_pindex_t, int *,
6001381811SJohn Baldwin 		int *);
6101381811SJohn Baldwin 
62d474440aSKonstantin Belousov const struct pagerops sgpagerops = {
6300a3fe96SKonstantin Belousov 	.pgo_kvme_type = KVME_TYPE_SG,
6401381811SJohn Baldwin 	.pgo_alloc =	sg_pager_alloc,
6501381811SJohn Baldwin 	.pgo_dealloc =	sg_pager_dealloc,
6601381811SJohn Baldwin 	.pgo_getpages =	sg_pager_getpages,
6701381811SJohn Baldwin 	.pgo_putpages =	sg_pager_putpages,
6801381811SJohn Baldwin 	.pgo_haspage =	sg_pager_haspage,
6901381811SJohn Baldwin };
7001381811SJohn Baldwin 
7101381811SJohn Baldwin static vm_object_t
sg_pager_alloc(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred)7201381811SJohn Baldwin sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
7301381811SJohn Baldwin     vm_ooffset_t foff, struct ucred *cred)
7401381811SJohn Baldwin {
7501381811SJohn Baldwin 	struct sglist *sg;
7601381811SJohn Baldwin 	vm_object_t object;
7701381811SJohn Baldwin 	vm_pindex_t npages, pindex;
7801381811SJohn Baldwin 	int i;
7901381811SJohn Baldwin 
8001381811SJohn Baldwin 	/*
8101381811SJohn Baldwin 	 * Offset should be page aligned.
8201381811SJohn Baldwin 	 */
8301381811SJohn Baldwin 	if (foff & PAGE_MASK)
8401381811SJohn Baldwin 		return (NULL);
8501381811SJohn Baldwin 
8601381811SJohn Baldwin 	/*
8701381811SJohn Baldwin 	 * The scatter/gather list must only include page-aligned
8801381811SJohn Baldwin 	 * ranges.
8901381811SJohn Baldwin 	 */
9001381811SJohn Baldwin 	npages = 0;
9101381811SJohn Baldwin 	sg = handle;
9201381811SJohn Baldwin 	for (i = 0; i < sg->sg_nseg; i++) {
9301381811SJohn Baldwin 		if ((sg->sg_segs[i].ss_paddr % PAGE_SIZE) != 0 ||
9401381811SJohn Baldwin 		    (sg->sg_segs[i].ss_len % PAGE_SIZE) != 0)
9501381811SJohn Baldwin 			return (NULL);
9601381811SJohn Baldwin 		npages += sg->sg_segs[i].ss_len / PAGE_SIZE;
9701381811SJohn Baldwin 	}
9801381811SJohn Baldwin 
9901381811SJohn Baldwin 	/*
10001381811SJohn Baldwin 	 * The scatter/gather list has a fixed size.  Refuse requests
10101381811SJohn Baldwin 	 * to map beyond that.
10201381811SJohn Baldwin 	 */
10301381811SJohn Baldwin 	size = round_page(size);
10410d9120cSKonstantin Belousov 	pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
10510d9120cSKonstantin Belousov 	if (pindex > npages || pindex < OFF_TO_IDX(foff) ||
10610d9120cSKonstantin Belousov 	    pindex < OFF_TO_IDX(size))
10701381811SJohn Baldwin 		return (NULL);
10801381811SJohn Baldwin 
10901381811SJohn Baldwin 	/*
11001381811SJohn Baldwin 	 * Allocate a new object and associate it with the
11101381811SJohn Baldwin 	 * scatter/gather list.  It is ok for our purposes to have
11201381811SJohn Baldwin 	 * multiple VM objects associated with the same scatter/gather
11301381811SJohn Baldwin 	 * list because scatter/gather lists are static.  This is also
11401381811SJohn Baldwin 	 * simpler than ensuring a unique object per scatter/gather
11501381811SJohn Baldwin 	 * list.
11601381811SJohn Baldwin 	 */
11701381811SJohn Baldwin 	object = vm_object_allocate(OBJT_SG, npages);
11801381811SJohn Baldwin 	object->handle = sglist_hold(sg);
11901381811SJohn Baldwin 	TAILQ_INIT(&object->un_pager.sgp.sgp_pglist);
12001381811SJohn Baldwin 	return (object);
12101381811SJohn Baldwin }
12201381811SJohn Baldwin 
12301381811SJohn Baldwin static void
sg_pager_dealloc(vm_object_t object)12401381811SJohn Baldwin sg_pager_dealloc(vm_object_t object)
12501381811SJohn Baldwin {
12601381811SJohn Baldwin 	struct sglist *sg;
12701381811SJohn Baldwin 	vm_page_t m;
12801381811SJohn Baldwin 
12901381811SJohn Baldwin 	/*
13001381811SJohn Baldwin 	 * Free up our fake pages.
13101381811SJohn Baldwin 	 */
13201381811SJohn Baldwin 	while ((m = TAILQ_FIRST(&object->un_pager.sgp.sgp_pglist)) != 0) {
1333cf3b4e6SJeff Roberson 		if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
1343cf3b4e6SJeff Roberson 			continue;
135c325e866SKonstantin Belousov 		TAILQ_REMOVE(&object->un_pager.sgp.sgp_pglist, m, plinks.q);
13610cf2560SAlan Cox 		vm_page_putfake(m);
13701381811SJohn Baldwin 	}
13801381811SJohn Baldwin 
13901381811SJohn Baldwin 	sg = object->handle;
14001381811SJohn Baldwin 	sglist_free(sg);
141e735691bSJohn Baldwin 	object->handle = NULL;
142e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
14301381811SJohn Baldwin }
14401381811SJohn Baldwin 
14501381811SJohn Baldwin static int
sg_pager_getpages(vm_object_t object,vm_page_t * m,int count,int * rbehind,int * rahead)146b0cd2017SGleb Smirnoff sg_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
147b0cd2017SGleb Smirnoff     int *rahead)
14801381811SJohn Baldwin {
14901381811SJohn Baldwin 	struct sglist *sg;
15001381811SJohn Baldwin 	vm_page_t m_paddr, page;
15101381811SJohn Baldwin 	vm_pindex_t offset;
15201381811SJohn Baldwin 	vm_paddr_t paddr;
15301381811SJohn Baldwin 	vm_memattr_t memattr;
15401381811SJohn Baldwin 	size_t space;
15501381811SJohn Baldwin 	int i;
15601381811SJohn Baldwin 
157b0cd2017SGleb Smirnoff 	/* Since our haspage reports zero after/before, the count is 1. */
158b0cd2017SGleb Smirnoff 	KASSERT(count == 1, ("%s: count %d", __func__, count));
159d6e13f3bSJeff Roberson 	/* Handle is stable while paging is in progress. */
16001381811SJohn Baldwin 	sg = object->handle;
16101381811SJohn Baldwin 	memattr = object->memattr;
162b0cd2017SGleb Smirnoff 	offset = m[0]->pindex;
16301381811SJohn Baldwin 
16401381811SJohn Baldwin 	/*
16501381811SJohn Baldwin 	 * Lookup the physical address of the requested page.  An initial
16601381811SJohn Baldwin 	 * value of '1' instead of '0' is used so we can assert that the
16701381811SJohn Baldwin 	 * page is found since '0' can be a valid page-aligned physical
16801381811SJohn Baldwin 	 * address.
16901381811SJohn Baldwin 	 */
17001381811SJohn Baldwin 	space = 0;
17101381811SJohn Baldwin 	paddr = 1;
17201381811SJohn Baldwin 	for (i = 0; i < sg->sg_nseg; i++) {
17301381811SJohn Baldwin 		if (space + sg->sg_segs[i].ss_len <= (offset * PAGE_SIZE)) {
17401381811SJohn Baldwin 			space += sg->sg_segs[i].ss_len;
17501381811SJohn Baldwin 			continue;
17601381811SJohn Baldwin 		}
17701381811SJohn Baldwin 		paddr = sg->sg_segs[i].ss_paddr + offset * PAGE_SIZE - space;
17801381811SJohn Baldwin 		break;
17901381811SJohn Baldwin 	}
18001381811SJohn Baldwin 	KASSERT(paddr != 1, ("invalid SG page index"));
18101381811SJohn Baldwin 
18201381811SJohn Baldwin 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
18301381811SJohn Baldwin 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
18401381811SJohn Baldwin 	    pmap_page_get_memattr(m_paddr) != memattr) {
18501381811SJohn Baldwin 		memattr = pmap_page_get_memattr(m_paddr);
18601381811SJohn Baldwin 		printf(
18701381811SJohn Baldwin 	    "WARNING: A device driver has set \"memattr\" inconsistently.\n");
18801381811SJohn Baldwin 	}
18901381811SJohn Baldwin 
19001381811SJohn Baldwin 	/* Return a fake page for the requested page. */
191b0cd2017SGleb Smirnoff 	KASSERT(!(m[0]->flags & PG_FICTITIOUS),
19201381811SJohn Baldwin 	    ("backing page for SG is fake"));
19301381811SJohn Baldwin 
19401381811SJohn Baldwin 	/* Construct a new fake page. */
19510cf2560SAlan Cox 	page = vm_page_getfake(paddr, memattr);
19689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
197c325e866SKonstantin Belousov 	TAILQ_INSERT_TAIL(&object->un_pager.sgp.sgp_pglist, page, plinks.q);
1983cf3b4e6SJeff Roberson 	vm_page_replace(page, object, offset, m[0]);
199d6e13f3bSJeff Roberson 	VM_OBJECT_WUNLOCK(object);
200b0cd2017SGleb Smirnoff 	m[0] = page;
2010012f373SJeff Roberson 	vm_page_valid(page);
20201381811SJohn Baldwin 
203b0cd2017SGleb Smirnoff 	if (rbehind)
204b0cd2017SGleb Smirnoff 		*rbehind = 0;
205b0cd2017SGleb Smirnoff 	if (rahead)
206b0cd2017SGleb Smirnoff 		*rahead = 0;
207b0cd2017SGleb Smirnoff 
20801381811SJohn Baldwin 	return (VM_PAGER_OK);
20901381811SJohn Baldwin }
21001381811SJohn Baldwin 
21101381811SJohn Baldwin static void
sg_pager_putpages(vm_object_t object,vm_page_t * m,int count,int flags,int * rtvals)21201381811SJohn Baldwin sg_pager_putpages(vm_object_t object, vm_page_t *m, int count,
213f74be55eSDimitry Andric     int flags, int *rtvals)
21401381811SJohn Baldwin {
21501381811SJohn Baldwin 
21601381811SJohn Baldwin 	panic("sg_pager_putpage called");
21701381811SJohn Baldwin }
21801381811SJohn Baldwin 
21901381811SJohn Baldwin static boolean_t
sg_pager_haspage(vm_object_t object,vm_pindex_t pindex,int * before,int * after)22001381811SJohn Baldwin sg_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
22101381811SJohn Baldwin     int *after)
22201381811SJohn Baldwin {
22301381811SJohn Baldwin 
22401381811SJohn Baldwin 	if (before != NULL)
22501381811SJohn Baldwin 		*before = 0;
22601381811SJohn Baldwin 	if (after != NULL)
22701381811SJohn Baldwin 		*after = 0;
22801381811SJohn Baldwin 	return (TRUE);
22901381811SJohn Baldwin }
230