linux_sync_file.c revision 1.2 1 1.2 thorpej /* $NetBSD: linux_sync_file.c,v 1.2 2022/02/12 15:51:29 thorpej Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/cdefs.h>
30 1.2 thorpej __KERNEL_RCSID(0, "$NetBSD: linux_sync_file.c,v 1.2 2022/02/12 15:51:29 thorpej Exp $");
31 1.1 riastrad
32 1.1 riastrad #include <sys/event.h>
33 1.1 riastrad #include <sys/fcntl.h>
34 1.1 riastrad #include <sys/file.h>
35 1.1 riastrad #include <sys/filedesc.h>
36 1.1 riastrad #include <sys/kmem.h>
37 1.1 riastrad #include <sys/mutex.h>
38 1.1 riastrad #include <sys/poll.h>
39 1.1 riastrad #include <sys/select.h>
40 1.1 riastrad #include <sys/queue.h>
41 1.1 riastrad
42 1.1 riastrad #include <linux/dma-fence.h>
43 1.1 riastrad #include <linux/sync_file.h>
44 1.1 riastrad
45 1.1 riastrad static const struct fileops sync_file_ops;
46 1.1 riastrad
47 1.1 riastrad struct sync_file *
48 1.1 riastrad sync_file_create(struct dma_fence *fence, struct file *fp)
49 1.1 riastrad {
50 1.1 riastrad struct sync_file *sf;
51 1.1 riastrad
52 1.1 riastrad sf = kmem_zalloc(sizeof(*sf), KM_SLEEP);
53 1.1 riastrad sf->file = fp;
54 1.1 riastrad sf->sf_fence = dma_fence_get(fence);
55 1.1 riastrad mutex_init(&sf->sf_lock, MUTEX_DEFAULT, IPL_VM);
56 1.1 riastrad selinit(&sf->sf_selq);
57 1.1 riastrad sf->sf_polling = false;
58 1.1 riastrad sf->sf_signalled = false;
59 1.1 riastrad
60 1.1 riastrad fp->f_type = DTYPE_MISC;
61 1.1 riastrad fp->f_flag = FREAD | FWRITE;
62 1.1 riastrad fp->f_ops = &sync_file_ops;
63 1.1 riastrad
64 1.1 riastrad return sf;
65 1.1 riastrad }
66 1.1 riastrad
67 1.1 riastrad static int
68 1.1 riastrad sync_file_close(struct file *fp)
69 1.1 riastrad {
70 1.1 riastrad struct sync_file *sf = fp->f_data;
71 1.1 riastrad
72 1.1 riastrad if (sf->sf_polling)
73 1.1 riastrad dma_fence_remove_callback(sf->sf_fence, &sf->sf_fcb);
74 1.1 riastrad dma_fence_put(sf->sf_fence);
75 1.1 riastrad sf->sf_fence = NULL;
76 1.1 riastrad
77 1.1 riastrad kmem_free(sf, sizeof(*sf));
78 1.1 riastrad
79 1.1 riastrad return 0;
80 1.1 riastrad }
81 1.1 riastrad
82 1.1 riastrad static void
83 1.1 riastrad sync_file_fence_cb(struct dma_fence *fence, struct dma_fence_cb *fcb)
84 1.1 riastrad {
85 1.1 riastrad struct sync_file *sf = container_of(fcb, struct sync_file, sf_fcb);
86 1.1 riastrad
87 1.1 riastrad mutex_enter(&sf->sf_lock);
88 1.1 riastrad sf->sf_signalled = true;
89 1.1 riastrad selnotify(&sf->sf_selq, POLLIN, NOTE_SUBMIT);
90 1.1 riastrad mutex_exit(&sf->sf_lock);
91 1.1 riastrad }
92 1.1 riastrad
93 1.1 riastrad static int
94 1.1 riastrad sync_file_poll(struct file *fp, int events)
95 1.1 riastrad {
96 1.1 riastrad struct sync_file *sf = fp->f_data;
97 1.1 riastrad int revents = 0;
98 1.1 riastrad int ret;
99 1.1 riastrad
100 1.1 riastrad if ((events & POLLIN) == 0)
101 1.1 riastrad return 0;
102 1.1 riastrad
103 1.1 riastrad mutex_enter(&sf->sf_lock);
104 1.1 riastrad if (sf->sf_signalled) {
105 1.1 riastrad revents |= POLLIN;
106 1.1 riastrad } else if (sf->sf_polling) {
107 1.1 riastrad selrecord(curlwp, &sf->sf_selq);
108 1.1 riastrad } else {
109 1.1 riastrad sf->sf_polling = true;
110 1.1 riastrad mutex_exit(&sf->sf_lock);
111 1.1 riastrad ret = dma_fence_add_callback(sf->sf_fence, &sf->sf_fcb,
112 1.1 riastrad sync_file_fence_cb);
113 1.1 riastrad mutex_enter(&sf->sf_lock);
114 1.1 riastrad if (ret < 0) {
115 1.1 riastrad sf->sf_signalled = true;
116 1.1 riastrad selnotify(&sf->sf_selq, POLLIN, NOTE_SUBMIT);
117 1.1 riastrad revents |= POLLIN;
118 1.1 riastrad } else {
119 1.1 riastrad selrecord(curlwp, &sf->sf_selq);
120 1.1 riastrad }
121 1.1 riastrad }
122 1.1 riastrad mutex_exit(&sf->sf_lock);
123 1.1 riastrad
124 1.1 riastrad return revents;
125 1.1 riastrad }
126 1.1 riastrad
127 1.1 riastrad static const struct filterops sync_file_filtops;
128 1.1 riastrad
129 1.1 riastrad static int
130 1.1 riastrad sync_file_kqfilter(struct file *fp, struct knote *kn)
131 1.1 riastrad {
132 1.1 riastrad struct sync_file *sf = fp->f_data;
133 1.1 riastrad
134 1.1 riastrad switch (kn->kn_filter) {
135 1.1 riastrad case EVFILT_READ:
136 1.1 riastrad kn->kn_fop = &sync_file_filtops;
137 1.1 riastrad kn->kn_hook = sf;
138 1.1 riastrad mutex_enter(&sf->sf_lock);
139 1.2 thorpej klist_insert(&sf->sf_selq.sel_klist, kn);
140 1.1 riastrad mutex_exit(&sf->sf_lock);
141 1.1 riastrad return 0;
142 1.1 riastrad default:
143 1.1 riastrad return EINVAL;
144 1.1 riastrad }
145 1.1 riastrad }
146 1.1 riastrad
147 1.1 riastrad static void
148 1.1 riastrad filt_sync_file_detach(struct knote *kn)
149 1.1 riastrad {
150 1.1 riastrad struct sync_file *sf = kn->kn_hook;
151 1.1 riastrad
152 1.1 riastrad mutex_enter(&sf->sf_lock);
153 1.2 thorpej klist_remove(&sf->sf_selq.sel_klist, kn);
154 1.1 riastrad mutex_exit(&sf->sf_lock);
155 1.1 riastrad }
156 1.1 riastrad
157 1.1 riastrad static int
158 1.1 riastrad filt_sync_file_event(struct knote *kn, long hint)
159 1.1 riastrad {
160 1.1 riastrad struct sync_file *sf = kn->kn_hook;
161 1.1 riastrad int ret;
162 1.1 riastrad
163 1.1 riastrad if (hint == NOTE_SUBMIT)
164 1.1 riastrad KASSERT(mutex_owned(&sf->sf_lock));
165 1.1 riastrad else
166 1.1 riastrad mutex_enter(&sf->sf_lock);
167 1.1 riastrad
168 1.1 riastrad if (sf->sf_signalled) {
169 1.1 riastrad kn->kn_data = 0; /* XXX Does this work?? */
170 1.1 riastrad ret = 1;
171 1.1 riastrad } else if (sf->sf_polling) {
172 1.1 riastrad ret = 0;
173 1.1 riastrad } else {
174 1.1 riastrad sf->sf_polling = true;
175 1.1 riastrad mutex_exit(&sf->sf_lock);
176 1.1 riastrad ret = dma_fence_add_callback(sf->sf_fence, &sf->sf_fcb,
177 1.1 riastrad sync_file_fence_cb);
178 1.1 riastrad mutex_enter(&sf->sf_lock);
179 1.1 riastrad if (ret < 0) {
180 1.1 riastrad sf->sf_signalled = true;
181 1.1 riastrad selnotify(&sf->sf_selq, POLLIN, NOTE_SUBMIT);
182 1.1 riastrad kn->kn_data = 0;
183 1.1 riastrad ret = 1;
184 1.1 riastrad } else {
185 1.1 riastrad selrecord(curlwp, &sf->sf_selq);
186 1.1 riastrad ret = 0;
187 1.1 riastrad }
188 1.1 riastrad }
189 1.1 riastrad
190 1.1 riastrad if (hint == NOTE_SUBMIT)
191 1.1 riastrad KASSERT(mutex_owned(&sf->sf_lock));
192 1.1 riastrad else
193 1.1 riastrad mutex_exit(&sf->sf_lock);
194 1.1 riastrad
195 1.1 riastrad return ret;
196 1.1 riastrad }
197 1.1 riastrad
198 1.1 riastrad static const struct filterops sync_file_filtops = {
199 1.1 riastrad .f_flags = FILTEROP_ISFD,
200 1.1 riastrad .f_attach = NULL,
201 1.1 riastrad .f_detach = filt_sync_file_detach,
202 1.1 riastrad .f_event = filt_sync_file_event,
203 1.1 riastrad };
204 1.1 riastrad
205 1.1 riastrad struct dma_fence *
206 1.1 riastrad sync_file_get_fence(int fd)
207 1.1 riastrad {
208 1.1 riastrad struct file *fp;
209 1.1 riastrad struct sync_file *sf;
210 1.1 riastrad struct dma_fence *fence;
211 1.1 riastrad
212 1.1 riastrad if ((fp = fd_getfile(fd)) == NULL)
213 1.1 riastrad return NULL;
214 1.1 riastrad sf = fp->f_data;
215 1.1 riastrad fence = dma_fence_get(sf->sf_fence);
216 1.1 riastrad fd_putfile(fd);
217 1.1 riastrad
218 1.1 riastrad return fence;
219 1.1 riastrad }
220 1.1 riastrad
221 1.1 riastrad static const struct fileops sync_file_ops = {
222 1.1 riastrad .fo_name = "linux_sync_file",
223 1.1 riastrad .fo_read = fbadop_read,
224 1.1 riastrad .fo_write = fbadop_write,
225 1.1 riastrad .fo_ioctl = fbadop_ioctl,
226 1.1 riastrad .fo_fcntl = fnullop_fcntl,
227 1.1 riastrad .fo_poll = sync_file_poll,
228 1.1 riastrad .fo_stat = fbadop_stat, /* XXX */
229 1.1 riastrad .fo_close = sync_file_close,
230 1.1 riastrad .fo_kqfilter = sync_file_kqfilter,
231 1.1 riastrad .fo_restart = fnullop_restart,
232 1.1 riastrad .fo_mmap = NULL,
233 1.1 riastrad };
234