From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DF609606A32C for ; Wed, 2 Dec 2026 20:58:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=zVcoGnru Received: by kanga.kvack.org (Postfix) id 0784C6B05B1; Wed, 2 Dec 2026 15:58:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 04FB56B05B5; Wed, 2 Dec 2026 15:58:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E800D6B05B7; Wed, 2 Dec 2026 15:58:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B7D1B6B05B1 for ; Wed, 2 Dec 2026 15:58:20 -0500 (EST) Received: from smtpin15.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B04D9A04F1 for ; Wed, 2 Dec 2026 20:58:18 +0000 (UTC) X-FDA: 85500255876.15.7E4EFC0 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by imf27.hostedemail.com (Postfix) with ESMTP id CE9244000A for ; Wed, 2 Dec 2026 20:58:16 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=zVcoGnru; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of vitorhg20080@gmail.com designates 209.85.128.46 as permitted sender) smtp.mailfrom=vitorhg20080@gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796245096; b=Q9qJukX4x4o7DGZI7WwyxGcJ7HmjWHNC/RVjAQqIxKiW9BbqA8DfUvL3B2r7YctxswJlb3 n/L950XQUa7yOSzmkFmzy5Z0O+tKXhpf8fgldKc7Z1wyOyghJQUJTfZ60nUX+/9myyH4U0 0820LXkWfwT3YHsvUoettxgJLHRe2XQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796245096; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=S3O+Hh26qnCTcO7bSf3f+7yLXOxppkMqeUCauX6HeEQ=; b=mFgFn9jMmvdP/jPP4vbeFPkvAAiGOCi4MgRwItme/ZmjfJ85RINYe5QpvRWOzs8c6NfGWS TO34NnaDmNyiSBrLVFWlnPhUKxdVOBvr1565jRYGqdrQToPeQ8DyWZF4BkEdmafFAxXPoM 11vV5D9BhRI2CugacKnc7I0wB9Su9rI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=zVcoGnru; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of vitorhg20080@gmail.com designates 209.85.128.46 as permitted sender) smtp.mailfrom=vitorhg20080@gmail.com Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-4bb9d8e8e90so104563595e9.3 for ; Wed, 02 Dec 2026 12:58:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796245095; x=1796849895; darn=kvack.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=S3O+Hh26qnCTcO7bSf3f+7yLXOxppkMqeUCauX6HeEQ=; b=zVcoGnruWQM5ON9DEaSiqNFhxaVpIhvpX0Yg2erOmniuSj1W0bxLR0FkBclDsOj6h4 s34lg8MpalFuBBUlu6w6IofYeIeofCUMxCWL6OvTGFCQ+8wLNeHRwfnNpwvu0hokJrsI PuiXWpOC3ia/s2B/JP4w0+byAP7ZLuXJwrSIAS/IcuAiYPGZffDsThawDG7OXGBh32JU HYod7/85PtqZ3RAAMOwyWUPUUA2LasQ3twm6VMirXM4XLYDf/ZBf14+ou8G21sZiwKqF l8/bcyuwp3L+6gQfGuVmgZgzBzUXRCwI7Zkjic01V6FYjhmgu+rGZOF1GSWP+X6F85GS hd+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796245095; x=1796849895; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=S3O+Hh26qnCTcO7bSf3f+7yLXOxppkMqeUCauX6HeEQ=; b=mDgo1fvFslaecbl81q0qEdiwaQwdTIstRAnuT2sX6WPzsdqxfuKTg4yFXRvqBSoWki To6DE+0aB9OS4tUshg8oRTgKDMt7Tt4UEG1vHY7qjYIaOIBkpxmjJ2ETZIZ1wOeo7T9+ d13i7sD6eDlYUf9j88PRZhtWLjQmsxRWqttnbvV1Oq2l++Qh3wXd2ZA6hpq3QQYXUo1F r/0qAkMvVgIZNx8yR6qEvpUIxMmpKw3xxKadpVhWT0JI/xhXx+srNzk8CezcHd2Lubwo Nhyrcy53eICFpfwVW8OQjA4s98sJ0SjxpmkJRAOSHXKT+3PKxejrhfcrATzRD3HHkp8r UsMg== X-Forwarded-Encrypted: i=1; AL+hErEBw+MXUZ9SbCouK6V8uLoA5ku83E5Grk7nVszlDYzfF5cMHSGnkdz4o/yJpWCpHHsQIg8xpXVr4Q==@kvack.org X-Gm-Message-State: APuU4Z/yWzlH0id/Vkz8+ALveI+e4PaToceq6phhcNmgb1lC0SNKuS8M GMJ2iCZT/IlVlqyqg80XqiQNz2D+MTUzJJBvmS0/1fyWFooP0o/jcU2M X-Gm-Gg: AZFdTZdysVr8E+m11ZLX5y2mpx0K+QyZ39rjLpTLaRAjHyC6DfIy3Kd83Jt42wq6NMp LUFsYqXoevxBndOcF5uHbol47ZzMyzPnId7RlPFJh29qpSqF2OCqGvbegVXCbkQWdir2gGLFaQ/ j28RKshwGsSb0zQmkMVqiFE+6zwyLkkSnesZIoQP7s02xy498P8uJt16LOPkf93lPJXmV4t0yuv wEfxK5Go+/0gJpH0vJ93D7PuZyRWCgte5b257tqpyYNjxvJZQ4OsJI9IxBwt/G85kyxE4h6pCEM Ri8DzL4ObtxNbKXWHPFel/+vTjs5ornIe8vPCzRV7DJYLXnbs/KZ1PJ8yslLDU+B2zQLcZVEFHg AUsvbTv/zAoZbh05oZQWMeiJZfv4EruFZ7bf/N3RjD1EoqhbuLsyCkaul5zBn2ocZlhS5Rt6YkI T3q2v5xZvb+0BlxS4RxOh7EAfwtwtcwANUiz+GWU0xOrQHFVZoQefmhcds12eA1znzqBpR775xa oEwXl2xe3mdn8qovLaT0EsfOT9vjwk4dTFo5U6lIc59xLAxm66mzDOd4BGkl9XTNdvzFpwA2bdX /UVQcTnN1sqErk3IoPSMmIMVfK8EjdkaLel1aArYi/h7larFAvhDT4p0rdAegaZD++HrN3JD1QP oOmfHXVw= X-Received: by 2002:adf:e19e:0:b0:4a2:dae8:ca5f with SMTP id ffacd0b85a97d-4abb2ece9cbmr76705596f8f.36.1796245095104; Wed, 02 Dec 2026 12:58:15 -0800 (PST) Received: from localhost (bl20-90-91.dsl.telepac.pt. [2.81.90.91]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab69bf630esm81720856f8f.16.2026.12.02.12.58.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 12:58:14 -0800 (PST) From: =?UTF-8?q?V=C3=ADtor=20Hugo=20Silva?= To: linux-riscv@lists.infradead.org Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kees@kernel.org, david@kernel.org, akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, sashiko-reviews@lists.linux.dev, =?UTF-8?q?V=C3=ADtor=20Hugo=20Silva?= Subject: [PATCH RFC v4] riscv: Synchronize instructions written to ESP32-P4 PSRAM Date: Wed, 2 Dec 2026 20:58:04 +0000 Message-ID: <20261202205804.435026-1-vitorhg20080@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261130122328.280061-1-vitorhg20080@gmail.com> References: <20261130122328.280061-1-vitorhg20080@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: CE9244000A X-Spam-Status: No, score=-14.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: 4sw7fbnaj51uqujz8dnrmthsak9374tc X-Rspam-User: X-HE-Tag: 1796245096-943326 X-HE-Meta: U2FsdGVkX18BgG09eqVXyYDDAEMPJ/rIWtlo/9GfkITanDlVB7C5Hgo7XDEd1Mr3ZHujYb1sk/xx6nVSA0cGUy/0ZHKrHgTwIZXNKANP4m3h0LK3ZQBoStrfpO5q4VgXvZmJa2nNQ4Ltpj6oQc4UeqGoryzcb1tJapqwYpv9qRWiVeoaU0ZDECcX60bAGTpHiUqsVeKp63w5kOuR29B7IG5hl62SqlhpZ0vyOUS7nmipQdk5vnKEcQYKmJRiVGt1h743WKFyxaP3s02eX3TeYPeEOfgvsckROacASlnEqF7yGg9oWpajCI+FQSI2w2FMa6Qm/PKNwOcZfcr6h/9y+wr2AULvWBqYCipOoVJ4EL1qeEjeD1rL0gBwmUuAkJT/Bs1XjnWi2mIbMa2UrDe/W+APGrTXlgta3rkYzlqI1t1ig8fP8ATgPXhJRlZUsI+yhTwD+bkmiAGzwcA8xxaS6rJR3F4FYpnI9Czz9Eqy905d4z7IHMw31qkrbZbXr2aHJnuV4ui4s4azQQnr+1Hy+Q7klVHO1etREgeOgt1XyM0CGd78o9GYtbW3JpQ0c17zvWsOBQaIcUpcJEFIAyE5rk41KsNduFW5yfTmRS4uxFSWXDZe25G32b0+pddfNotQQVO6E0js/ru47hOVSEHayfQ298G7KofTgwCzI3LMq8PkfbDyiItA1IyIa4I1X74KUYYgnpXKTfLS0heqrs8sKQ+LwaIjwDkcpAxp9mkuUFywoS5w0UQ89B/5D+UYEZTbV5Fto8lZVQjGRfycwYlCwOytxnhsKMN/cw2Dk0Wx49TPHkoFX4InAQ41rsm7onYPMbPHyGEkFdTM+wUBioiAIYdxf1hEUpe+8s115yc3TTtVNIY1SAq3GpiNIMas3fxOSBFNCWe7gNrZmKyhpe1BnfxoZFvl0fmXd+4uUtER5r02bgtma1Ml5OOId2OHMtK7L0kYFI9uyDFpEsYgefQ VJ/Ei/Kn XW7cWCkBUk5MR/jIalDZIqgDuZbia6scgwgHFguCfN0IV+GR6kQgCzTMF1yR6yTvqeDIRw5CFsCAbqdSY6Nr2/DmlS5w5TFleJdP+DmJzC9MeNHNGUq6VbFSU53ZWxYkedi8MA3eRajJirtZoqiHGz4I4Iwa70FyJ+nemHWb3THCWjzXgMuTec2BrZrc3PRAYb5lSftz795zNHh0aYB3dBut0R9lcCZUkb+d/N3VoUdp66Z2Y+pSsJhnrD/Ld0+MdsNbrXcWRpsXAjY3a/9MGcyXcqEolaGjUKmZ4pw8AJs5MMl2ZY4Tnt0CFyMwWdOGDVdTqCpsxqVqd8DvjawR2I3KFSgq8LVCLPy2dYqhA/AD7kkYFxDC0ipTEnNtJl1UT+3i1HYDqlsDM4cEqGZRInoVTiqlqdY0fN7xpb51Drgdgw+U5xNc6zsyRL66h2gZUG+4t38XAJBPsww2iGv4/2W6LSBCHLkQolmrBtPfCkXI0HwW8T8VzhZJ02fKvYsnDwSm04x8SOq6QvtSj/qxai328CWUOPZ4mWNM0FUMl9r5dXDL47Wh8l93jvhifB+MrGOvFCXauuIsg+0dMy3ky0Pe2rdm6oPeM6UGti7+Lnm2gRuUv0WP7iTx1zqBfdIsbOA/KCPOC4w/WTuWnF18gOFlXfzSqvBoOcXqUP80rljtTdu3rKHKJSavuQkl9MS2oP/tBIMTHakOpCQqkfetuvKeIdtUgW61LEZF2UiRCbsA1MiTpXsrZK4tYS75CDO+PishWXmZA3hAPdpMo4aXgdvI8sOo654AfI+NiRPMMCUocA3UuqmmO16zJYlIfWmi0K3a6PV58lR7nEkSZHZjv5khnRp8o2GVTTPhHuZbQ+BAqhqQ= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On ESP32-P4, loads can observe updated instruction bytes in the data cache while instruction fetch still sees stale bytes. During boot, runtime constant patching can then use an invalid address. Write back L1D and L2 for the affected PSRAM range, invalidate L1I and issue fence.i. Use 32 KiB chunks so interrupts can run between chunks. Bound polling per command and stop issuing commands after a timeout. Treat failure to synchronize patched kernel text as fatal and exclude the synchronization helpers from kprobes. Use checked synchronization for executable loading, signal return trampolines, the first executable NOMMU mapping of a region and growth of the NOMMU program break. A failed mmap synchronization discards the new mapping. A failed brk synchronization returns the current break. Synchronize remote writes to executable NOMMU mappings as well. This covers ptrace and /proc/pid/mem, whose writes previously used copy_to_user_page() and only issued fence.i. Report synchronization failure through their byte-count interface. Keep the existing copy operation for other platforms. On ESP32-P4, honor nonempty intervals in riscv_flush_icache(). Check that the requested interval is covered by the calling process's mappings under mmap_lock. Return -EINVAL for reversed intervals and -EFAULT for gaps. Propagate cache synchronization errors. Keep the existing whole-cache flush for empty intervals. An earlier board test reproduced stale instructions after fence.i alone. On the Waveshare ESP32-P4-Module-DEV-KIT, a userspace probe received SIGILL at the first instruction written to an executable mapping with the published v1 patch. With v2, the same probe executes modified instructions after riscv_flush_icache() with flags 0 and LOCAL. It also checks return values for unmapped, reversed and empty ranges and invalid flags. The board boots to a shell, runs bFLT programs and delivers SIGUSR1 to the shell. On the Waveshare board, a ptrace probe changed an instruction in an executable NOMMU mapping after the traced process had executed it once. The data readback confirmed the new instruction in both builds. Without remote-write synchronization the process executed the old instruction; with it, the process executed the replacement. The syscall also builds with ESP32-P4 support disabled in an RV32 NOMMU configuration. Detect the ESP32-P4 from the root Device Tree compatible before jump label initialization. Use that result to gate access to the cache controller and ESP32-P4 range handling in riscv_flush_icache(). Perform PSRAM synchronization in flush_icache_range() so callers such as KGDB and the module loader get the required cache maintenance. Synchronize the two instructions of an RV32 runtime constant together. Keep checked error handling for userspace. AI assisted with the cache paths and the test preparation. Link: https://docs.espressif.com/projects/esp-idf/en/stable/esp32p4/api-reference/system/mm_sync.html Signed-off-by: VĂ­tor Hugo Silva --- Changes in v4: - Synchronize ptrace and /proc/pid/mem writes to executable NOMMU mappings with the checked ESP32-P4 PSRAM cache helper. - Preserve the remote-write byte-count contract if synchronization fails. - On the Waveshare ESP32-P4-Module-DEV-KIT, the same ptrace probe executed an old instruction with v3 and the replacement with v4, although the data readback saw the replacement in both builds. v3: https://lore.kernel.org/r/20261130122328.280061-1-vitorhg20080@gmail.com arch/riscv/include/asm/cacheflush.h | 17 ++- arch/riscv/include/asm/runtime-const.h | 17 ++- arch/riscv/kernel/patch.c | 22 +++- arch/riscv/kernel/setup.c | 5 + arch/riscv/kernel/signal.c | 6 +- arch/riscv/kernel/sys_riscv.c | 43 ++++++- arch/riscv/mm/cacheflush.c | 149 +++++++++++++++++++++++++ fs/binfmt_flat.c | 6 +- fs/exec.c | 10 +- include/linux/cacheflush.h | 14 +++ mm/nommu.c | 44 +++++--- 11 files changed, 296 insertions(+), 37 deletions(-) diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h index c2b0a2928f06..10cf0a105fe1 100644 --- a/arch/riscv/include/asm/cacheflush.h +++ b/arch/riscv/include/asm/cacheflush.h @@ -75,15 +75,30 @@ void flush_icache_mm(struct mm_struct *mm, bool local); #endif /* CONFIG_SMP */ +#define flush_icache_range flush_icache_range +#ifdef CONFIG_RISCV_ESP32P4_CLIC +extern bool riscv_esp32p4_cache_enabled; + +void flush_icache_range(unsigned long start, unsigned long end); +int esp32p4_sync_user_text(unsigned long start, unsigned long end); +int copy_to_user_page_checked(struct vm_area_struct *vma, struct page *page, + unsigned long addr, void *dst, + const void *src, int len); + +#define flush_icache_user_range_checked esp32p4_sync_user_text +#define copy_to_user_page_checked copy_to_user_page_checked +#else +#define riscv_esp32p4_cache_enabled false + /* * RISC-V doesn't have an instruction to flush parts of the instruction cache, * so instead we just flush the whole thing. */ -#define flush_icache_range flush_icache_range static inline void flush_icache_range(unsigned long start, unsigned long end) { flush_icache_all(); } +#endif extern unsigned int riscv_cbom_block_size; extern unsigned int riscv_cboz_block_size; diff --git a/arch/riscv/include/asm/runtime-const.h b/arch/riscv/include/asm/runtime-const.h index ce2008bb5227..7000ced5d758 100644 --- a/arch/riscv/include/asm/runtime-const.h +++ b/arch/riscv/include/asm/runtime-const.h @@ -190,8 +190,7 @@ static inline void __runtime_fixup_caches(void *where, unsigned int insns) { - /* On riscv there are currently only cache-wide flushes so va is ignored. */ - __always_unused uintptr_t va = (uintptr_t)where; + uintptr_t va = (uintptr_t)where; flush_icache_range(va, va + 4 * insns); } @@ -243,8 +242,15 @@ static inline void __runtime_fixup_32(__le16 *lui_parcel, __le16 *addi_parcel, u addi_res = cpu_to_le32(addi_insn); lui_res = cpu_to_le32(lui_insn); mutex_lock(&text_mutex); - patch_insn_write(addi_parcel, &addi_res, sizeof(addi_res)); - patch_insn_write(lui_parcel, &lui_res, sizeof(lui_res)); + if (IS_ENABLED(CONFIG_32BIT) && riscv_esp32p4_cache_enabled) { + /* The RV32 pair is contiguous and can share one cache sync. */ + __le32 insns[] = { lui_res, addi_res }; + + patch_text_nosync(lui_parcel, insns, sizeof(insns)); + } else { + patch_insn_write(addi_parcel, &addi_res, sizeof(addi_res)); + patch_insn_write(lui_parcel, &lui_res, sizeof(lui_res)); + } mutex_unlock(&text_mutex); } @@ -252,7 +258,8 @@ static inline void __runtime_fixup_ptr(void *where, unsigned long val) { #ifdef CONFIG_32BIT __runtime_fixup_32(where, where + 4, val); - __runtime_fixup_caches(where, 2); + if (!riscv_esp32p4_cache_enabled) + __runtime_fixup_caches(where, 2); #else __runtime_fixup_32(where, where + 8, val); __runtime_fixup_32(where + 4, where + 12, val >> 32); diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 2b5adf01c550..a10b91a445c9 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -208,14 +208,15 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) int ret; ret = patch_insn_set(addr, c, len); - if (!ret) - flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len); + if (ret) + return ret; - return ret; + flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len); + return 0; } NOKPROBE_SYMBOL(patch_text_set_nosync); -int patch_insn_write(void *addr, const void *insn, size_t len) +static int patch_insn_copy(void *addr, const void *insn, size_t len) { size_t size; int ret; @@ -237,13 +238,24 @@ int patch_insn_write(void *addr, const void *insn, size_t len) return 0; } +NOKPROBE_SYMBOL(patch_insn_copy); + +int patch_insn_write(void *addr, const void *insn, size_t len) +{ + int ret = patch_insn_copy(addr, insn, len); + + if (!ret && riscv_esp32p4_cache_enabled) + flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len); + + return ret; +} NOKPROBE_SYMBOL(patch_insn_write); int patch_text_nosync(void *addr, const void *insns, size_t len) { int ret; - ret = patch_insn_write(addr, insns, len); + ret = patch_insn_copy(addr, insns, len); if (!ret) flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len); diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index d79f99b86033..f99c8dc33601 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -258,6 +258,11 @@ static void __init parse_dtb(void) if (early_init_dt_scan(dtb_early_va, dtb_early_pa)) { const char *name = of_flat_dt_get_machine_name(); +#ifdef CONFIG_RISCV_ESP32P4_CLIC + /* Identify the cache hardware before jump label patching. */ + riscv_esp32p4_cache_enabled = + of_flat_dt_is_compatible(of_get_flat_dt_root(), "esp,esp32p4"); +#endif if (name) { pr_info("Machine model: %s\n", name); dump_stack_set_arch_desc("%s (DT)", name); diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 59784dc117e4..05266d7c1466 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -21,7 +22,6 @@ #include #include #include -#include #include unsigned long signal_minsigstksz __ro_after_init; @@ -453,7 +453,9 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, addr = (unsigned long)&frame->sigreturn_code; /* Make sure the two instructions are pushed to icache. */ - flush_icache_range(addr, addr + sizeof(frame->sigreturn_code)); + err = flush_icache_user_range_checked(addr, addr + sizeof(frame->sigreturn_code)); + if (err) + return err; regs->ra = addr; #endif /* CONFIG_MMU */ diff --git a/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c index 22fc9b3268be..36bdfdedf116 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -5,8 +5,9 @@ * Copyright (C) 2017 SiFive */ +#include +#include #include -#include #include static long riscv_sys_mmap(unsigned long addr, unsigned long len, @@ -52,6 +53,33 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len, } #endif +#ifdef CONFIG_RISCV_ESP32P4_CLIC +static int esp32p4_flush_icache_mapped_range(unsigned long start, + unsigned long end) +{ + struct mm_struct *mm = current->mm; + struct vm_area_struct *vma; + unsigned long covered = start; + int ret = -EFAULT; + + VMA_ITERATOR(vmi, mm, start); + + mmap_read_lock(mm); + for_each_vma_range(vmi, vma, end) { + if (vma->vm_start > covered) + break; + covered = vma->vm_end; + if (covered >= end) { + ret = flush_icache_user_range_checked(start, end); + break; + } + } + mmap_read_unlock(mm); + + return ret; +} +#endif + /* * Allows the instruction cache to be flushed from userspace. Despite RISC-V * having a direct 'fence.i' instruction available to userspace (which we @@ -63,8 +91,8 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len, * * sys_riscv_flush_icache() is defined to flush the instruction cache over an * address range, with the flush applying to either all threads or just the - * caller. We don't currently do anything with the address range, that's just - * in there for forwards compatibility. + * caller. The generic implementation uses a whole-cache flush. ESP32-P4 + * also synchronizes mapped PSRAM in the requested range. */ SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end, uintptr_t, flags) @@ -73,6 +101,15 @@ SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end, if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL)) return -EINVAL; +#ifdef CONFIG_RISCV_ESP32P4_CLIC + if (riscv_esp32p4_cache_enabled) { + if (end < start) + return -EINVAL; + if (end > start) + return esp32p4_flush_icache_mapped_range(start, end); + } +#endif + flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL); return 0; diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c index f8ead7cb7c7d..52a1e02684c4 100644 --- a/arch/riscv/mm/cacheflush.c +++ b/arch/riscv/mm/cacheflush.c @@ -4,10 +4,159 @@ */ #include +#include +#include +#include +#include #include +#include #include +#include #include #include +#include + +#ifdef CONFIG_RISCV_ESP32P4_CLIC +bool riscv_esp32p4_cache_enabled __ro_after_init; + +#define ESP32P4_CACHE_BASE 0x3ff10000 +#define ESP32P4_CACHE_SYNC_CTRL 0x98 +#define ESP32P4_CACHE_SYNC_MAP 0x9c +#define ESP32P4_CACHE_SYNC_ADDR 0xa0 +#define ESP32P4_CACHE_SYNC_SIZE 0xa4 + +#define ESP32P4_CACHE_INVALIDATE BIT(0) +#define ESP32P4_CACHE_WRITEBACK BIT(2) +#define ESP32P4_CACHE_SYNC_DONE BIT(4) +#define ESP32P4_CACHE_SYNC_MAX_POLLS 1000000 + +#define ESP32P4_CACHE_L1_I0 BIT(0) +#define ESP32P4_CACHE_L1_D BIT(4) +#define ESP32P4_CACHE_L2 BIT(5) + +#define ESP32P4_PSRAM_START 0x48000000UL +#define ESP32P4_PSRAM_END 0x4c000000UL +#define ESP32P4_CACHE_MAX_LINE 128 + +static int esp32p4_cache_sync(u32 map, u32 start, u32 size, u32 op, + unsigned int count) +{ + /* The caller holds local IRQs disabled on the single supported hart. */ + static bool sync_failed; + void __iomem *base = (void __iomem *)ESP32P4_CACHE_BASE; + unsigned int polls; + + if (sync_failed) + return -EIO; + + writel(map, base + ESP32P4_CACHE_SYNC_MAP); + writel(start, base + ESP32P4_CACHE_SYNC_ADDR); + writel(size, base + ESP32P4_CACHE_SYNC_SIZE); + + while (count--) { + writel(op, base + ESP32P4_CACHE_SYNC_CTRL); + /* Order the sync command before reading its completion status. */ + mb(); + for (polls = 0; polls < ESP32P4_CACHE_SYNC_MAX_POLLS; polls++) { + if (readl(base + ESP32P4_CACHE_SYNC_CTRL) & ESP32P4_CACHE_SYNC_DONE) + break; + cpu_relax(); + } + if (polls == ESP32P4_CACHE_SYNC_MAX_POLLS) { + sync_failed = true; + return -ETIMEDOUT; + } + } + + return 0; +} +NOKPROBE_SYMBOL(esp32p4_cache_sync); + +static int esp32p4_cache_writeback(u32 start, u32 size) +{ + int ret; + + /* Repeat writeback to avoid losing a sync with conflicting writes. */ + ret = esp32p4_cache_sync(ESP32P4_CACHE_L1_D, start, size, ESP32P4_CACHE_WRITEBACK, 2); + if (ret) + return ret; + + return esp32p4_cache_sync(ESP32P4_CACHE_L2, start, size, ESP32P4_CACHE_WRITEBACK, 2); +} +NOKPROBE_SYMBOL(esp32p4_cache_writeback); + +static int esp32p4_sync_text(unsigned long start, unsigned long end, bool fatal) +{ + unsigned long flags; + + if (!riscv_esp32p4_cache_enabled) + return 0; + + start = max(start, ESP32P4_PSRAM_START); + end = min(end, ESP32P4_PSRAM_END); + if (start >= end) + return 0; + + start = ALIGN_DOWN(start, ESP32P4_CACHE_MAX_LINE); + end = ALIGN(end, ESP32P4_CACHE_MAX_LINE); + + /* Allow interrupts between chunks if they were enabled on entry. */ + while (start < end) { + u32 size = min_t(u32, end - start, SZ_32K); + int ret; + + local_irq_save(flags); + ret = esp32p4_cache_writeback(start, size); + if (!ret) + ret = esp32p4_cache_sync(ESP32P4_CACHE_L1_I0, start, size, + ESP32P4_CACHE_INVALIDATE, 1); + if (ret) { + if (fatal) + panic("ESP32-P4 text cache sync failed: %d\n", ret); + local_irq_restore(flags); + return ret; + } + + local_flush_icache_all(); + local_irq_restore(flags); + start += size; + } + + return 0; +} +NOKPROBE_SYMBOL(esp32p4_sync_text); + +void flush_icache_range(unsigned long start, unsigned long end) +{ + esp32p4_sync_text(start, end, true); + flush_icache_all(); +} +EXPORT_SYMBOL(flush_icache_range); +NOKPROBE_SYMBOL(flush_icache_range); + +int esp32p4_sync_user_text(unsigned long start, unsigned long end) +{ + int ret = esp32p4_sync_text(start, end, false); + + if (!ret) + flush_icache_all(); + + return ret; +} +NOKPROBE_SYMBOL(esp32p4_sync_user_text); + +int copy_to_user_page_checked(struct vm_area_struct *vma, struct page *page, + unsigned long addr, void *dst, + const void *src, int len) +{ + instrument_copy_to_user((void __user *)dst, src, len); + memcpy(dst, src, len); + if (vma->vm_flags & VM_EXEC) + return esp32p4_sync_user_text(addr, addr + len); + + return 0; +} +#endif #ifdef CONFIG_SMP diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c index b5b5ca1a44f7..890a81d805dc 100644 --- a/fs/binfmt_flat.c +++ b/fs/binfmt_flat.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -40,7 +41,6 @@ #include #include -#include #include #include @@ -830,7 +830,9 @@ static int load_flat_file(struct linux_binprm *bprm, #endif /* CONFIG_BINFMT_FLAT_OLD */ } - flush_icache_user_range(start_code, end_code); + ret = flush_icache_user_range_checked(start_code, end_code); + if (ret) + return ret; /* zero the BSS, BRK and stack areas */ if (clear_user((void __user *)(datapos + data_len), bss_len + diff --git a/fs/exec.c b/fs/exec.c index 8d2b14e1d455..9066d2bebd1a 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -23,6 +23,7 @@ * formats. */ +#include #include #include #include @@ -827,8 +828,13 @@ EXPORT_SYMBOL(open_exec); ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len) { ssize_t res = vfs_read(file, (void __user *)addr, len, &pos); - if (res > 0) - flush_icache_user_range(addr, addr + len); + + if (res > 0) { + int ret = flush_icache_user_range_checked(addr, addr + len); + + if (ret) + return ret; + } return res; } EXPORT_SYMBOL(read_code); diff --git a/include/linux/cacheflush.h b/include/linux/cacheflush.h index 55f297b2c23f..162dbded896b 100644 --- a/include/linux/cacheflush.h +++ b/include/linux/cacheflush.h @@ -26,4 +26,18 @@ static inline void flush_icache_pages(struct vm_area_struct *vma, #define flush_icache_page(vma, page) flush_icache_pages(vma, page, 1) +#ifndef flush_icache_user_range_checked +#define flush_icache_user_range_checked(start, end) ({ \ + flush_icache_user_range((start), (end)); \ + 0; \ +}) +#endif + +#ifndef copy_to_user_page_checked +#define copy_to_user_page_checked(vma, page, addr, dst, src, len) ({ \ + copy_to_user_page((vma), (page), (addr), (dst), (src), (len)); \ + 0; \ +}) +#endif + #endif /* _LINUX_CACHEFLUSH_H */ diff --git a/mm/nommu.c b/mm/nommu.c index 6c6f95329674..d438a856f1f3 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -16,6 +16,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include #include #include #include @@ -395,10 +396,9 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) return brk; } - /* - * Ok, looks good - let it rip. - */ - flush_icache_user_range(mm->brk, brk); + if (flush_icache_user_range_checked(mm->brk, brk)) + return mm->brk; + return mm->brk = brk; } @@ -1193,7 +1193,7 @@ unsigned long do_mmap(struct file *file, /* okay... we have a mapping; now we have to register it */ result = vma->vm_start; - current->mm->total_vm += len >> PAGE_SHIFT; + share: BUG_ON(!vma->vm_region); @@ -1201,6 +1201,19 @@ unsigned long do_mmap(struct file *file, if (vma_iter_prealloc(&vmi, vma)) goto error_vma_iter_prealloc; + /* Flush the region before publishing its first executable mapping. */ + if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { + ret = flush_icache_user_range_checked(region->vm_start, + region->vm_end); + if (ret) + goto error_free_mapped; + + region->vm_icache_flushed = true; + } + + if (region->vm_usage == 1) + current->mm->total_vm += len >> PAGE_SHIFT; + add_nommu_region(region); setup_vma_to_mm(vma, current->mm); @@ -1208,13 +1221,6 @@ unsigned long do_mmap(struct file *file, /* add the VMA to the tree */ vma_iter_store_new(&vmi, vma); - /* we flush the region from the icache only when the first executable - * mapping of it is made */ - if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { - flush_icache_user_range(region->vm_start, region->vm_end); - region->vm_icache_flushed = true; - } - up_write(&nommu_region_sem); return result; @@ -1250,6 +1256,7 @@ unsigned long do_mmap(struct file *file, show_mem(); ret = -ENOMEM; +error_free_mapped: /* in case that the region is allocated via do_mmap_private() */ if ((region->vm_usage == 1) && (region->vm_flags & VM_MAPPED_COPY)) free_page_series(region->vm_start, region->vm_top); @@ -1688,14 +1695,17 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr, len = vma->vm_end - addr; /* only read or write mappings where it is permitted */ - if (write && vma->vm_flags & VM_MAYWRITE) - copy_to_user_page(vma, NULL, addr, - (void *) addr, buf, len); - else if (!write && vma->vm_flags & VM_MAYREAD) + if (write && vma->vm_flags & VM_MAYWRITE) { + /* Report cache sync failures through the bytes copied API. */ + if (copy_to_user_page_checked(vma, NULL, addr, (void *)addr, + buf, len)) + len = 0; + } else if (!write && vma->vm_flags & VM_MAYREAD) { copy_from_user_page(vma, NULL, addr, buf, (void *) addr, len); - else + } else { len = 0; + } } else { len = 0; }