kmem.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/highmem.h>
  20. #include <linux/slab.h>
  21. #include <linux/swap.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/backing-dev.h>
  24. #include "kmem.h"
  25. #include "xfs_message.h"
  26. /*
  27. * Greedy allocation. May fail and may return vmalloced memory.
  28. */
  29. void *
  30. kmem_zalloc_greedy(size_t *size, size_t minsize, size_t maxsize)
  31. {
  32. void *ptr;
  33. size_t kmsize = maxsize;
  34. while (!(ptr = vzalloc(kmsize))) {
  35. if ((kmsize >>= 1) <= minsize)
  36. kmsize = minsize;
  37. }
  38. if (ptr)
  39. *size = kmsize;
  40. return ptr;
  41. }
  42. void *
  43. kmem_alloc(size_t size, xfs_km_flags_t flags)
  44. {
  45. int retries = 0;
  46. gfp_t lflags = kmem_flags_convert(flags);
  47. void *ptr;
  48. do {
  49. ptr = kmalloc(size, lflags);
  50. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  51. return ptr;
  52. if (!(++retries % 100))
  53. xfs_err(NULL,
  54. "%s(%u) possible memory allocation deadlock size %u in %s (mode:0x%x)",
  55. current->comm, current->pid,
  56. (unsigned int)size, __func__, lflags);
  57. congestion_wait(BLK_RW_ASYNC, HZ/50);
  58. } while (1);
  59. }
  60. void *
  61. kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
  62. {
  63. unsigned noio_flag = 0;
  64. void *ptr;
  65. gfp_t lflags;
  66. ptr = kmem_zalloc(size, flags | KM_MAYFAIL);
  67. if (ptr)
  68. return ptr;
  69. /*
  70. * __vmalloc() will allocate data pages and auxillary structures (e.g.
  71. * pagetables) with GFP_KERNEL, yet we may be under GFP_NOFS context
  72. * here. Hence we need to tell memory reclaim that we are in such a
  73. * context via PF_MEMALLOC_NOIO to prevent memory reclaim re-entering
  74. * the filesystem here and potentially deadlocking.
  75. */
  76. if ((current->flags & PF_FSTRANS) || (flags & KM_NOFS))
  77. noio_flag = memalloc_noio_save();
  78. lflags = kmem_flags_convert(flags);
  79. ptr = __vmalloc(size, lflags | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
  80. if ((current->flags & PF_FSTRANS) || (flags & KM_NOFS))
  81. memalloc_noio_restore(noio_flag);
  82. return ptr;
  83. }
  84. void *
  85. kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
  86. {
  87. int retries = 0;
  88. gfp_t lflags = kmem_flags_convert(flags);
  89. void *ptr;
  90. do {
  91. ptr = krealloc(old, newsize, lflags);
  92. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  93. return ptr;
  94. if (!(++retries % 100))
  95. xfs_err(NULL,
  96. "%s(%u) possible memory allocation deadlock size %zu in %s (mode:0x%x)",
  97. current->comm, current->pid,
  98. newsize, __func__, lflags);
  99. congestion_wait(BLK_RW_ASYNC, HZ/50);
  100. } while (1);
  101. }
  102. void *
  103. kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
  104. {
  105. int retries = 0;
  106. gfp_t lflags = kmem_flags_convert(flags);
  107. void *ptr;
  108. do {
  109. ptr = kmem_cache_alloc(zone, lflags);
  110. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  111. return ptr;
  112. if (!(++retries % 100))
  113. xfs_err(NULL,
  114. "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
  115. current->comm, current->pid,
  116. __func__, lflags);
  117. congestion_wait(BLK_RW_ASYNC, HZ/50);
  118. } while (1);
  119. }