From patchwork Thu Nov 10 20:35:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 18318 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp365501wru; Thu, 10 Nov 2022 12:41:39 -0800 (PST) X-Google-Smtp-Source: AMsMyM4sRlPhBFJyeEjNyRSNphSHOrCb9z1FufCg+d4c4lozrwk7StoqjWuscNrDv9w0Ca5HOqPD X-Received: by 2002:aa7:d797:0:b0:462:1e07:1dd7 with SMTP id s23-20020aa7d797000000b004621e071dd7mr3457550edq.293.1668112899294; Thu, 10 Nov 2022 12:41:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668112899; cv=none; d=google.com; s=arc-20160816; b=l8xe9QELGzD2rQDhvL4WCDM9OtfayKfdi5XDXqhY2c2Jils+S3HhjRd+9focgGcFZR a9IVHJghx7E+hQDMUdDLKrkYJiVwpq9MNO+hYhhlSCwennYcXOWgEPbv7XmaXcb3vPaG CTCqPL2zqvDfq14Apk4HWZoZrlwQEf7cjMlQmGCdwGH+oIb8siMXmLprclTBNCOHixWF eyQZEVliGanZO8t6il0u0AKlo0sXc5wtJOh2TlWtGuAzpp9h+cHOewHJdNMesDRmlGJv 0bCA4exNbZsPfTIiuhRUHIpqNzzXUVf9zNE1m8FdbyW1X7cbRxTUPULpuyFklCytOHP3 1GrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=NyjCQLTGMZRdycUacwFyAxLckz0Mzvzm9v3NS3Lsp2Q=; b=iHEk8GcJXJ8VIbsd2UN0gW+iQ2w9VkPolDF8xToIVmjq29kstESwxtRR0+FAtInlhy jXjs5Chv9kv+8hhhJkT/Anfitx5MMwwermB0X7AwDlgEhldNMkT301ul4m8x1YDiItPJ 1n4uerRuWKl05dqSP0ZwXbJ7kZ+Na96d+YCKtGRAYqZJJ1VXXOR/cYr3czpTRD/IwSaH fduCLnJladsmTSeuXUkk1y6Fqo8GfJKqzc1qlcYyTGoOThSIZi0mUXIQuY8yJWRj6Baa meueEbuHy/FT6QPx3zJqZwsWxHY4mFxb6+ZiEBKealjHkzHYvGJaYO+GJNciQNuz1+JN 6WbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ica0cDCU; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hr22-20020a1709073f9600b007ae743c61ccsi213756ejc.848.2022.11.10.12.41.15; Thu, 10 Nov 2022 12:41:39 -0800 (PST) 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=@google.com header.s=20210112 header.b=ica0cDCU; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231995AbiKJUf2 (ORCPT + 99 others); Thu, 10 Nov 2022 15:35:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231924AbiKJUfO (ORCPT ); Thu, 10 Nov 2022 15:35:14 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE9926397 for ; Thu, 10 Nov 2022 12:35:13 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id b2-20020a170902d50200b001871a3c51afso2060910plg.8 for ; Thu, 10 Nov 2022 12:35:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=NyjCQLTGMZRdycUacwFyAxLckz0Mzvzm9v3NS3Lsp2Q=; b=ica0cDCUID9I3SR6IOAWlSBXbXnnWiD8g//aKt7PzSybddsy+Uza8seX3JWpahv96N ii+SEb49m9HRiW0UzQwvzGN0CGk36fKeab0Fjd03cE1ylLgV713LcAbVoqO+sdoDUju7 IY44yRTBvl+jC+38xrbv5eGrlp2tqaKQxIsGsxD40M8Lr5zcIhNc5RI8j32X1tx3glTf NEZE09UOJjAVgMoQyII9XQXRLjc7N93J4Rw8/kv5Zv0lCt8K3Bipge9Fze2z4oOerTMd Dtm3F+4nv4LczFbQ44PV+4bMlwbrXTorjZIqsXOxRKe7U1SXL9bd5vCLy20dvKrmcJfO n2tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NyjCQLTGMZRdycUacwFyAxLckz0Mzvzm9v3NS3Lsp2Q=; b=w4LZr9KCM/oJ/Es5klQwaTKe+kbt+I2OolJyYrGuLdql9G6kGbgFlVxI/Vznx/GlE7 ucJZzMtCD7IxOL8HRC0IE/D1D2BUDgnFi9JYMjP4w+s3va4IVHm3Z9rcBA919DxKa/fn Iqa9dbWzpB38Mul/ttCdtl2J+huDRp8PGct0lDMXJFEaYV1CLkmhcAWXXo1Rrfr0a+dQ 13NHTSvynhlBDdQbZjtTXThRriGCgCc4rcGCNsSgxkJXEvDjii9Kcaf4IzIFNMfqWmLJ Vn/dLh2BrqDQbBbHu2pGR5Q4vqAK9Ly8ma/V/m6KSh2QwsrzDCj+S+mVfrc3tIxYGQn+ SXGA== X-Gm-Message-State: ACrzQf28T6wQvbR663VgNREM49FIuT1DQwGSwu+UNdcCzTtsZ0bDq9Mc M1z/lI842FzXaOgEVRg5drkXDRWP8bQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:2bcb:b0:200:462f:6419 with SMTP id n11-20020a17090a2bcb00b00200462f6419mr1968927pje.135.1668112513500; Thu, 10 Nov 2022 12:35:13 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 10 Nov 2022 20:35:03 +0000 In-Reply-To: <20221110203504.1985010-1-seanjc@google.com> Mime-Version: 1.0 References: <20221110203504.1985010-1-seanjc@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221110203504.1985010-5-seanjc@google.com> Subject: [PATCH v2 4/5] x86/kasan: Add helpers to align shadow addresses up and down From: Sean Christopherson To: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, Andrey Ryabinin Cc: "H. Peter Anvin" , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, Sean Christopherson , syzbot+ffb4f000dc2872c93f62@syzkaller.appspotmail.com, syzbot+8cdd16fd5a6c0565e227@syzkaller.appspotmail.com X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749143151432589867?= X-GMAIL-MSGID: =?utf-8?q?1749143151432589867?= Add helpers to dedup code for aligning shadow address up/down to page boundaries when translating an address to its shadow. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Andrey Ryabinin --- arch/x86/mm/kasan_init_64.c | 40 ++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c index ad7872ae10ed..afc5e129ca7b 100644 --- a/arch/x86/mm/kasan_init_64.c +++ b/arch/x86/mm/kasan_init_64.c @@ -316,22 +316,33 @@ void __init kasan_early_init(void) kasan_map_early_shadow(init_top_pgt); } +static unsigned long kasan_mem_to_shadow_align_down(unsigned long va) +{ + unsigned long shadow = (unsigned long)kasan_mem_to_shadow((void *)va); + + return round_down(shadow, PAGE_SIZE); +} + +static unsigned long kasan_mem_to_shadow_align_up(unsigned long va) +{ + unsigned long shadow = (unsigned long)kasan_mem_to_shadow((void *)va); + + return round_up(shadow, PAGE_SIZE); +} + void __init kasan_populate_shadow_for_vaddr(void *va, size_t size, int nid) { unsigned long shadow_start, shadow_end; - shadow_start = (unsigned long)kasan_mem_to_shadow(va); - shadow_start = round_down(shadow_start, PAGE_SIZE); - shadow_end = (unsigned long)kasan_mem_to_shadow(va + size); - shadow_end = round_up(shadow_end, PAGE_SIZE); - + shadow_start = kasan_mem_to_shadow_align_down((unsigned long)va); + shadow_end = kasan_mem_to_shadow_align_up((unsigned long)va + size); kasan_populate_shadow(shadow_start, shadow_end, nid); } void __init kasan_init(void) { + unsigned long shadow_cea_begin, shadow_cea_end; int i; - void *shadow_cea_begin, *shadow_cea_end; memcpy(early_top_pgt, init_top_pgt, sizeof(early_top_pgt)); @@ -372,16 +383,9 @@ void __init kasan_init(void) map_range(&pfn_mapped[i]); } - shadow_cea_begin = (void *)CPU_ENTRY_AREA_BASE; - shadow_cea_begin = kasan_mem_to_shadow(shadow_cea_begin); - shadow_cea_begin = (void *)round_down( - (unsigned long)shadow_cea_begin, PAGE_SIZE); - - shadow_cea_end = (void *)(CPU_ENTRY_AREA_BASE + - CPU_ENTRY_AREA_MAP_SIZE); - shadow_cea_end = kasan_mem_to_shadow(shadow_cea_end); - shadow_cea_end = (void *)round_up( - (unsigned long)shadow_cea_end, PAGE_SIZE); + shadow_cea_begin = kasan_mem_to_shadow_align_down(CPU_ENTRY_AREA_BASE); + shadow_cea_end = kasan_mem_to_shadow_align_up(CPU_ENTRY_AREA_BASE + + CPU_ENTRY_AREA_MAP_SIZE); kasan_populate_early_shadow( kasan_mem_to_shadow((void *)PAGE_OFFSET + MAXMEM), @@ -403,9 +407,9 @@ void __init kasan_init(void) kasan_populate_early_shadow( kasan_mem_to_shadow((void *)VMALLOC_END + 1), - shadow_cea_begin); + (void *)shadow_cea_begin); - kasan_populate_early_shadow(shadow_cea_end, + kasan_populate_early_shadow((void *)shadow_cea_end, kasan_mem_to_shadow((void *)__START_KERNEL_map)); kasan_populate_shadow((unsigned long)kasan_mem_to_shadow(_stext),