Skip to contentSkip to conversation

iov_iter: Improve page extraction (pin or just list)

PATCH Superseded David Howells 3 participants 33 messages Jan 24, 2023 – Jan 25, 2023

Subsystems (6)

v9 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v9 cover iov_iter: Improve page extraction (pin or just list)

Hi Al, Christoph,

Here are patches to provide support for extracting pages from an iov_iter and to use this in the extraction functions in the block layer bio code.

The patches make the following changes:

(1) Add a function, iov_iter_extract_pages() to replace iov_iter_get_pages*() that gets refs, pins or just lists the pages as appropriate to the iterator type.

Add a function, iov_iter_extract_will_pin() that will indicate from the iterator type how the cleanup is to be performed, returning true if the pages will need unpinning, false otherwise.

(2) Make the bio struct carry a pair of flags to indicate the cleanup mode. BIO_NO_PAGE_REF is replaced with BIO_PAGE_REFFED (indicating FOLL_GET was used) and BIO_PAGE_PINNED (indicating FOLL_PIN was used) is added.

BIO_PAGE_REFFED will go away, but at the moment fs/direct-io.c sets it and this series does not fully address that file.

(4) Add a function, bio_release_page(), to release a page appropriately to the cleanup mode indicated by the BIO_PAGE_* flags.

(5) Make the iter-to-bio code use iov_iter_extract_pages() to retain the pages appropriately and clean them up later.

(6) Fix bio_flagged() so that it doesn't prevent a gcc optimisation.

I've pushed the patches here also:

https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=iov-extract

David

Changes:
========
ver #9)
- It's now not permitted to use FOLL_PIN outside of mm/, so:
- Change iov_iter_extract_mode() into iov_iter_extract_will_pin() and return true/false instead of FOLL_PIN/0.
- Drop of folio_put_unpin() and page_put_unpin() and instead call unpin_user_page() (and put_page()) directly as necessary.
- Make __bio_release_pages() call bio_release_page() instead of unpin_user_page() as there's no BIO_* -> FOLL_* translation to do.
- Drop the FOLL_* renumbering patch.
- Change extract_flags to extraction_flags.

ver #8)
- Import Christoph Hellwig's changes.
- Split the conversion-to-extraction patch.
- Drop the extract_flags arg from iov_iter_extract_mode().
- Don't default bios to BIO_PAGE_REFFED, but set explicitly.
- Switch FOLL_PIN and FOLL_GET when renumbering so PIN is at bit 0.
- Switch BIO_PAGE_PINNED and BIO_PAGE_REFFED so PINNED is at bit 0.
- We should always be using FOLL_PIN (not FOLL_GET) for DIO, so adjust the patches for that.

ver #7)
- For now, drop the parts to pass the I/O direction to iov_iter_*pages*() as it turned out to be a lot more complicated, with places not setting IOCB_WRITE when they should, for example.
- Drop all the patches that changed things other then the block layer's bio handling. The netfslib and cifs changes can go into a separate patchset.
- Add support for extracting pages from KVEC-type iterators.
- When extracting from BVEC/KVEC, skip over empty vecs at the front.

ver #6)
- Fix write() syscall and co. not setting IOCB_WRITE.
- Added iocb_is_read() and iocb_is_write() to check IOCB_WRITE.
- Use op_is_write() in bio_copy_user_iov().
- Drop the iterator direction checks from smbd_recv().
- Define FOLL_SOURCE_BUF and FOLL_DEST_BUF and pass them in as part of gup_flags to iov_iter_get/extract_pages*().
- Replace iov_iter_get_pages*2() with iov_iter_get_pages*() and remove.
- Add back the function to indicate the cleanup mode.
- Drop the cleanup_mode return arg to iov_iter_extract_pages().
- Provide a helper to clean up a page.
- Renumbered FOLL_GET and FOLL_PIN and made BIO_PAGE_REFFED/PINNED have the same numerical values, enforced with an assertion.
- Converted AF_ALG, SCSI vhost, generic DIO, FUSE, splice to pipe, 9P and NFS.
- Added in the patches to make CIFS do top-to-bottom iterators and use various of the added extraction functions.
- Added a pair of work-in-progess patches to make sk_buff fragments store FOLL_GET and FOLL_PIN.

ver #5)
- Replace BIO_NO_PAGE_REF with BIO_PAGE_REFFED and split into own patch.
- Transcribe FOLL_GET/PIN into BIO_PAGE_REFFED/PINNED flags.
- Add patch to allow bio_flagged() to be combined by gcc.

ver #4)
- Drop the patch to move the FOLL_* flags to linux/mm_types.h as they're no longer referenced by linux/uio.h.
- Add ITER_SOURCE/DEST cleanup patches.
- Make iov_iter/netfslib iter extraction patches use ITER_SOURCE/DEST.
- Allow additional gup_flags to be passed into iov_iter_extract_pages().
- Add struct bio patch.

ver #3)
- Switch to using EXPORT_SYMBOL_GPL to prevent indirect 3rd-party access to get/pin_user_pages_fast()[1].

ver #2)
- Rolled the extraction cleanup mode query function into the extraction function, returning the indication through the argument list.
- Fixed patch 4 (extract to scatterlist) to actually use the new extraction API.

Christoph Hellwig (1):
block: Replace BIO_NO_PAGE_REF with BIO_PAGE_REFFED with inverted
logic

David Howells (7):
iov_iter: Define flags to qualify page extraction.
iov_iter: Add a function to extract a page list from an iterator
iomap: Don't get an reference on ZERO_PAGE for direct I/O block
zeroing
block: Fix bio_flagged() so that gcc can better optimise it
block: Switch to pinning pages.
block: Convert bio_iov_iter_get_pages to use iov_iter_extract_pages
block: convert bio_map_user_iov to use iov_iter_extract_pages

 block/bio.c               |  32 ++--
 block/blk-map.c           |  25 ++-
 block/blk.h               |  21 +++
 fs/direct-io.c            |   2 +
 fs/iomap/direct-io.c      |   1 -
 include/linux/bio.h       |   5 +-
 include/linux/blk_types.h |   3 +-
 include/linux/uio.h       |  32 +++-
 lib/iov_iter.c            | 335 +++++++++++++++++++++++++++++++++++++-
 9 files changed, 415 insertions(+), 41 deletions(-)
#1 lore raw

PATCH v9 1/8 iov_iter: Define flags to qualify page extraction.

Define flags to qualify page extraction to pass into iov_iter_*_pages*() rather than passing in FOLL_* flags.

For now only a flag to allow peer-to-peer DMA is supported.

---

Notes:
ver #9)
- Change extract_flags to extraction_flags.

ver #7)
- Don't use FOLL_* as a parameter, but rather define constants specifically to use with iov_iter_*_pages*().
- Drop the I/O direction constants for now.

 block/bio.c         |  6 +++---
 block/blk-map.c     |  8 ++++----
 include/linux/uio.h |  7 +++++--
 lib/iov_iter.c      | 14 ++++++++------
 4 files changed, 20 insertions(+), 15 deletions(-)
block/bio.c +3 -3
index ab59a491a883..683444e6b711 100644
@@ -1249,7 +1249,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
 	struct page **pages = (struct page **)bv;
-	unsigned int gup_flags = 0;
+	unsigned int extraction_flags = 0;
 	ssize_t size, left;
 	unsigned len, i = 0;
 	size_t offset, trim;
@@ -1264,7 +1264,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
 
 	if (bio->bi_bdev && blk_queue_pci_p2pdma(bio->bi_bdev->bd_disk->queue))
-		gup_flags |= FOLL_PCI_P2PDMA;
+		extraction_flags |= ITER_ALLOW_P2PDMA;
 
 	/*
 	 * Each segment in the iov is required to be a block size multiple.
@@ -1275,7 +1275,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 	 */
 	size = iov_iter_get_pages(iter, pages,
 				  UINT_MAX - bio->bi_iter.bi_size,
-				  nr_pages, &offset, gup_flags);
+				  nr_pages, &offset, extraction_flags);
 	if (unlikely(size <= 0))
 		return size ? size : -EFAULT;
 
