Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp716589rwd; Wed, 7 Jun 2023 06:06:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5gxKm/WuFKD7ssc6i09aM4M5gyNO0cMUrtIBXdcFmfBp/0m6MmeJCvWcJS8mT21U+CaRqh X-Received: by 2002:a05:6a20:1585:b0:10e:de4f:3437 with SMTP id h5-20020a056a20158500b0010ede4f3437mr3330590pzj.39.1686143183908; Wed, 07 Jun 2023 06:06:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686143183; cv=none; d=google.com; s=arc-20160816; b=G4GfLobhlGSNfHq6aXd8PYb1Ojnqh9HGeXmBATzBJacN031gylRj5xYWIMTT/qSMqO zICiY/d60XKMzlqrJZ1iUOIUPCXvwdSZi7moHUX2+xYVK7t29aTz38T0rnefD50YDUFk 3hWzZvAR0AaSutMve5uRcld5EPqhEGOAjBwqbfh1gLVrJykMHtDKsRJtoHm89LlDSlco dCe6u0gju1Hdmm/hiALYKYDFV2BiDtJWJeXsUpGpXGwpCKYMS4ZBVdVpKlf5sUcOxNLL d4rWWaZ7+PWmRXmE2QjPwpT1VQ34dXqvtIi6RdMxXkIinns7YHWT8c9obY4oz88pJnil RMeQ== 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=VJowdc9e59oB9wLZyR+086niExyuveZY6dunY32SgSw=; b=FhXpqcGEVu4JM2ieTUS3iecIdIh47o+4ftl3UWsjEx08ht9sHujitukaDdlUpc+n39 DHjmylXLbvSKokmbZLz3AC8nWlLiCz/hpnjFRJNAfDrFrF9C/CyV0T4N97tBtfnx5LPl tJPFYVObEEpbgFZL72CFOigTrW+JRM3BduWSwUG+5J7IQtYx+drf9ZAZXYlJw/xkb3y8 hI7E7BhzF56wXDuWUR/3ecpkcR0muDpQjCjXvje6Cy0Tg9mEtI9udJOzULoLdlqnxl4f SoN2JK4ULouTh80KEcO02Soz6AeqloU8wQMyaZnR+3vWLcXkQhV9fIBRws9CgKo5hqdS T+CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=qYGg+N98; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h188-20020a636cc5000000b00543d1e8c889si3606384pgc.846.2023.06.07.06.06.02; Wed, 07 Jun 2023 06:06:23 -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=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=qYGg+N98; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238604AbjFGM7A (ORCPT + 99 others); Wed, 7 Jun 2023 08:59:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234402AbjFGM67 (ORCPT ); Wed, 7 Jun 2023 08:58:59 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA0A7A6 for ; Wed, 7 Jun 2023 05:58:57 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-30e53cacc10so974236f8f.0 for ; Wed, 07 Jun 2023 05:58:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1686142736; x=1688734736; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=VJowdc9e59oB9wLZyR+086niExyuveZY6dunY32SgSw=; b=qYGg+N98bKzlnOSPs1w2WU3nsb+GHqRRgSHImrjk+cI7oLcG2JrvcdZENBR/LrxErj CJ03F5EweLHhwLn/VZG9dRqx8pnZmwYX1hsu4ZJvoNMwfYu4bLI6becMzNzZouVxOPsF DtM13TFnXRSQounOafPHJyFNSkuRogJ0byeSMwsOFxWckEdkJz6wZf97nGyE1QCgbIo1 gibaGN5I6O5VTp107lFVSW0BKXnSCwe3172yaicz42X+MT7xGq/mLjyIKZtO8cSZUlzh JIvtwJEHhjhIC4vmIofAz6h2MWGZsRSD1ZR+B6l5cSLt4DISIxxFLtuQuPdoBgkbL0ZP z+Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686142736; x=1688734736; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VJowdc9e59oB9wLZyR+086niExyuveZY6dunY32SgSw=; b=A/opAU6xeok0d6JQ5s036yrcWoLqwMnilWoBnx2FzFtFptmj7q/YLUZEcGFg43PiUt +IXPJyUy9d452+dfo9Ddho2M40QvZxbQ383JYu6xuILMwuXaVrTNS5gf7ZtqpL0AUuwL 8m1gDPjS8TOizdZ1qARbyGzIMYJD9o+QiHACVXImwEu91dpv9hm9SJ7t8QDytcy6ejdB ueHxZtDSjWDsDw82n4SZRlPE9QWgJgxnJfZo2j3PdY7syam7SVDtJjOjHK5o/qZRi7mq Fe3xpzVddEueanz500ZIQZmPP+XJfWQA/OZ9iMmvCe2AqqSW7hRH03S9Nz9D/+lMBIIC huFg== X-Gm-Message-State: AC+VfDxsP6FkaHxL5WBc8dM3RUYTH5amfepp3WBcAlRfoPnNTOEREwCQ we8QZ6kJsHgGmwfgGKf671aRvg== X-Received: by 2002:a5d:558d:0:b0:30a:e00e:8af7 with SMTP id i13-20020a5d558d000000b0030ae00e8af7mr4621946wrv.63.1686142736136; Wed, 07 Jun 2023 05:58:56 -0700 (PDT) Received: from localhost.localdomain (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id z10-20020a05600c220a00b003f42d8dd7d1sm2164096wml.7.2023.06.07.05.58.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jun 2023 05:58:55 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrew Jones , Alexandre Ghiti , Rob Herring , Anup Patel , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Song Shuai Subject: [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size Date: Wed, 7 Jun 2023 14:58:51 +0200 Message-Id: <20230607125851.63370-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,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 We used to only check the alignment of the physical address to decide which mapping would fit for a certain region of the linear mapping, but it is not enough since the virtual address must also be aligned, so check that too. Fixes: 3335068f8721 ("riscv: Use PUD/P4D/PGD pages for the linear mapping") Reported-by: Song Shuai Link: https://lore.kernel.org/linux-riscv/tencent_7C3B580B47C1B17C16488EC1@qq.com/ Signed-off-by: Alexandre Ghiti --- arch/riscv/mm/init.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index d42ea31c7de0..5143eef4c074 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -660,18 +660,19 @@ void __init create_pgd_mapping(pgd_t *pgdp, create_pgd_next_mapping(nextp, va, pa, sz, prot); } -static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) +static uintptr_t __init best_map_size(phys_addr_t pa, uintptr_t va, + phys_addr_t size) { - if (!(base & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE) + if (!(pa & (PGDIR_SIZE - 1)) && !(va & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE) return PGDIR_SIZE; - if (!(base & (P4D_SIZE - 1)) && size >= P4D_SIZE) + if (!(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE) return P4D_SIZE; - if (!(base & (PUD_SIZE - 1)) && size >= PUD_SIZE) + if (!(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE) return PUD_SIZE; - if (!(base & (PMD_SIZE - 1)) && size >= PMD_SIZE) + if (!(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE) return PMD_SIZE; return PAGE_SIZE; @@ -1177,7 +1178,7 @@ static void __init create_linear_mapping_range(phys_addr_t start, for (pa = start; pa < end; pa += map_size) { va = (uintptr_t)__va(pa); map_size = fixed_map_size ? fixed_map_size : - best_map_size(pa, end - pa); + best_map_size(pa, va, end - pa); create_pgd_mapping(swapper_pg_dir, va, pa, map_size, pgprot_from_va(va)); -- 2.39.2