From owner-svn-src-stable-10@FreeBSD.ORG Sun Mar 2 16:25:57 2014 Return-Path: Delivered-To: svn-src-stable-10@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id DAF11436; Sun, 2 Mar 2014 16:25:56 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id B9C9510CC; Sun, 2 Mar 2014 16:25:56 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s22GPurv055002; Sun, 2 Mar 2014 16:25:56 GMT (envelope-from bdrewery@svn.freebsd.org) Received: (from bdrewery@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s22GPuUI055001; Sun, 2 Mar 2014 16:25:56 GMT (envelope-from bdrewery@svn.freebsd.org) Message-Id: <201403021625.s22GPuUI055001@svn.freebsd.org> From: Bryan Drewery Date: Sun, 2 Mar 2014 16:25:56 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r262693 - stable/10/sys/kern X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-10@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: SVN commit messages for only the 10-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 02 Mar 2014 16:25:57 -0000 Author: bdrewery Date: Sun Mar 2 16:25:56 2014 New Revision: 262693 URL: http://svnweb.freebsd.org/changeset/base/262693 Log: MFC r262006,r262328: r262006: Fix M_FILEDESC leak in fdgrowtable() introduced in r244510. fdgrowtable() now only reallocates fd_map when necessary. r262328: Style. Approved by: bapt (mentor, implicit) Modified: stable/10/sys/kern/kern_descrip.c Directory Properties: stable/10/ (props changed) Modified: stable/10/sys/kern/kern_descrip.c ============================================================================== --- stable/10/sys/kern/kern_descrip.c Sun Mar 2 16:04:27 2014 (r262692) +++ stable/10/sys/kern/kern_descrip.c Sun Mar 2 16:25:56 2014 (r262693) @@ -1522,7 +1522,7 @@ fdgrowtable(struct filedesc *fdp, int nf return; /* - * Allocate a new table and map. We need enough space for the + * Allocate a new table. We need enough space for the * file entries themselves and the struct freetable we will use * when we decommission the table and place it on the freelist. * We place the struct freetable in the middle so we don't have @@ -1530,16 +1530,22 @@ fdgrowtable(struct filedesc *fdp, int nf */ ntable = malloc(nnfiles * sizeof(ntable[0]) + sizeof(struct freetable), M_FILEDESC, M_ZERO | M_WAITOK); - nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, M_FILEDESC, - M_ZERO | M_WAITOK); - /* copy the old data over and point at the new tables */ memcpy(ntable, otable, onfiles * sizeof(*otable)); - memcpy(nmap, omap, NDSLOTS(onfiles) * sizeof(*omap)); - - /* update the pointers and counters */ fdp->fd_ofiles = ntable; - fdp->fd_map = nmap; + + /* + * Allocate a new map only if the old is not large enough. It will + * grow at a slower rate than the table as it can map more + * entries than the table can hold. + */ + if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) { + nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, M_FILEDESC, + M_ZERO | M_WAITOK); + /* copy over the old data and update the pointer */ + memcpy(nmap, omap, NDSLOTS(onfiles) * sizeof(*omap)); + fdp->fd_map = nmap; + } /* * In order to have a valid pattern for fget_unlocked() @@ -1555,8 +1561,6 @@ fdgrowtable(struct filedesc *fdp, int nf * reference entries within it. Instead, place it on a freelist * which will be processed when the struct filedesc is released. * - * Do, however, free the old map. - * * Note that if onfiles == NDFILE, we're dealing with the original * static allocation contained within (struct filedesc0 *)fdp, * which must not be freed. @@ -1566,8 +1570,14 @@ fdgrowtable(struct filedesc *fdp, int nf fdp0 = (struct filedesc0 *)fdp; ft->ft_table = otable; SLIST_INSERT_HEAD(&fdp0->fd_free, ft, ft_next); - free(omap, M_FILEDESC); } + /* + * The map does not have the same possibility of threads still + * holding references to it. So always free it as long as it + * does not reference the original static allocation. + */ + if (NDSLOTS(onfiles) > NDSLOTS(NDFILE)) + free(omap, M_FILEDESC); } /*