From owner-freebsd-hackers@FreeBSD.ORG Thu Feb 20 08:05:56 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 5333CE9E for ; Thu, 20 Feb 2014 08:05:56 +0000 (UTC) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) (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 1F4A71086; Thu, 20 Feb 2014 08:05:56 +0000 (UTC) Received: from xyf.my.dom (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.7/8.14.7) with ESMTP id s1K85sHi032465; Thu, 20 Feb 2014 08:05:54 GMT (envelope-from davidxu@freebsd.org) Message-ID: <5305B786.8020708@freebsd.org> Date: Thu, 20 Feb 2014 16:06:30 +0800 From: David Xu User-Agent: Mozilla/5.0 (X11; FreeBSD i386; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Erich Dollansky Subject: Re: pthread programming eats up resources (My or FreeBSD's fault?) References: <20140218180646.GA67861@schlappy> <53059574.8090605@freebsd.org> <20140220140644.7b1e0074@X220.alogt.com> In-Reply-To: <20140220140644.7b1e0074@X220.alogt.com> Content-Type: multipart/mixed; boundary="------------060006050809000708040202" Cc: freebsd-hackers@freebsd.org, Andre Albsmeier X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Feb 2014 08:05:56 -0000 This is a multi-part message in MIME format. --------------060006050809000708040202 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 2014/02/20 14:06, Erich Dollansky wrote: > Hi, > > On Thu, 20 Feb 2014 13:41:08 +0800 > David Xu wrote: > >> On 2014/02/19 02:06, Andre Albsmeier wrote: >> >> please compile it as static binary and run it, check if the >> problem still exists, I am hunting the bug, it is not necessary in >> the libthr because I have not changed its code for a long time. > > I just compiled is a static program. The behaviour is now different. > The size still grows but much slower while 'res' stays below some 10MB. > > Size also got stagnant after some 2 min CPU time hanging around 126MB. > > I am running it on: > > FreeBSD X220.alogt.com 10.0-STABLE FreeBSD 10.0-STABLE #15 r261342: Sat > Feb 1 14:52:39 WITA 2014 > erich@X220.alogt.com:/usr/obj/usr/src/sys/X220 amd64 > > Erich > I have found the bug, it is in rtld, where malloc_aligned() is misfunctioning, memory can be corrupted by the function. libthr calls _rtld_allocate_tls to allocate tls control block, the function is in rtld, its uses malloc_aligned() which is not working correctly. Patch is attached. Regards, David Xu --------------060006050809000708040202 Content-Type: text/plain; charset=gbk; name="rtld_align_malloc.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="rtld_align_malloc.diff" Index: libexec/rtld-elf/xmalloc.c =================================================================== --- libexec/rtld-elf/xmalloc.c (revision 260700) +++ libexec/rtld-elf/xmalloc.c (working copy) @@ -72,14 +72,10 @@ malloc_aligned(size_t size, size_t align) { void *mem, *res; - uintptr_t x; - size_t asize, r; - r = round(sizeof(void *), align); - asize = round(size, align) + r; - mem = xmalloc(asize); - x = (uintptr_t)mem; - res = (void *)round(x, align); + mem = xmalloc(size + sizeof(void *) + align - 1); + res =(void*)(((uintptr_t)mem + sizeof(void *) + align - 1) & + ~(align - 1)); *(void **)((uintptr_t)res - sizeof(void *)) = mem; return (res); } --------------060006050809000708040202--