From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.3 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47AB3C43603 for ; Fri, 20 Dec 2019 15:01:16 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1335321655 for ; Fri, 20 Dec 2019 15:01:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="lEK7MNtB" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1335321655 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Content-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Dcu4XBe4p/53irEVdCNPI+EzQF71Rqo9U/Yh7gQCXIc=; b=lEK7MNtBOgcBfKY/8UAc/QPUH vSA0ZIXRwyYh8bM1+T11+rEVtOY/aWENcpJUvB9w9L94fjLEg6x3Y9DGF29JBwVpgDLjZKQRDzoc1 38PgVyl+OVWB2b5bOlsOUkzrNYbV7uTB5ykWsVu11/orlWC3C4pMVBW3M0rPtAdANE8s5fTV0SOTc BZCQ3irhnLwD8BqVSXEAUnTcN8vDIoMy9qMuaoj9FYNpHC74u2zs9rewguSag7E55K3d+GImFFor4 6rw60tb0+TMaGlBHvKc/7cZwR7Oipf3HwAi1dYJXJn0VmxBpLlG+Nvdd3o0oja6qFZTGiw2St4qXv fSWolIIAg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iiJmF-0002ba-HM; Fri, 20 Dec 2019 15:01:15 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iiJmD-0002aq-Ds for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2019 15:01:14 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 13E3530E; Fri, 20 Dec 2019 07:01:10 -0800 (PST) Received: from [192.168.1.123] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F18543F6CF; Fri, 20 Dec 2019 07:01:07 -0800 (PST) Subject: Re: [RFC PATCH v1] devres: align devres.data strictly only for devm_kmalloc() To: Peter Zijlstra , Marc Gonzalez References: <74ae22cd-08c1-d846-3e1d-cbc38db87442@free.fr> <20191220140655.GN2827@hirez.programming.kicks-ass.net> From: Robin Murphy Message-ID: <9be1d523-e92c-836b-b79d-37e880d092a0@arm.com> Date: Fri, 20 Dec 2019 15:01:03 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 MIME-Version: 1.0 In-Reply-To: <20191220140655.GN2827@hirez.programming.kicks-ass.net> Content-Language: en-GB X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20191220_070113_515000_0D8DCE0C X-CRM114-Status: GOOD ( 15.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Dmitry Torokhov , Greg Kroah-Hartman , Alexey Brodkin , Rafael Wysocki , LKML , Bjorn Andersson , Russell King , Mark Brown , Tejun Heo , Will Deacon , Linux ARM Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org /me rouses from holiday mode... On 2019-12-20 2:06 pm, Peter Zijlstra wrote: > On Fri, Dec 20, 2019 at 11:19:27AM +0100, Marc Gonzalez wrote: >> Would anyone else have any suggestions, comments, insights, recommendations, >> improvements, guidance, or wisdom? :-) > > Flip devres upside down! Which doesn't really help :( > **WARNING, wear protective glasses when reading the below** > > > struct devres { > struct devres_node node; > void *data; > }; > > /* > * We place struct devres at the tail of the memory allocation > * such that data retains the ARCH_KMALLOC_MINALIGN alignment. > * struct devres itself is just 4 pointers and should therefore > * only require trivial alignment. > */ > static inline struct devres *data2devres(void *data) > { > return (struct devres *)(data + ksize(data) - sizeof(struct devres)); > } > > void *alloc_dr(...) > { > struct devres *dr; > void *data; > > data = kmalloc(size + sizeof(struct devres), GFP_KERNEL); At this point, you'd still need to special-case devm_kmalloc() to ensure size is rounded up to the next ARCH_KMALLOC_MINALIGN granule, or you'd go back to the original problem of the struct devres fields potentially sharing a cache line with the data buffer. That needs to be avoided, because if the devres list is modified while the buffer is mapped for noncoherent DMA (which could legitimately happen as they are nominally distinct allocations with different owners) there's liable to be data corruption one way or the other. No matter which way round you allocate devres and data, by necessity they're always going to consume the same total amount of memory. Robin. > dr = data2devres(data); > WARN_ON((unsigned long)dr & __alignof(*dr)-1); > INIT_LIST_HEAD(&dr->node.entry); > dr->node.release = release; > dr->data = data; > > return dr; > } > > void devres_free(void *data) > { > if (data) { > struct devres *dr = data2devres(data); > BUG_ON(!list_empty(dr->node.entry)); > kfree(data); > } > } > > static int release_nodes(...) > { > ... > list_for_each_entry_safe_reverse(dr, ...) { > ... > kfree(dr->data); > } > } > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel