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 6D6B0C10DC1 for ; Fri, 8 Dec 2023 09:53:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDEDE6B0078; Fri, 8 Dec 2023 04:53:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E8F146B007E; Fri, 8 Dec 2023 04:53:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2F946B0080; Fri, 8 Dec 2023 04:53:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BDC1F6B0078 for ; Fri, 8 Dec 2023 04:53:55 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6225AA18DB for ; Fri, 8 Dec 2023 09:53:55 +0000 (UTC) X-FDA: 81543189630.03.1D9F845 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 92376A0008 for ; Fri, 8 Dec 2023 09:53:51 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BH558O8C; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702029233; 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:in-reply-to:references:references:dkim-signature; bh=TLOnd87tBx9NHJvM+Pm9GwgB7UUIejzObGLxdLzQlL4=; b=TBnzhqsZ6xFk5aiEC5BPB1meIaWUh2qp7IkjA6Kg5UQTUJk05/ytu2ewU0kDgiEPJELKlC jYWqYNpcoS5dxPIfS3x+nqk0gEkIQvPKY7UJ0K6U4KjMatT1zf2vFSDHD5gaAef1x/31da JfUDjU1O8jIEXNi7fD8iZjc1nF194jo= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BH558O8C; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702029233; a=rsa-sha256; cv=none; b=j6NkjNJvxAJy000exoOGZGdDQeQAs57M4Bqgx+C95VrQyfmO3gWWoLoyd+LxhwqHtmGCNr 8LiwG7PZnMNSPJWNZgBHBpYkC/c+dQ/QLX1KLfQNBbej4TRy+QeAZiYBzobHPz374hBDiN 36QAOY9mw9nQlUZmEy2GyjZ+tCcL694= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702029230; h=from:from: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:in-reply-to:references:references:autocrypt:autocrypt; bh=TLOnd87tBx9NHJvM+Pm9GwgB7UUIejzObGLxdLzQlL4=; b=BH558O8Coomz4mf5TrbE/KhueuaD2FBs2npNVXudKQdSA94PgOfY+kq+ZrkHAQsqgJWfJJ uAleUYYmrW2CIFHYtU9Mmdjh7USgBvDAQIbVQTJWulYcfcginaf13NtvpOgPnL2301mdNd 8UiGzEYx19CYDga4ao8Ftq6oiaYu0NE= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-637-L5L9v2vnNaGVGgEEyJKsmA-1; Fri, 08 Dec 2023 04:53:48 -0500 X-MC-Unique: L5L9v2vnNaGVGgEEyJKsmA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-333503fd5bdso1481938f8f.3 for ; Fri, 08 Dec 2023 01:53:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702029227; x=1702634027; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=TLOnd87tBx9NHJvM+Pm9GwgB7UUIejzObGLxdLzQlL4=; b=B7kZN6bLsdbVUvwYSlu+SRkDihsOgZxr2OV4nx5JaWkMRat8f+KvzBiOWyKPw6YYrC HhZFY3kGhKiJc2Rhhw5pmvQCRli9CVf+2YEujN2j6W+yqS+SoZMKeQ6AXbQU/8xxuDjn pYAiS3yyKPUacb3/46c3ojxFGaMcnlfW3lIQdYBxbARLXh3VSaebVrYgJH6lG4hXUjuv rGZiDW6GAk9DYieiIPKxdgF5edzLDoa3AkiTKohaovRP2bzEoOE3nlqePd31qIIU8jmd GrRwC/teLkU/S5+2I56B7z9t4jz7DuTWOSe99VndY3TISeXZJWAys6QcT7W6LlKQUhFJ W/KQ== X-Gm-Message-State: AOJu0Yw5RZRZRplZITpTKRtk1IkV2lk+/IxQ8KUedxnuQ9u/AMLVmb4T ySp30blafSorE/5Ya0/UO6t/GpUQBHjnqg0jE4w9h8CZDrfeYoRhzVlEZ84rdOY5At0RCFe0gB3 FLfW/dVBOi88= X-Received: by 2002:adf:f78f:0:b0:333:4ac2:fe2f with SMTP id q15-20020adff78f000000b003334ac2fe2fmr2114765wrp.104.1702029227041; Fri, 08 Dec 2023 01:53:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IH/jBiCohtCh+aXnThI4eu5SRpVq9Glo+jiyedFr4eiEyN7IbiI+Fp1jY/qLIPQye26OIQzSA== X-Received: by 2002:adf:f78f:0:b0:333:4ac2:fe2f with SMTP id q15-20020adff78f000000b003334ac2fe2fmr2114754wrp.104.1702029226598; Fri, 08 Dec 2023 01:53:46 -0800 (PST) Received: from ?IPV6:2003:cb:c724:2100:3826:4f41:d72c:dc1b? (p200300cbc724210038264f41d72cdc1b.dip0.t-ipconnect.de. [2003:cb:c724:2100:3826:4f41:d72c:dc1b]) by smtp.gmail.com with ESMTPSA id z10-20020a056000110a00b0033343804ec9sm1628709wrw.85.2023.12.08.01.53.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 08 Dec 2023 01:53:46 -0800 (PST) Message-ID: Date: Fri, 8 Dec 2023 10:53:44 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6) To: "Kasireddy, Vivek" , "dri-devel@lists.freedesktop.org" , "linux-mm@kvack.org" Cc: "Kim, Dongwon" , Daniel Vetter , Hugh Dickins , Peter Xu , Christoph Hellwig , Gerd Hoffmann , Jason Gunthorpe , "Chang, Junxiao" , Mike Kravetz References: <20231205053509.2342169-1-vivek.kasireddy@intel.com> <20231205053509.2342169-4-vivek.kasireddy@intel.com> <5ffe2ea3-83da-4b5f-adc8-af9cd9a57cd2@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jf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EqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 92376A0008 X-Rspam-User: X-Stat-Signature: nnke3pdfm39zyab4u5ehkcxdjzmrd4gf X-Rspamd-Server: rspam01 X-HE-Tag: 1702029231-151960 X-HE-Meta: U2FsdGVkX1+K8wtKPObpiZyLziLO5erezdkBmRcJEkkmYLuOTi9ta8qnhxr69uEDg2ljyRrh8F1TCpUNtaGnyWt+4KejWwPSMtmfe7pOeLn6vjf/ZQX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/15+iFt7/EL17HE10BN0xd6pOmgLenjl0pB90JRAOODu7xwsoY0YnpTcspiNP6ZruTaKFEg0DwcLx3vXNBNc/9AI5K+ouMsq8vqcw+ViQwQQEDn3tW2Pm5WVo4ICqG/jDQYUq1qTrzlFkhO8wpR+JhdefQXdnDR1HH4azrP4cJ1lWptKnFPcEsKLq7gMr44m22x0EcdIFZmqdGAiM2DxP9Owh3zyyHjC0xJssCQ2iT3YHSxXhUb9bH+gOXsY6ouJ5prQKJE+Dm/9Zpn2bw4fQZ/jRHMCdJAUnhszu9aqUkmBHMg2TsgL6rxgkOeZXGb4qZ8CeR7O869XBvhjB9WGJL461NVWDU/GYXuIge4JrY2soRQumWzsOO05wsUy6hGerS0TL5RS88Ta3/yTPV0ZQ3ufiz2kIOEWnFv3ofvdI9h5rvpKYiTRqWytQp3evKTsrnSkM2KNrxcaWN2Uw== 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: On 08.12.23 08:57, Kasireddy, Vivek wrote: > Hi David, > >>> >>>> On 05.12.23 06:35, Vivek Kasireddy wrote: >>>>> For drivers that would like to longterm-pin the pages associated >>>>> with a memfd, the pin_user_pages_fd() API provides an option to >>>>> not only pin the pages via FOLL_PIN but also to check and migrate >>>>> them if they reside in movable zone or CMA block. This API >>>>> currently works with memfds but it should work with any files >>>>> that belong to either shmemfs or hugetlbfs. Files belonging to >>>>> other filesystems are rejected for now. >>>>> >>>>> The pages need to be located first before pinning them via FOLL_PIN. >>>>> If they are found in the page cache, they can be immediately pinned. >>>>> Otherwise, they need to be allocated using the filesystem specific >>>>> APIs and then pinned. >>>>> >>>>> v2: >>>>> - Drop gup_flags and improve comments and commit message (David) >>>>> - Allocate a page if we cannot find in page cache for the hugetlbfs >>>>> case as well (David) >>>>> - Don't unpin pages if there is a migration related failure (David) >>>>> - Drop the unnecessary nr_pages <= 0 check (Jason) >>>>> - Have the caller of the API pass in file * instead of fd (Jason) >>>>> >>>>> v3: (David) >>>>> - Enclose the huge page allocation code with #ifdef >>>> CONFIG_HUGETLB_PAGE >>>>> (Build error reported by kernel test robot ) >>>>> - Don't forget memalloc_pin_restore() on non-migration related errors >>>>> - Improve the readability of the cleanup code associated with >>>>> non-migration related errors >>>>> - Augment the comments by describing FOLL_LONGTERM like behavior >>>>> - Include the R-b tag from Jason >>>>> >>>>> v4: >>>>> - Remove the local variable "page" and instead use 3 return statements >>>>> in alloc_file_page() (David) >>>>> - Add the R-b tag from David >>>>> >>>>> v5: (David) >>>>> - For hugetlb case, ensure that we only obtain head pages from the >>>>> mapping by using __filemap_get_folio() instead of >> find_get_page_flags() >>>>> - Handle -EEXIST when two or more potential users try to simultaneously >>>>> add a huge page to the mapping by forcing them to retry on failure >>>>> >>>>> v6: (Christoph) >>>>> - Rename this API to memfd_pin_user_pages() to make it clear that it >>>>> is intended for memfds >>>>> - Move the memfd page allocation helper from gup.c to memfd.c >>>>> - Fix indentation errors in memfd_pin_user_pages() >>>>> - For contiguous ranges of folios, use a helper such as >>>>> filemap_get_folios_contig() to lookup the page cache in batches >>>>> >>>>> Cc: David Hildenbrand >>>>> Cc: Christoph Hellwig >>>>> Cc: Daniel Vetter >>>>> Cc: Mike Kravetz >>>>> Cc: Hugh Dickins >>>>> Cc: Peter Xu >>>>> Cc: Gerd Hoffmann >>>>> Cc: Dongwon Kim >>>>> Cc: Junxiao Chang >>>>> Suggested-by: Jason Gunthorpe >>>>> Reviewed-by: Jason Gunthorpe (v2) >>>>> Reviewed-by: David Hildenbrand (v3) >>>>> Signed-off-by: Vivek Kasireddy >>>>> --- >>>>> include/linux/memfd.h | 5 +++ >>>>> include/linux/mm.h | 2 + >>>>> mm/gup.c | 102 >> ++++++++++++++++++++++++++++++++++++++++++ >>>>> mm/memfd.c | 34 ++++++++++++++ >>>>> 4 files changed, 143 insertions(+) >>>>> >>>>> diff --git a/include/linux/memfd.h b/include/linux/memfd.h >>>>> index e7abf6fa4c52..6fc0d1282151 100644 >>>>> --- a/include/linux/memfd.h >>>>> +++ b/include/linux/memfd.h >>>>> @@ -6,11 +6,16 @@ >>>>> >>>>> #ifdef CONFIG_MEMFD_CREATE >>>>> extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int >>>> arg); >>>>> +extern struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx); >>>>> #else >>>>> static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int >> a) >>>>> { >>>>> return -EINVAL; >>>>> } >>>>> +static inline struct page *memfd_alloc_page(struct file *memfd, pgoff_t >>>> idx) >>>>> +{ >>>>> + return ERR_PTR(-EINVAL); >>>>> +} >>>>> #endif >>>>> >>>>> #endif /* __LINUX_MEMFD_H */ >>>>> diff --git a/include/linux/mm.h b/include/linux/mm.h >>>>> index 418d26608ece..ac69db45509f 100644 >>>>> --- a/include/linux/mm.h >>>>> +++ b/include/linux/mm.h >>>>> @@ -2472,6 +2472,8 @@ long get_user_pages_unlocked(unsigned long >>>> start, unsigned long nr_pages, >>>>> struct page **pages, unsigned int gup_flags); >>>>> long pin_user_pages_unlocked(unsigned long start, unsigned long >>>> nr_pages, >>>>> struct page **pages, unsigned int gup_flags); >>>>> +long memfd_pin_user_pages(struct file *file, pgoff_t start, >>>>> + unsigned long nr_pages, struct page **pages); >>>>> >>>>> int get_user_pages_fast(unsigned long start, int nr_pages, >>>>> unsigned int gup_flags, struct page **pages); >>>>> diff --git a/mm/gup.c b/mm/gup.c >>>>> index 231711efa390..eb93d1ec9dc6 100644 >>>>> --- a/mm/gup.c >>>>> +++ b/mm/gup.c >>>>> @@ -5,6 +5,7 @@ >>>>> #include >>>>> >>>>> #include >>>>> +#include >>>>> #include >>>>> #include >>>>> #include >>>>> @@ -17,6 +18,7 @@ >>>>> #include >>>>> #include >>>>> #include >>>>> +#include >>>>> #include >>>>> #include >>>>> >>>>> @@ -3410,3 +3412,103 @@ long pin_user_pages_unlocked(unsigned >> long >>>> start, unsigned long nr_pages, >>>>> &locked, gup_flags); >>>>> } >>>>> EXPORT_SYMBOL(pin_user_pages_unlocked); >>>>> + >>>>> +/** >>>>> + * memfd_pin_user_pages() - pin user pages associated with a memfd >>>>> + * @memfd: the memfd whose pages are to be pinned >>>>> + * @start: starting memfd offset >>>>> + * @nr_pages: number of pages from start to pin >>>>> + * @pages: array that receives pointers to the pages pinned. >>>>> + * Should be at-least nr_pages long. >>>>> + * >>>>> + * Attempt to pin pages associated with a memfd; given that a memfd is >>>> either >>>>> + * backed by shmem or hugetlb, the pages can either be found in the >> page >>>> cache >>>>> + * or need to be allocated if necessary. Once the pages are located, they >>>> are >>>>> + * all pinned via FOLL_PIN. And, these pinned pages need to be released >>>> either >>>>> + * using unpin_user_pages() or unpin_user_page(). >>>>> + * >>>>> + * It must be noted that the pages may be pinned for an indefinite >> amount >>>>> + * of time. And, in most cases, the duration of time they may stay >> pinned >>>>> + * would be controlled by the userspace. This behavior is effectively the >>>>> + * same as using FOLL_LONGTERM with other GUP APIs. >>>>> + * >>>>> + * Returns number of pages pinned. This would be equal to the number >> of >>>>> + * pages requested. If no pages were pinned, it returns -errno. >>>>> + */ >>>>> +long memfd_pin_user_pages(struct file *memfd, pgoff_t start, >>>>> + unsigned long nr_pages, struct page **pages) >>>>> +{ >>>>> + pgoff_t start_idx, end_idx = start + nr_pages - 1; >>>>> + unsigned int flags, nr_folios, i, j; >>>>> + struct folio_batch fbatch; >>>>> + struct page *page = NULL; >>>>> + struct folio *folio; >>>>> + long ret; >>>>> + >>>>> + if (!nr_pages) >>>>> + return -EINVAL; >>>>> + >>>>> + if (!memfd) >>>>> + return -EINVAL; >>>>> + >>>>> + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) >>>>> + return -EINVAL; >>>>> + >>>>> + flags = memalloc_pin_save(); >>>>> + do { >>>>> + folio_batch_init(&fbatch); >>>>> + start_idx = start; >>>>> + i = 0; >>>>> + >>>>> + while (start_idx <= end_idx) { >>>>> + /* >>>>> + * In most cases, we should be able to find the page >>>>> + * in the page cache. If we cannot find it for some >>>>> + * reason, we try to allocate one and add it to the >>>>> + * page cache. >>>>> + */ >>>>> + nr_folios = filemap_get_folios_contig(memfd- >>>>> f_mapping, >>>>> + &start_idx, >>>>> + end_idx, >>>>> + &fbatch); >>>>> + if (page) { >>>>> + put_page(page); >>>>> + page = NULL; >>>>> + } >>>>> + for (j = 0; j < nr_folios; j++) { >>>>> + folio = fbatch.folios[j]; >>>>> + ret = try_grab_page(&folio->page, FOLL_PIN); >>>>> + if (unlikely(ret)) { >>>>> + folio_batch_release(&fbatch); >>>>> + goto err; >>>>> + } >>>>> + >>>>> + pages[i++] = &folio->page; >>>>> + } >>>> >>>> I might be wrong, but that interface is still inconsistent. I think your >>>> intention is to always return folios (head pages), but why are we >>>> returning pages from this interface then? >>>> >>>> It would be more consistent regarding the other GUP interfaces to return >>>> the actual tail pages that fit the given "pgoff_t start". So if you >>>> punch in "nr_pages" you expect to get "nr_pages" pages, and not some >>>> other number of folios. >>>> >>>> Otherwise, this interface is highly confusing. >>>> >>>> If you always want to return folios, then better name it >>>> "memfd_pin_user_folios" (or just "memfd_pin_folios") and pass in a >> range >>>> (instead of a nr_pages parameter), and somehow indicate to the caller >>>> how many folio were in that range, and if that range was fully covered. >>> I think it makes sense to return folios from this interface; and considering >> my >>> use-case, I'd like have this API return an error if it cannot pin (or allocate) >>> the exact number of folios the caller requested. >> >> Okay, then better use folios. >> >> Assuming a caller puts in "start = X" and gets some large folio back. >> How is the caller supposed to know at which offset to look into that >> folio (IOW< which subpage)? For "pages" it was obvious (you get the >> actual subpages), but as soon as we return a large folio, some >> information is missing for the caller. >> >> How can the caller figure that out? > I think the caller can deduce the subpage offset one way or the other. For > example, in udmabuf we do: > subpgoff = offset & ~huge_page_mask(hpstate); For hugetlb it's obvious, because we always have fixed-sized chunks for the whole memfd. > Do you think it is reasonable to expect callers to make these deductions? > Or would it make more sense to have the caller pass in an empty array > that this API would eventually fill with offsets? Only the offset into the first folio is really required. From there, it's mostly just processing full folios (well, the last folio might have to be processed partially, but that's easy to figure out by the caller by keeping track of the total range processed vs. requested). Maybe it makes sense to return the offset into the first folio? Would certainly take some magic out of that interface. -- Cheers, David / dhildenb