[PATCH 3/3] mm/vma: eliminate mmap_action->error_hook, introduce error_filter

From: Lorenzo Stoakes

Date: Thu May 21 2026 - 12:35:20 EST


Rather than providing a hook, simplify things by providing the ability to
filter errors. This allows us to more carefully validate the value provided
and thus ensure only a valid error code is specified, and simplifies the
interface.

This way, we eliminate all hooks but mmap_prepare and allow only mmap
actions to be specified (which core mm controls).

This significantly improves robustness and eliminates any unnecessary code
duplication in driver mmap hooks.

We also update the /dev/mem logic (the only user) to use
mmap_action->error_filter instead.

Signed-off-by: Lorenzo Stoakes <ljs@xxxxxxxxxx>
---
drivers/char/mem.c | 8 +-------
include/linux/mm_types.h | 9 +++------
mm/util.c | 29 +++++++++++++++++++++--------
tools/testing/vma/include/dup.h | 9 +++------
4 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index a4297eb39887..11639d988e47 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -322,11 +322,6 @@ static const struct vm_operations_struct mmap_mem_ops = {
#endif
};

-static int mmap_filter_error(int err)
-{
- return -EAGAIN;
-}
-
static int mmap_mem_prepare(struct vm_area_desc *desc)
{
struct file *file = desc->file;
@@ -362,8 +357,7 @@ static int mmap_mem_prepare(struct vm_area_desc *desc)

/* Remap-pfn-range will mark the range with the I/O flag. */
mmap_action_remap_full(desc, desc->pgoff);
- /* We filter remap errors to -EAGAIN. */
- desc->action.error_hook = mmap_filter_error;
+ desc->action.error_filter = -EAGAIN;

return 0;
}
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 945c0a5386d6..8d1fb85e7684 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -844,13 +844,10 @@ struct mmap_action {
enum mmap_action_type type;

/*
- * If specified, this hook is invoked when an error occurred when
- * attempting the selected action.
- *
- * The hook can return an error code in order to filter the error, but
- * it is not valid to clear the error here.
+ * If non-zero, filter errors that arise from mmap actions such that we
+ * return error_filter instead. Only valid error codes may be specified.
*/
- int (*error_hook)(int err);
+ int error_filter;

/*
* This should be set in rare instances where the operation required
diff --git a/mm/util.c b/mm/util.c
index 1555aa1487b8..939ed0b0bb5d 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -1413,16 +1413,22 @@ static int mmap_action_finish(struct vm_area_struct *vma,
*/
len = vma_pages(vma) << PAGE_SHIFT;
do_munmap(current->mm, vma->vm_start, len, NULL);
- if (action->error_hook) {
- /* We may want to filter the error. */
- err = action->error_hook(err);
- /* The caller should not clear the error. */
- VM_WARN_ON_ONCE(!err);
- }
- return err;
+
+ return action->error_filter ?: err;
}

#ifdef CONFIG_MMU
+
+static int check_mmap_action(struct mmap_action *action)
+{
+ const unsigned long filter = action->error_filter;
+
+ if (WARN_ON_ONCE(filter && !IS_ERR_VALUE(filter)))
+ return -EINVAL;
+
+ return 0;
+}
+
/**
* mmap_action_prepare - Perform preparatory setup for an VMA descriptor
* action which need to be performed.
@@ -1432,7 +1438,14 @@ static int mmap_action_finish(struct vm_area_struct *vma,
*/
int mmap_action_prepare(struct vm_area_desc *desc)
{
- switch (desc->action.type) {
+ struct mmap_action *action = &desc->action;
+ int err;
+
+ err = check_mmap_action(action);
+ if (err)
+ return err;
+
+ switch (action->type) {
case MMAP_NOTHING:
return 0;
case MMAP_REMAP_PFN:
diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
index bf67a80a8332..4f7dd92075a3 100644
--- a/tools/testing/vma/include/dup.h
+++ b/tools/testing/vma/include/dup.h
@@ -483,13 +483,10 @@ struct mmap_action {
enum mmap_action_type type;

/*
- * If specified, this hook is invoked when an error occurred when
- * attempting the selection action.
- *
- * The hook can return an error code in order to filter the error, but
- * it is not valid to clear the error here.
+ * If non-zero, filter errors that arise from mmap actions such that we
+ * return error_filter instead. Only valid error codes may be specified.
*/
- int (*error_hook)(int err);
+ int error_filter;

/*
* This should be set in rare instances where the operation required
--
2.54.0