1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
7 #include <linux/sched/mm.h>
8 #include <linux/highmem.h>
9 #include <linux/slab.h>
10 #include <linux/swap.h>
11 #include <linux/blkdev.h>
12 #include <linux/backing-dev.h>
14 #include "xfs_message.h"
17 kmem_alloc(size_t size
, xfs_km_flags_t flags
)
20 gfp_t lflags
= kmem_flags_convert(flags
);
24 ptr
= kmalloc(size
, lflags
);
25 if (ptr
|| (flags
& (KM_MAYFAIL
|KM_NOSLEEP
)))
27 if (!(++retries
% 100))
29 "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
30 current
->comm
, current
->pid
,
31 (unsigned int)size
, __func__
, lflags
);
32 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
37 kmem_alloc_large(size_t size
, xfs_km_flags_t flags
)
39 unsigned nofs_flag
= 0;
43 ptr
= kmem_alloc(size
, flags
| KM_MAYFAIL
);
48 * __vmalloc() will allocate data pages and auxillary structures (e.g.
49 * pagetables) with GFP_KERNEL, yet we may be under GFP_NOFS context
50 * here. Hence we need to tell memory reclaim that we are in such a
51 * context via PF_MEMALLOC_NOFS to prevent memory reclaim re-entering
52 * the filesystem here and potentially deadlocking.
55 nofs_flag
= memalloc_nofs_save();
57 lflags
= kmem_flags_convert(flags
);
58 ptr
= __vmalloc(size
, lflags
, PAGE_KERNEL
);
61 memalloc_nofs_restore(nofs_flag
);
67 kmem_realloc(const void *old
, size_t newsize
, xfs_km_flags_t flags
)
70 gfp_t lflags
= kmem_flags_convert(flags
);
74 ptr
= krealloc(old
, newsize
, lflags
);
75 if (ptr
|| (flags
& (KM_MAYFAIL
|KM_NOSLEEP
)))
77 if (!(++retries
% 100))
79 "%s(%u) possible memory allocation deadlock size %zu in %s (mode:0x%x)",
80 current
->comm
, current
->pid
,
81 newsize
, __func__
, lflags
);
82 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
87 kmem_zone_alloc(kmem_zone_t
*zone
, xfs_km_flags_t flags
)
90 gfp_t lflags
= kmem_flags_convert(flags
);
94 ptr
= kmem_cache_alloc(zone
, lflags
);
95 if (ptr
|| (flags
& (KM_MAYFAIL
|KM_NOSLEEP
)))
97 if (!(++retries
% 100))
99 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
100 current
->comm
, current
->pid
,
102 congestion_wait(BLK_RW_ASYNC
, HZ
/50);