hulk inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10302 -------------------------------- Advertise PCI P2PDMA on the loop queue when the backing block device supports P2PDMA and the loop device uses direct I/O, so that user O_DIRECT with p2pmem pages is accepted. Only the direct I/O path forwards the request pages unchanged. Short reads complete by zero filling the unread part of the request, which would access a P2PDMA page with the CPU, so fail those requests instead. Signed-off-by: Zizhi Wo <wozizhi@huawei.com> --- drivers/block/loop.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 8bda3a5e52c6..07234c05d295 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -32,10 +32,11 @@ #include <linux/blk-cgroup.h> #include <linux/sched/mm.h> #include <linux/statfs.h> #include <linux/uaccess.h> #include <linux/blk-mq.h> +#include <linux/memremap.h> #include <linux/spinlock.h> #include <uapi/linux/loop.h> /* Possible states of device */ enum { @@ -180,10 +181,28 @@ static bool lo_bdev_can_use_dio(struct loop_device *lo, if (lo->lo_offset & (sb_bsize - 1)) return false; return true; } +static void loop_update_p2pdma(struct loop_device *lo) +{ + struct file *file = lo->lo_backing_file; + struct inode *inode = file->f_mapping->host; + struct block_device *backing_bdev = NULL; + + if (S_ISBLK(inode->i_mode)) + backing_bdev = I_BDEV(inode); + else if (inode->i_sb->s_bdev) + backing_bdev = inode->i_sb->s_bdev; + + if (lo->use_dio && backing_bdev && + blk_queue_pci_p2pdma(bdev_get_queue(backing_bdev))) + blk_queue_flag_set(QUEUE_FLAG_PCI_P2PDMA, lo->lo_queue); + else + blk_queue_flag_clear(QUEUE_FLAG_PCI_P2PDMA, lo->lo_queue); +} + static void __loop_update_dio(struct loop_device *lo, bool dio) { struct file *file = lo->lo_backing_file; struct inode *inode = file->f_mapping->host; struct block_device *backing_bdev = NULL; @@ -213,10 +232,11 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) lo->use_dio = use_dio; if (use_dio) lo->lo_flags |= LO_FLAGS_DIRECT_IO; else lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; + loop_update_p2pdma(lo); if (lo->lo_state == Lo_bound) blk_mq_unfreeze_queue(lo->lo_queue); } /** @@ -251,10 +271,25 @@ static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) if (bw >= 0) bw = -EIO; return bw; } +/* + * A short read is completed by zero filling the rest of the request, which + * is a CPU access. Detect P2PDMA pages so the caller can fail instead. + */ +static bool loop_req_has_p2p(struct request *rq) +{ + struct req_iterator iter; + struct bio_vec bv; + + rq_for_each_bvec(bv, rq, iter) + if (is_pci_p2pdma_page(bv.bv_page)) + return true; + return false; +} + static int lo_write_simple(struct loop_device *lo, struct request *rq, loff_t pos) { struct bio_vec bvec; struct req_iterator iter; @@ -287,10 +322,13 @@ static int lo_read_simple(struct loop_device *lo, struct request *rq, flush_dcache_page(bvec.bv_page); if (len != bvec.bv_len) { struct bio *bio; + if (loop_req_has_p2p(rq)) + return -EIO; + __rq_for_each_bio(bio, rq) zero_fill_bio(bio); break; } cond_resched(); @@ -348,11 +386,11 @@ static void lo_complete_rq(struct request *rq) if (cmd->ret) { blk_update_request(rq, BLK_STS_OK, cmd->ret); cmd->ret = 0; blk_mq_requeue_request(rq, true); } else { - if (cmd->use_aio) { + if (cmd->use_aio && !loop_req_has_p2p(rq)) { struct bio *bio = rq->bio; while (bio) { zero_fill_bio(bio); bio = bio->bi_next; @@ -602,10 +640,11 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, lo->lo_backing_file = file; lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); mapping_set_gfp_mask(file->f_mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); loop_update_dio(lo); + loop_update_p2pdma(lo); blk_mq_unfreeze_queue(lo->lo_queue); partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; loop_global_unlock(lo, is_loop); /* @@ -1086,10 +1125,11 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode, blk_queue_io_min(lo->lo_queue, bsize); loop_config_discard(lo); loop_update_rotational(lo); loop_update_dio(lo); + loop_update_p2pdma(lo); loop_sysfs_init(lo); size = get_loop_size(lo, file); loop_set_size(lo, size); @@ -1150,10 +1190,11 @@ static void __loop_clr_fd(struct loop_device *lo, bool release) lo->lo_device = NULL; lo->lo_offset = 0; lo->lo_sizelimit = 0; memset(lo->lo_file_name, 0, LO_NAME_SIZE); + blk_queue_flag_clear(QUEUE_FLAG_PCI_P2PDMA, lo->lo_queue); blk_queue_logical_block_size(lo->lo_queue, 512); blk_queue_physical_block_size(lo->lo_queue, 512); blk_queue_io_min(lo->lo_queue, 512); invalidate_disk(lo->lo_disk); loop_sysfs_exit(lo); -- 2.52.0