aboutsummaryrefslogtreecommitdiff
path: root/sys/sys
diff options
context:
space:
mode:
authorJeff Roberson <jeff@FreeBSD.org>2007-12-30 01:42:15 +0000
committerJeff Roberson <jeff@FreeBSD.org>2007-12-30 01:42:15 +0000
commit397c19d1753d210247d77eb3ca33d1c7c1eb2fa9 (patch)
tree0f2354bfc200294c2629e6ecfba76e364beda579 /sys/sys
parent2a79fd39b4cf409d2c0bd7a449d7f3e91d7b9007 (diff)
downloadsrc-397c19d1753d210247d77eb3ca33d1c7c1eb2fa9.tar.gz
src-397c19d1753d210247d77eb3ca33d1c7c1eb2fa9.zip
Remove explicit locking of struct file.
- Introduce a finit() which is used to initailize the fields of struct file in such a way that the ops vector is only valid after the data, type, and flags are valid. - Protect f_flag and f_count with atomic operations. - Remove the global list of all files and associated accounting. - Rewrite the unp garbage collection such that it no longer requires the global list of all files and instead uses a list of all unp sockets. - Mark sockets in the accept queue so we don't incorrectly gc them. Tested by: kris, pho
Notes
Notes: svn path=/head/; revision=174988
Diffstat (limited to 'sys/sys')
-rw-r--r--sys/sys/file.h91
-rw-r--r--sys/sys/unpcb.h9
2 files changed, 39 insertions, 61 deletions
diff --git a/sys/sys/file.h b/sys/sys/file.h
index 58501fa83f37..c5f4afbfadae 100644
--- a/sys/sys/file.h
+++ b/sys/sys/file.h
@@ -99,49 +99,37 @@ struct fileops {
*
* Below is the list of locks that protects members in struct file.
*
- * (fl) filelist_lock
- * (f) f_mtx in struct file
+ * (f) protected with mtx_lock(mtx_pool_find(fp))
* none not locked
*/
struct file {
- LIST_ENTRY(file) f_list;/* (fl) list of active files */
- short f_type; /* descriptor type */
- void *f_data; /* file descriptor specific data */
- u_int f_flag; /* see fcntl.h */
- struct mtx *f_mtxp; /* mutex to protect data */
- struct fileops *f_ops; /* File operations */
- struct ucred *f_cred; /* credentials associated with descriptor */
- int f_count; /* (f) reference count */
- struct vnode *f_vnode; /* NULL or applicable vnode */
-
- /* DFLAG_SEEKABLE specific fields */
- off_t f_offset;
- short f_vnread_flags; /*
- * (f) home grown sleep lock for f_offset
- * Used only for shared vnode locking in
- * vnread()
- */
-#define FOFFSET_LOCKED 0x1
-#define FOFFSET_LOCK_WAITING 0x2
- /* DTYPE_SOCKET specific fields */
- short f_gcflag; /* used by thread doing fd garbage collection */
-#define FMARK 0x1 /* mark during gc() */
-#define FDEFER 0x2 /* defer for next gc pass */
-#define FWAIT 0x4 /* gc is scanning message buffers */
- int f_msgcount; /* (f) references from message queue */
-
- /* DTYPE_VNODE specific fields */
- int f_seqcount; /*
- * count of sequential accesses -- cleared
- * by most seek operations.
- */
- off_t f_nextoff; /*
- * offset of next expected read or write
- */
- void *f_label; /* Place-holder for struct label pointer. */
+ void *f_data; /* file descriptor specific data */
+ struct fileops *f_ops; /* File operations */
+ struct ucred *f_cred; /* associated credentials. */
+ struct vnode *f_vnode; /* NULL or applicable vnode */
+ short f_type; /* descriptor type */
+ short f_vnread_flags; /* (f) Sleep lock for f_offset */
+ volatile u_int f_flag; /* see fcntl.h */
+ volatile int f_count; /* reference count */
+ /*
+ * DTYPE_VNODE specific fields.
+ */
+ int f_seqcount; /* Count of sequential accesses. */
+ off_t f_nextoff; /* next expected read/write offset. */
+ /*
+ * DFLAG_SEEKABLE specific fields
+ */
+ off_t f_offset;
+ /*
+ * Mandatory Access control information.
+ */
+ void *f_label; /* Place-holder for MAC label. */
};
+#define FOFFSET_LOCKED 0x1
+#define FOFFSET_LOCK_WAITING 0x2
+
#endif /* _KERNEL */
/*
@@ -168,20 +156,17 @@ struct xfile {
MALLOC_DECLARE(M_FILE);
#endif
-LIST_HEAD(filelist, file);
-extern struct filelist filehead; /* (fl) head of list of open files */
extern struct fileops vnops;
extern struct fileops badfileops;
extern struct fileops socketops;
extern int maxfiles; /* kernel limit on number of open files */
extern int maxfilesperproc; /* per process limit on number of open files */
-extern int openfiles; /* (fl) actual number of open files */
-extern struct sx filelist_lock; /* sx to protect filelist and openfiles */
+extern volatile int openfiles; /* actual number of open files */
int fget(struct thread *td, int fd, struct file **fpp);
int fget_read(struct thread *td, int fd, struct file **fpp);
int fget_write(struct thread *td, int fd, struct file **fpp);
-int fdrop(struct file *fp, struct thread *td);
+int _fdrop(struct file *fp, struct thread *td);
/*
* The socket operations are used a couple of places.
@@ -196,12 +181,7 @@ fo_kqfilter_t soo_kqfilter;
fo_stat_t soo_stat;
fo_close_t soo_close;
-/* Lock a file. */
-#define FILE_LOCK(f) mtx_lock((f)->f_mtxp)
-#define FILE_UNLOCK(f) mtx_unlock((f)->f_mtxp)
-#define FILE_LOCKED(f) mtx_owned((f)->f_mtxp)
-#define FILE_LOCK_ASSERT(f, type) mtx_assert((f)->f_mtxp, (type))
-
+void finit(struct file *, u_int, short, void *, struct fileops *);
int fgetvp(struct thread *td, int fd, struct vnode **vpp);
int fgetvp_read(struct thread *td, int fd, struct vnode **vpp);
int fgetvp_write(struct thread *td, int fd, struct vnode **vpp);
@@ -209,18 +189,9 @@ int fgetvp_write(struct thread *td, int fd, struct vnode **vpp);
int fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp);
void fputsock(struct socket *sp);
-#define fhold_locked(fp) \
- do { \
- FILE_LOCK_ASSERT(fp, MA_OWNED); \
- (fp)->f_count++; \
- } while (0)
-
-#define fhold(fp) \
- do { \
- FILE_LOCK(fp); \
- (fp)->f_count++; \
- FILE_UNLOCK(fp); \
- } while (0)
+#define fhold(fp) atomic_add_int(&(fp)->f_count, 1)
+#define fdrop(fp, td) \
+ (atomic_fetchadd_int(&(fp)->f_count, -1) <= 1 ? _fdrop((fp), (td)) : 0)
static __inline fo_rdwr_t fo_read;
static __inline fo_rdwr_t fo_write;
diff --git a/sys/sys/unpcb.h b/sys/sys/unpcb.h
index c7b3a44588ef..4d69f3e5ace5 100644
--- a/sys/sys/unpcb.h
+++ b/sys/sys/unpcb.h
@@ -67,6 +67,7 @@ LIST_HEAD(unp_head, unpcb);
struct unpcb {
LIST_ENTRY(unpcb) unp_link; /* glue on list of all PCBs */
struct socket *unp_socket; /* pointer back to socket */
+ struct file *unp_file; /* back-pointer to file for gc. */
struct vnode *unp_vnode; /* if associated with file */
ino_t unp_ino; /* fake inode number */
struct unpcb *unp_conn; /* control block of connected socket */
@@ -76,9 +77,11 @@ struct unpcb {
int unp_cc; /* copy of rcv.sb_cc */
int unp_mbcnt; /* copy of rcv.sb_mbcnt */
unp_gen_t unp_gencnt; /* generation count of this instance */
- int unp_flags; /* flags */
+ short unp_flags; /* flags */
+ short unp_gcflag; /* Garbage collector flags. */
struct xucred unp_peercred; /* peer credentials, if applicable */
u_int unp_refcount;
+ u_int unp_msgcount; /* references from message queue */
struct mtx unp_mtx; /* mutex */
};
@@ -100,6 +103,10 @@ struct unpcb {
#define UNP_WANTCRED 0x004 /* credentials wanted */
#define UNP_CONNWAIT 0x008 /* connect blocks until accepted */
+#define UNPGC_REF 0x1 /* unpcb has external ref. */
+#define UNPGC_DEAD 0x2 /* unpcb might be dead. */
+#define UNPGC_SCANNED 0x4 /* Has been scanned. */
+
/*
* These flags are used to handle non-atomicity in connect() and bind()
* operations on a socket: in particular, to avoid races between multiple