1 /* 2 * Copyright (c) 1998 David Greenman. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $DragonFly: src/sys/kern/kern_sfbuf.c,v 1.15 2007/04/30 07:18:53 dillon Exp $ 26 */ 27 28 #include <sys/param.h> 29 #include <sys/types.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 #include <sys/malloc.h> 33 #include <sys/sfbuf.h> 34 #include <sys/objcache.h> 35 36 #include <cpu/lwbuf.h> 37 38 #include <vm/vm.h> 39 #include <vm/vm_extern.h> 40 #include <vm/vm_kern.h> 41 #include <vm/vm_page.h> 42 #include <vm/pmap.h> 43 44 static void sf_buf_init(void *arg); 45 SYSINIT(sock_sf, SI_BOOT2_MACHDEP, SI_ORDER_ANY, sf_buf_init, NULL) 46 47 LIST_HEAD(sf_buf_list, sf_buf); 48 49 static struct objcache *sf_buf_cache; 50 51 MALLOC_DEFINE(M_SFBUF, "sfbuf", "Sendfile buffer structures"); 52 struct objcache_malloc_args sf_buf_malloc_args = { sizeof(struct sf_buf), M_SFBUF }; 53 54 55 static boolean_t 56 sf_buf_cache_ctor(void *obj, void *pdata, int ocflags) 57 { 58 struct sf_buf *sf = (struct sf_buf *)obj; 59 60 sf->lwbuf = NULL; 61 sf->refs = 0; 62 63 return (TRUE); 64 } 65 66 /* 67 * Init objcache of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-)) 68 */ 69 static void 70 sf_buf_init(void *arg) 71 { 72 sf_buf_cache = objcache_create("sf_buf", 0, 0, 73 sf_buf_cache_ctor, NULL, NULL, 74 objcache_malloc_alloc, objcache_malloc_free, 75 &sf_buf_malloc_args); 76 } 77 78 /* 79 * Acquire an sf_buf reference for a vm_page. 80 */ 81 struct sf_buf * 82 sf_buf_alloc(struct vm_page *m) 83 { 84 struct sf_buf *sf; 85 86 if ((sf = objcache_get(sf_buf_cache, M_WAITOK)) == NULL) 87 goto done; 88 89 if ((sf->lwbuf = lwbuf_alloc(m)) == NULL) { 90 objcache_put(sf_buf_cache, sf); 91 sf = NULL; 92 goto done; 93 } 94 95 /* 96 * Force invalidation of the TLB entry on all CPU's 97 */ 98 lwbuf_set_global(sf->lwbuf); 99 100 sf->refs = 1; 101 102 done: 103 return (sf); 104 } 105 106 void 107 sf_buf_ref(void *arg) 108 { 109 struct sf_buf *sf = arg; 110 111 atomic_add_int(&sf->refs, 1); 112 } 113 114 /* 115 * Detach mapped page and release resources back to the system. 116 * 117 * Must be called at splimp. 118 */ 119 int 120 sf_buf_free(void *arg) 121 { 122 struct sf_buf *sf = arg; 123 124 KKASSERT(sf->refs > 0); 125 if (atomic_fetchadd_int(&sf->refs, -1) == 1) { 126 lwbuf_free(sf->lwbuf); 127 objcache_put(sf_buf_cache, sf); 128 return (0); 129 } 130 131 return (1); 132 } 133