All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support
@ 2026-12-05  1:24 Jiangtian Feng
  2026-12-05  1:24 ` [PATCH v16 1/9] uaccess: add generic fallback version of copy_mc_to_user() Jiangtian Feng
                   ` (8 more replies)
  0 siblings, 9 replies; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jiangtian Feng

History and handoff
===================

This series continues Tong Tiangen's work and is based on Ruidong Tian's
v15:

  https://lore.kernel.org/all/20260618092124.3901230-1-tianruidong@linux.alibaba.com/

Weilin Tong subsequently announced his takeover here:

  https://lore.kernel.org/all/20261111111033.627610-1-tongweilin@linux.alibaba.com/

With Weilin's agreement, I am preparing this version. Original authorship
and prior review history are preserved. Substantial rewrites are submitted
for fresh review.

Base: Linux v7.4-rc5
  fe6ff8d5d675c01793b9f051d16055dd4aa75244

Relevant v15 review history
===========================

The related MM fallback discussion covers the missing arm64 recovery path,
the need to avoid unnecessary server panics and Weilin's subsequent takeover:

  https://lore.kernel.org/all/20261110112641.1196786-1-xiangzao@linux.alibaba.com/

Ashok's cover reply noted the incorrect 0/8 patch count, corrected here:

  https://lore.kernel.org/all/akZWyYnAdnvlkZbi@hu-ashoraj-lv.qualcomm.com/

Ashok's Reviewed-by for patch 5/9 was explicitly conditional on a
tree-wide caller audit. That audit now covers all target-tree highpage
callers, and the khugepaged positive-residual checks are converted to
nonzero-error checks. The resulting Reviewed-by is included in patch 5:

  https://lore.kernel.org/all/akZ3t68so_q68OHx@hu-ashoraj-lv.qualcomm.com/

Ashok's patch 6/9 review raised the ACPI_APEI_SEA Kconfig gate, MOPS
changelog/macro/build handling, MTE failure state, and poison queue/offline
questions. The v16 changes below address the code and documentation points;
the related hardware and fault-injection boundaries remain in NOT_RUN:

  https://lore.kernel.org/all/akZ0Gax5zZ23PIv2@hu-ashoraj-lv.qualcomm.com/

Problem and field motivation
============================

Uncorrectable memory errors are a recurring operational concern on large
ARM64 systems. When such an error is consumed while the kernel reads memory
for a copy operation, an otherwise recoverable user or MM consumer can turn
into a fatal host event. Existing COW, KSM, khugepaged, migration, coredump,
uaccess and DAX/PMEM users already have error paths for these failures, but
arm64 needs the corresponding machine-check-safe copy primitives to return
through those paths instead of panicking.

This is also a long-standing downstream requirement. The public openEuler
OLK-5.10 branch has carried an arm64 copy_mc_to_kernel() implementation
since November 2022, and the public Anolis devel-5.10 branch has carried
copy_mc_[user]_highpage() support since 2023:

  https://gitee.com/openeuler/kernel/commit/ee7b5d7559e6f2ca633f8ab6fbd23c06991595ad
  https://gitee.com/anolis/cloud-kernel/commit/6d9fac4c059045e1ae70b9a5628038fdf3acb4de

These downstream references demonstrate established demand; they are not
evidence of upstream acceptance or proof of this version's correctness.

Solution and scope
==================

The series provides machine-check-safe user, kernel and page-copy paths.
Only explicitly marked accesses and exact EL1 data SEAs with SET=0 are
eligible for recovery. SET=0 means UER on RAS systems and is RES0 on
non-RAS systems. UC, UEO and other encodings are rejected. Instruction
aborts, table-walk and parity/ECC errors, S1PTW and faults without a
suitable exception-table handler retain the existing failure path.
Ordinary translation faults must not be swallowed by the new MC SEA fixup.

The buffer-copy interfaces retain the bytes-not-copied return convention.
The highpage interfaces use zero on success and -EFAULT on failure. All
highpage callers on the target tree were audited and treat any nonzero
return as failure without consuming a residual byte count. On arm64, every
data or tag-copy failure also queues the actual source PFN with flags=0;
a hugetlb tag failure uses the failing subpage. GHES may queue the same PFN,
so duplicate events are possible. Supported RAM callers rely on MM's
already-poisoned handling to avoid a second substantive isolation, not on
strict FIFO exactly-once or a final-recovery guarantee.

The GHES changes preserve the original exception context through deferred
processing. Kernel context is captured before the arch fixup decision:
recovered copies return an error, while rejected fixups retain the fatal
SEA path. USER and KVM/NA duplicate anti-storm behavior is retained, but a
duplicate KERNEL record still reaches the strict arch SEA/extable gate.

Non-APEI recovery is outside this version's scope. Adi's related proposal
is tracked here and is expected to be coordinated as a follow-up unless
maintainers request a unified scope:

  https://lore.kernel.org/all/20261028045028.72644-1-nikumbh.adi@gmail.com/

Shivank's batch-copy v4 is a separate series. This arm64 version does not
include its raw-range batching. I have proposed an explicit
ARCH_HAS_COPY_MC_BATCH opt-in so that adding arm64 ARCH_HAS_COPY_MC cannot
silently bypass the architecture's per-page MTE/KASAN/cache and failure
handling. The coordination discussion is here:

  https://lore.kernel.org/all/20261204034522.302814-1-jiangtianfeng@linux.alibaba.com/

Changes in v16
==============

- Rebase to v7.4-rc5 and correct the cover patch count.
- Adapt the generic copy_mc_to_user() fallback to the current uaccess
  layout without changing the existing x86/powerpc implementations.
- Adapt GHES exception-context propagation to queue aggregation, including
  the memory-failure flags and the synchronous !queued SIGBUS path. Keep
  USER/KVM duplicate anti-storm handling while allowing duplicate KERNEL
  records to reach the arch fixup gate.
- Merge the access error-zero extable types and keep ordinary-fault and
  TTBR0/PAN handling for the new kernel SEA type unchanged.
- Restrict kernel recovery to exact EL1 data FSC_EXTABT without S1PTW and
  with SET=0. Reject UC, UEO and other encodings; UEO support requires a
  separate RAS-version-aware change with fault-injection coverage.
- Handle MOPS EX_TYPE_UACCESS_CPY with direction-aware SEA fixups. In
  addition to the user side, accept copy_to_user's kernel source read to
  match the scalar path, while copy_from_user's kernel destination remains
  rejected. MOPS faults bypass the single-byte try-harder path to avoid
  repeating a zero-progress SEA. Ordinary zero-progress MOPS faults may
  therefore return the full residual instead of retrying one byte; scalar
  faults retain the existing behavior.
- Keep EX_TYPE_KACCESS_SEA source-only and reject MOPS destination writes.
- Keep the highpage 0/-EFAULT contract, audit all target-tree callers and
  convert the khugepaged positive-byte checks to nonzero error checks.
- Select ARCH_HAS_COPY_MC from ACPI_APEI_SEA, address the MOPS macro/fixup
  review and adapt the MTE declarations to v7.4-rc5. Failed destination
  pages remain unpublished and are freed through the existing MM paths.
  Match normal copy_highpage() for already-tagged migration destinations,
  and queue the actual failing source page or hugetlb subpage on error.
- Preserve the buffer residual-byte contract and unsigned-long types. Use
  the forward copy template so every completed store advances the current
  destination, including a correct MOPS Option A conversion on the error
  path. Leave the normal optimized memcpy implementation unchanged.
- Guard the KASAN memcpy_mc wrapper with CONFIG_ARCH_HAS_COPY_MC to avoid an
  undefined __memcpy_mc reference when COPY_MC is disabled.
- Add the copy_page/copy_mc_page and memcpy_mc/memcpy_mc_large KUnit cases
  with their architecture guards.

Validation
==========

- Relevant GHES, fault/extable, uaccess, copy-page, normal memcpy and MC
  objects build with W=1 in the default ARM64 configuration.
- Complete ARM64 Image build with Buildroot GCC 13.3.0 and binutils
  2.43.1: 43,010,560 bytes, SHA-256
  cfad340658f5e55a65b446ab76ff3c24505bcdad3aaac72227a8bd8fa77a39cd.
- A synthetic toolchain without MOPS assembler support builds the relevant
  fault/extable, uaccess, copy-page and MC objects with W=1. This tests the
  non-MOPS build path; it is not a claim about a particular old compiler.
- The final tree links vmlinux successfully with CONFIG_ARCH_HAS_COPY_MC=n
  and CONFIG_KASAN_GENERIC=y, without the earlier undefined __memcpy_mc
  reference. A matching KASAN=y/KASAN_GENERIC=y configuration also builds
  mm/kasan/shadow.o with W=1; this is a compile check, not KASAN runtime
  validation.
- Disassembly of memcpy_mc confirms that normal stores advance the current
  destination and the exception exit returns end - current_dst. MOPS
  main/epilogue faults perform the Option A conversion first. The normal
  memcpy source is byte-identical to the v7.4-rc5 base.
- The target-tree highpage caller audit covers MM COW/large folio, KSM,
  khugepaged and the arm64 internal caller; all use nonzero-error handling.
- Independent review of the residual state machine found that the original
  direction-selecting CPY operations could choose backward even without
  overlap. The final forward-only CPYFP/CPYFM/CPYFE revision was separately
  reviewed with no remaining High/Medium code issue.
- Independent review of the final SEA/uaccess changes found no
  High/Medium/Low code issue in the SET gate, direction matrix,
  source-only KACCESS handling or MOPS no-retry paths.
- Built-in KUnit on QEMU virt/Cortex-A57 ran ten cases with
  "pass:10 fail:0 skip:0 total:10", including copy_mc_page_test,
  memcpy_mc_test and the non-skipped slow memcpy_mc_large_test. The kernel
  mounted the Buildroot rootfs and reached login. QEMU was stopped by the
  collection timeout after these results.

NOT_RUN
=======

- Real UCE/SEA/GHES/EINJ injection, poison-page isolation and panic-boundary
  testing, including SET/WnR variants and duplicate error records.
- MOPS and MTE hardware execution, including MOPS prologue/main/epilogue
  fault injection and MTE failure-state validation.
- Partial-progress residual and the remaining negative-fault matrix across
  source alignments, copy-template paths, TTW/S1PTW/instruction/parity errors
  and incorrect uaccess directions. A separate test-only validation branch
  exercised a synthetic scalar zero-progress SEA with residual=8; that test
  code is not part of this submission.
- Runtime MM, uaccess, DAX/PMEM and coredump consumer tests.
- Actual source-PFN queue/refcount behavior, firmware-first queue-versus-final
  offline timing and dev_pagemap handling.
- A complete no-MOPS Image: available assemblers reject unrelated LSUI
  instructions elsewhere in the kernel. Relevant no-MOPS objects were built.

The clean-memory KUnit and static/build results do not establish any of
these fault-path behaviors.

Tool assistance
===============

Qoder, an AI coding assistant, assisted with source and review-history
comparison, rebase/conflict analysis, the SEA/MOPS/residual fixes, build and
disassembly review, and drafting this cover. The submitter is responsible
for reviewing the final content, validating the changes and responding to
review feedback.

Ruidong Tian (5):
  ACPI: APEI: GHES: use exception context to gate SIGBUS on poison
    consumption
  arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO into
    ACCESS_ERR_ZERO
  arm64: enable recover from synchronous external abort in kernel
    context
  lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests
  lib/tests: memcpy_kunit: add memcpy_mc() and memcpy_mc_large() test

Tong Tiangen (4):
  uaccess: add generic fallback version of copy_mc_to_user()
  mm/hwpoison: return -EFAULT when copy fail in
    copy_mc_[user]_highpage()
  arm64: support copy_mc_[user]_highpage()
  arm64: introduce copy_mc_to_kernel() implementation

 arch/arm64/Kconfig                   |   1 +
 arch/arm64/include/asm/asm-extable.h |  21 ++--
 arch/arm64/include/asm/asm-uaccess.h |   4 +
 arch/arm64/include/asm/extable.h     |   1 +
 arch/arm64/include/asm/mte.h         |   9 ++
 arch/arm64/include/asm/page.h        |  12 ++
 arch/arm64/include/asm/string.h      |   9 ++
 arch/arm64/include/asm/uaccess.h     |  17 +++
 arch/arm64/kernel/acpi.c             |   2 +-
 arch/arm64/lib/Makefile              |   2 +
 arch/arm64/lib/copy_from_user.S      |   6 +-
 arch/arm64/lib/copy_mc_page.S        |  43 +++++++
 arch/arm64/lib/copy_page.S           |  67 ++--------
 arch/arm64/lib/copy_page_template.S  |  70 ++++++++++
 arch/arm64/lib/copy_to_user.S        |  16 ++-
 arch/arm64/lib/memcpy_mc.S           |  84 ++++++++++++++
 arch/arm64/lib/mte.S                 |  29 +++++
 arch/arm64/mm/copypage.c             |  85 ++++++++++++++
 arch/arm64/mm/extable.c              |  54 ++++++--
 arch/arm64/mm/fault.c                |  38 ++++--
 arch/powerpc/include/asm/uaccess.h   |   1 +
 arch/x86/include/asm/uaccess.h       |   1 +
 drivers/acpi/apei/ghes.c             |  48 ++++-----
 include/acpi/ghes.h                  |  15 ++-
 include/linux/highmem.h              |  16 ++-
 include/linux/uaccess.h              |   8 ++
 lib/tests/memcpy_kunit.c             | 186 ++++++++++++++++++++++++++-
 mm/kasan/shadow.c                    |  12 ++
 mm/khugepaged.c                      |   4 +-
 29 files changed, 740 insertions(+), 121 deletions(-)
 create mode 100644 arch/arm64/lib/copy_mc_page.S
 create mode 100644 arch/arm64/lib/copy_page_template.S
 create mode 100644 arch/arm64/lib/memcpy_mc.S

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.47.3

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v16 1/9] uaccess: add generic fallback version of copy_mc_to_user()
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-06  1:26   ` sashiko-bot
  2026-12-05  1:24 ` [PATCH v16 2/9] ACPI: APEI: GHES: use exception context to gate SIGBUS on poison consumption Jiangtian Feng
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Mauro Carvalho Chehab,
	Jonathan Cameron, Jiangtian Feng

From: Tong Tiangen <tongtiangen@huawei.com>

x86/powerpc has it's implementation of copy_mc_to_user(), we add generic
fallback in include/linux/uaccess.h prepare for other architechures to
enable CONFIG_ARCH_HAS_COPY_MC.

Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Acked-by: Michael Ellerman <mpe@ellerman.id.au>
Reviewed-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Shuai Xue <xueshuai@linux.alibaba.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 arch/powerpc/include/asm/uaccess.h | 1 +
 arch/x86/include/asm/uaccess.h     | 1 +
 include/linux/uaccess.h            | 8 ++++++++
 3 files changed, 10 insertions(+)

diff --git a/arch/powerpc/include/asm/uaccess.h b/arch/powerpc/include/asm/uaccess.h
index 49039074b33f..be6430a230c1 100644
--- a/arch/powerpc/include/asm/uaccess.h
+++ b/arch/powerpc/include/asm/uaccess.h
@@ -458,6 +458,7 @@ copy_mc_to_user(void __user *to, const void *from, unsigned long n)
 
 	return n;
 }
+#define copy_mc_to_user copy_mc_to_user
 #endif
 
 extern size_t copy_from_user_flushcache(void *dst, const void __user *src, size_t size);
diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index f4700ad6b5ec..7a69d407eb3c 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -442,6 +442,7 @@ copy_mc_to_kernel(void *to, const void *from, unsigned len);
 
 unsigned long __must_check
 copy_mc_to_user(void __user *to, const void *from, unsigned len);
+#define copy_mc_to_user copy_mc_to_user
 #endif
 
 /*
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 67cdb0a4f583..689604cb590c 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -243,6 +243,14 @@ copy_mc_to_kernel(void *dst, const void *src, size_t cnt)
 }
 #endif
 
+#ifndef copy_mc_to_user
+static inline unsigned long __must_check
+copy_mc_to_user(void __user *dst, const void *src, unsigned long cnt)
+{
+	return copy_to_user(dst, src, cnt);
+}
+#endif
+
 static __always_inline void pagefault_disabled_inc(void)
 {
 	current->pagefault_disabled++;
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v16 2/9] ACPI: APEI: GHES: use exception context to gate SIGBUS on poison consumption
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
  2026-12-05  1:24 ` [PATCH v16 1/9] uaccess: add generic fallback version of copy_mc_to_user() Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-06  1:26   ` sashiko-bot
  2026-12-05  1:24 ` [PATCH v16 3/9] arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO into ACCESS_ERR_ZERO Jiangtian Feng
                   ` (6 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jiangtian Feng

From: Ruidong Tian <tianruidong@linux.alibaba.com>

When a GHES SEA (Synchronous External Abort) fires while the CPU
was executing in kernel mode, it typically means that kernel code
itself consumed a poisoned memory location -- e.g. copy_from_user()
/ copy_to_user() invoked from a ioctl() or write() syscall touched
a poisoned user page or page-cache page on behalf of the task.

The expected behaviour in that case is that the faulting kernel
helper returns via its extable fixup and the syscall returns an
error (e.g. -EFAULT) to user space. It is NOT appropriate to deliver
SIGBUS to the current task: the task did not directly dereference
the poisoned address, the kernel did on its behalf, and the kernel
is able to recover.

Up to now ghes_handle_memory_failure() unconditionally promoted any
synchronous recoverable memory error to MF_ACTION_REQUIRED, which
ends up SIGBUS on current -- regardless of whether the poison was
consumed from user space or from inside the kernel on the task's
behalf. That kills tasks that should instead have seen a plain
syscall error.

To fix this, the execution mode in which the exception was taken
must be captured at the arch-level entry point, where pt_regs (and
hence user_mode(regs)) are still available. The estatus node that
later drains the error in IRQ / process context no longer has
access to the original regs.

Introduce:

    enum context { ... };

and plumb the value all the way down to the queued estatus node:

 * Add an 'enum context context' field to struct ghes_estatus_node
   and record it in ghes_in_nmi_queue_one_entry().
 * Extend ghes_notify_sea() and the internal
   ghes_in_nmi_spool_from_list() with an enum context parameter.

Then consume the recorded context in ghes_handle_memory_failure()
and ghes_handle_arm_hw_error() for the synchronous path:

    flags = sync && context != GHES_CTX_KERNEL ? MF_ACTION_REQUIRED : 0;

MF_ACTION_REQUIRED is retained for user-mode and GHES_CTX_NA poison
consumption. The latter includes KVM's apei_claim_sea(NULL) guest-SEA path,
for which sync can still be true. Kernel-mode records use flags=0 because
the context is captured before the arch decides whether a copy fixup is
possible: a successful fixup returns a copy error, while a rejected fixup
continues through the existing fatal SEA path.

Also restrict the existing sync && !queued force-SIGBUS fallback to paths
other than GHES_CTX_KERNEL, preserving the USER and KVM/NA behavior.
Duplicate SEA anti-storm handling still returns -ECANCELED for USER and
KVM/NA, but a duplicate KERNEL record does not bypass the arch fixup path.
Otherwise a second read of an already reported error source could turn a
recoverable kernel copy into a fatal exception.

Qoder, an AI coding assistant, helped adapt the context handling to the
newer queue-aggregation and KVM/NA paths and review the resulting failure
semantics.

Signed-off-by: Ruidong Tian  <tianruidong@linux.alibaba.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 arch/arm64/kernel/acpi.c |  2 +-
 drivers/acpi/apei/ghes.c | 48 ++++++++++++++++++++++------------------
 include/acpi/ghes.h      | 15 +++++++++++--
 3 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/arch/arm64/kernel/acpi.c b/arch/arm64/kernel/acpi.c
index 681aa2bbc399..32be5499ea45 100644
--- a/arch/arm64/kernel/acpi.c
+++ b/arch/arm64/kernel/acpi.c
@@ -409,7 +409,7 @@ int apei_claim_sea(struct pt_regs *regs)
 	 */
 	local_daif_restore(DAIF_ERRCTX);
 	nmi_enter();
