Memory management “和”之间有什么区别__GFP“不失败”;及__GFP“重复”吗;?

Memory management “和”之间有什么区别__GFP“不失败”;及__GFP“重复”吗;?,memory-management,linux-kernel,kernel,Memory Management,Linux Kernel,Kernel,根据文件(), 上面说: Flag Description __GFP_REPEAT The kernel repeats the allocation if it fails. __GFP_NOFAIL The kernel can repeat the allocation. 因此,它们都可能导致内核重复分配操作。 我怎样才能在两者之间做出选择? 主要的区别是什么?这不是真正的“文档”,只是一篇关于LinuxJournal的文章。诚然,作者(罗伯特·洛夫)对这一

根据文件(), 上面说:

Flag           Description
__GFP_REPEAT   The kernel repeats the allocation if it fails.
__GFP_NOFAIL   The kernel can repeat the allocation.
因此,它们都可能导致内核重复分配操作。 我怎样才能在两者之间做出选择? 主要的区别是什么?

这不是真正的“文档”,只是一篇关于LinuxJournal的文章。诚然,作者(罗伯特·洛夫)对这一主题肯定很了解,但尽管如此,这些描述还是相当不精确和过时的(这篇文章是2003年的)

内核版本4.13(请参阅)中的
\uuuugfp\u REPEAT
标志被重命名为
\uuugfp\u RETRY\u MAYFAIL
,其语义也被修改

\u GFP\u REPEAT
的原意是(来自):

此标志的名称和语义有些不清楚,新的
\uuuuu GFP\u RETRY\u maybail
标志的名称和描述更为清晰(来自:

根据
\uuuu GFP\u NOFAIL
,您可以找到详细说明:


简而言之,
\uuuuugfp\u RETRY\u maybail
\uuuugfp\u NOFAIL
之间的区别在于前者在最终报告失败之前只会尝试分配有限的内存,而后者将无限期地继续尝试,直到内存可用,并且永远不会向调用者报告失败,因为它假定调用者无法处理分配失败

不用说,
\u GFP\u NOFAIL
标志只能在其他选项不可行的情况下小心使用。它很有用,因为它可以避免在请求成功之前在循环中显式调用分配器(例如,
,而(!kmalloc(…);
),因此更高效

__GFP_REPEAT: Try hard to allocate the memory, but the allocation attempt
  _might_ fail.  This depends upon the particular VM implementation.
%__GFP_RETRY_MAYFAIL: The VM implementation will retry memory reclaim
procedures that have previously failed if there is some indication
that progress has been made else where.  It can wait for other
tasks to attempt high level approaches to freeing memory such as
compaction (which removes fragmentation) and page-out.
There is still a definite limit to the number of retries, but it is
a larger limit than with %__GFP_NORETRY.
Allocations with this flag may fail, but only when there is
genuinely little unused memory. While these allocations do not
directly trigger the OOM killer, their failure indicates that
the system is likely to need to use the OOM killer soon.  The
caller must handle failure, but can reasonably do so by failing
a higher-level request, or completing it only in a much less
efficient manner.
If the allocation does fail, and the caller is in a position to
free some non-essential memory, doing so could benefit the system
as a whole.
%__GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
cannot handle allocation failures. The allocation could block
indefinitely but will never return with failure. Testing for
failure is pointless.
New users should be evaluated carefully (and the flag should be
used only when there is no reasonable failure policy) but it is
definitely preferable to use the flag rather than opencode endless
loop around allocator.
Using this flag for costly allocations is _highly_ discouraged.