本文介绍了如何执行"invd"操作操作说明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将缓存用作临时内存.在使用缓存后,我不想存储任何已修改的缓存行.我知道我可以通过运行 invd 指令来实现这一目标.因为与 wbinvd 不同, invd 使处理器的内部缓存无效(刷新),而不将其存储到主存储器中.

I'm trying to use cache as a temporary memory. And after using the cache I do not want to store any of the modified cache lines. I came to know that I can achieve that by running invd instruction. Because unlike wbinvd, invd Invalidates (flushes) the processor's internal caches without storing them into the main memory.

我写了一个内核模块来检查我是否可以执行 invd 指令.

I wrote a kernel module to check if I can execute the invd instruction.

#include <linux/module.h>     /* Needed by all modules */
#include <linux/kernel.h>     /* Needed for KERN_INFO */
#include <linux/init.h>       /* Needed for the macros */

int new_invd(void){
    asm volatile ("invd" : : : "memory");
    return 1;
}

static int __init hello_start(void)
{
    printk(KERN_INFO "Loading hello module...\n");

    //check if invd instruction executes
    printk(KERN_INFO "running invd\n", new_invd());

    return 0;
}

static void __exit hello_end(void)
{
    printk(KERN_INFO "Goodbye\n");
}

module_init(hello_start);
module_exit(hello_end);

编译并插入模块后,我得到 Segmentation fault(核心已转储) dmesg 显示,

Upon compiling and inserting the module I get Segmentation fault (core dumped) and dmesg shows,

.现在,我想我得到了错误,因为执行 invd 违反了主内存和缓存的一致性,如此处 by @Gunther Piez.但是,我不确定是否是这种情况.

I use asm volatile ("invd" : : : "memory"); as mentioned in chromium. Now I think I'm getting the error because executing invd violates the coherency of main memory and cache as pointing out here How can I do a CPU cache flush in x86 Windows? by @Gunther Piez. However, I'm not sure if this is the case.

那么,为什么我得到此 segfault 会有帮助吗?如果这是由于高速缓存核心冲突而导致的,我该如何解决?如果没有,如何执行 invd ?

So, any help why I'm getting this segfault? If this is due to the violating cache corehency, how can I fix that? If not, how can I execute invd?

我正在使用 Linux xxx 4.4.0-200-generic#232-Ubuntu SMP Wed Jan 13 10:18:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

cat/proc/cpuinfo 显示,

vendor_id   : GenuineIntel
cpu family  : 6
model       : 158
model name  : Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz

推荐答案

在Linux启动后,您无法安全地执行invd,尤其是在其他内核可能正在做任何事情的SMP系统中(例如,有一些脏的缓存行).丢弃所有最近的商店显然会在任何地方破坏事物,就像宇宙射线将缓存或RAM中的一堆比特翻转一样.

You can't safely execute invd after Linux boots, especially not in an SMP system where other cores might be doing anything (i.e. have some dirty cache lines). Discarding all recent stores is obviously going to break stuff everywhere, just like if cosmic rays flipped a bunch of bits in cache or RAM.

如果您确实想通过运行 invd 破坏系统,则您的内联asm语句是正确的.

Your inline asm statement is correct if you do want to corrupt your system by running invd.

不支持丢弃特定缓冲区的内容,无论如何我认为 invd 很慢.再加上使所有其他内核处于睡眠状态的成本(在深度睡眠刷新专用缓存的CPU型号上),以及在运行此温度之前,请确保以某种方式确保甚至同步了L3缓存,例如 wbinvd (也非常慢).缓冲内容+ invd (禁用了中断,其他内核一直处于睡眠状态).

There is no support for discarding contents of a specific buffer, and invd is I think slow anyway. Plus the cost of sleeping all other cores (on CPU models where deep sleep flushes private caches) and somehow making sure even L3 cache is synced, like maybe wbinvd (also extremely slow) before your run this temp-buffer thing + invd (with interrupts disabled and other cores asleep the whole time).

几乎可以肯定的是,让硬件最终写回缓冲区对于性能而言要好得多.请将其保持较小并重复使用,也许在两次使用之间不会被写回.

It's almost certainly much better for performance to just let the HW write back your buffer eventually. Keep it small and reuse it and maybe it won't be written back between uses.

我认为没有任何方法可以安全地使用 invd 来赢得任何性能.即使您只关心单核性能,也可能不会.(NT商店和/或 clflush 可能会使某些商店在 invd 之前进入DRAM,因此使用它似乎不太合理,但是它可能太昂贵了,以至于根本不值得它.

I don't think there's any way to safely use invd that could be a performance win for anything. Probably not even if you only care about single-core performance. (NT stores and/or clflush could make some stores go to DRAM before an invd so it's barely plausible to use it, but it's probably so expensive that it's not at all worth it.)

这篇关于如何执行"invd"操作操作说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 11:50