-	err = ghes_notify_sea();
+	err = ghes_notify_sea(GHES_CTX(regs));
 	nmi_exit();
 
 	/*
diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
index 5d888c82efea..eb4466aa87f2 100644
--- a/drivers/acpi/apei/ghes.c
+++ b/drivers/acpi/apei/ghes.c
@@ -529,7 +529,7 @@ static bool ghes_do_memory_failure(u64 physical_addr, int flags)
 }
 
 static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata,
-				       int sev, bool sync)
+				       int sev, bool sync, enum ghes_exec_ctx context)
 {
 	int flags = -1;
 	int sec_sev = ghes_severity(gdata->error_severity);
@@ -543,7 +543,7 @@ static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata,
 	    (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
 		flags = MF_SOFT_OFFLINE;
 	if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
-		flags = sync ? MF_ACTION_REQUIRED : 0;
+		flags = sync && context != GHES_CTX_KERNEL ? MF_ACTION_REQUIRED : 0;
 
 	if (flags != -1)
 		return ghes_do_memory_failure(mem_err->physical_addr, flags);
@@ -552,10 +552,10 @@ static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata,
 }
 
 static bool ghes_handle_arm_hw_error(struct acpi_hest_generic_data *gdata,
-				     int sev, bool sync)
+				     int sev, bool sync, enum ghes_exec_ctx context)
 {
 	struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
-	int flags = sync ? MF_ACTION_REQUIRED : 0;
+	int flags = sync && context != GHES_CTX_KERNEL ? MF_ACTION_REQUIRED : 0;
 	int length = gdata->error_data_length;
 	char error_type[120];
 	bool queued = false;
@@ -954,7 +954,8 @@ static u32 ghes_mem_err_min_len(u64 validation_bits)
 }
 
 static void ghes_do_proc(struct ghes *ghes,
-			 const struct acpi_hest_generic_status *estatus)
+			 const struct acpi_hest_generic_status *estatus,
+			 enum ghes_exec_ctx context)
 {
 	int sev, sec_sev;
 	struct acpi_hest_generic_data *gdata;
@@ -1000,11 +1001,11 @@ static void ghes_do_proc(struct ghes *ghes,
 			atomic_notifier_call_chain(&ghes_report_chain, sev, mem_err);
 
 			arch_apei_report_mem_error(sev, mem_err);
-			queued |= ghes_handle_memory_failure(gdata, sev, sync);
+			queued |= ghes_handle_memory_failure(gdata, sev, sync, context);
 		} else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
 			ghes_handle_aer(gdata);
 		} else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
-			queued |= ghes_handle_arm_hw_error(gdata, sev, sync);
+			queued |= ghes_handle_arm_hw_error(gdata, sev, sync, context);
 		} else if (guid_equal(sec_type, &CPER_SEC_CXL_PROT_ERR)) {
 			struct cxl_cper_sec_prot_err *prot_err = acpi_hest_get_payload(gdata);
 
@@ -1037,9 +1038,10 @@ static void ghes_do_proc(struct ghes *ghes,
 
 	/*
 	 * If no memory failure work is queued for abnormal synchronous
-	 * errors, do a force kill.
+	 * errors, force kill for USER and KVM/NA. Kernel records are left
+	 * for the arch SEA/extable path to decide whether recovery is possible.
 	 */
-	if (sync && !queued) {
+	if (sync && !queued && context != GHES_CTX_KERNEL) {
 		dev_err(ghes->dev,
 			HW_ERR GHES_PFX "%s:%d: synchronous unrecoverable error (SIGBUS)\n",
 			current->comm, task_pid_nr(current));
@@ -1257,7 +1259,7 @@ static int ghes_proc(struct ghes *ghes)
 		if (ghes_print_estatus(NULL, ghes->generic, estatus))
 			ghes_estatus_cache_add(ghes->generic, estatus);
 	}
-	ghes_do_proc(ghes, estatus);
+	ghes_do_proc(ghes, estatus, GHES_CTX_NA);
 
 out:
 	ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
@@ -1364,7 +1366,7 @@ static void ghes_proc_in_irq(struct irq_work *irq_work)
 		len = cper_estatus_len(estatus);
 		node_len = GHES_ESTATUS_NODE_LEN(len);
 
-		ghes_do_proc(estatus_node->ghes, estatus);
+		ghes_do_proc(estatus_node->ghes, estatus, estatus_node->context);
 
 		if (!ghes_estatus_cached(estatus)) {
 			generic = estatus_node->generic;
@@ -1402,7 +1404,8 @@ static void ghes_print_queued_estatus(void)
 }
 
 static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
-				       enum fixed_addresses fixmap_idx)
+				       enum fixed_addresses fixmap_idx,
+				       enum ghes_exec_ctx context)
 {
 	struct acpi_hest_generic_status *estatus, tmp_header;
 	struct ghes_estatus_node *estatus_node;
@@ -1431,6 +1434,7 @@ static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
 	if (!estatus_node)
 		return -ENOMEM;
 
+	estatus_node->context = context;
 	estatus_node->ghes = ghes;
 	estatus_node->generic = ghes->generic;
 	estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
@@ -1452,11 +1456,10 @@ static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
 	/* This error has been reported before, don't process it again. */
 	if (ghes_estatus_cached(estatus)) {
 		/*
-		 * Return failure on duplicate SEA entries so that the
-		 * subsequent SEA handler invocation sends a SIGBUS signal to
-		 * the task to prevent it from re-entering the handler loop.
+		 * Kernel copies must still reach their fixup; retain duplicate
+		 * SEA anti-storm handling for user and KVM/NA paths.
 		 */
-		if (is_hest_sync_notify(ghes))
+		if (is_hest_sync_notify(ghes) && context != GHES_CTX_KERNEL)
 			rc = -ECANCELED;
 		goto no_work;
 	}
@@ -1473,14 +1476,15 @@ static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
 }
 
 static int __maybe_unused ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
-			       enum fixed_addresses fixmap_idx)
+			       enum fixed_addresses fixmap_idx,
+			       enum ghes_exec_ctx context)
 {
 	int ret = -ENOENT;
 	struct ghes *ghes;
 
 	rcu_read_lock();
 	list_for_each_entry_rcu(ghes, rcu_list, list) {
-		if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
+		if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx, context))
 			ret = 0;
 	}
 	rcu_read_unlock();
@@ -1563,7 +1567,7 @@ static LIST_HEAD(ghes_sea);
  * Return 0 only if one of the SEA error sources successfully reported an error
  * record sent from the firmware.
  */
-int ghes_notify_sea(void)
+int ghes_notify_sea(enum ghes_exec_ctx context)
 {
 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
 	int rv;
@@ -1572,7 +1576,7 @@ int ghes_notify_sea(void)
 		return -ENOENT;
 
 	raw_spin_lock(&ghes_notify_lock_sea);
-	rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
+	rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA, context);
 	raw_spin_unlock(&ghes_notify_lock_sea);
 
 	return rv;
@@ -1627,7 +1631,7 @@ static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
 		return ret;
 
 	raw_spin_lock(&ghes_notify_lock_nmi);
-	if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
+	if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI, GHES_CTX_NA))
 		ret = NMI_HANDLED;
 	raw_spin_unlock(&ghes_notify_lock_nmi);
 
@@ -1681,7 +1685,7 @@ static void ghes_nmi_init_cxt(void)
 static int __ghes_sdei_callback(struct ghes *ghes,
 				enum fixed_addresses fixmap_idx)
 {
-	if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
+	if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx, GHES_CTX_NA)) {
 		irq_work_queue(&ghes_proc_irq_work);
 
 		return 0;
diff --git a/include/acpi/ghes.h b/include/acpi/ghes.h
index 7acf209061ea..2cd97f700214 100644
--- a/include/acpi/ghes.h
+++ b/include/acpi/ghes.h
@@ -33,10 +33,21 @@ struct ghes {
 	void __iomem *error_status_vaddr;
 };
 
+enum ghes_exec_ctx {
+	GHES_CTX_NA = -1,
+	GHES_CTX_KERNEL = 0,
+	GHES_CTX_USER = 1
+};
+
+#define GHES_CTX(regs)	((regs) ? (user_mode(regs) ? GHES_CTX_USER \
+						   : GHES_CTX_KERNEL) \
+				: GHES_CTX_NA)
+
 struct ghes_estatus_node {
 	struct llist_node llnode;
 	struct acpi_hest_generic *generic;
 	struct ghes *ghes;
+	enum ghes_exec_ctx context;
 };
 
 struct ghes_estatus_cache {
@@ -139,9 +150,9 @@ static inline void *acpi_hest_get_next(struct acpi_hest_generic_data *gdata)
 	     section = acpi_hest_get_next(section))
 
 #ifdef CONFIG_ACPI_APEI_SEA
-int ghes_notify_sea(void);
+int ghes_notify_sea(enum ghes_exec_ctx context);
 #else
-static inline int ghes_notify_sea(void) { return -ENOENT; }
+static inline int ghes_notify_sea(enum ghes_exec_ctx context) { return -ENOENT; }
 #endif
 
 struct notifier_block;
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v16 3/9] arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO into ACCESS_ERR_ZERO
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
  2026-12-05  1:24 ` [PATCH v16 1/9] uaccess: add generic fallback version of copy_mc_to_user() Jiangtian Feng
  2026-12-05  1:24 ` [PATCH v16 2/9] ACPI: APEI: GHES: use exception context to gate SIGBUS on poison consumption Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-06  1:26   ` sashiko-bot
  2026-12-05  1:24 ` [PATCH v16 4/9] arm64: enable recover from synchronous external abort in kernel context Jiangtian Feng
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jiangtian Feng

From: Ruidong Tian <tianruidong@linux.alibaba.com>

EX_TYPE_UACCESS_ERR_ZERO and EX_TYPE_KACCESS_ERR_ZERO have identical
handling in fixup_exception(): both unconditionally invoke
ex_handler_uaccess_err_zero() to set the error register to -EFAULT,
zero the destination register, and branch to the fixup address.

Merge them into a single EX_TYPE_ACCESS_ERR_ZERO to reduce redundancy
and renumber the subsequent types accordingly.

The _ASM_EXTABLE_UACCESS_ERR_ZERO and _ASM_EXTABLE_KACCESS_ERR_ZERO
helper macros are preserved as-is for caller readability, but both now
emit the unified EX_TYPE_ACCESS_ERR_ZERO type.

Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 arch/arm64/include/asm/asm-extable.h | 15 +++++++--------
 arch/arm64/mm/extable.c              |  7 +++----
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/arch/arm64/include/asm/asm-extable.h b/arch/arm64/include/asm/asm-extable.h
index d67e2fdd1aee..06b19023939b 100644
--- a/arch/arm64/include/asm/asm-extable.h
+++ b/arch/arm64/include/asm/asm-extable.h
@@ -7,12 +7,11 @@
 
 #define EX_TYPE_NONE			0
 #define EX_TYPE_BPF			1
-#define EX_TYPE_UACCESS_ERR_ZERO	2
-#define EX_TYPE_KACCESS_ERR_ZERO	3
-#define EX_TYPE_UACCESS_CPY		4
-#define EX_TYPE_LOAD_UNALIGNED_ZEROPAD	5
+#define EX_TYPE_ACCESS_ERR_ZERO		2
+#define EX_TYPE_UACCESS_CPY		3
+#define EX_TYPE_LOAD_UNALIGNED_ZEROPAD	4
 
-/* Data fields for EX_TYPE_UACCESS_ERR_ZERO */
+/* Data fields for EX_TYPE_ACCESS_ERR_ZERO */
 #define EX_DATA_REG_ERR_SHIFT	0
 #define EX_DATA_REG_ERR		GENMASK(4, 0)
 #define EX_DATA_REG_ZERO_SHIFT	5
