Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp177091ybe; Wed, 18 Sep 2019 15:05:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqxSatTtHXT4RDE0SNAgR6HLsHor8d+rwmOr4kjH9kU371wpFv4ISaGdDDmsLWubVf7tcKA8 X-Received: by 2002:aa7:d904:: with SMTP id a4mr12698990edr.41.1568844356301; Wed, 18 Sep 2019 15:05:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568844356; cv=none; d=google.com; s=arc-20160816; b=p6LEKPjRoWOktVwpRYnbE3b2dh1t92NV6qvZHF6Syk+iLQVnvd+ahtMeX3/H08lTC1 LZoUUX0kUCkeYHrW53caLCZbod31cXSaBWnMZVpFo++98gThrDju9Aar8b0TCBBkM6LZ ts8KFnq9Kl4tevcJZ1kyZslChu/7jrfaCGe+v3xhf4TYBTWCYqfDuN9SHZZ98F4y7SqS Qba8HhCac3xlS4hhSLP3CdlpGVNFkZ+HV3jbK+3SMN0nVtaUq4nxqpbD0nM3DKznKkzO /onFTzNf1fyhwQi5jBSSzxVhOC1Mo0Uj64WBYLnvI/AXdQwVqCk4JRTjPniHe097Qu75 UsMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=kYdtJ7oCFnyBm2ZB4A+7C5h6gT7MMWnprnThe/izcYI=; b=iQrkFRKqAQIQyjXYq30CntievN1VDAltnYf9sMQPacUGCQbJ9QxIBdY6aUc+dJNbWV pNa1DsBIzUxiBwQLiAdsuTDbFJReo8SChMBee+a+2BmerAUb3nKDImk4U+BWBZRLxQfk 0aYVCQ2LdipJhV/6ImwS/RlY5ShwI0yhRmcgc+Yc+mNPXpcKpuF4eJC5Mqq9POqaMP9p cvBzTV2KnFuLEl45EkGA91BrQV9UyAVpcTpRgHuMlokDDbekw+5arHijx05A0fFc9z5g F93ram8YUzYb45rUtteM7FpJdtJAt8sWao52gL8NsxZA7QfhREd7aMFlroYkvSsnkhyL Absg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FJYQZJbL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id jz16si3412644ejb.116.2019.09.18.15.05.32; Wed, 18 Sep 2019 15:05:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FJYQZJbL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731581AbfIRTiQ (ORCPT + 99 others); Wed, 18 Sep 2019 15:38:16 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:33974 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731564AbfIRTiQ (ORCPT ); Wed, 18 Sep 2019 15:38:16 -0400 Received: by mail-lf1-f65.google.com with SMTP id r22so556076lfm.1 for ; Wed, 18 Sep 2019 12:38:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kYdtJ7oCFnyBm2ZB4A+7C5h6gT7MMWnprnThe/izcYI=; b=FJYQZJbLlwnYy90gnbw5gVV6ZxnBb7ZVOPYNbwk/X+uv7Q2AcvUbaxXr5/I3hc68iC o1mnY1UhXLgFzBZgYShNRfik8r1itqOopd6dCzEH/iFUnarQHiflR2MAQp1g87ruIzZa GzycbwAQfEs/XaWi9qqDvP4CUHCYoSy9wDLwc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kYdtJ7oCFnyBm2ZB4A+7C5h6gT7MMWnprnThe/izcYI=; b=McrcwDyPdMRKWtROe3sH0Oyf/S4W0e2iDrC5tBVOFJyvGBMY4vWlVfxuksdq+BIG2W axUitm1JTTClEMXoboPOQb/NN7dty/9MBzsE2vKcUmzKQjgjYaC3UxJFjVl0MYl7+qF3 9QYv/dfDrOAwyQybL1f4bcdh/uTAOQkbyGoSb3r/bI4GR+rmjcH3J2fgzs6R/DY4LHCy mUzKyK/kp2ZbKPBU+RvSn/SG6tmBc4zCTNQwoCBVoErTZgRa3AS7CS4wqeg/mYQdtp8W ZaJDKOuoIO24kO91Xrs6MJ17c8Ee24hvHqi88OptBtWKc5EwayQKIPczLZ9TFKxe3Y+9 I6gw== X-Gm-Message-State: APjAAAWsTNJY7otVQ6vIctpL8G1uSTM6c6iDLxGQgcV+sVM5gWQFBywX MGjAIcTvqTZVG3frMloWEcLZpq3MIpk= X-Received: by 2002:ac2:44d2:: with SMTP id d18mr3058069lfm.67.1568835492945; Wed, 18 Sep 2019 12:38:12 -0700 (PDT) Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com. [209.85.208.181]) by smtp.gmail.com with ESMTPSA id a14sm1153634lfg.74.2019.09.18.12.38.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Sep 2019 12:38:11 -0700 (PDT) Received: by mail-lj1-f181.google.com with SMTP id d5so1093579lja.10 for ; Wed, 18 Sep 2019 12:38:11 -0700 (PDT) X-Received: by 2002:a2e:9bcc:: with SMTP id w12mr3122310ljj.181.1568835490845; Wed, 18 Sep 2019 12:38:10 -0700 (PDT) MIME-Version: 1.0 References: <20190910160903.65694-1-swboyd@chromium.org> <20190910160903.65694-4-swboyd@chromium.org> In-Reply-To: <20190910160903.65694-4-swboyd@chromium.org> From: Evan Green Date: Wed, 18 Sep 2019 12:37:34 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 3/5] memremap: Add support for read-only memory mappings To: Stephen Boyd Cc: Dan Williams , LKML , linux-arm-msm , linux-arm Mailing List , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , Rob Herring , Bjorn Andersson , Andy Gross , Will Deacon , Catalin Marinas Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Sep 10, 2019 at 9:09 AM Stephen Boyd wrote: > > 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 | 20 +++++++++++++++++--- > 2 files changed, 18 insertions(+), 3 deletions(-) > > diff --git a/include/linux/io.h b/include/linux/io.h > index accac822336a..15a63efcd153 100644 > --- a/include/linux/io.h > +++ b/include/linux/io.h > @@ -148,6 +148,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 62c92e43aa0d..6d76b7398714 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) > @@ -45,7 +52,7 @@ static void *try_ram_remap(resource_size_t offset, size_t size, > * @offset: iomem resource start address > * @size: size of remap > * @flags: any of MEMREMAP_WB, MEMREMAP_WT, MEMREMAP_WC, > - * MEMREMAP_ENC, MEMREMAP_DEC > + * MEMREMAP_ENC, MEMREMAP_DEC, MEMREMAP_RO > * > * memremap() is "ioremap" for cases where it is known that the resource > * being mapped does not have i/o side effects and the __iomem > @@ -53,6 +60,9 @@ static void *try_ram_remap(resource_size_t offset, size_t size, > * mapping types will be attempted in the order listed below until one of > * them succeeds. > * > + * MEMREMAP_RO - establish a mapping whereby writes are ignored/rejected. > + * Attempts to map System RAM with this mapping type will fail. Why should attempts to map RAM with this flag fail? MEMREMAP_WB will allow RAM and quietly give you back the direct mapping, so it seems like at least some values in this function allow RAM. Oh, I see a comment below about "Enforce that this mapping is not aliasing System RAM". I guess this is worried about cache coloring? But is that a problem with RO mappings? I guess the RO mappings could get partially stale, so if the memory were being updated out from under you, you might see some updates but not others. Was that the rationale? > + * > * MEMREMAP_WB - matches the default mapping for System RAM on > * the architecture. This is usually a read-allocate write-back cache. > * Moreover, if MEMREMAP_WB is specified and the requested remap region is RAM > @@ -84,7 +94,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 +116,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)) { Isn't this condition always true? Did you mean flags != MEM_REMAP_WB && flags != MEMREMAP_RO? > WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n", > &offset, (unsigned long) size); > return NULL; > -- > Sent by a computer through tubes >