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