@@ -43,7 +42,7 @@
 
 #define _ASM_EXTABLE_UACCESS_ERR_ZERO(insn, fixup, err, zero)		\
 	__ASM_EXTABLE_RAW(insn, fixup, 					\
-			  EX_TYPE_UACCESS_ERR_ZERO,			\
+			  EX_TYPE_ACCESS_ERR_ZERO,			\
 			  (						\
 			    EX_DATA_REG(ERR, err) |			\
 			    EX_DATA_REG(ZERO, zero)			\
@@ -96,7 +95,7 @@
 #define _ASM_EXTABLE_UACCESS_ERR_ZERO(insn, fixup, err, zero)		\
 	__DEFINE_ASM_GPR_NUMS						\
 	__ASM_EXTABLE_RAW(#insn, #fixup, 				\
-			  __stringify(EX_TYPE_UACCESS_ERR_ZERO),	\
+			  __stringify(EX_TYPE_ACCESS_ERR_ZERO),	\
 			  "("						\
 			    EX_DATA_REG(ERR, err) " | "			\
 			    EX_DATA_REG(ZERO, zero)			\
@@ -105,7 +104,7 @@
 #define _ASM_EXTABLE_KACCESS_ERR_ZERO(insn, fixup, err, zero)		\
 	__DEFINE_ASM_GPR_NUMS						\
 	__ASM_EXTABLE_RAW(#insn, #fixup, 				\
-			  __stringify(EX_TYPE_KACCESS_ERR_ZERO),	\
+			  __stringify(EX_TYPE_ACCESS_ERR_ZERO),	\
 			  "("						\
 			    EX_DATA_REG(ERR, err) " | "			\
 			    EX_DATA_REG(ZERO, zero)			\
diff --git a/arch/arm64/mm/extable.c b/arch/arm64/mm/extable.c
index 299a244f68e6..402c5db692ff 100644
--- a/arch/arm64/mm/extable.c
+++ b/arch/arm64/mm/extable.c
@@ -41,7 +41,7 @@ get_ex_fixup(const struct exception_table_entry *ex)
 	return ((unsigned long)&ex->fixup + ex->fixup);
 }
 
-static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
+static bool ex_handler_access_err_zero(const struct exception_table_entry *ex,
 					struct pt_regs *regs)
 {
 	int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data);
@@ -98,9 +98,8 @@ bool fixup_exception(struct pt_regs *regs, unsigned long esr)
 	switch (ex->type) {
 	case EX_TYPE_BPF:
 		return ex_handler_bpf(ex, regs);
-	case EX_TYPE_UACCESS_ERR_ZERO:
-	case EX_TYPE_KACCESS_ERR_ZERO:
-		return ex_handler_uaccess_err_zero(ex, regs);
+	case EX_TYPE_ACCESS_ERR_ZERO:
+		return ex_handler_access_err_zero(ex, regs);
 	case EX_TYPE_UACCESS_CPY:
 		return ex_handler_uaccess_cpy(ex, regs, esr);
 	case EX_TYPE_LOAD_UNALIGNED_ZEROPAD:
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v16 4/9] arm64: enable recover from synchronous external abort in kernel context
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
                   ` (2 preceding siblings ...)
  2026-12-05  1:24 ` [PATCH v16 3/9] arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO into ACCESS_ERR_ZERO Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-06  1:26   ` sashiko-bot
  2026-12-05  1:24 ` [PATCH v16 5/9] mm/hwpoison: return -EFAULT when copy fail in copy_mc_[user]_highpage() Jiangtian Feng
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jiangtian Feng

From: Ruidong Tian <tianruidong@linux.alibaba.com>

For the arm64 kernel, when it processes hardware memory errors for
synchronize notifications(do_sea()), if the errors is consumed within the
kernel, the current processing is panic. However, it is not optimal.

Take copy_from/to_user for example, If ld* triggers a memory error, even in
kernel mode, only the associated process is affected. Killing the user
process and isolating the corrupt page is a better choice.

Add new fixup type EX_TYPE_KACCESS_SEA to identify insn that can recover
from memory errors triggered by access to kernel memory, and this fixup
type is used in __arch_copy_to_user(), This make the regular copy_to_user()
will handle kernel memory errors.

[Ruidong: modify subject and rename EX_TYPE_KACCESS_ERR_ZERO_MEM_ERR to
EX_TYPE_KACCESS_SEA]

Also reject EX_TYPE_KACCESS_SEA in insn_may_access_user(). These entries
mark kernel source accesses, not user accesses. Treating them as uaccess
could route a TTBR0/PAN fault through user fault handling instead of
preserving the kernel-access checks.

A successful GHES claim is not sufficient to skip a kernel instruction.
Only an exact EL1 data abort with FSC_EXTABT, no S1PTW and SET=0 is
eligible for an explicit SEA fixup. SET=0 means UER on RAS systems and is
RES0 on non-RAS systems; UC, UEO and other encodings are rejected. UEO
support is left for a separate RAS-version-aware change with
fault-injection coverage. Instruction aborts, table-walk/parity errors and
faults without a suitable handler continue through the existing failure
path. Do not use the SError-only AET encoding for this decision.

Pass the real ESR to fixup_exception_me() and handle EX_TYPE_UACCESS_CPY
with the existing direction-aware handler. The SEA-only path additionally
accepts copy_to_user's kernel source read, matching its scalar path;
copy_from_user's kernel destination and all ordinary translation faults
remain unchanged. MOPS usercopy faults bypass the single-byte try-harder
path so a zero-progress fault cannot repeat the same access.

This bypass applies to ordinary MOPS faults as well: a zero-progress fault
may return the full residual instead of first retrying one byte. This is an
intentional conservative change to avoid repeating an SEA. Scalar faults
retain the existing try-harder behavior.

EX_TYPE_KACCESS_SEA accepts source reads only. In particular, a MOPS
destination write is not implicitly recovered by annotating the whole copy
instruction.

Qoder, an AI coding assistant, helped review the extable type usage and
TTBR0/PAN negative path, restrict the recoverable SEA/SET conditions, and
review the MOPS uaccess direction and repeated-fault paths.

Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 arch/arm64/include/asm/asm-extable.h |  5 +++
 arch/arm64/include/asm/asm-uaccess.h |  4 +++
 arch/arm64/include/asm/extable.h     |  1 +
 arch/arm64/lib/copy_from_user.S      |  6 ++--
 arch/arm64/lib/copy_to_user.S        | 16 +++++-----
 arch/arm64/mm/extable.c              | 47 +++++++++++++++++++++++++---
 arch/arm64/mm/fault.c                | 38 +++++++++++++++++-----
 7 files changed, 96 insertions(+), 21 deletions(-)

diff --git a/arch/arm64/include/asm/asm-extable.h b/arch/arm64/include/asm/asm-extable.h
index 06b19023939b..8450ec5a3af6 100644
--- a/arch/arm64/include/asm/asm-extable.h
+++ b/arch/arm64/include/asm/asm-extable.h
@@ -10,6 +10,7 @@
 #define EX_TYPE_ACCESS_ERR_ZERO		2
 #define EX_TYPE_UACCESS_CPY		3
 #define EX_TYPE_LOAD_UNALIGNED_ZEROPAD	4
+#define EX_TYPE_KACCESS_SEA		5
 
 /* Data fields for EX_TYPE_ACCESS_ERR_ZERO */
 #define EX_DATA_REG_ERR_SHIFT	0
@@ -76,6 +77,10 @@
 	__ASM_EXTABLE_RAW(\insn, \fixup, EX_TYPE_UACCESS_CPY, \uaccess_is_write)
 	.endm
 
+	.macro          _asm_extable_kaccess_sea, insn, fixup
+	__ASM_EXTABLE_RAW(\insn, \fixup, EX_TYPE_KACCESS_SEA, 0)
+	.endm
+
 #else /* __ASSEMBLER__ */
 
 #include <linux/stringify.h>
diff --git a/arch/arm64/include/asm/asm-uaccess.h b/arch/arm64/include/asm/asm-uaccess.h
index 12aa6a283249..27bf8edbf597 100644
--- a/arch/arm64/include/asm/asm-uaccess.h
+++ b/arch/arm64/include/asm/asm-uaccess.h
@@ -57,6 +57,10 @@ alternative_else_nop_endif
 	.endm
 #endif
 
+#define KERNEL_SEA(l, x...)			\
+9999:	x;					\
+	_asm_extable_kaccess_sea	9999b, l
+
 #define USER(l, x...)				\
 9999:	x;					\
 	_asm_extable_uaccess	9999b, l
diff --git a/arch/arm64/include/asm/extable.h b/arch/arm64/include/asm/extable.h
index 9dc39612bdf5..87fba582b5a7 100644
--- a/arch/arm64/include/asm/extable.h
+++ b/arch/arm64/include/asm/extable.h
@@ -48,4 +48,5 @@ bool ex_handler_bpf(const struct exception_table_entry *ex,
 #endif /* !CONFIG_BPF_JIT */
 
 bool fixup_exception(struct pt_regs *regs, unsigned long esr);
+bool fixup_exception_me(struct pt_regs *regs, unsigned long esr);
 #endif
diff --git a/arch/arm64/lib/copy_from_user.S b/arch/arm64/lib/copy_from_user.S
index 400057d607ec..04f152ff5b03 100644
--- a/arch/arm64/lib/copy_from_user.S
+++ b/arch/arm64/lib/copy_from_user.S
@@ -54,7 +54,7 @@
 
 	.macro cpy1 dst, src, count
 	.arch_extension mops
-	USER_CPY(9997f, 0, cpyfprt [\dst]!, [\src]!, \count!)
+	USER_CPY(9998f, 0, cpyfprt [\dst]!, [\src]!, \count!)
 	USER_CPY(9996f, 0, cpyfmrt [\dst]!, [\src]!, \count!)
 	USER_CPY(9996f, 0, cpyfert [\dst]!, [\src]!, \count!)
 	.endm
@@ -69,9 +69,11 @@ SYM_FUNC_START(__arch_copy_from_user)
 	ret
 
 	// Exception fixups
-9996:	b.cs	9997f
+	// MOPS faults skip the zero-progress retry to avoid repeating an SEA.
+9996:	b.cs	9998f
 	// Registers are in Option A format
 	add	dst, dst, count
+	b	9998f
 9997:	cmp	dst, dstin
 	b.ne	9998f
 	// Before being absolutely sure we couldn't copy anything, try harder
diff --git a/arch/arm64/lib/copy_to_user.S b/arch/arm64/lib/copy_to_user.S
index 819f2e3fc7a9..451bfeb0143b 100644
--- a/arch/arm64/lib/copy_to_user.S
+++ b/arch/arm64/lib/copy_to_user.S
@@ -20,7 +20,7 @@
  *	x0 - bytes not copied
  */
 	.macro ldrb1 reg, ptr, val
-	ldrb  \reg, [\ptr], \val
+	KERNEL_SEA(9998f, ldrb  \reg, [\ptr], \val)
 	.endm
 
 	.macro strb1 reg, ptr, val
@@ -28,7 +28,7 @@
 	.endm
 
 	.macro ldrh1 reg, ptr, val
-	ldrh  \reg, [\ptr], \val
+	KERNEL_SEA(9998f, ldrh  \reg, [\ptr], \val)
 	.endm
 
 	.macro strh1 reg, ptr, val
@@ -36,7 +36,7 @@
 	.endm
 
 	.macro ldr1 reg, ptr, val
-	ldr \reg, [\ptr], \val
+	KERNEL_SEA(9998f, ldr \reg, [\ptr], \val)
 	.endm
 
 	.macro str1 reg, ptr, val
@@ -44,7 +44,7 @@
 	.endm
 
 	.macro ldp1 reg1, reg2, ptr, val
-	ldp \reg1, \reg2, [\ptr], \val
+	KERNEL_SEA(9998f, ldp \reg1, \reg2, [\ptr], \val)
 	.endm
 
 	.macro stp1 reg1, reg2, ptr, val
@@ -53,7 +53,7 @@
 
 	.macro cpy1 dst, src, count
 	.arch_extension mops
-	USER_CPY(9997f, 1, cpyfpwt [\dst]!, [\src]!, \count!)
+	USER_CPY(9998f, 1, cpyfpwt [\dst]!, [\src]!, \count!)
 	USER_CPY(9996f, 1, cpyfmwt [\dst]!, [\src]!, \count!)
 	USER_CPY(9996f, 1, cpyfewt [\dst]!, [\src]!, \count!)
 	.endm
@@ -68,13 +68,15 @@ SYM_FUNC_START(__arch_copy_to_user)
 	ret
 
 	// Exception fixups
-9996:	b.cs	9997f
+	// MOPS faults skip the zero-progress retry to avoid repeating an SEA.
+9996:	b.cs	9998f
 	// Registers are in Option A format
 	add	dst, dst, count
+	b	9998f
 9997:	cmp	dst, dstin
 	b.ne	9998f
 	// Before being absolutely sure we couldn't copy anything, try harder
-	ldrb	tmp1w, [srcin]
+KERNEL_SEA(9998f, ldrb	tmp1w, [srcin])
 USER(9998f, sttrb tmp1w, [dst])
 	add	dst, dst, #1
 9998:	sub	x0, end, dst			// bytes not copied
diff --git a/arch/arm64/mm/extable.c b/arch/arm64/mm/extable.c
index 402c5db692ff..5ea013a3247c 100644
--- a/arch/arm64/mm/extable.c
+++ b/arch/arm64/mm/extable.c
@@ -30,6 +30,9 @@ bool insn_may_access_user(unsigned long addr, unsigned long esr)
 	switch (ex->type) {
 	case EX_TYPE_UACCESS_CPY:
 		return cpy_faulted_on_uaccess(ex, esr);
+	case EX_TYPE_KACCESS_SEA:
+		/* These entries mark kernel source accesses, not uaccess. */
+		return false;
 	default:
 		return true;
 	}
@@ -55,10 +58,14 @@ static bool ex_handler_access_err_zero(const struct exception_table_entry *ex,
 }
 
 static bool ex_handler_uaccess_cpy(const struct exception_table_entry *ex,
-				   struct pt_regs *regs, unsigned long esr)
+				   struct pt_regs *regs, unsigned long esr,
+				   bool allow_kernel_source)
 {
-	/* Do not fix up faults on kernel memory accesses */
-	if (!cpy_faulted_on_uaccess(ex, esr))
+	/* Only SEA may also accept copy_to_user's kernel source read. */
+	if (!cpy_faulted_on_uaccess(ex, esr) &&
+	    !(allow_kernel_source &&
+	      FIELD_GET(EX_DATA_UACCESS_WRITE, ex->data) &&
+	      !(esr & ESR_ELx_WNR)))
 		return false;
 
 	regs->pc = get_ex_fixup(ex);
@@ -101,10 +108,42 @@ bool fixup_exception(struct pt_regs *regs, unsigned long esr)
 	case EX_TYPE_ACCESS_ERR_ZERO:
 		return ex_handler_access_err_zero(ex, regs);
 	case EX_TYPE_UACCESS_CPY:
-		return ex_handler_uaccess_cpy(ex, regs, esr);
+		return ex_handler_uaccess_cpy(ex, regs, esr, false);
 	case EX_TYPE_LOAD_UNALIGNED_ZEROPAD:
 		return ex_handler_load_unaligned_zeropad(ex, regs);
+	/*
+	 * Kernel address faults (e.g. copy_to_user reading from kernel src).
+	 * Do not fixup here: a translation fault on a kernel address is a
+	 * kernel bug (e.g. NULL pointer dereference) and must oops.
+	 * Only SEA (hardware memory error) should be fixed up, which is
+	 * handled by fixup_exception_me() through the do_sea path.
+	 */
+	case EX_TYPE_KACCESS_SEA:
+		return false;
 	}
 
 	BUG();
 }
+
+bool fixup_exception_me(struct pt_regs *regs, unsigned long esr)
+{
+	const struct exception_table_entry *ex;
+
+	ex = search_exception_tables(instruction_pointer(regs));
+	if (!ex)
+		return false;
+
+	switch (ex->type) {
+	case EX_TYPE_ACCESS_ERR_ZERO:
+		return ex_handler_access_err_zero(ex, regs);
+	case EX_TYPE_UACCESS_CPY:
+		return ex_handler_uaccess_cpy(ex, regs, esr, true);
+	case EX_TYPE_KACCESS_SEA:
+		if (esr & ESR_ELx_WNR)
+			return false;
+		regs->pc = get_ex_fixup(ex);
+		return true;
+	}
+
+	return false;
+}
diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index b22736853fcd..d8ff65c86728 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -874,21 +874,43 @@ static int do_bad(unsigned long far, unsigned long esr, struct pt_regs *regs)
 	return 1; /* "fault" */
 }
 
+/*
+ * APEI claimed this as a firmware-first notification.
+ * Some processing deferred to task_work before ret_to_user().
+ */
+static int do_apei_claim_sea(struct pt_regs *regs, unsigned long esr)
+{
+	int ret;
+
+	ret = apei_claim_sea(regs);
+	if (ret)
+		return ret;
+
+	if (user_mode(regs))
+		return 0;
+
+	/* A GHES claim alone does not make a kernel exception safe to skip. */
+	if (!is_el1_data_abort(esr) ||
+	    (esr & ESR_ELx_FSC) != ESR_ELx_FSC_EXTABT ||
+	    (esr & ESR_ELx_S1PTW) ||
+	    (esr & ESR_ELx_SET_MASK))
+		return -ENOENT;
+
+	if (!fixup_exception_me(regs, esr))
+		return -ENOENT;
+
+	return 0;
+}
+
 static int do_sea(unsigned long far, unsigned long esr, struct pt_regs *regs)
 {
 	const struct fault_info *inf;
 	unsigned long siaddr;
 
-	inf = esr_to_fault_info(esr);
-
-	if (user_mode(regs) && apei_claim_sea(regs) == 0) {
-		/*
-		 * APEI claimed this as a firmware-first notification.
-		 * Some processing deferred to task_work before ret_to_user().
-		 */
+	if (do_apei_claim_sea(regs, esr) == 0)
 		return 0;
-	}
 
+	inf = esr_to_fault_info(esr);
 	if (esr & ESR_ELx_FnV) {
 		siaddr = 0;
 	} else {
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v16 5/9] mm/hwpoison: return -EFAULT when copy fail in copy_mc_[user]_highpage()
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
                   ` (3 preceding siblings ...)
  2026-12-05  1:24 ` [PATCH v16 4/9] arm64: enable recover from synchronous external abort in kernel context Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-05  1:24 ` [PATCH v16 6/9] arm64: support copy_mc_[user]_highpage() Jiangtian Feng
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jonathan Cameron,
	Mauro Carvalho Chehab, Jiangtian Feng

From: Tong Tiangen <tongtiangen@huawei.com>

Currently, copy_mc_[user]_highpage() returns zero on success, or in case
of failures, the number of bytes that weren't copied.

While tracking the number of not copied works fine for x86 and PPC, There
are some difficulties in doing the same thing on ARM64 because there is no
available caller-saved register in copy_page()(lib/copy_page.S) to save
"bytes not copied", and the following copy_mc_page() will also encounter
the same problem.

Consider the caller of copy_mc_[user]_highpage() cannot do any processing
on the remaining data(The page has hardware errors), they only check if
copy was succeeded or not, make the interface more generic by using an
error code when copy fails (-EFAULT) or return zero on success.

Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Reviewed-by: Shuai Xue <xueshuai@linux.alibaba.com>
Reviewed-by: Ashok Raj <ashok.raj@oss.qualcomm.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 include/linux/highmem.h | 8 ++++----
 mm/khugepaged.c         | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index d7aac9de1c8a..685c62c10408 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -428,8 +428,8 @@ static inline void copy_highpage(struct page *to, struct page *from)
 /*
  * If architecture supports machine check exception handling, define the
  * #MC versions of copy_user_highpage and copy_highpage. They copy a memory
- * page with #MC in source page (@from) handled, and return the number
- * of bytes not copied if there was a #MC, otherwise 0 for success.
+ * page with #MC in source page (@from) handled, and return -EFAULT if there
+ * was a #MC, otherwise 0 for success.
  */
 static inline int copy_mc_user_highpage(struct page *to, struct page *from,
 					unsigned long vaddr, struct vm_area_struct *vma)
@@ -448,7 +448,7 @@ static inline int copy_mc_user_highpage(struct page *to, struct page *from,
 	if (ret)
 		memory_failure_queue(page_to_pfn(from), 0);
 
-	return ret;
+	return ret ? -EFAULT : 0;
 }
 
 static inline int copy_mc_highpage(struct page *to, struct page *from)
@@ -467,7 +467,7 @@ static inline int copy_mc_highpage(struct page *to, struct page *from)
 	if (ret)
 		memory_failure_queue(page_to_pfn(from), 0);
 
-	return ret;
+	return ret ? -EFAULT : 0;
 }
 #else
 static inline int copy_mc_user_highpage(struct page *to, struct page *from,
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 964f78c3856b..4ceb440e242c 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -874,7 +874,7 @@ static enum scan_result __collapse_huge_page_copy(pte_t *pte, struct folio *foli
 			continue;
 		}
 		src_page = pte_page(pteval);
-		if (copy_mc_user_highpage(page, src_page, src_addr, vma) > 0) {
+		if (copy_mc_user_highpage(page, src_page, src_addr, vma)) {
 			result = SCAN_COPY_MC;
 			break;
 		}
@@ -2515,7 +2515,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 		}
 
 		for (i = 0; i < nr_pages; i++) {
-			if (copy_mc_highpage(dst, folio_page(folio, i)) > 0) {
+			if (copy_mc_highpage(dst, folio_page(folio, i))) {
 				result = SCAN_COPY_MC;
 				goto rollback;
 			}
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v16 6/9] arm64: support copy_mc_[user]_highpage()
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
                   ` (4 preceding siblings ...)
  2026-12-05  1:24 ` [PATCH v16 5/9] mm/hwpoison: return -EFAULT when copy fail in copy_mc_[user]_highpage() Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-06  1:26   ` sashiko-bot
  2026-12-05  1:24 ` [PATCH v16 7/9] arm64: introduce copy_mc_to_kernel() implementation Jiangtian Feng
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jiangtian Feng

From: Tong Tiangen <tongtiangen@huawei.com>

Currently, many scenarios that can tolerate memory errors when copying page
have been supported in the kernel[1~9], all of which are implemented by
copy_mc_[user]_highpage(). arm64 should also support this mechanism.

Due to mte, arm64 needs to have its own copy_mc_[user]_highpage()
architecture implementation, macros __HAVE_ARCH_COPY_MC_HIGHPAGE and
__HAVE_ARCH_COPY_MC_USER_HIGHPAGE have been added to control it.

Add new helper copy_mc_page() which provide a page copy implementation with
hardware memory error safe. The code logic of copy_mc_page() is the same as
copy_page(), and the main logic is extracted to copy_page_template.S. Both
the ldp and MOPS source operations in the MC variant carry
EX_TYPE_KACCESS_SEA fixups and return -EFAULT when a recoverable SEA is
consumed.

[Ruidong: add FEAT_MOPS support]

[1] commit d302c2398ba2 ("mm, hwpoison: when copy-on-write hits poison, take page offline")
[2] commit 1cb9dc4b475c ("mm: hwpoison: support recovery from HugePage copy-on-write faults")
[3] commit 6b970599e807 ("mm: hwpoison: support recovery from ksm_might_need_to_copy()")
[4] commit 98c76c9f1ef7 ("mm/khugepaged: recover from poisoned anonymous memory")
[5] commit 12904d953364 ("mm/khugepaged: recover from poisoned file-backed memory")
[6] commit 658be46520ce ("mm: support poison recovery from copy_present_page()")
[7] commit aa549f923f5e ("mm: support poison recovery from do_cow_fault()")
[8] commit f00b295b9b61 ("fs: hugetlbfs: support poisoned recover from hugetlbfs_migrate_folio()")
[9] commit 060913999d7a ("mm: migrate: support poisoned recover from migrate folio")

Select ARCH_HAS_COPY_MC when ACPI_APEI_SEA is enabled, since the recovery
relies on the in-kernel SEA claim path rather than merely on GHES.

Use the existing dstin register alias for the MOPS template invocation.

When MTE tag copying fails, current callers do not publish the destination
page and eventually free it. The existing buddy and hugetlb free paths clear
the tag/lock state, so a tag-only rollback would be insufficient and is not
attempted here.

Match copy_highpage() when the tagging helpers report an already-tagged
destination. Folio migration can legitimately reuse such a page, so the MC
path must not emit a spurious WARN for that case.

Queue the actual source page with flags=0 on each data or tag-copy failure,
matching the generic highpage fallback. A hugetlb tag failure queues the
failing subpage rather than always using the head page. GHES may already
have queued the same PFN; supported RAM callers rely on MM's already-poisoned
handling to avoid a second substantive isolation. Duplicate FIFO entries,
trace events and logs are still possible. This is not a strict exactly-once
or final-recovery guarantee and does not establish dev_pagemap idempotency.

Qoder, an AI coding assistant, helped analyze the MOPS/MTE review feedback,
the destination-page lifecycle, source-page recovery queueing and the
7.4-rc5 integration, and drafted the corresponding description updates.

Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 arch/arm64/Kconfig                   |  1 +
 arch/arm64/include/asm/asm-extable.h |  1 +
 arch/arm64/include/asm/mte.h         |  9 +++
 arch/arm64/include/asm/page.h        | 12 ++++
 arch/arm64/lib/Makefile              |  2 +
 arch/arm64/lib/copy_mc_page.S        | 43 ++++++++++++++
 arch/arm64/lib/copy_page.S           | 67 ++++------------------
 arch/arm64/lib/copy_page_template.S  | 70 +++++++++++++++++++++++
 arch/arm64/lib/mte.S                 | 29 ++++++++++
 arch/arm64/mm/copypage.c             | 85 ++++++++++++++++++++++++++++
 include/linux/highmem.h              |  8 +++
 11 files changed, 271 insertions(+), 56 deletions(-)
 create mode 100644 arch/arm64/lib/copy_mc_page.S
 create mode 100644 arch/arm64/lib/copy_page_template.S

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 9b08b2457a55..0c017bdc7759 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -21,6 +21,7 @@ config ARM64
 	select ARCH_HAS_CACHE_LINE_SIZE
 	select ARCH_HAS_CC_PLATFORM
 	select ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
+	select ARCH_HAS_COPY_MC if ACPI_APEI_SEA
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DEBUG_VIRTUAL
 	select ARCH_HAS_DEBUG_VM_PGTABLE
diff --git a/arch/arm64/include/asm/asm-extable.h b/arch/arm64/include/asm/asm-extable.h
index 8450ec5a3af6..cf4af0a1e8cd 100644
--- a/arch/arm64/include/asm/asm-extable.h
+++ b/arch/arm64/include/asm/asm-extable.h
@@ -10,6 +10,7 @@
 #define EX_TYPE_ACCESS_ERR_ZERO		2
 #define EX_TYPE_UACCESS_CPY		3
 #define EX_TYPE_LOAD_UNALIGNED_ZEROPAD	4
+/* Kernel source reads eligible for SEA recovery, including MOPS copies. */
 #define EX_TYPE_KACCESS_SEA		5
 
 /* Data fields for EX_TYPE_ACCESS_ERR_ZERO */
diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h
index ff3d78329be0..4ed248a388aa 100644
--- a/arch/arm64/include/asm/mte.h
+++ b/arch/arm64/include/asm/mte.h
@@ -97,6 +97,11 @@ void mte_zero_clear_page_tags(void *addr);
 void mte_clear_folio_tags(struct folio *folio);
 void mte_sync_tags(pte_t pte, unsigned int nr_pages);
 void mte_copy_page_tags(void *kto, const void *kfrom);
+
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+int mte_copy_mc_page_tags(void *kto, const void *kfrom);
+#endif
+
 void mte_thread_init_user(void);
 void mte_thread_switch(struct task_struct *next);
 void mte_cpu_setup(void);
@@ -136,6 +141,10 @@ static inline void mte_sync_tags(pte_t pte, unsigned int nr_pages)
 static inline void mte_copy_page_tags(void *kto, const void *kfrom)
 {
 }
+static inline int mte_copy_mc_page_tags(void *kto, const void *kfrom)
+{
+	return 0;
+}
 static inline void mte_thread_init_user(void)
 {
 }
diff --git a/arch/arm64/include/asm/page.h b/arch/arm64/include/asm/page.h
index b2eaa63d9056..c74292da41a3 100644
--- a/arch/arm64/include/asm/page.h
+++ b/arch/arm64/include/asm/page.h
@@ -40,6 +40,18 @@ void copy_user_highpage(struct page *to, struct page *from,
 void copy_highpage(struct page *to, struct page *from);
 #define __HAVE_ARCH_COPY_HIGHPAGE
 
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+int copy_mc_page(void *to, const void *from);
+#define __HAVE_ARCH_COPY_MC_PAGE
+
+int copy_mc_highpage(struct page *to, struct page *from);
+#define __HAVE_ARCH_COPY_MC_HIGHPAGE
+
+int copy_mc_user_highpage(struct page *to, struct page *from,
+		unsigned long vaddr, struct vm_area_struct *vma);
+#define __HAVE_ARCH_COPY_MC_USER_HIGHPAGE
+#endif
+
 struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 						unsigned long vaddr);
 #define vma_alloc_zeroed_movable_folio vma_alloc_zeroed_movable_folio
diff --git a/arch/arm64/lib/Makefile b/arch/arm64/lib/Makefile
index 38ffd359123c..f7952eb7fb8c 100644
--- a/arch/arm64/lib/Makefile
+++ b/arch/arm64/lib/Makefile
@@ -11,6 +11,8 @@ lib-y		:= clear_user.o delay.o copy_from_user.o		\
 
 lib-$(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) += uaccess_flushcache.o
 
+lib-$(CONFIG_ARCH_HAS_COPY_MC) += copy_mc_page.o
+
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
 
 obj-$(CONFIG_ARM64_MTE) += mte.o
diff --git a/arch/arm64/lib/copy_mc_page.S b/arch/arm64/lib/copy_mc_page.S
new file mode 100644
index 000000000000..542a083e2fd8
--- /dev/null
+++ b/arch/arm64/lib/copy_mc_page.S
@@ -0,0 +1,43 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <linux/linkage.h>
+#include <linux/const.h>
+#include <asm/assembler.h>
+#include <asm/page.h>
+#include <asm/cpufeature.h>
+#include <asm/alternative.h>
+#include <asm/asm-extable.h>
+#include <asm/asm-uaccess.h>
+
+/*
+ * Copy a page from src to dest (both are page aligned) with memory error safe
+ *
+ * Parameters:
+ *	x0 - dest
+ *	x1 - src
+ * Returns:
+ *	x0 - Return 0 on success, or -EFAULT for a supported source SEA.
+ */
+	.macro ldp1 reg1, reg2, ptr, val
+	KERNEL_SEA(9998f, ldp \reg1, \reg2, [\ptr, \val])
+	.endm
+
+	.macro cpy1 dst, src, count
+	.arch_extension mops
+	KERNEL_SEA(9998f, cpypwn [\dst]!, [\src]!, \count!)
+	KERNEL_SEA(9998f, cpymwn [\dst]!, [\src]!, \count!)
+	KERNEL_SEA(9998f, cpyewn [\dst]!, [\src]!, \count!)
+	.endm
+
+SYM_FUNC_START(__pi_copy_mc_page)
+#include "copy_page_template.S"
+
+	mov x0, #0
+	ret
+
+9998:	mov x0, #-EFAULT
+	ret
+
+SYM_FUNC_END(__pi_copy_mc_page)
+SYM_FUNC_ALIAS(copy_mc_page, __pi_copy_mc_page)
+EXPORT_SYMBOL(copy_mc_page)
diff --git a/arch/arm64/lib/copy_page.S b/arch/arm64/lib/copy_page.S
index e6374e7e5511..e520777b5150 100644
--- a/arch/arm64/lib/copy_page.S
+++ b/arch/arm64/lib/copy_page.S
@@ -17,65 +17,20 @@
  *	x0 - dest
  *	x1 - src
  */
-SYM_FUNC_START(__pi_copy_page)
-#ifdef CONFIG_AS_HAS_MOPS
-	.arch_extension mops
-alternative_if_not ARM64_HAS_MOPS
-	b	.Lno_mops
-alternative_else_nop_endif
-
-	mov	x2, #PAGE_SIZE
-	cpypwn	[x0]!, [x1]!, x2!
-	cpymwn	[x0]!, [x1]!, x2!
-	cpyewn	[x0]!, [x1]!, x2!
-	ret
-.Lno_mops:
-#endif
-	ldp	x2, x3, [x1]
-	ldp	x4, x5, [x1, #16]
-	ldp	x6, x7, [x1, #32]
-	ldp	x8, x9, [x1, #48]
-	ldp	x10, x11, [x1, #64]
-	ldp	x12, x13, [x1, #80]
-	ldp	x14, x15, [x1, #96]
-	ldp	x16, x17, [x1, #112]
-
-	add	x0, x0, #256
-	add	x1, x1, #128
-1:
-	tst	x0, #(PAGE_SIZE - 1)
 
-	stnp	x2, x3, [x0, #-256]
-	ldp	x2, x3, [x1]
-	stnp	x4, x5, [x0, #16 - 256]
-	ldp	x4, x5, [x1, #16]
-	stnp	x6, x7, [x0, #32 - 256]
-	ldp	x6, x7, [x1, #32]
-	stnp	x8, x9, [x0, #48 - 256]
-	ldp	x8, x9, [x1, #48]
-	stnp	x10, x11, [x0, #64 - 256]
-	ldp	x10, x11, [x1, #64]
-	stnp	x12, x13, [x0, #80 - 256]
-	ldp	x12, x13, [x1, #80]
-	stnp	x14, x15, [x0, #96 - 256]
-	ldp	x14, x15, [x1, #96]
-	stnp	x16, x17, [x0, #112 - 256]
-	ldp	x16, x17, [x1, #112]
+	.macro ldp1 reg1, reg2, ptr, val
+	ldp \reg1, \reg2, [\ptr, \val]
+	.endm
 
-	add	x0, x0, #128
-	add	x1, x1, #128
-
-	b.ne	1b
-
-	stnp	x2, x3, [x0, #-256]
-	stnp	x4, x5, [x0, #16 - 256]
-	stnp	x6, x7, [x0, #32 - 256]
-	stnp	x8, x9, [x0, #48 - 256]
-	stnp	x10, x11, [x0, #64 - 256]
-	stnp	x12, x13, [x0, #80 - 256]
-	stnp	x14, x15, [x0, #96 - 256]
-	stnp	x16, x17, [x0, #112 - 256]
+	.macro cpy1 dst, src, count
+	.arch_extension mops
+	cpypwn [\dst]!, [\src]!, \count!
+	cpymwn [\dst]!, [\src]!, \count!
+	cpyewn [\dst]!, [\src]!, \count!
+	.endm
 
+SYM_FUNC_START(__pi_copy_page)
+#include "copy_page_template.S"
 	ret
 SYM_FUNC_END(__pi_copy_page)
 SYM_FUNC_ALIAS(copy_page, __pi_copy_page)
diff --git a/arch/arm64/lib/copy_page_template.S b/arch/arm64/lib/copy_page_template.S
new file mode 100644
index 000000000000..ad931c37dcec
--- /dev/null
+++ b/arch/arm64/lib/copy_page_template.S
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ */
+
+/*
+ * Copy a page from src to dest (both are page aligned)
+ *
+ * Parameters:
+ *	x0 - dest
+ *	x1 - src
+ */
+dstin	.req	x0
+src	.req	x1
+
+#ifdef CONFIG_AS_HAS_MOPS
+alternative_if_not ARM64_HAS_MOPS
+	b	.Lno_mops
+alternative_else_nop_endif
+	mov	x2, #PAGE_SIZE
+	cpy1	dstin, src, x2
+	b	.Lexitfunc
+.Lno_mops:
+#endif
+
+	ldp1	x2, x3, x1, #0
+	ldp1	x4, x5, x1, #16
+	ldp1	x6, x7, x1, #32
+	ldp1	x8, x9, x1, #48
+	ldp1	x10, x11, x1, #64
+	ldp1	x12, x13, x1, #80
+	ldp1	x14, x15, x1, #96
+	ldp1	x16, x17, x1, #112
+
+	add	x0, x0, #256
+	add	x1, x1, #128
+1:
+	tst	x0, #(PAGE_SIZE - 1)
+
+	stnp	x2, x3, [x0, #-256]
+	ldp1	x2, x3, x1, #0
+	stnp	x4, x5, [x0, #16 - 256]
+	ldp1	x4, x5, x1, #16
+	stnp	x6, x7, [x0, #32 - 256]
+	ldp1	x6, x7, x1, #32
+	stnp	x8, x9, [x0, #48 - 256]
+	ldp1	x8, x9, x1, #48
+	stnp	x10, x11, [x0, #64 - 256]
+	ldp1	x10, x11, x1, #64
+	stnp	x12, x13, [x0, #80 - 256]
+	ldp1	x12, x13, x1, #80
+	stnp	x14, x15, [x0, #96 - 256]
+	ldp1	x14, x15, x1, #96
+	stnp	x16, x17, [x0, #112 - 256]
+	ldp1	x16, x17, x1, #112
+
+	add	x0, x0, #128
+	add	x1, x1, #128
+
+	b.ne	1b
+
+	stnp	x2, x3, [x0, #-256]
+	stnp	x4, x5, [x0, #16 - 256]
+	stnp	x6, x7, [x0, #32 - 256]
+	stnp	x8, x9, [x0, #48 - 256]
+	stnp	x10, x11, [x0, #64 - 256]
+	stnp	x12, x13, [x0, #80 - 256]
+	stnp	x14, x15, [x0, #96 - 256]
+	stnp	x16, x17, [x0, #112 - 256]
+.Lexitfunc:
diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S
index 5018ac03b6bf..1afe3ef1502c 100644
--- a/arch/arm64/lib/mte.S
+++ b/arch/arm64/lib/mte.S
@@ -80,6 +80,35 @@ SYM_FUNC_START(mte_copy_page_tags)
 	ret
 SYM_FUNC_END(mte_copy_page_tags)
 
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+/*
+ * Copy the tags from the source page to the destination one with memory error safe
+ *   x0 - address of the destination page
+ *   x1 - address of the source page
+ * Returns:
+ *   x0 - Return 0 if copy success, or
+ *        -EFAULT if anything goes wrong while copying.
+ */
+SYM_FUNC_START(mte_copy_mc_page_tags)
+	mov	x2, x0
+	mov	x3, x1
+	multitag_transfer_size x5, x6
+1:
+KERNEL_SEA(2f, ldgm	x4, [x3])
+	stgm	x4, [x2]
+	add	x2, x2, x5
+	add	x3, x3, x5
+	tst	x2, #(PAGE_SIZE - 1)
+	b.ne	1b
+
+	mov x0, #0
+	ret
+
+2:	mov x0, #-EFAULT
+	ret
+SYM_FUNC_END(mte_copy_mc_page_tags)
+#endif
+
 /*
  * Read tags from a user buffer (one tag per byte) and set the corresponding
  * tags at the given kernel address. Used by PTRACE_POKEMTETAGS.
diff --git a/arch/arm64/mm/copypage.c b/arch/arm64/mm/copypage.c
index cd5912ba617b..611024fca6d4 100644
--- a/arch/arm64/mm/copypage.c
+++ b/arch/arm64/mm/copypage.c
@@ -72,3 +72,88 @@ void copy_user_highpage(struct page *to, struct page *from,
 	flush_dcache_page(to);
 }
 EXPORT_SYMBOL_GPL(copy_user_highpage);
+
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+/*
+ * Return -EFAULT if anything goes wrong while copying page or mte.
+ */
+int copy_mc_highpage(struct page *to, struct page *from)
+{
+	void *kto = page_address(to);
+	void *kfrom = page_address(from);
+	struct folio *src = page_folio(from);
+	struct folio *dst = page_folio(to);
+	unsigned int i, nr_pages;
+	int ret;
+
+	ret = copy_mc_page(kto, kfrom);
+	if (ret) {
+		memory_failure_queue(page_to_pfn(from), 0);
+		return -EFAULT;
+	}
+
+	if (kasan_hw_tags_enabled())
+		page_kasan_tag_reset(to);
+
+	if (!system_supports_mte())
+		return 0;
+
+	if (folio_test_hugetlb(src)) {
+		if (!folio_test_hugetlb_mte_tagged(src) ||
+		    from != folio_page(src, 0))
+			return 0;
+
+		folio_try_hugetlb_mte_tagging(dst);
+
+		/*
+		 * Populate tags for all subpages.
+		 *
+		 * Don't assume the first page is head page since
+		 * huge page copy may start from any subpage.
+		 */
+		nr_pages = folio_nr_pages(src);
+		for (i = 0; i < nr_pages; i++) {
+			kfrom = page_address(folio_page(src, i));
+			kto = page_address(folio_page(dst, i));
+			ret = mte_copy_mc_page_tags(kto, kfrom);
+			if (ret) {
+				memory_failure_queue(page_to_pfn(folio_page(src, i)), 0);
+				return -EFAULT;
+			}
+		}
+		folio_set_hugetlb_mte_tagged(dst);
+	} else if (page_mte_tagged(from)) {
+		/* Migration may reuse an already tagged destination page. */
+		try_page_mte_tagging(to);
+
+		ret = mte_copy_mc_page_tags(kto, kfrom);
+		if (ret) {
+			memory_failure_queue(page_to_pfn(from), 0);
+			return -EFAULT;
+		}
+		set_page_mte_tagged(to);
+	}
+	/*
+	 * Failure paths above queue the actual source. GHES may already have
+	 * queued it; supported RAM callers rely on flags=0 duplicate requests
+	 * collapsing to already-poisoned handling, not repeated isolation.
+	 */
+	return 0;
+}
+EXPORT_SYMBOL(copy_mc_highpage);
+
+int copy_mc_user_highpage(struct page *to, struct page *from,
+			unsigned long vaddr, struct vm_area_struct *vma)
+{
+	int ret;
+
+	ret = copy_mc_highpage(to, from);
+	if (ret)
+		return ret;
+
+	flush_dcache_page(to);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(copy_mc_user_highpage);
+#endif
diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index 685c62c10408..f65d6ef3e991 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -425,6 +425,7 @@ static inline void copy_highpage(struct page *to, struct page *from)
 #endif
 
 #ifdef copy_mc_to_kernel
+#ifndef __HAVE_ARCH_COPY_MC_USER_HIGHPAGE
 /*
  * If architecture supports machine check exception handling, define the
  * #MC versions of copy_user_highpage and copy_highpage. They copy a memory
@@ -450,7 +451,9 @@ static inline int copy_mc_user_highpage(struct page *to, struct page *from,
 
 	return ret ? -EFAULT : 0;
 }
+#endif
 
+#ifndef __HAVE_ARCH_COPY_MC_HIGHPAGE
 static inline int copy_mc_highpage(struct page *to, struct page *from)
 {
 	unsigned long ret;
@@ -469,20 +472,25 @@ static inline int copy_mc_highpage(struct page *to, struct page *from)
 
 	return ret ? -EFAULT : 0;
 }
+#endif
 #else
+#ifndef __HAVE_ARCH_COPY_MC_USER_HIGHPAGE
 static inline int copy_mc_user_highpage(struct page *to, struct page *from,
 					unsigned long vaddr, struct vm_area_struct *vma)
 {
 	copy_user_highpage(to, from, vaddr, vma);
 	return 0;
 }
+#endif
 
+#ifndef __HAVE_ARCH_COPY_MC_HIGHPAGE
 static inline int copy_mc_highpage(struct page *to, struct page *from)
 {
 	copy_highpage(to, from);
 	return 0;
 }
 #endif
+#endif
 
 static inline void memcpy_page(struct page *dst_page, size_t dst_off,
 			       struct page *src_page, size_t src_off,
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v16 7/9] arm64: introduce copy_mc_to_kernel() implementation
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
                   ` (5 preceding siblings ...)
  2026-12-05  1:24 ` [PATCH v16 6/9] arm64: support copy_mc_[user]_highpage() Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-06  1:26   ` sashiko-bot
  2026-12-05  1:24 ` [PATCH v16 8/9] lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests Jiangtian Feng
  2026-12-05  1:24 ` [PATCH v16 9/9] lib/tests: memcpy_kunit: add memcpy_mc() and memcpy_mc_large() test Jiangtian Feng
  8 siblings, 1 reply; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jiangtian Feng

From: Tong Tiangen <tongtiangen@huawei.com>

The copy_mc_to_kernel() helper is memory copy implementation that handles
source exceptions. It can be used in memory copy scenarios that tolerate
hardware memory errors(e.g: pmem_read/dax_copy_to_iter).

Currently, only x86 and ppc support this helper, Add this for ARM64 as
well, if ARCH_HAS_COPY_MC is defined, by implementing copy_mc_to_kernel()
and memcpy_mc() functions.

The optimized memcpy template copies head and tail blocks out of order and
does not maintain a current destination pointer on every load path. After
a partially completed copy, using the original destination to calculate
the residual can therefore report the entire input length again.

Use the forward copy template shared with the uaccess code instead. Every
completed store advances dst, so the exception exit can calculate the
residual as end - dst. The MOPS main/epilogue fixup converts the Option A
register state to the current destination before the same calculation.
Use forward-only CPYF operations: direction-selecting CPY operations may
choose backward even for non-overlapping inputs, which would invalidate
this residual calculation. This also avoids changing the normal optimized
memcpy implementation.

MOPS copy instructions in the MC variant use EX_TYPE_KACCESS_SEA fixups
like the regular load instructions, but the handler accepts source reads
only and rejects destination writes.

Define __HAVE_ARCH_MEMCPY_MC and its declarations/macros only when
CONFIG_ARCH_HAS_COPY_MC is enabled. Otherwise KASAN generic/SW builds would
include the memcpy_mc() wrapper while memcpy_mc.o is not linked, leaving an
undefined __memcpy_mc reference.

Qoder, an AI coding assistant, helped identify the configuration-dependent
link failure and residual-byte bug, redesign the forward-progress copy path
and review the MOPS descriptions.

Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 arch/arm64/include/asm/string.h  |  9 ++++
 arch/arm64/include/asm/uaccess.h | 17 +++++++
 arch/arm64/lib/Makefile          |  2 +-
 arch/arm64/lib/memcpy_mc.S       | 84 ++++++++++++++++++++++++++++++++
 mm/kasan/shadow.c                | 12 +++++
 5 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/lib/memcpy_mc.S

diff --git a/arch/arm64/include/asm/string.h b/arch/arm64/include/asm/string.h
index 3a3264ff47b9..ac425e2c9744 100644
--- a/arch/arm64/include/asm/string.h
+++ b/arch/arm64/include/asm/string.h
@@ -35,6 +35,12 @@ extern void *memchr(const void *, int, __kernel_size_t);
 extern void *memcpy(void *, const void *, __kernel_size_t);
 extern void *__memcpy(void *, const void *, __kernel_size_t);
 
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+#define __HAVE_ARCH_MEMCPY_MC
+extern unsigned long memcpy_mc(void *, const void *, __kernel_size_t);
+extern unsigned long __memcpy_mc(void *, const void *, __kernel_size_t);
+#endif
+
 #define __HAVE_ARCH_MEMMOVE
 extern void *memmove(void *, const void *, __kernel_size_t);
 extern void *__memmove(void *, const void *, __kernel_size_t);
@@ -57,6 +63,9 @@ void memcpy_flushcache(void *dst, const void *src, size_t cnt);
  */
 
 #define memcpy(dst, src, len) __memcpy(dst, src, len)
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+#define memcpy_mc(dst, src, len) __memcpy_mc(dst, src, len)
+#endif
 #define memmove(dst, src, len) __memmove(dst, src, len)
 #define memset(s, c, n) __memset(s, c, n)
 
diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h
index 9f5bd9c69c24..734dde180a5a 100644
--- a/arch/arm64/include/asm/uaccess.h
+++ b/arch/arm64/include/asm/uaccess.h
@@ -498,5 +498,22 @@ static inline size_t probe_subpage_writeable(const char __user *uaddr,
 }
 
 #endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+/**
+ * copy_mc_to_kernel - memory copy that handles source exceptions
+ *
+ * @to:		destination address
+ * @from:	source address
+ * @size:	number of bytes to copy
+ *
+ * Return 0 for success, or bytes not copied.
+ */
+static inline unsigned long __must_check
+copy_mc_to_kernel(void *to, const void *from, unsigned long size)
+{
+	return memcpy_mc(to, from, size);
+}
+#define copy_mc_to_kernel copy_mc_to_kernel
+#endif
 
 #endif /* __ASM_UACCESS_H */
diff --git a/arch/arm64/lib/Makefile b/arch/arm64/lib/Makefile
index f7952eb7fb8c..cdb866ca71e7 100644
--- a/arch/arm64/lib/Makefile
+++ b/arch/arm64/lib/Makefile
@@ -11,7 +11,7 @@ lib-y		:= clear_user.o delay.o copy_from_user.o		\
 
 lib-$(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) += uaccess_flushcache.o
 
-lib-$(CONFIG_ARCH_HAS_COPY_MC) += copy_mc_page.o
+lib-$(CONFIG_ARCH_HAS_COPY_MC) += copy_mc_page.o memcpy_mc.o
 
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
 
diff --git a/arch/arm64/lib/memcpy_mc.S b/arch/arm64/lib/memcpy_mc.S
new file mode 100644
index 000000000000..c7a217808fee
--- /dev/null
+++ b/arch/arm64/lib/memcpy_mc.S
@@ -0,0 +1,84 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2013 ARM Ltd.
+ * Copyright (C) 2013 Linaro.
+ *
+ * This code is based on glibc cortex strings work originally authored by Linaro
+ * be found @
+ *
+ * http://bazaar.linaro.org/linaro-toolchain-dev/cortex-strings/trunk/
+ * files/head:/src/aarch64/
+ */
+
+#include <linux/linkage.h>
+#include <asm/assembler.h>
+#include <asm/cache.h>
+#include <asm/asm-uaccess.h>
+
+/*
+ * Copy a buffer from src to dest (alignment handled by the hardware)
+ *
+ * Parameters:
+ *	x0 - dest
+ *	x1 - src
+ *	x2 - n
+ * Returns:
+ *	x0 - bytes not copied
+ */
+	.macro ldrb1 reg, ptr, val
+	KERNEL_SEA(9997f, ldrb  \reg, [\ptr], \val)
+	.endm
+
+	.macro strb1 reg, ptr, val
+	strb \reg, [\ptr], \val
+	.endm
+
+	.macro ldrh1 reg, ptr, val
+	KERNEL_SEA(9997f, ldrh  \reg, [\ptr], \val)
+	.endm
+
+	.macro strh1 reg, ptr, val
+	strh \reg, [\ptr], \val
+	.endm
+
+	.macro ldr1 reg, ptr, val
+	KERNEL_SEA(9997f, ldr \reg, [\ptr], \val)
+	.endm
+
+	.macro str1 reg, ptr, val
+	str \reg, [\ptr], \val
+	.endm
+
+	.macro ldp1 reg1, reg2, ptr, val
+	KERNEL_SEA(9997f, ldp \reg1, \reg2, [\ptr], \val)
+	.endm
+
+	.macro stp1 reg1, reg2, ptr, val
+	stp \reg1, \reg2, [\ptr], \val
+	.endm
+
+	.macro cpy1 dst, src, count
+	.arch_extension mops
+	KERNEL_SEA(9997f, cpyfp [\dst]!, [\src]!, \count!)
+	KERNEL_SEA(9996f, cpyfm [\dst]!, [\src]!, \count!)
+	KERNEL_SEA(9996f, cpyfe [\dst]!, [\src]!, \count!)
+	.endm
+
+end	.req	x5
+SYM_FUNC_START(__memcpy_mc)
+	add	end, x0, x2
+#include "copy_template.S"
+	mov	x0, #0
+	ret
+
+	// Exception fixups
+9996:	b.cs	9997f
+	// Registers are in Option A format
+	add	dst, dst, count
+9997:	sub	x0, end, dst			// bytes not copied
+	ret
+SYM_FUNC_END(__memcpy_mc)
+
+EXPORT_SYMBOL(__memcpy_mc)
+SYM_FUNC_ALIAS_WEAK(memcpy_mc, __memcpy_mc)
+EXPORT_SYMBOL(memcpy_mc)
diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c
index d286e0a04543..da21a13151b9 100644
--- a/mm/kasan/shadow.c
+++ b/mm/kasan/shadow.c
@@ -79,6 +79,18 @@ void *memcpy(void *dest, const void *src, size_t len)
 }
 #endif
 
+#ifdef __HAVE_ARCH_MEMCPY_MC
+#undef memcpy_mc
+unsigned long memcpy_mc(void *dest, const void *src, size_t len)
+{
+	if (!kasan_check_range(src, len, false, _RET_IP_) ||
+	    !kasan_check_range(dest, len, true, _RET_IP_))
+		return len;
+
+	return __memcpy_mc(dest, src, len);
+}
+#endif
+
 void *__asan_memset(void *addr, int c, ssize_t len)
 {
 	if (!kasan_check_range(addr, len, true, _RET_IP_))
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v16 8/9] lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
                   ` (6 preceding siblings ...)
  2026-12-05  1:24 ` [PATCH v16 7/9] arm64: introduce copy_mc_to_kernel() implementation Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-06  1:26   ` sashiko-bot
  2026-12-05  1:24 ` [PATCH v16 9/9] lib/tests: memcpy_kunit: add memcpy_mc() and memcpy_mc_large() test Jiangtian Feng
  8 siblings, 1 reply; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jiangtian Feng

From: Ruidong Tian <tianruidong@linux.alibaba.com>

Add KUnit tests for copy_page() and copy_mc_page(), modeled after
the existing memcpy_test() style: a static page-aligned src and a
two-page dst, filled with random bytes plus non-zero edges, then
verify byte-for-byte equality and that the adjacent page is
untouched.

Keep page_pattern writable because init_page() fills it with memset();
declaring it const triggers a discarded-qualifier compiler warning.

Qoder, an AI coding assistant, helped identify the build warning and review
the test-buffer lifecycle.

Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 lib/tests/memcpy_kunit.c | 67 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)

