Implement copy offload support
Subsystems (7)
A message that is not in the archive (sent off-list or not yet received): CGME20231206101018epcas5p1c4f2dc5149853412918e0270314f96b0@epcas5p1.samsung.com
v18 cover Implement copy offload support
From: Nitesh Shetty <[email protected]>
Hi Martin, Christoph,
We have addressed most of the review-comments received from community in the previous iterations of this series.
Is it possible to know your opinion on this, what needs to be added to get this series merged?
The patch series covers the points discussed in past and most recently in LSFMM'23[0].
We have covered the initial agreed requirements in this patch set and further additional features suggested by community.
This is next iteration of our previous patch set v17[1].
Copy offload is performed using two bio's -
1. Take a plug
2. The first bio containing source info is prepared and sent, a request is formed.
3. This is followed by preparing and sending the second bio containing the destination info.
4. This bio is merged with the request containing the source info.
5. The plug is released, and the request containing source and destination bio's is sent to the driver.
This design helps to avoid putting payload (token) in the request, as sending payload that is not data to the device is considered a bad approach.
So copy offload works only for request based storage drivers. We can make use of copy emulation in case copy offload capability is absent.
Overall series supports:
========================
1. Driver
- NVMe Copy command (single NS, TP 4065), including support in nvme-target (for block and file back end).
2. Block layer
- Block-generic copy (REQ_OP_COPY_DST/SRC), operation with interface accommodating two block-devs
- Merging copy requests in request layer
- Emulation, for in-kernel user when offload is natively absent
- dm-linear support (for cases not requiring split)
3. User-interface
- copy_file_range
Testing
=======
Copy offload can be tested on:
a. QEMU: NVME simple copy (TP 4065). By setting nvme-ns
parameters mssrl,mcl, msrc. For more info [2].
b. Null block device
c. NVMe Fabrics loopback.
d. blktests[3]
Emulation can be tested on any device.
fio[4].
Infra and plumbing:
===================
We populate copy_file_range callback in def_blk_fops. For devices that support copy-offload, use blkdev_copy_offload to achieve in-device copy.
However for cases, where device doesn't support offload, use generic_copy_file_range.
For in-kernel users (like NVMe fabrics), use blkdev_copy_offload if device is copy offload capable or else use emulation using blkdev_copy_emulation.
Modify checks in generic_copy_file_range to support block-device.
Blktests[3]
======================
tests/block/035-040: Runs copy offload and emulation on null
block device.
tests/block/050,055: Runs copy offload and emulation on test
nvme block device.
tests/nvme/056-067: Create a loop backed fabrics device and
run copy offload and emulation.
Future Work
===========
- loopback device copy offload support
- upstream fio to use copy offload
- upstream blktest to test copy offload
- update man pages for copy_file_range
- expand in-kernel users of copy offload
These are to be taken up after this minimal series is agreed upon.
Additional links:
=================
[0] https://lore.kernel.org/linux-nvme/CA+1E3rJ7BZ7LjQXXTdX+-0Edz=zT14mmPGMiVCzUgB33C60tbQ@mail.gmail.com/ https://lore.kernel.org/linux-nvme/[email protected]/ https://lore.kernel.org/linux-nvme/[email protected]/ [1] https://lore.kernel.org/all/[email protected]/ [2] https://qemu-project.gitlab.io/qemu/system/devices/nvme.html#simple-copy [3] https://github.com/nitesh-shetty/blktests/tree/feat/copy_offload/v15 [4] https://github.com/OpenMPDK/fio/tree/copyoffload-3.35-v14
Changes since v17:
=================
- block, nvmet: static error fixes (Dan Carpenter, kernel test robot)
- nvmet: pass COPY_FILE_SPLICE flag for vfs_copy_file_range in case file backed nvmet device
Changes since v16:
=================
- block: fixed memory leaks and renamed function (Jinyoung Choi)
- nvmet: static error fixes (kernel test robot)
Changes since v15:
=================
- fs, nvmet: don't fallback to copy emulation for copy offload IO failure, user can still use emulation by disabling device offload (Hannes)
- block: patch,function description changes (Hannes)
- added Reviewed-by from Hannes and Luis.
Changes since v14:
=================
- block: (Bart Van Assche)
1. BLK_ prefix addition to COPY_MAX_BYES and COPY_MAX_SEGMENTS
2. Improved function,patch,cover-letter description
3. Simplified refcount updating.
- null-blk, nvme:
4. static warning fixes (kernel test robot)
Changes since v13:
=================
- block:
1. Simplified copy offload and emulation helpers, now caller needs to decide between offload/emulation fallback
2. src,dst bio order change (Christoph Hellwig)
3. refcount changes similar to dio (Christoph Hellwig)
4. Single outstanding IO for copy emulation (Christoph Hellwig)
5. use copy_max_sectors to identify copy offload capability and other reviews (Damien, Christoph)
6. Return status in endio handler (Christoph Hellwig)
- nvme-fabrics: fallback to emulation in case of partial offload completion
- in kernel user addition (Ming lei)
- indentation, documentation, minor fixes, misc changes (Damien, Christoph)
- blktests changes to test kernel changes
Changes since v12:
=================
- block,nvme: Replaced token based approach with request based single namespace capable approach (Christoph Hellwig)
Changes since v11:
=================
- Documentation: Improved documentation (Damien Le Moal)
- block,nvme: ssize_t return values (Darrick J. Wong)
- block: token is allocated to SECTOR_SIZE (Matthew Wilcox)
- block: mem leak fix (Maurizio Lombardi)
Changes since v10:
=================
- NVMeOF: optimization in NVMe fabrics (Chaitanya Kulkarni)
- NVMeOF: sparse warnings (kernel test robot)
Changes since v9:
=================
- null_blk, improved documentation, minor fixes(Chaitanya Kulkarni)
- fio, expanded testing and minor fixes (Vincent Fu)
Changes since v8:
=================
- null_blk, copy_max_bytes_hw is made config fs parameter (Damien Le Moal)
- Negative error handling in copy_file_range (Christian Brauner)
- minor fixes, better documentation (Damien Le Moal)
- fio upgraded to 3.34 (Vincent Fu)
Changes since v7:
=================
- null block copy offload support for testing (Damien Le Moal)
- adding direct flag check for copy offload to block device, as we are using generic_copy_file_range for cached cases.
- Minor fixes
Changes since v6:
=================
- copy_file_range instead of ioctl for direct block device
- Remove support for multi range (vectored) copy
- Remove ioctl interface for copy.
- Remove offload support in dm kcopyd.
Changes since v5:
=================
- Addition of blktests (Chaitanya Kulkarni)
- Minor fix for fabrics file backed path
- Remove buggy zonefs copy file range implementation.
Changes since v4:
=================
- make the offload and emulation design asynchronous (Hannes Reinecke)
- fabrics loopback support
- sysfs naming improvements (Damien Le Moal)
- use kfree() instead of kvfree() in cio_await_completion (Damien Le Moal)
- use ranges instead of rlist to represent range_entry (Damien Le Moal)
- change argument ordering in blk_copy_offload suggested (Damien Le Moal)
- removed multiple copy limit and merged into only one limit (Damien Le Moal)
- wrap overly long lines (Damien Le Moal)
- other naming improvements and cleanups (Damien Le Moal)
- correctly format the code example in description (Damien Le Moal)
- mark blk_copy_offload as static (kernel test robot)
Changes since v3:
=================
- added copy_file_range support for zonefs
- added documentation about new sysfs entries
- incorporated review comments on v3
- minor fixes
Changes since v2:
=================
- fixed possible race condition reported by Damien Le Moal
- new sysfs controls as suggested by Damien Le Moal
- fixed possible memory leak reported by Dan Carpenter, lkp
- minor fixes
Changes since v1:
=================
- sysfs documentation (Greg KH)
- 2 bios for copy operation (Bart Van Assche, Mikulas Patocka, Martin K. Petersen, Douglas Gilbert)
- better payload design (Darrick J. Wong)
Anuj Gupta (1):
fs/read_write: Enable copy_file_range for block device.
Nitesh Shetty (11):
block: Introduce queue limits and sysfs for copy-offload support Add infrastructure for copy offload in block and request layer.
block: add copy offload support
block: add emulation for copy
fs, block: copy_file_range for def_blk_ops for direct block device
nvme: add copy offload support
nvmet: add copy command support for bdev and file ns
dm: Add support for copy offload
dm: Enable copy offload for dm-linear target
null: Enable trace capability for null block
null_blk: add support for copy offload
Documentation/ABI/stable/sysfs-block | 23 ++ Documentation/block/null_blk.rst | 5 + block/blk-core.c | 7 + block/blk-lib.c | 427 +++++++++++++++++++++++++++ block/blk-merge.c | 41 +++ block/blk-settings.c | 24 ++ block/blk-sysfs.c | 36 +++ block/blk.h | 16 + block/elevator.h | 1 + block/fops.c | 25 ++ drivers/block/null_blk/Makefile | 2 - drivers/block/null_blk/main.c | 100 ++++++- drivers/block/null_blk/null_blk.h | 1 + drivers/block/null_blk/trace.h | 25 ++ drivers/block/null_blk/zoned.c | 1 - drivers/md/dm-linear.c | 1 + drivers/md/dm-table.c | 37 +++ drivers/md/dm.c | 7 + drivers/nvme/host/constants.c | 1 + drivers/nvme/host/core.c | 79 +++++ drivers/nvme/host/trace.c | 19 ++ drivers/nvme/target/admin-cmd.c | 9 +- drivers/nvme/target/io-cmd-bdev.c | 71 +++++ drivers/nvme/target/io-cmd-file.c | 50 ++++ drivers/nvme/target/nvmet.h | 1 + drivers/nvme/target/trace.c | 19 ++ fs/read_write.c | 8 +- include/linux/bio.h | 6 +- include/linux/blk_types.h | 10 + include/linux/blkdev.h | 22 ++ include/linux/device-mapper.h | 3 + include/linux/nvme.h | 43 ++- 32 files changed, 1101 insertions(+), 19 deletions(-)
base-commit: 629a3b49f3f957e975253c54846090b8d5ed2e9b
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 1/12 block: Introduce queue limits and sysfs for copy-offload support
From: Nitesh Shetty <[email protected]>
Add device limits as sysfs entries,
- copy_max_bytes (RW)
- copy_max_hw_bytes (RO)
Above limits help to split the copy payload in block layer.copy_max_bytes: maximum total length of copy in single payload.copy_max_hw_bytes: Reflects the device supported maximum limit.
--- Documentation/ABI/stable/sysfs-block | 23 ++++++++++++++++++ block/blk-settings.c | 24 +++++++++++++++++++ block/blk-sysfs.c | 36 ++++++++++++++++++++++++++++ include/linux/blkdev.h | 13 ++++++++++ 4 files changed, 96 insertions(+)
Documentation/ABI/stable/sysfs-block +23 -0
@@ -155,6 +155,29 @@ Description: last zone of the device which may be smaller. +What: /sys/block/<disk>/queue/copy_max_bytes +Date: August 2023 +Contact: [email protected] +Description: + [RW] This is the maximum number of bytes that the block layer + will allow for a copy request. This is always smaller or + equal to the maximum size allowed by the hardware, indicated by + 'copy_max_hw_bytes'. An attempt to set a value higher than + 'copy_max_hw_bytes' will truncate this to 'copy_max_hw_bytes'. + Writing '0' to this file will disable offloading copies for this + device, instead copy is done via emulation. + + +What: /sys/block/<disk>/queue/copy_max_hw_bytes +Date: August 2023 +Contact: [email protected] +Description: + [RO] This is the maximum number of bytes that the hardware + will allow for single data copy request. + A value of 0 means that the device does not support + copy offload. + + What: /sys/block/<disk>/queue/crypto/ Date: February 2022 Contact: [email protected]
block/blk-settings.c +24 -0
@@ -59,6 +59,8 @@ void blk_set_default_limits(struct queue_limits *lim) lim->zoned =BLK_ZONED_NONE ; lim->zone_write_granularity =0 ; lim->dma_alignment =511 ; + lim->max_copy_hw_sectors =0 ; + lim->max_copy_sectors =0 ; }/**
@@ -82,6 +84,8 @@ void blk_set_stacking_limits(struct queue_limits *lim) lim->max_dev_sectors =UINT_MAX ; lim->max_write_zeroes_sectors =UINT_MAX ; lim->max_zone_append_sectors =UINT_MAX ; + lim->max_copy_hw_sectors =UINT_MAX ; + lim->max_copy_sectors =UINT_MAX ; }EXPORT_SYMBOL (blk_set_stacking_limits);
@@ -183,6 +187,22 @@ void blk_queue_max_discard_sectors(struct request_queue *q, }EXPORT_SYMBOL (blk_queue_max_discard_sectors); +/* +* blk_queue_max_copy_hw_sectors - set max sectors for a single copy payload +* @q: the request queue for the device +* @max_copy_sectors: maximum number of sectors to copy +*/ +void blk_queue_max_copy_hw_sectors (struct request_queue *q, +unsigned int max_copy_sectors) +{ +if (max_copy_sectors > (BLK_COPY_MAX_BYTES >>SECTOR_SHIFT )) + max_copy_sectors =BLK_COPY_MAX_BYTES >>SECTOR_SHIFT ; + + q->limits.max_copy_hw_sectors = max_copy_sectors; + q->limits.max_copy_sectors = max_copy_sectors; +} +EXPORT_SYMBOL_GPL (blk_queue_max_copy_hw_sectors); +/** * blk_queue_max_secure_erase_sectors - set max sectors for a secure erase * @q: the request queue for the device
@@ -578,6 +598,10 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, t->max_segment_size =min_not_zero (t->max_segment_size, b->max_segment_size); + t->max_copy_sectors =min (t->max_copy_sectors, b->max_copy_sectors); + t->max_copy_hw_sectors =min (t->max_copy_hw_sectors, + b->max_copy_hw_sectors); + t->misaligned |= b->misaligned; alignment =queue_limit_alignment_offset (b, start);
block/blk-sysfs.c +36 -0
@@ -199,6 +199,37 @@ static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *pagreturn queue_var_show (0 , page); } +static ssize_t queue_copy_hw_max_show (struct request_queue *q,char *page) +{ +return sprintf (page,"%llu\n" , (unsigned long long ) + q->limits.max_copy_hw_sectors <<SECTOR_SHIFT ); +} + +static ssize_t queue_copy_max_show (struct request_queue *q,char *page) +{ +return sprintf (page,"%llu\n" , (unsigned long long ) + q->limits.max_copy_sectors <<SECTOR_SHIFT ); +} + +static ssize_t queue_copy_max_store (struct request_queue *q,const char *page, +size_t count) +{ +unsigned long max_copy; +ssize_t ret =queue_var_store (&max_copy, page, count); + +if (ret <0 ) +return ret; + +if (max_copy & (queue_logical_block_size (q) -1 )) +return -EINVAL ; + + max_copy >>=SECTOR_SHIFT ; + q->limits.max_copy_sectors = min_t(unsigned int, max_copy, + q->limits.max_copy_hw_sectors); + +return count; +} +static ssize_t queue_write_same_max_show (struct request_queue *q,char *page) {return queue_var_show (0 , page);
@@ -517,6 +548,9 @@ QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");QUEUE_RO_ENTRY (queue_max_open_zones,"max_open_zones" );QUEUE_RO_ENTRY (queue_max_active_zones,"max_active_zones" ); +QUEUE_RO_ENTRY (queue_copy_hw_max,"copy_max_hw_bytes" ); +QUEUE_RW_ENTRY (queue_copy_max,"copy_max_bytes" ); +QUEUE_RW_ENTRY (queue_nomerges,"nomerges" );QUEUE_RW_ENTRY (queue_rq_affinity,"rq_affinity" );QUEUE_RW_ENTRY (queue_poll,"io_poll" );
@@ -634,6 +668,8 @@ static struct attribute *queue_attrs[] = { &queue_discard_max_entry.attr, &queue_discard_max_hw_entry.attr, &queue_discard_zeroes_data_entry.attr, + &queue_copy_hw_max_entry.attr, + &queue_copy_max_entry.attr, &queue_write_same_max_entry.attr, &queue_write_zeroes_max_entry.attr, &queue_zone_append_max_entry.attr,
include/linux/blkdev.h +13 -0
@@ -311,6 +311,9 @@ struct queue_limits {unsigned int discard_alignment;unsigned int zone_write_granularity; +unsigned int max_copy_hw_sectors; +unsigned int max_copy_sectors; +unsigned short max_segments;unsigned short max_integrity_segments;unsigned short max_discard_segments;
@@ -893,6 +896,8 @@ extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);extern void blk_queue_max_segments (struct request_queue *,unsigned short );extern void blk_queue_max_discard_segments (struct request_queue *,unsigned short ); +extern void blk_queue_max_copy_hw_sectors (struct request_queue *q, +unsigned int max_copy_sectors);void blk_queue_max_secure_erase_sectors (struct request_queue *q,unsigned int max_sectors);extern void blk_queue_max_segment_size (struct request_queue *,unsigned int );
@@ -1211,6 +1216,14 @@ static inline unsigned int bdev_discard_granularity(struct block_device *bdev)return bdev_get_queue (bdev)->limits.discard_granularity; } +/* maximum copy offload length, this is set to 128MB based on current testing */ +#define BLK_COPY_MAX_BYTES (1 << 27) + +static inline unsigned int bdev_max_copy_sectors (struct block_device *bdev) +{ +return bdev_get_queue (bdev)->limits.max_copy_sectors; +} +static inline unsigned int bdev_max_secure_erase_sectors (struct block_device *bdev) {
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 2/12 Add infrastructure for copy offload in block and request layer.
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
We add two new opcode REQ_OP_COPY_SRC, REQ_OP_COPY_DST. Since copy is a composite operation involving src and dst sectors/lba, each needs to be represented by a separate bio to make it compatible with device mapper.
We expect caller to take a plug and send bio with source information, followed by bio with destination information.
Once the src bio arrives we form a request and wait for destination bio. Upon arrival of destination we merge these two bio's and send corresponding request down to device driver.
Merging non copy offload bio is avoided by checking for copy specific opcodes in merge function.
--- block/blk-core.c | 7 +++++++ block/blk-merge.c | 41 +++++++++++++++++++++++++++++++++++++++ block/blk.h | 16 +++++++++++++++ block/elevator.h | 1 + include/linux/bio.h | 6 +----- include/linux/blk_types.h | 10 ++++++++++ 6 files changed, 76 insertions(+), 5 deletions(-)
block/blk-core.c +7 -0
@@ -121,6 +121,8 @@ static const char *const blk_op_name[] = {REQ_OP_NAME (ZONE_FINISH ),REQ_OP_NAME (ZONE_APPEND ),REQ_OP_NAME (WRITE_ZEROES ), +REQ_OP_NAME (COPY_SRC ), +REQ_OP_NAME (COPY_DST ),REQ_OP_NAME (DRV_IN ), REQ_OP_NAME(DRV_OUT), };
@@ -800,6 +802,11 @@ void submit_bio_noacct(struct bio *bio)if (!q->limits.max_write_zeroes_sectors) goto not_supported;break ; +case REQ_OP_COPY_SRC : +case REQ_OP_COPY_DST : +if (!q->limits.max_copy_sectors) + goto not_supported; +break ;default :break ; }
block/blk-merge.c +41 -0
77 lines not on this page · raw message
block/blk.h +16 -0
@@ -159,6 +159,20 @@ static inline bool blk_discard_mergable(struct request *req)return false; } +/* +* Copy offload sends a pair of bio with REQ_OP_COPY_SRC and REQ_OP_COPY_DST +* operation by taking a plug. +* Initially SRC bio is sent which forms a request and +* waits for DST bio to arrive. Once DST bio arrives +* we merge it and send request down to driver. +*/ +static inline bool blk_copy_offload_mergable (struct request *req, +struct bio *bio) +{ +return (req_op (req) ==REQ_OP_COPY_SRC && +bio_op (bio) ==REQ_OP_COPY_DST ); +} +static inline unsigned int blk_rq_get_max_segments (struct request *rq) {if (req_op (rq) ==REQ_OP_DISCARD )
@@ -300,6 +314,8 @@ static inline bool bio_may_exceed_limits(struct bio *bio,case REQ_OP_DISCARD :case REQ_OP_SECURE_ERASE :case REQ_OP_WRITE_ZEROES : +case REQ_OP_COPY_SRC : +case REQ_OP_COPY_DST :return true;/* non-trivial splitting decisions */ default :break ;
block/elevator.h +1 -0
@@ -18,6 +18,7 @@ enum elv_merge {ELEVATOR_FRONT_MERGE =1 ,ELEVATOR_BACK_MERGE =2 ,ELEVATOR_DISCARD_MERGE =3 , + ELEVATOR_COPY_OFFLOAD_MERGE = 4, };struct blk_mq_alloc_data ;
include/linux/bio.h +1 -5
@@ -53,11 +53,7 @@ static inline unsigned int bio_max_segs(unsigned int nr_segs)*/ static inline bool bio_has_data (struct bio *bio) { -if (bio && - bio->bi_iter.bi_size && -bio_op (bio) !=REQ_OP_DISCARD && -bio_op (bio) !=REQ_OP_SECURE_ERASE && -bio_op (bio) !=REQ_OP_WRITE_ZEROES ) +if (bio && (bio_op (bio) ==REQ_OP_READ ||bio_op (bio) ==REQ_OP_WRITE ))return true;return false;
include/linux/blk_types.h +10 -0
@@ -393,6 +393,10 @@ enum req_op {/* reset all the zone present on the device */ REQ_OP_ZONE_RESET_ALL = (__force blk_opf_t )17 , +/* copy offload dst and src operation */ +REQ_OP_COPY_SRC = (__force blk_opf_t )19 , +REQ_OP_COPY_DST = (__force blk_opf_t )21 , +/* Driver private requests */ REQ_OP_DRV_IN = (__force blk_opf_t )34 , REQ_OP_DRV_OUT = (__force blk_opf_t)35,
@@ -481,6 +485,12 @@ static inline bool op_is_write(blk_opf_t op)return !!(op & (__force blk_opf_t )1 ); } +static inline bool op_is_copy (blk_opf_t op) +{ +return ((op &REQ_OP_MASK ) ==REQ_OP_COPY_SRC || + (op &REQ_OP_MASK ) ==REQ_OP_COPY_DST ); +} +/* * Check if the bio or request is one that needs special treatment in the * flush state machine.
Signature · 1 line
2.35.1.500.gb896f729e2
Keith Busch
maintainerRe: [PATCH v18 02/12] Add infrastructure for copy offload in block and request layer.
include/linux/bio.h static inline unsigned int bio_max_segs(unsigned int nr_segs) · line 57
Quoted 9 lines · Kanchan Joshi
static inline bool
bio_has_data(struct bio *bio)
{include/linux/bio.h +1
-5-if (bio && - bio->bi_iter.bi_size && -bio_op (bio) !=REQ_OP_DISCARD && -bio_op (bio) !=REQ_OP_SECURE_ERASE && -bio_op (bio) !=REQ_OP_WRITE_ZEROES ) +if (bio && (bio_op (bio) ==REQ_OP_READ ||bio_op (bio) ==REQ_OP_WRITE ))return true;
There are other ops besides READ and WRITE that have data, but this is might be fine by the fact that other ops with data currently don't call this function.
include/linux/blk_types.h enum req_op { · line 397
Quoted 10 lines
include/linux/blk_types.h +2
-0@@ -393,6 +393,10 @@ enum req_op {/* reset all the zone present on the device */ REQ_OP_ZONE_RESET_ALL = (__force blk_opf_t )17 , +/* copy offload dst and src operation */ + REQ_OP_COPY_SRC = (__force blk_opf_t)19,
Should this be an even numbered OP? The odd ones are for data WRITEs.
Quoted 1 line
include/linux/blk_types.h +1
-0+ REQ_OP_COPY_DST = (__force blk_opf_t)21,
Re: [PATCH v18 02/12] Add infrastructure for copy offload in block and request layer.
Quoted 25 lines
Quoted 25 lines · Keith Busch
Quoted 24 lines · Kanchan Joshi
include/linux/bio.h static inline unsigned int bio_max_segs(unsigned int nr_segs) · line 57
Quoted 9 lines
static inline bool
bio_has_data(struct bio *bio)
{include/linux/bio.h +1
-5-if (bio && - bio->bi_iter.bi_size && -bio_op (bio) !=REQ_OP_DISCARD && -bio_op (bio) !=REQ_OP_SECURE_ERASE && -bio_op (bio) !=REQ_OP_WRITE_ZEROES ) +if (bio && (bio_op (bio) ==REQ_OP_READ ||bio_op (bio) ==REQ_OP_WRITE ))return true;There are other ops besides READ and WRITE that have data, but this is might be fine by the fact that other ops with data currently don't call this function.
Quoted 8 lines
include/linux/blk_types.h +0
-0@@ -393,6 +393,10 @@ enum req_op {/* reset all the zone present on the device */ REQ_OP_ZONE_RESET_ALL = (__force blk_opf_t)17,
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
include/linux/blk_types.h enum req_op { · line 397
Quoted 2 lines
include/linux/blk_types.h +2
-0+/* copy offload dst and src operation */ + REQ_OP_COPY_SRC = (__force blk_opf_t)19,Should this be an even numbered OP? The odd ones are for data WRITEs.
Our request opcode needs to be write based(even) so that while forming nvme-tcp packets we send this as part of the nvme capsule.
But now I think this design can be simplified as you suggested, if we align COPY_SRC to even and COPY_DST to odd. This requires us to change the design by sending dst bio first hence forming a write based request, followed by src bio's.
Will send a follow up series next week fixing this.
Thank you,
Nitesh Shetty
PATCH v18 3/12 block: add copy offload support
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
Introduce blkdev_copy_offload to perform copy offload.
Issue REQ_OP_COPY_SRC with source info along with taking a plug. This flows till request layer and waits for dst bio to arrive. Issue REQ_OP_COPY_DST with destination info and this bio reaches request layer and merges with src request.
For any reason, if a request comes to the driver with only one of src/dst bio, we fail the copy offload.
Larger copy will be divided, based on max_copy_sectors limit.
--- block/blk-lib.c | 204 +++++++++++++++++++++++++++++++++++++++++ include/linux/blkdev.h | 4 + 2 files changed, 208 insertions(+)
block/blk-lib.c +204 -0
@@ -10,6 +10,22 @@#include "blk.h" +/* Keeps track of all outstanding copy IO */ +struct blkdev_copy_io { +atomic_t refcount; +ssize_t copied; +int status; +struct task_struct *waiter; +void (*endio)(void *private,int status,ssize_t copied); +void *private; +}; + +/* Keeps track of single outstanding copy offload IO */ +struct blkdev_copy_offload_io { +struct blkdev_copy_io *cio; +loff_t offset; +}; +static sector_t bio_discard_limit (struct block_device *bdev,sector_t sector) {unsigned int discard_granularity =bdev_discard_granularity (bdev);
@@ -115,6 +131,194 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, }EXPORT_SYMBOL (blkdev_issue_discard); +static inline ssize_t blkdev_copy_sanity_check (struct block_device *bdev_in, +loff_t pos_in, +struct block_device *bdev_out, +loff_t pos_out,size_t len) +{ +unsigned int align =max (bdev_logical_block_size (bdev_out), +bdev_logical_block_size (bdev_in)) -1 ; + +if ((pos_in & align) || (pos_out & align) || (len & align) || !len || + len >=BLK_COPY_MAX_BYTES ) +return -EINVAL ; + +return 0 ; +} + +static inline void blkdev_copy_endio (struct blkdev_copy_io *cio) +{ +if (cio->endio) { + cio->endio (cio->private, cio->status, cio->copied); +kfree (cio); + }else { +struct task_struct *waiter = cio->waiter; + +WRITE_ONCE (cio->waiter,NULL ); +blk_wake_io_task (waiter); + } +} + +/* +* This must only be called once all bios have been issued so that the refcount +* can only decrease. This just waits for all bios to complete. +* Returns the length of bytes copied or error +*/ +static ssize_t blkdev_copy_wait_for_completion_io (struct blkdev_copy_io *cio) +{ +ssize_t ret; + +for (;;) { +__set_current_state (TASK_UNINTERRUPTIBLE ); +if (!READ_ONCE (cio->waiter)) +break ; +blk_io_schedule (); + } +__set_current_state (TASK_RUNNING ); + ret = cio->copied; +kfree (cio); + +return ret; +} + +static void blkdev_copy_offload_dst_endio (struct bio *bio) +{ +struct blkdev_copy_offload_io *offload_io = bio->bi_private; +struct blkdev_copy_io *cio = offload_io->cio; + +if (bio->bi_status) { + cio->copied =min_t (ssize_t, offload_io->offset, cio->copied); +if (!cio->status) + cio->status =blk_status_to_errno (bio->bi_status); + } +bio_put (bio); +kfree (offload_io); + +if (atomic_dec_and_test (&cio->refcount)) +blkdev_copy_endio (cio); +} + +/* +* @bdev: block device +* @pos_in: source offset +* @pos_out: destination offset +* @len: length in bytes to be copied +* @endio: endio function to be called on completion of copy operation, +* for synchronous operation this should be NULL +* @private: endio function will be called with this private data, +* for synchronous operation this should be NULL +* @gfp_mask: memory allocation flags (for bio_alloc) +* +* For synchronous operation returns the length of bytes copied or error +* For asynchronous operation returns -EIOCBQUEUED or error +* +* Description: +* Copy source offset to destination offset within block device, using +* device's native copy offload feature. +* We perform copy operation using 2 bio's. +* 1. We take a plug and send a REQ_OP_COPY_SRC bio along with source +* sector and length. Once this bio reaches request layer, we form a +* request and wait for dst bio to arrive. +* 2. We issue REQ_OP_COPY_DST bio along with destination sector, length. +* Once this bio reaches request layer and find a request with previously +* sent source info we merge the destination bio and return. +* 3. Release the plug and request is sent to driver +* This design works only for drivers with request queue. +*/ +ssize_t blkdev_copy_offload (struct block_device *bdev,loff_t pos_in, +loff_t pos_out,size_t len, +void (*endio)(void *,int ,ssize_t ), +void *private,gfp_t gfp) +{ +struct blkdev_copy_io *cio; +struct blkdev_copy_offload_io *offload_io; +struct bio *src_bio, *dst_bio; +size_t rem, chunk; +size_t max_copy_bytes =bdev_max_copy_sectors (bdev) <<SECTOR_SHIFT ; +ssize_t ret; +struct blk_plug plug; + +if (!max_copy_bytes) +return -EOPNOTSUPP ; + + ret =blkdev_copy_sanity_check (bdev, pos_in, bdev, pos_out, len); +if (ret) +return ret; + + cio =kzalloc (sizeof (*cio), gfp); +if (!cio) +return -ENOMEM ; +atomic_set (&cio->refcount,1 ); + cio->waiter = current; + cio->endio = endio; + cio->private = private; + +/* +* If there is a error, copied will be set to least successfully +* completed copied length +*/ + cio->copied = len; +for (rem = len; rem >0 ; rem -= chunk) { + chunk =min (rem, max_copy_bytes); + + offload_io =kzalloc (sizeof (*offload_io), gfp); +if (!offload_io) + goto err_free_cio; + offload_io->cio = cio; +/* +* For partial completion, we use offload_io->offset to truncate +* successful copy length +*/ + offload_io->offset = len - rem; + + src_bio =bio_alloc (bdev,0 ,REQ_OP_COPY_SRC , gfp); +if (!src_bio) + goto err_free_offload_io; + src_bio->bi_iter.bi_size = chunk; + src_bio->bi_iter.bi_sector = pos_in >>SECTOR_SHIFT ; + +blk_start_plug (&plug); + dst_bio =blk_next_bio (src_bio, bdev,0 ,REQ_OP_COPY_DST , gfp); +if (!dst_bio) + goto err_free_src_bio; + dst_bio->bi_iter.bi_size = chunk; + dst_bio->bi_iter.bi_sector = pos_out >>SECTOR_SHIFT ; + dst_bio->bi_end_io = blkdev_copy_offload_dst_endio; + dst_bio->bi_private = offload_io; + +atomic_inc (&cio->refcount); +submit_bio (dst_bio); +blk_finish_plug (&plug); + pos_in += chunk; + pos_out += chunk; + } + +if (atomic_dec_and_test (&cio->refcount)) +blkdev_copy_endio (cio); +if (endio) +return -EIOCBQUEUED ; + +return blkdev_copy_wait_for_completion_io (cio); + +err_free_src_bio: +bio_put (src_bio); +err_free_offload_io: +kfree (offload_io); +err_free_cio: + cio->copied =min_t (ssize_t, cio->copied, (len - rem)); + cio->status = -ENOMEM ; +if (rem == len) { + ret = cio->status; +kfree (cio); +return ret; + } +if (cio->endio) +return cio->status; + +return blkdev_copy_wait_for_completion_io (cio); +} +EXPORT_SYMBOL_GPL (blkdev_copy_offload); +static int __blkdev_issue_write_zeroes (struct block_device *bdev,sector_t sector,sector_t nr_sects,gfp_t gfp_mask,struct bio **biop,unsigned flags)
include/linux/blkdev.h +4 -0
@@ -1042,6 +1042,10 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop);int blkdev_issue_secure_erase (struct block_device *bdev,sector_t sector,sector_t nr_sects,gfp_t gfp); +ssize_t blkdev_copy_offload (struct block_device *bdev,loff_t pos_in, +loff_t pos_out,size_t len, +void (*endio)(void *,int ,ssize_t ), +void *private,gfp_t gfp_mask);#define BLKDEV_ZERO_NOUNMAP (1 << 0)/* do not free blocks */ #define BLKDEV_ZERO_NOFALLBACK (1 << 1)/* don't write explicit zeroes */
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 4/12 block: add emulation for copy
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
For the devices which does not support copy, copy emulation is added. It is required for in-kernel users like fabrics, where file descriptor is not available and hence they can't use copy_file_range. Copy-emulation is implemented by reading from source into memory and writing to the corresponding destination.
At present in kernel user of emulation is fabrics.
--- block/blk-lib.c | 223 +++++++++++++++++++++++++++++++++++++++++ include/linux/blkdev.h | 4 + 2 files changed, 227 insertions(+)
block/blk-lib.c +223 -0
@@ -26,6 +26,20 @@ struct blkdev_copy_offload_io {loff_t offset; }; +/* Keeps track of single outstanding copy emulation IO */ +struct blkdev_copy_emulation_io { +struct blkdev_copy_io *cio; +struct work_struct emulation_work; +void *buf; +ssize_t buf_len; +loff_t pos_in; +loff_t pos_out; +ssize_t len; +struct block_device *bdev_in; +struct block_device *bdev_out; +gfp_t gfp; +}; +static sector_t bio_discard_limit (struct block_device *bdev,sector_t sector) {unsigned int discard_granularity =bdev_discard_granularity (bdev);
@@ -319,6 +333,215 @@ ssize_t blkdev_copy_offload(struct block_device *bdev, loff_t pos_in, }EXPORT_SYMBOL_GPL (blkdev_copy_offload); +static void *blkdev_copy_alloc_buf (ssize_t req_size,ssize_t *alloc_size, +gfp_t gfp) +{ +int min_size =PAGE_SIZE ; +char *buf; + +while (req_size >= min_size) { + buf =kvmalloc (req_size, gfp); +if (buf) { + *alloc_size = req_size; +return buf; + } + req_size >>=1 ; + } + +return NULL ; +} + +static struct bio *bio_map_buf (void *data,unsigned int len,gfp_t gfp) +{ +unsigned long kaddr = (unsigned long )data; +unsigned long end = (kaddr + len +PAGE_SIZE -1 ) >>PAGE_SHIFT ; +unsigned long start = kaddr >>PAGE_SHIFT ; +const int nr_pages = end - start; +bool is_vmalloc =is_vmalloc_addr (data); +struct page *page; +int offset, i; +struct bio *bio; + + bio =bio_kmalloc (nr_pages, gfp); +if (!bio) +return ERR_PTR (-ENOMEM ); +bio_init (bio,NULL , bio->bi_inline_vecs, nr_pages,0 ); + +if (is_vmalloc) { +flush_kernel_vmap_range (data, len); + bio->bi_private = data; + } + + offset =offset_in_page (kaddr); +for (i =0 ; i < nr_pages; i++) { +unsigned int bytes =PAGE_SIZE - offset; + +if (len <=0 ) +break ; + +if (bytes > len) + bytes = len; + +if (!is_vmalloc) + page =virt_to_page (data); +else + page =vmalloc_to_page (data); +if (bio_add_page (bio, page, bytes, offset) < bytes) { +/* we don't support partial mappings */ +bio_uninit (bio); +kfree (bio); +return ERR_PTR (-EINVAL ); + } + + data += bytes; + len -= bytes; + offset =0 ; + } + +return bio; +} + +static void blkdev_copy_emulation_work (struct work_struct *work) +{ +struct blkdev_copy_emulation_io *emulation_io =container_of (work, + struct blkdev_copy_emulation_io, emulation_work); +struct blkdev_copy_io *cio = emulation_io->cio; +struct bio *read_bio, *write_bio; +loff_t pos_in = emulation_io->pos_in, pos_out = emulation_io->pos_out; +ssize_t rem, chunk; +int ret =0 ; + +for (rem = emulation_io->len; rem >0 ; rem -= chunk) { + chunk =min_t (int, emulation_io->buf_len, rem); + + read_bio =bio_map_buf (emulation_io->buf, + emulation_io->buf_len, + emulation_io->gfp); +if (IS_ERR (read_bio)) { + ret =PTR_ERR (read_bio); +break ; + } + read_bio->bi_opf =REQ_OP_READ |REQ_SYNC ; +bio_set_dev (read_bio, emulation_io->bdev_in); + read_bio->bi_iter.bi_sector = pos_in >>SECTOR_SHIFT ; + read_bio->bi_iter.bi_size = chunk; + ret =submit_bio_wait (read_bio); +kfree (read_bio); +if (ret) +break ; + + write_bio =bio_map_buf (emulation_io->buf, + emulation_io->buf_len, + emulation_io->gfp); +if (IS_ERR (write_bio)) { + ret =PTR_ERR (write_bio); +break ; + } + write_bio->bi_opf =REQ_OP_WRITE |REQ_SYNC ; +bio_set_dev (write_bio, emulation_io->bdev_out); + write_bio->bi_iter.bi_sector = pos_out >>SECTOR_SHIFT ; + write_bio->bi_iter.bi_size = chunk; + ret =submit_bio_wait (write_bio); +kfree (write_bio); +if (ret) +break ; + + pos_in += chunk; + pos_out += chunk; + } + cio->status = ret; +kvfree (emulation_io->buf); +kfree (emulation_io); +blkdev_copy_endio (cio); +} + +static inline ssize_t queue_max_hw_bytes (struct request_queue *q) +{ +return min_t (ssize_t,queue_max_hw_sectors (q) <<SECTOR_SHIFT , +queue_max_segments (q) <<PAGE_SHIFT ); +} +/* +* @bdev_in: source block device +* @pos_in: source offset +* @bdev_out: destination block device +* @pos_out: destination offset +* @len: length in bytes to be copied +* @endio: endio function to be called on completion of copy operation, +* for synchronous operation this should be NULL +* @private: endio function will be called with this private data, +* for synchronous operation this should be NULL +* @gfp_mask: memory allocation flags (for bio_alloc) +* +* For synchronous operation returns the length of bytes copied or error +* For asynchronous operation returns -EIOCBQUEUED or error +* +* Description: +* If native copy offload feature is absent, caller can use this function +* to perform copy. +* We store information required to perform the copy along with temporary +* buffer allocation. We async punt copy emulation to a worker. And worker +* performs copy in 2 steps. +* 1. Read data from source to temporary buffer +* 2. Write data to destination from temporary buffer +*/ +ssize_t blkdev_copy_emulation (struct block_device *bdev_in,loff_t pos_in, +struct block_device *bdev_out,loff_t pos_out, +size_t len,void (*endio)(void *,int ,ssize_t ), +void *private,gfp_t gfp) +{ +struct request_queue *in =bdev_get_queue (bdev_in); +struct request_queue *out =bdev_get_queue (bdev_out); +struct blkdev_copy_emulation_io *emulation_io; +struct blkdev_copy_io *cio; +ssize_t ret; +size_t max_hw_bytes =min (queue_max_hw_bytes (in), +queue_max_hw_bytes (out)); + + ret =blkdev_copy_sanity_check (bdev_in, pos_in, bdev_out, pos_out, len); +if (ret) +return ret; + + cio =kzalloc (sizeof (*cio), gfp); +if (!cio) +return -ENOMEM ; + + cio->waiter = current; + cio->copied = len; + cio->endio = endio; + cio->private = private; + + emulation_io =kzalloc (sizeof (*emulation_io), gfp); +if (!emulation_io) + goto err_free_cio; + emulation_io->cio = cio; +INIT_WORK (&emulation_io->emulation_work, blkdev_copy_emulation_work); + emulation_io->pos_in = pos_in; + emulation_io->pos_out = pos_out; + emulation_io->len = len; + emulation_io->bdev_in = bdev_in; + emulation_io->bdev_out = bdev_out; + emulation_io->gfp = gfp; + + emulation_io->buf =blkdev_copy_alloc_buf (min (max_hw_bytes, len), + &emulation_io->buf_len, gfp); +if (!emulation_io->buf) + goto err_free_emulation_io; + +schedule_work (&emulation_io->emulation_work); + +if (cio->endio) +return -EIOCBQUEUED ; + +return blkdev_copy_wait_for_completion_io (cio); + +err_free_emulation_io: +kfree (emulation_io); +err_free_cio: +kfree (cio); +return -ENOMEM ; +} +EXPORT_SYMBOL_GPL (blkdev_copy_emulation); +static int __blkdev_issue_write_zeroes (struct block_device *bdev,sector_t sector,sector_t nr_sects,gfp_t gfp_mask,struct bio **biop,unsigned flags)
include/linux/blkdev.h +4 -0
@@ -1046,6 +1046,10 @@ ssize_t blkdev_copy_offload(struct block_device *bdev, loff_t pos_in, loff_t pos_out, size_t len, void (*endio)(void *, int, ssize_t), void *private, gfp_t gfp_mask); +ssize_t blkdev_copy_emulation (struct block_device *bdev_in,loff_t pos_in, +struct block_device *bdev_out,loff_t pos_out, +size_t len,void (*endio)(void *,int ,ssize_t ), +void *private,gfp_t gfp);#define BLKDEV_ZERO_NOUNMAP (1 << 0)/* do not free blocks */ #define BLKDEV_ZERO_NOFALLBACK (1 << 1)/* don't write explicit zeroes */
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 5/12 fs/read_write: Enable copy_file_range for block device.
In reply to Kanchan JoshiFrom: Anuj Gupta <[email protected]>
This is a prep patch. Allow copy_file_range to work for block devices. Relaxing generic_copy_file_checks allows us to reuse the existing infra, instead of adding a new user interface for block copy offload. Change generic_copy_file_checks to use ->f_mapping->host for both inode_in and inode_out. Allow block device in generic_file_rw_checks.
--- fs/read_write.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
fs/read_write.c +5 -3
@@ -1441,8 +1441,8 @@ static int generic_copy_file_checks(struct file *file_in, loff_t pos_in, struct file *file_out, loff_t pos_out, size_t *req_count, unsigned int flags) { - struct inode *inode_in = file_inode(file_in); -struct inode *inode_out =file_inode (file_out); + struct inode *inode_in = file_in->f_mapping->host; +struct inode *inode_out = file_out->f_mapping->host;uint64_t count = *req_count;loff_t size_in;int ret;
@@ -1750,7 +1750,9 @@ int generic_file_rw_checks(struct file *file_in, struct file *file_out)/* Don't copy dirs, pipes, sockets... */ if (S_ISDIR (inode_in->i_mode) ||S_ISDIR (inode_out->i_mode))return -EISDIR ; -if (!S_ISREG (inode_in->i_mode) || !S_ISREG (inode_out->i_mode)) +if (!S_ISREG (inode_in->i_mode) && !S_ISBLK (inode_in->i_mode)) +return -EINVAL ; +if ((inode_in->i_mode &S_IFMT ) != (inode_out->i_mode &S_IFMT ))return -EINVAL ; if (!(file_in->f_mode & FMODE_READ) ||
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 6/12 fs, block: copy_file_range for def_blk_ops for direct block device
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
For direct block device opened with O_DIRECT, use copy_file_range to issue device copy offload, or use generic_copy_file_range in case device copy offload capability is absent or the device files are not open with O_DIRECT.
--- block/fops.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
block/fops.c +25 -0
@@ -747,6 +747,30 @@ static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)return ret; } +static ssize_t blkdev_copy_file_range (struct file *file_in,loff_t pos_in, +struct file *file_out,loff_t pos_out, +size_t len,unsigned int flags) +{ +struct block_device *in_bdev =I_BDEV (bdev_file_inode (file_in)); +struct block_device *out_bdev =I_BDEV (bdev_file_inode (file_out)); +ssize_t copied =0 ; + +if ((in_bdev == out_bdev) &&bdev_max_copy_sectors (in_bdev) && + (file_in->f_iocb_flags &IOCB_DIRECT ) && + (file_out->f_iocb_flags &IOCB_DIRECT )) { + copied =blkdev_copy_offload (in_bdev, pos_in, pos_out, len, +NULL ,NULL ,GFP_KERNEL ); +if (copied <0 ) + copied =0 ; + }else { + copied =generic_copy_file_range (file_in, pos_in + copied, + file_out, pos_out + copied, + len - copied, flags); + } + +return copied; +} +#define BLKDEV_FALLOC_FL_SUPPORTED \ (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \ FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
@@ -851,6 +875,7 @@ const struct file_operations def_blk_fops = { .splice_read = filemap_splice_read, .splice_write = iter_file_splice_write, .fallocate = blkdev_fallocate, + .copy_file_range = blkdev_copy_file_range, };static __init int blkdev_init (void )
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 7/12 nvme: add copy offload support
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
Current design only supports single source range.
We receive a request with REQ_OP_COPY_SRC.
Parse this request which consists of src(1st) and dst(2nd) bios. Form a copy command (TP 4065)
trace event support for nvme_copy_cmd.
Set the device copy limits to queue limits.
--- drivers/nvme/host/constants.c | 1 + drivers/nvme/host/core.c | 79 +++++++++++++++++++++++++++++++++++ drivers/nvme/host/trace.c | 19 +++++++++ include/linux/blkdev.h | 1 + include/linux/nvme.h | 43 +++++++++++++++++-- 5 files changed, 140 insertions(+), 3 deletions(-)
drivers/nvme/host/constants.c +1 -0
@@ -19,6 +19,7 @@ static const char * const nvme_ops[] = { [nvme_cmd_resv_report] = "Reservation Report", [nvme_cmd_resv_acquire] = "Reservation Acquire", [nvme_cmd_resv_release] = "Reservation Release", + [nvme_cmd_copy] = "Copy Offload", [nvme_cmd_zone_mgmt_send] = "Zone Management Send", [nvme_cmd_zone_mgmt_recv] = "Zone Management Receive", [nvme_cmd_zone_append] = "Zone Append",
drivers/nvme/host/core.c +79 -0
109 lines not on this page · raw message
drivers/nvme/host/trace.c +19 -0
@@ -150,6 +150,23 @@ static const char *nvme_trace_read_write(struct trace_seq *p, u8 *cdw10)return ret; } +static const char *nvme_trace_copy (struct trace_seq *p,u8 *cdw10) +{ +const char *ret =trace_seq_buffer_ptr (p); +u64 sdlba =get_unaligned_le64 (cdw10); +u8 nr_range =get_unaligned_le16 (cdw10 +8 ); +u16 control =get_unaligned_le16 (cdw10 +10 ); +u32 dsmgmt =get_unaligned_le32 (cdw10 +12 ); +u32 reftag =get_unaligned_le32 (cdw10 +16 ); + +trace_seq_printf (p, +"sdlba=%llu, nr_range=%u, ctrl=0x%x, dsmgmt=%u, reftag=%u" , + sdlba, nr_range, control, dsmgmt, reftag); +trace_seq_putc (p,0 ); + +return ret; +} +static const char *nvme_trace_dsm (struct trace_seq *p,u8 *cdw10) {const char *ret =trace_seq_buffer_ptr (p);
@@ -243,6 +260,8 @@ const char *nvme_trace_parse_nvm_cmd(struct trace_seq *p,return nvme_trace_zone_mgmt_send (p, cdw10);case nvme_cmd_zone_mgmt_recv:return nvme_trace_zone_mgmt_recv (p, cdw10); +case nvme_cmd_copy: +return nvme_trace_copy (p, cdw10);default :return nvme_trace_common (p, cdw10); }
include/linux/blkdev.h +1 -0
@@ -1226,6 +1226,7 @@ static inline unsigned int bdev_discard_granularity(struct block_device *bdev)/* maximum copy offload length, this is set to 128MB based on current testing */ #define BLK_COPY_MAX_BYTES (1 << 27) +#define BLK_COPY_MAX_SEGMENTS 2static inline unsigned int bdev_max_copy_sectors (struct block_device *bdev) {
include/linux/nvme.h +40 -3
85 lines not on this page · raw message
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 8/12 nvmet: add copy command support for bdev and file ns
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
Add support for handling nvme_cmd_copy command on target.
For bdev-ns if backing device supports copy offload we call device copy offload (blkdev_copy_offload).
In case of absence of device copy offload capability, we use copy emulation (blkdev_copy_emulation)
For file-ns we call vfs_copy_file_range to service our request.
Currently target always shows copy capability by setting NVME_CTRL_ONCS_COPY in controller ONCS.
loop target has copy support, which can be used to test copy offload. trace event support for nvme_cmd_copy.
--- drivers/nvme/target/admin-cmd.c | 9 +++- drivers/nvme/target/io-cmd-bdev.c | 71 +++++++++++++++++++++++++++++++ drivers/nvme/target/io-cmd-file.c | 50 ++++++++++++++++++++++ drivers/nvme/target/nvmet.h | 1 + drivers/nvme/target/trace.c | 19 +++++++++ 5 files changed, 148 insertions(+), 2 deletions(-)
drivers/nvme/target/admin-cmd.c +7 -2
@@ -433,8 +433,7 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req) id->nn =cpu_to_le32 (NVMET_MAX_NAMESPACES ); id->mnan =cpu_to_le32 (NVMET_MAX_NAMESPACES ); id->oncs =cpu_to_le16 (NVME_CTRL_ONCS_DSM | -NVME_CTRL_ONCS_WRITE_ZEROES ); - +NVME_CTRL_ONCS_WRITE_ZEROES |NVME_CTRL_ONCS_COPY );/* XXX: don't report vwc if the underlying device is write through */ id->vwc =NVME_CTRL_VWC_PRESENT ;
@@ -536,6 +535,12 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)if (req->ns->bdev)nvmet_bdev_set_limits (req->ns->bdev, id); +else { + id->msrc = (__force u8 )to0based (BIO_MAX_VECS -1 ); + id->mssrl =cpu_to_le16 (BIO_MAX_VECS << + (PAGE_SHIFT -SECTOR_SHIFT )); + id->mcl =cpu_to_le32 (le16_to_cpu (id->mssrl)); + }/* * We just provide a single LBA format that matches what the
drivers/nvme/target/io-cmd-bdev.c +71 -0
89 lines not on this page · raw message
drivers/nvme/target/io-cmd-file.c +50 -0
68 lines not on this page · raw message
drivers/nvme/target/nvmet.h +1 -0
@@ -405,6 +405,7 @@ struct nvmet_req {struct device *p2p_client;u16 error_loc;u64 error_slba; +size_t copy_len; };#define NVMET_MAX_MPOOL_BVEC 16
drivers/nvme/target/trace.c +19 -0
@@ -92,6 +92,23 @@ static const char *nvmet_trace_dsm(struct trace_seq *p, u8 *cdw10)return ret; } +static const char *nvmet_trace_copy (struct trace_seq *p,u8 *cdw10) +{ +const char *ret =trace_seq_buffer_ptr (p); +u64 sdlba =get_unaligned_le64 (cdw10); +u8 nr_range =get_unaligned_le16 (cdw10 +8 ); +u16 control =get_unaligned_le16 (cdw10 +10 ); +u32 dsmgmt =get_unaligned_le32 (cdw10 +12 ); +u32 reftag =get_unaligned_le32 (cdw10 +16 ); + +trace_seq_printf (p, +"sdlba=%llu, nr_range=%u, ctrl=1x%x, dsmgmt=%u, reftag=%u" , + sdlba, nr_range, control, dsmgmt, reftag); +trace_seq_putc (p,0 ); + +return ret; +} +static const char *nvmet_trace_common (struct trace_seq *p,u8 *cdw10) {const char *ret =trace_seq_buffer_ptr (p);
@@ -129,6 +146,8 @@ const char *nvmet_trace_parse_nvm_cmd(struct trace_seq *p,return nvmet_trace_read_write (p, cdw10);case nvme_cmd_dsm:return nvmet_trace_dsm (p, cdw10); +case nvme_cmd_copy: +return nvmet_trace_copy (p, cdw10);default :return nvmet_trace_common (p, cdw10); }
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 9/12 dm: Add support for copy offload
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
Before enabling copy for dm target, check if underlying devices and dm target support copy. Avoid split happening inside dm target. Fail early if the request needs split, currently splitting copy request is not supported.
--- drivers/md/dm-table.c | 37 +++++++++++++++++++++++++++++++++++ drivers/md/dm.c | 7 +++++++ include/linux/device-mapper.h | 3 +++ 3 files changed, 47 insertions(+)
drivers/md/dm-table.c +37 -0
@@ -1899,6 +1899,38 @@ static bool dm_table_supports_nowait(struct dm_table *t)return true; } +static int device_not_copy_capable (struct dm_target *ti,struct dm_dev *dev, +sector_t start,sector_t len,void *data) +{ +struct request_queue *q =bdev_get_queue (dev->bdev); + +return !q->limits.max_copy_sectors; +} + +static bool dm_table_supports_copy (struct dm_table *t) +{ +struct dm_target *ti; +unsigned int i; + +for (i =0 ; i < t->num_targets; i++) { + ti =dm_table_get_target (t, i); + +if (!ti->copy_offload_supported) +return false; + +/* +* target provides copy support (as implied by setting +* 'copy_offload_supported') +* and it relies on _all_ data devices having copy support. +*/ +if (!ti->type->iterate_devices || + ti->type->iterate_devices (ti, device_not_copy_capable,NULL )) +return false; + } + +return true; +} +static int device_not_discard_capable (struct dm_target *ti,struct dm_dev *dev,sector_t start,sector_t len,void *data) {
@@ -1981,6 +2013,11 @@ int dm_table_set_restrictions(struct dm_table *t, struct request_queue *q, q->limits.discard_misaligned =0 ; } +if (!dm_table_supports_copy (t)) { + q->limits.max_copy_sectors =0 ; + q->limits.max_copy_hw_sectors =0 ; + } +if (!dm_table_supports_secure_erase (t)) q->limits.max_secure_erase_sectors =0 ;
drivers/md/dm.c +7 -0
@@ -1721,6 +1721,13 @@ static blk_status_t __split_and_process_bio(struct clone_info *ci)if (unlikely (ci->is_abnormal_io))return __process_abnormal_io (ci, ti); +if ((unlikely (op_is_copy (ci->bio->bi_opf)) && +max_io_len (ti, ci->sector) < ci->sector_count)) { +DMERR ("Error, IO size(%u) > max target size(%llu)\n" , + ci->sector_count,max_io_len (ti, ci->sector)); +return BLK_STS_IOERR ; + } +/* * Only support bio polling for normal IO, and the target io is * exactly inside the dm_io instance (verified in dm_poll_dm_io)
include/linux/device-mapper.h +3 -0
@@ -397,6 +397,9 @@ struct dm_target {* bio_set_dev(). NOTE: ideally a target should _not_ need this. */ bool needs_bio_set_dev:1; + +/* copy offload is supported */ + bool copy_offload_supported:1; };void *dm_per_bio_data (struct bio *bio,size_t data_size);
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 10/12 dm: Enable copy offload for dm-linear target
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
Setting copy_offload_supported flag to enable offload.
--- drivers/md/dm-linear.c | 1 + 1 file changed, 1 insertion(+)
drivers/md/dm-linear.c +1 -0
@@ -62,6 +62,7 @@ static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv) ti->num_discard_bios =1 ; ti->num_secure_erase_bios =1 ; ti->num_write_zeroes_bios =1 ; + ti->copy_offload_supported =1 ; ti->private = lc;return 0 ;
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 11/12 null: Enable trace capability for null block
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
This is a prep patch to enable copy trace capability.
At present only zoned null_block is using trace, so we decoupled trace and zoned dependency to make it usable in null_blk driver also.
--- drivers/block/null_blk/Makefile | 2 -- drivers/block/null_blk/main.c | 3 +++ drivers/block/null_blk/trace.h | 2 ++ drivers/block/null_blk/zoned.c | 1 - 4 files changed, 5 insertions(+), 3 deletions(-)
drivers/block/null_blk/Makefile +0 -2
@@ -5,7 +5,5 @@ ccflags-y += -I$(src) obj-$(CONFIG_BLK_DEV_NULL_BLK) += null_blk.o null_blk-objs := main.o -ifeq ($(CONFIG_BLK_DEV_ZONED), y) null_blk-$(CONFIG_TRACING) += trace.o -endif null_blk-$(CONFIG_BLK_DEV_ZONED) += zoned.o
drivers/block/null_blk/main.c +3 -0
@@ -11,6 +11,9 @@#include <linux/init.h> #include "null_blk.h" +#define CREATE_TRACE_POINTS +#include "trace.h" +#undef pr_fmt#define pr_fmt (fmt) "null_blk: " fmt
drivers/block/null_blk/trace.h +2 -0
@@ -30,6 +30,7 @@ static inline void __assign_disk_name(char *name, struct gendisk *disk) }#endif +#ifdef CONFIG_BLK_DEV_ZONED TRACE_EVENT(nullb_zone_op, TP_PROTO(struct nullb_cmd *cmd, unsigned int zone_no, unsigned int zone_cond),
@@ -67,6 +68,7 @@ TRACE_EVENT(nullb_report_zones,TP_printk ("%s nr_zones=%u" ,__print_disk_name (__entry->disk), __entry->nr_zones) ); +#endif /* CONFIG_BLK_DEV_ZONED */ #endif /* _TRACE_NULLB_H */
drivers/block/null_blk/zoned.c +0 -1
@@ -3,7 +3,6 @@#include <linux/bitmap.h> #include "null_blk.h" -#define CREATE_TRACE_POINTS #include "trace.h" #undef pr_fmt
Signature · 1 line
2.35.1.500.gb896f729e2
PATCH v18 12/12 null_blk: add support for copy offload
In reply to Kanchan JoshiFrom: Nitesh Shetty <[email protected]>
Implementation is based on existing read and write infrastructure.copy_max_bytes: A new configfs and module parameter is introduced, which can be used to set hardware/driver supported maximum copy limit. Only request based queue mode will support for copy offload. Added tracefs support to copy IO tracing.
--- Documentation/block/null_blk.rst | 5 ++ drivers/block/null_blk/main.c | 97 ++++++++++++++++++++++++++++++- drivers/block/null_blk/null_blk.h | 1 + drivers/block/null_blk/trace.h | 23 ++++++++ 4 files changed, 123 insertions(+), 3 deletions(-)
Documentation/block/null_blk.rst +5 -0
@@ -149,3 +149,8 @@ zone_size=[MB]: Default: 256 zone_nr_conv=[nr_conv]: Default: 0 The number of conventional zones to create when block device is zoned. If zone_nr_conv >= nr_zones, it will be reduced to nr_zones - 1. + +copy_max_bytes=[size in bytes]: Default: COPY_MAX_BYTES + A module and configfs parameter which can be used to set hardware/driver + supported maximum copy offload limit. + COPY_MAX_BYTES(=128MB at present) is defined in fs.h
drivers/block/null_blk/main.c +94 -3
156 lines not on this page · raw message
drivers/block/null_blk/null_blk.h +1 -0
@@ -107,6 +107,7 @@ struct nullb_device {unsigned int queue_mode;/* block interface */ unsigned int blocksize;/* block size */ unsigned int max_sectors;/* Max sectors per command */ +unsigned long copy_max_bytes;/* Max copy offload length in bytes */ unsigned int irqmode;/* IRQ completion handler */ unsigned int hw_queue_depth;/* queue depth */ unsigned int index;/* index of the disk, only valid with a disk */
drivers/block/null_blk/trace.h +23 -0
@@ -70,6 +70,29 @@ TRACE_EVENT(nullb_report_zones, );#endif /* CONFIG_BLK_DEV_ZONED */ +TRACE_EVENT (nullb_copy_op, + TP_PROTO(struct request *req, + sector_t dst, sector_t src, size_t len), +TP_ARGS (req, dst, src, len), +TP_STRUCT__entry ( + __array(char, disk, DISK_NAME_LEN) + __field(enum req_op, op) + __field(sector_t, dst) + __field(sector_t, src) +__field (size_t, len) + ), +TP_fast_assign ( + __entry->op = req_op(req); + __assign_disk_name(__entry->disk, req->q->disk); + __entry->dst = dst; + __entry->src = src; + __entry->len = len; + ), +TP_printk ("%s req=%-15s: dst=%llu, src=%llu, len=%lu" , +__print_disk_name (__entry->disk), +blk_op_str (__entry->op), + __entry->dst, __entry->src, __entry->len) +);#endif /* _TRACE_NULLB_H */ #undef TRACE_INCLUDE_PATH
Signature · 1 line
2.35.1.500.gb896f729e2