From owner-freebsd-hackers@FreeBSD.ORG Sun Oct 2 13:19:58 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 38A071065673; Sun, 2 Oct 2011 13:19:58 +0000 (UTC) (envelope-from davide.italiano@gmail.com) Received: from mail-vx0-f182.google.com (mail-vx0-f182.google.com [209.85.220.182]) by mx1.freebsd.org (Postfix) with ESMTP id D3AB08FC0C; Sun, 2 Oct 2011 13:19:57 +0000 (UTC) Received: by vcbf13 with SMTP id f13so3165971vcb.13 for ; Sun, 02 Oct 2011 06:19:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=Yia7roXR//u6yMTPx8629QUDp1OIn81/i9vT5JMZkH4=; b=hsyfWCE8nk4PHT+qYV2grvS0INqXNE9f2zfSNLg93CSWEQ8OdAjy3Aw5kon5IvYA2w 8gdVA4QZc/5lpGxdzfCFoXFHH886QTC+Y5UH1UrDckaQz6Dvw5cvKKZSl3Q1uHkleqac o3C9AeymQkuy4X44H9CVwjwiGVWiPTa3FL62E= MIME-Version: 1.0 Received: by 10.52.98.199 with SMTP id ek7mr13467275vdb.433.1317560268123; Sun, 02 Oct 2011 05:57:48 -0700 (PDT) Received: by 10.52.179.228 with HTTP; Sun, 2 Oct 2011 05:57:48 -0700 (PDT) In-Reply-To: <358651269.20111002162109@serebryakov.spb.ru> References: <358651269.20111002162109@serebryakov.spb.ru> Date: Sun, 2 Oct 2011 14:57:48 +0200 Message-ID: From: Davide Italiano To: lev@freebsd.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-hackers@freebsd.org Subject: Re: Memory allocation in kernel -- what to use in which situation? What is the best for page-sized allocations? 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: Sun, 02 Oct 2011 13:19:58 -0000 2011/10/2 Lev Serebryakov : > Hello, Freebsd-hackers. > > =A0Here are several memory-allocation mechanisms in the kernel. The two > I'm aware of is MALLOC_DEFINE()/malloc()/free() and uma_* (zone(9)). > > =A0As far as I understand, malloc() is general-purpose, but it has > fixed "transaction cost" (in term of memory consumption) for each > block allocated, and is not very suitable for allocation of many small > blocks, as lots of memory will be wasted for bookkeeping. > > =A0zone(9) allocator, on other hand, have very low cost of each > allocated block, but could allocate only pre-configured fixed-size > blocks, and ideal for allocation tons of small objects (and provide > API for reusing them, too!). > > =A0Am I right? > > =A0 But what if I need to allocate a lot (say, 16K-32K) of page-sized > blocks? Not in one chunk, for sure, but in lifetime of my kernel > module. Which allocator should I use? It seems, the best one will be > very low-level only-page-sized allocator. Is here any in kernel? > > -- > // Black Lion AKA Lev Serebryakov > > _______________________________________________ > 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= " > My 2cents: Everytime you request a certain amount of memory bigger than 4KB using kernel malloc(), it results in a direct call to uma_large_malloc(). Right now, uma_large_malloc() calls kmem_malloc() (i.e. the memory is requested to the VM directly). This kind of approach has two main drawbacks: 1) it heavily fragments the kernel heap 2) when free() is called on these multipage chunks, it in turn calls uma_large_free(), which immediately calls the VM system to unmap and free the chunk of memory. The unmapping requires a system-wide TLB shootdown, i.e. a global action by every processor in the system. I'm currently working supervised by alc@ to an intermediate layer that sits between UMA and the VM, which goal is satisfyinh efficiently requests > 4KB (so, the one you want considering you're asking for 16KB-32KB), but the work is in an early stage. Best, Davide