blob: c557a030acfea42d7a48677cfecc227db3539dfa [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"
Dave Chinner4f107002011-03-07 10:00:35 +11007#include "xfs_message.h"
Dave Chinner0ad95682019-08-26 12:08:10 -07008#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010void *
Al Viro77ba7872012-04-02 06:24:04 -040011kmem_alloc(size_t size, xfs_km_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012{
Al Viro27496a82005-10-21 03:20:48 -040013 int retries = 0;
14 gfp_t lflags = kmem_flags_convert(flags);
15 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Dave Chinner0ad95682019-08-26 12:08:10 -070017 trace_kmem_alloc(size, flags, _RET_IP_);
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 do {
Christoph Hellwigbdfb0432010-01-20 21:55:30 +000020 ptr = kmalloc(size, lflags);
Tetsuo Handa707e0dd2019-08-26 12:06:22 -070021 if (ptr || (flags & KM_MAYFAIL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 return ptr;
23 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +110024 xfs_err(NULL,
Eric Sandeen847f9f62015-10-12 16:04:45 +110025 "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
Tetsuo Handa5bf97b12015-10-12 15:41:29 +110026 current->comm, current->pid,
Eric Sandeen847f9f62015-10-12 16:04:45 +110027 (unsigned int)size, __func__, lflags);
NeilBrown40342472022-01-14 14:07:14 -080028 memalloc_retry_wait(lflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 } while (1);
30}