@@ -849,20 +849,14 @@ static int __bio_iov_bvec_add_pages(struct bio *bio, struct iov_iter *iter)
849
849
size = bio_add_page (bio , bv -> bv_page , len ,
850
850
bv -> bv_offset + iter -> iov_offset );
851
851
if (size == len ) {
852
- struct page * page ;
853
- int i ;
852
+ if (!bio_flagged (bio , BIO_NO_PAGE_REF )) {
853
+ struct page * page ;
854
+ int i ;
855
+
856
+ mp_bvec_for_each_page (page , bv , i )
857
+ get_page (page );
858
+ }
854
859
855
- /*
856
- * For the normal O_DIRECT case, we could skip grabbing this
857
- * reference and then not have to put them again when IO
858
- * completes. But this breaks some in-kernel users, like
859
- * splicing to/from a loop device, where we release the pipe
860
- * pages unconditionally. If we can fix that case, we can
861
- * get rid of the get here and the need to call
862
- * bio_release_pages() at IO completion time.
863
- */
864
- mp_bvec_for_each_page (page , bv , i )
865
- get_page (page );
866
860
iov_iter_advance (iter , size );
867
861
return 0 ;
868
862
}
@@ -925,10 +919,12 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
925
919
* This takes either an iterator pointing to user memory, or one pointing to
926
920
* kernel pages (BVEC iterator). If we're adding user pages, we pin them and
927
921
* map them into the kernel. On IO completion, the caller should put those
928
- * pages. For now, when adding kernel pages, we still grab a reference to the
929
- * page. This isn't strictly needed for the common case, but some call paths
930
- * end up releasing pages from eg a pipe and we can't easily control these.
931
- * See comment in __bio_iov_bvec_add_pages().
922
+ * pages. If we're adding kernel pages, and the caller told us it's safe to
923
+ * do so, we just have to add the pages to the bio directly. We don't grab an
924
+ * extra reference to those pages (the user should already have that), and we
925
+ * don't put the page on IO completion. The caller needs to check if the bio is
926
+ * flagged BIO_NO_PAGE_REF on IO completion. If it isn't, then pages should be
927
+ * released.
932
928
*
933
929
* The function tries, but does not guarantee, to pin as many pages as
934
930
* fit into the bio, or are requested in *iter, whatever is smaller. If
@@ -940,6 +936,13 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
940
936
const bool is_bvec = iov_iter_is_bvec (iter );
941
937
unsigned short orig_vcnt = bio -> bi_vcnt ;
942
938
939
+ /*
940
+ * If this is a BVEC iter, then the pages are kernel pages. Don't
941
+ * release them on IO completion, if the caller asked us to.
942
+ */
943
+ if (is_bvec && iov_iter_bvec_no_ref (iter ))
944
+ bio_set_flag (bio , BIO_NO_PAGE_REF );
945
+
943
946
do {
944
947
int ret ;
945
948
@@ -1696,7 +1699,8 @@ static void bio_dirty_fn(struct work_struct *work)
1696
1699
next = bio -> bi_private ;
1697
1700
1698
1701
bio_set_pages_dirty (bio );
1699
- bio_release_pages (bio );
1702
+ if (!bio_flagged (bio , BIO_NO_PAGE_REF ))
1703
+ bio_release_pages (bio );
1700
1704
bio_put (bio );
1701
1705
}
1702
1706
}
@@ -1713,7 +1717,8 @@ void bio_check_pages_dirty(struct bio *bio)
1713
1717
goto defer ;
1714
1718
}
1715
1719
1716
- bio_release_pages (bio );
1720
+ if (!bio_flagged (bio , BIO_NO_PAGE_REF ))
1721
+ bio_release_pages (bio );
1717
1722
bio_put (bio );
1718
1723
return ;
1719
1724
defer :
0 commit comments