Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933945AbcJ0NtK (ORCPT ); Thu, 27 Oct 2016 09:49:10 -0400 Received: from mail-lf0-f67.google.com ([209.85.215.67]:35198 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933084AbcJ0NtI (ORCPT ); Thu, 27 Oct 2016 09:49:08 -0400 MIME-Version: 1.0 In-Reply-To: <3334796.z7DWQtPDNS@wuerfel> References: <20161026191810.12275-1-dh.herrmann@gmail.com> <20161027123726.GD3175@twins.programming.kicks-ass.net> <3334796.z7DWQtPDNS@wuerfel> From: David Herrmann Date: Thu, 27 Oct 2016 15:31:55 +0200 Message-ID: Subject: Re: [RFC v1 04/14] bus1: util - fixed list utility library To: Arnd Bergmann Cc: Peter Zijlstra , linux-kernel , Andy Lutomirski , Jiri Kosina , Greg KH , Hannes Reinecke , Steven Rostedt , Tom Gundersen , Josh Triplett , Linus Torvalds , Andrew Morton Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1633 Lines: 44 Hi On Thu, Oct 27, 2016 at 2:56 PM, Arnd Bergmann wrote: > On Thursday, October 27, 2016 2:48:46 PM CEST David Herrmann wrote: >> On Thu, Oct 27, 2016 at 2:37 PM, Peter Zijlstra wrote: >> > On Wed, Oct 26, 2016 at 09:18:00PM +0200, David Herrmann wrote: >> >> + e = kmalloc_array(sizeof(*e), BUS1_FLIST_BATCH + 1, gfp); >> > >> >> +#define BUS1_FLIST_BATCH (1024) >> > >> >> +struct bus1_flist { >> >> + union { >> >> + struct bus1_flist *next; >> >> + void *ptr; >> >> + }; >> >> +}; >> > >> > So that's an allocation of 8*(1024+1), or slightly more than 2 pages. >> > >> > kmalloc will round up to the next power of two, so you'll end up with an >> > allocation of 16*1024, wasting a whopping 8184 bytes per such allocation >> > in slack space. >> > >> > Please consider using 1023 or something for your batch size, 511 would >> > get you to exactly 1 page which would be even better. >> >> Thanks for the hint! 511 looks like the obvious choice. Maybe even >> (PAGE_SIZE / sizeof(long) - 1). I will put a comment next to the >> definition. >> >> > > PAGE_SIZE can be up to 64KB though, so that might lead wasting a lot > of memory. The bus1-flist implementation never over-allocates. It is a fixed size list, so it only allocates as much memory as needed. The issue PeterZ pointed out is passing suitable sizes to kmalloc(), which internally over-allocates to power-of-2 bounds (or some similar bounds). So we only ever waste space here if kmalloc() internally rounds up. The code in bus1-flist allocates exactly the needed space. Thanks David