1c8d2ffd6SGleb Smirnoff /*-
2c8d2ffd6SGleb Smirnoff * Copyright (c) 2014 Gleb Smirnoff <glebius@FreeBSD.org>
3c8d2ffd6SGleb Smirnoff * Copyright (c) 2003, 2005 Alan L. Cox <alc@cs.rice.edu>
4c8d2ffd6SGleb Smirnoff * All rights reserved.
5c8d2ffd6SGleb Smirnoff *
6c8d2ffd6SGleb Smirnoff * Redistribution and use in source and binary forms, with or without
7c8d2ffd6SGleb Smirnoff * modification, are permitted provided that the following conditions
8c8d2ffd6SGleb Smirnoff * are met:
9c8d2ffd6SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
10c8d2ffd6SGleb Smirnoff * notice, this list of conditions and the following disclaimer.
11c8d2ffd6SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
12c8d2ffd6SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
13c8d2ffd6SGleb Smirnoff * documentation and/or other materials provided with the distribution.
14c8d2ffd6SGleb Smirnoff *
15c8d2ffd6SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c8d2ffd6SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c8d2ffd6SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c8d2ffd6SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c8d2ffd6SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c8d2ffd6SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c8d2ffd6SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c8d2ffd6SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c8d2ffd6SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c8d2ffd6SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c8d2ffd6SGleb Smirnoff * SUCH DAMAGE.
26c8d2ffd6SGleb Smirnoff */
27c8d2ffd6SGleb Smirnoff
28c8d2ffd6SGleb Smirnoff #include <sys/param.h>
29c8d2ffd6SGleb Smirnoff #include <sys/kernel.h>
30c8d2ffd6SGleb Smirnoff #include <sys/lock.h>
31c8d2ffd6SGleb Smirnoff #include <sys/malloc.h>
32c8d2ffd6SGleb Smirnoff #include <sys/mutex.h>
33beaa6e1eSEmmanuel Vadot #include <sys/proc.h>
34c8d2ffd6SGleb Smirnoff #include <sys/sf_buf.h>
35c8d2ffd6SGleb Smirnoff #include <sys/smp.h>
36c8d2ffd6SGleb Smirnoff #include <sys/sysctl.h>
37c8d2ffd6SGleb Smirnoff
38c8d2ffd6SGleb Smirnoff #include <vm/vm.h>
39c8d2ffd6SGleb Smirnoff #include <vm/vm_extern.h>
40c8d2ffd6SGleb Smirnoff #include <vm/vm_page.h>
41c8d2ffd6SGleb Smirnoff
42c8d2ffd6SGleb Smirnoff #ifndef NSFBUFS
43c8d2ffd6SGleb Smirnoff #define NSFBUFS (512 + maxusers * 16)
44c8d2ffd6SGleb Smirnoff #endif
45c8d2ffd6SGleb Smirnoff
46c8d2ffd6SGleb Smirnoff static int nsfbufs;
47c8d2ffd6SGleb Smirnoff static int nsfbufspeak;
48c8d2ffd6SGleb Smirnoff static int nsfbufsused;
49c8d2ffd6SGleb Smirnoff
50c8d2ffd6SGleb Smirnoff SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
51c8d2ffd6SGleb Smirnoff "Maximum number of sendfile(2) sf_bufs available");
52c8d2ffd6SGleb Smirnoff SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
53c8d2ffd6SGleb Smirnoff "Number of sendfile(2) sf_bufs at peak usage");
54c8d2ffd6SGleb Smirnoff SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
55c8d2ffd6SGleb Smirnoff "Number of sendfile(2) sf_bufs in use");
56c8d2ffd6SGleb Smirnoff
57c8d2ffd6SGleb Smirnoff static void sf_buf_init(void *arg);
58c8d2ffd6SGleb Smirnoff SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
59c8d2ffd6SGleb Smirnoff
60c8d2ffd6SGleb Smirnoff LIST_HEAD(sf_head, sf_buf);
61c8d2ffd6SGleb Smirnoff
62c8d2ffd6SGleb Smirnoff /*
63c8d2ffd6SGleb Smirnoff * A hash table of active sendfile(2) buffers
64c8d2ffd6SGleb Smirnoff */
65c8d2ffd6SGleb Smirnoff static struct sf_head *sf_buf_active;
66c8d2ffd6SGleb Smirnoff static u_long sf_buf_hashmask;
67c8d2ffd6SGleb Smirnoff
68c8d2ffd6SGleb Smirnoff #define SF_BUF_HASH(m) (((m) - vm_page_array) & sf_buf_hashmask)
69c8d2ffd6SGleb Smirnoff
70c8d2ffd6SGleb Smirnoff static TAILQ_HEAD(, sf_buf) sf_buf_freelist;
71c8d2ffd6SGleb Smirnoff static u_int sf_buf_alloc_want;
72c8d2ffd6SGleb Smirnoff
73c8d2ffd6SGleb Smirnoff /*
74c8d2ffd6SGleb Smirnoff * A lock used to synchronize access to the hash table and free list
75c8d2ffd6SGleb Smirnoff */
76c8d2ffd6SGleb Smirnoff static struct mtx sf_buf_lock;
77c8d2ffd6SGleb Smirnoff
78c8d2ffd6SGleb Smirnoff /*
79c8d2ffd6SGleb Smirnoff * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
80c8d2ffd6SGleb Smirnoff */
81c8d2ffd6SGleb Smirnoff static void
sf_buf_init(void * arg)82c8d2ffd6SGleb Smirnoff sf_buf_init(void *arg)
83c8d2ffd6SGleb Smirnoff {
84c8d2ffd6SGleb Smirnoff struct sf_buf *sf_bufs;
85c8d2ffd6SGleb Smirnoff vm_offset_t sf_base;
86c8d2ffd6SGleb Smirnoff int i;
87c8d2ffd6SGleb Smirnoff
88*9a8196ceSNathan Whitehorn if (PMAP_HAS_DMAP)
89c8d2ffd6SGleb Smirnoff return;
90c8d2ffd6SGleb Smirnoff
91c8d2ffd6SGleb Smirnoff nsfbufs = NSFBUFS;
92c8d2ffd6SGleb Smirnoff TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
93c8d2ffd6SGleb Smirnoff
94c8d2ffd6SGleb Smirnoff sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
95c8d2ffd6SGleb Smirnoff TAILQ_INIT(&sf_buf_freelist);
96c8d2ffd6SGleb Smirnoff sf_base = kva_alloc(nsfbufs * PAGE_SIZE);
97c8d2ffd6SGleb Smirnoff sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
98eaf78ad3SGleb Smirnoff M_WAITOK | M_ZERO);
99c8d2ffd6SGleb Smirnoff for (i = 0; i < nsfbufs; i++) {
100c8d2ffd6SGleb Smirnoff sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
101c8d2ffd6SGleb Smirnoff TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
102c8d2ffd6SGleb Smirnoff }
103c8d2ffd6SGleb Smirnoff sf_buf_alloc_want = 0;
104c8d2ffd6SGleb Smirnoff mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
105c8d2ffd6SGleb Smirnoff }
106c8d2ffd6SGleb Smirnoff
107c8d2ffd6SGleb Smirnoff /*
108c8d2ffd6SGleb Smirnoff * Get an sf_buf from the freelist. May block if none are available.
109c8d2ffd6SGleb Smirnoff */
110c8d2ffd6SGleb Smirnoff struct sf_buf *
sf_buf_alloc(struct vm_page * m,int flags)111c8d2ffd6SGleb Smirnoff sf_buf_alloc(struct vm_page *m, int flags)
112c8d2ffd6SGleb Smirnoff {
113c8d2ffd6SGleb Smirnoff struct sf_head *hash_list;
114c8d2ffd6SGleb Smirnoff struct sf_buf *sf;
115c8d2ffd6SGleb Smirnoff int error;
116c8d2ffd6SGleb Smirnoff
117*9a8196ceSNathan Whitehorn if (PMAP_HAS_DMAP)
118c8d2ffd6SGleb Smirnoff return ((struct sf_buf *)m);
119c8d2ffd6SGleb Smirnoff
120c8d2ffd6SGleb Smirnoff KASSERT(curthread->td_pinned > 0 || (flags & SFB_CPUPRIVATE) == 0,
121c8d2ffd6SGleb Smirnoff ("sf_buf_alloc(SFB_CPUPRIVATE): curthread not pinned"));
122c8d2ffd6SGleb Smirnoff hash_list = &sf_buf_active[SF_BUF_HASH(m)];
123c8d2ffd6SGleb Smirnoff mtx_lock(&sf_buf_lock);
124c8d2ffd6SGleb Smirnoff LIST_FOREACH(sf, hash_list, list_entry) {
125c8d2ffd6SGleb Smirnoff if (sf->m == m) {
126c8d2ffd6SGleb Smirnoff sf->ref_count++;
127c8d2ffd6SGleb Smirnoff if (sf->ref_count == 1) {
128c8d2ffd6SGleb Smirnoff TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
129c8d2ffd6SGleb Smirnoff nsfbufsused++;
130c8d2ffd6SGleb Smirnoff nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
131c8d2ffd6SGleb Smirnoff }
132c8d2ffd6SGleb Smirnoff #if defined(SMP) && defined(SFBUF_CPUSET)
133c8d2ffd6SGleb Smirnoff sf_buf_shootdown(sf, flags);
134c8d2ffd6SGleb Smirnoff #endif
135c8d2ffd6SGleb Smirnoff goto done;
136c8d2ffd6SGleb Smirnoff }
137c8d2ffd6SGleb Smirnoff }
138c8d2ffd6SGleb Smirnoff while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
139c8d2ffd6SGleb Smirnoff if (flags & SFB_NOWAIT)
140c8d2ffd6SGleb Smirnoff goto done;
141c8d2ffd6SGleb Smirnoff sf_buf_alloc_want++;
142c8d2ffd6SGleb Smirnoff SFSTAT_INC(sf_allocwait);
143c8d2ffd6SGleb Smirnoff error = msleep(&sf_buf_freelist, &sf_buf_lock,
144c8d2ffd6SGleb Smirnoff (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
145c8d2ffd6SGleb Smirnoff sf_buf_alloc_want--;
146c8d2ffd6SGleb Smirnoff
147c8d2ffd6SGleb Smirnoff /*
148c8d2ffd6SGleb Smirnoff * If we got a signal, don't risk going back to sleep.
149c8d2ffd6SGleb Smirnoff */
150c8d2ffd6SGleb Smirnoff if (error)
151c8d2ffd6SGleb Smirnoff goto done;
152c8d2ffd6SGleb Smirnoff }
153c8d2ffd6SGleb Smirnoff TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
154c8d2ffd6SGleb Smirnoff if (sf->m != NULL)
155c8d2ffd6SGleb Smirnoff LIST_REMOVE(sf, list_entry);
156c8d2ffd6SGleb Smirnoff LIST_INSERT_HEAD(hash_list, sf, list_entry);
157c8d2ffd6SGleb Smirnoff sf->ref_count = 1;
158c8d2ffd6SGleb Smirnoff sf->m = m;
159c8d2ffd6SGleb Smirnoff nsfbufsused++;
160c8d2ffd6SGleb Smirnoff nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
161c8d2ffd6SGleb Smirnoff sf_buf_map(sf, flags);
162c8d2ffd6SGleb Smirnoff done:
163c8d2ffd6SGleb Smirnoff mtx_unlock(&sf_buf_lock);
164c8d2ffd6SGleb Smirnoff return (sf);
165c8d2ffd6SGleb Smirnoff }
166c8d2ffd6SGleb Smirnoff
167c8d2ffd6SGleb Smirnoff /*
168c8d2ffd6SGleb Smirnoff * Remove a reference from the given sf_buf, adding it to the free
169c8d2ffd6SGleb Smirnoff * list when its reference count reaches zero. A freed sf_buf still,
170c8d2ffd6SGleb Smirnoff * however, retains its virtual-to-physical mapping until it is
171c8d2ffd6SGleb Smirnoff * recycled or reactivated by sf_buf_alloc(9).
172c8d2ffd6SGleb Smirnoff */
173c8d2ffd6SGleb Smirnoff void
sf_buf_free(struct sf_buf * sf)174c8d2ffd6SGleb Smirnoff sf_buf_free(struct sf_buf *sf)
175c8d2ffd6SGleb Smirnoff {
176c8d2ffd6SGleb Smirnoff
177*9a8196ceSNathan Whitehorn if (PMAP_HAS_DMAP)
178c8d2ffd6SGleb Smirnoff return;
179c8d2ffd6SGleb Smirnoff
180c8d2ffd6SGleb Smirnoff mtx_lock(&sf_buf_lock);
181c8d2ffd6SGleb Smirnoff sf->ref_count--;
182c8d2ffd6SGleb Smirnoff if (sf->ref_count == 0) {
183c8d2ffd6SGleb Smirnoff TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
184c8d2ffd6SGleb Smirnoff nsfbufsused--;
185c8d2ffd6SGleb Smirnoff if (sf_buf_unmap(sf)) {
186c8d2ffd6SGleb Smirnoff sf->m = NULL;
187c8d2ffd6SGleb Smirnoff LIST_REMOVE(sf, list_entry);
188c8d2ffd6SGleb Smirnoff }
189c8d2ffd6SGleb Smirnoff if (sf_buf_alloc_want > 0)
190c8d2ffd6SGleb Smirnoff wakeup(&sf_buf_freelist);
191c8d2ffd6SGleb Smirnoff }
192c8d2ffd6SGleb Smirnoff mtx_unlock(&sf_buf_lock);
193c8d2ffd6SGleb Smirnoff }
194c8d2ffd6SGleb Smirnoff
195818d40d0SGleb Smirnoff void
sf_buf_ref(struct sf_buf * sf)196818d40d0SGleb Smirnoff sf_buf_ref(struct sf_buf *sf)
197818d40d0SGleb Smirnoff {
198818d40d0SGleb Smirnoff
199*9a8196ceSNathan Whitehorn if (PMAP_HAS_DMAP)
200818d40d0SGleb Smirnoff return;
201818d40d0SGleb Smirnoff
202818d40d0SGleb Smirnoff mtx_lock(&sf_buf_lock);
203cd1692faSGleb Smirnoff KASSERT(sf->ref_count > 0, ("%s: sf %p not allocated", __func__, sf));
204818d40d0SGleb Smirnoff sf->ref_count++;
205818d40d0SGleb Smirnoff mtx_unlock(&sf_buf_lock);
206818d40d0SGleb Smirnoff }
207818d40d0SGleb Smirnoff
208c8d2ffd6SGleb Smirnoff #ifdef SFBUF_PROCESS_PAGE
209c8d2ffd6SGleb Smirnoff /*
210c8d2ffd6SGleb Smirnoff * Run callback function on sf_buf that holds a certain page.
211c8d2ffd6SGleb Smirnoff */
212c8d2ffd6SGleb Smirnoff boolean_t
sf_buf_process_page(vm_page_t m,void (* cb)(struct sf_buf *))213c8d2ffd6SGleb Smirnoff sf_buf_process_page(vm_page_t m, void (*cb)(struct sf_buf *))
214c8d2ffd6SGleb Smirnoff {
215c8d2ffd6SGleb Smirnoff struct sf_head *hash_list;
216c8d2ffd6SGleb Smirnoff struct sf_buf *sf;
217c8d2ffd6SGleb Smirnoff
218c8d2ffd6SGleb Smirnoff hash_list = &sf_buf_active[SF_BUF_HASH(m)];
219c8d2ffd6SGleb Smirnoff mtx_lock(&sf_buf_lock);
220c8d2ffd6SGleb Smirnoff LIST_FOREACH(sf, hash_list, list_entry) {
221c8d2ffd6SGleb Smirnoff if (sf->m == m) {
222c8d2ffd6SGleb Smirnoff cb(sf);
223c8d2ffd6SGleb Smirnoff mtx_unlock(&sf_buf_lock);
224c8d2ffd6SGleb Smirnoff return (TRUE);
225c8d2ffd6SGleb Smirnoff }
226c8d2ffd6SGleb Smirnoff }
227c8d2ffd6SGleb Smirnoff mtx_unlock(&sf_buf_lock);
228c8d2ffd6SGleb Smirnoff return (FALSE);
229c8d2ffd6SGleb Smirnoff }
230c8d2ffd6SGleb Smirnoff #endif /* SFBUF_PROCESS_PAGE */
231