]> git.ipfire.org Git - thirdparty/linux.git/blobdiff - block/blk-core.c
Merge tag 'for-5.8/block-2020-06-01' of git://git.kernel.dk/linux-block
[thirdparty/linux.git] / block / blk-core.c
index 38d7b1f160673405b1ac9c57a459d1dd6f0ceb11..03252af8c82c82c5580f7f56cff092c75c441bf0 100644 (file)
@@ -39,6 +39,8 @@
 #include <linux/debugfs.h>
 #include <linux/bpf.h>
 #include <linux/psi.h>
+#include <linux/sched/sysctl.h>
+#include <linux/blk-crypto.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/block.h>
@@ -121,6 +123,7 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
        rq->start_time_ns = ktime_get_ns();
        rq->part = NULL;
        refcount_set(&rq->ref, 1);
+       blk_crypto_rq_set_defaults(rq);
 }
 EXPORT_SYMBOL(blk_rq_init);
 
@@ -136,6 +139,7 @@ static const char *const blk_op_name[] = {
        REQ_OP_NAME(ZONE_OPEN),
        REQ_OP_NAME(ZONE_CLOSE),
        REQ_OP_NAME(ZONE_FINISH),
+       REQ_OP_NAME(ZONE_APPEND),
        REQ_OP_NAME(WRITE_SAME),
        REQ_OP_NAME(WRITE_ZEROES),
        REQ_OP_NAME(SCSI_IN),
@@ -241,6 +245,17 @@ static void req_bio_endio(struct request *rq, struct bio *bio,
 
        bio_advance(bio, nbytes);
 
+       if (req_op(rq) == REQ_OP_ZONE_APPEND && error == BLK_STS_OK) {
+               /*
+                * Partial zone append completions cannot be supported as the
+                * BIO fragments may end up not being written sequentially.
+                */
+               if (bio->bi_iter.bi_size)
+                       bio->bi_status = BLK_STS_IOERR;
+               else
+                       bio->bi_iter.bi_sector = rq->__sector;
+       }
+
        /* don't actually finish bio if it's part of flush sequence */
        if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
                bio_endio(bio);
@@ -441,6 +456,23 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
        }
 }
 
