From owner-freebsd-hackers@FreeBSD.ORG Wed Jun 13 20:24:53 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id E5DE6106564A; Wed, 13 Jun 2012 20:24:53 +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 80C768FC08; Wed, 13 Jun 2012 20:24:53 +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 q5DKOjS4040217; Wed, 13 Jun 2012 23:24:45 +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 q5DKOiCg056193; Wed, 13 Jun 2012 23:24:44 +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 q5DKOiTv056192; Wed, 13 Jun 2012 23:24:44 +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, 13 Jun 2012 23:24:44 +0300 From: Konstantin Belousov To: alc@freebsd.org Message-ID: <20120613202444.GW2337@deviant.kiev.zoral.com.ua> References: <1339259223.36051.328.camel@revolution.hippie.lan> <20120609165217.GO85127@deviant.kiev.zoral.com.ua> <1339512694.36051.362.camel@revolution.hippie.lan> <20120612204508.GP2337@deviant.kiev.zoral.com.ua> <1339593249.73426.5.camel@revolution.hippie.lan> <20120613191205.GU2337@deviant.kiev.zoral.com.ua> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="Fw8vdPO5iEPGjqL+" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i 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: Ian Lepore , freebsd-hackers@freebsd.org, Wojciech Puchar Subject: Re: Rtld object tasting [Was: Re: wired memory - again!] 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, 13 Jun 2012 20:24:54 -0000 --Fw8vdPO5iEPGjqL+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 13, 2012 at 03:17:56PM -0500, Alan Cox wrote: > On Wed, Jun 13, 2012 at 2:12 PM, Konstantin Belousov wrote: >=20 > > On Wed, Jun 13, 2012 at 07:14:09AM -0600, Ian Lepore wrote: > > > http://lists.freebsd.org/pipermail/freebsd-arm/2012-January/003288.ht= ml > > > > The map_object.c patch is step in the almost right direction, I wanted > > to remove the static page-sized buffer from get_elf_header for long tim= e. > > It works because rtld always holds bind_lock exclusively while loading > > an object. There is no need to copy the first page after it is mapped. > > > > commit 0f6f8629af1345acded7c0c685d3ff7b4d9180d6 > > Author: Konstantin Belousov > > Date: Wed Jun 13 22:04:18 2012 +0300 > > > > Eliminate the static buffer used to read the first page of the mapped > > object, and eliminate the pread(2) call as well. Mmap the first page > > of the object temporaly, and unmap it on error or last use. > > > > Fix several cases were the whole mapping of the object leaked on err= or. > > > > Potentially, this leaves one-page gap between succeeding dlopen(3), > > but there are other mmap(2) consumers as well. > > > > > I suggest adding MAP_PREFAULT_READ to the mmap(2) call. A heuristic in > vm_map_pmap_enter() would trigger automatic mapping for small files, but = if > the object file is larger than 96 pages then you need to explicitly > specific MAP_PREFAULT_READ. Thank you for the suggestion. commit 77de77b3124fb2742db1db72e9dfc47050c5ac36 Author: Konstantin Belousov Date: Wed Jun 13 23:22:27 2012 +0300 Use MAP_PREFAULT_READ for mmap(2) calls which map real object pages. =20 Suggested by: alc diff --git a/libexec/rtld-elf/map_object.c b/libexec/rtld-elf/map_object.c index 2afc88c..437e7c2 100644 --- a/libexec/rtld-elf/map_object.c +++ b/libexec/rtld-elf/map_object.c @@ -200,7 +200,7 @@ map_object(int fd, const char *path, const struct stat = *sb) data_prot =3D convert_prot(segs[i]->p_flags); data_flags =3D convert_flags(segs[i]->p_flags) | MAP_FIXED; if (mmap(data_addr, data_vlimit - data_vaddr, data_prot, - data_flags, fd, data_offset) =3D=3D (caddr_t) -1) { + data_flags | MAP_PREFAULT_READ, fd, data_offset) =3D=3D (caddr_t) -1) { _rtld_error("%s: mmap of data failed: %s", path, rtld_strerror(errno)); goto error1; @@ -307,7 +307,8 @@ get_elf_header(int fd, const char *path) { Elf_Ehdr *hdr; =20 - hdr =3D mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); + hdr =3D mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ, + fd, 0); if (hdr =3D=3D (Elf_Ehdr *)MAP_FAILED) { _rtld_error("%s: read error: %s", path, rtld_strerror(errno)); return NULL; --Fw8vdPO5iEPGjqL+ Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (FreeBSD) iEYEARECAAYFAk/Y9wwACgkQC3+MBN1Mb4idbQCg7wAMgJwU32kWoQD1fpiJEk4e mkMAoLINfWk04aKNm9eUgihRE8Ta7nxw =cZTm -----END PGP SIGNATURE----- --Fw8vdPO5iEPGjqL+--