genalloc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Basic general purpose allocator for managing special purpose memory
  3. * not managed by the regular kmalloc/kfree interface.
  4. * Uses for this includes on-device special memory, uncached memory
  5. * etc.
  6. *
  7. * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
  8. *
  9. * This source code is licensed under the GNU General Public License,
  10. * Version 2. See the file COPYING for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/genalloc.h>
  14. /*
  15. * Create a new special memory pool.
  16. *
  17. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  18. * @nid: node id of the node the pool structure should be allocated on, or -1
  19. */
  20. struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
  21. {
  22. struct gen_pool *pool;
  23. pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
  24. if (pool != NULL) {
  25. rwlock_init(&pool->lock);
  26. INIT_LIST_HEAD(&pool->chunks);
  27. pool->min_alloc_order = min_alloc_order;
  28. }
  29. return pool;
  30. }
  31. EXPORT_SYMBOL(gen_pool_create);
  32. /*
  33. * Add a new chunk of memory to the specified pool.
  34. *
  35. * @pool: pool to add new memory chunk to
  36. * @addr: starting address of memory chunk to add to pool
  37. * @size: size in bytes of the memory chunk to add to pool
  38. * @nid: node id of the node the chunk structure and bitmap should be
  39. * allocated on, or -1
  40. */
  41. int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
  42. int nid)
  43. {
  44. struct gen_pool_chunk *chunk;
  45. int nbits = size >> pool->min_alloc_order;
  46. int nbytes = sizeof(struct gen_pool_chunk) +
  47. (nbits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
  48. chunk = kmalloc_node(nbytes, GFP_KERNEL, nid);
  49. if (unlikely(chunk == NULL))
  50. return -1;
  51. memset(chunk, 0, nbytes);
  52. spin_lock_init(&chunk->lock);
  53. chunk->start_addr = addr;
  54. chunk->end_addr = addr + size;
  55. write_lock(&pool->lock);
  56. list_add(&chunk->next_chunk, &pool->chunks);
  57. write_unlock(&pool->lock);
  58. return 0;
  59. }
  60. EXPORT_SYMBOL(gen_pool_add);
  61. /*
  62. * Allocate the requested number of bytes from the specified pool.
  63. * Uses a first-fit algorithm.
  64. *
  65. * @pool: pool to allocate from
  66. * @size: number of bytes to allocate from the pool
  67. */
  68. unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
  69. {
  70. struct list_head *_chunk;
  71. struct gen_pool_chunk *chunk;
  72. unsigned long addr, flags;
  73. int order = pool->min_alloc_order;
  74. int nbits, bit, start_bit, end_bit;
  75. if (size == 0)
  76. return 0;
  77. nbits = (size + (1UL << order) - 1) >> order;
  78. read_lock(&pool->lock);
  79. list_for_each(_chunk, &pool->chunks) {
  80. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  81. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  82. end_bit -= nbits + 1;
  83. spin_lock_irqsave(&chunk->lock, flags);
  84. bit = -1;
  85. while (bit + 1 < end_bit) {
  86. bit = find_next_zero_bit(chunk->bits, end_bit, bit + 1);
  87. if (bit >= end_bit)
  88. break;
  89. start_bit = bit;
  90. if (nbits > 1) {
  91. bit = find_next_bit(chunk->bits, bit + nbits,
  92. bit + 1);
  93. if (bit - start_bit < nbits)
  94. continue;
  95. }
  96. addr = chunk->start_addr +
  97. ((unsigned long)start_bit << order);
  98. while (nbits--)
  99. __set_bit(start_bit++, &chunk->bits);
  100. spin_unlock_irqrestore(&chunk->lock, flags);
  101. read_unlock(&pool->lock);
  102. return addr;
  103. }
  104. spin_unlock_irqrestore(&chunk->lock, flags);
  105. }
  106. read_unlock(&pool->lock);
  107. return 0;
  108. }
  109. EXPORT_SYMBOL(gen_pool_alloc);
  110. /*
  111. * Free the specified memory back to the specified pool.
  112. *
  113. * @pool: pool to free to
  114. * @addr: starting address of memory to free back to pool
  115. * @size: size in bytes of memory to free
  116. */
  117. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  118. {
  119. struct list_head *_chunk;
  120. struct gen_pool_chunk *chunk;
  121. unsigned long flags;
  122. int order = pool->min_alloc_order;
  123. int bit, nbits;
  124. nbits = (size + (1UL << order) - 1) >> order;
  125. read_lock(&pool->lock);
  126. list_for_each(_chunk, &pool->chunks) {
  127. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  128. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  129. BUG_ON(addr + size > chunk->end_addr);
  130. spin_lock_irqsave(&chunk->lock, flags);
  131. bit = (addr - chunk->start_addr) >> order;
  132. while (nbits--)
  133. __clear_bit(bit++, &chunk->bits);
  134. spin_unlock_irqrestore(&chunk->lock, flags);
  135. break;
  136. }
  137. }
  138. BUG_ON(nbits > 0);
  139. read_unlock(&pool->lock);
  140. }
  141. EXPORT_SYMBOL(gen_pool_free);