Skip to content

Commit a2ad1b8

Browse files
Steve Sistarejgunthorpe
authored andcommitted
mm/gup: Add folio_add_pins()
Export a function that adds pins to an already-pinned huge-page folio. This allows any range of small pages within the folio to be unpinned later. For example, pages pinned via memfd_pin_folios and modified by folio_add_pins could be unpinned via unpin_user_page(s). Link: https://patch.msgid.link/r/[email protected] Suggested-by: Jason Gunthorpe <[email protected]> Suggested-by: David Hildenbrand <[email protected]> Signed-off-by: Steve Sistare <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Acked-by: David Hildenbrand <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent e2d8fe9 commit a2ad1b8

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

include/linux/mm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,7 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
25242524
long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
25252525
struct folio **folios, unsigned int max_folios,
25262526
pgoff_t *offset);
2527+
int folio_add_pins(struct folio *folio, unsigned int pins);
25272528

25282529
int get_user_pages_fast(unsigned long start, int nr_pages,
25292530
unsigned int gup_flags, struct page **pages);

mm/gup.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3717,3 +3717,27 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
37173717
return ret;
37183718
}
37193719
EXPORT_SYMBOL_GPL(memfd_pin_folios);
3720+
3721+
/**
3722+
* folio_add_pins() - add pins to an already-pinned folio
3723+
* @folio: the folio to add more pins to
3724+
* @pins: number of pins to add
3725+
*
3726+
* Try to add more pins to an already-pinned folio. The semantics
3727+
* of the pin (e.g., FOLL_WRITE) follow any existing pin and cannot
3728+
* be changed.
3729+
*
3730+
* This function is helpful when having obtained a pin on a large folio
3731+
* using memfd_pin_folios(), but wanting to logically unpin parts
3732+
* (e.g., individual pages) of the folio later, for example, using
3733+
* unpin_user_page_range_dirty_lock().
3734+
*
3735+
* This is not the right interface to initially pin a folio.
3736+
*/
3737+
int folio_add_pins(struct folio *folio, unsigned int pins)
3738+
{
3739+
VM_WARN_ON_ONCE(!folio_maybe_dma_pinned(folio));
3740+
3741+
return try_grab_folio(folio, pins, FOLL_PIN);
3742+
}
3743+
EXPORT_SYMBOL_GPL(folio_add_pins);

0 commit comments

Comments
 (0)