kmem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/vmalloc.h>
  20. #include <linux/highmem.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. #define MAX_VMALLOCS 6
  27. #define MAX_SLAB_SIZE 0x20000
  28. void *
  29. kmem_alloc(size_t size, unsigned int __nocast flags)
  30. {
  31. int retries = 0;
  32. gfp_t lflags = kmem_flags_convert(flags);
  33. void *ptr;
  34. #ifdef DEBUG
  35. if (unlikely(!(flags & KM_LARGE) && (size > PAGE_SIZE))) {
  36. printk(KERN_WARNING "Large %s attempt, size=%ld\n",
  37. __FUNCTION__, (long)size);
  38. dump_stack();
  39. }
  40. #endif
  41. do {
  42. if (size < MAX_SLAB_SIZE || retries > MAX_VMALLOCS)
  43. ptr = kmalloc(size, lflags);
  44. else
  45. ptr = __vmalloc(size, lflags, PAGE_KERNEL);
  46. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  47. return ptr;
  48. if (!(++retries % 100))
  49. printk(KERN_ERR "XFS: possible memory allocation "
  50. "deadlock in %s (mode:0x%x)\n",
  51. __FUNCTION__, lflags);
  52. congestion_wait(WRITE, HZ/50);
  53. } while (1);
  54. }
  55. void *
  56. kmem_zalloc(size_t size, unsigned int __nocast flags)
  57. {
  58. void *ptr;
  59. ptr = kmem_alloc(size, flags);
  60. if (ptr)
  61. memset((char *)ptr, 0, (int)size);
  62. return ptr;
  63. }
  64. void *
  65. kmem_zalloc_greedy(size_t *size, size_t minsize, size_t maxsize,
  66. unsigned int __nocast flags)
  67. {
  68. void *ptr;
  69. size_t kmsize = maxsize;
  70. unsigned int kmflags = (flags & ~KM_SLEEP) | KM_NOSLEEP;
  71. while (!(ptr = kmem_zalloc(kmsize, kmflags))) {
  72. if ((kmsize <= minsize) && (flags & KM_NOSLEEP))
  73. break;
  74. if ((kmsize >>= 1) <= minsize) {
  75. kmsize = minsize;
  76. kmflags = flags;
  77. }
  78. }
  79. if (ptr)
  80. *size = kmsize;
  81. return ptr;
  82. }
  83. void
  84. kmem_free(void *ptr, size_t size)
  85. {
  86. if (((unsigned long)ptr < VMALLOC_START) ||
  87. ((unsigned long)ptr >= VMALLOC_END)) {
  88. kfree(ptr);
  89. } else {
  90. vfree(ptr);
  91. }
  92. }
  93. void *
  94. kmem_realloc(void *ptr, size_t newsize, size_t oldsize,
  95. unsigned int __nocast flags)
  96. {
  97. void *new;
  98. new = kmem_alloc(newsize, flags);
  99. if (ptr) {
  100. if (new)
  101. memcpy(new, ptr,
  102. ((oldsize < newsize) ? oldsize : newsize));
  103. kmem_free(ptr, oldsize);
  104. }
  105. return new;
  106. }
  107. void *
  108. kmem_zone_alloc(kmem_zone_t *zone, unsigned int __nocast flags)
  109. {
  110. int retries = 0;
  111. gfp_t lflags = kmem_flags_convert(flags);
  112. void *ptr;
  113. do {
  114. ptr = kmem_cache_alloc(zone, lflags);
  115. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  116. return ptr;
  117. if (!(++retries % 100))
  118. printk(KERN_ERR "XFS: possible memory allocation "
  119. "deadlock in %s (mode:0x%x)\n",
  120. __FUNCTION__, lflags);
  121. congestion_wait(WRITE, HZ/50);
  122. } while (1);
  123. }
  124. void *
  125. kmem_zone_zalloc(kmem_zone_t *zone, unsigned int __nocast flags)
  126. {
  127. void *ptr;
  128. ptr = kmem_zone_alloc(zone, flags);
  129. if (ptr)
  130. memset((char *)ptr, 0, kmem_cache_size(zone));
  131. return ptr;
  132. }