diff --git a/lib/tests/memcpy_kunit.c b/lib/tests/memcpy_kunit.c
index d36933554e46..8bcaaf9ab1ea 100644
--- a/lib/tests/memcpy_kunit.c
+++ b/lib/tests/memcpy_kunit.c
@@ -493,6 +493,67 @@ static void memmove_overlap_test(struct kunit *test)
 	}
 }
 
+/* --- Page-sized copy tests --- */
+
+static u8 page_src[PAGE_SIZE] __aligned(PAGE_SIZE);
+static u8 page_dst[PAGE_SIZE * 2] __aligned(PAGE_SIZE);
+static u8 page_pattern[PAGE_SIZE] __aligned(PAGE_SIZE);
+
+static void init_page(struct kunit *test)
+{
+	/* Get many bit patterns. */
+	get_random_bytes(page_src, PAGE_SIZE);
+
+	/* Make sure we have non-zero edges. */
+	set_random_nonzero(test, &page_src[0]);
+	set_random_nonzero(test, &page_src[PAGE_SIZE - 1]);
+
+	memset(page_dst, 0xA5, ARRAY_SIZE(page_dst));
+	memset(page_pattern, 0xA5, PAGE_SIZE);
+}
+
+static void copy_page_test(struct kunit *test)
+{
+	init_page(test);
+
+	/* Copy. */
+	copy_page(page_dst, page_src);
+
+	/* Verify byte-for-byte exact. */
+	KUNIT_ASSERT_EQ_MSG(test,
+		memcmp(page_dst, page_src, PAGE_SIZE), 0,
+		"copy_page content mismatch with random data");
+
+	/* Verify no overflow into second page. */
+	KUNIT_ASSERT_EQ_MSG(test,
+		memcmp(page_dst + PAGE_SIZE, page_pattern, PAGE_SIZE), 0,
+		"copy_page overflow into adjacent page");
+}
+
+#ifdef __HAVE_ARCH_COPY_MC_PAGE
+static void copy_mc_page_test(struct kunit *test)
+{
+	int ret;
+
+	init_page(test);
+
+	/* Copy and check return value. */
+	ret = copy_mc_page(page_dst, page_src);
+	KUNIT_ASSERT_EQ_MSG(test, ret, 0,
+		"copy_mc_page returned %d on clean memory", ret);
+
+	/* Verify byte-for-byte exact. */
+	KUNIT_ASSERT_EQ_MSG(test,
+		memcmp(page_dst, page_src, PAGE_SIZE), 0,
+		"copy_mc_page content mismatch with random data");
+
+	/* Verify no overflow into second page. */
+	KUNIT_ASSERT_EQ_MSG(test,
+		memcmp(page_dst + PAGE_SIZE, page_pattern, PAGE_SIZE), 0,
+		"copy_mc_page overflow into adjacent page");
+}
+#endif /* __HAVE_ARCH_COPY_MC_PAGE */
+
 static struct kunit_case memcpy_test_cases[] = {
 	KUNIT_CASE(memset_test),
 	KUNIT_CASE(memcpy_test),
@@ -500,6 +561,10 @@ static struct kunit_case memcpy_test_cases[] = {
 	KUNIT_CASE_SLOW(memmove_test),
 	KUNIT_CASE_SLOW(memmove_large_test),
 	KUNIT_CASE_SLOW(memmove_overlap_test),
+	KUNIT_CASE(copy_page_test),
+#ifdef __HAVE_ARCH_COPY_MC_PAGE
+	KUNIT_CASE(copy_mc_page_test),
+#endif
 	{}
 };
 
@@ -510,5 +575,5 @@ static struct kunit_suite memcpy_test_suite = {
 
 kunit_test_suite(memcpy_test_suite);
 
-MODULE_DESCRIPTION("test cases for memcpy(), memmove(), and memset()");
+MODULE_DESCRIPTION("test cases for memcpy(), memmove(), memset() and copy_page()");
 MODULE_LICENSE("GPL");
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v16 9/9] lib/tests: memcpy_kunit: add memcpy_mc() and memcpy_mc_large() test
  2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
                   ` (7 preceding siblings ...)
  2026-12-05  1:24 ` [PATCH v16 8/9] lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests Jiangtian Feng
@ 2026-12-05  1:24 ` Jiangtian Feng
  2026-12-06  1:26   ` sashiko-bot
  8 siblings, 1 reply; 18+ messages in thread
From: Jiangtian Feng @ 2026-12-05  1:24 UTC (permalink / raw)
  To: catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, glider, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao,
	linux-arm-kernel, linux-acpi, linux-mm, linux-hardening,
	kasan-dev, linuxppc-dev, x86, linux-kernel, Jiangtian Feng

From: Ruidong Tian <tianruidong@linux.alibaba.com>

memcpy_mc() is the Machine-Check safe memcpy variant that returns the
number of bytes NOT copied on a hardware memory error, or 0 on success.

Add two test cases modeled after the existing memcpy_test() and
memcpy_large_test() implementations.

Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 lib/tests/memcpy_kunit.c | 121 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 120 insertions(+), 1 deletion(-)

diff --git a/lib/tests/memcpy_kunit.c b/lib/tests/memcpy_kunit.c
index 8bcaaf9ab1ea..479fc9b880b3 100644
--- a/lib/tests/memcpy_kunit.c
+++ b/lib/tests/memcpy_kunit.c
@@ -554,6 +554,121 @@ static void copy_mc_page_test(struct kunit *test)
 }
 #endif /* __HAVE_ARCH_COPY_MC_PAGE */
 
