linux_sync_file.c revision 1.3 1 1.3 riastrad /* $NetBSD: linux_sync_file.c,v 1.3 2024/04/28 15:35:39 riastradh 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.3 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_sync_file.c,v 1.3 2024/04/28 15:35:39 riastradh 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.3 riastrad
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.3 riastrad sf->sf_fence = dma_fence_get(fence);
60 1.1 riastrad
61 1.1 riastrad fp->f_type = DTYPE_MISC;
62 1.1 riastrad fp->f_flag = FREAD | FWRITE;
63 1.1 riastrad fp->f_ops = &sync_file_ops;
64 1.3 riastrad fp->f_data = sf;
65 1.1 riastrad
66 1.1 riastrad return sf;
67 1.1 riastrad }
68 1.1 riastrad
69 1.1 riastrad static int
70 1.1 riastrad sync_file_close(struct file *fp)
71 1.1 riastrad {
72 1.1 riastrad struct sync_file *sf = fp->f_data;
73 1.1 riastrad
74 1.1 riastrad if (sf->sf_polling)
75 1.1 riastrad dma_fence_remove_callback(sf->sf_fence, &sf->sf_fcb);
76 1.1 riastrad dma_fence_put(sf->sf_fence);
77 1.1 riastrad sf->sf_fence = NULL;
78 1.3 riastrad seldestroy(&sf->sf_selq);
79 1.3 riastrad mutex_destroy(&sf->sf_lock);
80 1.1 riastrad
81 1.1 riastrad kmem_free(sf, sizeof(*sf));
82 1.1 riastrad
83 1.1 riastrad return 0;
84 1.1 riastrad }
85 1.1 riastrad
86 1.1 riastrad static void
87 1.1 riastrad sync_file_fence_cb(struct dma_fence *fence, struct dma_fence_cb *fcb)
88 1.1 riastrad {
89 1.1 riastrad struct sync_file *sf = container_of(fcb, struct sync_file, sf_fcb);
90 1.1 riastrad
91 1.1 riastrad mutex_enter(&sf->sf_lock);
92 1.1 riastrad sf->sf_signalled = true;
93 1.1 riastrad selnotify(&sf->sf_selq, POLLIN, NOTE_SUBMIT);
94 1.1 riastrad mutex_exit(&sf->sf_lock);
95 1.1 riastrad }
96 1.1 riastrad
97 1.1 riastrad static int
98 1.1 riastrad sync_file_poll(struct file *fp, int events)
99 1.1 riastrad {
100 1.1 riastrad struct sync_file *sf = fp->f_data;
101 1.1 riastrad int revents = 0;
102 1.1 riastrad int ret;
103 1.1 riastrad
104 1.1 riastrad if ((events & POLLIN) == 0)
105 1.1 riastrad return 0;
106 1.1 riastrad
107 1.1 riastrad mutex_enter(&sf->sf_lock);
108 1.1 riastrad if (sf->sf_signalled) {
109 1.1 riastrad revents |= POLLIN;
110 1.1 riastrad } else if (sf->sf_polling) {
111 1.1 riastrad selrecord(curlwp, &sf->sf_selq);
112 1.1 riastrad } else {
113 1.1 riastrad sf->sf_polling = true;
114 1.1 riastrad mutex_exit(&sf->sf_lock);
115 1.1 riastrad ret = dma_fence_add_callback(sf->sf_fence, &sf->sf_fcb,
116 1.1 riastrad sync_file_fence_cb);
117 1.1 riastrad mutex_enter(&sf->sf_lock);
118 1.1 riastrad if (ret < 0) {
119 1.1 riastrad sf->sf_signalled = true;
120 1.1 riastrad selnotify(&sf->sf_selq, POLLIN, NOTE_SUBMIT);
121 1.1 riastrad revents |= POLLIN;
122 1.1 riastrad } else {
123 1.1 riastrad selrecord(curlwp, &sf->sf_selq);
124 1.1 riastrad }
125 1.1 riastrad }
126 1.1 riastrad mutex_exit(&sf->sf_lock);
127 1.1 riastrad
128 1.1 riastrad return revents;
129 1.1 riastrad }
130 1.1 riastrad
131 1.1 riastrad static const struct filterops sync_file_filtops;
132 1.1 riastrad
133 1.1 riastrad static int
134 1.1 riastrad sync_file_kqfilter(struct file *fp, struct knote *kn)
135 1.1 riastrad {
136 1.1 riastrad struct sync_file *sf = fp->f_data;
137 1.1 riastrad
138 1.1 riastrad switch (kn->kn_filter) {
139 1.1 riastrad case EVFILT_READ:
140 1.1 riastrad kn->kn_fop = &sync_file_filtops;
141 1.1 riastrad kn->kn_hook = sf;
142 1.1 riastrad mutex_enter(&sf->sf_lock);
143 1.2 thorpej klist_insert(&sf->sf_selq.sel_klist, kn);
144 1.1 riastrad mutex_exit(&sf->sf_lock);
145 1.1 riastrad return 0;
146 1.1 riastrad default:
147 1.1 riastrad return EINVAL;
148 1.1 riastrad }
149 1.1 riastrad }
150 1.1 riastrad
151 1.1 riastrad static void
152 1.1 riastrad filt_sync_file_detach(struct knote *kn)
153 1.1 riastrad {
154 1.1 riastrad struct sync_file *sf = kn->kn_hook;
155 1.1 riastrad
156 1.1 riastrad mutex_enter(&sf->sf_lock);
157 1.2 thorpej klist_remove(&sf->sf_selq.sel_klist, kn);
158 1.1 riastrad mutex_exit(&sf->sf_lock);
159 1.1 riastrad }
160 1.1 riastrad
161 1.1 riastrad static int
162 1.1 riastrad filt_sync_file_event(struct knote *kn, long hint)
163 1.1 riastrad {
164 1.1 riastrad struct sync_file *sf = kn->kn_hook;
165 1.1 riastrad int ret;
166 1.1 riastrad
167 1.1 riastrad if (hint == NOTE_SUBMIT)
168 1.1 riastrad KASSERT(mutex_owned(&sf->sf_lock));
169 1.1 riastrad else
170 1.1 riastrad mutex_enter(&sf->sf_lock);
171 1.1 riastrad
172 1.1 riastrad if (sf->sf_signalled) {
173 1.1 riastrad kn->kn_data = 0; /* XXX Does this work?? */
174 1.1 riastrad ret = 1;
175 1.1 riastrad } else if (sf->sf_polling) {
176 1.1 riastrad ret = 0;
177 1.1 riastrad } else {
178 1.1 riastrad sf->sf_polling = true;
179 1.1 riastrad mutex_exit(&sf->sf_lock);
180 1.1 riastrad ret = dma_fence_add_callback(sf->sf_fence, &sf->sf_fcb,
181 1.1 riastrad sync_file_fence_cb);
182 1.1 riastrad mutex_enter(&sf->sf_lock);
183 1.1 riastrad if (ret < 0) {
184 1.1 riastrad sf->sf_signalled = true;
185 1.1 riastrad selnotify(&sf->sf_selq, POLLIN, NOTE_SUBMIT);
186 1.1 riastrad kn->kn_data = 0;
187 1.1 riastrad ret = 1;
188 1.1 riastrad } else {
189 1.1 riastrad selrecord(curlwp, &sf->sf_selq);
190 1.1 riastrad ret = 0;
191 1.1 riastrad }
192 1.1 riastrad }
193 1.1 riastrad
194 1.1 riastrad if (hint == NOTE_SUBMIT)
195 1.1 riastrad KASSERT(mutex_owned(&sf->sf_lock));
196 1.1 riastrad else
197 1.1 riastrad mutex_exit(&sf->sf_lock);
198 1.1 riastrad
199 1.1 riastrad return ret;
200 1.1 riastrad }
201 1.1 riastrad
202 1.1 riastrad static const struct filterops sync_file_filtops = {
203 1.1 riastrad .f_flags = FILTEROP_ISFD,
204 1.1 riastrad .f_attach = NULL,
205 1.1 riastrad .f_detach = filt_sync_file_detach,
206 1.1 riastrad .f_event = filt_sync_file_event,
207 1.1 riastrad };
208 1.1 riastrad
209 1.1 riastrad struct dma_fence *
210 1.1 riastrad sync_file_get_fence(int fd)
211 1.1 riastrad {
212 1.1 riastrad struct file *fp;
213 1.1 riastrad struct sync_file *sf;
214 1.1 riastrad struct dma_fence *fence;
215 1.1 riastrad
216 1.1 riastrad if ((fp = fd_getfile(fd)) == NULL)
217 1.1 riastrad return NULL;
218 1.1 riastrad sf = fp->f_data;
219 1.1 riastrad fence = dma_fence_get(sf->sf_fence);
220 1.1 riastrad fd_putfile(fd);
221 1.1 riastrad
222 1.1 riastrad return fence;
223 1.1 riastrad }
224 1.1 riastrad
225 1.1 riastrad static const struct fileops sync_file_ops = {
226 1.1 riastrad .fo_name = "linux_sync_file",
227 1.1 riastrad .fo_read = fbadop_read,
228 1.1 riastrad .fo_write = fbadop_write,
229 1.1 riastrad .fo_ioctl = fbadop_ioctl,
230 1.1 riastrad .fo_fcntl = fnullop_fcntl,
231 1.1 riastrad .fo_poll = sync_file_poll,
232 1.1 riastrad .fo_stat = fbadop_stat, /* XXX */
233 1.1 riastrad .fo_close = sync_file_close,
234 1.1 riastrad .fo_kqfilter = sync_file_kqfilter,
235 1.1 riastrad .fo_restart = fnullop_restart,
236 1.1 riastrad .fo_mmap = NULL,
237 1.1 riastrad };
238