kmem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_large(size_t size, xfs_km_flags_t flags)
  62. {
  63. void *ptr;
  64. ptr = kmem_zalloc(size, flags | KM_MAYFAIL);
  65. if (ptr)
  66. return ptr;
  67. return vzalloc(size);
  68. }
  69. void
  70. kmem_free(const void *ptr)
  71. {
  72. if (!is_vmalloc_addr(ptr)) {
  73. kfree(ptr);
  74. } else {
  75. vfree(ptr);
  76. }
  77. }
  78. void *
  79. kmem_realloc(const void *ptr, size_t newsize, size_t oldsize,
  80. xfs_km_flags_t flags)
  81. {
  82. void *new;
  83. new = kmem_alloc(newsize, flags);
  84. if (ptr) {
  85. if (new)
  86. memcpy(new, ptr,
  87. ((oldsize < newsize) ? oldsize : newsize));
  88. kmem_free(ptr);
  89. }
  90. return new;
  91. }
  92. void *
  93. kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
  94. {
  95. int retries = 0;
  96. gfp_t lflags = kmem_flags_convert(flags);
  97. void *ptr;
  98. do {
  99. ptr = kmem_cache_alloc(zone, lflags);
  100. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  101. return ptr;
  102. if (!(++retries % 100))
  103. xfs_err(NULL,
  104. "possible memory allocation deadlock in %s (mode:0x%x)",
  105. __func__, lflags);
  106. congestion_wait(BLK_RW_ASYNC, HZ/50);
  107. } while (1);
  108. }