+#ifdef __HAVE_ARCH_MEMCPY_MC
+/*
+ * memcpy_mc() is a Machine-Check safe memcpy variant.
+ * Signature: unsigned long memcpy_mc(void *dst, const void *src, size_t len)
+ * Returns:   0 on success, or number of bytes NOT copied on MC error.
+ *
+ * In the normal (no-poison) path it must behave identically to memcpy()
+ * and always return 0.
+ */
+static void memcpy_mc_test(struct kunit *test)
+{
+#define TEST_OP "memcpy_mc"
+	struct some_bytes control = {
+		.data = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+			  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+			  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+			  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+		},
+	};
+	struct some_bytes zero = { };
+	struct some_bytes middle = {
+		.data = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+			  0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00,
+			  0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
+			  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+		},
+	};
+	struct some_bytes three = {
+		.data = { 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+			  0x20, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
+			  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+			  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+			},
+	};
+	struct some_bytes dest = { };
+	unsigned long ret;
+	int count;
+	u8 *ptr;
+
+	/* Verify static initializers. */
+	check(control, 0x20);
+	check(zero, 0);
+	compare("static initializers", dest, zero);
+
+	/* Verify assignment. */
+	dest = control;
+	compare("direct assignment", dest, control);
+
+	/* Verify complete overwrite. */
+	ret = memcpy_mc(dest.data, zero.data, sizeof(dest.data));
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	compare("complete overwrite", dest, zero);
+
+	/* Verify middle overwrite: 7 bytes at offset 12. */
+	dest = control;
+	ret = memcpy_mc(dest.data + 12, zero.data, 7);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	compare("middle overwrite", dest, middle);
+
+	/* Verify zero-length copy is a no-op. */
+	dest = control;
+	ret = memcpy_mc(dest.data, zero.data, 0);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	compare("zero length", dest, control);
+
+	/* Verify argument side-effects aren't repeated. */
+	dest = control;
+	ptr = dest.data;
+	count = 1;
+	ret = memcpy_mc(ptr++, zero.data, count++);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	ptr += 8;
+	ret = memcpy_mc(ptr++, zero.data, count++);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	compare("argument side-effects", dest, three);
+#undef TEST_OP
+}
+
+static void memcpy_mc_large_test(struct kunit *test)
+{
+	init_large(test);
+
+	/* Sweep 1..1024 bytes x shifting offset to cover all template paths. */
+	for (int bytes = 1; bytes <= ARRAY_SIZE(large_src); bytes++) {
+		for (int offset = 0; offset < ARRAY_SIZE(large_src); offset++) {
+			int right_zero_pos = offset + bytes;
+			int right_zero_size = ARRAY_SIZE(large_dst) - right_zero_pos;
+			int ret;
+
+			ret = memcpy_mc(large_dst + offset, large_src, bytes);
+			KUNIT_ASSERT_EQ_MSG(test, ret, 0,
+				"memcpy_mc returned %d with size %d at offset %d",
+				ret, bytes, offset);
+
+			/* No write before copy area. */
+			KUNIT_ASSERT_EQ_MSG(test,
+				memcmp(large_dst, large_zero, offset), 0,
+				"with size %d at offset %d", bytes, offset);
+			/* No write after copy area. */
+			KUNIT_ASSERT_EQ_MSG(test,
+				memcmp(&large_dst[right_zero_pos], large_zero,
+				       right_zero_size), 0,
+				"with size %d at offset %d", bytes, offset);
+			/* Byte-for-byte exact. */
+			KUNIT_ASSERT_EQ_MSG(test,
+				memcmp(large_dst + offset, large_src, bytes), 0,
+				"with size %d at offset %d", bytes, offset);
+
+			memset(large_dst + offset, 0, bytes);
+		}
+		cond_resched();
+	}
+}
+#endif /* __HAVE_ARCH_MEMCPY_MC */
+
 static struct kunit_case memcpy_test_cases[] = {
 	KUNIT_CASE(memset_test),
 	KUNIT_CASE(memcpy_test),
@@ -564,6 +679,10 @@ static struct kunit_case memcpy_test_cases[] = {
 	KUNIT_CASE(copy_page_test),
 #ifdef __HAVE_ARCH_COPY_MC_PAGE
 	KUNIT_CASE(copy_mc_page_test),
+#endif
+#ifdef __HAVE_ARCH_MEMCPY_MC
+	KUNIT_CASE(memcpy_mc_test),
+	KUNIT_CASE_SLOW(memcpy_mc_large_test),
 #endif
 	{}
 };
@@ -575,5 +694,5 @@ static struct kunit_suite memcpy_test_suite = {
 
 kunit_test_suite(memcpy_test_suite);
 
-MODULE_DESCRIPTION("test cases for memcpy(), memmove(), memset() and copy_page()");
+MODULE_DESCRIPTION("test cases for memcpy(), memmove(), memset(), copy_page() and memcpy_mc()");
 MODULE_LICENSE("GPL");
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v16 1/9] uaccess: add generic fallback version of copy_mc_to_user()
  2026-12-05  1:24 ` [PATCH v16 1/9] uaccess: add generic fallback version of copy_mc_to_user() Jiangtian Feng
@ 2026-12-06  1:26   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-06  1:26 UTC (permalink / raw)
  To: Jiangtian Feng, catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, gli, der, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao, linux-mm,
	kasan-dev, x86, Mauro  Carvalho Chehab, Jonathan Cameron

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] uaccess: generic copy_mc_to_user() invokes check_copy_size() on
  DAX reads
--

commit fe89ceb6da809d920129fc15fabc391ee6be64d2
Author: Tong Tiangen <tongtiangen@huawei.com>

uaccess: add generic fallback version of copy_mc_to_user()

This commit adds a generic copy_mc_to_user() fallback in
include/linux/uaccess.h that calls copy_to_user(), and defines
copy_mc_to_user macros on powerpc and x86 so those architectures keep
their existing implementations.

> diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
> index 67cdb0a4f5839..689604cb590c4 100644
> --- a/include/linux/uaccess.h
> +++ b/include/linux/uaccess.h
> @@ -243,6 +243,14 @@ copy_mc_to_kernel(void *dst, const void *src, size_t cnt)
>  }
>  #endif
>  
> +#ifndef copy_mc_to_user
> +static inline unsigned long __must_check
> +copy_mc_to_user(void __user *dst, const void *src, unsigned long cnt)
> +{
> +	return copy_to_user(dst, src, cnt);
> +}
> +#endif

[Severity: High]
Can calling copy_to_user() here trigger a usercopy_abort() BUG() on
FS-DAX reads?

copy_mc_to_user() is only called from lib/iov_iter.c:copy_to_user_iter_mc()
via _copy_mc_to_iter().  Unlike raw_copy_to_user() or x86's
copy_mc_to_user(), copy_to_user() invokes check_copy_size() under
CONFIG_HARDENED_USERCOPY and repeats access_ok() and instrumentation.

When an architecture enables CONFIG_ARCH_HAS_COPY_MC without its own
copy_mc_to_user() (as arm64 does in commit "arm64: support
copy_mc_[user]_highpage()"), devm_memremap_pages() clears MEMBLOCK_NOMAP
in arch_add_memory() so virt_addr_valid() is true for pmem addresses.

After mmap faults in a PMD-mapped FS-DAX compound folio via
dax_folio_init(), an unprivileged read(2) crossing that folio boundary
runs dax_copy_to_iter() -> _copy_mc_to_iter() -> copy_mc_to_user() ->
check_heap_object() and crashes the kernel with BUG().

Using raw_copy_to_user() here (or _copy_to_user() if direct callers
outside lib/iov_iter.c need access_ok()) avoids running
check_copy_size() inside _copy_mc_to_iter().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012435.1406255-1-jiangtianfeng@linux.alibaba.com?part=1


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v16 2/9] ACPI: APEI: GHES: use exception context to gate SIGBUS on poison consumption
  2026-12-05  1:24 ` [PATCH v16 2/9] ACPI: APEI: GHES: use exception context to gate SIGBUS on poison consumption Jiangtian Feng
@ 2026-12-06  1:26   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-06  1:26 UTC (permalink / raw)
  To: Jiangtian Feng, catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, gli, der, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao, linux-mm,
	kasan-dev, x86

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] ACPI: APEI: GHES_CTX_KERNEL defers SEA poisoning to async
  workqueue
