linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Gabriele Paoloni <gpaoloni@redhat.com>
To: corbet@lwn.net, skhan@linuxfoundation.org, arnd@arndb.de,
	gregkh@linuxfoundation.org, brendan.higgins@linux.dev,
	raemoar63@gmail.com, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	kunit-dev@googlegroups.com
Cc: acarminati@nvidia.com, linux-mm@kvack.org,
	safety-architecture@lists.elisa.tech,
	kstewart@linuxfoundation.org, chuckwolber@gmail.com,
	gpaoloni@redhat.com, Alessandro Carminati <acarmina@redhat.com>
Subject: [RFC PATCH v3 4/6] char: mem: expose devmem helpers for KUnit testing
Date: Thu, 12 Feb 2026 13:49:21 +0100	[thread overview]
Message-ID: <20260212124923.222484-5-gpaoloni@redhat.com> (raw)
In-Reply-To: <20260212124923.222484-1-gpaoloni@redhat.com>

From: Alessandro Carminati <acarmina@redhat.com>

Refactor selected /dev/mem helpers to allow access from KUnit tests.

This change:
  - makes page_is_allowed(), read_mem(), and write_mem() visible when
    KUnit is enabled
  - moves shared declarations into a new internal header
  - preserves existing behavior for non-KUnit builds

Signed-off-by: Alessandro Carminati <acarmina@redhat.com>
---
 drivers/char/mem.c | 15 +++++++++++----
 drivers/char/mem.h | 17 +++++++++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)
 create mode 100644 drivers/char/mem.h

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 9aa589ea2ef5..f1d0a2c11819 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -31,6 +31,10 @@
 #include <linux/uaccess.h>
 #include <linux/security.h>
 
+#include <kunit/visibility.h>
+
+#include "mem.h"
+
 #define DEVMEM_MINOR	1
 #define DEVPORT_MINOR	4
 
@@ -57,16 +61,17 @@ static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
 #endif
 
 #ifdef CONFIG_STRICT_DEVMEM
-static inline int page_is_allowed(unsigned long pfn)
+INLINE_VISIBLE_IF_KUNIT int page_is_allowed(unsigned long pfn)
 {
 	return devmem_is_allowed(pfn);
 }
 #else
-static inline int page_is_allowed(unsigned long pfn)
+INLINE_VISIBLE_IF_KUNIT int page_is_allowed(unsigned long pfn)
 {
 	return 1;
 }
 #endif
+EXPORT_SYMBOL_IF_KUNIT(page_is_allowed);
 
 static inline bool should_stop_iteration(void)
 {
@@ -104,7 +109,7 @@ static inline bool should_stop_iteration(void)
  * * %-ENOMEM - out of memory error for auxiliary kernel buffers supporting
  *   the operation of copying content from the physical pages
  */
-static ssize_t read_mem(struct file *file, char __user *buf,
+VISIBLE_IF_KUNIT ssize_t read_mem(struct file *file, char __user *buf,
 			size_t count, loff_t *ppos)
 {
 	phys_addr_t p = *ppos;
@@ -190,6 +195,7 @@ static ssize_t read_mem(struct file *file, char __user *buf,
 	kfree(bounce);
 	return err;
 }
+EXPORT_SYMBOL_IF_KUNIT(read_mem);
 
 /**
  * write_mem - write to physical memory (/dev/mem).
@@ -234,7 +240,7 @@ static ssize_t read_mem(struct file *file, char __user *buf,
  *   be copied from user-space
  * * %-EPERM - access to any of the required pages is not allowed
  */
-static ssize_t write_mem(struct file *file, const char __user *buf,
+VISIBLE_IF_KUNIT ssize_t write_mem(struct file *file, const char __user *buf,
 			 size_t count, loff_t *ppos)
 {
 	phys_addr_t p = *ppos;
@@ -306,6 +312,7 @@ static ssize_t write_mem(struct file *file, const char __user *buf,
 	*ppos += written;
 	return written;
 }
+EXPORT_SYMBOL_IF_KUNIT(write_mem);
 
 int __weak phys_mem_access_prot_allowed(struct file *file,
 	unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
diff --git a/drivers/char/mem.h b/drivers/char/mem.h
new file mode 100644
index 000000000000..dfeb5f1d5ec8
--- /dev/null
+++ b/drivers/char/mem.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2025 Red Hat inc Alessandro Carminati <acarmina@redhat.com>
+ */
+
+#ifndef _LINUX_CHAR_MEM_H
+#define _LINUX_CHAR_MEM_H
+
+#if IS_ENABLED(CONFIG_KUNIT)
+ssize_t read_mem(struct file *file, char __user *buf,
+			size_t count, loff_t *ppos);
+ssize_t write_mem(struct file *file, const char __user *buf,
+			 size_t count, loff_t *ppos);
+int page_is_allowed(unsigned long pfn);
+#endif
+
+#endif /* _LINUX_CHAR_MEM_H */
-- 
2.48.1



  parent reply	other threads:[~2026-02-12 12:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-12 12:49 [RFC PATCH v3 0/6] some /dev/mem specifications and traced testing Gabriele Paoloni
2026-02-12 12:49 ` [RFC PATCH v3 1/6] Documentation: extend the 'Function documentation' with expected behavior and constraints of use Gabriele Paoloni
2026-02-12 12:59   ` Greg KH
2026-02-12 14:00     ` Gabriele Paoloni
2026-02-12 15:23       ` Greg KH
2026-02-13 17:13         ` Gabriele Paoloni
2026-02-12 12:49 ` [RFC PATCH v3 2/6] /dev/mem: Add initial documentation of memory_open() and mem_fops Gabriele Paoloni
2026-02-12 12:49 ` [RFC PATCH v3 3/6] kunit: add visibility helpers for static inline functions Gabriele Paoloni
2026-02-12 12:49 ` Gabriele Paoloni [this message]
2026-02-12 12:49 ` [RFC PATCH v3 5/6] char: mem: add KUnit tests for /dev/mem read_mem() Gabriele Paoloni
2026-02-12 12:49 ` [RFC PATCH v3 6/6] char: mem: add Kconfig option for devmem KUnit tests Gabriele Paoloni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260212124923.222484-5-gpaoloni@redhat.com \
    --to=gpaoloni@redhat.com \
    --cc=acarmina@redhat.com \
    --cc=acarminati@nvidia.com \
    --cc=arnd@arndb.de \
    --cc=brendan.higgins@linux.dev \
    --cc=chuckwolber@gmail.com \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kstewart@linuxfoundation.org \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=raemoar63@gmail.com \
    --cc=safety-architecture@lists.elisa.tech \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox