2018-06-06 10:42:14 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2005-11-02 11:58:39 +08:00
|
|
|
* Copyright (c) 2000-2005 Silicon Graphics, Inc.
|
|
|
|
* All Rights Reserved.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2019-08-27 03:08:10 +08:00
|
|
|
#include "xfs.h"
|
2011-03-07 07:00:35 +08:00
|
|
|
#include "xfs_message.h"
|
2019-08-27 03:08:10 +08:00
|
|
|
#include "xfs_trace.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
void *
|
2012-04-02 18:24:04 +08:00
|
|
|
kmem_alloc(size_t size, xfs_km_flags_t flags)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-10-21 15:20:48 +08:00
|
|
|
int retries = 0;
|
|
|
|
gfp_t lflags = kmem_flags_convert(flags);
|
|
|
|
void *ptr;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-27 03:08:10 +08:00
|
|
|
trace_kmem_alloc(size, flags, _RET_IP_);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
do {
|
2010-01-21 05:55:30 +08:00
|
|
|
ptr = kmalloc(size, lflags);
|
2019-08-27 03:06:22 +08:00
|
|
|
if (ptr || (flags & KM_MAYFAIL))
|
2005-04-17 06:20:36 +08:00
|
|
|
return ptr;
|
|
|
|
if (!(++retries % 100))
|
2011-03-07 07:00:35 +08:00
|
|
|
xfs_err(NULL,
|
2015-10-12 13:04:45 +08:00
|
|
|
"%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
|
2015-10-12 12:41:29 +08:00
|
|
|
current->comm, current->pid,
|
2015-10-12 13:04:45 +08:00
|
|
|
(unsigned int)size, __func__, lflags);
|
mm: introduce memalloc_retry_wait()
Various places in the kernel - largely in filesystems - respond to a
memory allocation failure by looping around and re-trying. Some of
these cannot conveniently use __GFP_NOFAIL, for reasons such as:
- a GFP_ATOMIC allocation, which __GFP_NOFAIL doesn't work on
- a need to check for the process being signalled between failures
- the possibility that other recovery actions could be performed
- the allocation is quite deep in support code, and passing down an
extra flag to say if __GFP_NOFAIL is wanted would be clumsy.
Many of these currently use congestion_wait() which (in almost all
cases) simply waits the given timeout - congestion isn't tracked for
most devices.
It isn't clear what the best delay is for loops, but it is clear that
the various filesystems shouldn't be responsible for choosing a timeout.
This patch introduces memalloc_retry_wait() with takes on that
responsibility. Code that wants to retry a memory allocation can call
this function passing the GFP flags that were used. It will wait
however is appropriate.
For now, it only considers __GFP_NORETRY and whatever
gfpflags_allow_blocking() tests. If blocking is allowed without
__GFP_NORETRY, then alloc_page either made some reclaim progress, or
waited for a while, before failing. So there is no need for much
further waiting. memalloc_retry_wait() will wait until the current
jiffie ends. If this condition is not met, then alloc_page() won't have
waited much if at all. In that case memalloc_retry_wait() waits about
200ms. This is the delay that most current loops uses.
linux/sched/mm.h needs to be included in some files now,
but linux/backing-dev.h does not.
Link: https://lkml.kernel.org/r/163754371968.13692.1277530886009912421@noble.neil.brown.name
Signed-off-by: NeilBrown <neilb@suse.de>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Cc: Chao Yu <chao@kernel.org>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2022-01-15 06:07:14 +08:00
|
|
|
memalloc_retry_wait(lflags);
|
2005-04-17 06:20:36 +08:00
|
|
|
} while (1);
|
|
|
|
}
|