Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6663430rwd; Mon, 19 Jun 2023 10:18:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ776ukoUIdRFeH7d6PWl1MqAXudMOpfhpaVgj5dbmViXdC6clzJwePr7IZmY5gig7yxo4Ay X-Received: by 2002:a17:90b:4a05:b0:256:35f0:a2b0 with SMTP id kk5-20020a17090b4a0500b0025635f0a2b0mr10132194pjb.0.1687195122593; Mon, 19 Jun 2023 10:18:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687195122; cv=none; d=google.com; s=arc-20160816; b=lpwDoz11BtN7PmA0/fu0uQbbbk2hmN33ThZ0SMprS1XewqQo3YGe58BUSH/nX5hoYF gkIayf0XO4se4OqoDYlUugKTn/8xF732ZZMRD3FmzV3Mbft4O0IVsVF2FpiumsYc9AUq ++dUlrGM2RkYLZ4D/wu/jTkZeMcdL9lF2diFPvUol12CEgBvzrfn3arqtjOEPYwAachY s8NNT1W/ktDDbrxe+Dd5srz8/Ed+hpHqUvuF7r5ggZ3Wg4BCqmAJSstL25w9ldd9lnLS q8BNIHwv9IV6E5Bs+VUV4SO8SRlEVa3PgIcBECwzM1AbcLlVn4OaZwyKfqHbPyAm+O7x OlHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:cc:to:from:date:references:in-reply-to :message-id:mime-version:user-agent:feedback-id:dkim-signature; bh=fZ1d7PLihFU86bMAympnjgkNYOwZyWW+ZfVEkA+Xl3w=; b=pOrffLZgWazHsCgIoy594DjZnOuP5R3f5OGTxQbQ1eKv9/c6LivbPbrVlAtMp4vRgF gnnCnNDb47ahziNO0+PWMp2XxGkVXYF18GzAiv++2pMcI9qxQiuNv7hxZARuJTez2xE1 EYU5OQf3jOLLYXXwwb2XxR02UIIFu2zWj9J61AW/YIVqbCfc6Iw6tnSwEbH16H0JUqaI WoEdSk07y4r6hPPpwIeeRiPPIsfwbgjcx/vyqQOprfXr6XtfQ2AUaFEZokWayqAgyfi/ QsRvJ3waFAk/amGKG9hdYje+R9+5sWTND5z5qp38nqbDnSNlbgU/wEXdJsXbyD0jcTU7 YpSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YEOROC7Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id le8-20020a170902fb0800b001b675acd5b1si154952plb.341.2023.06.19.10.18.30; Mon, 19 Jun 2023 10:18:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YEOROC7Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231370AbjFSRJc (ORCPT + 99 others); Mon, 19 Jun 2023 13:09:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231351AbjFSRJa (ORCPT ); Mon, 19 Jun 2023 13:09:30 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74487130; Mon, 19 Jun 2023 10:09:28 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D8EBF60E84; Mon, 19 Jun 2023 17:09:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2BBEC433C0; Mon, 19 Jun 2023 17:09:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687194567; bh=JAKwTrp1uinVAtoLWlXEGNS+fEBZEu6il2wWrUqzEzo=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=YEOROC7YGunxOkL9NsbJwROPW3V877k09rIaiwwEQBd6bVLJWdMjusQxoVnbsuD5D SKibXVd7FHc2nMr1tudbdYIZvJGYXAOuK4akurudUoPTNxoZjnK21I5AdFijJrVbol wMmYZdLv8Hw2umDYdeUGRbNj+IGHaRZ0DomN5ubTCsJ6NBLT+yY4qE1y7ajOHRP1RW KGFKHX9UnHaE0jcV0Lk398WnGNpxO+L8j061yUD+7F16bO6b13PKK3oASVquk5plTv fi30eJK4gcno8KPn/VoCZ0cPaw9JHH45FBctirdWyvzfVojpY1MuX3tVhu6B84K6vz 1DQlrW9fvYTQw== Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailauth.nyi.internal (Postfix) with ESMTP id BDC6027C0054; Mon, 19 Jun 2023 13:09:24 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute3.internal (MEProxy); Mon, 19 Jun 2023 13:09:24 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgeefvddguddtjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvvefutgesthdtredtreertdenucfhrhhomhepfdet nhguhicunfhuthhomhhirhhskhhifdcuoehluhhtoheskhgvrhhnvghlrdhorhhgqeenuc ggtffrrghtthgvrhhnpedvhfeuvddthfdufffhkeekffetgffhledtleegffetheeugeej ffduhefgteeihfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfh hrohhmpegrnhguhidomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqudduiedu keehieefvddqvdeifeduieeitdekqdhluhhtoheppehkvghrnhgvlhdrohhrgheslhhinh hugidrlhhuthhordhush X-ME-Proxy: Feedback-ID: ieff94742:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 5BFB531A0063; Mon, 19 Jun 2023 13:09:22 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-496-g8c46984af0-fm-20230615.001-g8c46984a Mime-Version: 1.0 Message-Id: In-Reply-To: <20230618080027.GA52412@kernel.org> References: <20230616085038.4121892-1-rppt@kernel.org> <20230616085038.4121892-3-rppt@kernel.org> <20230618080027.GA52412@kernel.org> Date: Mon, 19 Jun 2023 10:09:02 -0700 From: "Andy Lutomirski" To: "Mike Rapoport" , "Mark Rutland" , "Kees Cook" Cc: "Linux Kernel Mailing List" , "Andrew Morton" , "Catalin Marinas" , "Christophe Leroy" , "David S. Miller" , "Dinh Nguyen" , "Heiko Carstens" , "Helge Deller" , "Huacai Chen" , "Kent Overstreet" , "Luis Chamberlain" , "Mark Rutland" , "Michael Ellerman" , "Nadav Amit" , "Naveen N. Rao" , "Palmer Dabbelt" , "Puranjay Mohan" , "Rick P Edgecombe" , "Russell King (Oracle)" , "Song Liu" , "Steven Rostedt" , "Thomas Bogendoerfer" , "Thomas Gleixner" , "Will Deacon" , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, "the arch/x86 maintainers" Subject: Re: [PATCH v2 02/12] mm: introduce execmem_text_alloc() and jit_text_alloc() Content-Type: text/plain X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DIET_1, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jun 18, 2023, at 1:00 AM, Mike Rapoport wrote: > On Sat, Jun 17, 2023 at 01:38:29PM -0700, Andy Lutomirski wrote: >> On Fri, Jun 16, 2023, at 1:50 AM, Mike Rapoport wrote: >> > From: "Mike Rapoport (IBM)" >> > >> > module_alloc() is used everywhere as a mean to allocate memory for code. >> > >> > Beside being semantically wrong, this unnecessarily ties all subsystems >> > that need to allocate code, such as ftrace, kprobes and BPF to modules >> > and puts the burden of code allocation to the modules code. >> > >> > Several architectures override module_alloc() because of various >> > constraints where the executable memory can be located and this causes >> > additional obstacles for improvements of code allocation. >> > >> > Start splitting code allocation from modules by introducing >> > execmem_text_alloc(), execmem_free(), jit_text_alloc(), jit_free() APIs. >> > >> > Initially, execmem_text_alloc() and jit_text_alloc() are wrappers for >> > module_alloc() and execmem_free() and jit_free() are replacements of >> > module_memfree() to allow updating all call sites to use the new APIs. >> > >> > The intention semantics for new allocation APIs: >> > >> > * execmem_text_alloc() should be used to allocate memory that must reside >> > close to the kernel image, like loadable kernel modules and generated >> > code that is restricted by relative addressing. >> > >> > * jit_text_alloc() should be used to allocate memory for generated code >> > when there are no restrictions for the code placement. For >> > architectures that require that any code is within certain distance >> > from the kernel image, jit_text_alloc() will be essentially aliased to >> > execmem_text_alloc(). >> > >> >> Is there anything in this series to help users do the appropriate >> synchronization when the actually populate the allocated memory with >> code? See here, for example: > > This series only factors out the executable allocations from modules and > puts them in a central place. > Anything else would go on top after this lands. Hmm. On the one hand, there's nothing wrong with factoring out common code. On the other hand, this is probably the right time to at least start thinking about synchronization, at least to the extent that it might make us want to change this API. (I'm not at all saying that this series should require changes -- I'm just saying that this is a good time to think about how this should work.) The current APIs, *and* the proposed jit_text_alloc() API, don't actually look like the one think in the Linux ecosystem that actually intelligently and efficiently maps new text into an address space: mmap(). On x86, you can mmap() an existing file full of executable code PROT_EXEC and jump to it with minimal synchronization (just the standard implicit ordering in the kernel that populates the pages before setting up the PTEs and whatever user synchronization is needed to avoid jumping into the mapping before mmap() finishes). It works across CPUs, and the only possible way userspace can screw it up (for a read-only mapping of read-only text, anyway) is to jump to the mapping too early, in which case userspace gets a page fault. Incoherence is impossible, and no one needs to "serialize" (in the SDM sense). I think the same sequence (from userspace's perspective) works on other architectures, too, although I think more cache management is needed on the kernel's end. As far as I know, no Linux SMP architecture needs an IPI to map executable text into usermode, but I could easily be wrong. (IIRC RISC-V has very developer-unfriendly icache management, but I don't remember the details.) Of course, using ptrace or any other FOLL_FORCE to modify text on x86 is rather fraught, and I bet many things do it wrong when userspace is multithreaded. But not in production because it's mostly not used in production.) But jit_text_alloc() can't do this, because the order of operations doesn't match. With jit_text_alloc(), the executable mapping shows up before the text is populated, so there is no atomic change from not-there to populated-and-executable. Which means that there is an opportunity for CPUs, speculatively or otherwise, to start filling various caches with intermediate states of the text, which means that various architectures (even x86!) may need serialization. For eBPF- and module- like use cases, where JITting/code gen is quite coarse-grained, perhaps something vaguely like: jit_text_alloc() -> returns a handle and an executable virtual address, but does *not* map it there jit_text_write() -> write to that handle jit_text_map() -> map it and synchronize if needed (no sync needed on x86, I think) could be more efficient and/or safer. (Modules could use this too. Getting alternatives right might take some fiddling, because off the top of my head, this doesn't match how it works now.) To make alternatives easier, this could work, maybe (haven't fully thought it through): jit_text_alloc() jit_text_map_rw_inplace() -> map at the target address, but RW, !X write the text and apply alternatives jit_text_finalize() -> change from RW to RX *and synchronize* jit_text_finalize() would either need to wait for RCU (possibly extra heavy weight RCU to get "serialization") or send an IPI. This is slower than the alloc, write, map solution, but allows alternatives to be applied at the final address. Even fancier variants where the writing is some using something like use_temporary_mm() might even make sense. To what extent does performance matter for the various users? module loading is slow, and I don't think we care that much. eBPF loaded is not super fast, and we care to a limited extent. I *think* the bcachefs use case needs to be very fast, but I'm not sure it can be fast and supportable. Anyway, food for thought.