kmem.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "time.h"
  25. #include "kmem.h"
  26. #include "xfs_message.h"
  27. /*
  28. * Greedy allocation. May fail and may return vmalloced memory.
  29. */
  30. void *
  31. kmem_zalloc_greedy(size_t *size, size_t minsize, size_t maxsize)
  32. {
  33. void *ptr;
  34. size_t kmsize = maxsize;
  35. while (!(ptr = vzalloc(kmsize))) {
  36. if ((kmsize >>= 1) <= minsize)
  37. kmsize = minsize;
  38. }
  39. if (ptr)
  40. *size = kmsize;
  41. return ptr;
  42. }
  43. void *
  44. kmem_alloc(size_t size, xfs_km_flags_t flags)
  45. {
  46. int retries = 0;
  47. gfp_t lflags = kmem_flags_convert(flags);
  48. void *ptr;
  49. do {
  50. ptr = kmalloc(size, lflags);
  51. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  52. return ptr;
  53. if (!(++retries % 100))
  54. xfs_err(NULL,
  55. "possible memory allocation deadlock in %s (mode:0x%x)",
  56. __func__, lflags);
  57. congestion_wait(BLK_RW_ASYNC, HZ/50);
  58. } while (1);
  59. }
  60. void *
  61. kmem_zalloc(size_t size, xfs_km_flags_t flags)
  62. {
  63. void *ptr;
  64. ptr = kmem_alloc(size, flags);
  65. if (ptr)
  66. memset((char *)ptr, 0, (int)size);
  67. return ptr;
  68. }
  69. void *
  70. kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
  71. {
  72. void *ptr;
  73. ptr = kmem_zalloc(size, flags | KM_MAYFAIL);
  74. if (ptr)
  75. return ptr;
  76. return vzalloc(size);
  77. }
  78. void
  79. kmem_free(const void *ptr)
  80. {
  81. if (!is_vmalloc_addr(ptr)) {
  82. kfree(ptr);
  83. } else {
  84. vfree(ptr);
  85. }
  86. }
  87. void *
  88. kmem_realloc(const void *ptr, size_t newsize, size_t oldsize,
  89. xfs_km_flags_t flags)
  90. {
  91. void *new;
  92. new = kmem_alloc(newsize, flags);
  93. if (ptr) {
  94. if (new)
  95. memcpy(new, ptr,
  96. ((oldsize < newsize) ? oldsize : newsize));
  97. kmem_free(ptr);
  98. }
  99. return new;
  100. }
  101. void *
  102. kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
  103. {
  104. int retries = 0;
  105. gfp_t lflags = kmem_flags_convert(flags);
  106. void *ptr;
  107. do {
  108. ptr = kmem_cache_alloc(zone, lflags);
  109. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  110. return ptr;
  111. if (!(++retries % 100))
  112. xfs_err(NULL,
  113. "possible memory allocation deadlock in %s (mode:0x%x)",
  114. __func__, lflags);
  115. congestion_wait(BLK_RW_ASYNC, HZ/50);
  116. } while (1);
  117. }
  118. void *
  119. kmem_zone_zalloc(kmem_zone_t *zone, xfs_km_flags_t flags)
  120. {
  121. void *ptr;
  122. ptr = kmem_zone_alloc(zone, flags);
  123. if (ptr)
  124. memset((char *)ptr, 0, kmem_cache_size(zone));
  125. return ptr;
  126. }