blob: e986b95d94c9b52821b1ddbd9eb279b9c1e6f3e9 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Dave Chinner0ad95682019-08-26 12:08:10 -07006#include "xfs.h"
Andrew Morton3fcfab12006-10-19 23:28:16 -07007#include <linux/backing-dev.h>
Dave Chinner4f107002011-03-07 10:00:35 +11008#include "xfs_message.h"
Dave Chinner0ad95682019-08-26 12:08:10 -07009#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011void *
Al Viro77ba7872012-04-02 06:24:04 -040012kmem_alloc(size_t size, xfs_km_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013{
Al Viro27496a82005-10-21 03:20:48 -040014 int retries = 0;
15 gfp_t lflags = kmem_flags_convert(flags);
16 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Dave Chinner0ad95682019-08-26 12:08:10 -070018 trace_kmem_alloc(size, flags, _RET_IP_);
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 do {
Christoph Hellwigbdfb0432010-01-20 21:55:30 +000021 ptr = kmalloc(size, lflags);
Tetsuo Handa707e0dd2019-08-26 12:06:22 -070022 if (ptr || (flags & KM_MAYFAIL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 return ptr;
24 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +110025 xfs_err(NULL,
Eric Sandeen847f9f62015-10-12 16:04:45 +110026 "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
Tetsuo Handa5bf97b12015-10-12 15:41:29 +110027 current->comm, current->pid,
Eric Sandeen847f9f62015-10-12 16:04:45 +110028 (unsigned int)size, __func__, lflags);
Jens Axboe8aa7e842009-07-09 14:52:32 +020029 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 } while (1);
31}
32
Dave Chinnerf8f9ee42019-08-26 12:08:39 -070033
34/*
Joe Perchescf085a12019-11-07 13:24:52 -080035 * __vmalloc() will allocate data pages and auxiliary structures (e.g.
Dave Chinnerf8f9ee42019-08-26 12:08:39 -070036 * pagetables) with GFP_KERNEL, yet we may be under GFP_NOFS context here. Hence
37 * we need to tell memory reclaim that we are in such a context via
38 * PF_MEMALLOC_NOFS to prevent memory reclaim re-entering the filesystem here
39 * and potentially deadlocking.
40 */
41static void *
42__kmem_vmalloc(size_t size, xfs_km_flags_t flags)
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100043{
Michal Hocko9ba1fb22017-05-03 14:53:19 -070044 unsigned nofs_flag = 0;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100045 void *ptr;
Dave Chinnerf8f9ee42019-08-26 12:08:39 -070046 gfp_t lflags = kmem_flags_convert(flags);
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100047
Michal Hocko9ba1fb22017-05-03 14:53:19 -070048 if (flags & KM_NOFS)
49 nofs_flag = memalloc_nofs_save();
Dave Chinnerae687e52014-03-07 16:19:14 +110050
Christoph Hellwig88dca4c2020-06-01 21:51:40 -070051 ptr = __vmalloc(size, lflags);
Dave Chinnerae687e52014-03-07 16:19:14 +110052
Michal Hocko9ba1fb22017-05-03 14:53:19 -070053 if (flags & KM_NOFS)
54 memalloc_nofs_restore(nofs_flag);
Dave Chinnerae687e52014-03-07 16:19:14 +110055
56 return ptr;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100057}
58
Dave Chinnerf8f9ee42019-08-26 12:08:39 -070059/*
60 * Same as kmem_alloc_large, except we guarantee the buffer returned is aligned
61 * to the @align_mask. We only guarantee alignment up to page size, we'll clamp
62 * alignment at page size if it is larger. vmalloc always returns a PAGE_SIZE
63 * aligned region.
64 */
65void *
66kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags)
67{
68 void *ptr;
69
70 trace_kmem_alloc_io(size, flags, _RET_IP_);
71
72 if (WARN_ON_ONCE(align_mask >= PAGE_SIZE))
73 align_mask = PAGE_SIZE - 1;
74
75 ptr = kmem_alloc(size, flags | KM_MAYFAIL);
76 if (ptr) {
77 if (!((uintptr_t)ptr & align_mask))
78 return ptr;
79 kfree(ptr);
80 }
81 return __kmem_vmalloc(size, flags);
82}
83
84void *
85kmem_alloc_large(size_t size, xfs_km_flags_t flags)
86{
87 void *ptr;
88
89 trace_kmem_alloc_large(size, flags, _RET_IP_);
90
91 ptr = kmem_alloc(size, flags | KM_MAYFAIL);
92 if (ptr)
93 return ptr;
94 return __kmem_vmalloc(size, flags);
95}