Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4298406pxb; Mon, 8 Feb 2021 12:49:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJzmV1RscooJEt1duKIuUfhkV5KQfMYSXdz/l9kXqimvuS0jABhvD2KpGVdVSCO1nMUBFkm+ X-Received: by 2002:a05:6402:424a:: with SMTP id g10mr18902558edb.236.1612817341105; Mon, 08 Feb 2021 12:49:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612817341; cv=none; d=google.com; s=arc-20160816; b=tppgKbcGiqVYaaDOoD54WdD8F9jJiz//R0UHyzbHhs7rvxqKVsy56LkT605VlPfnie jeK3/2cwjs1ooTp/3LwXn/2BWyrBdc82bl2YA+Q/FNyZ+LfoTyJGhThb9N3dZXxJxaB0 JhjBEzbZUjJeVtPs8GHu7iy9e++bYQ9RajhgOyWYYFLexeuo0+Q6MuxJ53WOS9JGJ/r2 hfUY8+7cvMwHHPcAIeqnxciEzIKUiTttV/8WZkpY+EXREIrQD+OnJ0YS+8H19ymbnEzG 83P2Ow2MWYgXT4JngCPhz9FTEhdN8i7wH249o9nMG7IkZXSgaGRxW5MMKitDHqgN5v6F 4zLQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=0A+o//LrYgYgbZiXith201cPHPBQpFD9Rvam+3jXqNs=; b=rTY1Y6rORHDxEZ04QN4JyyDhx0RoiZyVoMSTsA0LVooLbMMBHmVzbA2vUDqBJK97QE kjx80dwMq/guouWDEULZEy0EYqYMWh3d7436RnU0m/aDHVoBgTWGvDvuzY9ftMcexxEV p1mz9418J1YItnAiPhKpAYhPq3dd2U8FHvV54vrabdhdbRTNSFVZUCMAXr70GKLCgPZ7 42dwMERdYSGQvK4+uQ82q5DUrrXcWtK6OgcQHYmHL0KpKzj5fcXgrFOHD5432JfHLhEN hVL0mVorO7aHxpwscLLrjrqwAOW/tUQtHbYjsAqNBWnIVvA+TIy4wxu9qYe3SqXXarH0 Mftg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f16si14676246edy.25.2021.02.08.12.48.37; Mon, 08 Feb 2021 12:49:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234555AbhBHUr5 (ORCPT + 99 others); Mon, 8 Feb 2021 15:47:57 -0500 Received: from relay12.mail.gandi.net ([217.70.178.232]:52327 "EHLO relay12.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236267AbhBHTfZ (ORCPT ); Mon, 8 Feb 2021 14:35:25 -0500 Received: from debian.home (lfbn-lyo-1-457-219.w2-7.abo.wanadoo.fr [2.7.49.219]) (Authenticated sender: alex@ghiti.fr) by relay12.mail.gandi.net (Postfix) with ESMTPSA id 863E5200008; Mon, 8 Feb 2021 19:34:35 +0000 (UTC) From: Alexandre Ghiti To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Paul Walmsley , Palmer Dabbelt , Albert Ou , kasan-dev@googlegroups.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH 4/4] riscv: Improve kasan population by using hugepages when possible Date: Mon, 8 Feb 2021 14:30:17 -0500 Message-Id: <20210208193017.30904-5-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210208193017.30904-1-alex@ghiti.fr> References: <20210208193017.30904-1-alex@ghiti.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The kasan functions that populates the shadow regions used to allocate them page by page and did not take advantage of hugepages, so fix this by trying to allocate hugepages of 1GB and fallback to 2MB hugepages or 4K pages in case it fails. This reduces the page table memory consumption and improves TLB usage, as shown below: Before this patch: ---[ Kasan shadow start ]--- 0xffffffc000000000-0xffffffc400000000 0x00000000818ef000 16G PTE . A . . . . R V 0xffffffc400000000-0xffffffc447fc0000 0x00000002b7f4f000 1179392K PTE D A . . . W R V 0xffffffc480000000-0xffffffc800000000 0x00000000818ef000 14G PTE . A . . . . R V ---[ Kasan shadow end ]--- After this patch: ---[ Kasan shadow start ]--- 0xffffffc000000000-0xffffffc400000000 0x00000000818ef000 16G PTE . A . . . . R V 0xffffffc400000000-0xffffffc440000000 0x0000000240000000 1G PGD D A . . . W R V 0xffffffc440000000-0xffffffc447e00000 0x00000002b7e00000 126M PMD D A . . . W R V 0xffffffc447e00000-0xffffffc447fc0000 0x00000002b818f000 1792K PTE D A . . . W R V 0xffffffc480000000-0xffffffc800000000 0x00000000818ef000 14G PTE . A . . . . R V ---[ Kasan shadow end ]--- Signed-off-by: Alexandre Ghiti --- arch/riscv/mm/kasan_init.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c index b7d4d9abd144..2b196f512f07 100644 --- a/arch/riscv/mm/kasan_init.c +++ b/arch/riscv/mm/kasan_init.c @@ -83,6 +83,15 @@ static void kasan_populate_pmd(pgd_t *pgd, unsigned long vaddr, unsigned long en do { next = pmd_addr_end(vaddr, end); + + if (pmd_none(*pmdp) && IS_ALIGNED(vaddr, PMD_SIZE) && (next - vaddr) >= PMD_SIZE) { + phys_addr = memblock_phys_alloc(PMD_SIZE, PMD_SIZE); + if (phys_addr) { + set_pmd(pmdp, pfn_pmd(PFN_DOWN(phys_addr), PAGE_KERNEL)); + continue; + } + } + kasan_populate_pte(pmdp, vaddr, next); } while (pmdp++, vaddr = next, vaddr != end); @@ -103,6 +112,21 @@ static void kasan_populate_pgd(unsigned long vaddr, unsigned long end) do { next = pgd_addr_end(vaddr, end); + + /* + * pgdp can't be none since kasan_early_init initialized all KASAN + * shadow region with kasan_early_shadow_pmd: if this is stillthe case, + * that means we can try to allocate a hugepage as a replacement. + */ + if (pgd_page_vaddr(*pgdp) == (unsigned long)lm_alias(kasan_early_shadow_pmd) && + IS_ALIGNED(vaddr, PGDIR_SIZE) && (next - vaddr) >= PGDIR_SIZE) { + phys_addr = memblock_phys_alloc(PGDIR_SIZE, PGDIR_SIZE); + if (phys_addr) { + set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_KERNEL)); + continue; + } + } + kasan_populate_pmd(pgdp, vaddr, next); } while (pgdp++, vaddr = next, vaddr != end); } -- 2.20.1