summaryrefslogtreecommitdiff
path: root/sys/kern/kern_descrip.c
diff options
context:
space:
mode:
authorVisa Hankala <visa@cvs.openbsd.org>2018-06-24 05:58:06 +0000
committerVisa Hankala <visa@cvs.openbsd.org>2018-06-24 05:58:06 +0000
commit79829fb2d93d6d03f2f0e07e826cce25ade9199a (patch)
tree2c39f898eaf9cac95d5aa1667fcd2dd7258ac0fb /sys/kern/kern_descrip.c
parent8496902c1c1fea66cded9e3ce088affb53b8b329 (diff)
Use atomic operations for updating `numfiles'. This makes the file count
tracking work without locks. OK kettenis@, deraadt@
Diffstat (limited to 'sys/kern/kern_descrip.c')
-rw-r--r--sys/kern/kern_descrip.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index f6c17c8a67e..b0815c2111a 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_descrip.c,v 1.167 2018/06/20 10:52:49 mpi Exp $ */
+/* $OpenBSD: kern_descrip.c,v 1.168 2018/06/24 05:58:05 visa Exp $ */
/* $NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $ */
/*
@@ -957,7 +957,7 @@ int
falloc(struct proc *p, struct file **resultfp, int *resultfd)
{
struct file *fp;
- int error, i;
+ int error, i, nfiles;
KASSERT(resultfp != NULL);
KASSERT(resultfd != NULL);
@@ -971,7 +971,9 @@ restart:
}
return (error);
}
- if (numfiles >= maxfiles) {
+ nfiles = atomic_inc_int_nv(&numfiles);
+ if (nfiles > maxfiles) {
+ atomic_dec_int(&numfiles);
fd_unused(p->p_fd, i);
tablefull("file");
return (ENFILE);
@@ -982,7 +984,6 @@ restart:
* of open files at that point, otherwise put it at the front of
* the list of open files.
*/
- numfiles++;
fp = pool_get(&file_pool, PR_WAITOK|PR_ZERO);
/*
* We need to block interrupts as long as `f_mtx' is being taken
@@ -1222,7 +1223,7 @@ fdrop(struct file *fp, struct proc *p)
error = 0;
crfree(fp->f_cred);
- numfiles--;
+ atomic_dec_int(&numfiles);
pool_put(&file_pool, fp);
return (error);