- [Medium] ACPI: APEI: GHES: cached kernel SEA returns 0 without queuing
  work
- [Low] ACPI: APEI: commit message mismatches enum ghes_exec_ctx and
  reachability
- [Low] ACPI: APEI: GHES_CTX() double-evaluates, fails on NULL, lacks
  ptrace.h
--

commit 9312338f2bc24ddbc1ea6e4a8f429c087d02ab9f
Author: Ruidong Tian <tianruidong@linux.alibaba.com>

ACPI: APEI: GHES: use exception context to gate SIGBUS on poison consumption

This commit introduces an execution context enum and plumbs it from
apei_claim_sea() through GHES NMI-like spooling into memory failure
handling, so kernel-mode synchronous external aborts can avoid setting
MF_ACTION_REQUIRED or force-killing the current task with SIGBUS.

> Up to now ghes_handle_memory_failure() unconditionally promoted any
> synchronous recoverable memory error to MF_ACTION_REQUIRED, which
> ends up SIGBUS on current -- regardless of whether the poison was
> consumed from user space or from inside the kernel on the task's
> behalf. That kills tasks that should instead have seen a plain
> syscall error.

[ ... ]

> Introduce:
>
>     enum context { ... };
>
> and plumb the value all the way down to the queued estatus node:
>
>  * Add an 'enum context context' field to struct ghes_estatus_node
>    and record it in ghes_in_nmi_queue_one_entry().
>  * Extend ghes_notify_sea() and the internal
>    ghes_in_nmi_spool_from_list() with an enum context parameter.

[Severity: Low]
This isn't a bug, but the commit message refers to enum context three
times while the patch actually defines enum ghes_exec_ctx, and it omits
the new GHES_CTX() macro.

Also, at this commit do_sea() still checks user_mode(regs) before calling
apei_claim_sea(regs), so GHES_CTX_KERNEL is not yet reachable.  Describing
this as preparatory work rather than a currently reachable bug would match
the tree state.

> diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
> index 5d888c82efeaa..eb4466aa87f25 100644
> --- a/drivers/acpi/apei/ghes.c
> +++ b/drivers/acpi/apei/ghes.c
> @@ -543,7 +543,7 @@ static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata,
>  	    (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
>  		flags = MF_SOFT_OFFLINE;
>  	if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
> -		flags = sync ? MF_ACTION_REQUIRED : 0;
> +		flags = sync && context != GHES_CTX_KERNEL ? MF_ACTION_REQUIRED : 0;
>  
>  	if (flags != -1)
>  		return ghes_do_memory_failure(mem_err->physical_addr, flags);
> @@ -552,10 +552,10 @@ static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata,
>  }
>  
>  static bool ghes_handle_arm_hw_error(struct acpi_hest_generic_data *gdata,
> -				     int sev, bool sync)
> +				     int sev, bool sync, enum ghes_exec_ctx context)
>  {
>  	struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
> -	int flags = sync ? MF_ACTION_REQUIRED : 0;
> +	int flags = sync && context != GHES_CTX_KERNEL ? MF_ACTION_REQUIRED : 0;

[Severity: Medium]
Does clearing MF_ACTION_REQUIRED for GHES_CTX_KERNEL inadvertently defer
page poisoning to the asynchronous workqueue?

ghes_do_memory_failure() only queues synchronous TWA_RESUME task_work when
flags == MF_ACTION_REQUIRED && current->mm, so passing flags = 0 falls
through to memory_failure_queue(pfn, 0).  When a kernel copy helper such as
copy_from_user() recovers via its extable fixup and returns -EFAULT, the
task can return to userspace before the kworker runs, while the poisoned
page is still mapped and unpoisoned.

In that window, the task can munmap() or exit and return the poisoned page
to the allocator for another task or the kernel to consume, or re-access it
and hit the 10-second ghes_estatus_cached() entry just added by
ghes_proc_in_irq().  Gating task_work_add() on sync && current->mm while
gating the SIGBUS delivery in memory_failure_cb() on MF_ACTION_REQUIRED
would keep poisoning synchronous before returning to userspace while still
suppressing SIGBUS for GHES_CTX_KERNEL.

[ ... ]

> @@ -1452,11 +1456,10 @@ static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
>  	/* This error has been reported before, don't process it again. */
>  	if (ghes_estatus_cached(estatus)) {
>  		/*
> -		 * Return failure on duplicate SEA entries so that the
> -		 * subsequent SEA handler invocation sends a SIGBUS signal to
> -		 * the task to prevent it from re-entering the handler loop.
> +		 * Kernel copies must still reach their fixup; retain duplicate
> +		 * SEA anti-storm handling for user and KVM/NA paths.
>  		 */
> -		if (is_hest_sync_notify(ghes))
> +		if (is_hest_sync_notify(ghes) && context != GHES_CTX_KERNEL)
>  			rc = -ECANCELED;
>  		goto no_work;
>  	}
> @@ -1473,14 +1476,15 @@ static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
>  }
>  
>  static int __maybe_unused ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
> -			       enum fixed_addresses fixmap_idx)
> +			       enum fixed_addresses fixmap_idx,
> +			       enum ghes_exec_ctx context)
>  {
>  	int ret = -ENOENT;
>  	struct ghes *ghes;
>  
>  	rcu_read_lock();
>  	list_for_each_entry_rcu(ghes, rcu_list, list) {
> -		if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
> +		if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx, context))
>  			ret = 0;
>  	}
>  	rcu_read_unlock();

[Severity: Medium]
Can returning 0 from the cached no_work path break duplicate kernel SEA
recovery when IRQs are disabled?

For a cached GHES_CTX_KERNEL record, ghes_in_nmi_queue_one_entry() frees
estatus_node without adding it to ghes_estatus_llist, yet returns 0.
ghes_in_nmi_spool_from_list() then treats that 0 as queued work, calls
irq_work_queue(&ghes_proc_irq_work) on an empty list, and returns 0 to
apei_claim_sea().

If the duplicate SEA hits an IRQ-disabled kernel copy such as
__copy_from_user_inatomic() in unwind_user_frame() during a perf PMU IRQ,
apei_claim_sea() assumes queued work could not run, logs "APEI work queued
but not completed", and returns -EINPROGRESS.  With commit "arm64: enable
recover from synchronous external abort in kernel context",
do_apei_claim_sea() then skips fixup_exception_me() and do_sea() falls
through to arm64_notify_die(), turning a recoverable duplicate copy into a
fatal exception.  Even with IRQs enabled, it still triggers a spurious
irq_work_queue() and irq_work_run().

Distinguishing "claimed, already cached" from "claimed, work queued" across
ghes_in_nmi_queue_one_entry(), ghes_in_nmi_spool_from_list(), and
ghes_notify_sea() would avoid both the empty irq_work and the false
-EINPROGRESS.

> diff --git a/include/acpi/ghes.h b/include/acpi/ghes.h
> index 7acf209061ea7..2cd97f7002140 100644
> --- a/include/acpi/ghes.h
> +++ b/include/acpi/ghes.h
> @@ -33,10 +33,21 @@ struct ghes {
>  	void __iomem *error_status_vaddr;
>  };
>  
> +enum ghes_exec_ctx {
> +	GHES_CTX_NA = -1,
> +	GHES_CTX_KERNEL = 0,
> +	GHES_CTX_USER = 1
> +};
> +
> +#define GHES_CTX(regs)	((regs) ? (user_mode(regs) ? GHES_CTX_USER \
> +						   : GHES_CTX_KERNEL) \
> +				: GHES_CTX_NA)

[Severity: Low]
Should GHES_CTX() be a static inline helper instead of a macro?

As written, it evaluates regs twice when non-NULL, fails to compile if a
caller passes literal NULL because user_mode(NULL) dereferences void *, and
relies on callers transitively including ptrace.h for user_mode().  A
static inline helper taking const struct pt_regs *regs (with ptrace.h
included, or kept local to arch/arm64/kernel/acpi.c) avoids all three.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012435.1406255-1-jiangtianfeng@linux.alibaba.com?part=2


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v16 3/9] arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO into ACCESS_ERR_ZERO
  2026-12-05  1:24 ` [PATCH v16 3/9] arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO into ACCESS_ERR_ZERO Jiangtian Feng
@ 2026-12-06  1:26   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-06  1:26 UTC (permalink / raw)
  To: Jiangtian Feng, catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, gli, der, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao, linux-mm,
	kasan-dev, x86

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] arm64: extable: merging KACCESS_ERR_ZERO breaks
  kaccess/uaccess SEA split
- [Low] arm64: extable: duplicated UACCESS_ERR_ZERO and KACCESS_ERR_ZERO
  macros
--

commit 770e390d707bb88f38fe626fe07a20231f1369d8
Author: Ruidong Tian <tianruidong@linux.alibaba.com>

arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO into ACCESS_ERR_ZERO

This commit merges EX_TYPE_UACCESS_ERR_ZERO and EX_TYPE_KACCESS_ERR_ZERO
into a single EX_TYPE_ACCESS_ERR_ZERO exception table type and updates the
extable macros and fixup_exception() accordingly.

> diff --git a/arch/arm64/include/asm/asm-extable.h b/arch/arm64/include/asm/asm-extable.h
> index d67e2fdd1aee5..06b19023939be 100644
> --- a/arch/arm64/include/asm/asm-extable.h
> +++ b/arch/arm64/include/asm/asm-extable.h
> @@ -7,12 +7,11 @@
>  
>  #define EX_TYPE_NONE			0
>  #define EX_TYPE_BPF			1
> -#define EX_TYPE_UACCESS_ERR_ZERO	2
> -#define EX_TYPE_KACCESS_ERR_ZERO	3
> -#define EX_TYPE_UACCESS_CPY		4
> -#define EX_TYPE_LOAD_UNALIGNED_ZEROPAD	5
> +#define EX_TYPE_ACCESS_ERR_ZERO		2
> +#define EX_TYPE_UACCESS_CPY		3
> +#define EX_TYPE_LOAD_UNALIGNED_ZEROPAD	4
>  
> -/* Data fields for EX_TYPE_UACCESS_ERR_ZERO */
> +/* Data fields for EX_TYPE_ACCESS_ERR_ZERO */

[Severity: Medium]
Does merging EX_TYPE_KACCESS_ERR_ZERO into EX_TYPE_ACCESS_ERR_ZERO break
the separation between user and kernel nofault accesses needed for
synchronous external abort recovery?

Commit 4953fc3d3270 ("arm64: extable: add new extable type
EX_TYPE_KACCESS_ERR_ZERO support") split these two types so machine check
handling can distinguish __get_kernel_nofault() and __put_kernel_nofault()
from user accesses.

In the follow-up commit "arm64: enable recover from synchronous external
abort in kernel context", fixup_exception_me() handles
EX_TYPE_ACCESS_ERR_ZERO unconditionally without checking ESR_ELx_WNR, so
an abort on copy_to_kernel_nofault() is silently fixed up.

For example, text_poke_memcpy() ignores the return value of
copy_to_kernel_nofault(), so fixing up an abort there lets __text_poke()
flush icache and return success over partially patched kernel text.

Collapsing the two types also prevents insn_may_access_user() from
rejecting _ASM_EXTABLE_KACCESS* entries as kernel accesses.

[ ... ]

> @@ -96,7 +95,7 @@
>  #define _ASM_EXTABLE_UACCESS_ERR_ZERO(insn, fixup, err, zero)		\
>  	__DEFINE_ASM_GPR_NUMS						\
>  	__ASM_EXTABLE_RAW(#insn, #fixup, 				\
> -			  __stringify(EX_TYPE_UACCESS_ERR_ZERO),	\
> +			  __stringify(EX_TYPE_ACCESS_ERR_ZERO),	\
>  			  "("						\
>  			    EX_DATA_REG(ERR, err) " | "			\
>  			    EX_DATA_REG(ZERO, zero)			\
> @@ -105,7 +104,7 @@
>  #define _ASM_EXTABLE_KACCESS_ERR_ZERO(insn, fixup, err, zero)		\
>  	__DEFINE_ASM_GPR_NUMS						\
>  	__ASM_EXTABLE_RAW(#insn, #fixup, 				\
> -			  __stringify(EX_TYPE_KACCESS_ERR_ZERO),	\
> +			  __stringify(EX_TYPE_ACCESS_ERR_ZERO),	\
>  			  "("						\
>  			    EX_DATA_REG(ERR, err) " | "			\
>  			    EX_DATA_REG(ZERO, zero)			\

[Severity: Low]
This isn't a bug, but _ASM_EXTABLE_UACCESS_ERR_ZERO and
_ASM_EXTABLE_KACCESS_ERR_ZERO now have identical bodies.  If these types
stay merged, could both expand to a shared _ASM_EXTABLE_ACCESS_ERR_ZERO
helper macro to avoid duplicating the definition?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012435.1406255-1-jiangtianfeng@linux.alibaba.com?part=3


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v16 4/9] arm64: enable recover from synchronous external abort in kernel context
  2026-12-05  1:24 ` [PATCH v16 4/9] arm64: enable recover from synchronous external abort in kernel context Jiangtian Feng
@ 2026-12-06  1:26   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-06  1:26 UTC (permalink / raw)
  To: Jiangtian Feng, catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, gli, der, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao, linux-mm,
	kasan-dev, x86

Thank you for your contribution! Sashiko AI review found 11 potential
issues to consider:
- [High] arm64: EX_TYPE_ACCESS_ERR_ZERO SEA fixup livelocks futex retry
  loops
- [Medium] arm64: SEA fixup in __arch_copy_to_user() livelocks
  read_kcore_iter()
- [Medium] arm64: unconditional apei_claim_sea() in EL1 deadlocks on
  nested SEA
- [Medium] arm64: fixup_exception_me() recovers kernel/user writes via
  ACCESS_ERR_ZERO
- [Medium] arm64: broad SEA extable fixup turns EL0 emulation SEAs into
  SIGSEGV
- [Medium] arm64: do_apei_claim_sea() recovers SEAs even without page
  isolation
- [Low] arm64: unconfirmed ESR_ELx.WnR validity on FSC_EXTABT SEAs in
  extable
- [Low] arm64: commit message contradicts GHES_CTX_KERNEL SIGBUS and
  extable scope
- [Low] arm64: confirm ESR_ELx.SET == 0b00 encoding in
  do_apei_claim_sea()
- [Low] arm64: skipping 9997 fallback for MOPS faults may break usercopy
  progress
- [Low] arm64: do_apei_claim_sea() skips ARM64_HAS_RAS_EXTN check for
  RES0 SET
--

commit 50af6acf54c318c2deb67defca532465dfc1e14e
Author: Ruidong Tian <tianruidong@linux.alibaba.com>

arm64: enable recover from synchronous external abort in kernel context

This commit adds kernel-context recovery for EL1 synchronous external aborts
in do_sea() via a new EX_TYPE_KACCESS_SEA exception table type and
fixup_exception_me(), and annotates __arch_copy_to_user() kernel loads for
SEA recovery while bypassing the single-byte fallback on MOPS usercopy
faults.

> Take copy_from/to_user for example, If ld* triggers a memory error, even in
> kernel mode, only the associated process is affected. Killing the user
> process and isolating the corrupt page is a better choice.

[Severity: Low]
Does the commit message accurately reflect the SIGBUS and extable behavior?

With commit "ACPI: APEI: GHES: use exception context to gate SIGBUS on poison
consumption", GHES_CTX_KERNEL suppresses MF_ACTION_REQUIRED and
force_sig(SIGBUS), so the user process is not killed and instead receives an
error return such as -EFAULT.  The commit message also describes kernel SEA
recovery as limited to EX_TYPE_KACCESS_SEA and EX_TYPE_UACCESS_CPY source
reads, omitting that fixup_exception_me() unconditionally recovers all
EX_TYPE_ACCESS_ERR_ZERO entries.

> diff --git a/arch/arm64/lib/copy_from_user.S b/arch/arm64/lib/copy_from_user.S
> index 400057d607ecd..04f152ff5b033 100644
> --- a/arch/arm64/lib/copy_from_user.S
> +++ b/arch/arm64/lib/copy_from_user.S
> @@ -54,7 +54,7 @@
>  
>  	.macro cpy1 dst, src, count
>  	.arch_extension mops
> -	USER_CPY(9997f, 0, cpyfprt [\dst]!, [\src]!, \count!)
> +	USER_CPY(9998f, 0, cpyfprt [\dst]!, [\src]!, \count!)
>  	USER_CPY(9996f, 0, cpyfmrt [\dst]!, [\src]!, \count!)
>  	USER_CPY(9996f, 0, cpyfert [\dst]!, [\src]!, \count!)
>  	.endm
> @@ -69,9 +69,11 @@ SYM_FUNC_START(__arch_copy_from_user)
>  	ret
>  
>  	// Exception fixups
> -9996:	b.cs	9997f
> +	// MOPS faults skip the zero-progress retry to avoid repeating an SEA.
> +9996:	b.cs	9998f
>  	// Registers are in Option A format
>  	add	dst, dst, count
> +	b	9998f

[Severity: Low]
This question rests on an arm64 architecture detail that the review could not
confirm from the kernel source: can FEAT_MOPS unprivileged copy instructions
(cpyfprt/cpyfmrt/cpyfert and cpyfpwt/cpyfmwt/cpyfewt) take a synchronous data
abort with zero reported forward progress (leaving dst == dstin after Option
A/B register fixup) when a single-byte ldtrb/sttrb at byte 0 would succeed,
such as on an MTE tag mismatch in a later granule or unaligned Device memory?

If the answer is yes, bypassing label 9997 for all MOPS faults (here and in
arch/arm64/lib/copy_to_user.S) breaks the forward-progress contract that
295cf156231c ("arm64: Avoid premature usercopy failure") added 9997 to
uphold.  In generic_perform_write() and iomap_write_iter(),
fault_in_readable() only probes byte 0 of each page, so it would succeed and
spin the write loop until a fatal signal arrives; copy_to_user() callers such
as filemap_read() would return -EFAULT instead of a short read.  Bypassing
9997 only on SEA fixups would avoid repeating an SEA without changing
ordinary usercopy faults.