+static inline int bio_queue_enter(struct bio *bio)
+{
+       struct request_queue *q = bio->bi_disk->queue;
+       bool nowait = bio->bi_opf & REQ_NOWAIT;
+       int ret;
+
+       ret = blk_queue_enter(q, nowait ? BLK_MQ_REQ_NOWAIT : 0);
+       if (unlikely(ret)) {
+               if (nowait && !blk_queue_dying(q))
+                       bio_wouldblock_error(bio);
+               else
+                       bio_io_error(bio);
+       }
+
+       return ret;
+}
+
 void blk_queue_exit(struct request_queue *q)
 {
        percpu_ref_put(&q->q_usage_counter);
@@ -485,7 +517,7 @@ struct request_queue *__blk_alloc_queue(int node_id)
        if (ret)
                goto fail_id;
 
-       q->backing_dev_info = bdi_alloc_node(GFP_KERNEL, node_id);
+       q->backing_dev_info = bdi_alloc(node_id);
        if (!q->backing_dev_info)
                goto fail_split;
 
@@ -495,7 +527,6 @@ struct request_queue *__blk_alloc_queue(int node_id)
 
        q->backing_dev_info->ra_pages = VM_READAHEAD_PAGES;
        q->backing_dev_info->capabilities = BDI_CAP_CGROUP_WRITEBACK;
-       q->backing_dev_info->name = "block";
        q->node = node_id;
 
        timer_setup(&q->backing_dev_info->laptop_mode_wb_timer,
@@ -606,6 +637,16 @@ void blk_put_request(struct request *req)
 }
 EXPORT_SYMBOL(blk_put_request);
 
+static void blk_account_io_merge_bio(struct request *req)
+{
+       if (!blk_do_io_stat(req))
+               return;
+
+       part_stat_lock();
+       part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
+       part_stat_unlock();
+}
+
 bool bio_attempt_back_merge(struct request *req, struct bio *bio,
                unsigned int nr_segs)
 {
@@ -624,7 +665,9 @@ bool bio_attempt_back_merge(struct request *req, struct bio *bio,
        req->biotail = bio;
        req->__data_len += bio->bi_iter.bi_size;
 
-       blk_account_io_start(req, false);
+       bio_crypt_free_ctx(bio);
+
+       blk_account_io_merge_bio(req);
        return true;
 }
 
@@ -648,7 +691,9 @@ bool bio_attempt_front_merge(struct request *req, struct bio *bio,
        req->__sector = bio->bi_iter.bi_sector;
        req->__data_len += bio->bi_iter.bi_size;
 
-       blk_account_io_start(req, false);
+       bio_crypt_do_front_merge(req, bio);
+
+       blk_account_io_merge_bio(req);
        return true;
 }
 
@@ -670,7 +715,7 @@ bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
        req->__data_len += bio->bi_iter.bi_size;
        req->nr_phys_segments = segments + 1;
 
-       blk_account_io_start(req, false);
+       blk_account_io_merge_bio(req);
        return true;
 no_merge:
        req_set_nomerge(q, req);
@@ -872,6 +917,41 @@ out:
        return ret;
 }
 
+/*
+ * Check write append to a zoned block device.
+ */
+static inline blk_status_t blk_check_zone_append(struct request_queue *q,
+                                                struct bio *bio)
+{
+       sector_t pos = bio->bi_iter.bi_sector;
+       int nr_sectors = bio_sectors(bio);
+
+       /* Only applicable to zoned block devices */
+       if (!blk_queue_is_zoned(q))
+               return BLK_STS_NOTSUPP;
+
+       /* The bio sector must point to the start of a sequential zone */
+       if (pos & (blk_queue_zone_sectors(q) - 1) ||
+           !blk_queue_zone_is_seq(q, pos))
+               return BLK_STS_IOERR;
+
+       /*
+        * Not allowed to cross zone boundaries. Otherwise, the BIO will be
+        * split and could result in non-contiguous sectors being written in
+        * different zones.
+        */
+       if (nr_sectors > q->limits.chunk_sectors)
+               return BLK_STS_IOERR;
+
+       /* Make sure the BIO is small enough and will not get split */
+       if (nr_sectors > q->limits.max_zone_append_sectors)
+               return BLK_STS_IOERR;
+
+       bio->bi_opf |= REQ_NOMERGE;
+
+       return BLK_STS_OK;
+}
+
 static noinline_for_stack bool
 generic_make_request_checks(struct bio *bio)
 {
@@ -941,6 +1021,11 @@ generic_make_request_checks(struct bio *bio)
                if (!q->limits.max_write_same_sectors)
                        goto not_supported;
                break;
+       case REQ_OP_ZONE_APPEND:
+               status = blk_check_zone_append(q, bio);
+               if (status != BLK_STS_OK)
+                       goto end_io;
+               break;
        case REQ_OP_ZONE_RESET:
        case REQ_OP_ZONE_OPEN:
        case REQ_OP_ZONE_CLOSE:
@@ -961,12 +1046,13 @@ generic_make_request_checks(struct bio *bio)
        }
 
        /*
-        * Various block parts want %current->io_context and lazy ioc
-        * allocation ends up trading a lot of pain for a small amount of
-        * memory.  Just allocate it upfront.  This may fail and block
-        * layer knows how to live with it.
+        * Various block parts want %current->io_context, so allocate it up
+        * front rather than dealing with lots of pain to allocate it only
+        * where needed. This may fail and the block layer knows how to live
+        * with it.
         */
-       create_io_context(GFP_ATOMIC, q->node);
+       if (unlikely(!current->io_context))
+               create_task_io_context(current, GFP_ATOMIC, q->node);
 
        if (!blkcg_bio_issue_check(q, bio))
                return false;
@@ -988,29 +1074,28 @@ end_io:
        return false;
 }
 