block/blk-map.c +4 -4
index 19940c978c73..7db52ad5b2d0 100644
@@ -267,7 +267,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
 {
 	unsigned int max_sectors = queue_max_hw_sectors(rq->q);
 	unsigned int nr_vecs = iov_iter_npages(iter, BIO_MAX_VECS);
-	unsigned int gup_flags = 0;
+	unsigned int extraction_flags = 0;
 	struct bio *bio;
 	int ret;
 	int j;
@@ -280,7 +280,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
 		return -ENOMEM;
 
 	if (blk_queue_pci_p2pdma(rq->q))
-		gup_flags |= FOLL_PCI_P2PDMA;
+		extraction_flags |= ITER_ALLOW_P2PDMA;
 
 	while (iov_iter_count(iter)) {
 		struct page **pages, *stack_pages[UIO_FASTIOV];
@@ -291,10 +291,10 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
 		if (nr_vecs <= ARRAY_SIZE(stack_pages)) {
 			pages = stack_pages;
 			bytes = iov_iter_get_pages(iter, pages, LONG_MAX,
-						   nr_vecs, &offs, gup_flags);
+						   nr_vecs, &offs, extraction_flags);
 		} else {
 			bytes = iov_iter_get_pages_alloc(iter, &pages,
-						LONG_MAX, &offs, gup_flags);
+						LONG_MAX, &offs, extraction_flags);
 		}
 		if (unlikely(bytes <= 0)) {
 			ret = bytes ? bytes : -EFAULT;
include/linux/uio.h +5 -2
index 9f158238edba..58fda77f6847 100644
@@ -252,12 +252,12 @@ void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xarray *
 		     loff_t start, size_t count);
 ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
 		size_t maxsize, unsigned maxpages, size_t *start,
-		unsigned gup_flags);
+		unsigned extraction_flags);
 ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
 			size_t maxsize, unsigned maxpages, size_t *start);
 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
 		struct page ***pages, size_t maxsize, size_t *start,
-		unsigned gup_flags);
+		unsigned extraction_flags);
 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, struct page ***pages,
 			size_t maxsize, size_t *start);
 int iov_iter_npages(const struct iov_iter *i, int maxpages);
@@ -360,4 +360,7 @@ static inline void iov_iter_ubuf(struct iov_iter *i, unsigned int direction,
 	};
 }
 
+/* Flags for iov_iter_get/extract_pages*() */
+#define ITER_ALLOW_P2PDMA	0x01	/* Allow P2PDMA on the extracted pages */
+
 #endif
lib/iov_iter.c +8 -6
index f9a3ff37ecd1..da7db39075c6 100644
@@ -1432,9 +1432,9 @@ static struct page *first_bvec_segment(const struct iov_iter *i,
 static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 		   struct page ***pages, size_t maxsize,
 		   unsigned int maxpages, size_t *start,
-		   unsigned int gup_flags)
+		   unsigned int extraction_flags)
 {
-	unsigned int n;
+	unsigned int n, gup_flags = 0;
 
 	if (maxsize > i->count)
 		maxsize = i->count;
@@ -1442,6 +1442,8 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 		return 0;
 	if (maxsize > MAX_RW_COUNT)
 		maxsize = MAX_RW_COUNT;
+	if (extraction_flags & ITER_ALLOW_P2PDMA)
+		gup_flags |= FOLL_PCI_P2PDMA;
 
 	if (likely(user_backed_iter(i))) {
 		unsigned long addr;
@@ -1495,14 +1497,14 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 
 ssize_t iov_iter_get_pages(struct iov_iter *i,
 		   struct page **pages, size_t maxsize, unsigned maxpages,
-		   size_t *start, unsigned gup_flags)
+		   size_t *start, unsigned extraction_flags)
 {
 	if (!maxpages)
 		return 0;
 	BUG_ON(!pages);
 
 	return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages,
-					  start, gup_flags);
+					  start, extraction_flags);
 }
 EXPORT_SYMBOL_GPL(iov_iter_get_pages);
 
@@ -1515,14 +1517,14 @@ EXPORT_SYMBOL(iov_iter_get_pages2);
 
 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
 		   struct page ***pages, size_t maxsize,
-		   size_t *start, unsigned gup_flags)
+		   size_t *start, unsigned extraction_flags)
 {
 	ssize_t len;
 
 	*pages = NULL;
 
 	len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start,
-					 gup_flags);
+					 extraction_flags);
 	if (len <= 0) {
 		kvfree(*pages);
 		*pages = NULL;
Reviewed-by on v9 1/8
#3 lore raw

Re: [PATCH v9 1/8] iov_iter: Define flags to qualify page extraction.

In reply to David Howells
Quoted 12 lines · David Howells

Define flags to qualify page extraction to pass into iov_iter_*_pages*() rather than passing in FOLL_* flags.

For now only a flag to allow peer-to-peer DMA is supported.

Quoted 5 lines

---

Notes:
ver #9)
- Change extract_flags to extraction_flags.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 165 lines

ver #7)
- Don't use FOLL_* as a parameter, but rather define constants specifically to use with iov_iter_*_pages*().
- Drop the I/O direction constants for now.

  block/bio.c         |  6 +++---
  block/blk-map.c     |  8 ++++----
  include/linux/uio.h |  7 +++++--
  lib/iov_iter.c      | 14 ++++++++------
  4 files changed, 20 insertions(+), 15 deletions(-)
block/bio.c +3 -3
index ab59a491a883..683444e6b711 100644
@@ -1249,7 +1249,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
  	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
  	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
  	struct page **pages = (struct page **)bv;
-	unsigned int gup_flags = 0;
+	unsigned int extraction_flags = 0;
  	ssize_t size, left;
  	unsigned len, i = 0;
  	size_t offset, trim;
@@ -1264,7 +1264,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
  	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
 
  	if (bio->bi_bdev && blk_queue_pci_p2pdma(bio->bi_bdev->bd_disk->queue))
-		gup_flags |= FOLL_PCI_P2PDMA;
+		extraction_flags |= ITER_ALLOW_P2PDMA;
 
  	/*
  	 * Each segment in the iov is required to be a block size multiple.
@@ -1275,7 +1275,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
  	 */
  	size = iov_iter_get_pages(iter, pages,
  				  UINT_MAX - bio->bi_iter.bi_size,
-				  nr_pages, &offset, gup_flags);
+				  nr_pages, &offset, extraction_flags);
  	if (unlikely(size <= 0))
  		return size ? size : -EFAULT;
 
block/blk-map.c +4 -4
index 19940c978c73..7db52ad5b2d0 100644
@@ -267,7 +267,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
  {
  	unsigned int max_sectors = queue_max_hw_sectors(rq->q);
  	unsigned int nr_vecs = iov_iter_npages(iter, BIO_MAX_VECS);
-	unsigned int gup_flags = 0;
+	unsigned int extraction_flags = 0;
  	struct bio *bio;
  	int ret;
  	int j;
@@ -280,7 +280,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
  		return -ENOMEM;
 
  	if (blk_queue_pci_p2pdma(rq->q))
-		gup_flags |= FOLL_PCI_P2PDMA;
+		extraction_flags |= ITER_ALLOW_P2PDMA;
 
  	while (iov_iter_count(iter)) {
  		struct page **pages, *stack_pages[UIO_FASTIOV];
@@ -291,10 +291,10 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
  		if (nr_vecs <= ARRAY_SIZE(stack_pages)) {
  			pages = stack_pages;
  			bytes = iov_iter_get_pages(iter, pages, LONG_MAX,
-						   nr_vecs, &offs, gup_flags);
+						   nr_vecs, &offs, extraction_flags);
  		} else {
  			bytes = iov_iter_get_pages_alloc(iter, &pages,
-						LONG_MAX, &offs, gup_flags);
+						LONG_MAX, &offs, extraction_flags);
  		}
  		if (unlikely(bytes <= 0)) {
  			ret = bytes ? bytes : -EFAULT;
include/linux/uio.h +5 -2
index 9f158238edba..58fda77f6847 100644
@@ -252,12 +252,12 @@ void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xarray *
  		     loff_t start, size_t count);
  ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
  		size_t maxsize, unsigned maxpages, size_t *start,
-		unsigned gup_flags);
+		unsigned extraction_flags);
  ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
  			size_t maxsize, unsigned maxpages, size_t *start);
  ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
  		struct page ***pages, size_t maxsize, size_t *start,
-		unsigned gup_flags);
+		unsigned extraction_flags);
  ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, struct page ***pages,
  			size_t maxsize, size_t *start);
  int iov_iter_npages(const struct iov_iter *i, int maxpages);
@@ -360,4 +360,7 @@ static inline void iov_iter_ubuf(struct iov_iter *i, unsigned int direction,
  	};
  }
 
+/* Flags for iov_iter_get/extract_pages*() */
+#define ITER_ALLOW_P2PDMA	0x01	/* Allow P2PDMA on the extracted pages */
+
  #endif
