kmem.c 3.1 KB

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