+static blk_qc_t do_make_request(struct bio *bio)
+{
+       struct request_queue *q = bio->bi_disk->queue;
+       blk_qc_t ret = BLK_QC_T_NONE;
+
+       if (blk_crypto_bio_prep(&bio)) {
+               if (!q->make_request_fn)
+                       return blk_mq_make_request(q, bio);
+               ret = q->make_request_fn(q, bio);
+       }
+       blk_queue_exit(q);
+       return ret;
+}
+
 /**
- * generic_make_request - hand a buffer to its device driver for I/O
+ * generic_make_request - re-submit a bio to the block device layer for I/O
  * @bio:  The bio describing the location in memory and on the device.
  *
- * generic_make_request() is used to make I/O requests of block
- * devices. It is passed a &struct bio, which describes the I/O that needs
- * to be done.
- *
- * generic_make_request() does not return any status.  The
- * success/failure status of the request, along with notification of
- * completion, is delivered asynchronously through the bio->bi_end_io
- * function described (one day) else where.
- *
- * The caller of generic_make_request must make sure that bi_io_vec
- * are set to describe the memory buffer, and that bi_dev and bi_sector are
- * set to describe the device address, and the
- * bi_end_io and optionally bi_private are set to describe how
- * completion notification should be signaled.
- *
- * generic_make_request and the drivers it calls may use bi_next if this
- * bio happens to be merged with someone else, and may resubmit the bio to
- * a lower device by calling into generic_make_request recursively, which
- * means the bio should NOT be touched after the call to ->make_request_fn.
+ * This is a version of submit_bio() that shall only be used for I/O that is
+ * resubmitted to lower level drivers by stacking block drivers.  All file
+ * systems and other upper level users of the block layer should use
+ * submit_bio() instead.
  */
 blk_qc_t generic_make_request(struct bio *bio)
 {
@@ -1061,18 +1146,14 @@ blk_qc_t generic_make_request(struct bio *bio)
        current->bio_list = bio_list_on_stack;
        do {
                struct request_queue *q = bio->bi_disk->queue;
-               blk_mq_req_flags_t flags = bio->bi_opf & REQ_NOWAIT ?
-                       BLK_MQ_REQ_NOWAIT : 0;
 
-               if (likely(blk_queue_enter(q, flags) == 0)) {
+               if (likely(bio_queue_enter(bio) == 0)) {
                        struct bio_list lower, same;
 
                        /* Create a fresh bio_list for all subordinate requests */
                        bio_list_on_stack[1] = bio_list_on_stack[0];
                        bio_list_init(&bio_list_on_stack[0]);
-                       ret = q->make_request_fn(q, bio);
-
-                       blk_queue_exit(q);
+                       ret = do_make_request(bio);
 
                        /* sort new bios into those for a lower level
                         * and those for the same level
@@ -1088,12 +1169,6 @@ blk_qc_t generic_make_request(struct bio *bio)
                        bio_list_merge(&bio_list_on_stack[0], &lower);
                        bio_list_merge(&bio_list_on_stack[0], &same);
                        bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
-               } else {
-                       if (unlikely(!blk_queue_dying(q) &&
-                                       (bio->bi_opf & REQ_NOWAIT)))
-                               bio_wouldblock_error(bio);
-                       else
-                               bio_io_error(bio);
                }
                bio = bio_list_pop(&bio_list_on_stack[0]);
        } while (bio);
@@ -1110,30 +1185,25 @@ EXPORT_SYMBOL(generic_make_request);
  *
  * This function behaves like generic_make_request(), but does not protect
  * against recursion.  Must only be used if the called driver is known
- * to not call generic_make_request (or direct_make_request) again from
- * its make_request function.  (Calling direct_make_request again from
- * a workqueue is perfectly fine as that doesn't recurse).
+ * to be blk-mq based.
  */
 blk_qc_t direct_make_request(struct bio *bio)
 {
        struct request_queue *q = bio->bi_disk->queue;
-       bool nowait = bio->bi_opf & REQ_NOWAIT;
-       blk_qc_t ret;
 
+       if (WARN_ON_ONCE(q->make_request_fn)) {
+               bio_io_error(bio);
+               return BLK_QC_T_NONE;
+       }
        if (!generic_make_request_checks(bio))
                return BLK_QC_T_NONE;
-
-       if (unlikely(blk_queue_enter(q, nowait ? BLK_MQ_REQ_NOWAIT : 0))) {
-               if (nowait && !blk_queue_dying(q))
-                       bio_wouldblock_error(bio);
-               else
-                       bio_io_error(bio);
+       if (unlikely(bio_queue_enter(bio)))
+               return BLK_QC_T_NONE;
+       if (!blk_crypto_bio_prep(&bio)) {
+               blk_queue_exit(q);
                return BLK_QC_T_NONE;
        }
-
-       ret = q->make_request_fn(q, bio);
-       blk_queue_exit(q);
-       return ret;
+       return blk_mq_make_request(q, bio);
 }
 EXPORT_SYMBOL_GPL(direct_make_request);
 
@@ -1141,17 +1211,17 @@ EXPORT_SYMBOL_GPL(direct_make_request);
  * submit_bio - submit a bio to the block device layer for I/O
  * @bio: The &struct bio which describes the I/O
  *
- * submit_bio() is very similar in purpose to generic_make_request(), and
- * uses that function to do most of the work. Both are fairly rough
- * interfaces; @bio must be presetup and ready for I/O.
+ * submit_bio() is used to submit I/O requests to block devices.  It is passed a
+ * fully set up &struct bio that describes the I/O that needs to be done.  The
+ * bio will be send to the device described by the bi_disk and bi_partno fields.
  *
+ * The success/failure status of the request, along with notification of
+ * completion, is delivered asynchronously through the ->bi_end_io() callback
+ * in @bio.  The bio must NOT be touched by thecaller until ->bi_end_io() has
+ * been called.
  */
 blk_qc_t submit_bio(struct bio *bio)
 {
-       bool workingset_read = false;
-       unsigned long pflags;
-       blk_qc_t ret;
-
        if (blkcg_punt_bio_submit(bio))
                return BLK_QC_T_NONE;
 
@@ -1170,8 +1240,6 @@ blk_qc_t submit_bio(struct bio *bio)
                if (op_is_write(bio_op(bio))) {
                        count_vm_events(PGPGOUT, count);
                } else {
-                       if (bio_flagged(bio, BIO_WORKINGSET))
-                               workingset_read = true;
                        task_io_account_read(bio->bi_iter.bi_size);
                        count_vm_events(PGPGIN, count);
                }
@@ -1187,20 +1255,24 @@ blk_qc_t submit_bio(struct bio *bio)
        }
 
        /*
-        * If we're reading data that is part of the userspace
-        * workingset, count submission time as memory stall. When the
-        * device is congested, or the submitting cgroup IO-throttled,
-        * submission can be a significant part of overall IO time.
+        * If we're reading data that is part of the userspace workingset, count
+        * submission time as memory stall.  When the device is congested, or
+        * the submitting cgroup IO-throttled, submission can be a significant
+        * part of overall IO time.
         */
-       if (workingset_read)
-               psi_memstall_enter(&pflags);
-
-       ret = generic_make_request(bio);
+       if (unlikely(bio_op(bio) == REQ_OP_READ &&
+           bio_flagged(bio, BIO_WORKINGSET))) {
+               unsigned long pflags;
+               blk_qc_t ret;
 
-       if (workingset_read)
+               psi_memstall_enter(&pflags);
+               ret = generic_make_request(bio);
                psi_memstall_leave(&pflags);
 
-       return ret;
+               return ret;
+       }
+
+       return generic_make_request(bio);
 }
 EXPORT_SYMBOL(submit_bio);
 
@@ -1261,8 +1333,11 @@ blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request *
            should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
                return BLK_STS_IOERR;
 
+       if (blk_crypto_insert_cloned_request(rq))
+               return BLK_STS_IOERR;
+
        if (blk_queue_io_stat(q))
-               blk_account_io_start(rq, true);
+               blk_account_io_start(rq);
 
        /*
         * Since we have a scheduler attached on the top device,
@@ -1314,7 +1389,22 @@ unsigned int blk_rq_err_bytes(const struct request *rq)
 }
 EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
 
-void blk_account_io_completion(struct request *req, unsigned int bytes)
+static void update_io_ticks(struct hd_struct *part, unsigned long now, bool end)
+{
+       unsigned long stamp;
+again:
+       stamp = READ_ONCE(part->stamp);
+       if (unlikely(stamp != now)) {
+               if (likely(cmpxchg(&part->stamp, stamp, now) == stamp))
+                       __part_stat_add(part, io_ticks, end ? now - stamp : 1);
+       }
+       if (part->partno) {
+               part = &part_to_disk(part)->part0;
+               goto again;
+       }
+}
+
+static void blk_account_io_completion(struct request *req, unsigned int bytes)
 {
        if (req->part && blk_do_io_stat(req)) {
                const int sgrp = op_stat_group(req_op(req));
@@ -1345,48 +1435,57 @@ void blk_account_io_done(struct request *req, u64 now)
                update_io_ticks(part, jiffies, true);
                part_stat_inc(part, ios[sgrp]);
                part_stat_add(part, nsecs[sgrp], now - req->start_time_ns);
-               part_dec_in_flight(req->q, part, rq_data_dir(req));
+               part_stat_unlock();
 
                hd_struct_put(part);
-               part_stat_unlock();
        }
 }
 
-void blk_account_io_start(struct request *rq, bool new_io)
+void blk_account_io_start(struct request *rq)
 {
-       struct hd_struct *part;
-       int rw = rq_data_dir(rq);
-
        if (!blk_do_io_stat(rq))
                return;
 
+       rq->part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
+
        part_stat_lock();
+       update_io_ticks(rq->part, jiffies, false);
+       part_stat_unlock();
+}
 
-       if (!new_io) {
-               part = rq->part;
-               part_stat_inc(part, merges[rw]);
-       } else {
-               part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
-               if (!hd_struct_try_get(part)) {
-                       /*
-                        * The partition is already being removed,
-                        * the request will be accounted on the disk only
-                        *
-                        * We take a reference on disk->part0 although that
-                        * partition will never be deleted, so we can treat
-                        * it as any other partition.
-                        */
-                       part = &rq->rq_disk->part0;
-                       hd_struct_get(part);
-               }
-               part_inc_in_flight(rq->q, part, rw);
-               rq->part = part;
-       }
+unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
+               unsigned int op)
+{
+       struct hd_struct *part = &disk->part0;
+       const int sgrp = op_stat_group(op);
+       unsigned long now = READ_ONCE(jiffies);
+
+       part_stat_lock();
+       update_io_ticks(part, now, false);
+       part_stat_inc(part, ios[sgrp]);
+       part_stat_add(part, sectors[sgrp], sectors);
+       part_stat_local_inc(part, in_flight[op_is_write(op)]);
+       part_stat_unlock();
 
-       update_io_ticks(part, jiffies, false);
+       return now;
+}
+EXPORT_SYMBOL(disk_start_io_acct);
+
+void disk_end_io_acct(struct gendisk *disk, unsigned int op,
+               unsigned long start_time)
+{
+       struct hd_struct *part = &disk->part0;
+       const int sgrp = op_stat_group(op);
+       unsigned long now = READ_ONCE(jiffies);
+       unsigned long duration = now - start_time;
 
+       part_stat_lock();
+       update_io_ticks(part, now, true);
+       part_stat_add(part, nsecs[sgrp], jiffies_to_nsecs(duration));
+       part_stat_local_dec(part, in_flight[op_is_write(op)]);
        part_stat_unlock();
 }
+EXPORT_SYMBOL(disk_end_io_acct);
 
 /*
  * Steal bios from a request and add them to a bio list.
@@ -1636,7 +1735,9 @@ int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
        }
        rq->nr_phys_segments = rq_src->nr_phys_segments;
        rq->ioprio = rq_src->ioprio;
-       rq->extra_len = rq_src->extra_len;
+
+       if (rq->bio)
+               blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask);
 
        return 0;
 
@@ -1778,6 +1879,18 @@ void blk_finish_plug(struct blk_plug *plug)
 }
 EXPORT_SYMBOL(blk_finish_plug);
 
+void blk_io_schedule(void)
+{
+       /* Prevent hang_check timer from firing at us during very long I/O */
+       unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
+
+       if (timeout)
+               io_schedule_timeout(timeout);
+       else
+               io_schedule();
+}
+EXPORT_SYMBOL_GPL(blk_io_schedule);
+
 int __init blk_dev_init(void)
 {
        BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));