Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3700157ybl; Fri, 20 Dec 2019 14:04:40 -0800 (PST) X-Google-Smtp-Source: APXvYqzVHzngw43LA7u2VS0s4T1WhZwajb8RG9m1GDZR6uayfBDyf9FOzjY3+HpIvhfxA60VS5bu X-Received: by 2002:a9d:7315:: with SMTP id e21mr4038561otk.255.1576879480278; Fri, 20 Dec 2019 14:04:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576879480; cv=none; d=google.com; s=arc-20160816; b=Mo7kCf1DF2RsHuCLfLwYTUjg2l9oCW/qbr3vRgEOzBW8UIGV73OHcOGjAysRAmWLnt eQl8FxlxKUgs12El6ZtC/gwV4/PbxcyESy341hdDVeNbRrQIJ+hQbq3PVw3UuqXYJhQt 216oY12OCvjCGoPkrkujwEER978AE4X/udi15nMJeWxeTj7ZjXTHRsZ2EzNkT8r4BP7c mjQDxiVEb3ZFsvraVE43KM4Z3hYSMgFFgKT56rGb7by3oVGf8XGTlBsgbiHCJfTwBUG4 j53lgKq2a5PGtfyAFmiXWBWJ7jD5VzZO1uCJzqjXc5D62mdmEIfl+tLoBKT/JMxWQiLu Z4sQ== 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=1avOETDoMPwcKyHVJhN1Nt6Wz5vQi5LVnT/P6fMq8Qc=; b=ed1Zhl2PloZTxsLE1XStnTZwApT5EGGxC32Cnpwf4aTlqlTLeCwqjBgxrx3KhN5bDU h/hRAWmnW2lP7Z0lcF+MH1DoGNpMIGgaoti2uzar6f3Mj/3+czXkiHrbJX9rshDMtwIl 0pTre8YXN5tU1MZyw1nTciHLz2OGyxwm5ytJW/Modji6p7YQ3MmlU2AC4AM9kk5JrD2L Rkc2xSeb7NTjSsMSLYBUplV+0stKWEre11ivtFtXqGpWHhm/r9NHMBqyHixHg2IaXHNV A02QAYihf2rqN4lbIxDFTSHP1vZUAfYEGPW7m+ZYFw/2FhpvLVy4/+M77F9G6gViZPD0 U4FA== 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 b6si5716294oiy.116.2019.12.20.14.04.28; Fri, 20 Dec 2019 14:04:40 -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 S1727587AbfLTWCS (ORCPT + 99 others); Fri, 20 Dec 2019 17:02:18 -0500 Received: from foss.arm.com ([217.140.110.172]:55124 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727473AbfLTWCS (ORCPT ); Fri, 20 Dec 2019 17:02:18 -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 893BF30E; Fri, 20 Dec 2019 14:02:17 -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 9CCEC3F67D; Fri, 20 Dec 2019 14:02:15 -0800 (PST) Subject: Re: [RFC PATCH v1] devres: align devres.data strictly only for devm_kmalloc() To: Peter Zijlstra Cc: Marc Gonzalez , 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> <9be1d523-e92c-836b-b79d-37e880d092a0@arm.com> <20191220171359.GP2827@hirez.programming.kicks-ass.net> From: Robin Murphy Message-ID: Date: Fri, 20 Dec 2019 22:02:13 +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: <20191220171359.GP2827@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 On 2019-12-20 5:13 pm, Peter Zijlstra wrote: > On Fri, Dec 20, 2019 at 03:01:03PM +0000, Robin Murphy wrote: >> On 2019-12-20 2:06 pm, Peter Zijlstra wrote: > >>> data = kmalloc(size + sizeof(struct devres), GFP_KERNEL); [ afterthought: size could also legitimately be smaller than a pointer or some odd length such that the necessary alignment of struct devres itself isn't met ] >> 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. > > Wait up, why are you allowing non-coherent DMA at less than page size > granularity? Is that really sane? Is this really supported behaviour for > devm ? There are two DMA APIs - the coherent (or "consistent") API for allocating buffers which are guaranteed safe for random access at any time *is* page-based, and on non-coherent architectures typically involves a non-cacheable remap. There is also the streaming API for one-off transfers of data already existing at a given kernel address (think network packets, USB URBs, etc), which on non-coherent architectures is achieved with explicit cache maintenance plus an API contract that buffers must not be explicitly accessed by CPUs for the duration of the mapping. Addresses from kmalloc() are explicitly valid for dma_map_single() (and indeed are about the only thing you'd ever reasonably feed it), which is the primary reason why ARCH_KMALLOC_MINALIGN gets so big on architectures which can be non-coherent and also suffer from creative cache designs. See DMA-API.txt and DMA-API-HOWTO.txt in Documentation/ for more details if you like. Robin.