Tue Oct 27 02:58:29 2009 UTC ()
- Amend fd_hold() to take an argument and add assert (reflects two cases,
  fork1() and the rest, e.g. kthread_create(), when creating from lwp0).

- lwp_create(): do not touch filedesc internals, use fd_hold().


(rmind)
diff -r1.199 -r1.200 src/sys/kern/kern_descrip.c
diff -r1.135 -r1.136 src/sys/kern/kern_lwp.c
diff -r1.56 -r1.57 src/sys/sys/filedesc.h

cvs diff -r1.199 -r1.200 src/sys/kern/kern_descrip.c (expand / switch to unified diff)

--- src/sys/kern/kern_descrip.c 2009/08/16 11:00:20 1.199
+++ src/sys/kern/kern_descrip.c 2009/10/27 02:58:28 1.200
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_descrip.c,v 1.199 2009/08/16 11:00:20 yamt Exp $ */ 1/* $NetBSD: kern_descrip.c,v 1.200 2009/10/27 02:58:28 rmind Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -60,27 +60,27 @@ @@ -60,27 +60,27 @@
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE. 63 * SUCH DAMAGE.
64 * 64 *
65 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95 65 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
66 */ 66 */
67 67
68/* 68/*
69 * File descriptor management. 69 * File descriptor management.
70 */ 70 */
71 71
72#include <sys/cdefs.h> 72#include <sys/cdefs.h>
73__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.199 2009/08/16 11:00:20 yamt Exp $"); 73__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.200 2009/10/27 02:58:28 rmind Exp $");
74 74
75#include <sys/param.h> 75#include <sys/param.h>
76#include <sys/systm.h> 76#include <sys/systm.h>
77#include <sys/filedesc.h> 77#include <sys/filedesc.h>
78#include <sys/kernel.h> 78#include <sys/kernel.h>
79#include <sys/proc.h> 79#include <sys/proc.h>
80#include <sys/file.h> 80#include <sys/file.h>
81#include <sys/socket.h> 81#include <sys/socket.h>
82#include <sys/socketvar.h> 82#include <sys/socketvar.h>
83#include <sys/stat.h> 83#include <sys/stat.h>
84#include <sys/ioctl.h> 84#include <sys/ioctl.h>
85#include <sys/fcntl.h> 85#include <sys/fcntl.h>
86#include <sys/pool.h> 86#include <sys/pool.h>
@@ -1316,30 +1316,32 @@ void @@ -1316,30 +1316,32 @@ void
1316fd_share(struct proc *p2) 1316fd_share(struct proc *p2)
1317{ 1317{
1318 filedesc_t *fdp; 1318 filedesc_t *fdp;
1319 1319
1320 fdp = curlwp->l_fd; 1320 fdp = curlwp->l_fd;
1321 p2->p_fd = fdp; 1321 p2->p_fd = fdp;
1322 atomic_inc_uint(&fdp->fd_refcnt); 1322 atomic_inc_uint(&fdp->fd_refcnt);
1323} 1323}
1324 1324
1325/* 1325/*
1326 * Acquire a hold on a filedesc structure. 1326 * Acquire a hold on a filedesc structure.
1327 */ 1327 */
1328void 1328void
1329fd_hold(void) 1329fd_hold(lwp_t *l)
1330{ 1330{
 1331 filedesc_t *fdp = l->l_fd;
1331 1332
1332 atomic_inc_uint(&curlwp->l_fd->fd_refcnt); 1333 KASSERT(fdp == curlwp->l_fd || fdp == lwp0.l_fd);
 1334 atomic_inc_uint(&fdp->fd_refcnt);
1333} 1335}
1334 1336
1335/* 1337/*
1336 * Copy a filedesc structure. 1338 * Copy a filedesc structure.
1337 */ 1339 */
1338filedesc_t * 1340filedesc_t *
1339fd_copy(void) 1341fd_copy(void)
1340{ 1342{
1341 filedesc_t *newfdp, *fdp; 1343 filedesc_t *newfdp, *fdp;
1342 fdfile_t *ff, **ffp, **nffp, *ff2; 1344 fdfile_t *ff, **ffp, **nffp, *ff2;
1343 int i, j, numfiles, lastfile, newlast; 1345 int i, j, numfiles, lastfile, newlast;
1344 file_t *fp; 1346 file_t *fp;
1345 fdtab_t *newdt; 1347 fdtab_t *newdt;

cvs diff -r1.135 -r1.136 src/sys/kern/kern_lwp.c (expand / switch to unified diff)

--- src/sys/kern/kern_lwp.c 2009/10/22 22:28:57 1.135
+++ src/sys/kern/kern_lwp.c 2009/10/27 02:58:28 1.136
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_lwp.c,v 1.135 2009/10/22 22:28:57 rmind Exp $ */ 1/* $NetBSD: kern_lwp.c,v 1.136 2009/10/27 02:58:28 rmind Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2001, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2001, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams, and Andrew Doran. 8 * by Nathan J. Williams, and Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -199,27 +199,27 @@ @@ -199,27 +199,27 @@
199 * (But not always for kernel threads. There are some special cases 199 * (But not always for kernel threads. There are some special cases
200 * as mentioned above. See kern_softint.c.) 200 * as mentioned above. See kern_softint.c.)
201 * 201 *
202 * Note that an LWP is considered running or likely to run soon if in 202 * Note that an LWP is considered running or likely to run soon if in
203 * one of the following states. This affects the value of p_nrlwps: 203 * one of the following states. This affects the value of p_nrlwps:
204 * 204 *
205 * LSRUN, LSONPROC, LSSLEEP 205 * LSRUN, LSONPROC, LSSLEEP
206 * 206 *
207 * p_lock does not need to be held when transitioning among these 207 * p_lock does not need to be held when transitioning among these
208 * three states, hence p_lock is rarely taken for state transitions. 208 * three states, hence p_lock is rarely taken for state transitions.
209 */ 209 */
210 210
211#include <sys/cdefs.h> 211#include <sys/cdefs.h>
212__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.135 2009/10/22 22:28:57 rmind Exp $"); 212__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.136 2009/10/27 02:58:28 rmind Exp $");
213 213
214#include "opt_ddb.h" 214#include "opt_ddb.h"
215#include "opt_lockdebug.h" 215#include "opt_lockdebug.h"
216#include "opt_sa.h" 216#include "opt_sa.h"
217 217
218#define _LWP_API_PRIVATE 218#define _LWP_API_PRIVATE
219 219
220#include <sys/param.h> 220#include <sys/param.h>
221#include <sys/systm.h> 221#include <sys/systm.h>
222#include <sys/cpu.h> 222#include <sys/cpu.h>
223#include <sys/pool.h> 223#include <sys/pool.h>
224#include <sys/proc.h> 224#include <sys/proc.h>
225#include <sys/sa.h> 225#include <sys/sa.h>
@@ -596,27 +596,27 @@ lwp_create(lwp_t *l1, proc_t *p2, vaddr_ @@ -596,27 +596,27 @@ lwp_create(lwp_t *l1, proc_t *p2, vaddr_
596 l2->l_priority = l1->l_priority; 596 l2->l_priority = l1->l_priority;
597 l2->l_inheritedprio = -1; 597 l2->l_inheritedprio = -1;
598 l2->l_flag = 0; 598 l2->l_flag = 0;
599 l2->l_pflag = LP_MPSAFE; 599 l2->l_pflag = LP_MPSAFE;
600 TAILQ_INIT(&l2->l_ld_locks); 600 TAILQ_INIT(&l2->l_ld_locks);
601 601
602 /* 602 /*
603 * If not the first LWP in the process, grab a reference to the 603 * If not the first LWP in the process, grab a reference to the
604 * descriptor table. 604 * descriptor table.
605 */ 605 */
606 l2->l_fd = p2->p_fd; 606 l2->l_fd = p2->p_fd;
607 if (p2->p_nlwps != 0) { 607 if (p2->p_nlwps != 0) {
608 KASSERT(l1->l_proc == p2); 608 KASSERT(l1->l_proc == p2);
609 atomic_inc_uint(&l2->l_fd->fd_refcnt); 609 fd_hold(l2);
610 } else { 610 } else {
611 KASSERT(l1->l_proc != p2); 611 KASSERT(l1->l_proc != p2);
612 } 612 }
613 613
614 if (p2->p_flag & PK_SYSTEM) { 614 if (p2->p_flag & PK_SYSTEM) {
615 /* Mark it as a system LWP. */ 615 /* Mark it as a system LWP. */
616 l2->l_flag |= LW_SYSTEM; 616 l2->l_flag |= LW_SYSTEM;
617 } 617 }
618 618
619 kpreempt_disable(); 619 kpreempt_disable();
620 l2->l_mutex = l1->l_cpu->ci_schedstate.spc_mutex; 620 l2->l_mutex = l1->l_cpu->ci_schedstate.spc_mutex;
621 l2->l_cpu = l1->l_cpu; 621 l2->l_cpu = l1->l_cpu;
622 kpreempt_enable(); 622 kpreempt_enable();

cvs diff -r1.56 -r1.57 src/sys/sys/filedesc.h (expand / switch to unified diff)

--- src/sys/sys/filedesc.h 2009/05/25 03:59:45 1.56
+++ src/sys/sys/filedesc.h 2009/10/27 02:58:28 1.57
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: filedesc.h,v 1.56 2009/05/25 03:59:45 yamt Exp $ */ 1/* $NetBSD: filedesc.h,v 1.57 2009/10/27 02:58:28 rmind Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -180,27 +180,27 @@ struct proc; @@ -180,27 +180,27 @@ struct proc;
180/* 180/*
181 * Kernel global variables and routines. 181 * Kernel global variables and routines.
182 */ 182 */
183void fd_sys_init(void); 183void fd_sys_init(void);
184int fd_dupopen(int, int *, int, int); 184int fd_dupopen(int, int *, int, int);
185int fd_alloc(struct proc *, int, int *); 185int fd_alloc(struct proc *, int, int *);
186void fd_tryexpand(struct proc *); 186void fd_tryexpand(struct proc *);
187int fd_allocfile(file_t **, int *); 187int fd_allocfile(file_t **, int *);
188void fd_affix(struct proc *, file_t *, unsigned); 188void fd_affix(struct proc *, file_t *, unsigned);
189void fd_abort(struct proc *, file_t *, unsigned); 189void fd_abort(struct proc *, file_t *, unsigned);
190filedesc_t *fd_copy(void); 190filedesc_t *fd_copy(void);
191filedesc_t *fd_init(filedesc_t *); 191filedesc_t *fd_init(filedesc_t *);
192void fd_share(proc_t *); 192void fd_share(proc_t *);
193void fd_hold(void); 193void fd_hold(lwp_t *);
194void fd_free(void); 194void fd_free(void);
195void fd_closeexec(void); 195void fd_closeexec(void);
196int fd_checkstd(void); 196int fd_checkstd(void);
197file_t *fd_getfile(unsigned); 197file_t *fd_getfile(unsigned);
198file_t *fd_getfile2(proc_t *, unsigned); 198file_t *fd_getfile2(proc_t *, unsigned);
199void fd_putfile(unsigned); 199void fd_putfile(unsigned);
200int fd_getvnode(unsigned, file_t **); 200int fd_getvnode(unsigned, file_t **);
201int fd_getsock(unsigned, struct socket **); 201int fd_getsock(unsigned, struct socket **);
202void fd_putvnode(unsigned); 202void fd_putvnode(unsigned);
203void fd_putsock(unsigned); 203void fd_putsock(unsigned);
204int fd_close(unsigned); 204int fd_close(unsigned);
205int fd_dup(file_t *, int, int *, bool); 205int fd_dup(file_t *, int, int *, bool);
206int fd_dup2(file_t *, unsigned); 206int fd_dup2(file_t *, unsigned);