> diff --git a/arch/arm64/lib/copy_to_user.S b/arch/arm64/lib/copy_to_user.S
> index 819f2e3fc7a93..451bfeb0143b4 100644
> --- a/arch/arm64/lib/copy_to_user.S
> +++ b/arch/arm64/lib/copy_to_user.S
> @@ -20,7 +20,7 @@
>   *	x0 - bytes not copied
>   */
>  	.macro ldrb1 reg, ptr, val
> -	ldrb  \reg, [\ptr], \val
> +	KERNEL_SEA(9998f, ldrb  \reg, [\ptr], \val)
>  	.endm

[Severity: Medium]
Does annotating regular __arch_copy_to_user() kernel loads with KERNEL_SEA
(and allowing kernel-source reads in ex_handler_uaccess_cpy()) livelock
callers that treat a short copy as an unmapped user page?

In read_kcore_iter() for KCORE_VMALLOC, a while (true) loop with no signal
check calls vread_iter() -> aligned_vread_iter() ->
copy_page_to_iter_nofault() -> __arch_copy_to_user().  When the kernel load
hits a poisoned vmalloc page, __arch_copy_to_user() returns a full residual
(0 bytes copied), fault_in_iov_iter_writeable() succeeds because the user
buffer is writable, and the loop spins forever re-triggering SEAs (commit
"ACPI: APEI: GHES: use exception context to gate SIGBUS on poison
consumption" skips duplicate-SEA -ECANCELED cancellation for
GHES_CTX_KERNEL).

This also causes filemap_read() to report a poisoned page-cache page as
-EFAULT instead of -EIO, and btrfs copy_to_sk() to retry on poisoned extent
buffers.  Restricting kernel-source SEA recovery to an opt-in
copy_mc_to_user() path, as x86 and powerpc do, would avoid conflating kernel
memory errors with user page faults.

> diff --git a/arch/arm64/mm/extable.c b/arch/arm64/mm/extable.c
> index 402c5db692ffe..5ea013a3247c3 100644
> --- a/arch/arm64/mm/extable.c
> +++ b/arch/arm64/mm/extable.c

[ ... ]

> @@ -55,10 +58,14 @@ static bool ex_handler_access_err_zero(const struct exception_table_entry *ex,
>  }
>  
>  static bool ex_handler_uaccess_cpy(const struct exception_table_entry *ex,
> -				   struct pt_regs *regs, unsigned long esr)
> +				   struct pt_regs *regs, unsigned long esr,
> +				   bool allow_kernel_source)
>  {
> -	/* Do not fix up faults on kernel memory accesses */
> -	if (!cpy_faulted_on_uaccess(ex, esr))
> +	/* Only SEA may also accept copy_to_user's kernel source read. */
> +	if (!cpy_faulted_on_uaccess(ex, esr) &&
> +	    !(allow_kernel_source &&
> +	      FIELD_GET(EX_DATA_UACCESS_WRITE, ex->data) &&
> +	      !(esr & ESR_ELx_WNR)))
>  		return false;

[Severity: Low]
This question rests on an arm64 architecture detail that the review could not
confirm from the kernel source: for an EL1 synchronous external abort with
DFSC == ESR_ELx_FSC_EXTABT (especially on a FEAT_MOPS CPY* instruction or
when ESR_ELx_FnV is set), can a write-triggered SEA report ESR_ELx.WnR == 0
or UNKNOWN?

If the answer is yes, cpy_faulted_on_uaccess() would treat a
kernel-destination write SEA in __arch_copy_from_user() (where
EX_DATA_UACCESS_WRITE is 0) as a user read and recover it, and the
ESR_ELx_WNR check on EX_TYPE_KACCESS_SEA in fixup_exception_me() would
likewise fail to reject a write SEA.

[ ... ]

> +bool fixup_exception_me(struct pt_regs *regs, unsigned long esr)
> +{
> +	const struct exception_table_entry *ex;
> +
> +	ex = search_exception_tables(instruction_pointer(regs));
> +	if (!ex)
> +		return false;
> +
> +	switch (ex->type) {
> +	case EX_TYPE_ACCESS_ERR_ZERO:
> +		return ex_handler_access_err_zero(ex, regs);

[Severity: High]
Can unconditionally recovering EX_TYPE_ACCESS_ERR_ZERO entries here livelock
futex retry loops on a poisoned user page?

All arm64 futex atomics in arch/arm64/include/asm/futex.h emit
EX_TYPE_ACCESS_ERR_ZERO.  When one hits an SEA in EL1, GHES_CTX_KERNEL queues
memory_failure_queue() asynchronously on the current CPU via
schedule_work_on(smp_processor_id(), &mf_cpu->work) and suppresses
force_sig(SIGBUS), while fixup_exception_me() returns -EFAULT.

In futex_lock_pi() and handle_futex_death(), -EFAULT calls
fault_in_user_writeable(), which sees the PTE still mapped and returns 0, and
immediately retries with no cond_resched() or fatal_signal_pending() check.
Under CONFIG_PREEMPT_NONE this starves the queued memory_failure work on the
same CPU; under any preemption model, if memory_failure is disabled or cannot
unmap the page, futex_lock_pi(), handle_futex_death(), futex_wake_op(), and
futex_requeue() loop indefinitely because GHES_CTX_KERNEL skips
duplicate-SEA -ECANCELED cancellation.

[Severity: Medium]
Does handling EX_TYPE_ACCESS_ERR_ZERO unconditionally also recover kernel
writes, user writes, and cache maintenance instructions?

After commit "arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO
into ACCESS_ERR_ZERO", EX_TYPE_ACCESS_ERR_ZERO covers __put_kernel_nofault(),
user stores and atomics (such as put_user(), clear_user(), and sttr* in
copy_to_user()), and cache maintenance operations in
caches_clean_inval_user_pou().  Because fixup_exception_me() checks neither
ESR_ELx_WNR nor ESR_ELx_CM and cannot distinguish kernel from user entries,
it silently turns SEAs on kernel destination writes and cache maintenance
into -EFAULT.

[Severity: Medium]
Does this EX_TYPE_ACCESS_ERR_ZERO fixup also turn SEAs during trapped EL0
emulation into SIGSEGV instead of SIGBUS?

Both user_cache_maint_handler() and compat swp_handler() annotate their user
accesses with _ASM_EXTABLE_UACCESS_ERR and call arm64_notify_segfault() when
the access returns -EFAULT.  Because do_apei_claim_sea() does not exclude
ESR_ELx_CM and GHES_CTX_KERNEL suppresses SIGBUS, an SEA on either path is
reported to userspace as SIGSEGV.

> diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> index b22736853fcd8..d8ff65c86728d 100644
> --- a/arch/arm64/mm/fault.c
> +++ b/arch/arm64/mm/fault.c
> @@ -874,21 +874,43 @@ static int do_bad(unsigned long far, unsigned long esr, struct pt_regs *regs)
>  	return 1; /* "fault" */
>  }
>  
> +/*
> + * APEI claimed this as a firmware-first notification.
> + * Some processing deferred to task_work before ret_to_user().
> + */
> +static int do_apei_claim_sea(struct pt_regs *regs, unsigned long esr)
> +{
> +	int ret;
> +
> +	ret = apei_claim_sea(regs);
> +	if (ret)
> +		return ret;
> +
> +	if (user_mode(regs))
> +		return 0;

[Severity: Medium]
Can calling apei_claim_sea() unconditionally on every EL1 SEA self-deadlock
on ghes_notify_lock_sea if a nested EL1 SEA occurs?

DAIF_ERRCTX cannot mask synchronous external aborts, and unlike
ghes_notify_nmi(), ghes_notify_sea() has no re-entrancy guard:

do_sea()
  do_apei_claim_sea()
    apei_claim_sea()
      local_daif_restore(DAIF_ERRCTX)
      nmi_enter()
      ghes_notify_sea()
        raw_spin_lock(&ghes_notify_lock_sea)
        ghes_in_nmi_spool_from_list()
          <nested EL1 SEA>
            do_sea()
              do_apei_claim_sea()
                apei_claim_sea()
                  ghes_notify_sea()
                    raw_spin_lock(&ghes_notify_lock_sea)  <-- spins forever

If a nested EL1 SEA hits before ghes_clear_estatus() clears block_status,
this CPU deadlocks on ghes_notify_lock_sea (blocking any other CPU that later
takes an SEA).  If the read inside ghes_has_active_errors() itself triggers
an SEA, it recurses until stack overflow.

[Severity: Medium]
Can do_apei_claim_sea() recover a kernel SEA even when GHES did not isolate
the failing page or process a memory error?

ghes_in_nmi_queue_one_entry() returns 0 for any active non-panic SEA status
block regardless of whether ghes_do_proc() queues memory_failure() (for
example, a CPER record without CPER_MEM_VALID_PA, a non-memory section, or a
kernel with CONFIG_ACPI_APEI_MEMORY_FAILURE=n).  Since commit "ACPI: APEI:
GHES: use exception context to gate SIGBUS on poison consumption" also
disabled the unqueued SIGBUS fallback for GHES_CTX_KERNEL, matching
fixup_exception_me() silently converts an unisolated hardware error into
-EFAULT without queuing memory_failure(), delivering SIGBUS, or setting
TAINT_MACHINE_CHECK.

> +	/* A GHES claim alone does not make a kernel exception safe to skip. */
> +	if (!is_el1_data_abort(esr) ||
> +	    (esr & ESR_ELx_FSC) != ESR_ELx_FSC_EXTABT ||
> +	    (esr & ESR_ELx_S1PTW) ||
> +	    (esr & ESR_ELx_SET_MASK))
> +		return -ENOENT;

[Severity: Low]
This question rests on an arm64 architecture detail that the review could not
confirm from the kernel source: on FEAT_RAS systems (including RASv1p1 and
RASv2), can ESR_ELx.SET == 0b00 for an EL1 data abort with DFSC ==
ESR_ELx_FSC_EXTABT ever encode an uncontainable or non-UER error state rather
than UER?

arch/arm64/include/asm/esr.h only defines ESR_ELx_SET_SHIFT and
ESR_ELx_SET_MASK without named ESR_ELx_SET_* encodings.  If 0b00 can encode a
non-UER error, checking (esr & ESR_ELx_SET_MASK) == 0 would skip a kernel
instruction on an uncontainable SEA instead of panicking (and reject valid
UER aborts if UER uses a non-zero encoding).

[Severity: Low]
This question rests on an arm64 architecture detail that the review could not
confirm from the kernel source: on a CPU without FEAT_RAS (where
ARM64_HAS_RAS_EXTN is absent and ESR_ELx.SET is RES0, as noted in
kvm_handle_guest_sea()), can hardware deliver an EL1 synchronous external
data abort with ESR_ELx_FSC_EXTABT and S1PTW == 0 for an uncontainable or
unrecoverable error?

If the answer is yes, because do_apei_claim_sea() does not check
ARM64_HAS_RAS_EXTN, (esr & ESR_ELx_SET_MASK) always evaluates to 0 on non-RAS
CPUs, and apei_claim_sea() returns 0 for any non-panic GHES record, so
fixup_exception_me() would skip the faulting kernel instruction and continue
running after an uncontained hardware error instead of calling
arm64_notify_die().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012435.1406255-1-jiangtianfeng@linux.alibaba.com?part=4


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v16 6/9] arm64: support copy_mc_[user]_highpage()
  2026-12-05  1:24 ` [PATCH v16 6/9] arm64: support copy_mc_[user]_highpage() Jiangtian Feng
@ 2026-12-06  1:26   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-06  1:26 UTC (permalink / raw)
  To: Jiangtian Feng, catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, gli, der, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao, linux-mm,
	kasan-dev, x86

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] arm64: missing copy_mc_to_user() causes usercopy_abort() BUG()
  on FS-DAX
- [Medium] arm64: missing copy_mc_to_user() defeats DAX
  HARDENED_USERCOPY bypass
- [Medium] arm64: missing KASAN tag reset for hugetlb tail pages in
  copy_mc_highpage()
- [Low] arm64: unconfirmed ESR_ELx.WnR semantics for SEA on MOPS cpy and
  MTE ldgm
- [Low] arm64: copy_mc_highpage() duplicates KASAN and MTE logic of
  copy_highpage()
- [Low] arm64: unnecessary __pi_copy_mc_page symbol alias in
  copy_mc_page.S
--

commit 94594e11edb86f9c462d47449731063afc1c8dca
Author: Tong Tiangen <tongtiangen@huawei.com>

arm64: support copy_mc_[user]_highpage()

This commit adds machine-check safe page copying helpers copy_mc_page(),
copy_mc_highpage(), and copy_mc_user_highpage() on arm64, along with MTE tag
copying in mte_copy_mc_page_tags(), and selects ARCH_HAS_COPY_MC when
ACPI_APEI_SEA is enabled.

> diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
> index 9b08b2457a555..0c017bdc77594 100644
> --- a/arch/arm64/Kconfig
> +++ b/arch/arm64/Kconfig
> @@ -21,6 +21,7 @@ config ARM64
>  	select ARCH_HAS_CACHE_LINE_SIZE
>  	select ARCH_HAS_CC_PLATFORM
>  	select ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
> +	select ARCH_HAS_COPY_MC if ACPI_APEI_SEA
>  	select ARCH_HAS_CURRENT_STACK_POINTER
>  	select ARCH_HAS_DEBUG_VIRTUAL
>  	select ARCH_HAS_DEBUG_VM_PGTABLE

[Severity: High]
Can selecting ARCH_HAS_COPY_MC without defining an arm64 copy_mc_to_user()
trigger usercopy_abort() BUG() on FS-DAX reads?

With CONFIG_ARCH_HAS_COPY_MC enabled, _copy_mc_to_iter() switches from
_copy_to_iter() (which calls raw_copy_to_user()) to copy_to_user_iter_mc().
Without an arm64 copy_mc_to_user(), that calls the generic fallback
copy_mc_to_user() -> copy_to_user() -> __check_object_size() ->
check_heap_object().

When a user mmap()s an FS-DAX file with a PMD fault, dax_folio_init()
creates a PageCompound() folio of PMD_SIZE.  A subsequent read() spanning
past that folio boundary on a linear-mapped pmem range tracked in
memblock.memory (where arch_add_memory() cleared MEMBLOCK_NOMAP so
virt_addr_valid() is true) passes the full extent length to
dax_copy_to_iter(), tripping usercopy_abort() -> BUG() under
CONFIG_HARDENED_USERCOPY while holding inode->i_rwsem and dax_read_lock().

[Severity: Medium]
Does falling back to generic copy_mc_to_user() also defeat the
HARDENED_USERCOPY bypass on the DAX read hot path?

dax_copy_to_iter() uses _copy_mc_to_iter() on DAXDEV_NOMC devices, and
copy_to_user_iter_mc() already performs access_ok() and
instrument_copy_to_user().  Routing through the generic copy_mc_to_user()
-> copy_to_user() adds __check_object_size() (with a memblock_is_map_memory()
lookup) and repeats might_fault(), access_ok(), and instrument_copy_to_user()
on every read segment.

Since __arch_copy_to_user() already carries KERNEL_SEA() fixups, defining an
arm64 copy_mc_to_user() around raw_copy_to_user() would preserve SEA recovery
without the extra overhead or the usercopy_abort() BUG() above.

[ ... ]

> diff --git a/arch/arm64/lib/copy_mc_page.S b/arch/arm64/lib/copy_mc_page.S
> new file mode 100644
> index 0000000000000..542a083e2fd88
> --- /dev/null
> +++ b/arch/arm64/lib/copy_mc_page.S

[ ... ]

> +	.macro cpy1 dst, src, count
> +	.arch_extension mops
> +	KERNEL_SEA(9998f, cpypwn [\dst]!, [\src]!, \count!)
> +	KERNEL_SEA(9998f, cpymwn [\dst]!, [\src]!, \count!)
> +	KERNEL_SEA(9998f, cpyewn [\dst]!, [\src]!, \count!)
> +	.endm

[Severity: Low]
This question rests on an arm64 architectural detail that the review could
not confirm from the kernel source: when an EL1 synchronous external abort
(ESR_ELx_FSC_EXTABT, S1PTW=0, SET=0) occurs on a destination write of
FEAT_MOPS cpypwn/cpymwn/cpyewn, can hardware report ESR_ELx.WnR as 0?
Similarly, in mte_copy_mc_page_tags() (arch/arm64/lib/mte.S), can an SEA on
ldgm ever report ESR_ELx.WnR as 1?

fixup_exception_me() accepts EX_TYPE_KACCESS_SEA solely when ESR_ELx_WNR is
0.  If a destination-write SEA on cpy* reported WnR as 0, copy_mc_page()
would return -EFAULT, causing copy_mc_highpage() to poison-queue the healthy
source page while callers free the corrupted destination page instead of
panicking.  If an SEA on ldgm reported WnR as 1, fixup_exception_me() would
reject the fixup and do_sea() would panic instead of recovering.

> +
> +SYM_FUNC_START(__pi_copy_mc_page)
> +#include "copy_page_template.S"
> +
> +	mov x0, #0
> +	ret
> +
> +9998:	mov x0, #-EFAULT
> +	ret
> +
> +SYM_FUNC_END(__pi_copy_mc_page)
> +SYM_FUNC_ALIAS(copy_mc_page, __pi_copy_mc_page)
> +EXPORT_SYMBOL(copy_mc_page)

[Severity: Low]
Is the __pi_copy_mc_page name and alias needed here?

Unlike __pi_copy_page, __pi_copy_mc_page has no PI or KVM nVHE callers, and
its EL1 KERNEL_SEA() __ex_table fixups are not usable in early boot or KVM
nVHE context.  Other extable-using routines in arch/arm64/lib/ (such as
copy_from_user.S, copy_to_user.S, and mte.S) define their public symbol
directly with SYM_FUNC_START().

[ ... ]

> diff --git a/arch/arm64/mm/copypage.c b/arch/arm64/mm/copypage.c
> index cd5912ba617b7..611024fca6d45 100644
> --- a/arch/arm64/mm/copypage.c
> +++ b/arch/arm64/mm/copypage.c
> @@ -72,3 +72,88 @@ void copy_user_highpage(struct page *to, struct page *from,
>  	flush_dcache_page(to);
>  }
>  EXPORT_SYMBOL_GPL(copy_user_highpage);
> +
> +#ifdef CONFIG_ARCH_HAS_COPY_MC
> +/*
> + * Return -EFAULT if anything goes wrong while copying page or mte.
> + */
> +int copy_mc_highpage(struct page *to, struct page *from)

[Severity: Low]
This isn't a bug, but copy_mc_highpage() duplicates the KASAN tag reset and
hugetlb/base-page MTE tag-copying logic from copy_highpage() directly above
it.  Factoring the shared body into a static __always_inline helper taking a
compile-time constant bool mc would avoid future divergence without adding
overhead to copy_highpage().

[ ... ]

> +	if (kasan_hw_tags_enabled())
> +		page_kasan_tag_reset(to);
> +
> +	if (!system_supports_mte())
> +		return 0;
> +
> +	if (folio_test_hugetlb(src)) {
> +		if (!folio_test_hugetlb_mte_tagged(src) ||
> +		    from != folio_page(src, 0))
> +			return 0;
> +
> +		folio_try_hugetlb_mte_tagging(dst);
> +
> +		/*
> +		 * Populate tags for all subpages.
> +		 *
> +		 * Don't assume the first page is head page since
> +		 * huge page copy may start from any subpage.
> +		 */
> +		nr_pages = folio_nr_pages(src);
> +		for (i = 0; i < nr_pages; i++) {
> +			kfrom = page_address(folio_page(src, i));
> +			kto = page_address(folio_page(dst, i));
> +			ret = mte_copy_mc_page_tags(kto, kfrom);
> +			if (ret) {
> +				memory_failure_queue(page_to_pfn(folio_page(src, i)), 0);
> +				return -EFAULT;
> +			}
> +		}
> +		folio_set_hugetlb_mte_tagged(dst);

[Severity: Medium]
Can overwriting hardware MTE tags on hugetlb tail pages without resetting
their KASAN tags trigger spurious KASAN tag check faults under
CONFIG_KASAN_HW_TAGS?

copy_mc_highpage() only resets the KASAN tag on subpage 0, while
mte_copy_mc_page_tags() overwrites hardware MTE tags across all subpages
dst[0..nr_pages-1].  When a hugetlb folio is allocated via GFP_HIGHUSER
(for example gigantic pages with default movable_gigantic_pages=0, or any
hugepage when CONFIG_MIGRATION=n), __GFP_SKIP_KASAN is omitted and every
subpage gets a random non-match-all KASAN tag.

On the normal path, the next subpage copy accesses dst[1] via page_address()
with the stale KASAN tag against the overwritten user MTE tags.  On the new
SEA error path, returning -EFAULT mid-loop leaves dst[1..i-1] mismatched,
and free_huge_folio() -> arch_clear_hugetlb_flags() returns the folio to the
hugetlb pool without resetting per-subpage KASAN tags, causing tag check
faults on later reuse.  Calling page_kasan_tag_reset(folio_page(dst, i))
before mte_copy_mc_page_tags() when kasan_hw_tags_enabled() would prevent
this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012435.1406255-1-jiangtianfeng@linux.alibaba.com?part=6


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v16 7/9] arm64: introduce copy_mc_to_kernel() implementation
  2026-12-05  1:24 ` [PATCH v16 7/9] arm64: introduce copy_mc_to_kernel() implementation Jiangtian Feng
@ 2026-12-06  1:26   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-06  1:26 UTC (permalink / raw)
  To: Jiangtian Feng, catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, gli, der, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao, linux-mm,
	kasan-dev, x86

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Low] arm64: copy_mc_to_kernel() omits KCSAN instrumentation
- [Low] arm64: unconfirmed privileged MOPS SEA progress in __memcpy_mc()
- [Low] arm64: unconfirmed ESR_EL1.WnR for MOPS SEAs in
  fixup_exception_me()
- [Low] arm64: copy_mc_to_kernel() skips instrument_memcpy_before() for
  KCSAN/KASAN
--

commit 6b62a64a134e02b524513d98d82bc0317b32a3bb
Author: Tong Tiangen <tongtiangen@huawei.com>

arm64: introduce copy_mc_to_kernel() implementation

This commit implements copy_mc_to_kernel() and memcpy_mc() for arm64
using the forward copy template with Synchronous External Abort (SEA)
fixups, along with a KASAN wrapper in mm/kasan/shadow.c.

> diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h
> index 9f5bd9c69c249..734dde180a5a9 100644
> --- a/arch/arm64/include/asm/uaccess.h
> +++ b/arch/arm64/include/asm/uaccess.h
> @@ -498,5 +498,22 @@ static inline size_t probe_subpage_writeable(const char __user *uaddr,
>  }
>  
>  #endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */
> +#ifdef CONFIG_ARCH_HAS_COPY_MC
> +/**
> + * copy_mc_to_kernel - memory copy that handles source exceptions
> + *
> + * @to:		destination address
> + * @from:	source address
> + * @size:	number of bytes to copy
> + *
> + * Return 0 for success, or bytes not copied.
> + */
> +static inline unsigned long __must_check
> +copy_mc_to_kernel(void *to, const void *from, unsigned long size)
> +{
> +	return memcpy_mc(to, from, size);
> +}
> +#define copy_mc_to_kernel copy_mc_to_kernel
> +#endif

[Severity: Low]
Does copy_mc_to_kernel() lose KCSAN instrumentation when
CONFIG_ARCH_HAS_COPY_MC and CONFIG_KCSAN are both enabled?

Previously arm64 used the generic fallback in include/linux/uaccess.h,
which calls memcpy() and is intercepted by KCSAN via __tsan_memcpy().
Because memcpy_mc() is an assembly routine rather than a builtin,
-fsanitize=thread does not instrument calls to it.

As a result, data races in copy_mc_to_kernel() callers such as pmem or
dax reads go undetected under KCSAN.  Calling
instrument_memcpy_before() here, as arch/x86/lib/copy_mc.c does, would
preserve KCSAN coverage.

> diff --git a/arch/arm64/lib/memcpy_mc.S b/arch/arm64/lib/memcpy_mc.S
> new file mode 100644
> index 0000000000000..c7a217808feeb
> --- /dev/null
> +++ b/arch/arm64/lib/memcpy_mc.S

[ ... ]

> +	.macro cpy1 dst, src, count
> +	.arch_extension mops
> +	KERNEL_SEA(9997f, cpyfp [\dst]!, [\src]!, \count!)
> +	KERNEL_SEA(9996f, cpyfm [\dst]!, [\src]!, \count!)
> +	KERNEL_SEA(9996f, cpyfe [\dst]!, [\src]!, \count!)
> +	.endm

[Severity: Low]
For a Synchronous External Abort taken during privileged cpyfp, cpyfm,
or cpyfe, can ESR_EL1.WnR be 0 or unknown on a destination-write abort,
or 1 on a source-read abort?

This rests on an arm64 architecture detail that could not be confirmed
from the kernel source.  fixup_exception_me() in
arch/arm64/mm/extable.c handles EX_TYPE_KACCESS_SEA solely by testing
ESR_ELx_WNR.

If a destination-write SEA can report WnR as 0, fixup_exception_me()
will treat an unrecoverable destination-write error as a short copy
instead of failing; if a source-read SEA can report WnR as 1, it will
reject recovery and trigger an unnecessary panic.

> +
> +end	.req	x5
> +SYM_FUNC_START(__memcpy_mc)
> +	add	end, x0, x2
> +#include "copy_template.S"
> +	mov	x0, #0
> +	ret
> +
> +	// Exception fixups
> +9996:	b.cs	9997f
> +	// Registers are in Option A format
> +	add	dst, dst, count
> +9997:	sub	x0, end, dst			// bytes not copied
> +	ret
> +SYM_FUNC_END(__memcpy_mc)

[Severity: Low]
On a Synchronous External Abort during privileged cpyfm or cpyfe on a
source read, can destination bytes before the reported dst remain
unwritten, or can cpyfp take an SEA after transitioning dst into
Option A format?

This rests on an arm64 architecture detail that could not be confirmed
from the kernel source.  Commit fe59e0358d9b ("arm64: lib: Use MOPS for
usercopy routines") documents the fault-progress guarantee only for
unprivileged CPY variants, not for privileged cpyfp/cpyfm/cpyfe on SEA.

If bytes before dst can remain unwritten, sub x0, end, dst under-reports
the residual and callers such as _copy_mc_to_iter() will treat unwritten
destination bytes as copied.  If cpyfp can fault in Option A format,
branching straight to 9997 skips the Option A adjustment and returns 0.

> diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c
> index d286e0a045437..da21a13151b9e 100644
> --- a/mm/kasan/shadow.c
> +++ b/mm/kasan/shadow.c
> @@ -79,6 +79,18 @@ void *memcpy(void *dest, const void *src, size_t len)
>  }
>  #endif
>  
> +#ifdef __HAVE_ARCH_MEMCPY_MC
> +#undef memcpy_mc
> +unsigned long memcpy_mc(void *dest, const void *src, size_t len)
> +{
> +	if (!kasan_check_range(src, len, false, _RET_IP_) ||
> +	    !kasan_check_range(dest, len, true, _RET_IP_))
> +		return len;
> +
> +	return __memcpy_mc(dest, src, len);
> +}
> +#endif

[Severity: Low]
Should memcpy_mc() instrumentation use instrument_memcpy_before() and
instrument_memcpy_after() from include/linux/instrumented.h instead of
adding an arm64-only __HAVE_ARCH_MEMCPY_MC override to
mm/kasan/shadow.c?

This override sits in generic KASAN code outside the legacy
!CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX && !CONFIG_GENERIC_ENTRY guard,
and because CONFIG_KCSAN depends on !KASAN, mm/kasan/shadow.c is not
built under KCSAN, leaving memcpy_mc() uninstrumented there.

Wrapping __memcpy_mc() with instrument_memcpy_before() and
instrument_memcpy_after() in arm64 code, as commit 61b258b0d2f6 ("x86:
call instrumentation hooks from copy_mc.c") does, would cover KASAN,
KCSAN, and KMSAN together without touching mm/kasan/shadow.c.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012435.1406255-1-jiangtianfeng@linux.alibaba.com?part=7


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v16 8/9] lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests
  2026-12-05  1:24 ` [PATCH v16 8/9] lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests Jiangtian Feng
@ 2026-12-06  1:26   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-06  1:26 UTC (permalink / raw)
  To: Jiangtian Feng, catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, gli, der, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao, linux-mm,
	kasan-dev, x86

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] lib/tests: static 4-page BSS and redundant page_pattern in
  memcpy_kunit
- [Low] lib/tests: init_page() rejects 0x00 edges instead of 0xA5
  destination fill
--

commit 690bd24ed06124a93662ef175f347a2c4a928eae
Author: Ruidong Tian <tianruidong@linux.alibaba.com>

lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests

This commit adds KUnit test cases for copy_page() and copy_mc_page() in
lib/tests/memcpy_kunit.c, using static page-aligned buffers to verify
page copy accuracy and guard against overflow into an adjacent page.

> diff --git a/lib/tests/memcpy_kunit.c b/lib/tests/memcpy_kunit.c
> index d36933554e464..8bcaaf9ab1ea7 100644
> --- a/lib/tests/memcpy_kunit.c
> +++ b/lib/tests/memcpy_kunit.c
> @@ -493,6 +493,67 @@ static void memmove_overlap_test(struct kunit *test)
>  	}
>  }
>  
> +/* --- Page-sized copy tests --- */
> +
> +static u8 page_src[PAGE_SIZE] __aligned(PAGE_SIZE);
> +static u8 page_dst[PAGE_SIZE * 2] __aligned(PAGE_SIZE);
> +static u8 page_pattern[PAGE_SIZE] __aligned(PAGE_SIZE);

[Severity: Low]
Should page_src and page_dst be allocated dynamically for the test instead
of reserving 4 * PAGE_SIZE in static .bss?  When CONFIG_MEMCPY_KUNIT_TEST
is built in (such as with CONFIG_KUNIT_ALL_TESTS=y), this pins up to
256 KiB on 64K-page kernels permanently after the test finishes.

Allocating pages dynamically (for example with alloc_pages() or
__get_free_pages()) also exercises direct-mapped memory rather than module
vmalloc BSS, where implementations such as MIPS copy_page() bypass their
primary DMA path and fall back to copy_page_cpu().

Also, page_pattern is redundant: checking the adjacent page with
memchr_inv(page_dst + PAGE_SIZE, 0xA5, PAGE_SIZE) removes both the buffer
and its memset().

> +
> +static void init_page(struct kunit *test)
> +{
> +	/* Get many bit patterns. */
> +	get_random_bytes(page_src, PAGE_SIZE);
> +
> +	/* Make sure we have non-zero edges. */
> +	set_random_nonzero(test, &page_src[0]);
> +	set_random_nonzero(test, &page_src[PAGE_SIZE - 1]);
> +
> +	memset(page_dst, 0xA5, ARRAY_SIZE(page_dst));
> +	memset(page_pattern, 0xA5, PAGE_SIZE);
> +}

[Severity: Low]
Can page_src[0] or page_src[PAGE_SIZE - 1] still match the 0xA5 fill in
page_dst?

set_random_nonzero() only rejects 0x00, which works for init_large()
because large_dst is zero-filled.  Since page_dst is filled with 0xA5, an
edge byte of 0xA5 would let an off-by-one copy omission pass memcmp().

Ensuring the edge bytes differ from 0xA5 (or zero-filling page_dst) would
avoid that gap.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012435.1406255-1-jiangtianfeng@linux.alibaba.com?part=8


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v16 9/9] lib/tests: memcpy_kunit: add memcpy_mc() and memcpy_mc_large() test
  2026-12-05  1:24 ` [PATCH v16 9/9] lib/tests: memcpy_kunit: add memcpy_mc() and memcpy_mc_large() test Jiangtian Feng
@ 2026-12-06  1:26   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-06  1:26 UTC (permalink / raw)
  To: Jiangtian Feng, catalin.marinas, will, akpm, rafael
  Cc: mark.rutland, james.morse, robin.murphy, nikumbh.adi, tony.luck,
	guohanjun, mchehab, lenb, xueshuai, tongtiangen, tianruidong,
	tongweilin, baolin.wang, ashok.raj, jonathan.cameron, david, liam,
	vbabka, rppt, mhocko, ljs, surenb, shivankg, wangkefeng.wang,
	xiangzao, ying.huang, linmiaohe, nao.horiguchi, ziy, tujinjiang,
	mgorman, kaitao.cheng, chengkaitao, muchun.song, nico.pache,
	ryan.roberts, dev.jain, baohua, lance.yang, usama.arif, kas, kees,
	andreyknvl, dvyukov, gli, der, ryabinin.a.a, vincenzo.frascino,
	tglx, mingo, bp, dave.hansen, hpa, maddy, mpe, npiggin, chleroy,
	ritesh.list, sshegde, aneesh.kumar, naveen.n.rao, linux-mm,
	kasan-dev, x86

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Low] lib/tests: memcpy_mc_large_test() truncates unsigned long return
  value to int
