使用net_device可能取消对私人数据的引用 [英] Possible de-reference of private data using net_device

查看:82
本文介绍了使用net_device可能取消对私人数据的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在Linux内核中使用net_device模块,我有一个具体问题.
让我们解决这个代码示例 .

I have a specific question regarding using net_device module in linux kernel.
Lets address this code example please.

当我初始化设备时,我会调用alloc_netdev并为其提供私有数据大小,因此它将正确分配它.

When i init my device, i call alloc_netdev and provide it with the private data size , so it will properly allocate it.

现在,当我希望停止使用此设备时,调用snull_cleanup会发生什么.我看到它基本上是免费的结构(包括私有数据).
问题是,如果当前运行的代码是我的设备模块中的内部函数,该函数使用cally netdev_priv()获得的私有数据,保存对私有数据的引用,突然之间我在内核中获得了上下文切换-snull_cleanup函数的空间-释放私有数据.

Now, What happens when i call snull_cleanup when i wish to stop using this device. i see it basically free's the structure (including the private data).
The question is, what if the code currently running is an inside function inside my device module which uses the private data got by cally netdev_priv(), holdes a reference to the private data, and all of the sudden i get a context switch in kernel-space towards the snull_cleanup function - which frees the private data.

然后,当将上下文切换回保存对私有数据的引用的函数时,我是否会获得对释放结构的引用?

Then, when switching context back to the function holding the reference to the private data, wouldnt i get a reference to a freed structure?

如果您能为我解决这个问题,我会很高兴,

I would be glad if you could sort this issue out for me, thanks

推荐答案

我最初的想法是没有意义的:这是您的私人数据,需要您自己处理-您已经分配了它,因此必须释放它.这是私有数据的常见模式-例如,您可以在struct bio中看到它.但是我看到的是一个非常肮脏的骇客.

My initial thought was that it doesn't make sense: it's your private data so deal with by yourself - you have allocated it, so you have to free it. That's a common pattern for private data - you can see it, for example, in struct bio. But what I saw was a really dirty hack.

所以,我查看了alloc_netdev代码,这就是我所发现的.

So, I looked at alloc_netdev code and that's what I've found.

您不分配您的私有结构,您可以将size传递给alloc_netdev.如果将私有数据的大小传递给alloc_netdev(第一个参数),则它将分配大小:

You don't allocate your private struct, you can pass size to alloc_netdev. If you pass size of your private data to alloc_netdev (first argument), then it will make allocation of size:

ALIGN_OF_32( sizeof(struct net_device) + sizeof(struct snull_private) )

因此,您的私人数据是结构net_device的固有部分-它被附加在其后

So your private data is inherent part of struct net_device - it's appended after it

     Whole struct net_device 
       you're working with
+-------------------------------+
| +---------------------------+ |
| |                           | | 
| |                           | |
| | Actual struct net_device  | |
| |                           | |
| |                           | |
| +---------------------------+ |
| | --> Your private data <-- | |
| +---------------------------+ |
| |      Alignment to 32      | |
| +---------------------------+ |
+-------------------------------+

要获取私有数据,您可以使用netdev_priv,它仅将struct net_device的大小添加到指针,从而获得私有数据的起始地址:

To get private data you use netdev_priv that just adds size of struct net_device to pointer thus getting start address of your private data:

static inline void *netdev_priv(const struct net_device *dev)
{
        return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
}

具有free_netdev的释放设备将取消分配包括私有数据在内的整个结构.尽管不会立即完成,但是您将可以在一段时间后访问数据.

Freeing device with free_netdev will deallocate whole structure including private data. Though it will not be done immediately, so you'll be able to access your data some time.

这个棘手的事情将使您的结构缓存友好,从而提高性能.看起来,这种私有数据仅在net_device的生命周期内有意义,因此,如果您希望在销毁net_device之后拥有一些数据,则可以将net_device本身嵌入到您的结构中,这样您的结构将是一个包装器.

This tricky thing will make working with your struct cache friendly, thus boosting your performance. Looks like this private data assumed to make sense only for lifetime of net_device, so if you want to have some data after destroying net_device you can embed net_device itself into your structure, so your structure will be kind of a wrapper.

这篇关于使用net_device可能取消对私人数据的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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