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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/mm.h> |
Ingo Molnar | 5b3cc15 | 2017-02-02 20:43:54 +0100 | [diff] [blame] | 7 | #include <linux/sched/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/swap.h> |
| 11 | #include <linux/blkdev.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 12 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "kmem.h" |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 14 | #include "xfs_message.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | void * |
Al Viro | 77ba787 | 2012-04-02 06:24:04 -0400 | [diff] [blame] | 17 | kmem_alloc(size_t size, xfs_km_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | { |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 19 | int retries = 0; |
| 20 | gfp_t lflags = kmem_flags_convert(flags); |
| 21 | void *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | do { |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 24 | ptr = kmalloc(size, lflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP))) |
| 26 | return ptr; |
| 27 | if (!(++retries % 100)) |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 28 | xfs_err(NULL, |
Eric Sandeen | 847f9f6 | 2015-10-12 16:04:45 +1100 | [diff] [blame] | 29 | "%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] | 30 | current->comm, current->pid, |
Eric Sandeen | 847f9f6 | 2015-10-12 16:04:45 +1100 | [diff] [blame] | 31 | (unsigned int)size, __func__, lflags); |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 32 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } while (1); |
| 34 | } |
| 35 | |
| 36 | void * |
Dave Chinner | cb0a8d2 | 2018-03-06 17:03:28 -0800 | [diff] [blame] | 37 | kmem_alloc_large(size_t size, xfs_km_flags_t flags) |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 38 | { |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 39 | unsigned nofs_flag = 0; |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 40 | void *ptr; |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 41 | gfp_t lflags; |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 42 | |
Dave Chinner | cb0a8d2 | 2018-03-06 17:03:28 -0800 | [diff] [blame] | 43 | ptr = kmem_alloc(size, flags | KM_MAYFAIL); |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 44 | if (ptr) |
| 45 | return ptr; |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 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 |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 51 | * context via PF_MEMALLOC_NOFS to prevent memory reclaim re-entering |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 52 | * the filesystem here and potentially deadlocking. |
| 53 | */ |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 54 | if (flags & KM_NOFS) |
| 55 | nofs_flag = memalloc_nofs_save(); |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 56 | |
| 57 | lflags = kmem_flags_convert(flags); |
Dave Chinner | cb0a8d2 | 2018-03-06 17:03:28 -0800 | [diff] [blame] | 58 | ptr = __vmalloc(size, lflags, PAGE_KERNEL); |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 59 | |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 60 | if (flags & KM_NOFS) |
| 61 | memalloc_nofs_restore(nofs_flag); |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 62 | |
| 63 | return ptr; |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 64 | } |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | void * |
Christoph Hellwig | 664b60f | 2016-04-06 09:47:01 +1000 | [diff] [blame] | 67 | 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] | 68 | { |
Christoph Hellwig | 664b60f | 2016-04-06 09:47:01 +1000 | [diff] [blame] | 69 | int retries = 0; |
| 70 | gfp_t lflags = kmem_flags_convert(flags); |
| 71 | void *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Christoph Hellwig | 664b60f | 2016-04-06 09:47:01 +1000 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void * |
Al Viro | 77ba787 | 2012-04-02 06:24:04 -0400 | [diff] [blame] | 87 | kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 89 | int retries = 0; |
| 90 | gfp_t lflags = kmem_flags_convert(flags); |
| 91 | void *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 98 | xfs_err(NULL, |
Tetsuo Handa | 5bf97b1 | 2015-10-12 15:41:29 +1100 | [diff] [blame] | 99 | "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)", |
| 100 | current->comm, current->pid, |
| 101 | __func__, lflags); |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 102 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } while (1); |
| 104 | } |