blob: fdd9d6ede25ca74065c2b092c742031068270aae [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/mm.h>
Ingo Molnar5b3cc152017-02-02 20:43:54 +01007#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/swap.h>
11#include <linux/blkdev.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070012#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "kmem.h"
Dave Chinner4f107002011-03-07 10:00:35 +110014#include "xfs_message.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016void *
Al Viro77ba7872012-04-02 06:24:04 -040017kmem_alloc(size_t size, xfs_km_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Al Viro27496a82005-10-21 03:20:48 -040019 int retries = 0;
20 gfp_t lflags = kmem_flags_convert(flags);
21 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 do {
Christoph Hellwigbdfb0432010-01-20 21:55:30 +000024 ptr = kmalloc(size, lflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
26 return ptr;
27 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +110028 xfs_err(NULL,
Eric Sandeen847f9f62015-10-12 16:04:45 +110029 "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
Tetsuo Handa5bf97b12015-10-12 15:41:29 +110030 current->comm, current->pid,
Eric Sandeen847f9f62015-10-12 16:04:45 +110031 (unsigned int)size, __func__, lflags);
Jens Axboe8aa7e842009-07-09 14:52:32 +020032 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 } while (1);
34}
35
36void *
Dave Chinnercb0a8d22018-03-06 17:03:28 -080037kmem_alloc_large(size_t size, xfs_km_flags_t flags)
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100038{
Michal Hocko9ba1fb22017-05-03 14:53:19 -070039 unsigned nofs_flag = 0;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100040 void *ptr;
Dave Chinnerae687e52014-03-07 16:19:14 +110041 gfp_t lflags;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100042
Dave Chinnercb0a8d22018-03-06 17:03:28 -080043 ptr = kmem_alloc(size, flags | KM_MAYFAIL);
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100044 if (ptr)
45 return ptr;
Dave Chinnerae687e52014-03-07 16:19:14 +110046
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 Hocko9ba1fb22017-05-03 14:53:19 -070051 * context via PF_MEMALLOC_NOFS to prevent memory reclaim re-entering
Dave Chinnerae687e52014-03-07 16:19:14 +110052 * the filesystem here and potentially deadlocking.
53 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -070054 if (flags & KM_NOFS)
55 nofs_flag = memalloc_nofs_save();
Dave Chinnerae687e52014-03-07 16:19:14 +110056
57 lflags = kmem_flags_convert(flags);
Dave Chinnercb0a8d22018-03-06 17:03:28 -080058 ptr = __vmalloc(size, lflags, PAGE_KERNEL);
Dave Chinnerae687e52014-03-07 16:19:14 +110059
Michal Hocko9ba1fb22017-05-03 14:53:19 -070060 if (flags & KM_NOFS)
61 memalloc_nofs_restore(nofs_flag);
Dave Chinnerae687e52014-03-07 16:19:14 +110062
63 return ptr;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +100064}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066void *
Christoph Hellwig664b60f2016-04-06 09:47:01 +100067kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Christoph Hellwig664b60f2016-04-06 09:47:01 +100069 int retries = 0;
70 gfp_t lflags = kmem_flags_convert(flags);
71 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Christoph Hellwig664b60f2016-04-06 09:47:01 +100073 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 Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86void *
Al Viro77ba7872012-04-02 06:24:04 -040087kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Al Viro27496a82005-10-21 03:20:48 -040089 int retries = 0;
90 gfp_t lflags = kmem_flags_convert(flags);
91 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
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 Chinner4f107002011-03-07 10:00:35 +110098 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +110099 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
100 current->comm, current->pid,
101 __func__, lflags);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200102 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } while (1);
104}