Linux-3.14.12 Memory Management Note "Kmalloc and Kfree Implementation" "Go"

Source: Internet
Author: User

This article was reproduced from: http://blog.chinaunix.net/uid-26859697-id-5573776.html

Kmalloc () is implemented on the basis of the Slab/slob/slub allocation algorithm, and many places use it as the entrance to the Slab/slob/slub allocation algorithm, which is actually slightly different.

Now analyze the implementation:

  1. "File:/include/linux/slab.h"
  2. /**
  3. * Kmalloc-allocate Memory
  4. * @size: How many bytes of memory is required.
  5. * @flags: The type of memory to allocate.
  6. *
  7. * Kmalloc is the normal method of allocating memory
  8. * For objects smaller than page size in the kernel.
  9. *
  10. * The @flags argument May is one of:
  11. *
  12. *%gfp_user-allocate memory on behalf of USER. May sleep.
  13. *
  14. *%gfp_kernel-allocate normal KERNEL RAM. May sleep.
  15. *
  16. *%gfp_atomic-allocation won't sleep. may use emergency pools.
  17. * For example, use this inside interrupt handlers.
  18. *
  19. *%gfp_highuser-allocate pages from high memory.
  20. *
  21. *%gfp_noio-do not does any I/O at all and trying to get memory.
  22. *
  23. *%gfp_nofs-do not make no FS calls while trying to get memory.
  24. *
  25. *%gfp_nowait-allocation won't sleep.
  26. *
  27. *%__gfp_thisnode-allocate node-local memory only.
  28. *
  29. *%gfp_dma-allocation suitable for DMA.
  30. * Should only is used for kmalloc () caches. Otherwise, use a
  31. * Slab created with SLAB_DMA.
  32. *
  33. * Also It is possible to set different the flags by OR ' ing
  34. * In one or more of the following additional @flags:
  35. *
  36. *%__gfp_cold-request cache-cold pages instead of
  37. * Trying to return cache-warm pages.
  38. *
  39. *%__gfp_high-this allocation have high priority and may use emergency pools.
  40. *
  41. *%__gfp_nofail-indicate that this allocation are in no-allowed to fail
  42. * (think twice before using).
  43. *
  44. *%__gfp_noretry-if memory is not immediately available,
  45. * Then give up at once.
  46. *
  47. *%__gfp_nowarn-if allocation fails, don ' t issue any warnings.
  48. *
  49. *%__gfp_repeat-if allocation fails Initially, try once more before failing.
  50. *
  51. * There is other flags available as well, but these is not intended
  52. * For general use, and so is not the documented here. For a full list of
  53. * Potential flags, always refer to linux/gfp.h.
  54. */
  55. static __always_inline void *kmalloc (size_t size, gfp_t flags)
  56. {
  57. if (__builtin_constant_p (size)) {
  58. if (Size > Kmalloc_max_cache_size)
  59. Return Kmalloc_large (size, flags);
  60. #ifndef Config_slob
  61. if (! ( Flags & GFP_DMA)) {
  62. int index = kmalloc_index (size);
  63. if (!index)
  64. return zero_size_ptr;
  65. Return Kmem_cache_alloc_trace (Kmalloc_caches[index],
  66. flags, size);
  67. }
  68. #endif
  69. }
  70. Return __kmalloc (size, flags);
  71. }

The parameter size of Kmalloc () indicates the amount of space requested, while flags represents the allocation flag. The Kamlloc has numerous distribution marks, each of which is assigned to identify specific bit bits, which can be combined in a variety of combinations.

Gfp_user: Used to indicate that memory is allocated for user space and may cause hibernation;

Gfp_kernel: The general allocation of kernel memory may cause hibernation;

Gfp_atomic: This assignment does not cause hibernation, but may use emergency memory resources, which are typically used in interrupt processing;

Gfp_highuser: Use high-end memory for allocation;

Gfp_noio: When allocating memory, prohibit any IO operation;

Gfp_nofs: When allocating memory, prohibit any file system operation;

Gfp_nowait: Disables hibernation when allocating memory;

__gfp_thisnode: Allocates memory only from local node memory;

GFP_DMA: Allocate the appropriate memory from DMA memory and should be used only for kmalloc cache allocation;

__gfp_cold: Used to request the allocation of cold pages in hot and cold pages;

__gfp_high: Used to indicate that the allocation has a higher priority and may use emergency memory resources;

__gfp_nofail: Used to indicate that the allocation is not allowed to fail, the flag needs to be used with caution;

__gfp_noretry: If the allocated memory can not be directly obtained, then no longer try to allocate, give up directly;

__gfp_nowarn: If the allocation process fails, do not report any alarms;

__gfp_repeat: If the allocation process fails, try to apply again;

