From patchwork Fri Mar 7 00:57:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pratyush Yadav X-Patchwork-Id: 14005626 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1ED75C28B23 for ; Fri, 7 Mar 2025 00:58:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB290280009; Thu, 6 Mar 2025 19:58:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C6119280001; Thu, 6 Mar 2025 19:58:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B0249280009; Thu, 6 Mar 2025 19:58:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 90E17280001 for ; Thu, 6 Mar 2025 19:58:47 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 51189B67D5 for ; Fri, 7 Mar 2025 00:58:48 +0000 (UTC) X-FDA: 83192945136.11.65988F1 Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) by imf22.hostedemail.com (Postfix) with ESMTP id 59EB6C0005 for ; Fri, 7 Mar 2025 00:58:46 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=amazon.de header.s=amazon201209 header.b=UI9YsCAE; spf=pass (imf22.hostedemail.com: domain of "prvs=1541f9db8=ptyadav@amazon.com" designates 52.95.48.154 as permitted sender) smtp.mailfrom="prvs=1541f9db8=ptyadav@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1741309126; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=vUnosHPz47fWK45st0LZ5YJp1oss1R9eNp+qhCVevDg=; b=E1NR2Y0ehNThCXQSXC33AOBmpFPzYRppOvMBI7pCheBI8Cm4gnKBOb+U1vUmsJLy/qWPZH K52+rqf2HkRjKFGSNKwUj3fOx3mj6CSe0xoGlK6HmP4wsWTpPk2MkAhIE7eFjmAINhfRQS NNKHzIL04MJVtO42YhwwJeDJ2FNx/bM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1741309126; a=rsa-sha256; cv=none; b=Z8KmbyxN7qbvm0c13xnfIc1SnyGYGZaFHXrFw3Ia3s2YRuu+zISmo3PdYJEllHyB35knKj 5TdvqPQkVusgmxV/XyKM0ZG3rYeQSVL9BvJFDMqeoPskVrDPWm5cDwOJjx4alfeoNbNtaw +8OuhphHpOBqU+w10zbq/yUjVH/QT8M= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=amazon.de header.s=amazon201209 header.b=UI9YsCAE; spf=pass (imf22.hostedemail.com: domain of "prvs=1541f9db8=ptyadav@amazon.com" designates 52.95.48.154 as permitted sender) smtp.mailfrom="prvs=1541f9db8=ptyadav@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1741309126; x=1772845126; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=vUnosHPz47fWK45st0LZ5YJp1oss1R9eNp+qhCVevDg=; b=UI9YsCAEfu7hrGGxL06ezF5UeKGB93ZT5EWar8hU553lqC+dgjGBy+gP pZ6U8KJTtAYJygom2t/4A8ZEMtLyRv/9E9yE3q5pWtyi95inMqEYVgoeU XtL4LtLAS9y6N6kLKtXdBrzS6GFHYyl5rOGdxGGgzZ2nNOf8WUaSEdRKM w=; X-IronPort-AV: E=Sophos;i="6.14,227,1736812800"; d="scan'208";a="468696731" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Mar 2025 00:58:34 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.21.151:51103] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.40.40:2525] with esmtp (Farcaster) id 7db8d410-3306-4a9b-a196-6db5e20f1018; Fri, 7 Mar 2025 00:58:34 +0000 (UTC) X-Farcaster-Flow-ID: 7db8d410-3306-4a9b-a196-6db5e20f1018 Received: from EX19D020UWC002.ant.amazon.com (10.13.138.147) by EX19MTAUWA001.ant.amazon.com (10.250.64.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 7 Mar 2025 00:58:34 +0000 Received: from EX19MTAUWA002.ant.amazon.com (10.250.64.202) by EX19D020UWC002.ant.amazon.com (10.13.138.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 7 Mar 2025 00:58:33 +0000 Received: from email-imr-corp-prod-pdx-all-2b-5ec155c2.us-west-2.amazon.com (10.25.36.210) by mail-relay.amazon.com (10.250.64.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14 via Frontend Transport; Fri, 7 Mar 2025 00:58:33 +0000 Received: from dev-dsk-ptyadav-1c-43206220.eu-west-1.amazon.com (dev-dsk-ptyadav-1c-43206220.eu-west-1.amazon.com [172.19.91.144]) by email-imr-corp-prod-pdx-all-2b-5ec155c2.us-west-2.amazon.com (Postfix) with ESMTP id 68D5D4024E; Fri, 7 Mar 2025 00:58:33 +0000 (UTC) Received: by dev-dsk-ptyadav-1c-43206220.eu-west-1.amazon.com (Postfix, from userid 23027615) id F0F9B4FBC; Fri, 7 Mar 2025 00:58:32 +0000 (UTC) From: Pratyush Yadav To: CC: Pratyush Yadav , Jonathan Corbet , "Eric Biederman" , Arnd Bergmann , "Greg Kroah-Hartman" , Alexander Viro , Christian Brauner , Jan Kara , Hugh Dickins , Alexander Graf , Benjamin Herrenschmidt , "David Woodhouse" , James Gowans , "Mike Rapoport" , Paolo Bonzini , "Pasha Tatashin" , Anthony Yznaga , Dave Hansen , David Hildenbrand , Jason Gunthorpe , Matthew Wilcox , "Wei Yang" , Andrew Morton , , , , Subject: [RFC PATCH 0/5] Introduce FDBox, and preserve memfd with shmem over KHO Date: Fri, 7 Mar 2025 00:57:34 +0000 Message-ID: <20250307005830.65293-1-ptyadav@amazon.de> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Stat-Signature: rjqhgpnfcr51m4mkdz113nyh4wg3j9z1 X-Rspamd-Queue-Id: 59EB6C0005 X-Rspam-User: X-HE-Tag: 1741309126-710628 X-HE-Meta: U2FsdGVkX1+OqAzg3bKV4lw6qouDvul9OkP69TXr8ONxy9WjVaJ2j0tXMqh3/kdRXDHDLxX4n1KeqfRrsm94RWR61VOpiCzT5uJ1SSrb3ZFUM+pbgCKi3XOQFbtgPKkkaVReja8jXWPnFpc9adqOeZiHlqhLVsR8A3QbzQX635XWqG/eyYh5DMvGZZzYK9/oOyox6Ep6GojlJlLvEFbIDypCuiPvnGg/COOWkknxJMU8YvSeLze8V5UlHxAGd8qWxooyidp7jEzQli7o3bu7sJ3lu8SEWBDm9W4hRJpLehARGxK3TLmPv+hl2Ij8pyRsMUnDtDHiBF7E9Tp/4IEL/T/vfjis0BhlTCiA9P1nGip6ZIHqnLW2qa9Onq3cf9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s25QK5qiQaNTX4sXmn33uNYy63In+cv0Ihu1kDvOJNfLy4zjuEDOqxPTIpXMt3mX1XSQFzHHGoziV7OWCMrwi9UmtXWS/4w1vVScaSuN1rEs2x6Cfy8gDhYP1YVH6Wp+CYA94IABNNuXjuMQ1+a5f+T37mOo2w3w= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This series introduces the File Descriptor Box (FDBox), along with support in memfd and shmem for persisting memfds over KHO using FDBox. FDBox is a mechanism for userspace to name file descriptors and give them over to the kernel to hold. They can later be retrieved by passing in the same name. The primary purpose of it is to be used with Kexec Handover (KHO) [0]. See Documentation/kho/fdbox.rst for more details. The original idea for FDBox came from Alex Graf. The main problem it attempts to solve is to give a name to anonymous file descriptors like memfd, guest_memfd, iommufd, etc. so they can be retrieved after KHO. Alex wrote some initial code [1] which this series is based upon, though that code is quite hacky and proof-of-concept, and has been significantly refactored and polished with this series. Alex's code mainly played around with KVM, but since I am more familiar with memfd and shmem, I have picked those as the first users. That is not to say this series is in a top notch state already. There is still a lot of room for improvement, both in FDBox and in memfd and shmem. The aim of the patches is to present the idea to get early feedback, and to demonstrate KHO in action, potentially having a consumer of KHO ready by the time those patches are ready for prime time. I have written a simple userspace tool to interact with FDBox and memfd. It can be found here [2]. It is quite simple currently. When given the create command, it creates a box, and a memfd, and fills the memfd with data from a file called "data". It then adds the memfd to the box and seals it. Then one can do KHO. After KHO, the restore command gets the FD out of the box and writes the output to a file called "out". The original and new file can be compared to ensure data consistency. I have tested using the tool and a 1 GiB file, and the memfd came back over KHO with the same contents. The performance was fast enough to not be noticeable to the naked eye, though I have not done much more performance analysis than that. The whole process can be seen in action in this Asciinema [4]. Sample instructions to use the tool: $ make $ dd if=/dev/urandom of=data bs=1G count=1 $ ./fdbox create $ echo 1 > /sys/kernel/kho/active $ kexec -s -l [...] $ kexec -e After the kexec is done, $ ./fdbox restore $ cmp data out $ echo $? The full tree with the patches can be found at [3]. It contains a couple of my patches on top of Mike's KHO patches [0] to fix some small bugs. [0] https://lore.kernel.org/lkml/20250206132754.2596694-1-rppt@kernel.org/ [1] https://github.com/agraf/linux-2.6/blob/kvm-kho-gmem-test/drivers/misc/fdbox.c [2] https://github.com/prati0100/fdbox-utils/blob/main/fdbox.c [3] https://web.git.kernel.org/pub/scm/linux/kernel/git/pratyush/linux.git/log/?h=kho [4] https://asciinema.org/a/mnyVpy1w67mueIkKZzqHI0oAN Pratyush Yadav (5): misc: introduce FDBox misc: add documentation for FDBox mm: shmem: allow callers to specify operations to shmem_undo_range mm: shmem: allow preserving file over FDBOX + KHO mm/memfd: allow preserving FD over FDBOX + KHO Documentation/filesystems/locking.rst | 21 + Documentation/kho/fdbox.rst | 224 ++++++++ Documentation/kho/index.rst | 3 + MAINTAINERS | 9 + drivers/misc/Kconfig | 7 + drivers/misc/Makefile | 1 + drivers/misc/fdbox.c | 758 ++++++++++++++++++++++++++ include/linux/fdbox.h | 119 ++++ include/linux/fs.h | 7 + include/linux/miscdevice.h | 1 + include/linux/shmem_fs.h | 6 + include/uapi/linux/fdbox.h | 61 +++ mm/memfd.c | 128 ++++- mm/shmem.c | 498 +++++++++++++++-- 14 files changed, 1800 insertions(+), 43 deletions(-) create mode 100644 Documentation/kho/fdbox.rst create mode 100644 drivers/misc/fdbox.c create mode 100644 include/linux/fdbox.h create mode 100644 include/uapi/linux/fdbox.h