From mboxrd@z Thu Jan 1 00:00:00 1970 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lists.astral-os.org; s=s2026; t=1791298014; bh=KHq5VqZexMVHRsmIR6+U2CJm0T+f1WWYDWBW3KaUYFQ=; h=From:To:Subject:Date:In-Reply-To:References:CC:List-Id:List-Help: List-Owner:List-Post:List-Subscribe:List-Unsubscribe:From; b=o1xbF1/m25sV7C+N9Q43s5VWiF66a1N5RmOd2gHqwVN/k7q82Jb+YDmnRO3l6kevy K9olnBC2S89fcaUJg/rCk+8yPN6xZK9QZqSlByLZPLSFK9VVX4idycJt40bgAeh6ZN ETSkUTUbri+kEfh6xvKFl7l7mCm14tWkxcBt1MmQixsGbelfpqnTN+sj+sknbGgryl X4YtsI53U3I8Cn0jcGX3cPqFe1F6IEv/40pbsZk6B8oXotEJn46BA5H3BH/fqxciuF okRYHgOCOEROpuCWW124IAra3sTjAzZfmkWeTQy8mwvD8RaArljnRHuJJS/EzTluGM hABW+hxUQZN1w== Received: from vps-ace4d786.vps.ovh.us (localhost [127.0.0.1]) by mail.astral-os.org (Postfix) with ESMTP id E8A273ED44; Tue, 06 Oct 2026 14:46:54 +0000 (UTC) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=lists.astral-os.org; s=arc2026; t=1791298014; b=RWE6pt+gdnECkk1cCfofCmZq2LP5Yahzb40Mu/A7tyVHUFWQox7KaZp37c0Hur1HxGJ9G LFjld92sBMNnUwESaqJlvns7VHj1qHLN//mAfopxIs4guKpi4ru3mazY6xSqJv7/CV3cFfG Fs6ATf4Ecswi4YluVP3OgC04WyMpI2QPodaYMmmqdFYazjTYT/zEKTIVlsNSKi9x4sYRrXJ 2GWOw6J/IhlCtIBoV0aVGihOnJSX68stv9g5qgCXTyxHA2OF4UFB2hsiJZ55xBoTXkUOcLU SGgkRjzU+cCn1YiWmrrgv8MoM4JESPqS3nEMTtjyehPgFj86AY0RKlNarFTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=lists.astral-os.org; s=arc2026; t=1791298014; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=JCqGWxPfJRUbVA/NazNVVT9EsbDtylEe4tkDqz0EZwk=; b=Eownjj/RrX8QLsgknTrLgoP/DtBK7l+dzpf3O8K/wM/lh+vD6gmVzlnz3h9gKYyok8dkK Cun2LLAJSHz6QFIXlU6JnwfHGbavHO+HR0yxtZkqOUqAJYGmN/jKxkspYormCAxrKA1UX+w zf2EcFvu7XNAZxIHZCrNMRGv+XNMh67ZKfg9XOaYAKrgbs2wpGLP7BihJRbGfJkzMDEQ3/T FHn/kHNVGk1iPr4XIpvJKabGjCqi7GFxY9KqeGpph/5fDLQfm2U1wQHhDgXNlLDLZzeP/2r EMPo7xgxzcG0UkckuE7Ya9o8aycP3OtqP/01jWQP98lv8NjKephYxTdz93lg== ARC-Authentication-Results: i=1; lists.astral-os.org; dkim=pass header.d=astral-os.org; arc=none; dmarc=none Authentication-Results: lists.astral-os.org; dkim=pass header.d=astral-os.org; arc=none (Message is not ARC signed); dmarc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=astral-os.org; s=mail2026; t=1791298012; bh=KHq5VqZexMVHRsmIR6+U2CJm0T+f1WWYDWBW3KaUYFQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WO75R94+DnDQ1m5DkEhbzSCe8zLeeg7m/wO8LwVFPpi++RfwkK+itDwsfupjvEVPy c6EAGosBZY0/L2B1LpZcHgN+jmy3KU6bhFHhKuTf+bn5WsduVnhAAaOnhUUzfmNqxG 0mScTppu+LN4eQsCul56fDUkgHV2PAPR+VqgJaFqVaWfhKf4EmRxjhNlN6ficY9eEg NCN4hgejPc+pEIIs0LArtN2ezmxAMfyQ7/1TNCEIWXK4FCXwrtYfr+Gdc4WyIA07iM 9Fr5c8gio0d2qPacxK0ewuLbQ7vXvG5I6T5OsxU+15TvJ+pv4XnvakEcJIR1pmsT6C dWuQgRWj3dP7Q== Received: from LAPTOP-R01LEAQM.localdomain (unknown [189.112.151.75]) by mail.astral-os.org (Postfix) with ESMTPSA id 1968A3ED3F; Tue, 06 Oct 2026 14:46:51 +0000 (UTC) From: Matheus To: astral-devel@lists.astral-os.org Subject: [PATCH 1/4] mm: properly fork shared file mappings Date: Tue, 6 Oct 2026 11:46:32 -0300 Message-ID: <20261006144635.141071-2-matheus@astral-os.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261006144635.141071-1-matheus@astral-os.org> References: <20261006144635.141071-1-matheus@astral-os.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: YD332SYXV3E7AUSPJ7D2RIN7S4M4VVMT X-Message-ID-Hash: YD332SYXV3E7AUSPJ7D2RIN7S4M4VVMT X-MailFrom: matheus@astral-os.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Matheus X-Mailman-Version: 3.3.10 Precedence: list List-Id: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Matheus 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