--- /dev/null
+From db37a34c563bf4692b36990ae89005c031385e52 Mon Sep 17 00:00:00 2001
+From: Paolo Valente <paolo.valente@linaro.org>
+Date: Mon, 3 Feb 2020 11:40:59 +0100
+Subject: block, bfq: get a ref to a group when adding it to a service tree
+
+From: Paolo Valente <paolo.valente@linaro.org>
+
+commit db37a34c563bf4692b36990ae89005c031385e52 upstream.
+
+BFQ schedules generic entities, which may represent either bfq_queues
+or groups of bfq_queues. When an entity is inserted into a service
+tree, a reference must be taken, to make sure that the entity does not
+disappear while still referred in the tree. Unfortunately, such a
+reference is mistakenly taken only if the entity represents a
+bfq_queue. This commit takes a reference also in case the entity
+represents a group.
+
+Tested-by: Oleksandr Natalenko <oleksandr@natalenko.name>
+Tested-by: Chris Evich <cevich@redhat.com>
+Signed-off-by: Paolo Valente <paolo.valente@linaro.org>
+Signed-off-by: Jens Axboe <axboe@kernel.dk>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ block/bfq-cgroup.c | 2 +-
+ block/bfq-iosched.h | 1 +
+ block/bfq-wf2q.c | 12 ++++++++++--
+ 3 files changed, 12 insertions(+), 3 deletions(-)
+
+--- a/block/bfq-cgroup.c
++++ b/block/bfq-cgroup.c
+@@ -332,7 +332,7 @@ static void bfqg_put(struct bfq_group *b
+ kfree(bfqg);
+ }
+
+-static void bfqg_and_blkg_get(struct bfq_group *bfqg)
++void bfqg_and_blkg_get(struct bfq_group *bfqg)
+ {
+ /* see comments in bfq_bic_update_cgroup for why refcounting bfqg */
+ bfqg_get(bfqg);
+--- a/block/bfq-iosched.h
++++ b/block/bfq-iosched.h
+@@ -978,6 +978,7 @@ struct bfq_group *bfq_find_set_group(str
+ struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg);
+ struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
+ struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node);
++void bfqg_and_blkg_get(struct bfq_group *bfqg);
+ void bfqg_and_blkg_put(struct bfq_group *bfqg);
+
+ #ifdef CONFIG_BFQ_GROUP_IOSCHED
+--- a/block/bfq-wf2q.c
++++ b/block/bfq-wf2q.c
+@@ -536,7 +536,9 @@ static void bfq_get_entity(struct bfq_en
+ bfqq->ref++;
+ bfq_log_bfqq(bfqq->bfqd, bfqq, "get_entity: %p %d",
+ bfqq, bfqq->ref);
+- }
++ } else
++ bfqg_and_blkg_get(container_of(entity, struct bfq_group,
++ entity));
+ }
+
+ /**
+@@ -650,8 +652,14 @@ static void bfq_forget_entity(struct bfq
+
+ entity->on_st = false;
+ st->wsum -= entity->weight;
+- if (bfqq && !is_in_service)
++ if (is_in_service)
++ return;
++
++ if (bfqq)
+ bfq_put_queue(bfqq);
++ else
++ bfqg_and_blkg_put(container_of(entity, struct bfq_group,
++ entity));
+ }
+
+ /**
--- /dev/null
+From 4d8340d0d4d90e7ca367d18ec16c2fefa89a339c Mon Sep 17 00:00:00 2001
+From: Paolo Valente <paolo.valente@linaro.org>
+Date: Mon, 3 Feb 2020 11:40:58 +0100
+Subject: block, bfq: remove ifdefs from around gets/puts of bfq groups
+
+From: Paolo Valente <paolo.valente@linaro.org>
+
+commit 4d8340d0d4d90e7ca367d18ec16c2fefa89a339c upstream.
+
+ifdefs around gets and puts of bfq groups reduce readability, remove them.
+
+Tested-by: Oleksandr Natalenko <oleksandr@natalenko.name>
+Reported-by: Jens Axboe <axboe@kernel.dk>
+Signed-off-by: Paolo Valente <paolo.valente@linaro.org>
+Signed-off-by: Jens Axboe <axboe@kernel.dk>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ block/bfq-cgroup.c | 4 ++++
+ block/bfq-iosched.c | 6 +-----
+ block/bfq-iosched.h | 1 +
+ 3 files changed, 6 insertions(+), 5 deletions(-)
+
+--- a/block/bfq-cgroup.c
++++ b/block/bfq-cgroup.c
+@@ -1388,6 +1388,10 @@ struct bfq_group *bfqq_group(struct bfq_
+ return bfqq->bfqd->root_group;
+ }
+
++void bfqg_and_blkg_get(struct bfq_group *bfqg) {}
++
++void bfqg_and_blkg_put(struct bfq_group *bfqg) {}
++
+ struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
+ {
+ struct bfq_group *bfqg;
+--- a/block/bfq-iosched.c
++++ b/block/bfq-iosched.c
+@@ -4826,9 +4826,7 @@ void bfq_put_queue(struct bfq_queue *bfq
+ {
+ struct bfq_queue *item;
+ struct hlist_node *n;
+-#ifdef CONFIG_BFQ_GROUP_IOSCHED
+ struct bfq_group *bfqg = bfqq_group(bfqq);
+-#endif
+
+ if (bfqq->bfqd)
+ bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d",
+@@ -4901,9 +4899,7 @@ void bfq_put_queue(struct bfq_queue *bfq
+ bfqq->bfqd->last_completed_rq_bfqq = NULL;
+
+ kmem_cache_free(bfq_pool, bfqq);
+-#ifdef CONFIG_BFQ_GROUP_IOSCHED
+ bfqg_and_blkg_put(bfqg);
+-#endif
+ }
+
+ static void bfq_put_cooperator(struct bfq_queue *bfqq)
+@@ -6387,10 +6383,10 @@ static void bfq_exit_queue(struct elevat
+
+ hrtimer_cancel(&bfqd->idle_slice_timer);
+
+-#ifdef CONFIG_BFQ_GROUP_IOSCHED
+ /* release oom-queue reference to root group */
+ bfqg_and_blkg_put(bfqd->root_group);
+
++#ifdef CONFIG_BFQ_GROUP_IOSCHED
+ blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq);
+ #else
+ spin_lock_irq(&bfqd->lock);
+--- a/block/bfq-iosched.h
++++ b/block/bfq-iosched.h
+@@ -916,6 +916,7 @@ struct bfq_group {
+
+ #else
+ struct bfq_group {
++ struct bfq_entity entity;
+ struct bfq_sched_data sched_data;
+
+ struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];