From nobody Mon Feb 9 00:07:16 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDF14C7EE23 for ; Wed, 7 Jun 2023 12:59:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239237AbjFGM7A (ORCPT ); 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-Google-Smtp-Source: ACHHUZ4TWTfgOEoFeZVMqWAp8ZN/koVFrf7eHsf6VrJB/pLXD1S4I70XGWbwV2qVsNa3FRrfijmKDQ== 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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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); } =20 -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 >=3D PGDIR_SIZE) + if (!(pa & (PGDIR_SIZE - 1)) && !(va & (PGDIR_SIZE - 1)) && size >=3D PGD= IR_SIZE) return PGDIR_SIZE; =20 - if (!(base & (P4D_SIZE - 1)) && size >=3D P4D_SIZE) + if (!(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >=3D P4D_SIZ= E) return P4D_SIZE; =20 - if (!(base & (PUD_SIZE - 1)) && size >=3D PUD_SIZE) + if (!(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >=3D PUD_SIZ= E) return PUD_SIZE; =20 - if (!(base & (PMD_SIZE - 1)) && size >=3D PMD_SIZE) + if (!(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >=3D PMD_SIZ= E) return PMD_SIZE; =20 return PAGE_SIZE; @@ -1177,7 +1178,7 @@ static void __init create_linear_mapping_range(phys_a= ddr_t start, for (pa =3D start; pa < end; pa +=3D map_size) { va =3D (uintptr_t)__va(pa); map_size =3D fixed_map_size ? fixed_map_size : - best_map_size(pa, end - pa); + best_map_size(pa, va, end - pa); =20 create_pgd_mapping(swapper_pg_dir, va, pa, map_size, pgprot_from_va(va)); --=20 2.39.2