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 2B455C4167B for ; Wed, 6 Dec 2023 11:19:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A34A46B00A9; Wed, 6 Dec 2023 06:19:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BC366B00AA; Wed, 6 Dec 2023 06:19:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C0BA6B00AB; Wed, 6 Dec 2023 06:19:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6364F6B00A9 for ; Wed, 6 Dec 2023 06:19:34 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 403FFA0AC8 for ; Wed, 6 Dec 2023 11:19:34 +0000 (UTC) X-FDA: 81536147868.25.BA69153 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id F2B3D1C0013 for ; Wed, 6 Dec 2023 11:19:30 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=huipIA5+; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701861571; 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=f7roCKr2mcGdqHQUXuuIaroHOsYDCDLJgd8x5ZSB/dY=; b=BcDFm1vqxyn4/pTBM/ZaqIhy5L/oaE34WVP+JK1DV1H/NkanD9bcuCAhYZU0SMeTSwh9+d L5YE80uJK/rHCyvKsUQsOIuoXNpoRaRzTewJuxz2ALPD2lbMpFLnRSRlNZODkAIkRh7M9K l2ENkSZS0PQttPs0+M0XJOJjHiy/JqE= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=huipIA5+; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701861571; a=rsa-sha256; cv=none; b=14vmZZjl5c72dJmdy8suYpEBOcuo02hWzGHlMFs1QOXs32oDhTwGM0iJAPhtCC7yoGTHLn ahOpxpkFv5vIimsu7Xvi9yJHpt8z9cGBG6I7VslJbJIMwZA6TPG/UQZHQ0RbOtql1ErTvK 1X/Nt6/YTrOMxXtIfpSaxrkTEnzZZYw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701861570; 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=f7roCKr2mcGdqHQUXuuIaroHOsYDCDLJgd8x5ZSB/dY=; b=huipIA5+o1QKIR4V+RvpxVWuL1wo9YTSubxq73fKz/t4U5GDuUXqlwAwxTs6jxYmm8OX/V d8RpQpLle1uCFuB7IU1U8cbi4Nm+immYFupMAD1zKnUeA/uEoiElVaymhjd21A1tnBD1wV Z6p7wG0EOQDe2aI3EcXio7ok4KhZI8I= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-269-C-mVBvMoPxavavGV_P5wtQ-1; Wed, 06 Dec 2023 06:19:28 -0500 X-MC-Unique: C-mVBvMoPxavavGV_P5wtQ-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-40b3dbe99d9so51444095e9.1 for ; Wed, 06 Dec 2023 03:19:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701861567; x=1702466367; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=f7roCKr2mcGdqHQUXuuIaroHOsYDCDLJgd8x5ZSB/dY=; b=rhitHBgQn9perZO8NyScCAHc8u0fSmj76V99EQO83h307AOmV9mgKVMT1TFNzALgD5 6B2OCXmeqK0XPF2vpklmqWEdHi8mZXBw4TRpT1CAR3jgLvx0PNxIUwviXBJ6eYrmgduh B1TFj70pxGS8731+iq7BEWVGoU0NGfe9s7jHAqOQrbuRuWHU+XdMUghIC3Pa3SM2f12p 8rKuH7ry0RvgAmI0qmhAc0HcnWS7c04lba0+azSzKha4BtroCMEQwxQ8+z4K3ubsXxJI UmWCez6AmzdyyAeV8Hcj8AGqy0gcq7i3oxOsiOdNMrEttIcbsfA+Uc0CHHz+/iv/nxlr +Z2w== X-Gm-Message-State: AOJu0Yz9+5ie3dLjScAgMgHGG+hlwiCHoX3N7sffmS0llrZqrQyxQhBm QDRd2J7cw8wbtP76yUZbYWfyBmHWzhTxD2KDpliHfPH6n5V8ML7FSsOACW2wPcCkfUCYC0xjQ+K rq9IcWXv/reQ= X-Received: by 2002:a05:600c:5199:b0:40c:2395:1fa7 with SMTP id fa25-20020a05600c519900b0040c23951fa7mr195538wmb.141.1701861567505; Wed, 06 Dec 2023 03:19:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IHo2+sn3lzaBWGdqRBjbmSLaFrvex+tUmSTtXpMdIieqiRbjWBhwTje9Gs7p8SFKTmOHhvygw== X-Received: by 2002:a05:600c:5199:b0:40c:2395:1fa7 with SMTP id fa25-20020a05600c519900b0040c23951fa7mr195523wmb.141.1701861567062; Wed, 06 Dec 2023 03:19:27 -0800 (PST) Received: from ?IPV6:2a09:80c0:192:0:5dac:bf3d:c41:c3e7? ([2a09:80c0:192:0:5dac:bf3d:c41:c3e7]) by smtp.gmail.com with ESMTPSA id p17-20020adff211000000b00333332a8d39sm13520672wro.55.2023.12.06.03.19.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Dec 2023 03:19:26 -0800 (PST) Message-ID: <5ffe2ea3-83da-4b5f-adc8-af9cd9a57cd2@redhat.com> Date: Wed, 6 Dec 2023 12:19:25 +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: Vivek Kasireddy , dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Christoph Hellwig , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe References: <20231205053509.2342169-1-vivek.kasireddy@intel.com> <20231205053509.2342169-4-vivek.kasireddy@intel.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaS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DcDHIlmguXjC1Q5tZTwNB 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: <20231205053509.2342169-4-vivek.kasireddy@intel.com> 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: F2B3D1C0013 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: duzmb7wgysu7frjbeisx1jnbx9fpjjdt X-HE-Tag: 1701861570-784677 X-HE-Meta: U2FsdGVkX1+XdW0Yd75CbCKbJ/LS9fPNBoMLjInD3nBztGQAEEEtB06y9MI0QP5CkYPjTqf4cwKTzkkmXCyizPwOzkbj1rRwA5KAED8pwPx39NXC+29U7Iov/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FtLsuel08Xr8WIbIiAwDHEdkaD51IDW3VhbFPSmfoQuW0uE+gm1QBo4QIGuq68lz+ZnpttRu/bKGSPNq+6TYBkUfy/MZc3PizUQlf5AH9cd1UNO/1jiWJ6GHnYc2oKTF0vQzGkONOS6DMeV6lAxG3piPOHE3anYN/nzyjiLAW5rt0hE2U6UaKK6qlt+hIsnfjfXz1PW/8UOl2KWtG66MvV8KQ0p6UNPucJKLyl1ikRMy4+UqY9ckj4rKBXjMos6dGPrFkzkdecOdKQaPAHEZYZ9EtbjNsY8V2zkY5mOQKoeQNqrBJFyfFRpQVrowPV4DcT6S8S/JR38BkheeBXCxL7MO5C1EhFaQkI89FnZx61xo8pCEMtqwSNDRiysdMjLLiRd9V/SwQI0W22Wg1K5zeWzWYpS9BZCpYdQ6NC9+pMQC7ha6+R9oTbUSuYDgG8TbsO9WHpAtL/d 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 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. Or am I missing something? -- Cheers, David / dhildenb