Print this page
pbchk
cleanup port_free_event_local() semantics
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/portfs/port_vnops.c
+++ new/usr/src/uts/common/fs/portfs/port_vnops.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
|
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 - * Copyright (c) 2014, Joyent, Inc. All rights reserved.
28 + * Copyright 2020 Joyent, Inc.
29 29 */
30 30
31 31 #include <sys/types.h>
32 32 #include <sys/vnode.h>
33 33 #include <sys/vfs_opreg.h>
34 34 #include <sys/kmem.h>
35 35 #include <fs/fs_subr.h>
36 36 #include <sys/proc.h>
37 37 #include <sys/kstat.h>
38 38 #include <sys/port_impl.h>
39 39
40 40 /* local functions */
41 41 static int port_open(struct vnode **, int, cred_t *, caller_context_t *);
42 42 static int port_close(struct vnode *, int, int, offset_t, cred_t *,
43 43 caller_context_t *);
44 44 static int port_getattr(struct vnode *, struct vattr *, int, cred_t *,
45 45 caller_context_t *);
46 46 static int port_access(struct vnode *, int, int, cred_t *, caller_context_t *);
47 47 static int port_realvp(vnode_t *, vnode_t **, caller_context_t *);
48 48 static int port_poll(vnode_t *, short, int, short *, struct pollhead **,
49 49 caller_context_t *);
50 50 static void port_inactive(struct vnode *, cred_t *, caller_context_t *);
51 51
52 52 const fs_operation_def_t port_vnodeops_template[] = {
53 53 VOPNAME_OPEN, { .vop_open = port_open },
54 54 VOPNAME_CLOSE, { .vop_close = port_close },
55 55 VOPNAME_GETATTR, { .vop_getattr = port_getattr },
56 56 VOPNAME_ACCESS, { .vop_access = port_access },
57 57 VOPNAME_INACTIVE, { .vop_inactive = port_inactive },
58 58 VOPNAME_FRLOCK, { .error = fs_error },
59 59 VOPNAME_REALVP, { .vop_realvp = port_realvp },
60 60 VOPNAME_POLL, { .vop_poll = port_poll },
61 61 VOPNAME_PATHCONF, { .error = fs_error },
62 62 VOPNAME_DISPOSE, { .error = fs_error },
63 63 VOPNAME_GETSECATTR, { .error = fs_error },
64 64 VOPNAME_SHRLOCK, { .error = fs_error },
65 65 NULL, NULL
66 66 };
67 67
68 68 /* ARGSUSED */
69 69 static int
70 70 port_open(struct vnode **vpp, int flag, cred_t *cr, caller_context_t *ct)
71 71 {
72 72 return (0);
73 73 }
74 74
75 75 /*
76 76 * port_discard_events() scans the port event queue for events owned
77 77 * by current proc. Non-shareable events will be discarded, all other
78 78 * events remain in the event queue.
79 79 */
80 80 void
81 81 port_discard_events(port_queue_t *portq)
82 82 {
83 83 port_kevent_t *kevp;
84 84 pid_t pid = curproc->p_pid;
85 85
86 86 /*
87 87 * The call to port_block() is required to avoid interaction
88 88 * with other threads in port_get(n).
89 89 */
90 90 mutex_enter(&portq->portq_mutex);
91 91 port_block(portq);
92 92 port_push_eventq(portq); /* empty temporary queue */
93 93 kevp = list_head(&portq->portq_list);
94 94 while (kevp) {
95 95 if (kevp->portkev_pid == pid) {
96 96 /* own event, check if it is shareable */
97 97 if (kevp->portkev_flags & PORT_KEV_NOSHARE)
98 98 kevp->portkev_flags |= PORT_KEV_FREE;
99 99 }
100 100 kevp = list_next(&portq->portq_list, kevp);
101 101 }
102 102 port_unblock(portq);
103 103 mutex_exit(&portq->portq_mutex);
104 104 }
105 105
106 106 /*
107 107 * Called from port_close().
|
↓ open down ↓ |
69 lines elided |
↑ open up ↑ |
108 108 * Free all kernel events structures which are still in the event queue.
109 109 */
110 110 static void
111 111 port_close_events(port_queue_t *portq)
112 112 {
113 113 port_kevent_t *pkevp;
114 114 int events; /* ignore events */
115 115
116 116 mutex_enter(&portq->portq_mutex);
117 117 while (pkevp = list_head(&portq->portq_list)) {
118 + port_t *pp = pkevp->portkev_port;
119 +
118 120 portq->portq_nent--;
119 121 list_remove(&portq->portq_list, pkevp);
120 122 if (pkevp->portkev_callback) {
121 123 (void) (*pkevp->portkev_callback)(pkevp->portkev_arg,
122 124 &events, pkevp->portkev_pid, PORT_CALLBACK_CLOSE,
123 125 pkevp);
124 126 }
125 - mutex_exit(&portq->portq_mutex);
126 - port_free_event_local(pkevp, 0);
127 + /*
128 + * Don't drop the portq_mutex, but instead perform the
129 + * decrement of port_curr in advance of calling
130 + * port_free_event_local(). We do need to reacquire
131 + * portq_mutex so we can properly wait for any
132 + * pollwakeup()-signalled threads to finish up.
133 + */
134 + if (--pp->port_curr < pp->port_max_events)
135 + cv_signal(&pp->port_cv);
136 + port_free_event_local(pkevp, B_FALSE);
127 137 mutex_enter(&portq->portq_mutex);
128 138 }
129 139
130 140 /*
131 141 * Wait for any thread in pollwakeup(), accessing this port to
132 142 * finish.
133 143 */
134 144 while (portq->portq_flags & PORTQ_POLLWK_PEND) {
135 145 cv_wait(&portq->portq_closecv, &portq->portq_mutex);
136 146 }
137 147 mutex_exit(&portq->portq_mutex);
138 148 }
139 149
140 150 /*
141 151 * The port_close() function is called from standard close(2) when
142 152 * the file descriptor is of type S_IFPORT/VPORT.
143 153 * Port file descriptors behave like standard file descriptors. It means,
144 154 * the port file/vnode is only destroyed on last close.
145 155 * If the reference counter is > 1 then
146 156 * - sources associated with the port will be notified about the close,
147 157 * - objects associated with the port will be dissociated,
148 158 * - pending and delivered events will be discarded.
149 159 * On last close all references and caches will be removed. The vnode itself
150 160 * will be destroyed with VOP_RELE().
151 161 */
152 162 /* ARGSUSED */
153 163 static int
154 164 port_close(struct vnode *vp, int flag, int count, offset_t offset, cred_t *cr,
155 165 caller_context_t *ct)
156 166 {
157 167 port_t *pp;
158 168 port_queue_t *portq;
159 169 port_source_t *ps;
160 170 port_source_t *ps_next;
161 171 int source;
162 172
163 173 pp = VTOEP(vp);
164 174 mutex_enter(&pp->port_mutex);
165 175 if (pp->port_flags & PORT_CLOSED) {
166 176 mutex_exit(&pp->port_mutex);
167 177 return (0);
168 178 }
169 179 mutex_exit(&pp->port_mutex);
170 180
171 181 portq = &pp->port_queue;
172 182 if (count > 1) {
173 183 /*
174 184 * It is not the last close.
175 185 * Remove/free all event resources owned by the current proc
176 186 * First notify all with the port associated sources about the
177 187 * close(2). The last argument of the close callback function
178 188 * advises the source about the type of of the close.
179 189 * If the port was set in alert mode by the curren process then
180 190 * remove the alert mode.
181 191 */
182 192
183 193 /* check alert mode of the port */
184 194 mutex_enter(&portq->portq_mutex);
185 195 if ((portq->portq_flags & PORTQ_ALERT) &&
186 196 (portq->portq_alert.portal_pid == curproc->p_pid))
187 197 portq->portq_flags &= ~PORTQ_ALERT;
188 198 mutex_exit(&portq->portq_mutex);
189 199
190 200 /* notify all event sources about port_close() */
191 201 mutex_enter(&portq->portq_source_mutex);
192 202 for (source = 0; source < PORT_SCACHE_SIZE; source++) {
193 203 ps = portq->portq_scache[PORT_SHASH(source)];
194 204 for (; ps != NULL; ps = ps->portsrc_next) {
195 205 if (ps->portsrc_close != NULL)
196 206 (*ps->portsrc_close)
197 207 (ps->portsrc_closearg, pp->port_fd,
198 208 curproc->p_pid, 0);
199 209 }
200 210 }
201 211 mutex_exit(&portq->portq_source_mutex);
202 212 port_discard_events(&pp->port_queue);
203 213 return (0);
204 214 }
205 215
206 216 /*
207 217 * We are executing the last close of the port -> discard everything
208 218 * Make sure that all threads/processes accessing this port leave
209 219 * the kernel immediately.
210 220 */
211 221
212 222 mutex_enter(&portq->portq_mutex);
213 223 portq->portq_flags |= PORTQ_CLOSE;
214 224 while (portq->portq_thrcnt > 0) {
215 225 if (portq->portq_thread != NULL)
216 226 cv_signal(&portq->portq_thread->portget_cv);
217 227 cv_wait(&portq->portq_closecv, &portq->portq_mutex);
218 228 }
219 229 mutex_exit(&portq->portq_mutex);
220 230
221 231 /*
222 232 * Send "last close" message to associated sources.
223 233 * - new event allocation requests are being denied since uf_file entry
224 234 * was set to NULL in closeandsetf().
225 235 * - all still allocated event structures must be returned to the
226 236 * port immediately:
227 237 * - call port_free_event(*event) or
228 238 * - call port_send_event(*event) to complete event operations
229 239 * which need activities in a dedicated process environment.
230 240 * The port_close() function waits until all allocated event structures
231 241 * are delivered back to the port.
232 242 */
233 243
234 244 mutex_enter(&portq->portq_source_mutex);
235 245 for (source = 0; source < PORT_SCACHE_SIZE; source++) {
236 246 ps = portq->portq_scache[PORT_SHASH(source)];
237 247 for (; ps != NULL; ps = ps_next) {
238 248 ps_next = ps->portsrc_next;
239 249 if (ps->portsrc_close != NULL)
240 250 (*ps->portsrc_close)(ps->portsrc_closearg,
241 251 pp->port_fd, curproc->p_pid, 1);
242 252 kmem_free(ps, sizeof (port_source_t));
243 253 }
244 254 }
245 255 kmem_free(portq->portq_scache,
246 256 PORT_SCACHE_SIZE * sizeof (port_source_t *));
247 257 portq->portq_scache = NULL;
248 258 mutex_exit(&portq->portq_source_mutex);
249 259
250 260 mutex_enter(&portq->portq_mutex);
251 261 /* Wait for outstanding events */
252 262 while (pp->port_curr > portq->portq_nent)
253 263 cv_wait(&portq->portq_closecv, &portq->portq_mutex);
254 264 mutex_exit(&portq->portq_mutex);
255 265
256 266 /*
257 267 * If PORT_SOURCE_FD objects were not associated with the port then
258 268 * it is necessary to free the port_fdcache structure here.
259 269 */
260 270
261 271 if (portq->portq_pcp != NULL) {
262 272 mutex_destroy(&portq->portq_pcp->pc_lock);
263 273 kmem_free(portq->portq_pcp, sizeof (port_fdcache_t));
264 274 portq->portq_pcp = NULL;
265 275 }
266 276
267 277 /*
268 278 * Now all events are passed back to the port,
269 279 * discard remaining events in the port queue
270 280 */
271 281
272 282 port_close_events(portq);
273 283 return (0);
274 284 }
275 285
276 286 /*
277 287 * The port_poll() function is the VOP_POLL() entry of event ports.
278 288 * Event ports return:
279 289 * POLLIN : events are available in the event queue
280 290 * POLLOUT : event queue can still accept events
281 291 */
282 292 /*ARGSUSED*/
283 293 static int
284 294 port_poll(vnode_t *vp, short events, int anyyet, short *reventsp,
285 295 struct pollhead **phpp, caller_context_t *ct)
286 296 {
287 297 port_t *pp;
288 298 port_queue_t *portq;
289 299 short levents;
290 300
291 301 pp = VTOEP(vp);
292 302 portq = &pp->port_queue;
293 303 levents = 0;
294 304 mutex_enter(&portq->portq_mutex);
295 305 if (portq->portq_nent)
296 306 levents = POLLIN;
297 307 if (pp->port_curr < pp->port_max_events)
298 308 levents |= POLLOUT;
299 309 levents &= events;
300 310 *reventsp = levents;
301 311 if ((levents == 0 && !anyyet) || (events & POLLET)) {
302 312 *phpp = &pp->port_pollhd;
303 313 portq->portq_flags |= events & POLLIN ? PORTQ_POLLIN : 0;
304 314 portq->portq_flags |= events & POLLOUT ? PORTQ_POLLOUT : 0;
305 315 }
306 316 mutex_exit(&portq->portq_mutex);
307 317 return (0);
308 318 }
309 319
310 320
311 321 /* ARGSUSED */
312 322 static int
313 323 port_getattr(struct vnode *vp, struct vattr *vap, int flags, cred_t *cr,
314 324 caller_context_t *ct)
315 325 {
316 326 port_t *pp;
317 327 extern dev_t portdev;
318 328
319 329 pp = VTOEP(vp);
320 330
321 331 vap->va_type = vp->v_type; /* vnode type (for create) */
322 332 vap->va_mode = 0; /* file access mode */
323 333 vap->va_uid = pp->port_uid; /* owner user id */
324 334 vap->va_gid = pp->port_gid; /* owner group id */
325 335 vap->va_fsid = portdev; /* file system id */
326 336 vap->va_nodeid = (ino64_t)0; /* node id */
327 337 vap->va_nlink = vp->v_count; /* number of references to file */
328 338 vap->va_size = (u_offset_t)pp->port_queue.portq_nent; /* file size */
329 339 vap->va_atime = pp->port_ctime; /* time of last access */
330 340 vap->va_mtime = pp->port_ctime; /* time of last modification */
331 341 vap->va_ctime = pp->port_ctime; /* time file ``created'' */
332 342 vap->va_rdev = portdev; /* device the file represents */
333 343 vap->va_blksize = 0; /* fundamental block size */
334 344 vap->va_nblocks = (fsblkcnt64_t)0; /* # of blocks allocated */
335 345 vap->va_seq = 0; /* sequence number */
336 346
|
↓ open down ↓ |
200 lines elided |
↑ open up ↑ |
337 347 return (0);
338 348 }
339 349
340 350 /*
341 351 * Destroy the port.
342 352 */
343 353 /* ARGSUSED */
344 354 static void
345 355 port_inactive(struct vnode *vp, cred_t *cr, caller_context_t *ct)
346 356 {
347 - port_t *pp = VTOEP(vp);
348 - extern port_kstat_t port_kstat;
357 + port_t *pp = VTOEP(vp);
358 + extern port_kstat_t port_kstat;
349 359
350 360 mutex_enter(&port_control.pc_mutex);
351 361 port_control.pc_nents--;
352 362 curproc->p_portcnt--;
353 363 port_kstat.pks_ports.value.ui32--;
354 364 mutex_exit(&port_control.pc_mutex);
355 365 vn_free(vp);
356 366 mutex_destroy(&pp->port_mutex);
357 367 mutex_destroy(&pp->port_queue.portq_mutex);
358 368 mutex_destroy(&pp->port_queue.portq_source_mutex);
359 369 kmem_free(pp, sizeof (port_t));
360 370 }
361 371
362 372 /* ARGSUSED */
363 373 static int
364 374 port_access(struct vnode *vp, int mode, int flags, cred_t *cr,
365 375 caller_context_t *ct)
366 376 {
367 377 return (0);
368 378 }
369 379
370 380 /* ARGSUSED */
371 381 static int
372 382 port_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
373 383 {
374 384 *vpp = vp;
375 385 return (0);
376 386 }
|
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX