Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp5815106ybe; Tue, 10 Sep 2019 09:11:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqwcKcsV1mrnchPTfs3RTMG1eEFvgOREx6uYq3ZnZpy92DkQYk2xKaEcZiNI6WjUUKv+vbrF X-Received: by 2002:a17:906:3e46:: with SMTP id t6mr26038903eji.104.1568131893239; Tue, 10 Sep 2019 09:11:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568131893; cv=none; d=google.com; s=arc-20160816; b=Evd3+XjILtedBGGBwcYm+iLTOMbgeJ5NbcRYpbOsZF5wphRcuS/r4YoK3aEFFzhnPX saykQZDb79LMy0RGSaI46weAFlXwnmwInh5IrI2xrDdg+QhkQT9dx3cnHUDiHHO41BkI wl/+FXqAmW6vLJks+E8STwTCzvTo92B+y08t7AAbJuVMfb1L9zCS9fcnagYU0qdfsEKY k/sn/dw0+lWKAVBOfl1LTfHJBpinsLZMDwhH5XPuuh50J1gIjFPUeR759HTnfoSz8tU3 4NCb0av/3N4RLuFQ/dh6QLUm2ZVP/0zv2x4SgGnXC+xYdUZAaZJEW8dpp3ptlrsZnwDI Oq1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=YIkLTnrkWt/EuuPaM/5KgusucJyV+/pu2XQTD6+l1B4=; b=aTIDut72Mz7Sq7Kyo3i2CkvzKm5zwqC5r5QdDEMV4+hCd/VG80dWJZwp0b6IlX9mzm 8R8v1FXvIeV3idfEgfDn+B9BhGabE0UY54UKq0cgbcm5Fr9ufqd/LXlcOAZNuBATw7aH yrFvYEEdoNQgPTHb4Hqx8WnyzibZ9P03IwKKq6azkdgWdwEcZI94DvYCZiJ+F6MeocVW QJr2yqykvAtEN7F27RmGt3D43Hpn8qazZoJzrENIAkxTWVVjaExHBQVt9D9hsLQZOOOW yWMphIwPNeZZhXAz+LI4KvejkXNH0YNKw9UXDfvJLamdIrpSvoxton84QIGSydg18InS WGeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=foUXLvfX; 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 d20si9893863edr.195.2019.09.10.09.11.08; Tue, 10 Sep 2019 09:11:33 -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=foUXLvfX; 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 S2436696AbfIJQJI (ORCPT + 99 others); Tue, 10 Sep 2019 12:09:08 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:33265 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2436679AbfIJQJI (ORCPT ); Tue, 10 Sep 2019 12:09:08 -0400 Received: by mail-pf1-f195.google.com with SMTP id q10so11807605pfl.0 for ; Tue, 10 Sep 2019 09:09:07 -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=YIkLTnrkWt/EuuPaM/5KgusucJyV+/pu2XQTD6+l1B4=; b=foUXLvfXEBZ8ItH2km73P/GyOobcyj/2AlkrAl5hS06ssjzVy3VMEdus9dZJ3XfRko n3d1oj1yZXYkG2g88eBnSetaXilib5WWGGyXSHK7SjUDXQXeIo8DFzlrCmNLoyCGk1MI BMHL2SWL8Fvoc+XCOlcilC9vuG29PN8KWwjVs= 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=YIkLTnrkWt/EuuPaM/5KgusucJyV+/pu2XQTD6+l1B4=; b=edRoGVyPXFdrfjUaniXxm+cRjpf7SvbF7+TE25uf708C4QC4bEeOPezKxv058MwDUd 0gIU3n/jrX25zfKuYA+zY6Qo9syM4+EyF9Yz7gCyxeYCmHzAikrBHGhBegpM2i2M1Pqw XNohgA5f+nnPE9ggbLVIbhERhZT0O7em0lP7F0JDUSYflhqwaprlJvsesk8xUiw8ivnd 0ke8yGryf1VvqHmDB5w5DqYP+qYAB5li6C+vZET9qMhnY8rp8UtvAVuMbaZatGZNjckd MUiNycy/260lJsnc21Ws2ery8Njp+5EMsZi4Njc+VoojR3u/p3HzQb5wQxCRoxMDl5TM K7LA== X-Gm-Message-State: APjAAAUnoJl6zOTc8pHx7xTWBckbamlsg3J8F6PCrw9z6UpST45fU0aP MgTrlYF5CGxlOpSFUwVJMCtvtw== X-Received: by 2002:aa7:81d1:: with SMTP id c17mr35893243pfn.219.1568131747300; Tue, 10 Sep 2019 09:09:07 -0700 (PDT) Received: from smtp.gmail.com ([2620:15c:202:1:fa53:7765:582b:82b9]) by smtp.gmail.com with ESMTPSA id em21sm106088pjb.31.2019.09.10.09.09.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2019 09:09:06 -0700 (PDT) From: Stephen Boyd To: Dan Williams Cc: linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, Evan Green , Rob Herring , Bjorn Andersson , Andy Gross , Will Deacon , Catalin Marinas Subject: [PATCH v3 3/5] memremap: Add support for read-only memory mappings Date: Tue, 10 Sep 2019 09:09:01 -0700 Message-Id: <20190910160903.65694-4-swboyd@chromium.org> X-Mailer: git-send-email 2.23.0.162.g0b9fbb3734-goog In-Reply-To: <20190910160903.65694-1-swboyd@chromium.org> References: <20190910160903.65694-1-swboyd@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. + * * 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)) { WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n", &offset, (unsigned long) size); return NULL; -- Sent by a computer through tubes