lib/iov_iter.c +8 -6
index f9a3ff37ecd1..da7db39075c6 100644
@@ -1432,9 +1432,9 @@ static struct page *first_bvec_segment(const struct iov_iter *i,
  static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
  		   struct page ***pages, size_t maxsize,
  		   unsigned int maxpages, size_t *start,
-		   unsigned int gup_flags)
+		   unsigned int extraction_flags)
  {
-	unsigned int n;
+	unsigned int n, gup_flags = 0;
 
  	if (maxsize > i->count)
  		maxsize = i->count;
@@ -1442,6 +1442,8 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
  		return 0;
  	if (maxsize > MAX_RW_COUNT)
  		maxsize = MAX_RW_COUNT;
+	if (extraction_flags & ITER_ALLOW_P2PDMA)
+		gup_flags |= FOLL_PCI_P2PDMA;
 
  	if (likely(user_backed_iter(i))) {
  		unsigned long addr;
@@ -1495,14 +1497,14 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 
  ssize_t iov_iter_get_pages(struct iov_iter *i,
  		   struct page **pages, size_t maxsize, unsigned maxpages,
-		   size_t *start, unsigned gup_flags)
+		   size_t *start, unsigned extraction_flags)
  {
  	if (!maxpages)
  		return 0;
  	BUG_ON(!pages);
 
  	return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages,
-					  start, gup_flags);
+					  start, extraction_flags);
  }
  EXPORT_SYMBOL_GPL(iov_iter_get_pages);
 
@@ -1515,14 +1517,14 @@ EXPORT_SYMBOL(iov_iter_get_pages2);
 
  ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
  		   struct page ***pages, size_t maxsize,
-		   size_t *start, unsigned gup_flags)
+		   size_t *start, unsigned extraction_flags)
  {
  	ssize_t len;
 
  	*pages = NULL;
 
  	len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start,
-					 gup_flags);
+					 extraction_flags);
  	if (len <= 0) {
  		kvfree(*pages);
  		*pages = NULL;
#4 lore raw

PATCH v9 2/8 iov_iter: Add a function to extract a page list from an iterator

In reply to David Howells

Add a function, iov_iter_extract_pages(), to extract a list of pages from an iterator. The pages may be returned with a pin added or nothing, depending on the type of iterator.

Add a second function, iov_iter_extract_will_pin(), to determine how the cleanup should be done.

There are two cases:

(1) ITER_IOVEC or ITER_UBUF iterator.

Extracted pages will have pins (FOLL_PIN) obtained on them so that a concurrent fork() will forcibly copy the page so that DMA is done to/from the parent's buffer and is unavailable to/unaffected by the child process.

iov_iter_extract_will_pin() will return true for this case. The caller should use something like unpin_user_page() to dispose of the page.

(2) Any other sort of iterator.

No refs or pins are obtained on the page, the assumption is made that the caller will manage page retention.

iov_iter_extract_will_pin() will return false. The pages don't need additional disposal.

cc Al Viro <[email protected]>
cc Christoph Hellwig <[email protected]>
cc John Hubbard <[email protected]>
cc David Hildenbrand <[email protected]>
cc Matthew Wilcox <[email protected]>

---

Notes:
ver #9)
- Rename iov_iter_extract_mode() to iov_iter_extract_will_pin() and make it return true/false not FOLL_PIN/0 as FOLL_PIN is going to be made private to mm/.
- Change extract_flags to extraction_flags.

ver #8)
- It seems that all DIO is supposed to be done under FOLL_PIN now, and not FOLL_GET, so switch to only using pin_user_pages() for user-backed iters.
- Wrap an argument in brackets in the iov_iter_extract_mode() macro.
- Drop the extract_flags argument to iov_iter_extract_mode() for now [hch].

ver #7)
- Switch to passing in iter-specific flags rather than FOLL_* flags.
- Drop the direction flags for now.
- Use ITER_ALLOW_P2PDMA to request FOLL_PCI_P2PDMA.
- Disallow use of ITER_ALLOW_P2PDMA with non-user-backed iter.
- Add support for extraction from KVEC-type iters.
- Use iov_iter_advance() rather than open-coding it.
- Make BVEC- and KVEC-type skip over initial empty vectors.

ver #6)
- Add back the function to indicate the cleanup mode.
- Drop the cleanup_mode return arg to iov_iter_extract_pages().
- Pass FOLL_SOURCE/DEST_BUF in gup_flags. Check this against the iter data_source.

ver #4)
- Use ITER_SOURCE/DEST instead of WRITE/READ.
- Allow additional FOLL_* flags, such as FOLL_PCI_P2PDMA to be passed in.

ver #3)
- Switch to using EXPORT_SYMBOL_GPL to prevent indirect 3rd-party access to get/pin_user_pages_fast()[1].

 include/linux/uio.h |  25 ++++
 lib/iov_iter.c      | 321 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 346 insertions(+)
include/linux/uio.h +25 -0
index 58fda77f6847..47ebb59a0202 100644
@@ -363,4 +363,29 @@ static inline void iov_iter_ubuf(struct iov_iter *i, unsigned int direction,
 /* Flags for iov_iter_get/extract_pages*() */
 #define ITER_ALLOW_P2PDMA	0x01	/* Allow P2PDMA on the extracted pages */
 
+ssize_t iov_iter_extract_pages(struct iov_iter *i, struct page ***pages,
+			       size_t maxsize, unsigned int maxpages,
+			       unsigned int extraction_flags, size_t *offset0);
+
+/**
+ * iov_iter_extract_will_pin - Indicate how pages from the iterator will be retained
+ * @iter: The iterator
+ *
+ * Examine the iterator and indicate by returning true or false as to how, if
+ * at all, pages extracted from the iterator will be retained by the extraction
+ * function.
+ *
+ * %true indicates that the pages will have a pin placed in them that the
+ * caller must unpin.  This is must be done for DMA/async DIO to force fork()
+ * to forcibly copy a page for the child (the parent must retain the original
+ * page).
+ *
+ * %false indicates that no measures are taken and that it's up to the caller
+ * to retain the pages.
+ */
+static inline bool iov_iter_extract_will_pin(const struct iov_iter *iter)
+{
+	return user_backed_iter(iter);
+}
+
 #endif
lib/iov_iter.c +321 -0

324 lines not on this page · raw message

#6 lore raw

Re: [PATCH v9 2/8] iov_iter: Add a function to extract a page list from an iterator

In reply to David Howells

On 1/24/23 09:01, David Howells wrote:
...

Quoted 24 lines
lib/iov_iter.c +24 -0
+/*
+ * Extract a list of contiguous pages from an ITER_BVEC iterator.  This does
+ * not get references on the pages, nor does it get a pin on them.
+ */
+static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i,
+					   struct page ***pages, size_t maxsize,
+					   unsigned int maxpages,
+					   unsigned int extraction_flags,
+					   size_t *offset0)
+{
+	struct page **p, *page;
+	size_t skip = i->iov_offset, offset;
+	int k;
+
+	for (;;) {
+		if (i->nr_segs == 0)
+			return 0;
+		maxsize = min(maxsize, i->bvec->bv_len - skip);
+		if (maxsize)
+			break;
+		i->iov_offset = 0;
+		i->nr_segs--;
+		i->kvec++;
+		skip = 0;

lib/iov_iter.c void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) · line 2046

Quoted 10 lines
lib/iov_iter.c +10 -0
+	}
+
+	skip += i->bvec->bv_offset;
+	page = i->bvec->bv_page + skip / PAGE_SIZE;
+	offset = skip % PAGE_SIZE;
+	*offset0 = offset;
+
+	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
+	if (!maxpages)
+		return -ENOMEM;

Is it OK that the iov_iter position has been advanced, and left that way, in the case of an early -ENOMEM return here?

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#7 lore raw

Re: [PATCH v9 2/8] iov_iter: Add a function to extract a page list from an iterator

Quoted 15 lines · John Hubbard
Quoted 15 lines
lib/iov_iter.c +15 -0
+	for (;;) {
+		if (i->nr_segs == 0)
+			return 0;
+		maxsize = min(maxsize, i->bvec->bv_len - skip);
+		if (maxsize)
+			break;
+		i->iov_offset = 0;
+		i->nr_segs--;
+		i->kvec++;
+		skip = 0;
+	}
+
+	skip += i->bvec->bv_offset;
+	page = i->bvec->bv_page + skip / PAGE_SIZE;
+	offset = skip % PAGE_SIZE;
Quoted 8 lines

lib/iov_iter.c void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) · line 1950

Quoted 5 lines
lib/iov_iter.c +5 -0
+	*offset0 = offset;
+
+	maxpages = want_pages_array(pages, maxsize, offset, maxpages);
+	if (!maxpages)
+		return -ENOMEM;

Is it OK that the iov_iter position has been advanced, and left that way, in the case of an early -ENOMEM return here?

I think it should be okay. The for-loop at the top just skips over empty segments, so it doesn't really advance things. There is an error there,
though: it should be i->bvec++, not i->kvec++ in the loop.

David

#8 lore raw

PATCH v9 3/8 iomap: Don't get an reference on ZERO_PAGE for direct I/O block zeroing

