From patchwork Tue May 2 20:28:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Goel, Sameer" X-Patchwork-Id: 9708575 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 EB98F60385 for ; Tue, 2 May 2017 20:28:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DCB6A2815E for ; Tue, 2 May 2017 20:28:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CEB9528557; Tue, 2 May 2017 20:28: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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable 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 771142815E for ; Tue, 2 May 2017 20:28:44 +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: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:In-Reply-To: References:List-Owner; bh=dAng4W5mQK2LjOgxXnumSLcUEjKLrbVELSGjx6xecU8=; b=PEj 35hC1lOBdRzYK1n3Hl0KOsdoobucMRdUflikgfMselAnhSABEl6NnHKwdUdifo1N0joSVkERHmTAw yaPQG5nL/c75PmsS5lCMUQ7d1Nh+g5MBK8ta2ECUrJyneEK6OQfkD1zetrRQIA0rBEJRfY26bB4cr ZhxlOtRxXxeeD0EO3T/dbBr3+zncxxX/ruIoMDfO+iL5l1dbTk3dDBsRQ2GhkCpz29QmeO6R0Sbhf FSIUN2ZiZnYmZEej0npq1Yl53H5XuZZ6e7Nq1eyV8sL4EyLAtaZRD+W9THQ+WFsS0llvSJh5Ja0Ao PODTmvf0RTvEwa1/ES23Xccv3GME5cg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1d5ePa-0000Gd-FX; Tue, 02 May 2017 20:28:42 +0000 Received: from smtp.codeaurora.org ([198.145.29.96]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1d5ePX-0000Bf-17 for linux-arm-kernel@lists.infradead.org; Tue, 02 May 2017 20:28:40 +0000 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 20D156099A; Tue, 2 May 2017 20:28:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1493756898; bh=Lhr9vc1//JjBViORBUb5JDqcAqA/8fOp4YxFbCkecVw=; h=From:To:Cc:Subject:Date:From; b=L3ld3s8XAaAGAyzDuUjn2YqaDIiPLKpBKTspb7QImvScZJbbR3AUPH/zYCxIwDOu8 cVUeRYxDJUU0oYBCLSPTp776Waqh3p/H9WkMRcP4jwqmPuqCcF8270Zpsax1ZslBqJ B8Uu4sBv+rHTC0qVTUMypB6Xp3eUbBHVK3+SaQQM= Received: from sgoel-lnx.qualcomm.com (unknown [129.46.14.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: sgoel@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 188096073A; Tue, 2 May 2017 20:28:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1493756897; bh=Lhr9vc1//JjBViORBUb5JDqcAqA/8fOp4YxFbCkecVw=; h=From:To:Cc:Subject:Date:From; b=O6LZnv36rfDmfBbwGvWCPS2gspP8mcWGRCepT2BOi2eJrZQd0NbnNYVybi6wn3X1e cGcJxce+1vd2nER47r1Lwgj6DiMl5DJFbP+otVLJVJFDzyRprq1s4jm7pDF6P2wTx9 NDU4IjZPvUDDi8BMTUehdUFa+d/20Pi4Dxu9ZbJ4= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 188096073A Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=sgoel@codeaurora.org From: Sameer Goel To: linux-arm-kernel@lists.infradead.org Subject: [PATCH] arm64: Add translation functions for /dev/mem read/write Date: Tue, 2 May 2017 14:28:05 -0600 Message-Id: <1493756885-29704-1-git-send-email-sgoel@codeaurora.org> X-Mailer: git-send-email 1.8.2.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170502_132839_126318_303B063D X-CRM114-Status: GOOD ( 16.53 ) 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: Catalin Marinas , Will Deacon , linux-kernel@vger.kernel.org, Shanker Donthineni , Sameer Goel 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 Port architecture specific xlate and unxlate functions for /dev/mem read/write. This sets up the mapping for a valid physical address if a kernel direct mapping is not already present. This is a generic issue as a user space app should not be allowed to crash the kernel. This issue was observed when systemd tried to access performance pointer record from the FPDT table. Ported from commit e045fb2a988a ("x86: PAT avoid aliasing in /dev/mem read/write") Crash Signature: Unable to handle kernel paging request at virtual address ffff800008ff0000 pgd = ffff8007de8b2200 [ffff800008ff0000] *pgd=0000000000000000, *pud=0000000000000000 Internal error: Oops: 96000007 [#1] SMP ................ CPU: 0 PID: 1 Comm: systemd Not tainted 4.10.0 #1 task: ffff8007c0820000 task.stack: ffff8007c0900000 PC is at __arch_copy_to_user+0xb4/0x280 LR is at read_mem+0xc0/0x138 pc : [] lr : [] pstate: 80000145 sp : ffff8007c0903d40 .................... x3 : ffff800800000000 x2 : 0000000000000008 x1 : ffff800008ff0000 x0 : 0000fffff6fdac00 .................... Call trace: Exception stack(0xffff8007c0903b70 to 0xffff8007c0903ca0) [] __arch_copy_to_user+0xb4/0x280 [] __vfs_read+0x48/0x130 [] vfs_read+0x8c/0x148 [] SyS_pread64+0x94/0xa8 [] el0_svc_naked+0x24/0x28 Code: a88120c7 d503201f d503201f 36180082 (f8408423) Signed-off-by: Sameer Goel Tested-by: Shanker Donthineni --- arch/arm64/include/asm/io.h | 5 +++++ arch/arm64/mm/ioremap.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/arch/arm64/include/asm/io.h b/arch/arm64/include/asm/io.h index 0c00c87..c869ea4 100644 --- a/arch/arm64/include/asm/io.h +++ b/arch/arm64/include/asm/io.h @@ -183,6 +183,11 @@ static inline u64 __raw_readq(const volatile void __iomem *addr) #define iowrite32be(v,p) ({ __iowmb(); __raw_writel((__force __u32)cpu_to_be32(v), p); }) #define iowrite64be(v,p) ({ __iowmb(); __raw_writeq((__force __u64)cpu_to_be64(v), p); }) +extern void *xlate_dev_mem_ptr(phys_addr_t phys); +extern void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr); + +#define xlate_dev_mem_ptr xlate_dev_mem_ptr +#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr #include /* diff --git a/arch/arm64/mm/ioremap.c b/arch/arm64/mm/ioremap.c index c4c8cd4..ba7e63b 100644 --- a/arch/arm64/mm/ioremap.c +++ b/arch/arm64/mm/ioremap.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -105,6 +106,36 @@ void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size) EXPORT_SYMBOL(ioremap_cache); /* + * Convert a physical pointer to a virtual kernel pointer for /dev/mem + * access + */ +void *xlate_dev_mem_ptr(phys_addr_t phys) +{ + unsigned long start = phys & PAGE_MASK; + unsigned long offset = phys & ~PAGE_MASK; + void *vaddr; + + /* If page is RAM, we can use __va. Otherwise ioremap and unmap. */ + if (page_is_ram(start >> PAGE_SHIFT) && memblock_is_memory(phys)) + return __va(phys); + + vaddr = ioremap_cache(start, PAGE_SIZE); + /* Add the offset on success and return NULL if ioremap() failed */ + if (vaddr) + vaddr += offset; + + return vaddr; +} + +void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr) +{ + if (page_is_ram(phys >> PAGE_SHIFT)) + return; + + iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK)); +} + +/* * Must be called after early_fixmap_init */ void __init early_ioremap_init(void)