Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp2547282pxt; Mon, 9 Aug 2021 03:17:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/7sO+sCCWKRRmh2Bj+9+imvK0sLaz3eIng+8ZT05lUuyb+F9LRHE45+FTsUJFylbePha/ X-Received: by 2002:a17:906:cf91:: with SMTP id um17mr5196337ejb.490.1628504261513; Mon, 09 Aug 2021 03:17:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628504261; cv=none; d=google.com; s=arc-20160816; b=pmwiVssV852W18UIoGKjc7HhZ9tARvf7QwYQI9+fTbybd1T9mTcSYfzxON5Ydgw4Kc WsPzAXOEJyUtNtmmXIu6bwcBrQOAWsJB8FC/4+oqWRvvoUnHusXpjemVP4iM4KwPIgGa VkTXalHx2HJ7UKc5l21EtHK6qw/hO3eA5/LzA9Di0MjY7vSDMfJtm71IxF5Y3rWuzmBR qScfaMZ3MHnQw4v4dbQR5oWjvtB4lIZgpjoW/QweLPQaTMAo4z/rLVCt02QUjCMFQS0l Snp1HkqV9m3FIpkftUdGk5NKru44aC//aDv6k4AUwajLXhk9WQlMdjAqziMqcLZN/keJ 8jbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=8q+pHnl9SthD7L9VCimaeesPg4nIcOV9+xsvK6ckShU=; b=xZdpfIIV4cjrG1yPX/HEXE1XyTDSGjAaPjs+kJWCA2gOqcYYIp7iBj5F+b7AxARICe +me6RPUmoEceFCIViA5m9dAI2yyJJKOPOxeFlIgKuOmQKbIiNVBNwjy18oXJmB2gY3NN VB7Ki44bLyKK47kkpnd5n9smzBuFSAFDAEq3LxgdTVY0OHZsvlpar8RGMoLyJ0g/oR8G cc/GqCJQhEuwuBHCd8OMVVBXrp5V6Q4DEzY6iU6YDPydlYXRli8O9TC7LzpAlR6yEtd4 vJPoTIY1jdkE6/nf3PmruZjadPgdJ6vel8/zMQXSwdniVlPhtMJqk2TIUt7VYacB/cOH gRnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZXkLPeDS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id de22si16553661ejc.18.2021.08.09.03.17.17; Mon, 09 Aug 2021 03:17:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZXkLPeDS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233559AbhHIHt4 (ORCPT + 99 others); Mon, 9 Aug 2021 03:49:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:42928 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233533AbhHIHt4 (ORCPT ); Mon, 9 Aug 2021 03:49:56 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 029DE61056 for ; Mon, 9 Aug 2021 07:49:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628495376; bh=q/S/ccLXHytgWzYsEEYPeb1axvMFlx+hkfDx9JyqjhI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ZXkLPeDSnu/5fSVRxKq0MAg7wIhU9SY3SUjybAvltMCLBCcoXhz08WyVaEetNEy8O eyMorI6OqeuXLwlS4DFnzwkr/53N5tAv/hau8SIZ2UcyM00eKIEXtPQapzBSomI/s6 IB0VrFfCIJV4LcEnhMlmTJArCU0HpC3W/D3EaNtEqG7jXOMJ/7jTcM5ELT7KXHAQgO 3kC8CaJKV3XrUVg+/Fzds11q7/0sDUpAS2UZoxvHra0RbbxZTmZoQgtVO+li9uoVdr ZbD7NiTkgToVF3oc4zdn63svc3dtk75DvHp5JKmqdPRhn68B2iAxM13/xy9KMk+AdI wFhqRAgwA/Pug== Received: by mail-wr1-f47.google.com with SMTP id k29so7332456wrd.7 for ; Mon, 09 Aug 2021 00:49:35 -0700 (PDT) X-Gm-Message-State: AOAM533DZVYV14bImdZ2uFVI5m6vrq7oS8lMl/k8tfJIF5N/dypBFfRb AgO7/S/5hN5PXowQKipOx6TgOU6SRswMzwBq1+8= X-Received: by 2002:adf:fd90:: with SMTP id d16mr24684354wrr.105.1628495374617; Mon, 09 Aug 2021 00:49:34 -0700 (PDT) MIME-Version: 1.0 References: <20210807145537.124744-1-xianting.tian@linux.alibaba.com> <20210809003044.6692ddce@xhacker> In-Reply-To: From: Arnd Bergmann Date: Mon, 9 Aug 2021 09:49:18 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] riscv: add ARCH_DMA_MINALIGN support To: Xianting TIan Cc: Jisheng Zhang , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 9, 2021 at 8:20 AM Xianting TIan wrote: > > >> +#define ARCH_DMA_MINALIGN L1_CACHE_BYTES > > It's not a good idea to blindly set this for all riscv. For "coherent" > > platforms, this is not necessary and will waste memory. > > I checked ARCH_DMA_MINALIGN definition, "If an architecture isn't fully > DMA-coherent, ARCH_DMA_MINALIGN must be set". > > so that the memory allocator makes sure that kmalloc'ed buffer doesn't > share a cache line with the others. > > Documentation/core-api/dma-api-howto.rst > > 2) ARCH_DMA_MINALIGN > > Architectures must ensure that kmalloc'ed buffer is > DMA-safe. Drivers and subsystems depend on it. If an architecture > isn't fully DMA-coherent (i.e. hardware doesn't ensure that data in > the CPU cache is identical to data in main memory), > ARCH_DMA_MINALIGN must be set so that the memory allocator > makes sure that kmalloc'ed buffer doesn't share a cache line with > the others. See arch/arm/include/asm/cache.h as an example. > > Note that ARCH_DMA_MINALIGN is about DMA memory alignment > constraints. You don't need to worry about the architecture data > alignment constraints (e.g. the alignment constraints about 64-bit > objects). The platform spec [1] says about this: | Memory accesses by I/O masters can be coherent or non-coherent | with respect to all hart-related caches. So the kernel in its default configuration can not assume that DMA is cache coherent on RISC-V. Making this configurable implies that a kernel that is configured for cache-coherent machines can no longer run on all hardware that follows the platform spec. We have the same problem on arm64, where most of the server parts are cache coherent, but the majority of the low-end embedded devices are not, and we require that a single kernel ran run on all of the above. One idea that we have discussed several times is to start the kernel without the small kmalloc caches and defer their creation until a later point in the boot process after determining whether any non-coherent devices have been discovered. Any in-kernel structures that have an explicit ARCH_DMA_MINALIGN alignment won't benefit from this, but any subsequent kmalloc() calls can use the smaller caches. The tricky bit is finding out whether /everything/ on the system is cache-coherent or not, since we do not have a global flag for that in the DT. See [2] for a recent discussion. Arnd [1] https://github.com/riscv/riscv-platform-specs/blob/main/riscv-platform-spec.adoc#architecture [2] https://lore.kernel.org/linux-arm-kernel/20210527124356.22367-1-will@kernel.org/