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