如何防止 MMAP 缓存值? [英] How would one prevent MMAP from caching values?

查看:12
本文介绍了如何防止 MMAP 缓存值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 linux 驱动程序,ioremaps 将特定设备的 PCI BAR0 导出到 sysfs 二进制属性,允许用户空间直接控制它.

I've written a linux driver that ioremaps exports PCI BAR0 for a particular device to a sysfs binary attribute allowing userspace to directly control it.

当我尝试在属性之上进行 MMAP 以直接访问该位内存(从用户态程序)时,问题就出现了.读取成功并返回预期值,尽管当我写入该内存时,它似乎缓存在内核和内存之间的某处,并且没有传递到 GMCH 根联合体(因此也传递给设备).我想做的是在每次访问后都有一个隐式的写内存屏障.

The problem rears when I attempt to MMAP on top of the attribute to directly access that bit of memory (from a userland program). Reads succeed just fine and return expected values, though when I write to that memory it appears to be cached somewhere between the kernel and memory and not delivered to the GMCH root complex (and therefore the device). What I'd like to do is have an implicit write memory barrier after each access.

  • 有什么方法可以防止内核缓存写入到 mmap 的内存位?

跟进:

  • 是否在每次访问后调用 msync() 以接受"的方式执行此操作?
  • Is calling msync() after every access the "accepted" way to do this?

推荐答案

我自己用我的解决方案来回答这个问题.

Going to go ahead and answer this one myself with my solution.

在我的 sysfs mmap 函数的内核驱动程序中,/include/asm/pgtable.h 中有一个宏,它为 nocache 的 pfn 重映射设置正确的标志.它看起来像这样:

In the Kernel driver from my sysfs mmap function, there is a macro in /include/asm/pgtable.h that sets the proper flags for a nocache'd pfn remap. It looks like this:

vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
               vma->vm_end - vma->vm_start,
               vma->vm_page_prot))
    return -EAGAIN;

此外,在用户空间 mmap 中,我在 mmap 标志参数中使用了 MAP_SHARED 标志.

Additionally, in the userland mmap, I used the MAP_SHARED flag in the mmap flags argument.

两者的结合最终成功了.

The combination of the two ultimately did the trick.

这篇关于如何防止 MMAP 缓存值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