Re: [RFC PATCH v3 1/2] block: add simple copy support

2020-12-13 Thread Selva Jove
On Fri, Dec 11, 2020 at 9:56 PM Johannes Thumshirn
 wrote:
>
> On 11/12/2020 15:57, SelvaKumar S wrote:
> [...]
> > +int blk_copy_emulate(struct block_device *bdev, struct blk_copy_payload 
> > *payload,
> > + gfp_t gfp_mask)
> > +{
> > + struct request_queue *q = bdev_get_queue(bdev);
> > + struct bio *bio;
> > + void *buf = NULL;
> > + int i, nr_srcs, max_range_len, ret, cur_dest, cur_size;
> > +
> > + nr_srcs = payload->copy_range;
> > + max_range_len = q->limits.max_copy_range_sectors << SECTOR_SHIFT;
> > + cur_dest = payload->dest;
> > + buf = kvmalloc(max_range_len, GFP_ATOMIC);
>
> Why GFP_ATOMIC and not the passed in gfp_mask? Especially as this is a 
> kvmalloc()
> which has the potential to grow quite big.
>
> > +int __blkdev_issue_copy(struct block_device *bdev, sector_t dest,
> > + sector_t nr_srcs, struct range_entry *rlist, gfp_t gfp_mask,
> > + int flags, struct bio **biop)
> > +{
>
> [...]
>
> > + total_size = struct_size(payload, range, nr_srcs);
> > + payload = kmalloc(total_size, GFP_ATOMIC | __GFP_NOWARN);
>
> Same here.
>
>
> > diff --git a/block/ioctl.c b/block/ioctl.c
> > index 6b785181344f..a4a507d85e56 100644
> > --- a/block/ioctl.c
> > +++ b/block/ioctl.c
> > @@ -142,6 +142,47 @@ static int blk_ioctl_discard(struct block_device 
> > *bdev, fmode_t mode,
> >   GFP_KERNEL, flags);
> >  }
> >
> > +static int blk_ioctl_copy(struct block_device *bdev, fmode_t mode,
> > + unsigned long arg, unsigned long flags)
> > +{
>
> [...]
>
> > +
> > + rlist = kmalloc_array(crange.nr_range, sizeof(*rlist),
> > + GFP_ATOMIC | __GFP_NOWARN);
>
> And here. I think this one can even be GFP_KERNEL.
>
>
>

Thanks. Will fix this.


Re: [RFC PATCH v3 1/2] block: add simple copy support

2020-12-13 Thread Selva Jove
On Fri, Dec 11, 2020 at 11:35 PM Keith Busch  wrote:
>
> On Fri, Dec 11, 2020 at 07:21:38PM +0530, SelvaKumar S wrote:
> > +int blk_copy_emulate(struct block_device *bdev, struct blk_copy_payload 
> > *payload,
> > + gfp_t gfp_mask)
> > +{
> > + struct request_queue *q = bdev_get_queue(bdev);
> > + struct bio *bio;
> > + void *buf = NULL;
> > + int i, nr_srcs, max_range_len, ret, cur_dest, cur_size;
> > +
> > + nr_srcs = payload->copy_range;
> > + max_range_len = q->limits.max_copy_range_sectors << SECTOR_SHIFT;
>
> The default value for this limit is 0, and this is the function for when
> the device doesn't support copy. Are we expecting drivers to set this
> value to something else for that case?

Sorry. Missed that. Will add a fix.

>
> > + cur_dest = payload->dest;
> > + buf = kvmalloc(max_range_len, GFP_ATOMIC);
> > + if (!buf)
> > + return -ENOMEM;
> > +
> > + for (i = 0; i < nr_srcs; i++) {
> > + bio = bio_alloc(gfp_mask, 1);
> > + bio->bi_iter.bi_sector = payload->range[i].src;
> > + bio->bi_opf = REQ_OP_READ;
> > + bio_set_dev(bio, bdev);
> > +
> > + cur_size = payload->range[i].len << SECTOR_SHIFT;
> > + ret = bio_add_page(bio, virt_to_page(buf), cur_size,
> > +offset_in_page(payload));
>
> 'buf' is vmalloc'ed, so we don't necessarily have congituous pages. I
> think you need to allocate the bio with bio_map_kern() or something like
> that instead with that kind of memory.
>

Sure. Will use bio_map_kern().

> > + if (ret != cur_size) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + ret = submit_bio_wait(bio);
> > + bio_put(bio);
> > + if (ret)
> > + goto out;
> > +
> > + bio = bio_alloc(gfp_mask, 1);
> > + bio_set_dev(bio, bdev);
> > + bio->bi_opf = REQ_OP_WRITE;
> > + bio->bi_iter.bi_sector = cur_dest;
> > + ret = bio_add_page(bio, virt_to_page(buf), cur_size,
> > +offset_in_page(payload));
> > + if (ret != cur_size) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + ret = submit_bio_wait(bio);
> > + bio_put(bio);
> > + if (ret)
> > + goto out;
> > +
> > + cur_dest += payload->range[i].len;
> > + }
>
> I think this would be a faster implementation if the reads were
> asynchronous with a payload buffer allocated specific to that read, and
> the callback can enqueue the write part. This would allow you to
> accumulate all the read data and write it in a single call.

Sounds like a better approach. Will add this implementation in v4.


Re: [RFC PATCH v3 1/2] block: add simple copy support

2020-12-11 Thread Keith Busch
On Fri, Dec 11, 2020 at 07:21:38PM +0530, SelvaKumar S wrote:
> +int blk_copy_emulate(struct block_device *bdev, struct blk_copy_payload 
> *payload,
> + gfp_t gfp_mask)
> +{
> + struct request_queue *q = bdev_get_queue(bdev);
> + struct bio *bio;
> + void *buf = NULL;
> + int i, nr_srcs, max_range_len, ret, cur_dest, cur_size;
> +
> + nr_srcs = payload->copy_range;
> + max_range_len = q->limits.max_copy_range_sectors << SECTOR_SHIFT;

The default value for this limit is 0, and this is the function for when
the device doesn't support copy. Are we expecting drivers to set this
value to something else for that case?

> + cur_dest = payload->dest;
> + buf = kvmalloc(max_range_len, GFP_ATOMIC);
> + if (!buf)
> + return -ENOMEM;
> +
> + for (i = 0; i < nr_srcs; i++) {
> + bio = bio_alloc(gfp_mask, 1);
> + bio->bi_iter.bi_sector = payload->range[i].src;
> + bio->bi_opf = REQ_OP_READ;
> + bio_set_dev(bio, bdev);
> +
> + cur_size = payload->range[i].len << SECTOR_SHIFT;
> + ret = bio_add_page(bio, virt_to_page(buf), cur_size,
> +offset_in_page(payload));

'buf' is vmalloc'ed, so we don't necessarily have congituous pages. I
think you need to allocate the bio with bio_map_kern() or something like
that instead with that kind of memory.

> + if (ret != cur_size) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + ret = submit_bio_wait(bio);
> + bio_put(bio);
> + if (ret)
> + goto out;
> +
> + bio = bio_alloc(gfp_mask, 1);
> + bio_set_dev(bio, bdev);
> + bio->bi_opf = REQ_OP_WRITE;
> + bio->bi_iter.bi_sector = cur_dest;
> + ret = bio_add_page(bio, virt_to_page(buf), cur_size,
> +offset_in_page(payload));
> + if (ret != cur_size) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + ret = submit_bio_wait(bio);
> + bio_put(bio);
> + if (ret)
> + goto out;
> +
> + cur_dest += payload->range[i].len;
> + }

I think this would be a faster implementation if the reads were
asynchronous with a payload buffer allocated specific to that read, and
the callback can enqueue the write part. This would allow you to
accumulate all the read data and write it in a single call. 


Re: [RFC PATCH v3 1/2] block: add simple copy support

2020-12-11 Thread Mikulas Patocka



On Fri, 11 Dec 2020, Johannes Thumshirn wrote:

> On 11/12/2020 15:57, SelvaKumar S wrote:
> [...] 
> > +int blk_copy_emulate(struct block_device *bdev, struct blk_copy_payload 
> > *payload,
> > +   gfp_t gfp_mask)
> > +{
> > +   struct request_queue *q = bdev_get_queue(bdev);
> > +   struct bio *bio;
> > +   void *buf = NULL;
> > +   int i, nr_srcs, max_range_len, ret, cur_dest, cur_size;
> > +
> > +   nr_srcs = payload->copy_range;
> > +   max_range_len = q->limits.max_copy_range_sectors << SECTOR_SHIFT;
> > +   cur_dest = payload->dest;
> > +   buf = kvmalloc(max_range_len, GFP_ATOMIC);
> 
> Why GFP_ATOMIC and not the passed in gfp_mask? Especially as this is a 
> kvmalloc()
> which has the potential to grow quite big.

You are right, this is confusing.

There's this piece of code at the top of kvmalloc_node:
if ((flags & GFP_KERNEL) != GFP_KERNEL)
return kmalloc_node(size, flags, node);

So, when you use GFP_ATOMIC flag, it will always fall back to kmalloc.

Mikulas



Re: [RFC PATCH v3 1/2] block: add simple copy support

2020-12-11 Thread Johannes Thumshirn
On 11/12/2020 15:57, SelvaKumar S wrote:
[...] 
> +int blk_copy_emulate(struct block_device *bdev, struct blk_copy_payload 
> *payload,
> + gfp_t gfp_mask)
> +{
> + struct request_queue *q = bdev_get_queue(bdev);
> + struct bio *bio;
> + void *buf = NULL;
> + int i, nr_srcs, max_range_len, ret, cur_dest, cur_size;
> +
> + nr_srcs = payload->copy_range;
> + max_range_len = q->limits.max_copy_range_sectors << SECTOR_SHIFT;
> + cur_dest = payload->dest;
> + buf = kvmalloc(max_range_len, GFP_ATOMIC);

Why GFP_ATOMIC and not the passed in gfp_mask? Especially as this is a 
kvmalloc()
which has the potential to grow quite big.

> +int __blkdev_issue_copy(struct block_device *bdev, sector_t dest,
> + sector_t nr_srcs, struct range_entry *rlist, gfp_t gfp_mask,
> + int flags, struct bio **biop)
> +{

[...]

> + total_size = struct_size(payload, range, nr_srcs);
> + payload = kmalloc(total_size, GFP_ATOMIC | __GFP_NOWARN);

Same here. 


> diff --git a/block/ioctl.c b/block/ioctl.c
> index 6b785181344f..a4a507d85e56 100644
> --- a/block/ioctl.c
> +++ b/block/ioctl.c
> @@ -142,6 +142,47 @@ static int blk_ioctl_discard(struct block_device *bdev, 
> fmode_t mode,
>   GFP_KERNEL, flags);
>  }
>  
> +static int blk_ioctl_copy(struct block_device *bdev, fmode_t mode,
> + unsigned long arg, unsigned long flags)
> +{

[...]

> +
> + rlist = kmalloc_array(crange.nr_range, sizeof(*rlist),
> + GFP_ATOMIC | __GFP_NOWARN);

And here. I think this one can even be GFP_KERNEL.

 



[RFC PATCH v3 1/2] block: add simple copy support

2020-12-11 Thread SelvaKumar S
Add new BLKCOPY ioctl that offloads copying of multiple sources
to a destination to the device. Accept copy_ranges that contains
destination, no of sources and pointer to the array of source
ranges. Each range_entry contains start and length of source
ranges (in bytes).

Introduce REQ_OP_COPY, a no-merge copy offload operation. Create
bio with control information as payload and submit to the device.
REQ_OP_COPY(19) is a write op and takes zone_write_lock when submitted
to zoned device.

If the device doesn't support copy or copy offload is disabled, then
copy is emulated by reading and writing each source ranges one by one.

Introduce queue limits for simple copy and other helper functions.
Add device limits as sysfs entries.
- copy_offload
- max_copy_sectors
- max_copy_ranges_sectors
- max_copy_nr_ranges

copy_offload(= 0) is disabled by default.
max_copy_sectors = 0 indicates the device doesn't support copy.

simple copy is not supported for stacked devices.

Signed-off-by: SelvaKumar S 
Signed-off-by: Kanchan Joshi 
Signed-off-by: Nitesh Shetty 
Signed-off-by: Javier González 
---
 block/blk-core.c  |  94 ++--
 block/blk-lib.c   | 182 ++
 block/blk-merge.c |   2 +
 block/blk-settings.c  |  10 +++
 block/blk-sysfs.c |  50 +++
 block/blk-zoned.c |   1 +
 block/bounce.c|   1 +
 block/ioctl.c |  43 +
 include/linux/bio.h   |   1 +
 include/linux/blk_types.h |  15 
 include/linux/blkdev.h|  16 
 include/uapi/linux/fs.h   |  13 +++
 12 files changed, 420 insertions(+), 8 deletions(-)

diff --git a/block/blk-core.c b/block/blk-core.c
index 2db8bda43b6e..07d64514e77b 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -719,6 +719,17 @@ static noinline int should_fail_bio(struct bio *bio)
 }
 ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
 
+static inline int bio_check_copy_eod(struct bio *bio, sector_t start,
+   sector_t nr_sectors, sector_t maxsector)
+{
+   if (nr_sectors && maxsector &&
+   (nr_sectors > maxsector || start > maxsector - nr_sectors)) {
+   handle_bad_sector(bio, maxsector);
+   return -EIO;
+   }
+   return 0;
+}
+
 /*
  * Check whether this bio extends beyond the end of the device or partition.
  * This may well happen - the kernel calls bread() without checking the size of
@@ -737,6 +748,65 @@ static inline int bio_check_eod(struct bio *bio, sector_t 
maxsector)
return 0;
 }
 
+/*
+ * Check for copy limits and remap source ranges if needed.
+ */
+static int blk_check_copy(struct bio *bio)
+{
+   struct hd_struct *p = NULL;
+   struct request_queue *q = bio->bi_disk->queue;
+   struct blk_copy_payload *payload;
+   int i, maxsector, start_sect = 0, ret = -EIO;
+   unsigned short nr_range;
+
+   rcu_read_lock();
+
+   if (bio->bi_partno) {
+   p = __disk_get_part(bio->bi_disk, bio->bi_partno);
+   if (unlikely(!p))
+   goto out;
+   if (unlikely(bio_check_ro(bio, p)))
+   goto out;
+   maxsector = part_nr_sects_read(p);
+   start_sect = p->start_sect;
+   } else {
+   if (unlikely(bio_check_ro(bio, >bi_disk->part0)))
+   goto out;
+   maxsector =  get_capacity(bio->bi_disk);
+   }
+
+   payload = bio_data(bio);
+   nr_range = payload->copy_range;
+
+   /* cannot handle copy crossing nr_ranges limit */
+   if (payload->copy_range > q->limits.max_copy_nr_ranges)
+   goto out;
+
+   /* cannot handle copy more than copy limits */
+   if (payload->copy_size > q->limits.max_copy_sectors)
+   goto out;
+
+   /* check if copy length crosses eod */
+   if (unlikely(bio_check_copy_eod(bio, bio->bi_iter.bi_sector,
+   payload->copy_size, maxsector)))
+   goto out;
+   bio->bi_iter.bi_sector += start_sect;
+
+   for (i = 0; i < nr_range; i++) {
+   if (unlikely(bio_check_copy_eod(bio, payload->range[i].src,
+   payload->range[i].len, maxsector)))
+   goto out;
+   payload->range[i].src += start_sect;
+   }
+
+   if (p)
+   bio->bi_partno = 0;
+   ret = 0;
+out:
+   rcu_read_unlock();
+   return ret;
+}
+
 /*
  * Remap block n of partition p to block n+start(p) of the disk.
  */
@@ -825,14 +895,16 @@ static noinline_for_stack bool submit_bio_checks(struct 
bio *bio)
if (should_fail_bio(bio))
goto end_io;
 
-   if (bio->bi_partno) {
-   if (unlikely(blk_partition_remap(bio)))
-   goto end_io;
-   } else {
-   if (unlikely(bio_check_ro(bio, >bi_disk->part0)))
-   goto