1269577Sglebius/*-
2269577Sglebius * Copyright (c) 2014 Gleb Smirnoff <glebius@FreeBSD.org>
3269577Sglebius * Copyright (c) 2003, 2005 Alan L. Cox <alc@cs.rice.edu>
4269577Sglebius * All rights reserved.
5269577Sglebius *
6269577Sglebius * Redistribution and use in source and binary forms, with or without
7269577Sglebius * modification, are permitted provided that the following conditions
8269577Sglebius * are met:
9269577Sglebius * 1. Redistributions of source code must retain the above copyright
10269577Sglebius *    notice, this list of conditions and the following disclaimer.
11269577Sglebius * 2. Redistributions in binary form must reproduce the above copyright
12269577Sglebius *    notice, this list of conditions and the following disclaimer in the
13269577Sglebius *    documentation and/or other materials provided with the distribution.
14269577Sglebius *
15269577Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16269577Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17269577Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18269577Sglebius * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19269577Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20269577Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21269577Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22269577Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23269577Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24269577Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25269577Sglebius * SUCH DAMAGE.
26269577Sglebius */
27269577Sglebius
28269577Sglebius#include <sys/cdefs.h>
29269577Sglebius__FBSDID("$FreeBSD: stable/11/sys/kern/subr_sfbuf.c 313989 2017-02-20 10:44:24Z kib $");
30269577Sglebius
31269577Sglebius#include <sys/param.h>
32269577Sglebius#include <sys/kernel.h>
33269577Sglebius#include <sys/lock.h>
34269577Sglebius#include <sys/malloc.h>
35269577Sglebius#include <sys/mutex.h>
36313989Skib#include <sys/proc.h>
37269577Sglebius#include <sys/sf_buf.h>
38269577Sglebius#include <sys/smp.h>
39269577Sglebius#include <sys/sysctl.h>
40269577Sglebius
41269577Sglebius#include <vm/vm.h>
42269577Sglebius#include <vm/vm_extern.h>
43269577Sglebius#include <vm/vm_page.h>
44269577Sglebius
45269577Sglebius#ifndef NSFBUFS
46269577Sglebius#define	NSFBUFS		(512 + maxusers * 16)
47269577Sglebius#endif
48269577Sglebius
49269577Sglebiusstatic int nsfbufs;
50269577Sglebiusstatic int nsfbufspeak;
51269577Sglebiusstatic int nsfbufsused;
52269577Sglebius
53269577SglebiusSYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
54269577Sglebius    "Maximum number of sendfile(2) sf_bufs available");
55269577SglebiusSYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
56269577Sglebius    "Number of sendfile(2) sf_bufs at peak usage");
57269577SglebiusSYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
58269577Sglebius    "Number of sendfile(2) sf_bufs in use");
59269577Sglebius
60269577Sglebiusstatic void	sf_buf_init(void *arg);
61269577SglebiusSYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
62269577Sglebius
63269577SglebiusLIST_HEAD(sf_head, sf_buf);
64269577Sglebius
65269577Sglebius/*
66269577Sglebius * A hash table of active sendfile(2) buffers
67269577Sglebius */
68269577Sglebiusstatic struct sf_head *sf_buf_active;
69269577Sglebiusstatic u_long sf_buf_hashmask;
70269577Sglebius
71269577Sglebius#define	SF_BUF_HASH(m)	(((m) - vm_page_array) & sf_buf_hashmask)
72269577Sglebius
73269577Sglebiusstatic TAILQ_HEAD(, sf_buf) sf_buf_freelist;
74269577Sglebiusstatic u_int	sf_buf_alloc_want;
75269577Sglebius
76269577Sglebius/*
77269577Sglebius * A lock used to synchronize access to the hash table and free list
78269577Sglebius */
79269577Sglebiusstatic struct mtx sf_buf_lock;
80269577Sglebius
81269577Sglebius/*
82269577Sglebius * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
83269577Sglebius */
84269577Sglebiusstatic void
85269577Sglebiussf_buf_init(void *arg)
86269577Sglebius{
87269577Sglebius	struct sf_buf *sf_bufs;
88269577Sglebius	vm_offset_t sf_base;
89269577Sglebius	int i;
90269577Sglebius
91269577Sglebius#ifdef SFBUF_OPTIONAL_DIRECT_MAP
92269577Sglebius	if (SFBUF_OPTIONAL_DIRECT_MAP)
93269577Sglebius		return;
94269577Sglebius#endif
95269577Sglebius
96269577Sglebius	nsfbufs = NSFBUFS;
97269577Sglebius	TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
98269577Sglebius
99269577Sglebius	sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
100269577Sglebius	TAILQ_INIT(&sf_buf_freelist);
101269577Sglebius	sf_base = kva_alloc(nsfbufs * PAGE_SIZE);
102269577Sglebius	sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
103269808Sglebius	    M_WAITOK | M_ZERO);
104269577Sglebius	for (i = 0; i < nsfbufs; i++) {
105269577Sglebius		sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
106269577Sglebius		TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
107269577Sglebius	}
108269577Sglebius	sf_buf_alloc_want = 0;
109269577Sglebius	mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
110269577Sglebius}
111269577Sglebius
112269577Sglebius/*
113269577Sglebius * Get an sf_buf from the freelist.  May block if none are available.
114269577Sglebius */
115269577Sglebiusstruct sf_buf *
116269577Sglebiussf_buf_alloc(struct vm_page *m, int flags)
117269577Sglebius{
118269577Sglebius	struct sf_head *hash_list;
119269577Sglebius	struct sf_buf *sf;
120269577Sglebius	int error;
121269577Sglebius
122269577Sglebius#ifdef SFBUF_OPTIONAL_DIRECT_MAP
123269577Sglebius	if (SFBUF_OPTIONAL_DIRECT_MAP)
124269577Sglebius		return ((struct sf_buf *)m);
125269577Sglebius#endif
126269577Sglebius
127269577Sglebius	KASSERT(curthread->td_pinned > 0 || (flags & SFB_CPUPRIVATE) == 0,
128269577Sglebius	    ("sf_buf_alloc(SFB_CPUPRIVATE): curthread not pinned"));
129269577Sglebius	hash_list = &sf_buf_active[SF_BUF_HASH(m)];
130269577Sglebius	mtx_lock(&sf_buf_lock);
131269577Sglebius	LIST_FOREACH(sf, hash_list, list_entry) {
132269577Sglebius		if (sf->m == m) {
133269577Sglebius			sf->ref_count++;
134269577Sglebius			if (sf->ref_count == 1) {
135269577Sglebius				TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
136269577Sglebius				nsfbufsused++;
137269577Sglebius				nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
138269577Sglebius			}
139269577Sglebius#if defined(SMP) && defined(SFBUF_CPUSET)
140269577Sglebius			sf_buf_shootdown(sf, flags);
141269577Sglebius#endif
142269577Sglebius			goto done;
143269577Sglebius		}
144269577Sglebius	}
145269577Sglebius	while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
146269577Sglebius		if (flags & SFB_NOWAIT)
147269577Sglebius			goto done;
148269577Sglebius		sf_buf_alloc_want++;
149269577Sglebius		SFSTAT_INC(sf_allocwait);
150269577Sglebius		error = msleep(&sf_buf_freelist, &sf_buf_lock,
151269577Sglebius		    (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
152269577Sglebius		sf_buf_alloc_want--;
153269577Sglebius
154269577Sglebius		/*
155269577Sglebius		 * If we got a signal, don't risk going back to sleep.
156269577Sglebius		 */
157269577Sglebius		if (error)
158269577Sglebius			goto done;
159269577Sglebius	}
160269577Sglebius	TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
161269577Sglebius	if (sf->m != NULL)
162269577Sglebius		LIST_REMOVE(sf, list_entry);
163269577Sglebius	LIST_INSERT_HEAD(hash_list, sf, list_entry);
164269577Sglebius	sf->ref_count = 1;
165269577Sglebius	sf->m = m;
166269577Sglebius	nsfbufsused++;
167269577Sglebius	nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
168269577Sglebius	sf_buf_map(sf, flags);
169269577Sglebiusdone:
170269577Sglebius	mtx_unlock(&sf_buf_lock);
171269577Sglebius	return (sf);
172269577Sglebius}
173269577Sglebius
174269577Sglebius/*
175269577Sglebius * Remove a reference from the given sf_buf, adding it to the free
176269577Sglebius * list when its reference count reaches zero.  A freed sf_buf still,
177269577Sglebius * however, retains its virtual-to-physical mapping until it is
178269577Sglebius * recycled or reactivated by sf_buf_alloc(9).
179269577Sglebius */
180269577Sglebiusvoid
181269577Sglebiussf_buf_free(struct sf_buf *sf)
182269577Sglebius{
183269577Sglebius
184269577Sglebius#ifdef SFBUF_OPTIONAL_DIRECT_MAP
185269577Sglebius	if (SFBUF_OPTIONAL_DIRECT_MAP)
186269577Sglebius		return;
187269577Sglebius#endif
188269577Sglebius
189269577Sglebius	mtx_lock(&sf_buf_lock);
190269577Sglebius	sf->ref_count--;
191269577Sglebius	if (sf->ref_count == 0) {
192269577Sglebius		TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
193269577Sglebius		nsfbufsused--;
194269577Sglebius		if (sf_buf_unmap(sf)) {
195269577Sglebius			sf->m = NULL;
196269577Sglebius			LIST_REMOVE(sf, list_entry);
197269577Sglebius		}
198269577Sglebius		if (sf_buf_alloc_want > 0)
199269577Sglebius			wakeup(&sf_buf_freelist);
200269577Sglebius	}
201269577Sglebius	mtx_unlock(&sf_buf_lock);
202269577Sglebius}
203269577Sglebius
204269807Sglebiusvoid
205269807Sglebiussf_buf_ref(struct sf_buf *sf)
206269807Sglebius{
207269807Sglebius
208269807Sglebius#ifdef SFBUF_OPTIONAL_DIRECT_MAP
209269807Sglebius	if (SFBUF_OPTIONAL_DIRECT_MAP)
210269807Sglebius		return;
211269807Sglebius#endif
212269807Sglebius
213269813Sglebius	mtx_lock(&sf_buf_lock);
214269807Sglebius	KASSERT(sf->ref_count > 0, ("%s: sf %p not allocated", __func__, sf));
215269807Sglebius	sf->ref_count++;
216269807Sglebius	mtx_unlock(&sf_buf_lock);
217269807Sglebius}
218269807Sglebius
219269577Sglebius#ifdef SFBUF_PROCESS_PAGE
220269577Sglebius/*
221269577Sglebius * Run callback function on sf_buf that holds a certain page.
222269577Sglebius */
223269577Sglebiusboolean_t
224269577Sglebiussf_buf_process_page(vm_page_t m, void (*cb)(struct sf_buf *))
225269577Sglebius{
226269577Sglebius	struct sf_head *hash_list;
227269577Sglebius	struct sf_buf *sf;
228269577Sglebius
229269577Sglebius	hash_list = &sf_buf_active[SF_BUF_HASH(m)];
230269577Sglebius	mtx_lock(&sf_buf_lock);
231269577Sglebius	LIST_FOREACH(sf, hash_list, list_entry) {
232269577Sglebius		if (sf->m == m) {
233269577Sglebius			cb(sf);
234269577Sglebius			mtx_unlock(&sf_buf_lock);
235269577Sglebius			return (TRUE);
236269577Sglebius		}
237269577Sglebius	}
238269577Sglebius	mtx_unlock(&sf_buf_lock);
239269577Sglebius	return (FALSE);
240269577Sglebius}
241269577Sglebius#endif	/* SFBUF_PROCESS_PAGE */
242