In reply to David Howells

ZERO_PAGE can't go away, no need to hold an extra reference.

---
 fs/iomap/direct-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/iomap/direct-io.c +1 -1
index 9804714b1751..47db4ead1e74 100644
@@ -202,7 +202,7 @@ static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
 	bio->bi_private = dio;
 	bio->bi_end_io = iomap_dio_bio_end_io;
 
-	get_page(page);
+	bio_set_flag(bio, BIO_NO_PAGE_REF);
 	__bio_add_page(bio, page, len, 0);
 	iomap_dio_submit_bio(iter, dio, bio, pos);
 }
#10 lore raw

Re: [PATCH v9 3/8] iomap: Don't get an reference on ZERO_PAGE for direct I/O block zeroing

Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · David Howells

ZERO_PAGE can't go away, no need to hold an extra reference.

If you send this on this needs your signoff as well, btw.

Um. You quoted my signoff. Do you mean your signoff?

David

#11 lore raw

Re: [PATCH v9 3/8] iomap: Don't get an reference on ZERO_PAGE for direct I/O block zeroing

Quoted 11 lines · David Howells
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · David Howells

ZERO_PAGE can't go away, no need to hold an extra reference.

If you send this on this needs your signoff as well, btw.

Um. You quoted my signoff. Do you mean your signoff?

Umm, I'm confused because you had my signoff on the last version :) The patch is ok as-is.

Reviewed-by on v9 3/8
#12 lore raw

Re: [PATCH v9 3/8] iomap: Don't get an reference on ZERO_PAGE for direct I/O block zeroing

In reply to David Howells
Quoted 8 lines · David Howells

ZERO_PAGE can't go away, no need to hold an extra reference.

---
  fs/iomap/direct-io.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 14 lines
fs/iomap/direct-io.c +1 -1
index 9804714b1751..47db4ead1e74 100644
@@ -202,7 +202,7 @@ static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
  	bio->bi_private = dio;
  	bio->bi_end_io = iomap_dio_bio_end_io;
 
-	get_page(page);
+	bio_set_flag(bio, BIO_NO_PAGE_REF);
  	__bio_add_page(bio, page, len, 0);
  	iomap_dio_submit_bio(iter, dio, bio, pos);
  }
#13 lore raw

PATCH v9 4/8 block: Fix bio_flagged() so that gcc can better optimise it

In reply to David Howells

Fix bio_flagged() so that multiple instances of it, such as:

	if (bio_flagged(bio, BIO_PAGE_REFFED) ||
	    bio_flagged(bio, BIO_PAGE_PINNED))

can be combined by the gcc optimiser into a single test in assembly (arguably, this is a compiler optimisation issue[1]).

The missed optimisation stems from bio_flagged() comparing the result of the bitwise-AND to zero. This results in an out-of-line bio_release_page() being compiled to something like:

   <+0>:     mov    0x14(%rdi),%eax
   <+3>:     test   $0x1,%al
   <+5>:     jne    0xffffffff816dac53 <bio_release_pages+11>
   <+7>:     test   $0x2,%al
   <+9>:     je     0xffffffff816dac5c <bio_release_pages+20>
   <+11>:    movzbl %sil,%esi
   <+15>:    jmp    0xffffffff816daba1 <__bio_release_pages>
   <+20>:    jmp    0xffffffff81d0b800 <__x86_return_thunk>

However, the test is superfluous as the return type is bool. Removing it results in:

   <+0>:     testb  $0x3,0x14(%rdi)
   <+4>:     je     0xffffffff816e4af4 <bio_release_pages+15>
   <+6>:     movzbl %sil,%esi
   <+10>:    jmp    0xffffffff816dab7c <__bio_release_pages>
   <+15>:    jmp    0xffffffff81d0b7c0 <__x86_return_thunk>

instead.

Also, the MOVZBL instruction looks unnecessary[2] - I think it's just 're-booling' the mark_dirty parameter.

---
 include/linux/bio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bio.h +1 -1
index c1da63f6c808..10366b8bdb13 100644
@@ -227,7 +227,7 @@ static inline void bio_cnt_set(struct bio *bio, unsigned int count)
 
 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
 {
-	return (bio->bi_flags & (1U << bit)) != 0;
+	return bio->bi_flags & (1U << bit);
 }
 
 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
Reviewed-by on v9 4/8
#14 lore raw

Re: [PATCH v9 4/8] block: Fix bio_flagged() so that gcc can better optimise it

Quoted 52 lines · David Howells

Fix bio_flagged() so that multiple instances of it, such as:

	if (bio_flagged(bio, BIO_PAGE_REFFED) ||
	    bio_flagged(bio, BIO_PAGE_PINNED))

can be combined by the gcc optimiser into a single test in assembly (arguably, this is a compiler optimisation issue[1]).

The missed optimisation stems from bio_flagged() comparing the result of the bitwise-AND to zero. This results in an out-of-line bio_release_page() being compiled to something like:

    <+0>:     mov    0x14(%rdi),%eax
    <+3>:     test   $0x1,%al
    <+5>:     jne    0xffffffff816dac53 <bio_release_pages+11>
    <+7>:     test   $0x2,%al
    <+9>:     je     0xffffffff816dac5c <bio_release_pages+20>
    <+11>:    movzbl %sil,%esi
    <+15>:    jmp    0xffffffff816daba1 <__bio_release_pages>
    <+20>:    jmp    0xffffffff81d0b800 <__x86_return_thunk>

However, the test is superfluous as the return type is bool. Removing it results in:

    <+0>:     testb  $0x3,0x14(%rdi)
    <+4>:     je     0xffffffff816e4af4 <bio_release_pages+15>
    <+6>:     movzbl %sil,%esi
    <+10>:    jmp    0xffffffff816dab7c <__bio_release_pages>
    <+15>:    jmp    0xffffffff81d0b7c0 <__x86_return_thunk>

instead.

Also, the MOVZBL instruction looks unnecessary[2] - I think it's just 're-booling' the mark_dirty parameter.

---
  include/linux/bio.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bio.h +0 -0
index c1da63f6c808..10366b8bdb13 100644

include/linux/bio.h static inline void bio_cnt_set(struct bio *bio, unsigned int count) · line 233

Quoted 8 lines
include/linux/bio.h +1 -1
@@ -227,7 +227,7 @@ static inline void bio_cnt_set(struct bio *bio, unsigned int count)
 
  static inline bool bio_flagged(struct bio *bio, unsigned int bit)
  {
-	return (bio->bi_flags & (1U << bit)) != 0;
+	return bio->bi_flags & (1U << bit);
  }
 
  static inline void bio_set_flag(struct bio *bio, unsigned int bit)

I don't know how you noticed that this was even a problem! Neatly fixed.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#15 lore raw

Re: [PATCH v9 4/8] block: Fix bio_flagged() so that gcc can better optimise it

Quoted 2 lines · John Hubbard

I don't know how you noticed that this was even a problem! Neatly fixed.

I wanted BIO_PAGE_REFFED/PINNED to translate to FOLL_GET/PIN with no more than a single AND instruction, assuming they were assigned to the same values (1 & 2), so I checked to see what assembly was produced by:

	gup_flags |= bio_flagged(bio, BIO_PAGE_REFFED) ? FOLL_GET : 0;
	gup_flags |= bio_flagged(bio, BIO_PAGE_PINNED) ? FOLL_PIN : 0;

Complicated though it looks, it should optimise down to something like:

	and $3,%eax

assuming something like REFFED/GET == 0x1 and PINNED/PIN == 0x2.

David

#16 lore raw

PATCH v9 5/8 block: Replace BIO_NO_PAGE_REF with BIO_PAGE_REFFED with inverted logic

In reply to David Howells

From: Christoph Hellwig <[email protected]>

Replace BIO_NO_PAGE_REF with a BIO_PAGE_REFFED flag that has the inverted meaning is only set when a page reference has been acquired that needs to be released by bio_release_pages().

cc Al Viro <[email protected]>
cc Jens Axboe <[email protected]>
cc Jan Kara <[email protected]>
cc Matthew Wilcox <[email protected]>
cc Logan Gunthorpe <[email protected]>

---

Notes:
ver #8)
- Don't default to BIO_PAGE_REFFED [hch].

ver #5)
- Split from patch that uses iov_iter_extract_pages().

 block/bio.c               | 2 +-
 block/blk-map.c           | 1 +
 fs/direct-io.c            | 2 ++
 fs/iomap/direct-io.c      | 1 -
 include/linux/bio.h       | 2 +-
 include/linux/blk_types.h | 2 +-
 6 files changed, 6 insertions(+), 4 deletions(-)
