From owner-freebsd-hackers@FreeBSD.ORG Wed Sep 19 06:10:13 2012 Return-Path: Delivered-To: hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8A28C106566B for ; Wed, 19 Sep 2012 06:10:13 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.zoral.com.ua (mx0.zoral.com.ua [91.193.166.200]) by mx1.freebsd.org (Postfix) with ESMTP id 224CA8FC0C for ; Wed, 19 Sep 2012 06:10:11 +0000 (UTC) Received: from skuns.kiev.zoral.com.ua (localhost [127.0.0.1]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id q8J6AImI002695; Wed, 19 Sep 2012 09:10:18 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.5/8.14.5) with ESMTP id q8J6A6QH042158; Wed, 19 Sep 2012 09:10:06 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.5/8.14.5/Submit) id q8J6A5U9042157; Wed, 19 Sep 2012 09:10:05 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 19 Sep 2012 09:10:05 +0300 From: Konstantin Belousov To: Dag-Erling Sm??rgrav Message-ID: <20120919061005.GR37286@deviant.kiev.zoral.com.ua> References: <86wqzr8hbk.fsf@ds4.des.no> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="0p6fkQojZ86Dx7oG" Content-Disposition: inline In-Reply-To: <86wqzr8hbk.fsf@ds4.des.no> User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: clamav-milter 0.95.2 at skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-4.0 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua Cc: hackers@freebsd.org Subject: Re: fdgrowtable() cleanup X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 19 Sep 2012 06:10:13 -0000 --0p6fkQojZ86Dx7oG Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Sep 18, 2012 at 05:46:23PM +0200, Dag-Erling Sm??rgrav wrote: > The patch below my signature improves the legibility of fdgrowtable(), > and adds comments explaining the hairier bits. The only functional > change is that the code no longer overwrites the old fileflags array > when the old table is placed on the freelist; instead, it uses the space > actually set aside for that purpose. >=20 > (I assume that the old behavior was harmless, since it has persisted for > decades, but it was certainly confusing.) >=20 > The slightly repetitive nature of the new code is intentional. >=20 > DES > --=20 > Dag-Erling Sm??rgrav - des@des.no >=20 > Index: sys/kern/kern_descrip.c > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > --- sys/kern/kern_descrip.c (revision 240654) > +++ sys/kern/kern_descrip.c (working copy) > @@ -148,11 +148,6 @@ > #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES) > =20 > /* > - * Storage required per open file descriptor. > - */ > -#define OFILESIZE (sizeof(struct file *) + sizeof(char)) > - > -/* > * Storage to hold unused ofiles that need to be reclaimed. > */ > struct freetable { > @@ -1436,7 +1431,7 @@ > struct freetable *fo; > struct file **ntable; > struct file **otable; > - char *nfileflags; > + char *nfileflags, *ofileflags; > int nnfiles, onfiles; > NDSLOTTYPE *nmap; > =20 > @@ -1447,33 +1442,46 @@ > =20 > /* compute the size of the new table */ > onfiles =3D fdp->fd_nfiles; > + otable =3D fdp->fd_ofiles; > + ofileflags =3D fdp->fd_ofileflags; These two new calculations could be unused if the function return early. > nnfiles =3D NDSLOTS(nfd) * NDENTRIES; /* round up */ > if (nnfiles <=3D onfiles) > /* the table is already large enough */ > return; > =20 > - /* allocate a new table and (if required) new bitmaps */ > - ntable =3D malloc((nnfiles * OFILESIZE) + sizeof(struct freetable), > + /* > + * Allocate a new table. We need enough space for a) the file > + * entries themselves, b) the file flags, and c) the struct > + * freetable we will use when we decommission the table and place > + * it on the freelist. > + */ > + ntable =3D malloc(nnfiles * sizeof(*ntable) + > + nnfiles * sizeof(*nfileflags) + > + sizeof(struct freetable), > M_FILEDESC, M_ZERO | M_WAITOK); Please use the horizontal space less lavishly. I think that this calculation, as well as fo calculation below, does not take a required alignment of struct freetable into consideration. > nfileflags =3D (char *)&ntable[nnfiles]; > + > + /* allocate new bitmaps if necessary */ > if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) > nmap =3D malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, > M_FILEDESC, M_ZERO | M_WAITOK); > else > nmap =3D NULL; > =20 > - bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable)); > - bcopy(fdp->fd_ofileflags, nfileflags, onfiles); > - otable =3D fdp->fd_ofiles; > + /* copy the old data over and point at the new tables */ > + bcopy(otable, ntable, onfiles * sizeof(*otable)); > + bcopy(ofileflags, nfileflags, onfiles * sizeof(*ofileflags)); > fdp->fd_ofileflags =3D nfileflags; > fdp->fd_ofiles =3D ntable; > + > /* > * We must preserve ofiles until the process exits because we can't > * be certain that no threads have references to the old table via > * _fget(). > */ > if (onfiles > NDFILE) { > - fo =3D (struct freetable *)&otable[onfiles]; > + fo =3D (struct freetable *)(char *)otable + > + onfiles * sizeof(*otable) + onfiles * sizeof(*ofileflags); > fdp0 =3D (struct filedesc0 *)fdp; > fo->ft_table =3D otable; > SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next); > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org" --0p6fkQojZ86Dx7oG Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (FreeBSD) iEUEARECAAYFAlBZYb0ACgkQC3+MBN1Mb4ieowCVGSDK2XOcFD9sTCgsq/ErfCBo rwCeOxO292mbQ0ClxXwS8D97zx0Pc9U= =TBJL -----END PGP SIGNATURE----- --0p6fkQojZ86Dx7oG--