From patchwork Fri Jan 12 16:49:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10161369 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E024D602B3 for ; Fri, 12 Jan 2018 16:50:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D069828989 for ; Fri, 12 Jan 2018 16:50:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C4AF928A1A; Fri, 12 Jan 2018 16:50:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 30FF828A47 for ; Fri, 12 Jan 2018 16:50:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ZOi337to/k5c8PDCddpDQotqGhzQyLDGS8GjeJ5lMH4=; b=CC7PFMyfbJvjQOgrCh6pqSy6Rq lajuSM+m9b0jXMud5VDykpozImE2kK96B73aKvEKp0JRdBSvMJDCbEnMzagB/lj5iczsulX3BS6XZ bBk+uM9SetPyiunGPNzf3qpDn64cpSxzTOaZNyQLc1v3664gNpUdsrWEEI48TLrmEGIVs5PAjkAfF OEr4TZL10Z3WFzlTP5A1PgVAvEe6uWYrDjXmPsMbYHgAHDe+jWb9i4eBosKa+tq386m49Vs2B+74x rCAr62WBkFkF9vQfsLaNqP8MrQb0ihVly5GZ6XwKadEagiWxYXI8kqfLwWkwgq8gmsJubgH9H168P jkyYwTsA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1ea2X9-0004qO-O0; Fri, 12 Jan 2018 16:50:23 +0000 Received: from mail-wr0-x244.google.com ([2a00:1450:400c:c0c::244]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1ea2WX-0003kN-Ph for linux-arm-kernel@lists.infradead.org; Fri, 12 Jan 2018 16:49:48 +0000 Received: by mail-wr0-x244.google.com with SMTP id s13so5822736wra.10 for ; Fri, 12 Jan 2018 08:49:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=1KcxwSiYAuoKtF+cFHSjhE7h/b3G8yTmJCNCGDDDXSw=; b=N6Z1x59mHp9yGzlSuzbT9aHqvvDCsJFFig1wXL4kfcGSgnfrB/XI6lNUbNBJRRTrH0 0bIZZP9J78p24Nq2iohg1G4TOxek4DJqsCmRd8u3BIq6Zs5xQuk4DgDMHD1/Oql71uPK b7iRSLTi+/wV/B6dQmmmJl6BjGK3Mfdy/uoLwmUZ+qLnujADEl0LpgJfBifwZ6UcmkEl SiN3leYopkgrG5JqbmQunnv5Z9ZzRlBfXIdyLo5KIVVlweuoUdWb+9Kx0zjPAmzmq/Lg 8FbJG47PWGcJXyM2HAjPE1Jb5mNqCg7J28Yv9PwYr2asW3u1vt0t0tHIZ4JHlmKwPJXs GjFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=1KcxwSiYAuoKtF+cFHSjhE7h/b3G8yTmJCNCGDDDXSw=; b=BVX81+5GJQC7vmXtDqWw1l0qAyc/CXqVYWBJMhZhuEFGKue2w+t10Am5SHgruii1kt vDq/T0tcoW9TwB1Sv6XQma55kkI/2XTrCJgDftLfqbg7vKq9u4tJPkV390iMQTsBAIdW NPdgYogYIkKOkFVRJ133Ze7/66QDDZlpR2Ka5Kexn1rOYun0Rykm3TxnSNbbi6KOPiyX Hpn2StZM7Evzrnx//k8ui9ZSLS+lXyze2xx4uC4m5ANZEOWf8THS6oQEA8fZu0unQtsY I4dzPcbuIlPzTsFzF/+lecd3jKoFsh4MFN9+FHE56fwZopH7tcMiTBbDpFcIznfRCRTi tk/A== X-Gm-Message-State: AKwxytcszGqIXcX5XrxqiQL0Coif5DQWSOl4fQiWE5VashzC87wpRGUD qNa6t2Y6oKO79aLCaSMFMvKrJA== X-Google-Smtp-Source: ACJfBou8a1TjgBQPpne+65r0+I4dkHWu72gjhinXxqbF/WrsrdlpW7yciTwISSUd0ZO0AAQD3O4mkA== X-Received: by 10.223.176.39 with SMTP id f36mr7490996wra.113.1515775773705; Fri, 12 Jan 2018 08:49:33 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([100.105.28.69]) by smtp.gmail.com with ESMTPSA id 36sm11392516wrt.96.2018.01.12.08.49.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 08:49:32 -0800 (PST) Received: by andreyknvl0.muc.corp.google.com (Postfix, from userid 206546) id 0A77E18322A; Fri, 12 Jan 2018 17:49:32 +0100 (CET) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] kasan: clean up KASAN_SHADOW_SCALE_SHIFT usage Date: Fri, 12 Jan 2018 17:49:28 +0100 Message-Id: <34937ca3b90736eaad91b568edf5684091f662e3.1515775666.git.andreyknvl@google.com> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog In-Reply-To: References: In-Reply-To: References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180112_084945_841242_C6D285DC X-CRM114-Status: GOOD ( 16.75 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kostya Serebryany , Andrey Konovalov MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Right now the fact that KASAN uses a single shadow byte for 8 bytes of memory is scattered all over the code. This change defines KASAN_SHADOW_SCALE_SHIFT early in asm include files and makes use of this constant where necessary. Signed-off-by: Andrey Konovalov Acked-by: Andrey Ryabinin --- arch/arm64/include/asm/kasan.h | 17 ++++++++++------- arch/arm64/include/asm/memory.h | 3 ++- arch/arm64/mm/kasan_init.c | 3 ++- arch/x86/include/asm/kasan.h | 12 ++++++++---- include/linux/kasan.h | 2 -- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h index e266f80e45b7..b1591f4cbbdb 100644 --- a/arch/arm64/include/asm/kasan.h +++ b/arch/arm64/include/asm/kasan.h @@ -12,7 +12,8 @@ /* * KASAN_SHADOW_START: beginning of the kernel virtual addresses. - * KASAN_SHADOW_END: KASAN_SHADOW_START + 1/8 of kernel virtual addresses. + * KASAN_SHADOW_END: KASAN_SHADOW_START + 1/N of kernel virtual addresses, + * where N = (1 << KASAN_SHADOW_SCALE_SHIFT). */ #define KASAN_SHADOW_START (VA_START) #define KASAN_SHADOW_END (KASAN_SHADOW_START + KASAN_SHADOW_SIZE) @@ -20,14 +21,16 @@ /* * This value is used to map an address to the corresponding shadow * address by the following formula: - * shadow_addr = (address >> 3) + KASAN_SHADOW_OFFSET; + * shadow_addr = (address >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET * - * (1 << 61) shadow addresses - [KASAN_SHADOW_OFFSET,KASAN_SHADOW_END] - * cover all 64-bits of virtual addresses. So KASAN_SHADOW_OFFSET - * should satisfy the following equation: - * KASAN_SHADOW_OFFSET = KASAN_SHADOW_END - (1ULL << 61) + * (1 << (64 - KASAN_SHADOW_SCALE_SHIFT)) shadow addresses that lie in range + * [KASAN_SHADOW_OFFSET, KASAN_SHADOW_END) cover all 64-bits of virtual + * addresses. So KASAN_SHADOW_OFFSET should satisfy the following equation: + * KASAN_SHADOW_OFFSET = KASAN_SHADOW_END - + * (1ULL << (64 - KASAN_SHADOW_SCALE_SHIFT)) */ -#define KASAN_SHADOW_OFFSET (KASAN_SHADOW_END - (1ULL << (64 - 3))) +#define KASAN_SHADOW_OFFSET (KASAN_SHADOW_END - (1ULL << \ + (64 - KASAN_SHADOW_SCALE_SHIFT))) void kasan_init(void); void kasan_copy_shadow(pgd_t *pgdir); diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h index d4bae7d6e0d8..50fa96a49792 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -85,7 +85,8 @@ * stack size when KASAN is in use. */ #ifdef CONFIG_KASAN -#define KASAN_SHADOW_SIZE (UL(1) << (VA_BITS - 3)) +#define KASAN_SHADOW_SCALE_SHIFT 3 +#define KASAN_SHADOW_SIZE (UL(1) << (VA_BITS - KASAN_SHADOW_SCALE_SHIFT)) #define KASAN_THREAD_SHIFT 1 #else #define KASAN_SHADOW_SIZE (0) diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index acba49fb5aac..6e02e6fb4c7b 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -135,7 +135,8 @@ static void __init kasan_pgd_populate(unsigned long addr, unsigned long end, /* The early shadow maps everything to a single page of zeroes */ asmlinkage void __init kasan_early_init(void) { - BUILD_BUG_ON(KASAN_SHADOW_OFFSET != KASAN_SHADOW_END - (1UL << 61)); + BUILD_BUG_ON(KASAN_SHADOW_OFFSET != + KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE)); BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE)); kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h index b577dd0916aa..13e70da38bed 100644 --- a/arch/x86/include/asm/kasan.h +++ b/arch/x86/include/asm/kasan.h @@ -4,6 +4,7 @@ #include #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) +#define KASAN_SHADOW_SCALE_SHIFT 3 /* * Compiler uses shadow offset assuming that addresses start @@ -12,12 +13,15 @@ * 'kernel address space start' >> KASAN_SHADOW_SCALE_SHIFT */ #define KASAN_SHADOW_START (KASAN_SHADOW_OFFSET + \ - ((-1UL << __VIRTUAL_MASK_SHIFT) >> 3)) + ((-1UL << __VIRTUAL_MASK_SHIFT) >> \ + KASAN_SHADOW_SCALE_SHIFT)) /* - * 47 bits for kernel address -> (47 - 3) bits for shadow - * 56 bits for kernel address -> (56 - 3) bits for shadow + * 47 bits for kernel address -> (47 - KASAN_SHADOW_SCALE_SHIFT) bits for shadow + * 56 bits for kernel address -> (56 - KASAN_SHADOW_SCALE_SHIFT) bits for shadow */ -#define KASAN_SHADOW_END (KASAN_SHADOW_START + (1ULL << (__VIRTUAL_MASK_SHIFT - 3))) +#define KASAN_SHADOW_END (KASAN_SHADOW_START + \ + (1ULL << (__VIRTUAL_MASK_SHIFT - \ + KASAN_SHADOW_SCALE_SHIFT))) #ifndef __ASSEMBLY__ diff --git a/include/linux/kasan.h b/include/linux/kasan.h index e3eb834c9a35..e9eaa964473a 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -11,8 +11,6 @@ struct task_struct; #ifdef CONFIG_KASAN -#define KASAN_SHADOW_SCALE_SHIFT 3 - #include #include