The __builtin_constant_p in the function entry if judgment is the GCC built-in function that determines whether a value is a compile-time constant, or returns true, otherwise false. It also means that if you call Kmalloc () and the value is greater than kmalloc_max_cache_size (that is, the request space exceeds Kmalloc () to allocate the largest CACHE size), then the allocation will be made through Kmalloc_large () Otherwise, they will be allocated through __kmalloc (). If memory allocations are made through Kmalloc_large (), Kmalloc_large ()->kmalloc_order ()->__get_free_pages () will eventually request the required memory through the Buddy partner algorithm.

The partner algorithm has been analyzed before, no longer repeat, next look at the implementation of __kmalloc ():

  1. "FILE:/MM/SLUB.C"
  2. void *__kmalloc (size_t size, gfp_t flags)
  3. {
  4. struct Kmem_cache *s;
  5. void *ret;
  6. if (Unlikely (Size > Kmalloc_max_cache_size))
  7. Return Kmalloc_large (size, flags);
  8. s = kmalloc_slab (size, flags);
  9. if (Unlikely (Zero_or_null_ptr (s)))
  10. return s;
  11. ret = Slab_alloc (s, flags, _RET_IP_);
  12. Trace_kmalloc (_ret_ip_, RET, size, s->size, flags);
  13. return ret;
  14. }

The function also determines whether the request exceeds the maximum cache size, if it is allocated by Kmalloc_large (), and then calls Kmalloc_slab () through the application size and application flags to find the applicable kmem_cache; finally through Slab_alloc () For slab distribution.

Take a look at the implementation of Kmalloc_slab ():

  1. "File:/mm/slab_commmon.c"
  2. /*
  3. * Find The Kmem_cache structure that serves a given size of
  4. * Allocation
  5. */
  6. struct Kmem_cache *kmalloc_slab (size_t size, gfp_t flags)
  7. {
  8. int index;
  9. if (Unlikely (Size > Kmalloc_max_size)) {
  10. Warn_on_once (! ( Flags & __gfp_nowarn));
  11. return NULL;
  12. }
  13. if (size <= 192) {
  14. if (!size)
  15. return zero_size_ptr;
  16. index = Size_index[size_index_elem (size)];
  17. } else
  18. index = FLS (size-1);
  19. #ifdef CONFIG_ZONE_DMA
  20. if (Unlikely (Flags & GFP_DMA))
  21. return Kmalloc_dma_caches[index];
  22. #endif
  23. return Kmalloc_caches[index];
  24. }

If the size of the request exceeds the Kmalloc_max_size maximum, the return null represents a failure, and if the request size is less than 192 and is not 0, the index value is obtained by Size_index Global array after the Size_index_elem macro is converted to subscript. Otherwise, the index value will be obtained directly through FLS (); Finally, if the DMA memory configuration is turned on and the GFP_DMA flag is set, the Kmem_cache management structure information will be returned by Kmalloc_dma_caches with the index value, otherwise the KMALLOC_ Caches returns the structure.

It can be seen that the kmalloc () implementation is relatively simple, the allocated memory is not only the virtual address of the continuous storage space, but also the physical address of the continuous storage space. This is different from the memory of the Vmalloc () application that will be analyzed later.

In addition to the interface implementation of Kfree (), the function is implemented in many places, mainly in the SLAB.C/SLOB.C/SLUB.C, so also said Kmalloc () and Kfree () is based on slab/slob/slub implementation. Here the previous Slub algorithm, the main analysis of the SLUB.C in the Kfree () implementation:

  1. "FILE:/MM/SLUB.C"
  2. void Kfree (const void *x)
  3. {
  4. struct page *page;
  5. void *object = (void *) x;
  6. Trace_kfree (_ret_ip_, x);
  7. if (Unlikely (Zero_or_null_ptr (x)))
  8. Return
  9. page = Virt_to_head_page (x);
  10. if (Unlikely (! Pageslab (page))) {
  11. BUG_ON (! Pagecompound (page));
  12. Kfree_hook (x);
  13. __free_memcg_kmem_pages (page, Compound_order (page));
  14. Return
  15. }
  16. Slab_free (Page->slab_cache, page, object, _RET_IP_);
  17. }

The function is simple, first of all through the Trace_kfree () record kfree trajectory, then if (Unlikely (Zero_or_null_ptr (x)) to the address does not 0 judgment, and then Virt_to_head_page (x) Convert the virtual address to a page, and then judge if (The unlikely (! Pageslab (page))) determines whether the page is managed as a slab assignment and, if it is, is released through Slab_free (), otherwise it will enter the if branch, and in the If branch, the Kfree_hook () Do the kmemleak before releasing (the function is to encapsulate the Kmemleak_free ()), and after that it will __free_memcg_kmem_pages () release the page, and the function will also cgroup release processing.

Kmalloc () and Kfree () are so simple.

Linux-3.14.12 Memory Management Note "Kmalloc and Kfree Implementation" "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.