block/bio.c +1 -1
index 683444e6b711..851c23641a0d 100644
@@ -1198,7 +1198,6 @@ void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
 	bio->bi_io_vec = (struct bio_vec *)iter->bvec;
 	bio->bi_iter.bi_bvec_done = iter->iov_offset;
 	bio->bi_iter.bi_size = size;
-	bio_set_flag(bio, BIO_NO_PAGE_REF);
 	bio_set_flag(bio, BIO_CLONED);
 }
 
@@ -1343,6 +1342,7 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 		return 0;
 	}
 
+	bio_set_flag(bio, BIO_PAGE_REFFED);
 	do {
 		ret = __bio_iov_iter_get_pages(bio, iter);
 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
block/blk-map.c +1 -0
index 7db52ad5b2d0..0e2b0a861ba3 100644
@@ -282,6 +282,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
 	if (blk_queue_pci_p2pdma(rq->q))
 		extraction_flags |= ITER_ALLOW_P2PDMA;
 
+	bio_set_flag(bio, BIO_PAGE_REFFED);
 	while (iov_iter_count(iter)) {
 		struct page **pages, *stack_pages[UIO_FASTIOV];
 		ssize_t bytes;
fs/direct-io.c +2 -0
index 03d381377ae1..07810465fc9d 100644
@@ -403,6 +403,8 @@ dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
 		bio->bi_end_io = dio_bio_end_aio;
 	else
 		bio->bi_end_io = dio_bio_end_io;
+	/* for now require references for all pages */
+	bio_set_flag(bio, BIO_PAGE_REFFED);
 	sdio->bio = bio;
 	sdio->logical_offset_in_bio = sdio->cur_page_fs_offset;
 }
fs/iomap/direct-io.c +0 -1
index 47db4ead1e74..c0e75900e754 100644
@@ -202,7 +202,6 @@ static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
 	bio->bi_private = dio;
 	bio->bi_end_io = iomap_dio_bio_end_io;
 
-	bio_set_flag(bio, BIO_NO_PAGE_REF);
 	__bio_add_page(bio, page, len, 0);
 	iomap_dio_submit_bio(iter, dio, bio, pos);
 }
include/linux/bio.h +1 -1
index 10366b8bdb13..805957c99147 100644
@@ -484,7 +484,7 @@ void zero_fill_bio(struct bio *bio);
 
 static inline void bio_release_pages(struct bio *bio, bool mark_dirty)
 {
-	if (!bio_flagged(bio, BIO_NO_PAGE_REF))
+	if (bio_flagged(bio, BIO_PAGE_REFFED))
 		__bio_release_pages(bio, mark_dirty);
 }
 
include/linux/blk_types.h +1 -1
index 99be590f952f..7daa261f4f98 100644
@@ -318,7 +318,7 @@ struct bio {
  * bio flags
  */
 enum {
-	BIO_NO_PAGE_REF,	/* don't put release vec pages */
+	BIO_PAGE_REFFED,	/* put pages in bio_release_pages() */
 	BIO_CLONED,		/* doesn't own data */
 	BIO_BOUNCED,		/* bio is a bounce bio */
 	BIO_QUIET,		/* Make BIO Quiet */
Reviewed-by on v9 5/8
#18 lore raw

Re: [PATCH v9 5/8] block: Replace BIO_NO_PAGE_REF with BIO_PAGE_REFFED with inverted logic

In reply to David Howells
Quoted 22 lines · David Howells

From: Christoph Hellwig <[email protected]>

Replace BIO_NO_PAGE_REF with a BIO_PAGE_REFFED flag that has the inverted meaning is only set when a page reference has been acquired that needs to be released by bio_release_pages().

cc Al Viro <[email protected]>
cc Jens Axboe <[email protected]>
cc Jan Kara <[email protected]>
cc Matthew Wilcox <[email protected]>
cc Logan Gunthorpe <[email protected]>

---

Notes:
ver #8)
- Don't default to BIO_PAGE_REFFED [hch].

ver #5)
- Split from patch that uses iov_iter_extract_pages().

Quoted 7 lines
  block/bio.c               | 2 +-
  block/blk-map.c           | 1 +
  fs/direct-io.c            | 2 ++
  fs/iomap/direct-io.c      | 1 -
  include/linux/bio.h       | 2 +-
  include/linux/blk_types.h | 2 +-
  6 files changed, 6 insertions(+), 4 deletions(-)

One documentation nit below, but either way,

Quoted 38 lines
block/bio.c +1 -1
index 683444e6b711..851c23641a0d 100644
@@ -1198,7 +1198,6 @@ void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
  	bio->bi_io_vec = (struct bio_vec *)iter->bvec;
  	bio->bi_iter.bi_bvec_done = iter->iov_offset;
  	bio->bi_iter.bi_size = size;
-	bio_set_flag(bio, BIO_NO_PAGE_REF);
  	bio_set_flag(bio, BIO_CLONED);
  }
 
@@ -1343,6 +1342,7 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
  		return 0;
  	}
 
+	bio_set_flag(bio, BIO_PAGE_REFFED);
  	do {
  		ret = __bio_iov_iter_get_pages(bio, iter);
  	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
block/blk-map.c +1 -0
index 7db52ad5b2d0..0e2b0a861ba3 100644
@@ -282,6 +282,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
  	if (blk_queue_pci_p2pdma(rq->q))
  		extraction_flags |= ITER_ALLOW_P2PDMA;
 
+	bio_set_flag(bio, BIO_PAGE_REFFED);
  	while (iov_iter_count(iter)) {
  		struct page **pages, *stack_pages[UIO_FASTIOV];
  		ssize_t bytes;
fs/direct-io.c +0 -0
index 03d381377ae1..07810465fc9d 100644

fs/direct-io.c dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, · line 406

Quoted 4 lines
fs/direct-io.c +1 -0
@@ -403,6 +403,8 @@ dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
  		bio->bi_end_io = dio_bio_end_aio;
  	else
  		bio->bi_end_io = dio_bio_end_io;
+	/* for now require references for all pages */

Maybe just delete this comment?

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#19 lore raw

Re: [PATCH v9 5/8] block: Replace BIO_NO_PAGE_REF with BIO_PAGE_REFFED with inverted logic

Quoted 3 lines · John Hubbard

fs/direct-io.c dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, · line 406

Quoted 1 line
fs/direct-io.c +1 -0
+	/* for now require references for all pages */

Maybe just delete this comment?

Christoph added that. Presumably because this really should move to pinning or be replaced with iomap, but it's not straightforward either way. Christoph?

David

#20 lore raw

Re: [PATCH v9 5/8] block: Replace BIO_NO_PAGE_REF with BIO_PAGE_REFFED with inverted logic

Quoted 8 lines · David Howells
Quoted 3 lines · John Hubbard

fs/direct-io.c dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, · line 406

Quoted 1 line
fs/direct-io.c +1 -0
+	/* for now require references for all pages */

Maybe just delete this comment?

Christoph added that. Presumably because this really should move to pinning or be replaced with iomap, but it's not straightforward either way. Christoph?

Mostly because it adds the flag when allocating the bio, and not where doing the gup. If John thinks it adds more confusion than it helps, we can drop the comment.

That being said you had a conversion in an earlier version of the series, and once the current batch of patches is in we should resurrected it ASAP as that will allow us to kill BIO_FLAG_REFFED.

#21 lore raw

PATCH v9 6/8 block: Switch to pinning pages.

In reply to David Howells

Add BIO_PAGE_PINNED to indicate that the pages in a bio are pinned (FOLL_PIN) and that the pin will need removing.

cc Al Viro <[email protected]>
cc Jens Axboe <[email protected]>
cc Jan Kara <[email protected]>
cc Christoph Hellwig <[email protected]>
cc Matthew Wilcox <[email protected]>
cc Logan Gunthorpe <[email protected]>

---

Notes:
ver #9)
- Only consider pinning in bio_set_cleanup_mode(). Ref'ing pages in struct bio is going away.
- page_put_unpin() is removed; call unpin_user_page() and put_page() directly.
- Use bio_release_page() in __bio_release_pages().
- BIO_PAGE_PINNED and BIO_PAGE_REFFED can't both be set, so use if-else when testing both of them.

ver #8)
- Move the infrastructure to clean up pinned pages to this patch [hch].
- Put BIO_PAGE_PINNED before BIO_PAGE_REFFED as the latter should probably be removed at some point. FOLL_PIN can then be renumbered first.

 block/bio.c               |  6 +++---
 block/blk.h               | 21 +++++++++++++++++++++
 include/linux/bio.h       |  3 ++-
 include/linux/blk_types.h |  1 +
 4 files changed, 27 insertions(+), 4 deletions(-)
block/bio.c +3 -3
index 851c23641a0d..fc45aaa97696 100644
@@ -1176,7 +1176,7 @@ void __bio_release_pages(struct bio *bio, bool mark_dirty)
 	bio_for_each_segment_all(bvec, bio, iter_all) {
 		if (mark_dirty && !PageCompound(bvec->bv_page))
 			set_page_dirty_lock(bvec->bv_page);
-		put_page(bvec->bv_page);
+		bio_release_page(bio, bvec->bv_page);
 	}
 }
 EXPORT_SYMBOL_GPL(__bio_release_pages);
@@ -1496,8 +1496,8 @@ void bio_set_pages_dirty(struct bio *bio)
  * the BIO and re-dirty the pages in process context.
  *
  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
- * here on.  It will run one put_page() against each page and will run one
- * bio_put() against the BIO.
+ * here on.  It will unpin each page and will run one bio_put() against the
+ * BIO.
  */
 
 static void bio_dirty_fn(struct work_struct *work);
block/blk.h +21 -0
index 4c3b3325219a..32b252903f9a 100644
@@ -425,6 +425,27 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,
 		struct page *page, unsigned int len, unsigned int offset,
 		unsigned int max_sectors, bool *same_page);
 
