xref: /dragonfly/sys/kern/kern_sfbuf.c (revision bbb35c81)
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 
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
31 #include <sys/sfbuf.h>
32 #include <sys/refcount.h>
33 #include <sys/objcache.h>
34 
35 #include <cpu/lwbuf.h>
36 
37 #include <vm/vm.h>
38 #include <vm/vm_extern.h>
39 #include <vm/vm_kern.h>
40 #include <vm/vm_page.h>
41 #include <vm/pmap.h>
42 
43 static void sf_buf_init(void *arg);
44 SYSINIT(sock_sf, SI_BOOT2_MACHDEP, SI_ORDER_ANY, sf_buf_init, NULL);
45 
46 static struct objcache *sf_buf_cache;
47 
48 MALLOC_DEFINE(M_SFBUF, "sfbuf", "Sendfile buffer structures");
49 struct objcache_malloc_args sf_buf_malloc_args = { sizeof(struct sf_buf), M_SFBUF };
50 
51 
52 static boolean_t
53 sf_buf_cache_ctor(void *obj, void *pdata, int ocflags)
54 {
55 	struct sf_buf *sf = (struct sf_buf *)obj;
56 
57 	sf->lwbuf = NULL;
58 	refcount_init(&sf->ref, 0);
59 
60 	return (TRUE);
61 }
62 
63 /*
64  * Init objcache of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
65  */
66 static void
67 sf_buf_init(void *arg)
68 {
69 	sf_buf_cache = objcache_create("sf_buf", 0, 0,
70 		sf_buf_cache_ctor, NULL, NULL,
71 		objcache_malloc_alloc, objcache_malloc_free,
72 		&sf_buf_malloc_args);
73 }
74 
75 /*
76  * Acquire an sf_buf reference for a vm_page.
77  */
78 struct sf_buf *
79 sf_buf_alloc(struct vm_page *m)
80 {
81 	struct sf_buf *sf;
82 
83 	if ((sf = objcache_get(sf_buf_cache, M_WAITOK)) == NULL)
84 		goto done;
85 
86 	if ((sf->lwbuf = lwbuf_alloc(m, &sf->lwbuf_cache)) == NULL) {
87 		objcache_put(sf_buf_cache, sf);
88 		sf = NULL;
89 		goto done;
90 	}
91 
92 	/*
93 	 * Force invalidation of the TLB entry on all CPU's
94 	 */
95 	lwbuf_set_global(sf->lwbuf);
96 
97 	refcount_init(&sf->ref, 1);
98 
99 done:
100 	return (sf);
101 }
102 
103 void
104 sf_buf_ref(void *arg)
105 {
106 	struct sf_buf *sf = arg;
107 
108 	refcount_acquire(&sf->ref);
109 }
110 
111 /*
112  * Detach mapped page and release resources back to the system.
113  *
114  * Returns non-zero (TRUE) if this was the last release of the sfbuf.
115  *
116  * (matching the same API that refcount_release() uses to reduce confusion)
117  */
118 int
119 sf_buf_free(void *arg)
120 {
121 	struct sf_buf *sf = arg;
122 
123 	if (refcount_release(&sf->ref)) {
124 		lwbuf_free(sf->lwbuf);
125 		objcache_put(sf_buf_cache, sf);
126 		return (1);
127 	}
128 	return (0);
129 }
130