- [Low] lib/tests: use generic copy_mc_to_kernel() instead of arm64-only
  memcpy_mc()
- [Low] lib/tests: memcpy_mc_large_test() skips unaligned-src paths in
  copy_template.S
- [Low] lib/tests: duplicate copy_large_test() and memcpy_test() in
  memcpy_mc tests
--

commit 5d6144c729bba5b735c9853da3f6e1e7481d3b4a
Author: Ruidong Tian <tianruidong@linux.alibaba.com>

lib/tests: memcpy_kunit: add memcpy_mc() and memcpy_mc_large() test

This patch adds KUnit test cases memcpy_mc_test() and memcpy_mc_large_test()
to lib/tests/memcpy_kunit.c, guarded by __HAVE_ARCH_MEMCPY_MC.

> diff --git a/lib/tests/memcpy_kunit.c b/lib/tests/memcpy_kunit.c
> index 8bcaaf9ab1ea7..479fc9b880b3c 100644
> --- a/lib/tests/memcpy_kunit.c
> +++ b/lib/tests/memcpy_kunit.c
> @@ -554,6 +554,121 @@ static void copy_mc_page_test(struct kunit *test)
>  }
>  #endif /* __HAVE_ARCH_COPY_MC_PAGE */
>  
> +#ifdef __HAVE_ARCH_MEMCPY_MC

[Severity: Low]
Should these tests call the generic copy_mc_to_kernel() API under
CONFIG_ARCH_HAS_COPY_MC instead of gating on __HAVE_ARCH_MEMCPY_MC and
calling memcpy_mc() directly?

memcpy_mc() and __HAVE_ARCH_MEMCPY_MC only exist on arm64, where
copy_mc_to_kernel() is an inline wrapper around memcpy_mc().  Testing
copy_mc_to_kernel() exercises arm64 memcpy_mc() identically while also
covering x86 and powerpc.

[ ... ]

> +static void memcpy_mc_large_test(struct kunit *test)
> +{
> +	init_large(test);

[Severity: Low]
Can memcpy_mc_large_test() reuse copy_large_test() instead of duplicating the
sweep loop?

copy_large_test() already shares this 1..1024 byte sweep between
memcpy_large_test() and memmove_large_test(); extending it with an enum for
the copy operation would avoid copying the loop and cleanup code.  Likewise,
memcpy_mc_test() duplicates the struct some_bytes tables and the
static-initializer and direct-assignment checks from memcpy_test().

> +
> +	/* Sweep 1..1024 bytes x shifting offset to cover all template paths. */
> +	for (int bytes = 1; bytes <= ARRAY_SIZE(large_src); bytes++) {
> +		for (int offset = 0; offset < ARRAY_SIZE(large_src); offset++) {
> +			int right_zero_pos = offset + bytes;
> +			int right_zero_size = ARRAY_SIZE(large_dst) - right_zero_pos;
> +			int ret;
> +
> +			ret = memcpy_mc(large_dst + offset, large_src, bytes);

[Severity: Low]
Does keeping large_src at a fixed offset skip the unaligned-source paths in
arch/arm64/lib/copy_template.S?

__memcpy_mc() includes copy_template.S, whose alignment prologue aligns src
(not dst) modulo 16 before branching to .LSrcAligned.  Because large_src is a
fixed static array in .bss, at most one alignment residue is exercised, and
if large_src is 16-byte aligned the unaligned-source prologue is never run.
Sweeping source offsets across 0..15 would cover those paths.

> +			KUNIT_ASSERT_EQ_MSG(test, ret, 0,
> +				"memcpy_mc returned %d with size %d at offset %d",
> +				ret, bytes, offset);

[Severity: Low]
Does storing the return value of memcpy_mc() in int truncate its unsigned
long result?

On 64-bit architectures, narrowing unsigned long to a 32-bit signed int
before checking ret == 0 lets a non-zero upper 32 bits pass silently, and %d
misformats values with bit 31 set.  Changing ret to unsigned long and the
format specifier to %lu matches memcpy_mc() and memcpy_mc_test().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012435.1406255-1-jiangtianfeng@linux.alibaba.com?part=9


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2026-12-06  1:27 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  1:24 [PATCH v16 0/9] arm64: add ARCH_HAS_COPY_MC support Jiangtian Feng
2026-12-05  1:24 ` [PATCH v16 1/9] uaccess: add generic fallback version of copy_mc_to_user() Jiangtian Feng
2026-12-06  1:26   ` sashiko-bot
2026-12-05  1:24 ` [PATCH v16 2/9] ACPI: APEI: GHES: use exception context to gate SIGBUS on poison consumption Jiangtian Feng
2026-12-06  1:26   ` sashiko-bot
2026-12-05  1:24 ` [PATCH v16 3/9] arm64: extable: merge UACCESS_ERR_ZERO and KACCESS_ERR_ZERO into ACCESS_ERR_ZERO Jiangtian Feng
2026-12-06  1:26   ` sashiko-bot
2026-12-05  1:24 ` [PATCH v16 4/9] arm64: enable recover from synchronous external abort in kernel context Jiangtian Feng
2026-12-06  1:26   ` sashiko-bot
2026-12-05  1:24 ` [PATCH v16 5/9] mm/hwpoison: return -EFAULT when copy fail in copy_mc_[user]_highpage() Jiangtian Feng
2026-12-05  1:24 ` [PATCH v16 6/9] arm64: support copy_mc_[user]_highpage() Jiangtian Feng
2026-12-06  1:26   ` sashiko-bot
2026-12-05  1:24 ` [PATCH v16 7/9] arm64: introduce copy_mc_to_kernel() implementation Jiangtian Feng
2026-12-06  1:26   ` sashiko-bot
2026-12-05  1:24 ` [PATCH v16 8/9] lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests Jiangtian Feng
2026-12-06  1:26   ` sashiko-bot
2026-12-05  1:24 ` [PATCH v16 9/9] lib/tests: memcpy_kunit: add memcpy_mc() and memcpy_mc_large() test Jiangtian Feng
2026-12-06  1:26   ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.