+/*
+ * Set the cleanup mode for a bio from an iterator and the extraction flags.
+ */
+static inline void bio_set_cleanup_mode(struct bio *bio, struct iov_iter *iter)
+{
+	if (iov_iter_extract_will_pin(iter))
+		bio_set_flag(bio, BIO_PAGE_PINNED);
+}
+
+/*
+ * Clean up a page appropriately, where the page may be pinned, may have a
+ * ref taken on it or neither.
+ */
+static inline void bio_release_page(struct bio *bio, struct page *page)
+{
+	if (bio_flagged(bio, BIO_PAGE_PINNED))
+		unpin_user_page(page);
+	else if (bio_flagged(bio, BIO_PAGE_REFFED))
+		put_page(page);
+}
+
 struct request_queue *blk_alloc_queue(int node_id);
 
 int disk_scan_partitions(struct gendisk *disk, fmode_t mode, void *owner);
include/linux/bio.h +2 -1
index 805957c99147..b2c09997d79c 100644
@@ -484,7 +484,8 @@ void zero_fill_bio(struct bio *bio);
 
 static inline void bio_release_pages(struct bio *bio, bool mark_dirty)
 {
-	if (bio_flagged(bio, BIO_PAGE_REFFED))
+	if (bio_flagged(bio, BIO_PAGE_REFFED) ||
+	    bio_flagged(bio, BIO_PAGE_PINNED))
 		__bio_release_pages(bio, mark_dirty);
 }
 
include/linux/blk_types.h +1 -0
index 7daa261f4f98..a0e339ff3d09 100644
@@ -318,6 +318,7 @@ struct bio {
  * bio flags
  */
 enum {
+	BIO_PAGE_PINNED,	/* Unpin pages in bio_release_pages() */
 	BIO_PAGE_REFFED,	/* put pages in bio_release_pages() */
 	BIO_CLONED,		/* doesn't own data */
 	BIO_BOUNCED,		/* bio is a bounce bio */
Reviewed-by on v9 6/8
#22 lore raw

Re: [PATCH v9 6/8] block: Switch to pinning pages.

Quoted 2 lines · David Howells

Add BIO_PAGE_PINNED to indicate that the pages in a bio are pinned (FOLL_PIN) and that the pin will need removing.

The subject is odd when this doesn't actually switch anything, but just adds the infrastructure to unpin pages.

block/blk.h int bio_add_hw_page(struct request_queue *q, struct bio *bio, · line 435

Quoted 8 lines
block/blk.h +8 -0
+/*
+ * Set the cleanup mode for a bio from an iterator and the extraction flags.
+ */
+static inline void bio_set_cleanup_mode(struct bio *bio, struct iov_iter *iter)
+{
+	if (iov_iter_extract_will_pin(iter))
+		bio_set_flag(bio, BIO_PAGE_PINNED);
+}

At this point I'd be tempted to just open code these two lines instead of adding a helper, but I can live with the helper if you prefer it.

Otherwise looks good:

#23 lore raw

Re: [PATCH v9 6/8] block: Switch to pinning pages.

Quoted 5 lines · Christoph Hellwig
Quoted 2 lines

Add BIO_PAGE_PINNED to indicate that the pages in a bio are pinned (FOLL_PIN) and that the pin will need removing.

The subject is odd when this doesn't actually switch anything, but just adds the infrastructure to unpin pages.

How about:

block: Add BIO_PAGE_PINNED and associated infrastructure

Quoted 5 lines
Quoted 1 line

+static inline void bio_set_cleanup_mode(struct bio *bio, struct iov_iter *iter)

...
At this point I'd be tempted to just open code these two lines instead of adding a helper, but I can live with the helper if you prefer it.

I can do that. It makes sense to put the call to that next to the call to iov_iter_extract_pages().

David

#24 lore raw

Re: [PATCH v9 6/8] block: Switch to pinning pages.

Quoted 11 lines · David Howells
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines

Add BIO_PAGE_PINNED to indicate that the pages in a bio are pinned (FOLL_PIN) and that the pin will need removing.

The subject is odd when this doesn't actually switch anything, but just adds the infrastructure to unpin pages.

How about:

block: Add BIO_PAGE_PINNED and associated infrastructure

sounds good.

Reviewed-by on v9 6/8
#25 lore raw

Re: [PATCH v9 6/8] block: Switch to pinning pages.

In reply to David Howells
Quoted 29 lines · David Howells

Add BIO_PAGE_PINNED to indicate that the pages in a bio are pinned (FOLL_PIN) and that the pin will need removing.

cc Al Viro <[email protected]>
cc Jens Axboe <[email protected]>
cc Jan Kara <[email protected]>
cc Christoph Hellwig <[email protected]>
cc Matthew Wilcox <[email protected]>
cc Logan Gunthorpe <[email protected]>

---

Notes:
ver #9)
- Only consider pinning in bio_set_cleanup_mode(). Ref'ing pages in struct bio is going away.
- page_put_unpin() is removed; call unpin_user_page() and put_page() directly.
- Use bio_release_page() in __bio_release_pages().
- BIO_PAGE_PINNED and BIO_PAGE_REFFED can't both be set, so use if-else when testing both of them.

ver #8)
- Move the infrastructure to clean up pinned pages to this patch [hch].
- Put BIO_PAGE_PINNED before BIO_PAGE_REFFED as the latter should probably be removed at some point. FOLL_PIN can then be renumbered first.

Quoted 5 lines
  block/bio.c               |  6 +++---
  block/blk.h               | 21 +++++++++++++++++++++
  include/linux/bio.h       |  3 ++-
  include/linux/blk_types.h |  1 +
  4 files changed, 27 insertions(+), 4 deletions(-)

Neatly avoiding any use of FOLL_PIN or FOLL_GET, good. :)

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 84 lines
block/bio.c +3 -3
index 851c23641a0d..fc45aaa97696 100644
@@ -1176,7 +1176,7 @@ void __bio_release_pages(struct bio *bio, bool mark_dirty)
  	bio_for_each_segment_all(bvec, bio, iter_all) {
  		if (mark_dirty && !PageCompound(bvec->bv_page))
  			set_page_dirty_lock(bvec->bv_page);
-		put_page(bvec->bv_page);
+		bio_release_page(bio, bvec->bv_page);
  	}
  }
  EXPORT_SYMBOL_GPL(__bio_release_pages);
@@ -1496,8 +1496,8 @@ void bio_set_pages_dirty(struct bio *bio)
   * the BIO and re-dirty the pages in process context.
   *
   * It is expected that bio_check_pages_dirty() will wholly own the BIO from
- * here on.  It will run one put_page() against each page and will run one
- * bio_put() against the BIO.
+ * here on.  It will unpin each page and will run one bio_put() against the
+ * BIO.
   */
 
  static void bio_dirty_fn(struct work_struct *work);
block/blk.h +21 -0
index 4c3b3325219a..32b252903f9a 100644
@@ -425,6 +425,27 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,
  		struct page *page, unsigned int len, unsigned int offset,
  		unsigned int max_sectors, bool *same_page);
 
+/*
+ * Set the cleanup mode for a bio from an iterator and the extraction flags.
+ */
+static inline void bio_set_cleanup_mode(struct bio *bio, struct iov_iter *iter)
+{
+	if (iov_iter_extract_will_pin(iter))
+		bio_set_flag(bio, BIO_PAGE_PINNED);
+}
+
+/*
+ * Clean up a page appropriately, where the page may be pinned, may have a
+ * ref taken on it or neither.
+ */
+static inline void bio_release_page(struct bio *bio, struct page *page)
+{
+	if (bio_flagged(bio, BIO_PAGE_PINNED))
+		unpin_user_page(page);
+	else if (bio_flagged(bio, BIO_PAGE_REFFED))
+		put_page(page);
+}
+
  struct request_queue *blk_alloc_queue(int node_id);
 
  int disk_scan_partitions(struct gendisk *disk, fmode_t mode, void *owner);
