Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3293625ybl; Fri, 20 Dec 2019 07:02:26 -0800 (PST) X-Google-Smtp-Source: APXvYqwHIyTFiA4GzqU/EOLdLKyguzkxugmsFyyPtgz5oNBMGhs67wKTaccJmob/Fpi98Q+eKByA X-Received: by 2002:a9d:7d99:: with SMTP id j25mr5915495otn.226.1576854146636; Fri, 20 Dec 2019 07:02:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576854146; cv=none; d=google.com; s=arc-20160816; b=BmDzElBWusPflvovTaqq0ska5jcMDVHLSULgRYdv37Rnit1AmehvLTOBoAfIGPgdlW IJSjBgWI09narxjUQf3k1Wb1rG6pLs59CSKzVKYW7CWmWt6IRycuefRpQnCPw68e304J MR0hpZzFqYPOSTrjYTn7ChXoMnp8SeyUkKLQL2SYCHRxG8fDoru/7j8A0cxwVrq7A++9 NlaXCSThBri1iXRIY1A+kT38bcdmKevSVraAdAEUyFnpDgl05b6qr7jFa3mwNdoT+OlU qH3czIpFnaCWkLrKmPgUaD+IAt1tCU51zYU3C8szdCnCX/M3MyW6PN2TMcQzGL+//4pK WDJw== 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:from:references:cc:to:subject; bh=bQWKc3zUXbSJRmyyhvhUidFbc3+v9lDNgtwLq4JEuPg=; b=SMEtBC2v5FlVUNqyqV0NC+zFujxoix+s5htotolrFhpSs9R+3boWZ5d3z1Vp9okIel E4G4iBmh82qNkL82o8ivpHK4EzzgOqFQP9T3JmUkbjqCYfDoWHryvSEXVsMjWVdW1nNj kQcrA8CSzSCSXSPiBs0iaWXtVaSvNFgGmVRxpao2QLwygjFij6XKsuZGISuClPJiiTg+ n+3FL3o4oLocwBgHsY0uOWDatHQPfZq02zyZ9/0S1KWIZ9l9IL+CBLcBhJOefYyTugPG 5Z3edtpfJ1IMoJpE7Jq4U/3aatV2HFa+J6bEBY5Up0LjPs0DB1vCHbsISQrOqz9V6mj4 6vHA== 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 l17si4861191otk.218.2019.12.20.07.02.11; Fri, 20 Dec 2019 07:02:26 -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 S1727403AbfLTPBL (ORCPT + 99 others); Fri, 20 Dec 2019 10:01:11 -0500 Received: from foss.arm.com ([217.140.110.172]:52118 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727233AbfLTPBL (ORCPT ); Fri, 20 Dec 2019 10:01:11 -0500 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 Cc: Greg Kroah-Hartman , Rafael Wysocki , LKML , Linux ARM , Dmitry Torokhov , Alexey Brodkin , Will Deacon , Russell King , Bjorn Andersson , Tejun Heo , Mark Brown 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-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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); > } > } >