# \[RFC PATCH 8/8\] bfq: optimize the calculation of bfq\_weight\_to\_ioprio()

- From: brookxu \<hidden\>
- Date: 2021-03-08 12:01:22 
- List: linux-block
- Page: https://ratatoskr.run/linux-block/2021/03/6292019
- Thread: https://ratatoskr.run/linux-block/2021/03/6292011/t.md

```text
From: Chunguang Xu <redacted>

From: Chunguang Xu <redacted>

The value range of ioprio is [0, 7], but the result of
bfq_weight_to_ioprio() may exceed this range, so simple
optimization is required.

Signed-off-by: Chunguang Xu <redacted>
---
 block/bfq-wf2q.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/block/bfq-wf2q.c b/block/bfq-wf2q.c
index 850a8e2f0bda..1d565daf516f 100644
--- a/block/bfq-wf2q.c
+++ b/block/bfq-wf2q.c
@@ -536,8 +536,9 @@ unsigned short bfq_ioprio_to_weight(int ioprio)
  */
 static unsigned short bfq_weight_to_ioprio(int weight)
 {
-	return max_t(int, 0,
-		     IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF - weight);
+	int ioprio = IOPRIO_BE_NR  - weight / BFQ_WEIGHT_CONVERSION_COEFF;
+
+	return ioprio < 0 ? 0 : min_t(int, ioprio, IOPRIO_BE_NR - 1);
 }
 
 static void bfq_get_entity(struct bfq_entity *entity)
-- 
2.30.0

```
