cifs: fix incorrect handling of smb2_set_sparse() return in smb3_simple_falloc
[linux-2.6-block.git] / fs / xfs / kmem.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769
NS
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4 6#include <linux/mm.h>
5b3cc15a 7#include <linux/sched/mm.h>
1da177e4 8#include <linux/highmem.h>
5a0e3ad6 9#include <linux/slab.h>
1da177e4
LT
10#include <linux/swap.h>
11#include <linux/blkdev.h>
3fcfab16 12#include <linux/backing-dev.h>
1da177e4 13#include "kmem.h"
4f10700a 14#include "xfs_message.h"
1da177e4 15
1da177e4 16void *
77ba7877 17kmem_alloc(size_t size, xfs_km_flags_t flags)
1da177e4 18{
27496a8c
AV
19 int retries = 0;
20 gfp_t lflags = kmem_flags_convert(flags);
21 void *ptr;
1da177e4
LT
22
23 do {
bdfb0430 24 ptr = kmalloc(size, lflags);
1da177e4
LT
25 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
26 return ptr;
27 if (!(++retries % 100))
4f10700a 28 xfs_err(NULL,
847f9f68 29 "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
5bf97b1c 30 current->comm, current->pid,
847f9f68 31 (unsigned int)size, __func__, lflags);
8aa7e847 32 congestion_wait(BLK_RW_ASYNC, HZ/50);
1da177e4
LT
33 } while (1);
34}
35
fdd3ccee 36void *
cb0a8d23 37kmem_alloc_large(size_t size, xfs_km_flags_t flags)
fdd3ccee 38{
9ba1fb2c 39 unsigned nofs_flag = 0;
fdd3ccee 40 void *ptr;
ae687e58 41 gfp_t lflags;
fdd3ccee 42
cb0a8d23 43 ptr = kmem_alloc(size, flags | KM_MAYFAIL);
fdd3ccee
DC
44 if (ptr)
45 return ptr;
ae687e58
DC
46
47 /*
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
9ba1fb2c 51 * context via PF_MEMALLOC_NOFS to prevent memory reclaim re-entering
ae687e58
DC
52 * the filesystem here and potentially deadlocking.
53 */
9ba1fb2c
MH
54 if (flags & KM_NOFS)
55 nofs_flag = memalloc_nofs_save();
ae687e58
DC
56
57 lflags = kmem_flags_convert(flags);
cb0a8d23 58 ptr = __vmalloc(size, lflags, PAGE_KERNEL);
ae687e58 59
9ba1fb2c
MH
60 if (flags & KM_NOFS)
61 memalloc_nofs_restore(nofs_flag);
ae687e58
DC
62
63 return ptr;
fdd3ccee
DC
64}
65
1da177e4 66void *
664b60f6 67kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
1da177e4 68{
664b60f6
CH
69 int retries = 0;
70 gfp_t lflags = kmem_flags_convert(flags);
71 void *ptr;
1da177e4 72
664b60f6
CH
73 do {
74 ptr = krealloc(old, newsize, lflags);
75 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
76 return ptr;
77 if (!(++retries % 100))
78 xfs_err(NULL,
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);
83 } while (1);
1da177e4
LT
84}
85
86void *
77ba7877 87kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
1da177e4 88{
27496a8c
AV
89 int retries = 0;
90 gfp_t lflags = kmem_flags_convert(flags);
91 void *ptr;
1da177e4
LT
92
93 do {
94 ptr = kmem_cache_alloc(zone, lflags);
95 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
96 return ptr;
97 if (!(++retries % 100))
4f10700a 98 xfs_err(NULL,
5bf97b1c
TH
99 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
100 current->comm, current->pid,
101 __func__, lflags);
8aa7e847 102 congestion_wait(BLK_RW_ASYNC, HZ/50);
1da177e4
LT
103 } while (1);
104}