public inbox for astral-devel@lists.astral-os.org
 help / color / mirror / Atom feed
From: Matheus <matheus@astral-os.org>
To: astral-devel@lists.astral-os.org
Cc: Matheus <matheusuky@gmail.com>
Subject: [PATCH 1/4] mm: properly fork shared file mappings
Date: Tue,  6 Oct 2026 11:46:32 -0300	[thread overview]
Message-ID: <20261006144635.141071-2-matheus@astral-os.org> (raw)
In-Reply-To: <20261006144635.141071-1-matheus@astral-os.org>

From: Matheus <matheusuky@gmail.com>

mm_fork_context() currently maps shared chdev mappings 1:1 when forking
a range. The issue with this approach is that chdev mappings are not
guaranteed to be backed by physical memory, and thus cannot safely be
refcounted. This can lead to issues on devices with more complex buffer
lifetimes.

To allow for internal book-keeping, do not map shared chdev memory on
fork and instead allow it to go through the entire fault-in process
again.
---
 kernel-src/mm/mm.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/kernel-src/mm/mm.c b/kernel-src/mm/mm.c
index 158382f1..7a26ccd7 100644
--- a/kernel-src/mm/mm.c
+++ b/kernel-src/mm/mm.c
@@ -590,21 +590,27 @@ mm_context_t *mm_fork_context(mm_context_t *old_context) {
 
 			bool writeable = arch_mmu_iswritable(old_context->pagetable, vaddr);
 			bool private = !(range->flags & MM_RANGE_FLAGS_SHARED);
-			if ((range->flags & MM_RANGE_FLAGS_FILE) && range->vnode->type == V_TYPE_CHDEV && !private) {
-				// TODO: implement fork semantics for device mappings properly.
-				if (arch_mmu_map(new_context->pagetable, phys, vaddr, new_range->mmuflags) == false)
-					goto error;
+			// the semantics of device mappings in general are:
+			// - a device mapping always gets *fully* mapped on VOP_MMAP
+			// - if a device mapping is private, it gets a valid
+			//   physical page and goes through the normal fork path.
+			// - if a device mapping is shared, it does not have a valid
+			//   physical page and must have VOP_MMAP called for it again.
+			//   defer that until its actually accessed in a new page fault
+			if ((range->flags & MM_RANGE_FLAGS_FILE) &&
+			    range->vnode->type == V_TYPE_CHDEV && !private)
 				continue;
-			}
 
 			page_t *page = mm_get_page(phys);
 			if (mm_is_page_locked(page) && writeable && private) {
-				// the page is a private locked page, eagerly copy it to ensure fork/futex correctness
+				// the page is a private locked page, eagerly copy it
+				// to ensure fork/futex correctness
 				void *new_phys = mm_alloc_page(MEMORY_SECTION_DEFAULT);
 				if (new_phys == NULL)
 					goto error;
 
-				if (arch_mmu_map(new_context->pagetable, new_phys, vaddr, new_range->mmuflags) == false) {
+				if (arch_mmu_map(new_context->pagetable, new_phys, vaddr,
+						 new_range->mmuflags) == false) {
 					mm_release_page(new_phys);
 					goto error;
 				}
@@ -612,12 +618,14 @@ mm_context_t *mm_fork_context(mm_context_t *old_context) {
 				memcpy(MAKE_HHDM(new_phys), MAKE_HHDM(phys), PAGE_SIZE);
 			} else {
 				// go through the normal CoW path
-				if (arch_mmu_map(new_context->pagetable, phys, vaddr, new_range->mmuflags & ~ARCH_MMU_FLAGS_WRITE) == false)
+				if (arch_mmu_map(new_context->pagetable, phys, vaddr,
+						 new_range->mmuflags & ~ARCH_MMU_FLAGS_WRITE) == false)
 					goto error;
 
 				mm_hold_page(phys);
 
-				arch_mmu_remap(old_context->pagetable, phys, vaddr, new_range->mmuflags & ~ARCH_MMU_FLAGS_WRITE);
+				arch_mmu_remap(old_context->pagetable, phys, vaddr,
+					       new_range->mmuflags & ~ARCH_MMU_FLAGS_WRITE);
 			}
 		}
 
-- 
2.43.0


  reply	other threads:[~2026-10-06 14:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-06 14:46 [PATCH 0/4] kcov: initial implementation Matheus
2026-10-06 14:46 ` Matheus [this message]
2026-10-06 14:46 ` [PATCH 2/4] cpu: add irq depth counting Matheus
2026-10-06 14:46 ` [PATCH 3/4] mmu: move tlb shootdown to IPL_MAX Matheus
2026-10-06 14:46 ` [PATCH 4/4] kcov: initial implementation Matheus

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=20261006144635.141071-2-matheus@astral-os.org \
    --to=matheus@astral-os.org \
    --cc=astral-devel@lists.astral-os.org \
    --cc=matheusuky@gmail.com \
    /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