[知其然不知其所以然-41] resource allocation in Linux

来源:互联网 发布:dnd战士优化 编辑:程序博客网 时间:2024/06/10 12:28

Linux uses resource tree to manage the io and memory resources for different device
drivers. Resources are manipulated in the tree structure, every resource has one parent
and several brothers, AKA siblings. Each time when driver wants to allocate a  new resource
from this resource tree, the new resource will be compared from top down, try to find
an existing parent resource tree node which is the superset of the new set, if yes, then check the children
of this parent to find the first overlap child, then set this child as the new parent, and so on.
until there is no overlap of the new resource under the parent P, we link the new resource under the
P, and returns OK.

That is to say, only the conflict resource is the super set of new resource, or vice versa, conflict
resource is the subset of the new resource, everything is OK,  the only  failure case
is  when the overlapping does not satisfy the condition that the new resource partially overlaps with
P, then it failed to allocate new resource.


According to kernel/resource.c, the core functions here are actually __request_resource and __insert_resource,
the formal is the base of resource management, which was written by Linus many years ago.
So the formal just check if the given new resource is conflict with any existing resources allocated
under the resource tree,  if there is, return the first conflict resource otherwise return NULL, For the latter,
__insert_resource is based on __request_resource, it tries to leverage __request_resource recursively
to find the suitable position to insert the new resource. If new resource is a subset of the P, then loop until
find a hole under P, and done with it. If the P is not a superset of new resource, then check if they
are partially overlapped, if it is , then failed and return. If not, then P must be a subset of new resource, then
new resource should take charge of all P's children, and P become the child of new resource , and also all
P's brothers(siblings) should be the brother of P now.  

For example, you can refer to /proc/iomem for a more vivid:

8b800000-dfffffff : PCI Bus 0000:00  8b800000-8b81ffff : pnp 00:08  90000000-9fffffff : 0000:00:02.0  a0000000-a0ffffff : 0000:00:02.0  a1000000-a10fffff : PCI Bus 0000:02    a1000000-a10007ff : 0000:02:00.0    a1001000-a1001fff : 0000:02:00.0      a1001000-a1001fff : mmc0
pnp 00:08 , 0000:00:02.0 and PCI Bus 0000:02 are siblings, and mmc0 is the child of 0000:02:00.0.




0 0
原创粉丝点击