include/linux/bio.h +2 -1
index 805957c99147..b2c09997d79c 100644
@@ -484,7 +484,8 @@ void zero_fill_bio(struct bio *bio);
 
  static inline void bio_release_pages(struct bio *bio, bool mark_dirty)
  {
-	if (bio_flagged(bio, BIO_PAGE_REFFED))
+	if (bio_flagged(bio, BIO_PAGE_REFFED) ||
+	    bio_flagged(bio, BIO_PAGE_PINNED))
  		__bio_release_pages(bio, mark_dirty);
  }
 
include/linux/blk_types.h +1 -0
index 7daa261f4f98..a0e339ff3d09 100644
@@ -318,6 +318,7 @@ struct bio {
   * bio flags
   */
  enum {
+	BIO_PAGE_PINNED,	/* Unpin pages in bio_release_pages() */
  	BIO_PAGE_REFFED,	/* put pages in bio_release_pages() */
  	BIO_CLONED,		/* doesn't own data */
  	BIO_BOUNCED,		/* bio is a bounce bio */
#26 lore raw

PATCH v9 7/8 block: Convert bio_iov_iter_get_pages to use iov_iter_extract_pages

In reply to David Howells

This will pin pages or leave them unaltered rather than getting a ref on them as appropriate to the iterator.

The pages need to be pinned for DIO rather than having refs taken on them to prevent VM copy-on-write from malfunctioning during a concurrent fork() (the result of the I/O could otherwise end up being affected by/visible to the child process).

cc Al Viro <[email protected]>
cc Jens Axboe <[email protected]>
cc Jan Kara <[email protected]>
cc Christoph Hellwig <[email protected]>
cc Matthew Wilcox <[email protected]>
cc Logan Gunthorpe <[email protected]>

---

Notes:
ver #8)
- Split the patch up a bit [hch].
- We should only be using pinned/non-pinned pages and not ref'd pages, so adjust the comments appropriately.

ver #7)
- Don't treat BIO_PAGE_REFFED/PINNED as being the same as FOLL_GET/PIN.

ver #5)
- Transcribe the FOLL_* flags returned by iov_iter_extract_pages() to BIO_* flags and got rid of bi_cleanup_mode.
- Replaced BIO_NO_PAGE_REF to BIO_PAGE_REFFED in the preceding patch.

 block/bio.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
block/bio.c +11 -11
index fc45aaa97696..936301519e6c 100644
@@ -1212,7 +1212,7 @@ static int bio_iov_add_page(struct bio *bio, struct page *page,
 	}
 
 	if (same_page)
-		put_page(page);
+		bio_release_page(bio, page);
 	return 0;
 }
 
@@ -1226,7 +1226,7 @@ static int bio_iov_add_zone_append_page(struct bio *bio, struct page *page,
 			queue_max_zone_append_sectors(q), &same_page) != len)
 		return -EINVAL;
 	if (same_page)
-		put_page(page);
+		bio_release_page(bio, page);
 	return 0;
 }
 
@@ -1237,10 +1237,10 @@ static int bio_iov_add_zone_append_page(struct bio *bio, struct page *page,
  * @bio: bio to add pages to
  * @iter: iov iterator describing the region to be mapped
  *
- * Pins pages from *iter and appends them to @bio's bvec array. The
- * pages will have to be released using put_page() when done.
- * For multi-segment *iter, this function only adds pages from the
- * next non-empty segment of the iov iterator.
+ * Extracts pages from *iter and appends them to @bio's bvec array.  The pages
+ * will have to be cleaned up in the way indicated by the BIO_PAGE_PINNED flag.
+ * For a multi-segment *iter, this function only adds pages from the next
+ * non-empty segment of the iov iterator.
  */
 static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 {
@@ -1272,9 +1272,9 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 	 * result to ensure the bio's total size is correct. The remainder of
 	 * the iov data will be picked up in the next bio iteration.
 	 */
-	size = iov_iter_get_pages(iter, pages,
-				  UINT_MAX - bio->bi_iter.bi_size,
-				  nr_pages, &offset, extraction_flags);
+	size = iov_iter_extract_pages(iter, &pages,
+				      UINT_MAX - bio->bi_iter.bi_size,
+				      nr_pages, extraction_flags, &offset);
 	if (unlikely(size <= 0))
 		return size ? size : -EFAULT;
 
@@ -1307,7 +1307,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 	iov_iter_revert(iter, left);
 out:
 	while (i < nr_pages)
-		put_page(pages[i++]);
+		bio_release_page(bio, pages[i++]);
 
 	return ret;
 }
@@ -1342,7 +1342,7 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 		return 0;
 	}
 
-	bio_set_flag(bio, BIO_PAGE_REFFED);
+	bio_set_cleanup_mode(bio, iter);
 	do {
 		ret = __bio_iov_iter_get_pages(bio, iter);
 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
Reviewed-by on v9 7/8
#27 lore raw

Re: [PATCH v9 7/8] block: Convert bio_iov_iter_get_pages to use iov_iter_extract_pages

Quoted 7 lines · David Howells

This will pin pages or leave them unaltered rather than getting a ref on them as appropriate to the iterator.

The pages need to be pinned for DIO rather than having refs taken on them to prevent VM copy-on-write from malfunctioning during a concurrent fork() (the result of the I/O could otherwise end up being affected by/visible to the child process).

Reviewed-by on v9 7/8
#28 lore raw

Re: [PATCH v9 7/8] block: Convert bio_iov_iter_get_pages to use iov_iter_extract_pages

In reply to David Howells
Quoted 73 lines · David Howells

This will pin pages or leave them unaltered rather than getting a ref on them as appropriate to the iterator.

The pages need to be pinned for DIO rather than having refs taken on them to prevent VM copy-on-write from malfunctioning during a concurrent fork() (the result of the I/O could otherwise end up being affected by/visible to the child process).

cc Al Viro <[email protected]>
cc Jens Axboe <[email protected]>
cc Jan Kara <[email protected]>
cc Christoph Hellwig <[email protected]>
cc Matthew Wilcox <[email protected]>
cc Logan Gunthorpe <[email protected]>

---

Notes:
ver #8)
- Split the patch up a bit [hch].
- We should only be using pinned/non-pinned pages and not ref'd pages, so adjust the comments appropriately.

ver #7)
- Don't treat BIO_PAGE_REFFED/PINNED as being the same as FOLL_GET/PIN.

ver #5)
- Transcribe the FOLL_* flags returned by iov_iter_extract_pages() to BIO_* flags and got rid of bi_cleanup_mode.
- Replaced BIO_NO_PAGE_REF to BIO_PAGE_REFFED in the preceding patch.

  block/bio.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
block/bio.c +6 -6
index fc45aaa97696..936301519e6c 100644
@@ -1212,7 +1212,7 @@ static int bio_iov_add_page(struct bio *bio, struct page *page,
  	}
 
  	if (same_page)
-		put_page(page);
+		bio_release_page(bio, page);
  	return 0;
  }
 
@@ -1226,7 +1226,7 @@ static int bio_iov_add_zone_append_page(struct bio *bio, struct page *page,
  			queue_max_zone_append_sectors(q), &same_page) != len)
  		return -EINVAL;
  	if (same_page)
-		put_page(page);
+		bio_release_page(bio, page);
  	return 0;
  }
 
@@ -1237,10 +1237,10 @@ static int bio_iov_add_zone_append_page(struct bio *bio, struct page *page,
   * @bio: bio to add pages to
   * @iter: iov iterator describing the region to be mapped
   *
- * Pins pages from *iter and appends them to @bio's bvec array. The
- * pages will have to be released using put_page() when done.
- * For multi-segment *iter, this function only adds pages from the
- * next non-empty segment of the iov iterator.
+ * Extracts pages from *iter and appends them to @bio's bvec array.  The pages
+ * will have to be cleaned up in the way indicated by the BIO_PAGE_PINNED flag.
+ * For a multi-segment *iter, this function only adds pages from the next
+ * non-empty segment of the iov iterator.
   */
  static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
  {

block/bio.c static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) · line 1277

Quoted 9 lines
block/bio.c +3 -3
@@ -1272,9 +1272,9 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
  	 * result to ensure the bio's total size is correct. The remainder of
  	 * the iov data will be picked up in the next bio iteration.
  	 */
-	size = iov_iter_get_pages(iter, pages,
-				  UINT_MAX - bio->bi_iter.bi_size,
-				  nr_pages, &offset, extraction_flags);
+	size = iov_iter_extract_pages(iter, &pages,
+				      UINT_MAX - bio->bi_iter.bi_size,
+				      nr_pages, extraction_flags, &offset);

A quite minor point: it seems like the last two args got reversed more or less by accident. It's not worth re-spinning or anything, but it seems better to leave the order the same between these two routines.

Either way, though,

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 22 lines
block/bio.c +2 -2
  	if (unlikely(size <= 0))
  		return size ? size : -EFAULT;
 
@@ -1307,7 +1307,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
  	iov_iter_revert(iter, left);
  out:
  	while (i < nr_pages)
-		put_page(pages[i++]);
+		bio_release_page(bio, pages[i++]);
 
  	return ret;
  }
