blob: bb2beaef531af2310b6fbcc2156ae5fd206dc64c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/swap.h>
22#include <linux/blkdev.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070023#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "kmem.h"
Dave Chinner4f107002011-03-07 10:00:35 +110025#include "xfs_message.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027void *
Al Viro77ba7872012-04-02 06:24:04 -040028kmem_alloc(size_t size, xfs_km_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Al Viro27496a82005-10-21 03:20:48 -040030 int retries = 0;
31 gfp_t lflags = kmem_flags_convert(flags);
32 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 do {
Christoph Hellwigbdfb0432010-01-20 21:55:30 +000035 ptr = kmalloc(size, lflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
37 return ptr;
38 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +110039 xfs_err(NULL,
Eric Sandeen847f9f62015-10-12 16:04:45 +110040 "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
Tetsuo Handa5bf97b12015-10-12 15:41:29 +110041 current->comm, current->pid,
Eric Sandeen847f9f62015-10-12 16:04:45 +110042 (unsigned int)size, __func__, lflags);
Jens Axboe8aa7e842009-07-09 14:52:32 +020043 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 } while (1);
45}
46
47void *
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100048kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
49{
Dave Chinnerae687e52014-03-07 16:19:14 +110050 unsigned noio_flag = 0;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100051 void *ptr;
Dave Chinnerae687e52014-03-07 16:19:14 +110052 gfp_t lflags;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100053
54 ptr = kmem_zalloc(size, flags | KM_MAYFAIL);
55 if (ptr)
56 return ptr;
Dave Chinnerae687e52014-03-07 16:19:14 +110057
58 /*
59 * __vmalloc() will allocate data pages and auxillary structures (e.g.
60 * pagetables) with GFP_KERNEL, yet we may be under GFP_NOFS context
61 * here. Hence we need to tell memory reclaim that we are in such a
62 * context via PF_MEMALLOC_NOIO to prevent memory reclaim re-entering
63 * the filesystem here and potentially deadlocking.
64 */
65 if ((current->flags & PF_FSTRANS) || (flags & KM_NOFS))
66 noio_flag = memalloc_noio_save();
67
68 lflags = kmem_flags_convert(flags);
69 ptr = __vmalloc(size, lflags | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
70
71 if ((current->flags & PF_FSTRANS) || (flags & KM_NOFS))
72 memalloc_noio_restore(noio_flag);
73
74 return ptr;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100075}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077void *
Christoph Hellwig664b60f2016-04-06 09:47:01 +100078kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Christoph Hellwig664b60f2016-04-06 09:47:01 +100080 int retries = 0;
81 gfp_t lflags = kmem_flags_convert(flags);
82 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Christoph Hellwig664b60f2016-04-06 09:47:01 +100084 do {
85 ptr = krealloc(old, newsize, lflags);
86 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
87 return ptr;
88 if (!(++retries % 100))
89 xfs_err(NULL,
90 "%s(%u) possible memory allocation deadlock size %zu in %s (mode:0x%x)",
91 current->comm, current->pid,
92 newsize, __func__, lflags);
93 congestion_wait(BLK_RW_ASYNC, HZ/50);
94 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97void *
Al Viro77ba7872012-04-02 06:24:04 -040098kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Al Viro27496a82005-10-21 03:20:48 -0400100 int retries = 0;
101 gfp_t lflags = kmem_flags_convert(flags);
102 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 do {
105 ptr = kmem_cache_alloc(zone, lflags);
106 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
107 return ptr;
108 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100109 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100110 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
111 current->comm, current->pid,
112 __func__, lflags);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200113 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 } while (1);
115}