From patchwork Fri Jun 14 20:37:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 10996801 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 782A4112C for ; Fri, 14 Jun 2019 20:42:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67CD828733 for ; Fri, 14 Jun 2019 20:42:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5BC172876E; Fri, 14 Jun 2019 20:42:25 +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=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.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 E350728733 for ; Fri, 14 Jun 2019 20:42:24 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=9sV9bG7y8EE47OYVKW8ZCAl0c6DYZ2bjC8j6+RXBVu4=; b=rlYraqjsqYiN3i FLsEYmg5NFAuKxGUSP3B+8t7hk4GabCAu09Kyct0dIFETiJxqsKbUOcRVnC2JIcN3mRjsr10XIceu mlXhsZxY4aImdg2shCTOOKVWsI66NKgXCY5IHgV7vbDNG/EEm45qEbhlQ3c1ws9dHPWDzH/HHRBs6 jSs0oH+3lUuBFa0FeSTw+aHLFkpzwzFcUbGzXMmWUilG+bOp4WP8+iGNw3Sj1QOl9J6DXw2rb9sd8 8cE3TMJK3k1HelZ+udaH/3FGFRx9mhqJaI2mTl6kV20vtypxYizFrhIehLF1jGUgMHNaogiFM/qaf NL/WlKTlt2nrBRzpg/Ow==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hbt1d-0001Ta-KQ; Fri, 14 Jun 2019 20:42:17 +0000 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hbswt-0004LQ-Gm for linux-arm-kernel@lists.infradead.org; Fri, 14 Jun 2019 20:37:34 +0000 Received: by mail-pg1-x544.google.com with SMTP id s27so2173499pgl.2 for ; Fri, 14 Jun 2019 13:37:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TIy8YQDmiH0lGYDV1tdQuIF92/7kUVyaQcmdzLpFsEA=; b=DPpqEvR2xRVIvswlW2ndF1y9QKk/EJuzPD08Z5oPj4JN719FIPwvtEToWha++Fa1Hp Qb8VmkV96OQsM+BbP3DUKWo9ridoF9L2RV+ZNbYHAh3544fZJLPFVqEXyCPQQGyc1ld8 9f5H6/eK3vxM8AsDZbD/TWhdCBBhZQTLmxhYw= 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:mime-version:content-transfer-encoding; bh=TIy8YQDmiH0lGYDV1tdQuIF92/7kUVyaQcmdzLpFsEA=; b=r7gMvcwzhh63jSpyvIRqm53HD36KlfkXdkesjV4wZ9QU6lyNysAdgMjOhlVnmkvONM hJrAi4ETaCnNEHV9x8yXU8NzfRsZvvm7RROOFz6epXRG0W8vLo/FW1RBFJMLuyClerzy GEkNJ/D/vDfmNO29R/O6Lh16fCG/AaYRCGtAKudM6Dg8pVyGGrR3BSnz8aXuCR+TLFSV Enl1aykZ/Hd9NMjxNNTrqn2VU5PhRSKV+CejxWmxpCu59MOTFn8wElFdM8Uyv9VIerO/ vFVPsyRKJlpYEoGvjpe7WGiFE9lHJEmHcV0bvft9Xcjq/H7VKHrjjKVZkcR977RdUk9Z 82qw== X-Gm-Message-State: APjAAAWiUyqOzGXqahZVIWzfXk9uDGPQGONWvOlg4ETmeCQSSN18ugny PZcKW0QsifijJBc7IOa21ZHbtA== X-Google-Smtp-Source: APXvYqxmpfHSDjrUYD6uZNYIRRn0cr0CR+UNJC8dHrCxflAPdm/P9a9hwNIpx+rOpjwL7hA2RLbd6A== X-Received: by 2002:a63:81c6:: with SMTP id t189mr36883280pgd.293.1560544641989; Fri, 14 Jun 2019 13:37:21 -0700 (PDT) Received: from smtp.gmail.com ([2620:15c:202:1:fa53:7765:582b:82b9]) by smtp.gmail.com with ESMTPSA id x5sm3673187pjp.21.2019.06.14.13.37.21 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 14 Jun 2019 13:37:21 -0700 (PDT) From: Stephen Boyd To: Dan Williams Subject: [PATCH v2 3/5] memremap: Add support for read-only memory mappings Date: Fri, 14 Jun 2019 13:37:15 -0700 Message-Id: <20190614203717.75479-4-swboyd@chromium.org> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog In-Reply-To: <20190614203717.75479-1-swboyd@chromium.org> References: <20190614203717.75479-1-swboyd@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190614_133723_564744_30885740 X-CRM114-Status: GOOD ( 15.33 ) 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: devicetree@vger.kernel.org, linux-arm-msm@vger.kernel.org, Will Deacon , linux-kernel@vger.kernel.org, Evan Green , Bjorn Andersson , Rob Herring , Andy Gross , Catalin Marinas , linux-arm-kernel@lists.infradead.org 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 Sometimes we have memories that are supposed to be read-only, but when we map these regions the best we can do is map them as write-back with MEMREMAP_WB. Introduce a read-only memory mapping (MEMREMAP_RO) that allows us to map reserved memory regions as read-only. This way, we're less likely to see these special memory regions become corrupted by stray writes to them. Cc: Evan Green Cc: Rob Herring Cc: Bjorn Andersson Cc: Andy Gross Cc: Will Deacon Cc: Catalin Marinas Cc: Dan Williams Reviewed-by: Bjorn Andersson Signed-off-by: Stephen Boyd --- include/linux/io.h | 1 + kernel/iomem.c | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/linux/io.h b/include/linux/io.h index 32e30e8fb9db..16c7f4498869 100644 --- a/include/linux/io.h +++ b/include/linux/io.h @@ -159,6 +159,7 @@ enum { MEMREMAP_WC = 1 << 2, MEMREMAP_ENC = 1 << 3, MEMREMAP_DEC = 1 << 4, + MEMREMAP_RO = 1 << 5, }; void *memremap(resource_size_t offset, size_t size, unsigned long flags); diff --git a/kernel/iomem.c b/kernel/iomem.c index 93c264444510..10d5ef0ff09e 100644 --- a/kernel/iomem.c +++ b/kernel/iomem.c @@ -19,6 +19,13 @@ static void *arch_memremap_wb(resource_size_t offset, unsigned long size) } #endif +#ifndef arch_memremap_ro +static void *arch_memremap_ro(resource_size_t offset, unsigned long size) +{ + return NULL; +} +#endif + #ifndef arch_memremap_can_ram_remap static bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size, unsigned long flags) @@ -84,7 +91,10 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags) } /* Try all mapping types requested until one returns non-NULL */ - if (flags & MEMREMAP_WB) { + if ((flags & MEMREMAP_RO) && is_ram != REGION_INTERSECTS) + addr = arch_memremap_ro(offset, size); + + if (!addr && (flags & MEMREMAP_WB)) { /* * MEMREMAP_WB is special in that it can be satisfied * from the direct map. Some archs depend on the @@ -103,7 +113,8 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags) * address mapping. Enforce that this mapping is not aliasing * System RAM. */ - if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) { + if (!addr && is_ram == REGION_INTERSECTS && + (flags != MEMREMAP_WB || flags != MEMREMAP_RO)) { WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n", &offset, (unsigned long) size); return NULL;