diff mbox series

[v1,07/15] block: add bvec_put_page_dirty*() to replace put_page(bvec_page())

Message ID 20190411210834.4105-8-jglisse@redhat.com (mailing list archive)
State New, archived
Headers show
Series Keep track of GUPed pages in fs and block | expand

Commit Message

Jerome Glisse April 11, 2019, 9:08 p.m. UTC
From: Jérôme Glisse <jglisse@redhat.com>

For bio_vec.page we need to use the appropriate put_page ie put_user_page
if the page reference was taken through GUP (any of the get_user_page*)
or the regular put_page otherwise.

To distinguish between the two we store a flag as the top if of the pfn
values on all archectitecture we have at least one bit available there.

We also take care of dirtnyness ie calling set_page_dirty*().

Signed-off-by: Jérôme Glisse <jglisse@redhat.com>
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-block@vger.kernel.org
Cc: linux-mm@kvack.org
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Johannes Thumshirn <jthumshirn@suse.de>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Matthew Wilcox <willy@infradead.org>
---
 include/linux/bvec.h | 52 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/include/linux/bvec.h b/include/linux/bvec.h
index ac84ac66a333..a1e464c708fb 100644
--- a/include/linux/bvec.h
+++ b/include/linux/bvec.h
@@ -20,6 +20,7 @@ 
 #ifndef __LINUX_BVEC_ITER_H
 #define __LINUX_BVEC_ITER_H
 
+#include <asm/bitsperlong.h>
 #include <linux/kernel.h>
 #include <linux/bug.h>
 #include <linux/errno.h>
@@ -34,6 +35,9 @@  struct bio_vec {
 	unsigned int	bv_offset;
 };
 
+#define BVEC_PFN_GUP (1UL << (BITS_PER_LONG - 1))
+#define BVEC_PFN_MASK (~BVEC_PFN_GUP)
+
 struct bvec_iter {
 	sector_t		bi_sector;	/* device address in 512 byte
 						   sectors */
@@ -58,7 +62,13 @@  static inline unsigned long page_to_bvec_pfn(struct page *page)
 
 static inline struct page *bvec_page(const struct bio_vec *bvec)
 {
-	return bvec->bv_pfn == -1UL ? NULL : pfn_to_page(bvec->bv_pfn);
+	return bvec->bv_pfn == -1UL ? NULL :
+		pfn_to_page(bvec->bv_pfn & BVEC_PFN_MASK);
+}
+
+static inline void bvec_set_gup_page(struct bio_vec *bvec, struct page *page)
+{
+	bvec->bv_pfn = page_to_bvec_pfn(page) | BVEC_PFN_GUP;
 }
 
 static inline void bvec_set_page(struct bio_vec *bvec, struct page *page)
@@ -71,6 +81,46 @@  static inline struct page *bvec_nth_page(struct page *page, int idx)
 	return idx == 0 ? page : nth_page(page, idx);
 }
 
+static inline void bvec_put_page(const struct bio_vec *bvec)
+{
+	struct page *page = bvec_page(bvec);
+
+	if (page == NULL)
+		return;
+
+	if (bvec->bv_pfn & BVEC_PFN_GUP)
+		put_user_page(page);
+	else
+		put_page(page);
+}
+
+static inline void bvec_put_page_dirty(const struct bio_vec *bvec, bool dirty)
+{
+	struct page *page = bvec_page(bvec);
+
+	if (page == NULL)
+		return;
+
+	if (dirty)
+		set_page_dirty(page);
+
+	bvec_put_page(bvec);
+}
+
+static inline void bvec_put_page_dirty_lock(const struct bio_vec *bvec,
+					    bool dirty)
+{
+	struct page *page = bvec_page(bvec);
+
+	if (page == NULL)
+		return;
+
+	if (dirty)
+		set_page_dirty_lock(page);
+
+	bvec_put_page(bvec);
+}
+
 /*
  * various member access, note that bio_data should of course not be used
  * on highmem page vectors