Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2757018lqp; Mon, 25 Mar 2024 08:24:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVY4BpQeX83f18wc25dijT7GhWacebDD84l3BkayKyUazfMOV3MEOh0i05E94C1Qx6tTadZjNj7EjYDcqj3Lo9WanH42cFMqK6PYsfv+A== X-Google-Smtp-Source: AGHT+IGHqS8Hwf3sHpA7D5YD6z4V/SHcbgPHY5iPeGNPJ0pI27lXZDkpyT/nnOIyozi2jerAtJ1I X-Received: by 2002:a05:6358:9386:b0:17f:5c83:cdd6 with SMTP id h6-20020a056358938600b0017f5c83cdd6mr9059531rwb.2.1711380277022; Mon, 25 Mar 2024 08:24:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711380276; cv=pass; d=google.com; s=arc-20160816; b=BeetQr1epTzDbeozT10GEJQefaBUpfZASexqbUZu9EJ9SCd1I/ZPQLSGy2HP460LgN hW+NIhkQ/O5Yh6bEv1xGzvo6OOVsCFxo21cQB6ygoMdBwR2rGYxvSimQxVa8AogbVj9d cLoFpaWg1CDAGJMatuHVuorCKgtbcC2apfEu13olCLHM9UfdcGftii94RnQDNRmY7vh1 iBsi9JyuURd0DsS5hKZmlJcMVPpbGILL6Lmc7DEJYKz5v9xY5LcxJyzay25+kd8tLqkG wxTBwWUbhqd23ZD7xRw/LB51/QgcnBhoqldnhT+3k1PhbB+7v1PZgqyfD8EMogk9gBW/ sV+Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=UIOeRFhaYP6Vco0L4utOAfxw7G3xqCgFjf+vAykbL4Q=; fh=wPTYEO2TsmNWTzmGeCH5wR08wCQmyRu3mnK3nNjtZOI=; b=Ermew13eTQhOJM5gYiZ37oE6x9BAKXdG/2YNzExiLMBlM/YTaj40APulPnKxYRRl/1 UVpLdnES35AYuZE50tP8CvffLB4wRWmmSAqc5hgfkMrh1YBXm7dU7JEGVhEOuel9BMm3 ftMzUxCASATZbm1sTK1qZZFRWsyjBmimQNPz67CgwxV3reBFxopkd0hGuQ6iqdCVZCA9 2fnLTnO1WE3JavZvV9t6c/IJ54eWNC6WI3GK/MGCFSEZrYudmx9sP1sU/Z0U6/ZISX1D XLmoY/id7J51QSE/xf5BYA1Ot1KMT++Lcll+Bcac8VCJU7RhYielnUCF4GNgJZ0OFsdA r0Pw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="b/hKpDDf"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-116886-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id b12-20020a655ccc000000b005e525ed7336si7858196pgt.85.2024.03.25.08.24.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 08:24:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-116886-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="b/hKpDDf"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-116886-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 8466CBA133F for ; Mon, 25 Mar 2024 14:41:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 022E91BAC49; Mon, 25 Mar 2024 11:30:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="b/hKpDDf" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 100681BAC21 for ; Mon, 25 Mar 2024 11:13:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711365231; cv=none; b=RzodYVjXy9a2NxqPduficj+tDheoHhYu1OOZLFEgaYcS7Ik1UQXW/0/VFxEyj5rA3iaI8UBLyDMWTD2nCytSy8krZ/1SHgwnv5oJony+vsREQotEXex7xxzQ6YOrbwc33rsp0VNi3lykQONzt4gIAO+XbdV+UiULwk3JYrGHbbw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711365231; c=relaxed/simple; bh=sf6mJcuyNikMRdduR4UQlk6A2RQlNed9frvcHXQotOo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ht293Pexmbq+B2s8BTwurXu1EPQQJOKzMKyJmTkupTTPAI538Ncnxbnz4nbVf59xK/QW8bWfSmJAUJxqSg4rqLlJAbl/dkEC+cw+GZb/Ht9Lk3muDdnJY3pEpyj4V3nKpHIH8bXEvEw7cIRJUZ4MDIpr+ekskszWKxk6mh3vKt8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=b/hKpDDf; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 655F0C433C7; Mon, 25 Mar 2024 11:13:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711365230; bh=sf6mJcuyNikMRdduR4UQlk6A2RQlNed9frvcHXQotOo=; h=From:To:Cc:Subject:Date:From; b=b/hKpDDf2yVvm7N4rbZtQBjtnrHFpaBGU1tuIUV3FJrPb+AzjmE8ksDN3KwvvZBNK rpjeuG8OYc0tAjZWSaqBai301lcOAGUOb/kKtkvvSapOkRqniavt6XXBfwCv1efMWW zj/z0cvYEh87mR18EsFoD55kJ9kevJuUwm3r5UE5Wf7MACpyuBGPPZVxWuoHbcxHUZ b9BCFopMO010/aVJYK9pSef+Kmzb4vH3wjCyhp5vKCME7LO/esuThbCG+utkHfeY5w kdhDdrMCQdzr/6xCYMEdrWaES2E3qT6DttCFSDEgSs+jXE2HQJ56+GvLALTekJkiiG RItNXxDm++hKA== From: Jisheng Zhang To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Alexandre Ghiti Subject: [PATCH v3 RESEND] riscv: mm: still create swiotlb buffer for kmalloc() bouncing if required Date: Mon, 25 Mar 2024 19:00:36 +0800 Message-ID: <20240325110036.1564-1-jszhang@kernel.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit After commit f51f7a0fc2f4 ("riscv: enable DMA_BOUNCE_UNALIGNED_KMALLOC for !dma_coherent"), for non-coherent platforms with less than 4GB memory, we rely on users to pass "swiotlb=mmnn,force" kernel parameters to enable DMA bouncing for unaligned kmalloc() buffers. Now let's go further: If no bouncing needed for ZONE_DMA, let kernel automatically allocate 1MB swiotlb buffer per 1GB of RAM for kmalloc() bouncing on non-coherent platforms, so that no need to pass "swiotlb=mmnn,force" any more. The math of "1MB swiotlb buffer per 1GB of RAM for kmalloc() bouncing" is taken from arm64. Users can still force smaller swiotlb buffer by passing "swiotlb=mmnn". Signed-off-by: Jisheng Zhang Reviewed-by: Alexandre Ghiti --- since v2: - rebase on v6.8-rc1 - collect Reviewed-by tag since v1 - fix build error if CONFIG_RISCV_DMA_NONCOHERENT=n arch/riscv/include/asm/cache.h | 2 +- arch/riscv/mm/init.c | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/cache.h b/arch/riscv/include/asm/cache.h index 2174fe7bac9a..570e9d8acad1 100644 --- a/arch/riscv/include/asm/cache.h +++ b/arch/riscv/include/asm/cache.h @@ -26,8 +26,8 @@ #ifndef __ASSEMBLY__ -#ifdef CONFIG_RISCV_DMA_NONCOHERENT extern int dma_cache_alignment; +#ifdef CONFIG_RISCV_DMA_NONCOHERENT #define dma_get_cache_alignment dma_get_cache_alignment static inline int dma_get_cache_alignment(void) { diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index fe8e159394d8..ac5e7f64c05c 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -161,11 +161,25 @@ static void print_vm_layout(void) { } void __init mem_init(void) { + bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit); #ifdef CONFIG_FLATMEM BUG_ON(!mem_map); #endif /* CONFIG_FLATMEM */ - swiotlb_init(max_pfn > PFN_DOWN(dma32_phys_limit), SWIOTLB_VERBOSE); + if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb && + dma_cache_alignment != 1) { + /* + * If no bouncing needed for ZONE_DMA, allocate 1MB swiotlb + * buffer per 1GB of RAM for kmalloc() bouncing on + * non-coherent platforms. + */ + unsigned long size = + DIV_ROUND_UP(memblock_phys_mem_size(), 1024); + swiotlb_adjust_size(min(swiotlb_size_or_default(), size)); + swiotlb = true; + } + + swiotlb_init(swiotlb, SWIOTLB_VERBOSE); memblock_free_all(); print_vm_layout(); -- 2.43.0