[PATCH 1/3] delayacct: introduce delayacct_enabled() to simplify implement

From: brookxu.cn
Date: Sun Oct 08 2023 - 06:49:52 EST


From: Chunguang Xu <chunguang.xu@xxxxxxxxxx>

Introduce delayacct_enabled() to simplify the code and make it
more concise.

Signed-off-by: Chunguang Xu <chunguang.xu@xxxxxxxxxx>
---
include/linux/delayacct.h | 74 +++++++++++----------------------------
kernel/delayacct.c | 2 +-
2 files changed, 21 insertions(+), 55 deletions(-)

diff --git a/include/linux/delayacct.h b/include/linux/delayacct.h
index 6639f48dac36..660e534ce7c4 100644
--- a/include/linux/delayacct.h
+++ b/include/linux/delayacct.h
@@ -94,137 +94,103 @@ static inline void delayacct_tsk_init(struct task_struct *tsk)
__delayacct_tsk_init(tsk);
}

+static inline bool delayacct_enabled(void)
+{
+ return static_branch_unlikely(&delayacct_key);
+}
+
/* Free tsk->delays. Called from bad fork and __put_task_struct
* where there's no risk of tsk->delays being accessed elsewhere
*/
static inline void delayacct_tsk_free(struct task_struct *tsk)
{
- if (tsk->delays)
+ if (delayacct_enabled())
kmem_cache_free(delayacct_cache, tsk->delays);
tsk->delays = NULL;
}

static inline void delayacct_blkio_start(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_blkio_start();
}

static inline void delayacct_blkio_end(struct task_struct *p)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (p->delays)
+ if (delayacct_enabled())
__delayacct_blkio_end(p);
}

static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
{
- if (tsk->delays)
+ if (delayacct_enabled())
return __delayacct_blkio_ticks(tsk);
return 0;
}

static inline void delayacct_freepages_start(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_freepages_start();
}

static inline void delayacct_freepages_end(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_freepages_end();
}

static inline void delayacct_thrashing_start(bool *in_thrashing)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_thrashing_start(in_thrashing);
}

static inline void delayacct_thrashing_end(bool *in_thrashing)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_thrashing_end(in_thrashing);
}

static inline void delayacct_swapin_start(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_swapin_start();
}

static inline void delayacct_swapin_end(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_swapin_end();
}

static inline void delayacct_compact_start(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_compact_start();
}

static inline void delayacct_compact_end(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_compact_end();
}

static inline void delayacct_wpcopy_start(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_wpcopy_start();
}

static inline void delayacct_wpcopy_end(void)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (current->delays)
+ if (delayacct_enabled())
__delayacct_wpcopy_end();
}

static inline void delayacct_irq(struct task_struct *task, u32 delta)
{
- if (!static_branch_unlikely(&delayacct_key))
- return;
-
- if (task->delays)
+ if (delayacct_enabled())
__delayacct_irq(task, delta);
}

diff --git a/kernel/delayacct.c b/kernel/delayacct.c
index 6f0c358e73d8..ce10b7c1ee43 100644
--- a/kernel/delayacct.c
+++ b/kernel/delayacct.c
@@ -161,7 +161,7 @@ int delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
d->cpu_run_virtual_total =
(tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp;

- if (!tsk->delays)
+ if (!delayacct_enabled())
return 0;

/* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
--
2.25.1