【问题标题】:why truncated param in this Linux kernel code?为什么在这个 Linux 内核代码中截断参数?
【发布时间】:2019-12-21 15:07:01
【问题描述】:

我对 Linux 内核中 kernel/sched/sched.h 中的代码感到困惑。 就像 kernel.org 中的最新版本代码一样。 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/sched/sched.h?h=v5.3-rc4 第 285-299 行。

__dl_update(dl_b, (s32)tsk_bw / cpus);

函数“__dl_update”在第二个参数中需要 s64 类型。 tsk_bw 是 u64 类型。 为什么使用“s32”而不是“s64”???

static inline void __dl_update(struct dl_bw *dl_b, s64 bw);

static inline
void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
{
    dl_b->total_bw -= tsk_bw;
    __dl_update(dl_b, (s32)tsk_bw / cpus);
}

static inline
void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
{
    dl_b->total_bw += tsk_bw;
    __dl_update(dl_b, -((s32)tsk_bw / cpus));
}

static inline
bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
{
    return dl_b->bw != -1 &&
           dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
}

【问题讨论】:

    标签: c linux kernel


    【解决方案1】:

    一个原因可能是 32 位有符号除法需要 26 个周期,而在 Skylake 上,64 位除法需要 42-95 个周期。 64 位有符号除法是最昂贵的除法,因此尽可能避免使用它是有意义的。见Agner Fog's Instruction tables

    【讨论】:

    • 我觉得安全应该更重要,不是吗?
    • @liu 要回答这个问题,需要知道tsk_bwbw 是什么以及它们的有效范围。
    猜你喜欢
    • 2018-07-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 2017-07-07
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    相关资源
    最近更新 更多