From owner-svn-src-all@FreeBSD.ORG Wed Jun 13 17:12:54 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 0B847106564A; Wed, 13 Jun 2012 17:12:54 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [69.147.83.44]) by mx1.freebsd.org (Postfix) with ESMTP id D15658FC0C; Wed, 13 Jun 2012 17:12:53 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q5DHCrkR037620; Wed, 13 Jun 2012 17:12:53 GMT (envelope-from mjg@svn.freebsd.org) Received: (from mjg@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q5DHCrbf037618; Wed, 13 Jun 2012 17:12:53 GMT (envelope-from mjg@svn.freebsd.org) Message-Id: <201206131712.q5DHCrbf037618@svn.freebsd.org> From: Mateusz Guzik Date: Wed, 13 Jun 2012 17:12:53 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r237012 - head/sys/kern X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jun 2012 17:12:54 -0000 Author: mjg Date: Wed Jun 13 17:12:53 2012 New Revision: 237012 URL: http://svn.freebsd.org/changeset/base/237012 Log: Re-apply reverted parts of r236935 by pjd with some changes. If fdalloc() decides to grow fdtable it does it once and at most doubles the size. This still may be not enough for sufficiently large fd. Use fd in calculations of new size in order to fix this. When growing the table, fd is already equal to first free descriptor >= minfd, also fdgrowtable() no longer drops the filedesc lock. As a result of this there is no need to retry allocation nor lookup. Fix description of fd_first_free to note all return values. In co-operation with: pjd Approved by: trasz (mentor) MFC after: 1 month Modified: head/sys/kern/kern_descrip.c Modified: head/sys/kern/kern_descrip.c ============================================================================== --- head/sys/kern/kern_descrip.c Wed Jun 13 16:40:47 2012 (r237011) +++ head/sys/kern/kern_descrip.c Wed Jun 13 17:12:53 2012 (r237012) @@ -189,8 +189,9 @@ void (*mq_fdclose)(struct thread *td, in static struct mtx fdesc_mtx; /* - * Find the first zero bit in the given bitmap, starting at low and not - * exceeding size - 1. + * If low >= size, just return low. Otherwise find the first zero bit in the + * given bitmap, starting at low and not exceeding size - 1. Return size if + * not found. */ static int fd_first_free(struct filedesc *fdp, int low, int size) @@ -1461,7 +1462,7 @@ fdalloc(struct thread *td, int minfd, in { struct proc *p = td->td_proc; struct filedesc *fdp = p->p_fd; - int fd = -1, maxfd; + int fd = -1, maxfd, allocfd; #ifdef RACCT int error; #endif @@ -1476,25 +1477,26 @@ fdalloc(struct thread *td, int minfd, in PROC_UNLOCK(p); /* - * Search the bitmap for a free descriptor. If none is found, try - * to grow the file table. Keep at it until we either get a file - * descriptor or run into process or system limits. + * Search the bitmap for a free descriptor starting at minfd. + * If none is found, grow the file table. */ - for (;;) { - fd = fd_first_free(fdp, minfd, fdp->fd_nfiles); - if (fd >= maxfd) - return (EMFILE); - if (fd < fdp->fd_nfiles) - break; + fd = fd_first_free(fdp, minfd, fdp->fd_nfiles); + if (fd >= maxfd) + return (EMFILE); + if (fd >= fdp->fd_nfiles) { + allocfd = min(fd * 2, maxfd); #ifdef RACCT PROC_LOCK(p); - error = racct_set(p, RACCT_NOFILE, - min(fdp->fd_nfiles * 2, maxfd)); + error = racct_set(p, RACCT_NOFILE, allocfd); PROC_UNLOCK(p); if (error != 0) return (EMFILE); #endif - fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd)); + /* + * fd is already equal to first free descriptor >= minfd, so + * we only need to grow the table and we are done. + */ + fdgrowtable(fdp, allocfd); } /*