From owner-freebsd-current@FreeBSD.ORG Thu Nov 28 13:34:15 2013 Return-Path: Delivered-To: freebsd-current@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 B37481DF for ; Thu, 28 Nov 2013 13:34:15 +0000 (UTC) Received: from plane.gmane.org (plane.gmane.org [80.91.229.3]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 6FDE41E39 for ; Thu, 28 Nov 2013 13:34:15 +0000 (UTC) Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Vm1jF-0007w6-LX for freebsd-current@freebsd.org; Thu, 28 Nov 2013 14:34:03 +0100 Received: from 79-139-19-75.prenet.pl ([79.139.19.75]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 28 Nov 2013 14:34:01 +0100 Received: from jb.1234abcd by 79-139-19-75.prenet.pl with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 28 Nov 2013 14:34:01 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: freebsd-current@freebsd.org From: jb Subject: Re: [RFC] how to get the size of a malloc(9) block ? Date: Thu, 28 Nov 2013 13:33:41 +0000 (UTC) Lines: 50 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 79.139.19.75 (Mozilla/5.0 (X11; Linux i686; rv:25.0) Gecko/20100101 Firefox/25.0) X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Nov 2013 13:34:15 -0000 Luigi Rizzo iet.unipi.it> writes: > > in porting some linux kernel code to FreeBSD we > stumbled upon ksize(), which returns the > actual size of a kmalloc() block. > > We could easily implement it as the first part > of realloc(9) -- see kern/kern_malloc.c > > Would it make sense to add this to the malloc(9) API ? > The userspace equivalent seems to be > malloc_usable_size(3) which according to the > manpage appeared in FreeBSD 7.0 Hi, The implementation of ksize() depends on (has non-standard behavior across) architectures, memory allocators, page sizes, C libs, etc. It means, ksize() exports its implementation details to the caller, and this disqualifies it, regardless whether in kernel or user spaces. This leads to dangerous and conflicting assertions: malloc_usable_size(3) on Linux: NOTES The value returned by malloc_usable_size() may be greater than the requested size of the allocation because of alignment and minimum size constraints. Although the excess bytes can be overwritten by the application without ill effects, this is not good programming practice: the number of excess bytes in an allocation depends on the underlying implementation. The main use of this function is for debugging and introspection. Other sources: ... The caller may use this additional memory, even though a smaller amount of memory was initially specified with the kmalloc call. ... This is hair-raising: http://lwn.net/Articles/319686/ Result ? The same code works here, but may not elsewhere. It follows, you should remove malloc_usable_size() from user space instead. jb