Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Dave Chinner | 0ad9568 | 2019-08-26 12:08:10 -0700 | [diff] [blame^] | 6 | #include "xfs.h" |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 7 | #include <linux/backing-dev.h> |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 8 | #include "xfs_message.h" |
Dave Chinner | 0ad9568 | 2019-08-26 12:08:10 -0700 | [diff] [blame^] | 9 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | void * |
Al Viro | 77ba787 | 2012-04-02 06:24:04 -0400 | [diff] [blame] | 12 | kmem_alloc(size_t size, xfs_km_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | { |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 14 | int retries = 0; |
| 15 | gfp_t lflags = kmem_flags_convert(flags); |
| 16 | void *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Dave Chinner | 0ad9568 | 2019-08-26 12:08:10 -0700 | [diff] [blame^] | 18 | trace_kmem_alloc(size, flags, _RET_IP_); |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | do { |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 21 | ptr = kmalloc(size, lflags); |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 22 | if (ptr || (flags & KM_MAYFAIL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | return ptr; |
| 24 | if (!(++retries % 100)) |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 25 | xfs_err(NULL, |
Eric Sandeen | 847f9f6 | 2015-10-12 16:04:45 +1100 | [diff] [blame] | 26 | "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)", |
Tetsuo Handa | 5bf97b1 | 2015-10-12 15:41:29 +1100 | [diff] [blame] | 27 | current->comm, current->pid, |
Eric Sandeen | 847f9f6 | 2015-10-12 16:04:45 +1100 | [diff] [blame] | 28 | (unsigned int)size, __func__, lflags); |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 29 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } while (1); |
| 31 | } |
| 32 | |
| 33 | void * |
Dave Chinner | cb0a8d2 | 2018-03-06 17:03:28 -0800 | [diff] [blame] | 34 | kmem_alloc_large(size_t size, xfs_km_flags_t flags) |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 35 | { |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 36 | unsigned nofs_flag = 0; |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 37 | void *ptr; |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 38 | gfp_t lflags; |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 39 | |
Dave Chinner | 0ad9568 | 2019-08-26 12:08:10 -0700 | [diff] [blame^] | 40 | trace_kmem_alloc_large(size, flags, _RET_IP_); |
| 41 | |
Dave Chinner | cb0a8d2 | 2018-03-06 17:03:28 -0800 | [diff] [blame] | 42 | ptr = kmem_alloc(size, flags | KM_MAYFAIL); |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 43 | if (ptr) |
| 44 | return ptr; |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * __vmalloc() will allocate data pages and auxillary structures (e.g. |
| 48 | * pagetables) with GFP_KERNEL, yet we may be under GFP_NOFS context |
| 49 | * here. Hence we need to tell memory reclaim that we are in such a |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 50 | * context via PF_MEMALLOC_NOFS to prevent memory reclaim re-entering |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 51 | * the filesystem here and potentially deadlocking. |
| 52 | */ |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 53 | if (flags & KM_NOFS) |
| 54 | nofs_flag = memalloc_nofs_save(); |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 55 | |
| 56 | lflags = kmem_flags_convert(flags); |
Dave Chinner | cb0a8d2 | 2018-03-06 17:03:28 -0800 | [diff] [blame] | 57 | ptr = __vmalloc(size, lflags, PAGE_KERNEL); |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 58 | |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 59 | if (flags & KM_NOFS) |
| 60 | memalloc_nofs_restore(nofs_flag); |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 61 | |
| 62 | return ptr; |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 63 | } |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | void * |
Christoph Hellwig | 664b60f | 2016-04-06 09:47:01 +1000 | [diff] [blame] | 66 | kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Christoph Hellwig | 664b60f | 2016-04-06 09:47:01 +1000 | [diff] [blame] | 68 | int retries = 0; |
| 69 | gfp_t lflags = kmem_flags_convert(flags); |
| 70 | void *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Dave Chinner | 0ad9568 | 2019-08-26 12:08:10 -0700 | [diff] [blame^] | 72 | trace_kmem_realloc(newsize, flags, _RET_IP_); |
| 73 | |
Christoph Hellwig | 664b60f | 2016-04-06 09:47:01 +1000 | [diff] [blame] | 74 | do { |
| 75 | ptr = krealloc(old, newsize, lflags); |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 76 | if (ptr || (flags & KM_MAYFAIL)) |
Christoph Hellwig | 664b60f | 2016-04-06 09:47:01 +1000 | [diff] [blame] | 77 | return ptr; |
| 78 | if (!(++retries % 100)) |
| 79 | xfs_err(NULL, |
| 80 | "%s(%u) possible memory allocation deadlock size %zu in %s (mode:0x%x)", |
| 81 | current->comm, current->pid, |
| 82 | newsize, __func__, lflags); |
| 83 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 84 | } while (1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void * |
Al Viro | 77ba787 | 2012-04-02 06:24:04 -0400 | [diff] [blame] | 88 | kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 90 | int retries = 0; |
| 91 | gfp_t lflags = kmem_flags_convert(flags); |
| 92 | void *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Dave Chinner | 0ad9568 | 2019-08-26 12:08:10 -0700 | [diff] [blame^] | 94 | trace_kmem_zone_alloc(kmem_cache_size(zone), flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | do { |
| 96 | ptr = kmem_cache_alloc(zone, lflags); |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 97 | if (ptr || (flags & KM_MAYFAIL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return ptr; |
| 99 | if (!(++retries % 100)) |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 100 | xfs_err(NULL, |
Tetsuo Handa | 5bf97b1 | 2015-10-12 15:41:29 +1100 | [diff] [blame] | 101 | "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)", |
| 102 | current->comm, current->pid, |
| 103 | __func__, lflags); |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 104 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } while (1); |
| 106 | } |