From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2333C433DF for ; Mon, 19 Oct 2020 09:39:40 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1F8CF22243 for ; Mon, 19 Oct 2020 09:39:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Kp6Oc1SF"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="SWYzc7Vg" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1F8CF22243 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.ibm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=J9dVbUgHxDVFU/TDDYUBYRGX5rriwCawZwQJVlKbEO0=; b=Kp6Oc1SF4MnYdyA72bs5ufWAF RaMi/g103h/stkaU4epAhTVsHF2dFWqXBwEm87joQxPX3YCi91Sq21lmmURDFsw5YGD1KrIy/sCMl caDAGgii7pGuJS/l4om9xU9NRgv8ozDLbr6L41PAYWrnjGNBVtZkBpmOXZ1xDK6GuKHL7lPBxNGKR cQtRrBl27Mn/IbEGWLITzYiO4f9oe1yYlC2maZbr6vXPHfmrN3dd+OcNfTP5JJ8p+El0wM9eZaBn4 beacht7KsdGiqlyhHwqaNGh/ZyYqlROrTi1IUMFjicv2eEehte9RjghkvLDliNTPvnBsp7Q24RVI4 UWlWm7s5A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kURcA-00063x-LE; Mon, 19 Oct 2020 09:38:02 +0000 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kURc5-00062u-Vs for linux-arm-kernel@lists.infradead.org; Mon, 19 Oct 2020 09:38:00 +0000 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 09J9bXZO180636; Mon, 19 Oct 2020 05:37:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=pp1; bh=DGqDx0QDLVB5nhiA0UEyllENc9qqMSGZv+rHdnkHovM=; b=SWYzc7VgTvpmm6T0c4N4y75d7ufLl2qfB4SGRwWmsS0Ml6CQ7CDaL6osVLj2l65rtKkd 2nPT1qjg8LzNu5iHv0LdDc3mBowXDM/S3CiZM33SP/IaFQjeTAykIZ3hmWA7FpMXxGdw fP/Cw/BHBNYabXsmVNDTYxivLM2kr7V63VlW3VNKiKwkjkAkaJZazOvlnxEnbN3AiOod EZxLw8vzNAYS7gaKGLg3KTsNOMjfGEv41ivaCyhssQX6dV4sZ1X3baPmEQBQiIH8ISR7 mK6ctdpEs8UK4IEqj1v3yOLdUPFk+7bVKKJXM5KwQcrV82rbtEyLOpe04+xTJG99Uk/I yA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3498010kny-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Oct 2020 05:37:39 -0400 Received: from m0098396.ppops.net (m0098396.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 09J9bXav180693; Mon, 19 Oct 2020 05:37:39 -0400 Received: from ppma02fra.de.ibm.com (47.49.7a9f.ip4.static.sl-reverse.com [159.122.73.71]) by mx0a-001b2d01.pphosted.com with ESMTP id 3498010kgh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Oct 2020 05:37:39 -0400 Received: from pps.filterd (ppma02fra.de.ibm.com [127.0.0.1]) by ppma02fra.de.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 09J9TbOf006391; Mon, 19 Oct 2020 09:34:28 GMT Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by ppma02fra.de.ibm.com with ESMTP id 347r880yvm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Oct 2020 09:34:27 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 09J9YPOI25428418 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Oct 2020 09:34:25 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 836EF11C052; Mon, 19 Oct 2020 09:34:25 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1210811C04A; Mon, 19 Oct 2020 09:34:24 +0000 (GMT) Received: from linux.ibm.com (unknown [9.145.52.221]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Mon, 19 Oct 2020 09:34:23 +0000 (GMT) Date: Mon, 19 Oct 2020 12:34:21 +0300 From: Mike Rapoport To: Linus Walleij Subject: Re: [PATCH 4/5 v16] ARM: Initialize the mapping of KASan shadow memory Message-ID: <20201019093421.GA455883@linux.ibm.com> References: <20201019084140.4532-1-linus.walleij@linaro.org> <20201019084140.4532-5-linus.walleij@linaro.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20201019084140.4532-5-linus.walleij@linaro.org> X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-10-19_02:2020-10-16, 2020-10-19 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=1 impostorscore=0 bulkscore=0 clxscore=1011 adultscore=0 mlxscore=0 phishscore=0 spamscore=0 mlxlogscore=999 priorityscore=1501 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010190072 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201019_053758_399130_5C874469 X-CRM114-Status: GOOD ( 42.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Florian Fainelli , Ahmad Fatoum , Arnd Bergmann , Abbott Liu , Russell King , kasan-dev@googlegroups.com, Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , Ard Biesheuvel , linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Mon, Oct 19, 2020 at 10:41:39AM +0200, Linus Walleij wrote: > This patch initializes KASan shadow region's page table and memory. > There are two stage for KASan initializing: > > 1. At early boot stage the whole shadow region is mapped to just > one physical page (kasan_zero_page). It is finished by the function > kasan_early_init which is called by __mmap_switched(arch/arm/kernel/ > head-common.S) > > 2. After the calling of paging_init, we use kasan_zero_page as zero > shadow for some memory that KASan does not need to track, and we > allocate a new shadow space for the other memory that KASan need to > track. These issues are finished by the function kasan_init which is > call by setup_arch. > > When using KASan we also need to increase the THREAD_SIZE_ORDER > from 1 to 2 as the extra calls for shadow memory uses quite a bit > of stack. > > As we need to make a temporary copy of the PGD when setting up > shadow memory we create a helpful PGD_SIZE definition for both > LPAE and non-LPAE setups. > > The KASan core code unconditionally calls pud_populate() so this > needs to be changed from BUG() to do {} while (0) when building > with KASan enabled. > > After the initial development by Andre Ryabinin several modifications > have been made to this code: > > Abbott Liu > - Add support ARM LPAE: If LPAE is enabled, KASan shadow region's > mapping table need be copied in the pgd_alloc() function. > - Change kasan_pte_populate,kasan_pmd_populate,kasan_pud_populate, > kasan_pgd_populate from .meminit.text section to .init.text section. > Reported by Florian Fainelli > > Linus Walleij : > - Drop the custom mainpulation of TTBR0 and just use > cpu_switch_mm() to switch the pgd table. > - Adopt to handle 4th level page tabel folding. > - Rewrite the entire page directory and page entry initialization > sequence to be recursive based on ARM64:s kasan_init.c. > > Ard Biesheuvel : > - Necessary underlying fixes. > - Crucial bug fixes to the memory set-up code. > > Cc: Alexander Potapenko > Cc: Dmitry Vyukov > Cc: kasan-dev@googlegroups.com > Cc: Mike Rapoport > Co-developed-by: Andrey Ryabinin > Co-developed-by: Abbott Liu > Co-developed-by: Ard Biesheuvel > Acked-by: Mike Rapoport > Reviewed-by: Ard Biesheuvel > Tested-by: Ard Biesheuvel # QEMU/KVM/mach-virt/LPAE/8G > Tested-by: Florian Fainelli # Brahma SoCs > Tested-by: Ahmad Fatoum # i.MX6Q > Reported-by: Russell King - ARM Linux > Reported-by: Florian Fainelli > Signed-off-by: Andrey Ryabinin > Signed-off-by: Abbott Liu > Signed-off-by: Florian Fainelli > Signed-off-by: Ard Biesheuvel > Signed-off-by: Linus Walleij > --- ... > + cpu_switch_mm(tmp_pgd_table, &init_mm); > + local_flush_tlb_all(); > + > + clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END); > + > + kasan_populate_early_shadow(kasan_mem_to_shadow((void *)VMALLOC_START), > + kasan_mem_to_shadow((void *)-1UL) + 1); > + > + for_each_memblock(memory, reg) { > + void *start = __va(reg->base); > + void *end = __va(reg->base + reg->size); > + I've killed for_each_memblock() recently and we have now for_each_mem_range(idx, &pa_start, &pa_end) instead. > + /* Do not attempt to shadow highmem */ > + if (reg->base >= arm_lowmem_limit) { > + pr_info("Skip highmem block %pap-%pap\n", > + ®->base, ®->base + reg->size); > + continue; > + } > + if (reg->base + reg->size > arm_lowmem_limit) { > + pr_info("Truncating shadow for %pap-%pap to lowmem region\n", > + ®->base, ®->base + reg->size); > + end = __va(arm_lowmem_limit); > + } > + if (start >= end) { > + pr_info("Skipping invalid memory block %px-%px\n", > + start, end); > + continue; > + } > + > + create_mapping(start, end); > + } > + > + /* > + * 1. The module global variables are in MODULES_VADDR ~ MODULES_END, > + * so we need to map this area. > + * 2. PKMAP_BASE ~ PKMAP_BASE+PMD_SIZE's shadow and MODULES_VADDR > + * ~ MODULES_END's shadow is in the same PMD_SIZE, so we can't > + * use kasan_populate_zero_shadow. > + */ > + create_mapping((void *)MODULES_VADDR, (void *)(PKMAP_BASE + PMD_SIZE)); > + > + /* > + * KAsan may reuse the contents of kasan_early_shadow_pte directly, so > + * we should make sure that it maps the zero page read-only. > + */ > + for (i = 0; i < PTRS_PER_PTE; i++) > + set_pte_at(&init_mm, KASAN_SHADOW_START + i*PAGE_SIZE, > + &kasan_early_shadow_pte[i], > + pfn_pte(virt_to_pfn(kasan_early_shadow_page), > + __pgprot(pgprot_val(PAGE_KERNEL) > + | L_PTE_RDONLY))); > + > + cpu_switch_mm(swapper_pg_dir, &init_mm); > + local_flush_tlb_all(); > + > + memset(kasan_early_shadow_page, 0, PAGE_SIZE); > + pr_info("Kernel address sanitizer initialized\n"); > + init_task.kasan_depth = 0; > +} > diff --git a/arch/arm/mm/pgd.c b/arch/arm/mm/pgd.c > index c5e1b27046a8..f8e9bc58a84f 100644 > --- a/arch/arm/mm/pgd.c > +++ b/arch/arm/mm/pgd.c > @@ -66,7 +66,21 @@ pgd_t *pgd_alloc(struct mm_struct *mm) > new_pmd = pmd_alloc(mm, new_pud, 0); > if (!new_pmd) > goto no_pmd; > -#endif > +#ifdef CONFIG_KASAN > + /* > + * Copy PMD table for KASAN shadow mappings. > + */ > + init_pgd = pgd_offset_k(TASK_SIZE); > + init_p4d = p4d_offset(init_pgd, TASK_SIZE); > + init_pud = pud_offset(init_p4d, TASK_SIZE); > + init_pmd = pmd_offset(init_pud, TASK_SIZE); > + new_pmd = pmd_offset(new_pud, TASK_SIZE); > + memcpy(new_pmd, init_pmd, > + (pmd_index(MODULES_VADDR) - pmd_index(TASK_SIZE)) > + * sizeof(pmd_t)); > + clean_dcache_area(new_pmd, PTRS_PER_PMD * sizeof(pmd_t)); > +#endif /* CONFIG_KASAN */ > +#endif /* CONFIG_LPAE */ > > if (!vectors_high()) { > /* > -- > 2.26.2 > -- Sincerely yours, Mike. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel