From patchwork Tue Jul 18 15:22:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jisheng Zhang X-Patchwork-Id: 12222 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1857939vqt; Tue, 18 Jul 2023 09:16:13 -0700 (PDT) X-Google-Smtp-Source: APBJJlFqlf5O/kd23gXSJevMjWsaXQRruxWd6gZdHm5sI/Om4gNfl3xQJ8DVClBrB9O2189tkTlX X-Received: by 2002:a17:902:d4ce:b0:1b8:7e53:70f with SMTP id o14-20020a170902d4ce00b001b87e53070fmr18962809plg.27.1689696973384; Tue, 18 Jul 2023 09:16:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689696973; cv=none; d=google.com; s=arc-20160816; b=P8hkXHF8eS406XviTwWHIP6kNgpnDa16BK9sCBoFu9fB70RNvI3DP4fJmNLlONgXEd NlYlD6JYnKqamjGrZEpHH5/BrtzL30uAjsdhaHQbn2X6hHRDwHQcrA1+i0LLHph0k9Sx mn6LjakExn/k23sQKsxkBbUvvHwBfQP9c+++2gxuhTddmDRKi1tKobHtIs2CqiK5T/vi a28qQodjoR7sr8UqtmxQWC7fIJD2r/LTqmUsY5UMyGnuSSCadm7cYwfaNpXiNb8gWEpX zU1RcHQ2/TVFbq9Z+BEt478PosfYvBVEoi2zrG1p3IbvLxWd3dYfYW4I63c4WeQVXLD5 laoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=TLLnjZ2cgb4BLvYFjK3JaQqLRgohKxvpW851SM2IKB8=; fh=4dEsi7QIwecjtTyQysIed7zhM6Kqdn9yv5jIBHUtcd8=; b=zvPWTkktEBKSnLqDu6t3ZOz1OkOZxRSTylACNRslpRpIfjP9+6kFoNByIobcUbVZWu Rqj3YAVxgcI6yTjwP/yavRFSf+fijN1R9jwTau7pMUBfOvF4ih4B/OK5iz2t03HzbOrN pytd9dqsoDLhR0qJq2rxpEbgrS44Sd20/WFIvs+CpMTObH4ERvU4zNZdTchWZAtOvyCi EDi665WfxoJD2g+GNy8WqNgH8W2El7DGZ0OzuwHsVz3JfLVgMbh9ST8EfsJ1vMYrn538 O6Lhtb1W8nm7SI07slPuqCdimyosDzxohgW71NotBmSJP8Y8hFaSkF+usieTrOEwEGse 6TVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rY51zBkT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u5-20020a170903124500b001b9d2010b27si1933008plh.119.2023.07.18.09.16.00; Tue, 18 Jul 2023 09:16:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rY51zBkT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S233561AbjGRPeX (ORCPT + 99 others); Tue, 18 Jul 2023 11:34:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233674AbjGRPeS (ORCPT ); Tue, 18 Jul 2023 11:34:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC385E7E for ; Tue, 18 Jul 2023 08:33:57 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D53F6614CC for ; Tue, 18 Jul 2023 15:33:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41155C433C8; Tue, 18 Jul 2023 15:33:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689694427; bh=yQK5I0+HMW9nWQ22U27x/b51/ujU5XxLQPYhyGnOtwU=; h=From:To:Cc:Subject:Date:From; b=rY51zBkT/0hoHP7Mm8B5FRNu8nHsPs6l05QAL/UYOYvplcrBu0lt7Ccep8iAUSocT 3RyJwkakKLhWOdVG/4JFr2m8pFubJViamN0uMoamjRd8NK5LKCGAuVEjcAT7X7H9/i O5c2pTqj3dhjefvHCQUo/MrqGpOYdT5whSD5Bi0M/lqIg//ALL+bNs4DVXaR6FHry8 SK4lm9pSHqpHF/NZzNIwDKXL7UNikdDBUAWtU38fjvhWfcofXKDTdqPsTFCg5UzWsW wjUUJ7U3ImkDyyA5m1+aBja5s7I+OVJiTU1XXDbIwXPwObGfqmhh6a+cLjwbSpiM8k CMo80hx/FvQCA== From: Jisheng Zhang To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 0/2] riscv: Reduce ARCH_KMALLOC_MINALIGN to 8 Date: Tue, 18 Jul 2023 23:22:12 +0800 Message-Id: <20230718152214.2907-1-jszhang@kernel.org> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771775693824452127 X-GMAIL-MSGID: 1771775693824452127 Currently, riscv defines ARCH_DMA_MINALIGN as L1_CACHE_BYTES, I.E 64Bytes, if CONFIG_RISCV_DMA_NONCOHERENT=y. To support unified kernel Image, usually we have to enable CONFIG_RISCV_DMA_NONCOHERENT, thus it brings some bad effects to coherent platforms: Firstly, it wastes memory, kmalloc-96, kmalloc-32, kmalloc-16 and kmalloc-8 slab caches don't exist any more, they are replaced with either kmalloc-128 or kmalloc-64. Secondly, larger than necessary kmalloc aligned allocations results in unnecessary cache/TLB pressure. This issue also exists on arm64 platforms. From last year, Catalin tried to solve this issue by decoupling ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN, limiting kmalloc() minimum alignment to dma_get_cache_alignment() and replacing ARCH_KMALLOC_MINALIGN usage in various drivers with ARCH_DMA_MINALIGN etc.[1] One fact we can make use of for riscv: if the CPU doesn't support ZICBOM or T-HEAD CMO, we know the platform is coherent. Based on Catalin's work and above fact, we can easily solve the kmalloc align issue for riscv: we can override dma_get_cache_alignment(), then let it return ARCH_DMA_MINALIGN at the beginning and return 1 once we know the underlying HW neither supports ZICBOM nor supports T-HEAD CMO. So what about if the CPU supports ZICBOM or T-HEAD CMO, but all the devices are dma coherent? Well, we use ARCH_DMA_MINALIGN as the kmalloc minimum alignment, nothing changed in this case. This case can be improved in the future once we see such platforms in mainline. After this patch, a simple test of booting to a small buildroot rootfs on qemu shows: kmalloc-96 5041 5041 96 ... kmalloc-64 9606 9606 64 ... kmalloc-32 5128 5128 32 ... kmalloc-16 7682 7682 16 ... kmalloc-8 10246 10246 8 ... So we save about 1268KB memory. The saving will be much larger in normal OS env on real HW platforms. patch1 allows kmalloc() caches aligned to the smallest value. patch2 enables DMA_BOUNCE_UNALIGNED_KMALLOC. After this series: As for coherent platforms, kmalloc-{8,16,32,96} caches come back on coherent both RV32 and RV64 platforms, I.E !ZICBOM and !THEAD_CMO. As for noncoherent RV32 platforms, nothing changed. As for noncoherent RV64 platforms, I.E either ZICBOM or THEAD_CMO, the above kmalloc caches also come back if > 4GB memory or users pass "swiotlb=mmnn,force" to force swiotlb creation if <= 4GB memory. How much mmnn should be depends on the specific platform, it needs to be tried and tested all possible usage case on the specific hardware. For example, I can use the minimal I/O TLB slabs on Sipeed M1S Dock. Link: https://lore.kernel.org/linux-arm-kernel/20230524171904.3967031-1-catalin.marinas@arm.com/ [1] Since v2: - remove Change-Id - use EXPORT_SYMBOL_GPL instead of EXPORT_SYMBOL - update Link in commit msg per Conor's suggestion - collect reviewed-by tag Since v1 - remove preparation patches since they have been merged - adjust Kconfig entry to keep entries sorted - add new function riscv_set_dma_cache_alignment() to set the dma_cache_alignment var. Jisheng Zhang (2): riscv: allow kmalloc() caches aligned to the smallest value riscv: enable DMA_BOUNCE_UNALIGNED_KMALLOC for !dma_coherent arch/riscv/Kconfig | 1 + arch/riscv/include/asm/cache.h | 14 ++++++++++++++ arch/riscv/include/asm/cacheflush.h | 2 ++ arch/riscv/kernel/setup.c | 1 + arch/riscv/mm/dma-noncoherent.c | 8 ++++++++ 5 files changed, 26 insertions(+)