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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B701C7115A for ; Sun, 22 Jun 2025 07:28:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 30E366B0095; Sun, 22 Jun 2025 03:28:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 291376B0096; Sun, 22 Jun 2025 03:28:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 158656B0098; Sun, 22 Jun 2025 03:28:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F38066B0095 for ; Sun, 22 Jun 2025 03:28:16 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AE3175FD0E for ; Sun, 22 Jun 2025 07:28:16 +0000 (UTC) X-FDA: 83582208192.15.EC45C70 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf22.hostedemail.com (Postfix) with ESMTP id DDFF0C0007 for ; Sun, 22 Jun 2025 07:28:14 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dDIcZ4SF; spf=pass (imf22.hostedemail.com: domain of rppt@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1750577295; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5nRBVQo9bSOhT+TUQk1iTzGePCcKNvSHPsRyLCWZJUc=; b=vRCJu1c9iVHYt/2sj1mwiPU8ukji+BEXjZl8hXtP6R6TkbmvmQgrYLjOk0p02n5nE0dmVh dn3SGpVZioPErx58DhXoxWyxp+7prxlQ9NeDXBvfxts9wl6aA5259lC9hcrF7qvVmwx8WS D36rlgEyHQzlS2jcsav89W48UtnBAzg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750577295; a=rsa-sha256; cv=none; b=Q3zhJgOkz7GNkQAx2IKUZSjeYdBK9qoz08IBWSQu6G2jAP2Qs7PkLUN3Yqy2kADdabRJvM GB+j/pAgm85LbbejoB5sWHm0bg6LsMAi9a82oQWjAeAnx3GDkmUxkdskX8MpB7WpzNK43M 3/kufAl7qxdPWv+DCkbeNvCvPg+YCtA= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dDIcZ4SF; spf=pass (imf22.hostedemail.com: domain of rppt@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 968C54A16E; Sun, 22 Jun 2025 07:28:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A27CC4CEE3; Sun, 22 Jun 2025 07:28:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750577293; bh=ZZ2XvIKaZc+qHuHN/cwtEjIrE44seOhCx+4+J1dO8A0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dDIcZ4SFurenyXdrjh4EbhutJVTqpVHg32N3U4WBEIc0CaHcRtNS7tEN80nD8nUM+ iwfLM7W9KJX09tue4HZdoUJyaPjLM29CulNmr9zeRxt8884jD7z8TdVaooA95KDJum VXxKHHYVjyP7TXFHYQcV3kDkMUH7HoZtbvUtQn7U4p59CCCcGuqomgO6O32id7LDOV Rc27LHD+iAt4jI7SpDyRC1MZfj5fvdn8zaD0kJZNyV9kzfN2EBER2zMjFjf/5RmSwF 5qSsj8EnuQtbY0TDwFpBavMZpy6Jb7a/v4LVCjoMqM7GdS/VtzJ9kQ6X04aipb4HkJ l2zMK4nDPAwpw== Date: Sun, 22 Jun 2025 10:28:04 +0300 From: Mike Rapoport To: Peter Xu Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Nikita Kalyazin , Hugh Dickins , Oscar Salvador , Michal Hocko , David Hildenbrand , Muchun Song , Andrea Arcangeli , Ujwal Kundur , Suren Baghdasaryan , Andrew Morton , Vlastimil Babka , "Liam R . Howlett" , James Houghton , Lorenzo Stoakes , Axel Rasmussen Subject: Re: [PATCH 1/4] mm: Introduce vm_uffd_ops API Message-ID: References: <20250620190342.1780170-1-peterx@redhat.com> <20250620190342.1780170-2-peterx@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250620190342.1780170-2-peterx@redhat.com> X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: DDFF0C0007 X-Stat-Signature: e99frjakzsamoj77r95gqwutbdodmi4p X-Rspam-User: X-HE-Tag: 1750577294-43981 X-HE-Meta: U2FsdGVkX18vBG107zzqIzBH6h+EBDe2ZOIY8N7YrTUe302cKYvyAE12dPLheWisXnNQ+Pbgm1lmVrnP517sEtWdHrjAIeUCDrLk/RqFR5WfguL5Evvk+Kn328nNnAyIxciJ5P68u3RiR3JfJLQQ257fKdHN0b1J0fb4EltP9Pn5Fd2diUv+RXRz7V86GkCIHb9ccdbbxSUb5x/y9oBu2Kf/5z+8uVrFGiaKiYgaxg9e1idJXigT3/EsRjny0qJGKRUn69u4Whx/lJ6x4Ycvb57Y7ONzvHhRav4wrNdbOybN50B4PEZWj9iK5cgOY3542RHl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ter, 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: Hi Peter, On Fri, Jun 20, 2025 at 03:03:39PM -0400, Peter Xu wrote: > Introduce a generic userfaultfd API for vm_operations_struct, so that one > vma, especially when as a module, can support userfaults without modifying > the core files. More importantly, when the module can be compiled out of > the kernel. > > So, instead of having core mm referencing modules that may not ever exist, > we need to have modules opt-in on core mm hooks instead. > > After this API applied, if a module wants to support userfaultfd, the > module should only need to touch its own file and properly define > vm_uffd_ops, instead of changing anything in core mm. > > Note that such API will not work for anonymous. Core mm will process > anonymous memory separately for userfault operations like before. > > This patch only introduces the API alone so that we can start to move > existing users over but without breaking them. > > Currently the uffd_copy() API is almost designed to be the simplistic with > minimum mm changes to move over to the API. > > Signed-off-by: Peter Xu > --- > include/linux/mm.h | 71 +++++++++++++++++++++++++++++++++++ > include/linux/userfaultfd_k.h | 12 ------ > 2 files changed, 71 insertions(+), 12 deletions(-) > > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 98a606908307..8dfd83f01d3d 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -576,6 +576,70 @@ struct vm_fault { > */ > }; > > +#ifdef CONFIG_USERFAULTFD > +/* A combined operation mode + behavior flags. */ > +typedef unsigned int __bitwise uffd_flags_t; > + > +enum mfill_atomic_mode { > + MFILL_ATOMIC_COPY, > + MFILL_ATOMIC_ZEROPAGE, > + MFILL_ATOMIC_CONTINUE, > + MFILL_ATOMIC_POISON, > + NR_MFILL_ATOMIC_MODES, > +}; > + > +/* VMA userfaultfd operations */ > +typedef struct { > + /** > + * @uffd_features: features supported in bitmask. > + * > + * When the ops is defined, the driver must set non-zero features > + * to be a subset (or all) of: VM_UFFD_MISSING|WP|MINOR. > + */ > + unsigned long uffd_features; > + /** > + * @uffd_ioctls: ioctls supported in bitmask. > + * > + * Userfaultfd ioctls supported by the module. Below will always > + * be supported by default whenever a module provides vm_uffd_ops: > + * > + * _UFFDIO_API, _UFFDIO_REGISTER, _UFFDIO_UNREGISTER, _UFFDIO_WAKE > + * > + * The module needs to provide all the rest optionally supported > + * ioctls. For example, when VM_UFFD_MISSING was supported, > + * _UFFDIO_COPY must be supported as ioctl, while _UFFDIO_ZEROPAGE > + * is optional. > + */ > + unsigned long uffd_ioctls; > + /** > + * uffd_get_folio: Handler to resolve UFFDIO_CONTINUE request. > + * > + * @inode: the inode for folio lookup > + * @pgoff: the pgoff of the folio > + * @folio: returned folio pointer > + * > + * Return: zero if succeeded, negative for errors. > + */ > + int (*uffd_get_folio)(struct inode *inode, pgoff_t pgoff, > + struct folio **folio); > + /** > + * uffd_copy: Handler to resolve UFFDIO_COPY|ZEROPAGE request. > + * > + * @dst_pmd: target pmd to resolve page fault > + * @dst_vma: target vma > + * @dst_addr: target virtual address > + * @src_addr: source address to copy from > + * @flags: userfaultfd request flags > + * @foliop: previously allocated folio > + * > + * Return: zero if succeeded, negative for errors. > + */ > + int (*uffd_copy)(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, > + unsigned long dst_addr, unsigned long src_addr, > + uffd_flags_t flags, struct folio **foliop); > +} vm_uffd_ops; > +#endif Can't we define vm_uffd_ops in userfaultfd_k.h? A forward declaration in mm.h should suffice and modules that want to use uffd can include userfaultfd_k.h. > + > /* > * These are the virtual MM functions - opening of an area, closing and > * unmapping it (needed to keep files on disk up-to-date etc), pointer > @@ -653,6 +717,13 @@ struct vm_operations_struct { > */ > struct page *(*find_special_page)(struct vm_area_struct *vma, > unsigned long addr); > +#ifdef CONFIG_USERFAULTFD > + /* > + * Userfaultfd related ops. Modules need to define this to support > + * userfaultfd. > + */ > + const vm_uffd_ops *userfaultfd_ops; > +#endif > }; > > #ifdef CONFIG_NUMA_BALANCING > diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h > index ccad58602846..e79c724b3b95 100644 > --- a/include/linux/userfaultfd_k.h > +++ b/include/linux/userfaultfd_k.h > @@ -80,18 +80,6 @@ struct userfaultfd_ctx { > > extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason); > > -/* A combined operation mode + behavior flags. */ > -typedef unsigned int __bitwise uffd_flags_t; > - > -/* Mutually exclusive modes of operation. */ > -enum mfill_atomic_mode { > - MFILL_ATOMIC_COPY, > - MFILL_ATOMIC_ZEROPAGE, > - MFILL_ATOMIC_CONTINUE, > - MFILL_ATOMIC_POISON, > - NR_MFILL_ATOMIC_MODES, > -}; > - > #define MFILL_ATOMIC_MODE_BITS (const_ilog2(NR_MFILL_ATOMIC_MODES - 1) + 1) > #define MFILL_ATOMIC_BIT(nr) BIT(MFILL_ATOMIC_MODE_BITS + (nr)) > #define MFILL_ATOMIC_FLAG(nr) ((__force uffd_flags_t) MFILL_ATOMIC_BIT(nr)) > -- > 2.49.0 > -- Sincerely yours, Mike.