From patchwork Mon Dec 26 20:38:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Fainelli X-Patchwork-Id: 708828 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tnWBM1Z69z9ssP for ; Tue, 27 Dec 2016 07:41:47 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="WGd3vGsw"; dkim-atps=neutral Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1cLc44-0000w0-J5; Mon, 26 Dec 2016 20:40:12 +0000 Received: from mail-oi0-x244.google.com ([2607:f8b0:4003:c06::244]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1cLc39-0000EQ-6S; Mon, 26 Dec 2016 20:39:17 +0000 Received: by mail-oi0-x244.google.com with SMTP id v84so45672784oie.2; Mon, 26 Dec 2016 12:38:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=fjLffKmh6pRdLT+uaRjvjRwmvXCkPFQJ+/fCOiUiwb4=; b=WGd3vGswc6XLtUqIAnpGu5Y2RQresDwa/j1YTd7+jRCjKNqgXvIQxWHz29FDh+tZCK zSh73ufHsEts1VGuYhaeHOZqG324UAXtamR10Ee0ASHW1va+13WC/zwdXG+M/2+ka3fZ psGwGaOjjEemBFF6FQsdkxCGH/NUo9tEp2obm5ZD/RqEYhFD+v0b0oViU0VUEPnhlPxr NEuuaFU3s25Ms06zYTid1ZPpt+9NDcfmgjJ3xE0MLpx58ViGhd6YAenr61bY/N2Tlekk 5tjRqaF6SC1ucU6nUGCwFa3TmN92WaaUmFs7JYb/BzT3lbBwJXSLVVfcRSz4ievBjnoE Gcyw== 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; bh=fjLffKmh6pRdLT+uaRjvjRwmvXCkPFQJ+/fCOiUiwb4=; b=KzvqohjaaI5QGdJL2Kq9MyOOQpT6MWfN1SHi2z4vHN3KfASFbiddvjc8JXHFlXhW3V 97e2f9kI96n2WjUz5pWsSMLWgKbdTYli57YRjca/BWpoqVYZad347Z3QwUrCjnei9/JZ +/lqBhJsAE8NC6h5s4qR3G8wYpz9uYCLqRn0X+XAtUO4wdUPKF8Jycqyx0JZhGYaV2v2 kHbErLhk4oABQZWNXt2J5EIDI7FLnL2XNnkENPp0aA7FI2LDjD8zxLLTN+OyBzVGxk0p G5oecfZPTaTEwBzDMpO1hYjwp+ohI4qcZCZXGd+iXEH6FKIehH9Xb9nqidjbYBv8Oc2x nNWQ== X-Gm-Message-State: AIkVDXJ329T9SQc4JBYPWnLFfP0bAT0eNCaJ37W+C4FeTX07lk7LCDS3kYYvlndSgxSv2A== X-Received: by 10.202.3.139 with SMTP id 133mr14310957oid.31.1482784733974; Mon, 26 Dec 2016 12:38:53 -0800 (PST) Received: from bender.lan ([2001:470:d:73f:5586:4b80:7aae:266e]) by smtp.gmail.com with ESMTPSA id 36sm15361158otg.24.2016.12.26.12.38.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Dec 2016 12:38:53 -0800 (PST) From: Florian Fainelli To: linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 3/4] ARM: Add support for CONFIG_DEBUG_VIRTUAL Date: Mon, 26 Dec 2016 12:38:41 -0800 Message-Id: <20161226203842.19457-4-f.fainelli@gmail.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161226203842.19457-1-f.fainelli@gmail.com> References: <20161209233628.6642-1-f.fainelli@gmail.com> <20161226203842.19457-1-f.fainelli@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20161226_123915_494314_1109A5E0 X-CRM114-Status: GOOD ( 16.48 ) X-Spam-Score: -2.0 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2607:f8b0:4003:c06:0:0:0:244 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (f.fainelli[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: nicolas.pitre@linaro.org, mark.rutland@arm.com, matt@codeblueprint.co.uk, stefan@agner.ch, chris.brandt@renesas.com, linux-mtd@lists.infradead.org, cyrille.pitchen@atmel.com, panand@redhat.com, boris.brezillon@free-electrons.com, Florian Fainelli , pawel.moll@arm.com, richard@nod.at, linux@armlinux.org.uk, ben@decadent.org.uk, vladimir.murzin@arm.com, keescook@chromium.org, arnd@arndb.de, labbott@fedoraproject.org, jonathan.austin@arm.com, ard.biesheuvel@linaro.org, linux-kernel@vger.kernel.org, computersforpeace@gmail.com, dwmw2@infradead.org, kirill.shutemov@linux.intel.com, js07.lee@samsung.com MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org x86 has an option: CONFIG_DEBUG_VIRTUAL to do additional checks on virt_to_phys calls. The goal is to catch users who are calling virt_to_phys on non-linear addresses immediately. This includes caller using __virt_to_phys() on image addresses instead of __pa_symbol(). This is a generally useful debug feature to spot bad code (particulary in drivers). Signed-off-by: Florian Fainelli --- arch/arm/Kconfig | 1 + arch/arm/include/asm/memory.h | 16 +++++++++++-- arch/arm/mm/Makefile | 1 + arch/arm/mm/physaddr.c | 55 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 arch/arm/mm/physaddr.c diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index b5d529fdffab..5e66173c5787 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -2,6 +2,7 @@ config ARM bool default y select ARCH_CLOCKSOURCE_DATA + select ARCH_HAS_DEBUG_VIRTUAL select ARCH_HAS_DEVMEM_IS_ALLOWED select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST diff --git a/arch/arm/include/asm/memory.h b/arch/arm/include/asm/memory.h index bee7511c5098..d90300193adf 100644 --- a/arch/arm/include/asm/memory.h +++ b/arch/arm/include/asm/memory.h @@ -213,7 +213,7 @@ extern const void *__pv_table_begin, *__pv_table_end; : "r" (x), "I" (__PV_BITS_31_24) \ : "cc") -static inline phys_addr_t __virt_to_phys(unsigned long x) +static inline phys_addr_t __virt_to_phys_nodebug(unsigned long x) { phys_addr_t t; @@ -245,7 +245,7 @@ static inline unsigned long __phys_to_virt(phys_addr_t x) #define PHYS_OFFSET PLAT_PHYS_OFFSET #define PHYS_PFN_OFFSET ((unsigned long)(PHYS_OFFSET >> PAGE_SHIFT)) -static inline phys_addr_t __virt_to_phys(unsigned long x) +static inline phys_addr_t __virt_to_phys_nodebug(unsigned long x) { return (phys_addr_t)x - PAGE_OFFSET + PHYS_OFFSET; } @@ -261,6 +261,16 @@ static inline unsigned long __phys_to_virt(phys_addr_t x) ((((unsigned long)(kaddr) - PAGE_OFFSET) >> PAGE_SHIFT) + \ PHYS_PFN_OFFSET) +#define __pa_symbol_nodebug(x) __virt_to_phys_nodebug((x)) + +#ifdef CONFIG_DEBUG_VIRTUAL +extern phys_addr_t __virt_to_phys(unsigned long x); +extern phys_addr_t __phys_addr_symbol(unsigned long x); +#else +#define __virt_to_phys(x) __virt_to_phys_nodebug(x) +#define __phys_addr_symbol(x) __pa_symbol_nodebug(x) +#endif + /* * These are *only* valid on the kernel direct mapped RAM memory. * Note: Drivers should NOT use these. They are the wrong @@ -283,9 +293,11 @@ static inline void *phys_to_virt(phys_addr_t x) * Drivers should NOT use these either. */ #define __pa(x) __virt_to_phys((unsigned long)(x)) +#define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0)) #define __va(x) ((void *)__phys_to_virt((phys_addr_t)(x))) #define pfn_to_kaddr(pfn) __va((phys_addr_t)(pfn) << PAGE_SHIFT) + extern long long arch_phys_to_idmap_offset; /* diff --git a/arch/arm/mm/Makefile b/arch/arm/mm/Makefile index e8698241ece9..b3dea80715b4 100644 --- a/arch/arm/mm/Makefile +++ b/arch/arm/mm/Makefile @@ -14,6 +14,7 @@ endif obj-$(CONFIG_ARM_PTDUMP) += dump.o obj-$(CONFIG_MODULES) += proc-syms.o +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_ALIGNMENT_TRAP) += alignment.o obj-$(CONFIG_HIGHMEM) += highmem.o diff --git a/arch/arm/mm/physaddr.c b/arch/arm/mm/physaddr.c new file mode 100644 index 000000000000..f10bdcbcb155 --- /dev/null +++ b/arch/arm/mm/physaddr.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "mm.h" + +static inline bool __virt_addr_valid(unsigned long x) +{ + /* high_memory does not get immediately defined, and there + * are early callers of __pa() against PAGE_OFFSET + */ + if (!high_memory && x >= PAGE_OFFSET) + return true; + + if (high_memory && x >= PAGE_OFFSET && x < (unsigned long)high_memory) + return true; + + /* ARM uses the default per-CPU allocation routing which forces us to + * have an explicit check here to avoid a false positive + */ + if (x == MAX_DMA_ADDRESS) + return true; + + return false; +} + +phys_addr_t __virt_to_phys(unsigned long x) +{ + WARN(!__virt_addr_valid(x), + "virt_to_phys used for non-linear address: %pK (%pS)\n", + (void *)x, + (void *)x); + + return __virt_to_phys_nodebug(x); +} +EXPORT_SYMBOL(__virt_to_phys); + +phys_addr_t __phys_addr_symbol(unsigned long x) +{ + /* This is bounds checking against the kernel image only. + * __pa_symbol should only be used on kernel symbol addresses. + */ + VIRTUAL_BUG_ON(x < (unsigned long)KERNEL_START || + x > (unsigned long)KERNEL_END); + + return __pa_symbol_nodebug(x); +} +EXPORT_SYMBOL(__phys_addr_symbol);