无阻塞分配sk_buff的机制

问题描述 投票:0回答:1

我有一个关于 Rx 路径上的

skb
分配的问题。如果我理解的话,skb 是由在中断上下文中运行的软中断或 ksoftirqd 分配的,然后再通过
netif_receive_skb
向上传递到网络堆栈。有人可以解释内核如何确保此分配不会阻塞的底层机制(假设软中断处理是不可抢占的)吗?

linux-device-driver
1个回答
0
投票

有人可以解释内核如何确保此分配不会阻塞的底层机制......吗?

参考net/core/skbuff.c中__alloc_skb()的说明

/*  Allocate a new skbuff. We do this ourselves so we can fill in a few
 *  'private' fields and also do memory statistics to find all the
 *  [BEEP] leaks.
 *
 */

/**
 *  __alloc_skb -   allocate a network buffer
 *  @size: size to allocate
 *  @gfp_mask: allocation mask
 *  @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
 *      instead of head cache and allocate a cloned (child) skb.
 *      If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
 *      allocations in case the data is required for writeback
 *  @node: numa node to allocate memory on
 *
 *  Allocate a new &sk_buff. The returned buffer has no headroom and a
 *  tail room of at least size bytes. The object has a reference count
 *  of one. The return is the buffer. On a failure the return is %NULL.
 *
 *  Buffers may only be allocated from interrupts using a @gfp_mask of
 *  %GFP_ATOMIC.
 */

GFP_ATOMIC 标志被传递到内存分配器以防止休眠/阻塞。
“GFP”代表“获取免费页面”。
请参阅 SO 帖子 GFP_ATOMIC 如何阻止睡眠


研究 Linux 内核中 netdev_alloc_skb() 的调用者如何处理 NULL 返回代码,这意味着没有可用内存可用于新的 skbuff。典型的以太网 MAC 驱动程序会递增

rx_dropped
统计计数器并丢弃接收到的帧。

© www.soinside.com 2019 - 2024. All rights reserved.