balance 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. Started Jan 2000 by Kanoj Sarcar <kanoj@sgi.com>
  2. Memory balancing is needed for non __GFP_WAIT as well as for non
  3. __GFP_IO allocations.
  4. There are two reasons to be requesting non __GFP_WAIT allocations:
  5. the caller can not sleep (typically intr context), or does not want
  6. to incur cost overheads of page stealing and possible swap io for
  7. whatever reasons.
  8. __GFP_IO allocation requests are made to prevent file system deadlocks.
  9. In the absence of non sleepable allocation requests, it seems detrimental
  10. to be doing balancing. Page reclamation can be kicked off lazily, that
  11. is, only when needed (aka zone free memory is 0), instead of making it
  12. a proactive process.
  13. That being said, the kernel should try to fulfill requests for direct
  14. mapped pages from the direct mapped pool, instead of falling back on
  15. the dma pool, so as to keep the dma pool filled for dma requests (atomic
  16. or not). A similar argument applies to highmem and direct mapped pages.
  17. OTOH, if there is a lot of free dma pages, it is preferable to satisfy
  18. regular memory requests by allocating one from the dma pool, instead
  19. of incurring the overhead of regular zone balancing.
  20. In 2.2, memory balancing/page reclamation would kick off only when the
  21. _total_ number of free pages fell below 1/64 th of total memory. With the
  22. right ratio of dma and regular memory, it is quite possible that balancing
  23. would not be done even when the dma zone was completely empty. 2.2 has
  24. been running production machines of varying memory sizes, and seems to be
  25. doing fine even with the presence of this problem. In 2.3, due to
  26. HIGHMEM, this problem is aggravated.
  27. In 2.3, zone balancing can be done in one of two ways: depending on the
  28. zone size (and possibly of the size of lower class zones), we can decide
  29. at init time how many free pages we should aim for while balancing any
  30. zone. The good part is, while balancing, we do not need to look at sizes
  31. of lower class zones, the bad part is, we might do too frequent balancing
  32. due to ignoring possibly lower usage in the lower class zones. Also,
  33. with a slight change in the allocation routine, it is possible to reduce
  34. the memclass() macro to be a simple equality.
  35. Another possible solution is that we balance only when the free memory
  36. of a zone _and_ all its lower class zones falls below 1/64th of the
  37. total memory in the zone and its lower class zones. This fixes the 2.2
  38. balancing problem, and stays as close to 2.2 behavior as possible. Also,
  39. the balancing algorithm works the same way on the various architectures,
  40. which have different numbers and types of zones. If we wanted to get
  41. fancy, we could assign different weights to free pages in different
  42. zones in the future.
  43. Note that if the size of the regular zone is huge compared to dma zone,
  44. it becomes less significant to consider the free dma pages while
  45. deciding whether to balance the regular zone. The first solution
  46. becomes more attractive then.
  47. The appended patch implements the second solution. It also "fixes" two
  48. problems: first, kswapd is woken up as in 2.2 on low memory conditions
  49. for non-sleepable allocations. Second, the HIGHMEM zone is also balanced,
  50. so as to give a fighting chance for replace_with_highmem() to get a
  51. HIGHMEM page, as well as to ensure that HIGHMEM allocations do not
  52. fall back into regular zone. This also makes sure that HIGHMEM pages
  53. are not leaked (for example, in situations where a HIGHMEM page is in
  54. the swapcache but is not being used by anyone)
  55. kswapd also needs to know about the zones it should balance. kswapd is
  56. primarily needed in a situation where balancing can not be done,
  57. probably because all allocation requests are coming from intr context
  58. and all process contexts are sleeping. For 2.3, kswapd does not really
  59. need to balance the highmem zone, since intr context does not request
  60. highmem pages. kswapd looks at the zone_wake_kswapd field in the zone
  61. structure to decide whether a zone needs balancing.
  62. Page stealing from process memory and shm is done if stealing the page would
  63. alleviate memory pressure on any zone in the page's node that has fallen below
  64. its watermark.
  65. pages_min/pages_low/pages_high/low_on_memory/zone_wake_kswapd: These are
  66. per-zone fields, used to determine when a zone needs to be balanced. When
  67. the number of pages falls below pages_min, the hysteric field low_on_memory
  68. gets set. This stays set till the number of free pages becomes pages_high.
  69. When low_on_memory is set, page allocation requests will try to free some
  70. pages in the zone (providing GFP_WAIT is set in the request). Orthogonal
  71. to this, is the decision to poke kswapd to free some zone pages. That
  72. decision is not hysteresis based, and is done when the number of free
  73. pages is below pages_low; in which case zone_wake_kswapd is also set.
  74. (Good) Ideas that I have heard:
  75. 1. Dynamic experience should influence balancing: number of failed requests
  76. for a zone can be tracked and fed into the balancing scheme (jalvo@mbay.net)
  77. 2. Implement a replace_with_highmem()-like replace_with_regular() to preserve
  78. dma pages. (lkd@tantalophile.demon.co.uk)