"get_user_pages"如何工作(对于Linux驱动程序) [英] How does "get_user_pages" work (For linux driver)

查看:541
本文介绍了"get_user_pages"如何工作(对于Linux驱动程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用Linux PCI驱动程序,现在我正尝试使用分散/收集功能为DMA编写代码.

Working on a Linux PCI driver, now I'm trying to write codes for DMA using scatter/gather.

到目前为止,我了解到,要直接从用户空间访问DMA数据,我们需要将用户空间页面固定到内核空间.

For now, I've learned that to access to DMA datas directly from User space, we need to pin user space pages to kernel space.

为此,我们有get_user_pages,其完整定义如下:

And to do this, we have get_user_pages, its full definition is like:

int get_user_pages(struct task_struct * tsk,  
    struct mm_struct * mm,  
    unsigned long start,  
    int nr_pages,  
    int write,  
    int force,  
    struct page ** pages,  
    struct vm_area_struct ** vmas);

我的第一个问题是关于struct page ** pages的问题.在调用get_user_pages之前,我们是否需要为pages分配内存(例如,使用kcalloc?)?

My first question is about the struct page ** pages. Here do we need to allocate memory(using kcalloc for ex.) for the pages before calling the get_user_pages?

我的第二个问题是关于unsigned long start的,它在手册页上显示为起始用户地址",这是否意味着,如果我在int *p这样的用户空间中声明了一个指针,则起始用户地址"我应该传递给内核空间的是p?

My second question is about the unsigned long start, on the man page, it says "starting user address", does it mean that, if I declare a pointer in user space like int *p, the "starting user address" I should pass to kernel space is p?

我的第三个问题也是关于unsigned long start的问题,如果我对第二个问题的理解正确,那么我们如何确保该地址恰好在页面的开头?

My third question is also about the unsigned long start, if I understand correctly in the second question, then how can we make sure that this address begins exactly at the beginning of a page?

这三个问题,谢谢你的提出.

So three questions, thanks for advance.

推荐答案

我的第一个问题是关于struct页面**页面.在调用get_user_pages之前,我们是否需要在页面上分配内存(例如,使用kcalloc?)?

My first question is about the struct page ** pages. Here do we need to allocate memory(using kcalloc for ex.) for the pages before calling the get_user_pages?

您可以(但不是强制性的)数组就足够了(数组的大小取决于nr_pages)

You can, but it's not mandatory, an array is sufficent (its size depends on nr_pages)

如果您要固定4页,则struct page *pages[4];就足够了.

If you want to pin 4 pages, struct page *pages[4]; is enough.

我的第二个问题是关于无符号长启动的,在手册页上,它显示起始用户地址",这是否意味着,如果我声明了类似int * p的指针,则应该传递起始用户地址"到内核​​空间是p?

My second question is about the unsigned long start, on the man page, it says "starting user address", does it mean that, if I declare a pointer like int *p, the "starting user address" I should pass to kernel space is p?

此参数应指向用户进程拥有的内存(例如malloc之后).

This parameter should points to memory owned by your user process (like after a malloc).

我的第三个问题也是关于无符号的长距离起步,如果我在第二个问题中理解正确,那么我们如何确保该地址恰好在页面的开头?

My third question is also about the unsigned long start, if I understand correctly in the second question, then how can we make sure that this address begins exactly at the beginning of a page?

我认为您可以使用 getpagesize 函数来做到这一点.

I think you can do it with getpagesize function.

我认为此博客条目:"get_user_pages示例" 可能会有所帮助你.

I think this blog entry: "get_user_pages example" may help you.

这篇关于"get_user_pages"如何工作(对于Linux驱动程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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