kmem.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/sched/mm.h>
  8. #include <linux/highmem.h>
  9. #include <linux/slab.h>
  10. #include <linux/swap.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/backing-dev.h>
  13. #include "kmem.h"
  14. #include "xfs_message.h"
  15. void *
  16. kmem_alloc(size_t size, xfs_km_flags_t flags)
  17. {
  18. int retries = 0;
  19. gfp_t lflags = kmem_flags_convert(flags);
  20. void *ptr;
  21. do {
  22. ptr = kmalloc(size, lflags);
  23. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  24. return ptr;
  25. if (!(++retries % 100))
  26. xfs_err(NULL,
  27. "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
  28. current->comm, current->pid,
  29. (unsigned int)size, __func__, lflags);
  30. congestion_wait(BLK_RW_ASYNC, HZ/50);
  31. } while (1);
  32. }
  33. void *
  34. kmem_alloc_large(size_t size, xfs_km_flags_t flags)
  35. {
  36. unsigned nofs_flag = 0;
  37. void *ptr;
  38. gfp_t lflags;
  39. ptr = kmem_alloc(size, flags | KM_MAYFAIL);
  40. if (ptr)
  41. return ptr;
  42. /*
  43. * __vmalloc() will allocate data pages and auxillary structures (e.g.
  44. * pagetables) with GFP_KERNEL, yet we may be under GFP_NOFS context
  45. * here. Hence we need to tell memory reclaim that we are in such a
  46. * context via PF_MEMALLOC_NOFS to prevent memory reclaim re-entering
  47. * the filesystem here and potentially deadlocking.
  48. */
  49. if (flags & KM_NOFS)
  50. nofs_flag = memalloc_nofs_save();
  51. lflags = kmem_flags_convert(flags);
  52. ptr = __vmalloc(size, lflags, PAGE_KERNEL);
  53. if (flags & KM_NOFS)
  54. memalloc_nofs_restore(nofs_flag);
  55. return ptr;
  56. }
  57. void *
  58. kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
  59. {
  60. int retries = 0;
  61. gfp_t lflags = kmem_flags_convert(flags);
  62. void *ptr;
  63. do {
  64. ptr = krealloc(old, newsize, lflags);
  65. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  66. return ptr;
  67. if (!(++retries % 100))
  68. xfs_err(NULL,
  69. "%s(%u) possible memory allocation deadlock size %zu in %s (mode:0x%x)",
  70. current->comm, current->pid,
  71. newsize, __func__, lflags);
  72. congestion_wait(BLK_RW_ASYNC, HZ/50);
  73. } while (1);
  74. }
  75. void *
  76. kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
  77. {
  78. int retries = 0;
  79. gfp_t lflags = kmem_flags_convert(flags);
  80. void *ptr;
  81. do {
  82. ptr = kmem_cache_alloc(zone, lflags);
  83. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  84. return ptr;
  85. if (!(++retries % 100))
  86. xfs_err(NULL,
  87. "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
  88. current->comm, current->pid,
  89. __func__, lflags);
  90. congestion_wait(BLK_RW_ASYNC, HZ/50);
  91. } while (1);
  92. }