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/cdefs.h> 29c8d2ffd6SGleb Smirnoff __FBSDID("$FreeBSD$"); 30c8d2ffd6SGleb Smirnoff 31c8d2ffd6SGleb Smirnoff #include <sys/param.h> 32c8d2ffd6SGleb Smirnoff #include <sys/kernel.h> 33c8d2ffd6SGleb Smirnoff #include <sys/lock.h> 34c8d2ffd6SGleb Smirnoff #include <sys/malloc.h> 35c8d2ffd6SGleb Smirnoff #include <sys/mutex.h> 36c8d2ffd6SGleb Smirnoff #include <sys/sf_buf.h> 37c8d2ffd6SGleb Smirnoff #include <sys/smp.h> 38c8d2ffd6SGleb Smirnoff #include <sys/sysctl.h> 39c8d2ffd6SGleb Smirnoff 40c8d2ffd6SGleb Smirnoff #include <vm/vm.h> 41c8d2ffd6SGleb Smirnoff #include <vm/vm_extern.h> 42c8d2ffd6SGleb Smirnoff #include <vm/vm_page.h> 43c8d2ffd6SGleb Smirnoff 44c8d2ffd6SGleb Smirnoff #ifndef NSFBUFS 45c8d2ffd6SGleb Smirnoff #define NSFBUFS (512 + maxusers * 16) 46c8d2ffd6SGleb Smirnoff #endif 47c8d2ffd6SGleb Smirnoff 48c8d2ffd6SGleb Smirnoff static int nsfbufs; 49c8d2ffd6SGleb Smirnoff static int nsfbufspeak; 50c8d2ffd6SGleb Smirnoff static int nsfbufsused; 51c8d2ffd6SGleb Smirnoff 52c8d2ffd6SGleb Smirnoff SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0, 53c8d2ffd6SGleb Smirnoff "Maximum number of sendfile(2) sf_bufs available"); 54c8d2ffd6SGleb Smirnoff SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0, 55c8d2ffd6SGleb Smirnoff "Number of sendfile(2) sf_bufs at peak usage"); 56c8d2ffd6SGleb Smirnoff SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0, 57c8d2ffd6SGleb Smirnoff "Number of sendfile(2) sf_bufs in use"); 58c8d2ffd6SGleb Smirnoff 59c8d2ffd6SGleb Smirnoff static void sf_buf_init(void *arg); 60c8d2ffd6SGleb Smirnoff SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL); 61c8d2ffd6SGleb Smirnoff 62c8d2ffd6SGleb Smirnoff LIST_HEAD(sf_head, sf_buf); 63c8d2ffd6SGleb Smirnoff 64c8d2ffd6SGleb Smirnoff /* 65c8d2ffd6SGleb Smirnoff * A hash table of active sendfile(2) buffers 66c8d2ffd6SGleb Smirnoff */ 67c8d2ffd6SGleb Smirnoff static struct sf_head *sf_buf_active; 68c8d2ffd6SGleb Smirnoff static u_long sf_buf_hashmask; 69c8d2ffd6SGleb Smirnoff 70c8d2ffd6SGleb Smirnoff #define SF_BUF_HASH(m) (((m) - vm_page_array) & sf_buf_hashmask) 71c8d2ffd6SGleb Smirnoff 72c8d2ffd6SGleb Smirnoff static TAILQ_HEAD(, sf_buf) sf_buf_freelist; 73c8d2ffd6SGleb Smirnoff static u_int sf_buf_alloc_want; 74c8d2ffd6SGleb Smirnoff 75c8d2ffd6SGleb Smirnoff /* 76c8d2ffd6SGleb Smirnoff * A lock used to synchronize access to the hash table and free list 77c8d2ffd6SGleb Smirnoff */ 78c8d2ffd6SGleb Smirnoff static struct mtx sf_buf_lock; 79c8d2ffd6SGleb Smirnoff 80c8d2ffd6SGleb Smirnoff /* 81c8d2ffd6SGleb Smirnoff * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-)) 82c8d2ffd6SGleb Smirnoff */ 83c8d2ffd6SGleb Smirnoff static void 84c8d2ffd6SGleb Smirnoff sf_buf_init(void *arg) 85c8d2ffd6SGleb Smirnoff { 86c8d2ffd6SGleb Smirnoff struct sf_buf *sf_bufs; 87c8d2ffd6SGleb Smirnoff vm_offset_t sf_base; 88c8d2ffd6SGleb Smirnoff int i; 89c8d2ffd6SGleb Smirnoff 90c8d2ffd6SGleb Smirnoff #ifdef SFBUF_OPTIONAL_DIRECT_MAP 91c8d2ffd6SGleb Smirnoff if (SFBUF_OPTIONAL_DIRECT_MAP) 92c8d2ffd6SGleb Smirnoff return; 93c8d2ffd6SGleb Smirnoff #endif 94c8d2ffd6SGleb Smirnoff 95c8d2ffd6SGleb Smirnoff nsfbufs = NSFBUFS; 96c8d2ffd6SGleb Smirnoff TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs); 97c8d2ffd6SGleb Smirnoff 98c8d2ffd6SGleb Smirnoff sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask); 99c8d2ffd6SGleb Smirnoff TAILQ_INIT(&sf_buf_freelist); 100c8d2ffd6SGleb Smirnoff sf_base = kva_alloc(nsfbufs * PAGE_SIZE); 101c8d2ffd6SGleb Smirnoff sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, 102*eaf78ad3SGleb Smirnoff M_WAITOK | M_ZERO); 103c8d2ffd6SGleb Smirnoff for (i = 0; i < nsfbufs; i++) { 104c8d2ffd6SGleb Smirnoff sf_bufs[i].kva = sf_base + i * PAGE_SIZE; 105c8d2ffd6SGleb Smirnoff TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry); 106c8d2ffd6SGleb Smirnoff } 107c8d2ffd6SGleb Smirnoff sf_buf_alloc_want = 0; 108c8d2ffd6SGleb Smirnoff mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF); 109c8d2ffd6SGleb Smirnoff } 110c8d2ffd6SGleb Smirnoff 111c8d2ffd6SGleb Smirnoff /* 112c8d2ffd6SGleb Smirnoff * Get an sf_buf from the freelist. May block if none are available. 113c8d2ffd6SGleb Smirnoff */ 114c8d2ffd6SGleb Smirnoff struct sf_buf * 115c8d2ffd6SGleb Smirnoff sf_buf_alloc(struct vm_page *m, int flags) 116c8d2ffd6SGleb Smirnoff { 117c8d2ffd6SGleb Smirnoff struct sf_head *hash_list; 118c8d2ffd6SGleb Smirnoff struct sf_buf *sf; 119c8d2ffd6SGleb Smirnoff int error; 120c8d2ffd6SGleb Smirnoff 121c8d2ffd6SGleb Smirnoff #ifdef SFBUF_OPTIONAL_DIRECT_MAP 122c8d2ffd6SGleb Smirnoff if (SFBUF_OPTIONAL_DIRECT_MAP) 123c8d2ffd6SGleb Smirnoff return ((struct sf_buf *)m); 124c8d2ffd6SGleb Smirnoff #endif 125c8d2ffd6SGleb Smirnoff 126c8d2ffd6SGleb Smirnoff KASSERT(curthread->td_pinned > 0 || (flags & SFB_CPUPRIVATE) == 0, 127c8d2ffd6SGleb Smirnoff ("sf_buf_alloc(SFB_CPUPRIVATE): curthread not pinned")); 128c8d2ffd6SGleb Smirnoff hash_list = &sf_buf_active[SF_BUF_HASH(m)]; 129c8d2ffd6SGleb Smirnoff mtx_lock(&sf_buf_lock); 130c8d2ffd6SGleb Smirnoff LIST_FOREACH(sf, hash_list, list_entry) { 131c8d2ffd6SGleb Smirnoff if (sf->m == m) { 132c8d2ffd6SGleb Smirnoff sf->ref_count++; 133c8d2ffd6SGleb Smirnoff if (sf->ref_count == 1) { 134c8d2ffd6SGleb Smirnoff TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry); 135c8d2ffd6SGleb Smirnoff nsfbufsused++; 136c8d2ffd6SGleb Smirnoff nsfbufspeak = imax(nsfbufspeak, nsfbufsused); 137c8d2ffd6SGleb Smirnoff } 138c8d2ffd6SGleb Smirnoff #if defined(SMP) && defined(SFBUF_CPUSET) 139c8d2ffd6SGleb Smirnoff sf_buf_shootdown(sf, flags); 140c8d2ffd6SGleb Smirnoff #endif 141c8d2ffd6SGleb Smirnoff goto done; 142c8d2ffd6SGleb Smirnoff } 143c8d2ffd6SGleb Smirnoff } 144c8d2ffd6SGleb Smirnoff while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) { 145c8d2ffd6SGleb Smirnoff if (flags & SFB_NOWAIT) 146c8d2ffd6SGleb Smirnoff goto done; 147c8d2ffd6SGleb Smirnoff sf_buf_alloc_want++; 148c8d2ffd6SGleb Smirnoff SFSTAT_INC(sf_allocwait); 149c8d2ffd6SGleb Smirnoff error = msleep(&sf_buf_freelist, &sf_buf_lock, 150c8d2ffd6SGleb Smirnoff (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0); 151c8d2ffd6SGleb Smirnoff sf_buf_alloc_want--; 152c8d2ffd6SGleb Smirnoff 153c8d2ffd6SGleb Smirnoff /* 154c8d2ffd6SGleb Smirnoff * If we got a signal, don't risk going back to sleep. 155c8d2ffd6SGleb Smirnoff */ 156c8d2ffd6SGleb Smirnoff if (error) 157c8d2ffd6SGleb Smirnoff goto done; 158c8d2ffd6SGleb Smirnoff } 159c8d2ffd6SGleb Smirnoff TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry); 160c8d2ffd6SGleb Smirnoff if (sf->m != NULL) 161c8d2ffd6SGleb Smirnoff LIST_REMOVE(sf, list_entry); 162c8d2ffd6SGleb Smirnoff LIST_INSERT_HEAD(hash_list, sf, list_entry); 163c8d2ffd6SGleb Smirnoff sf->ref_count = 1; 164c8d2ffd6SGleb Smirnoff sf->m = m; 165c8d2ffd6SGleb Smirnoff nsfbufsused++; 166c8d2ffd6SGleb Smirnoff nsfbufspeak = imax(nsfbufspeak, nsfbufsused); 167c8d2ffd6SGleb Smirnoff sf_buf_map(sf, flags); 168c8d2ffd6SGleb Smirnoff done: 169c8d2ffd6SGleb Smirnoff mtx_unlock(&sf_buf_lock); 170c8d2ffd6SGleb Smirnoff return (sf); 171c8d2ffd6SGleb Smirnoff } 172c8d2ffd6SGleb Smirnoff 173c8d2ffd6SGleb Smirnoff /* 174c8d2ffd6SGleb Smirnoff * Remove a reference from the given sf_buf, adding it to the free 175c8d2ffd6SGleb Smirnoff * list when its reference count reaches zero. A freed sf_buf still, 176c8d2ffd6SGleb Smirnoff * however, retains its virtual-to-physical mapping until it is 177c8d2ffd6SGleb Smirnoff * recycled or reactivated by sf_buf_alloc(9). 178c8d2ffd6SGleb Smirnoff */ 179c8d2ffd6SGleb Smirnoff void 180c8d2ffd6SGleb Smirnoff sf_buf_free(struct sf_buf *sf) 181c8d2ffd6SGleb Smirnoff { 182c8d2ffd6SGleb Smirnoff 183c8d2ffd6SGleb Smirnoff #ifdef SFBUF_OPTIONAL_DIRECT_MAP 184c8d2ffd6SGleb Smirnoff if (SFBUF_OPTIONAL_DIRECT_MAP) 185c8d2ffd6SGleb Smirnoff return; 186c8d2ffd6SGleb Smirnoff #endif 187c8d2ffd6SGleb Smirnoff 188c8d2ffd6SGleb Smirnoff mtx_lock(&sf_buf_lock); 189c8d2ffd6SGleb Smirnoff sf->ref_count--; 190c8d2ffd6SGleb Smirnoff if (sf->ref_count == 0) { 191c8d2ffd6SGleb Smirnoff TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry); 192c8d2ffd6SGleb Smirnoff nsfbufsused--; 193c8d2ffd6SGleb Smirnoff if (sf_buf_unmap(sf)) { 194c8d2ffd6SGleb Smirnoff sf->m = NULL; 195c8d2ffd6SGleb Smirnoff LIST_REMOVE(sf, list_entry); 196c8d2ffd6SGleb Smirnoff } 197c8d2ffd6SGleb Smirnoff if (sf_buf_alloc_want > 0) 198c8d2ffd6SGleb Smirnoff wakeup(&sf_buf_freelist); 199c8d2ffd6SGleb Smirnoff } 200c8d2ffd6SGleb Smirnoff mtx_unlock(&sf_buf_lock); 201c8d2ffd6SGleb Smirnoff } 202c8d2ffd6SGleb Smirnoff 203818d40d0SGleb Smirnoff void 204818d40d0SGleb Smirnoff sf_buf_ref(struct sf_buf *sf) 205818d40d0SGleb Smirnoff { 206818d40d0SGleb Smirnoff 207818d40d0SGleb Smirnoff #ifdef SFBUF_OPTIONAL_DIRECT_MAP 208818d40d0SGleb Smirnoff if (SFBUF_OPTIONAL_DIRECT_MAP) 209818d40d0SGleb Smirnoff return; 210818d40d0SGleb Smirnoff #endif 211818d40d0SGleb Smirnoff 212818d40d0SGleb Smirnoff KASSERT(sf->ref_count > 0, ("%s: sf %p not allocated", __func__, sf)); 213818d40d0SGleb Smirnoff 214818d40d0SGleb Smirnoff mtx_lock(&sf_buf_lock); 215818d40d0SGleb Smirnoff sf->ref_count++; 216818d40d0SGleb Smirnoff mtx_unlock(&sf_buf_lock); 217818d40d0SGleb Smirnoff } 218818d40d0SGleb Smirnoff 219c8d2ffd6SGleb Smirnoff #ifdef SFBUF_PROCESS_PAGE 220c8d2ffd6SGleb Smirnoff /* 221c8d2ffd6SGleb Smirnoff * Run callback function on sf_buf that holds a certain page. 222c8d2ffd6SGleb Smirnoff */ 223c8d2ffd6SGleb Smirnoff boolean_t 224c8d2ffd6SGleb Smirnoff sf_buf_process_page(vm_page_t m, void (*cb)(struct sf_buf *)) 225c8d2ffd6SGleb Smirnoff { 226c8d2ffd6SGleb Smirnoff struct sf_head *hash_list; 227c8d2ffd6SGleb Smirnoff struct sf_buf *sf; 228c8d2ffd6SGleb Smirnoff 229c8d2ffd6SGleb Smirnoff hash_list = &sf_buf_active[SF_BUF_HASH(m)]; 230c8d2ffd6SGleb Smirnoff mtx_lock(&sf_buf_lock); 231c8d2ffd6SGleb Smirnoff LIST_FOREACH(sf, hash_list, list_entry) { 232c8d2ffd6SGleb Smirnoff if (sf->m == m) { 233c8d2ffd6SGleb Smirnoff cb(sf); 234c8d2ffd6SGleb Smirnoff mtx_unlock(&sf_buf_lock); 235c8d2ffd6SGleb Smirnoff return (TRUE); 236c8d2ffd6SGleb Smirnoff } 237c8d2ffd6SGleb Smirnoff } 238c8d2ffd6SGleb Smirnoff mtx_unlock(&sf_buf_lock); 239c8d2ffd6SGleb Smirnoff return (FALSE); 240c8d2ffd6SGleb Smirnoff } 241c8d2ffd6SGleb Smirnoff #endif /* SFBUF_PROCESS_PAGE */ 242