From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 31415C7EE32 for ; Sun, 22 Jun 2025 21:34:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=nJZY7fSdjsRmE94xDQ9kPtMR3Ats2B2OoNFvSkK6k9U=; b=VMmKQJCmxRedRRbg1OKZ7vLMLU f5ABorTVyxzp6BfIR+sWtWIhecvXDvw5Z6dcXNOdlBzdYZUw59iY0sbpIIn/m2Uc5NVigKNxQhW+F 24dEKjVLP4MDJzOPNWE2pNtKSTCjyv80MaftSWG3QdunUeCK4+6pp7UP8OPxXTHNTXX9YQVXO8N8h ikgHcA/boCTpyC9aMQZcVMQ5haAtGAre1JyZBmvCAx4329uhUY2ZRxnNefODCqB2whcosqG53UlaW C76EfhAXm6HuWmm9Ln5wFMxzCm79rzRTyK1zR7t4EQUlSy/h5aCx4BFuXQHGyO77BsRSgOvDKTSj1 CkB3r0Qg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1uTSKG-00000001F4X-0OPh; Sun, 22 Jun 2025 21:34:08 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1uTSJe-00000001Eri-0CNp for linux-um@lists.infradead.org; Sun, 22 Jun 2025 21:33:31 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-23649faf69fso34226755ad.0 for ; Sun, 22 Jun 2025 14:33:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750628009; x=1751232809; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nJZY7fSdjsRmE94xDQ9kPtMR3Ats2B2OoNFvSkK6k9U=; b=DH1cL8vIztlUWaDxGNP0xRz89/KS7CsJiNRzQPvbhJJT/UlI+VMYjOavHMrNop1Y2r eZNffU6FbC6+rINz078TZGiF4eu/Ra+O0utVH3p0VjkqL6tNJxSEkR4tqF5WGLysReAF EknmxCf8PEgTWQ74lJYPy4gEZObChkCC/OX/r5S6sT2XY4tHJ14WXAzlBjG6c9o+OY5G tclZ3CfBM8HShWh0B1GJBKFDGvdhBow9PhYWEHJgeQH0n/k3z9YcXmFGz0XccHBeesii MunMwgR7xGW4DIDKB+s/IVXr4B4Ri+pZQEJbUBXysx2oJ/XjlwL0L7UyXpDvV7maXpo9 iR5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750628009; x=1751232809; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nJZY7fSdjsRmE94xDQ9kPtMR3Ats2B2OoNFvSkK6k9U=; b=PAxQUdwUwb/eHeIg4yqI7B61gHmEd12J/MaFyQGo6VKDODdx6jpMhSpv4zv6PZRIsY Uog3dUXb0rk6MBahdubC8hCwv6ydlKbx5zXO2766s8JZXtEUbdFRdUEFZkoiTR73spcX IsdV1sHeuTbU8Xtvgf3QZjZHQDhFf17qGZtUpLp8FJwlv5N14jo9RYJiIhR595kdt412 MQM+esw98iOkwfyfRlWoZ5pf81R9lZ2YvcNXRijN7Rv38l2Yna1VxfxeAxsmH3biJyFv w6bjXALC8kqGDj5mDQSIQTm2T4MUqbeRyigG4+Cuc6yX1F5yR08Z4voCF8XOFZyrkul4 9jDA== X-Gm-Message-State: AOJu0YwwTzeXX71JBK3IfztYdzMfE+zziSp+In2hIDrnIn92CvR2Wbwx UIwffhl4jA7fPjF7jDgaiXu1QOQEsuLCrkxxXWwHjuSeIka4NDW3t7Oy X-Gm-Gg: ASbGncsebDOqPWSqjaegetJYSB7zFlMLqPijEuxpINVflo+rLdxksaB/+86lSZpA2NF fDDlPbIlRL/zg3NOWej5hUNu5fdHcv8ExZj1UkYxnA+L5PctWpXIJlRLaSP9uKGwgGJFTV+ZwRt kXyiF10csXg+xqhczmQYgg+Qjv5fVdYbSJRZ6R5FifxKlG1DUe0TjjHrgF03usCSHpFLZbHuwbO Opap8K/Mx3V0YFOcSGzV5kWOoRLhK5+0WHeUq5s5yHkDu148IuYTZYgTtTWUdZyQEV3rQbAUdwQ r1dYtzr66R8R/Hi/yrRBmPJ336qeK1sHcrLsFHHUvq056Yh3UXtWHlj1Xc53rHP+ApQPuKBtkaJ hRBuOKDLUwZDTaFWRorCobPNwOO3sCZL7 X-Google-Smtp-Source: AGHT+IE+C6TqiI6itKxfIEkY9onlcQ1YuOUVxXsgpsSghi9Pc7uabtrgYH9aXbzxZQRAffufgmsfkQ== X-Received: by 2002:a17:902:ce8c:b0:235:880:cf8a with SMTP id d9443c01a7336-237d97fbc21mr155530705ad.15.1750628009187; Sun, 22 Jun 2025 14:33:29 -0700 (PDT) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-237d8393227sm67844035ad.24.2025.06.22.14.33.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Jun 2025 14:33:27 -0700 (PDT) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id D0078EF1EAA; Mon, 23 Jun 2025 06:33:21 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH v10 03/13] um: nommu: memory handling Date: Mon, 23 Jun 2025 06:33:01 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250622_143330_088932_C4B65CA8 X-CRM114-Status: GOOD ( 15.45 ) X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+linux-um=archiver.kernel.org@lists.infradead.org This commit adds memory operations on UML under !MMU environment. Some part of the original UML code relying on CONFIG_MMU are excluded from compilation when !CONFIG_MMU. Additionally, generic functions such as uaccess, futex, memcpy/strnlen/strncpy can be used as user- and kernel-space share the address space in !CONFIG_MMU mode. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/um/Makefile | 4 ++++ arch/um/include/asm/futex.h | 4 ++++ arch/um/include/asm/mmu.h | 3 +++ arch/um/include/asm/mmu_context.h | 2 ++ arch/um/include/asm/uaccess.h | 7 ++++--- arch/um/kernel/mem.c | 3 ++- arch/um/os-Linux/mem.c | 4 ++++ arch/um/os-Linux/process.c | 4 ++-- 8 files changed, 25 insertions(+), 6 deletions(-) diff --git a/arch/um/Makefile b/arch/um/Makefile index 7be0143b5ba3..5371c9a1b11e 100644 --- a/arch/um/Makefile +++ b/arch/um/Makefile @@ -46,6 +46,10 @@ ARCH_INCLUDE := -I$(srctree)/$(SHARED_HEADERS) ARCH_INCLUDE += -I$(srctree)/$(HOST_DIR)/um/shared KBUILD_CPPFLAGS += -I$(srctree)/$(HOST_DIR)/um +ifneq ($(CONFIG_MMU),y) +core-y += $(ARCH_DIR)/nommu/ +endif + # -Dvmap=kernel_vmap prevents anything from referencing the libpcap.o symbol so # named - it's a common symbol in libpcap, so we get a binary which crashes. # diff --git a/arch/um/include/asm/futex.h b/arch/um/include/asm/futex.h index 780aa6bfc050..785fd6649aa2 100644 --- a/arch/um/include/asm/futex.h +++ b/arch/um/include/asm/futex.h @@ -7,8 +7,12 @@ #include +#ifdef CONFIG_MMU int arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr); int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, u32 oldval, u32 newval); +#else +#include +#endif #endif diff --git a/arch/um/include/asm/mmu.h b/arch/um/include/asm/mmu.h index e9661846b4a3..9f30c69e5278 100644 --- a/arch/um/include/asm/mmu.h +++ b/arch/um/include/asm/mmu.h @@ -18,10 +18,13 @@ typedef struct mm_context { unsigned long sync_tlb_range_from; unsigned long sync_tlb_range_to; +#ifndef CONFIG_MMU + unsigned long end_brk; #ifdef CONFIG_BINFMT_ELF_FDPIC unsigned long exec_fdpic_loadmap; unsigned long interp_fdpic_loadmap; #endif +#endif /* !CONFIG_MMU */ } mm_context_t; #endif diff --git a/arch/um/include/asm/mmu_context.h b/arch/um/include/asm/mmu_context.h index 23dcc914d44e..033a70166066 100644 --- a/arch/um/include/asm/mmu_context.h +++ b/arch/um/include/asm/mmu_context.h @@ -36,11 +36,13 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, } } +#ifdef CONFIG_MMU #define init_new_context init_new_context extern int init_new_context(struct task_struct *task, struct mm_struct *mm); #define destroy_context destroy_context extern void destroy_context(struct mm_struct *mm); +#endif #include diff --git a/arch/um/include/asm/uaccess.h b/arch/um/include/asm/uaccess.h index 1c6e0ae41b0c..b9677758e759 100644 --- a/arch/um/include/asm/uaccess.h +++ b/arch/um/include/asm/uaccess.h @@ -23,6 +23,7 @@ #define __addr_range_nowrap(addr, size) \ ((unsigned long) (addr) <= ((unsigned long) (addr) + (size))) +#ifdef CONFIG_MMU extern unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n); extern unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n); extern unsigned long __clear_user(void __user *mem, unsigned long len); @@ -34,9 +35,6 @@ static inline int __access_ok(const void __user *ptr, unsigned long size); #define INLINE_COPY_FROM_USER #define INLINE_COPY_TO_USER - -#include - static inline int __access_ok(const void __user *ptr, unsigned long size) { unsigned long addr = (unsigned long)ptr; @@ -70,5 +68,8 @@ do { \ barrier(); \ current->thread.segv_continue = NULL; \ } while (0) +#endif + +#include #endif diff --git a/arch/um/kernel/mem.c b/arch/um/kernel/mem.c index 106a2f85ab5c..4be1cf240d71 100644 --- a/arch/um/kernel/mem.c +++ b/arch/um/kernel/mem.c @@ -64,7 +64,8 @@ void __init arch_mm_preinit(void) * to be turned on. */ brk_end = (unsigned long) UML_ROUND_UP(sbrk(0)); - map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0); + map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, + !IS_ENABLED(CONFIG_MMU)); memblock_free((void *)brk_end, uml_reserved - brk_end); uml_reserved = brk_end; min_low_pfn = PFN_UP(__pa(uml_reserved)); diff --git a/arch/um/os-Linux/mem.c b/arch/um/os-Linux/mem.c index 72f302f4d197..4f5d9a94f8e2 100644 --- a/arch/um/os-Linux/mem.c +++ b/arch/um/os-Linux/mem.c @@ -213,6 +213,10 @@ int __init create_mem_file(unsigned long long len) { int err, fd; + /* NOMMU kernel uses -1 as a fd for further use (e.g., mmap) */ + if (!IS_ENABLED(CONFIG_MMU)) + return -1; + fd = create_tmp_file(len); err = os_set_exec_close(fd); diff --git a/arch/um/os-Linux/process.c b/arch/um/os-Linux/process.c index 4eb7e137ef4b..8a1ab59a089f 100644 --- a/arch/um/os-Linux/process.c +++ b/arch/um/os-Linux/process.c @@ -99,8 +99,8 @@ int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len, prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | (x ? PROT_EXEC : 0); - loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED, - fd, off); + loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED | + (!IS_ENABLED(CONFIG_MMU) ? MAP_ANONYMOUS : 0), fd, off); if (loc == MAP_FAILED) return -errno; return 0; -- 2.43.0