Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3007605ybl; Fri, 20 Dec 2019 02:20:33 -0800 (PST) X-Google-Smtp-Source: APXvYqyhza3SSyHCRwNgK6+Weob7bHbUFbzIjMMcvkRfEbKKLz3cPlAUNTOVHJPMse5uSoycwvhu X-Received: by 2002:a9d:2dca:: with SMTP id g68mr13077602otb.150.1576837233535; Fri, 20 Dec 2019 02:20:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576837233; cv=none; d=google.com; s=arc-20160816; b=NhY8dDQCOsZeqlDVkEaiYi0qUOSJB7YUiKx3F2nUHsRCu36xAY3yP968nIm+0Ek1r0 b2RjYApId42UpSFkklzvrEoV2X6Ldh4TeQarc/K8DcGdpZS8JiXRNKLeuon/bD4UwLPd le7F/XKxkMwekrK9CLaUvVEwLqRDbzIg8+p2xdWQZZNM5p3LRKLNBOwUB9zwycYwrbvt kq+Ec8KjQkIogYcp5DiXgoTZDKs1ATlV61nUI5/EdNGtvOlw0viC8ANRuN+fcRZ8Ga5Z 8xElEDn4+WCQMIXDCTYiQ67LtCn8QZq/1OdKhLfmGBEhVoylGwIgGfs7lXJU/pvYH9aS Z8NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:from:subject; bh=80ab1MthYtrBtIEfDNi72aLhNFAzThucYBpzwUkFAWc=; b=IA2FYfwfxKRGOir/ooAdhRv/RWlqsUjg7552o/+1Ln8p1tR6VSkr9i8sA2xvzoNchm D1Nu2JD4/RAMqK6n3fGSuLLlqQFdgSINoWa7quIWD+vvPMQKuOsCVTBbaetrf0uMuE7o y57yX+VhwONbgrzj/xdMyF05G84t4rOkOijIrqGJZkV+8pSSF1RmfiPACQjdldY1KHmK 83ttk3WfPHmqDtaOEmIjmQjzZkLfeZGgoz+2DsTXgJEPvdLynqNBW7WnRhW/OgppbOot XHua4/G2btX8j2V1KuK+b0UPUvrBzHoT2twT/jLBzDkySRGGRhJbdb7cMunjStI+P5+B Ud3w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j24si2196519otn.110.2019.12.20.02.20.22; Fri, 20 Dec 2019 02:20:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727351AbfLTKT3 (ORCPT + 99 others); Fri, 20 Dec 2019 05:19:29 -0500 Received: from ns.iliad.fr ([212.27.33.1]:48074 "EHLO ns.iliad.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727129AbfLTKT3 (ORCPT ); Fri, 20 Dec 2019 05:19:29 -0500 Received: from ns.iliad.fr (localhost [127.0.0.1]) by ns.iliad.fr (Postfix) with ESMTP id 1F5352061F; Fri, 20 Dec 2019 11:19:28 +0100 (CET) Received: from [192.168.108.51] (freebox.vlq16.iliad.fr [213.36.7.13]) by ns.iliad.fr (Postfix) with ESMTP id EEA5F20348; Fri, 20 Dec 2019 11:19:27 +0100 (CET) Subject: Re: [RFC PATCH v1] devres: align devres.data strictly only for devm_kmalloc() From: Marc Gonzalez To: Greg Kroah-Hartman , Rafael Wysocki , LKML , Linux ARM Cc: Robin Murphy , Dmitry Torokhov , Alexey Brodkin , Peter Zijlstra , Will Deacon , Russell King , Bjorn Andersson , Tejun Heo , Mark Brown References: <74ae22cd-08c1-d846-3e1d-cbc38db87442@free.fr> Message-ID: Date: Fri, 20 Dec 2019 11:19:27 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: <74ae22cd-08c1-d846-3e1d-cbc38db87442@free.fr> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV using ClamSMTP ; ns.iliad.fr ; Fri Dec 20 11:19:28 2019 +0100 (CET) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 17/12/2019 16:30, Marc Gonzalez wrote: > Commit a66d972465d15 ("devres: Align data[] to ARCH_KMALLOC_MINALIGN") > increased the alignment of devres.data unconditionally. > > Some platforms have very strict alignment requirements for DMA-safe > addresses, e.g. 128 bytes on arm64. There, struct devres amounts to: > 3 pointers + pad_to_128 + data + pad_to_256 > i.e. ~220 bytes of padding. > > Let's enforce the alignment only for devm_kmalloc(). > > Suggested-by: Robin Murphy > Signed-off-by: Marc Gonzalez > --- > I had not been aware that dynamic allocation granularity on arm64 was > 128 bytes. This means there's a lot of waste on small allocations. > I suppose there's no easy solution, though. > --- > drivers/base/devres.c | 23 +++++++++++++---------- > 1 file changed, 13 insertions(+), 10 deletions(-) > > diff --git a/drivers/base/devres.c b/drivers/base/devres.c > index 0bbb328bd17f..bf39188613d9 100644 > --- a/drivers/base/devres.c > +++ b/drivers/base/devres.c > @@ -26,14 +26,7 @@ struct devres_node { > > struct devres { > struct devres_node node; > - /* > - * Some archs want to perform DMA into kmalloc caches > - * and need a guaranteed alignment larger than > - * the alignment of a 64-bit integer. > - * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same > - * buffer alignment as if it was allocated by plain kmalloc(). > - */ > - u8 __aligned(ARCH_KMALLOC_MINALIGN) data[]; > + u8 data[]; > }; > > struct devres_group { > @@ -789,9 +782,16 @@ static void devm_kmalloc_release(struct device *dev, void *res) > /* noop */ > } > > +#define DEVM_KMALLOC_PADDING_SIZE \ > + (ARCH_KMALLOC_MINALIGN - sizeof(struct devres) % ARCH_KMALLOC_MINALIGN) > + > static int devm_kmalloc_match(struct device *dev, void *res, void *data) > { > - return res == data; > + /* > + * 'res' is dr->data (not DMA-safe) > + * 'data' is the hand-aligned address from devm_kmalloc > + */ > + return res + DEVM_KMALLOC_PADDING_SIZE == data; > } > > /** > @@ -811,6 +811,9 @@ void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) > { > struct devres *dr; > > + /* Add enough padding to provide a DMA-safe address */ > + size += DEVM_KMALLOC_PADDING_SIZE; > + > /* use raw alloc_dr for kmalloc caller tracing */ > dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev)); > if (unlikely(!dr)) > @@ -822,7 +825,7 @@ void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) > */ > set_node_dbginfo(&dr->node, "devm_kzalloc_release", size); > devres_add(dev, dr->data); > - return dr->data; > + return dr->data + DEVM_KMALLOC_PADDING_SIZE; > } > EXPORT_SYMBOL_GPL(devm_kmalloc); Would anyone else have any suggestions, comments, insights, recommendations, improvements, guidance, or wisdom? :-) I keep thinking about the memory waste caused by the strict alignment requirement on arm64. Is there a way to inspect how much memory has been requested vs how much has been allocated? (Turning on SLAB DEBUG perhaps?) Couldn't there be a kmalloc flag saying "this alloc will not require strict alignment, so just give me something 8-byte aligned" ? Regards.