From: Jérôme Glisse <jglisse@xxxxxxxxxx> 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@xxxxxxxxxx> Cc: linux-fsdevel@xxxxxxxxxxxxxxx Cc: linux-block@xxxxxxxxxxxxxxx Cc: linux-mm@xxxxxxxxx Cc: John Hubbard <jhubbard@xxxxxxxxxx> Cc: Jan Kara <jack@xxxxxxx> Cc: Dan Williams <dan.j.williams@xxxxxxxxx> Cc: Alexander Viro <viro@xxxxxxxxxxxxxxxxxx> Cc: Johannes Thumshirn <jthumshirn@xxxxxxx> Cc: Christoph Hellwig <hch@xxxxxx> Cc: Jens Axboe <axboe@xxxxxxxxx> Cc: Ming Lei <ming.lei@xxxxxxxxxx> Cc: Dave Chinner <david@xxxxxxxxxxxxx> Cc: Jason Gunthorpe <jgg@xxxxxxxx> Cc: Matthew Wilcox <willy@xxxxxxxxxxxxx> --- include/linux/bvec.h | 52 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) 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 -- 2.20.1