kmem.c 3.1 KB

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