patch-lib_mm_memlock_c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. --- LVM2.2.02.177.orig/lib/mm/memlock.c 2017-12-18 21:44:35.000000000 +0100
  2. +++ LVM2.2.02.177/lib/mm/memlock.c 2018-06-07 03:09:58.000000000 +0200
  3. @@ -25,7 +25,6 @@
  4. #include <sys/mman.h>
  5. #include <sys/time.h>
  6. #include <sys/resource.h>
  7. -#include <malloc.h>
  8. #ifdef HAVE_VALGRIND
  9. #include <valgrind.h>
  10. @@ -152,10 +151,8 @@ static void _touch_memory(void *mem, siz
  11. static void _allocate_memory(void)
  12. {
  13. #ifndef VALGRIND_POOL
  14. - void *stack_mem;
  15. + void *stack_mem, *temp_malloc_mem;
  16. struct rlimit limit;
  17. - int i, area = 0, missing = _size_malloc_tmp, max_areas = 32, hblks;
  18. - char *areas[max_areas];
  19. /* Check if we could preallocate requested stack */
  20. if ((getrlimit (RLIMIT_STACK, &limit) == 0) &&
  21. @@ -164,50 +161,13 @@ static void _allocate_memory(void)
  22. _touch_memory(stack_mem, _size_stack);
  23. /* FIXME else warn user setting got ignored */
  24. - /*
  25. - * When a brk() fails due to fragmented address space (which sometimes
  26. - * happens when we try to grab 8M or so), glibc will make a new
  27. - * arena. In this arena, the rules for using “direct” mmap are relaxed,
  28. - * circumventing the MAX_MMAPs and MMAP_THRESHOLD settings. We can,
  29. - * however, detect when this happens with mallinfo() and try to co-opt
  30. - * malloc into using MMAP as a MORECORE substitute instead of returning
  31. - * MMAP'd memory directly. Since MMAP-as-MORECORE does not munmap the
  32. - * memory on free(), this is good enough for our purposes.
  33. - */
  34. - while (missing > 0) {
  35. - struct mallinfo inf = mallinfo();
  36. - hblks = inf.hblks;
  37. -
  38. - if ((areas[area] = malloc(_size_malloc_tmp)))
  39. - _touch_memory(areas[area], _size_malloc_tmp);
  40. -
  41. - inf = mallinfo();
  42. -
  43. - if (hblks < inf.hblks) {
  44. - /* malloc cheated and used mmap, even though we told it
  45. - not to; we try with twice as many areas, each half
  46. - the size, to circumvent the faulty logic in glibc */
  47. - free(areas[area]);
  48. - _size_malloc_tmp /= 2;
  49. - } else {
  50. - ++ area;
  51. - missing -= _size_malloc_tmp;
  52. - }
  53. -
  54. - if (area == max_areas && missing > 0) {
  55. - /* Too bad. Warn the user and proceed, as things are
  56. - * most likely going to work out anyway. */
  57. - log_warn("WARNING: Failed to reserve memory, %d bytes missing.", missing);
  58. - break;
  59. - }
  60. - }
  61. + if ((temp_malloc_mem = malloc(_size_malloc_tmp)))
  62. + _touch_memory(temp_malloc_mem, _size_malloc_tmp);
  63. if ((_malloc_mem = malloc(_size_malloc)))
  64. _touch_memory(_malloc_mem, _size_malloc);
  65. - /* free up the reserves so subsequent malloc's can use that memory */
  66. - for (i = 0; i < area; ++i)
  67. - free(areas[i]);
  68. + free(temp_malloc_mem);
  69. #endif
  70. }