From patchwork Wed Jan 4 01:14:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Fainelli X-Patchwork-Id: 9495983 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 0CC76606B4 for ; Wed, 4 Jan 2017 01:16:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F33692711E for ; Wed, 4 Jan 2017 01:16:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E7D6D27829; Wed, 4 Jan 2017 01:16:45 +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.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9019D2711E for ; Wed, 4 Jan 2017 01:16:45 +0000 (UTC) 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 1cOaAm-0005YR-OL; Wed, 04 Jan 2017 01:15:24 +0000 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1cOaAL-0004ay-F4; Wed, 04 Jan 2017 01:14:59 +0000 Received: by mail-pg0-x241.google.com with SMTP id b1so35171731pgc.1; Tue, 03 Jan 2017 17:14:39 -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=FH9sB45sbdInCZHw4eN++Kq6mycDlyv5IzJDbJn9mbo=; b=BoUZBevVviRT1pm4P0xOvoWofVSLeKq1CgWsRybVOLpHFTeALr8mZZqqlVPk31bdre FECZ8FvT+1yVbmXgaAyuyFKds+PbkrrDvtKqXdZqNnO2HsMR36kVDUfCmiQqDMQpm6qd mCVzbJW6AUxrcShKR+J/ug17tLddY+PuqBKnTzDtEwGUwr4Z0U7q9d2y8sBMc8GBuOKY VOh9Av9ENd/1ZNtHiTg7eE/XQOKNAZLPMWWyk+z4NO9Wj+G+y/KbmwdNsne/M+hlbqMh nijNATIbDVObUFl3/kRoZnrxzZ4gMDeJwdsgSORyC1K7XRzctoOKcgQWMLSVAu7CtxP4 RGJw== 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=FH9sB45sbdInCZHw4eN++Kq6mycDlyv5IzJDbJn9mbo=; b=durqsyxsaiQCIP4Mg/F6MpP7uirCUN2j0KzlphqvwiyDBi21mPKn0uMxjG4AksOz4r jM9FurSnlKwtZoEeepynDzQD7SuMmt1z/noqDK2TRHkNOWnZ0mKWmObwzcPwTDatcO/l 5mWCWEuzgX40o+FGU2HBLCgo6iWs1iUSdODafuYNO0bA11aaDC/YGd0EmbFXwSRv6p1w Zkv6KyZXWJMSjt++cXZtd0UPP5FYA/iBXWsgNMOtm9cdQ93y/7KUkrva66IN8mevv4yo ITqCvQGD0c00GzF0LCuKqtz1cx+k+giPF75JaN9x0Mn/IR2xCLdnlhbOaYxAK9Xz5cvq Ei+g== X-Gm-Message-State: AIkVDXIIVKQNg+huPQKQIaYsC+i5HMWmo2RRhZ62a+hJJBT76XEXi5ruHrDw/dH3/yOh/w== X-Received: by 10.99.51.76 with SMTP id z73mr118329191pgz.173.1483492479034; Tue, 03 Jan 2017 17:14:39 -0800 (PST) Received: from fainelli-desktop.broadcom.com ([192.19.255.250]) by smtp.gmail.com with ESMTPSA id 64sm142574551pfu.17.2017.01.03.17.14.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 03 Jan 2017 17:14:38 -0800 (PST) From: Florian Fainelli To: linux-arm-kernel@lists.infradead.org, catalin.marinas@arm.com Subject: [PATCH v5 3/4] ARM: Add support for CONFIG_DEBUG_VIRTUAL Date: Tue, 3 Jan 2017 17:14:16 -0800 Message-Id: <20170104011417.1496-4-f.fainelli@gmail.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170104011417.1496-1-f.fainelli@gmail.com> References: <20170104011417.1496-1-f.fainelli@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170103_171457_737020_17F8F622 X-CRM114-Status: GOOD ( 16.73 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: nicolas.pitre@linaro.org, mark.rutland@arm.com, matt@codeblueprint.co.uk, will.deacon@arm.com, 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-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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). Acked-by: Russell King Signed-off-by: Florian Fainelli Acked-by: Laura Abbott --- 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 5fab553fd03a..4700294f4e09 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);