@@ -1342,7 +1342,7 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
  		return 0;
  	}
 
-	bio_set_flag(bio, BIO_PAGE_REFFED);
+	bio_set_cleanup_mode(bio, iter);
  	do {
  		ret = __bio_iov_iter_get_pages(bio, iter);
  	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
#29 lore raw

Re: [PATCH v9 7/8] block: Convert bio_iov_iter_get_pages to use iov_iter_extract_pages

Quoted 3 lines · John Hubbard

A quite minor point: it seems like the last two args got reversed more or less by accident. It's not worth re-spinning or anything, but it seems better to leave the order the same between these two routines.

I pushed the extra return value to the end. It seems better that way.

David

#30 lore raw

PATCH v9 8/8 block: convert bio_map_user_iov to use iov_iter_extract_pages

In reply to David Howells

This will pin pages or leave them unaltered rather than getting a ref on them as appropriate to the iterator.

The pages need to be pinned for DIO rather than having refs taken on them to prevent VM copy-on-write from malfunctioning during a concurrent fork() (the result of the I/O could otherwise end up being visible to/affected by the child process).

cc Al Viro <[email protected]>
cc Jens Axboe <[email protected]>
cc Jan Kara <[email protected]>
cc Christoph Hellwig <[email protected]>
cc Matthew Wilcox <[email protected]>
cc Logan Gunthorpe <[email protected]>

---

Notes:
ver #8)
- Split the patch up a bit [hch].
- We should only be using pinned/non-pinned pages and not ref'd pages, so adjust the comments appropriately.

ver #7)
- Don't treat BIO_PAGE_REFFED/PINNED as being the same as FOLL_GET/PIN.

ver #5)
- Transcribe the FOLL_* flags returned by iov_iter_extract_pages() to BIO_* flags and got rid of bi_cleanup_mode.
- Replaced BIO_NO_PAGE_REF to BIO_PAGE_REFFED in the preceding patch.

 block/blk-map.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
block/blk-map.c +10 -12
index 0e2b0a861ba3..4e22dccdbe9b 100644
@@ -282,21 +282,19 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
 	if (blk_queue_pci_p2pdma(rq->q))
 		extraction_flags |= ITER_ALLOW_P2PDMA;
 
-	bio_set_flag(bio, BIO_PAGE_REFFED);
+	bio_set_cleanup_mode(bio, iter);
 	while (iov_iter_count(iter)) {
-		struct page **pages, *stack_pages[UIO_FASTIOV];
+		struct page *stack_pages[UIO_FASTIOV];
+		struct page **pages = stack_pages;
 		ssize_t bytes;
 		size_t offs;
 		int npages;
 
-		if (nr_vecs <= ARRAY_SIZE(stack_pages)) {
-			pages = stack_pages;
-			bytes = iov_iter_get_pages(iter, pages, LONG_MAX,
-						   nr_vecs, &offs, extraction_flags);
-		} else {
-			bytes = iov_iter_get_pages_alloc(iter, &pages,
-						LONG_MAX, &offs, extraction_flags);
-		}
+		if (nr_vecs > ARRAY_SIZE(stack_pages))
+			pages = NULL;
+
+		bytes = iov_iter_extract_pages(iter, &pages, LONG_MAX,
+					       nr_vecs, extraction_flags, &offs);
 		if (unlikely(bytes <= 0)) {
 			ret = bytes ? bytes : -EFAULT;
 			goto out_unmap;
@@ -318,7 +316,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
 				if (!bio_add_hw_page(rq->q, bio, page, n, offs,
 						     max_sectors, &same_page)) {
 					if (same_page)
-						put_page(page);
+						bio_release_page(bio, page);
 					break;
 				}
 
@@ -330,7 +328,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
 		 * release the pages we didn't map into the bio, if any
 		 */
 		while (j < npages)
-			put_page(pages[j++]);
+			bio_release_page(bio, pages[j++]);
 		if (pages != stack_pages)
 			kvfree(pages);
 		/* couldn't stuff something into bio? */
Reviewed-by on v9 8/8
#31 lore raw

Re: [PATCH v9 8/8] block: convert bio_map_user_iov to use iov_iter_extract_pages

Quoted 7 lines · David Howells

This will pin pages or leave them unaltered rather than getting a ref on them as appropriate to the iterator.

The pages need to be pinned for DIO rather than having refs taken on them to prevent VM copy-on-write from malfunctioning during a concurrent fork() (the result of the I/O could otherwise end up being visible to/affected by the child process).

Reviewed-by on v9 8/8
#32 lore raw

Re: [PATCH v9 8/8] block: convert bio_map_user_iov to use iov_iter_extract_pages

In reply to David Howells
Quoted 27 lines · David Howells

This will pin pages or leave them unaltered rather than getting a ref on them as appropriate to the iterator.

The pages need to be pinned for DIO rather than having refs taken on them to prevent VM copy-on-write from malfunctioning during a concurrent fork() (the result of the I/O could otherwise end up being visible to/affected by the child process).

cc Al Viro <[email protected]>
cc Jens Axboe <[email protected]>
cc Jan Kara <[email protected]>
cc Christoph Hellwig <[email protected]>
cc Matthew Wilcox <[email protected]>
cc Logan Gunthorpe <[email protected]>

---

Notes:
ver #8)
- Split the patch up a bit [hch].
- We should only be using pinned/non-pinned pages and not ref'd pages, so adjust the comments appropriately.

ver #7)
- Don't treat BIO_PAGE_REFFED/PINNED as being the same as FOLL_GET/PIN.

Quoted 8 lines

ver #5)
- Transcribe the FOLL_* flags returned by iov_iter_extract_pages() to BIO_* flags and got rid of bi_cleanup_mode.
- Replaced BIO_NO_PAGE_REF to BIO_PAGE_REFFED in the preceding patch.

  block/blk-map.c | 22 ++++++++++------------
  1 file changed, 10 insertions(+), 12 deletions(-)

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 53 lines
block/blk-map.c +10 -12
index 0e2b0a861ba3..4e22dccdbe9b 100644
@@ -282,21 +282,19 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
  	if (blk_queue_pci_p2pdma(rq->q))
  		extraction_flags |= ITER_ALLOW_P2PDMA;
 
-	bio_set_flag(bio, BIO_PAGE_REFFED);
+	bio_set_cleanup_mode(bio, iter);
  	while (iov_iter_count(iter)) {
-		struct page **pages, *stack_pages[UIO_FASTIOV];
+		struct page *stack_pages[UIO_FASTIOV];
+		struct page **pages = stack_pages;
  		ssize_t bytes;
  		size_t offs;
  		int npages;
 
-		if (nr_vecs <= ARRAY_SIZE(stack_pages)) {
-			pages = stack_pages;
-			bytes = iov_iter_get_pages(iter, pages, LONG_MAX,
-						   nr_vecs, &offs, extraction_flags);
-		} else {
-			bytes = iov_iter_get_pages_alloc(iter, &pages,
-						LONG_MAX, &offs, extraction_flags);
-		}
+		if (nr_vecs > ARRAY_SIZE(stack_pages))
+			pages = NULL;
+
+		bytes = iov_iter_extract_pages(iter, &pages, LONG_MAX,
+					       nr_vecs, extraction_flags, &offs);
  		if (unlikely(bytes <= 0)) {
  			ret = bytes ? bytes : -EFAULT;
  			goto out_unmap;
@@ -318,7 +316,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
  				if (!bio_add_hw_page(rq->q, bio, page, n, offs,
  						     max_sectors, &same_page)) {
  					if (same_page)
-						put_page(page);
+						bio_release_page(bio, page);
  					break;
  				}
 
@@ -330,7 +328,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
  		 * release the pages we didn't map into the bio, if any
  		 */
  		while (j < npages)
-			put_page(pages[j++]);
+			bio_release_page(bio, pages[j++]);
  		if (pages != stack_pages)
  			kvfree(pages);
  		/* couldn't stuff something into bio? */