slabmgr.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * slabmgr.h
  17. *
  18. * This file contains implementation of SLAB (variable-size) memory allocator.
  19. */
  20. #ifndef SLABMGR_H
  21. #define SLABMGR_H
  22. #include "list.h"
  23. #include "api.h"
  24. #include <pal_debug.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <sys/mman.h>
  28. // Before calling any of `system_malloc` and `system_free` this library will
  29. // acquire `system_lock` (the systen_* implementation must not do it).
  30. #ifndef system_malloc
  31. #error "macro \"void * system_malloc(int size)\" not declared"
  32. #endif
  33. #ifndef system_free
  34. #error "macro \"void * system_free(void * ptr, int size)\" not declared"
  35. #endif
  36. #ifndef system_lock
  37. #define system_lock() ({})
  38. #endif
  39. #ifndef system_unlock
  40. #define system_unlock() ({})
  41. #endif
  42. /* malloc is supposed to provide some kind of alignment guarantees, but
  43. * I can't find a specific reference to what that should be for x86_64.
  44. * The first link here is a reference to a technical report from Mozilla,
  45. * which seems to indicate that 64-bit platforms align return values to
  46. * 16-bytes. calloc and malloc provide the same alignment guarantees.
  47. * calloc additionally sets the memory to 0, which malloc is not required
  48. * to do.
  49. *
  50. * http://www.erahm.org/2016/03/24/minimum-alignment-of-allocation-across-platforms/
  51. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html
  52. */
  53. #define MIN_MALLOC_ALIGNMENT 16
  54. /* Slab objects need to be a multiple of 16 bytes to ensure proper address
  55. * alignment for malloc and calloc. */
  56. #define OBJ_PADDING 15
  57. #define LARGE_OBJ_PADDING 8
  58. /* Returns the smallest exact multiple of _y that is at least as large as _x.
  59. * In other words, returns _x if _x is a multiple of _y, otherwise rounds
  60. * _x up to be a multiple of _y.
  61. */
  62. #define ROUND_UP(_x, _y) ((((_x) + (_y) - 1) / (_y)) * (_y))
  63. DEFINE_LIST(slab_obj);
  64. typedef struct __attribute__((packed)) slab_obj {
  65. unsigned char level;
  66. unsigned char padding[OBJ_PADDING];
  67. union {
  68. LIST_TYPE(slab_obj) __list;
  69. unsigned char *raw;
  70. };
  71. } SLAB_OBJ_TYPE, * SLAB_OBJ;
  72. /* In order for slab elements to be 16-byte aligned, struct slab_area must
  73. * be a multiple of 16 bytes. TODO: Add compile time assertion that this
  74. * invariant is respected. */
  75. #define AREA_PADDING 12
  76. DEFINE_LIST(slab_area);
  77. typedef struct __attribute__((packed)) slab_area {
  78. LIST_TYPE(slab_area) __list;
  79. unsigned int size;
  80. unsigned char pad[AREA_PADDING];
  81. unsigned char raw[];
  82. } SLAB_AREA_TYPE, * SLAB_AREA;
  83. #ifdef SLAB_DEBUG
  84. struct slab_debug {
  85. struct {
  86. const char * file;
  87. int line;
  88. } alloc, free;
  89. };
  90. # define SLAB_DEBUG_SIZE sizeof(struct slab_debug)
  91. #else
  92. # define SLAB_DEBUG_SIZE 0
  93. #endif
  94. #ifdef SLAB_CANARY
  95. # define SLAB_CANARY_STRING 0xDEADBEEF
  96. # define SLAB_CANARY_SIZE sizeof(unsigned long)
  97. #else
  98. # define SLAB_CANARY_SIZE 0
  99. #endif
  100. #define SLAB_HDR_SIZE \
  101. ROUND_UP((sizeof(SLAB_OBJ_TYPE) - sizeof(LIST_TYPE(slab_obj)) + \
  102. SLAB_DEBUG_SIZE + SLAB_CANARY_SIZE), \
  103. MIN_MALLOC_ALIGNMENT)
  104. #ifndef SLAB_LEVEL
  105. #define SLAB_LEVEL 8
  106. #endif
  107. #ifndef SLAB_LEVEL_SIZES
  108. # define SLAB_LEVEL_SIZES 16, 32, 64, \
  109. 128 - SLAB_HDR_SIZE, \
  110. 256 - SLAB_HDR_SIZE, \
  111. 512 - SLAB_HDR_SIZE, \
  112. 1024 - SLAB_HDR_SIZE, \
  113. 2048 - SLAB_HDR_SIZE
  114. # define SLAB_LEVELS_SUM (4080 - SLAB_HDR_SIZE * 5)
  115. #else
  116. # ifndef SLAB_LEVELS_SUM
  117. # error "SALB_LEVELS_SUM not defined"
  118. # endif
  119. #endif
  120. // User buffer sizes on each level (not counting mandatory header
  121. // (SLAB_HDR_SIZE)).
  122. static const int slab_levels[SLAB_LEVEL] = { SLAB_LEVEL_SIZES };
  123. DEFINE_LISTP(slab_obj);
  124. DEFINE_LISTP(slab_area);
  125. typedef struct slab_mgr {
  126. LISTP_TYPE(slab_area) area_list[SLAB_LEVEL];
  127. LISTP_TYPE(slab_obj) free_list[SLAB_LEVEL];
  128. size_t size[SLAB_LEVEL];
  129. void * addr[SLAB_LEVEL], * addr_top[SLAB_LEVEL];
  130. SLAB_AREA active_area[SLAB_LEVEL];
  131. } SLAB_MGR_TYPE, * SLAB_MGR;
  132. typedef struct __attribute__((packed)) large_mem_obj {
  133. // offset 0
  134. unsigned long size; // User buffer size (i.e. excluding control structures)
  135. unsigned char large_padding[LARGE_OBJ_PADDING];
  136. // offset 16
  137. unsigned char level;
  138. unsigned char padding[OBJ_PADDING];
  139. // offset 32
  140. unsigned char raw[];
  141. } LARGE_MEM_OBJ_TYPE, * LARGE_MEM_OBJ;
  142. #define OBJ_LEVEL(obj) ((obj)->level)
  143. #define OBJ_RAW(obj) (&(obj)->raw)
  144. #define RAW_TO_LEVEL(raw_ptr) \
  145. (*((const unsigned char *) (raw_ptr) - OBJ_PADDING - 1))
  146. #define RAW_TO_OBJ(raw_ptr, type) container_of((raw_ptr), type, raw)
  147. #define __SUM_OBJ_SIZE(slab_size, size) \
  148. (((slab_size) + SLAB_HDR_SIZE) * (size))
  149. #define __MIN_MEM_SIZE() (sizeof(SLAB_AREA_TYPE))
  150. #define __MAX_MEM_SIZE(slab_size, size) \
  151. (__MIN_MEM_SIZE() + __SUM_OBJ_SIZE((slab_size), (size)))
  152. #define __INIT_SUM_OBJ_SIZE(size) \
  153. ((SLAB_LEVELS_SUM + SLAB_HDR_SIZE * SLAB_LEVEL) * (size))
  154. #define __INIT_MIN_MEM_SIZE() \
  155. (sizeof(SLAB_MGR_TYPE) + sizeof(SLAB_AREA_TYPE) * SLAB_LEVEL)
  156. #define __INIT_MAX_MEM_SIZE(size) \
  157. (__INIT_MIN_MEM_SIZE() + __INIT_SUM_OBJ_SIZE((size)))
  158. #ifdef PAGE_SIZE
  159. static inline int size_align_down(int slab_size, int size)
  160. {
  161. int s = __MAX_MEM_SIZE(slab_size, size);
  162. int p = s - (s & ~(PAGE_SIZE - 1));
  163. int o = __SUM_OBJ_SIZE(slab_size, 1);
  164. return size - p / o - (p % o ? 1 : 0);
  165. }
  166. static inline int size_align_up(int slab_size, int size)
  167. {
  168. int s = __MAX_MEM_SIZE(slab_size, size);
  169. int p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  170. int o = __SUM_OBJ_SIZE(slab_size, 1);
  171. return size + p / o;
  172. }
  173. static inline int init_align_down(int size)
  174. {
  175. int s = __INIT_MAX_MEM_SIZE(size);
  176. int p = s - (s & ~(PAGE_SIZE - 1));
  177. int o = __INIT_SUM_OBJ_SIZE(1);
  178. return size - p /o - (p % o ? 1 : 0);
  179. }
  180. static inline int init_size_align_up(int size)
  181. {
  182. int s = __INIT_MAX_MEM_SIZE(size);
  183. int p = ((s + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) - s;
  184. int o = __INIT_SUM_OBJ_SIZE(1);
  185. return size + p / o;
  186. }
  187. #endif /* PAGE_SIZE */
  188. #ifndef STARTUP_SIZE
  189. # define STARTUP_SIZE 16
  190. #endif
  191. static inline void __set_free_slab_area (SLAB_AREA area, SLAB_MGR mgr,
  192. int level)
  193. {
  194. int slab_size = slab_levels[level] + SLAB_HDR_SIZE;
  195. mgr->addr[level] = (void *) area->raw;
  196. mgr->addr_top[level] = (void *) area->raw + (area->size * slab_size);
  197. mgr->size[level] += area->size;
  198. mgr->active_area[level] = area;
  199. }
  200. static inline SLAB_MGR create_slab_mgr (void)
  201. {
  202. #ifdef PAGE_SIZE
  203. size_t size = init_size_align_up(STARTUP_SIZE);
  204. #else
  205. size_t size = STARTUP_SIZE;
  206. #endif
  207. void * mem = NULL;
  208. SLAB_AREA area;
  209. SLAB_MGR mgr;
  210. /* If the allocation failed, always try smaller sizes */
  211. for (; size > 0; size >>= 1) {
  212. mem = system_malloc(__INIT_MAX_MEM_SIZE(size));
  213. if (mem)
  214. break;
  215. }
  216. if (!mem)
  217. return NULL;
  218. mgr = (SLAB_MGR) mem;
  219. void * addr = (void *) mgr + sizeof(SLAB_MGR_TYPE);
  220. int i;
  221. for (i = 0 ; i < SLAB_LEVEL ; i++) {
  222. area = (SLAB_AREA) addr;
  223. area->size = size;
  224. INIT_LIST_HEAD(area, __list);
  225. INIT_LISTP(&mgr->area_list[i]);
  226. listp_add_tail(area, &mgr->area_list[i], __list);
  227. INIT_LISTP(&mgr->free_list[i]);
  228. mgr->size[i] = 0;
  229. __set_free_slab_area(area, mgr, i);
  230. addr += __MAX_MEM_SIZE(slab_levels[i], STARTUP_SIZE);
  231. }
  232. return mgr;
  233. }
  234. static inline void destroy_slab_mgr (SLAB_MGR mgr)
  235. {
  236. void * addr = (void *) mgr + sizeof(SLAB_MGR_TYPE);
  237. SLAB_AREA area, tmp, n;
  238. int i;
  239. for (i = 0 ; i < SLAB_LEVEL; i++) {
  240. area = (SLAB_AREA) addr;
  241. listp_for_each_entry_safe(tmp, n, &mgr->area_list[i], __list) {
  242. if (tmp != area)
  243. system_free(area,
  244. __MAX_MEM_SIZE(slab_levels[i], area->size));
  245. }
  246. addr += __MAX_MEM_SIZE(slab_levels[i], STARTUP_SIZE);
  247. }
  248. system_free(mgr, addr - (void *) mgr);
  249. }
  250. // system_lock needs to be held by the caller on entry.
  251. static inline int enlarge_slab_mgr (SLAB_MGR mgr, int level)
  252. {
  253. assert(level < SLAB_LEVEL);
  254. /* DEP 11/24/17: This strategy basically doubles a level's size
  255. * every time it grows. The assumption if we get this far is that
  256. * mgr->addr == mgr->top_addr */
  257. assert(mgr->addr[level] == mgr->addr_top[level]);
  258. size_t size = mgr->size[level];
  259. SLAB_AREA area;
  260. /* If there is a previously allocated area, just activate it. */
  261. area = listp_prev_entry(mgr->active_area[level], &mgr->area_list[level], __list);
  262. if (area) {
  263. __set_free_slab_area(area, mgr, level);
  264. return 0;
  265. }
  266. /* system_malloc() may be blocking, so we release the lock before
  267. * allocating more memory */
  268. system_unlock();
  269. /* If the allocation failed, always try smaller sizes */
  270. for (; size > 0; size >>= 1) {
  271. area = (SLAB_AREA) system_malloc(__MAX_MEM_SIZE(slab_levels[level], size));
  272. if (area)
  273. break;
  274. }
  275. if (!area) {
  276. system_lock();
  277. return -ENOMEM;
  278. }
  279. system_lock();
  280. area->size = size;
  281. INIT_LIST_HEAD(area, __list);
  282. /* There can be concurrent operations to extend the SLAB manager. In case
  283. * someone has already enlarged the space, we just add the new area to the
  284. * list for later use. */
  285. listp_add(area, &mgr->area_list[level], __list);
  286. if (mgr->size[level] == size) /* check if the size has changed */
  287. __set_free_slab_area(area, mgr, level);
  288. return 0;
  289. }
  290. static inline void * slab_alloc (SLAB_MGR mgr, int size)
  291. {
  292. SLAB_OBJ mobj;
  293. int i;
  294. int level = -1;
  295. for (i = 0 ; i < SLAB_LEVEL ; i++)
  296. if (size <= slab_levels[i]) {
  297. level = i;
  298. break;
  299. }
  300. if (level == -1) {
  301. LARGE_MEM_OBJ mem = (LARGE_MEM_OBJ)
  302. system_malloc(sizeof(LARGE_MEM_OBJ_TYPE) + size);
  303. if (!mem)
  304. return NULL;
  305. mem->size = size;
  306. OBJ_LEVEL(mem) = (unsigned char) -1;
  307. return OBJ_RAW(mem);
  308. }
  309. system_lock();
  310. assert(mgr->addr[level] <= mgr->addr_top[level]);
  311. if (mgr->addr[level] == mgr->addr_top[level] &&
  312. listp_empty(&mgr->free_list[level])) {
  313. int ret = enlarge_slab_mgr(mgr, level);
  314. if (ret < 0) {
  315. system_unlock();
  316. return NULL;
  317. }
  318. }
  319. if (!listp_empty(&mgr->free_list[level])) {
  320. mobj = listp_first_entry(&mgr->free_list[level], SLAB_OBJ_TYPE, __list);
  321. listp_del(mobj, &mgr->free_list[level], __list);
  322. } else {
  323. mobj = (void *) mgr->addr[level];
  324. mgr->addr[level] += slab_levels[level] + SLAB_HDR_SIZE;
  325. }
  326. assert(mgr->addr[level] <= mgr->addr_top[level]);
  327. OBJ_LEVEL(mobj) = level;
  328. system_unlock();
  329. #ifdef SLAB_CANARY
  330. unsigned long * m =
  331. (unsigned long *) ((void *) OBJ_RAW(mobj) + slab_levels[level]);
  332. *m = SLAB_CANARY_STRING;
  333. #endif
  334. return OBJ_RAW(mobj);
  335. }
  336. #ifdef SLAB_DEBUG
  337. static inline void * slab_alloc_debug (SLAB_MGR mgr, int size,
  338. const char * file, int line)
  339. {
  340. void * mem = slab_alloc(mgr, size);
  341. int i;
  342. int level = -1;
  343. for (i = 0 ; i < SLAB_LEVEL ; i++)
  344. if (size <= slab_levels[i]) {
  345. level = i;
  346. break;
  347. }
  348. if (level != -1) {
  349. struct slab_debug * debug =
  350. (struct slab_debug *) (mem + slab_levels[level] +
  351. SLAB_CANARY_SIZE);
  352. debug->alloc.file = file;
  353. debug->alloc.line = line;
  354. }
  355. return mem;
  356. }
  357. #endif
  358. // Returns user buffer size (i.e. excluding size of control structures).
  359. static inline size_t slab_get_buf_size(const void * ptr)
  360. {
  361. assert(ptr);
  362. unsigned char level = RAW_TO_LEVEL(ptr);
  363. if (level == (unsigned char) -1) {
  364. LARGE_MEM_OBJ mem = RAW_TO_OBJ(ptr, LARGE_MEM_OBJ_TYPE);
  365. return mem->size;
  366. }
  367. if (level >= SLAB_LEVEL) {
  368. pal_printf("Heap corruption detected: invalid heap level %u\n", level);
  369. __abort();
  370. }
  371. #ifdef SLAB_CANARY
  372. const unsigned long * m = (const unsigned long *)(ptr + slab_levels[level]);
  373. assert((*m) == SLAB_CANARY_STRING);
  374. #endif
  375. return slab_levels[level];
  376. }
  377. static inline void slab_free (SLAB_MGR mgr, void * obj)
  378. {
  379. /* In a general purpose allocator, free of NULL is allowed (and is a
  380. * nop). We might want to enforce stricter rules for our allocator if
  381. * we're sure that no clients rely on being able to free NULL. */
  382. if (!obj)
  383. return;
  384. unsigned char level = RAW_TO_LEVEL(obj);
  385. if (level == (unsigned char) -1) {
  386. LARGE_MEM_OBJ mem = RAW_TO_OBJ(obj, LARGE_MEM_OBJ_TYPE);
  387. system_free(mem, mem->size + sizeof(LARGE_MEM_OBJ_TYPE));
  388. return;
  389. }
  390. /* If this happens, either the heap is already corrupted, or someone's
  391. * freeing something that's wrong, which will most likely lead to heap
  392. * corruption. Either way, panic if this happens. TODO: this doesn't allow
  393. * us to detect cases where the heap headers have been zeroed, which
  394. * is a common type of heap corruption. We could make this case slightly
  395. * more likely to be detected by adding a non-zero offset to the level,
  396. * so a level of 0 in the header would no longer be a valid level. */
  397. if (level >= SLAB_LEVEL) {
  398. pal_printf("Heap corruption detected: invalid heap level %d\n", level);
  399. __abort();
  400. }
  401. #ifdef SLAB_CANARY
  402. unsigned long * m = (unsigned long *) (obj + slab_levels[level]);
  403. assert((*m) == SLAB_CANARY_STRING);
  404. #endif
  405. SLAB_OBJ mobj = RAW_TO_OBJ(obj, SLAB_OBJ_TYPE);
  406. system_lock();
  407. INIT_LIST_HEAD(mobj, __list);
  408. listp_add_tail(mobj, &mgr->free_list[level], __list);
  409. system_unlock();
  410. }
  411. #ifdef SLAB_DEBUG
  412. static inline void slab_free_debug (SLAB_MGR mgr, void * obj,
  413. const char * file, int line)
  414. {
  415. if (!obj)
  416. return;
  417. unsigned char level = RAW_TO_LEVEL(obj);
  418. if (level < SLAB_LEVEL && level != (unsigned char) -1) {
  419. struct slab_debug * debug =
  420. (struct slab_debug *) (obj + slab_levels[level] +
  421. SLAB_CANARY_SIZE);
  422. debug->free.file = file;
  423. debug->free.line = line;
  424. }
  425. slab_free(mgr, obj);
  426. }
  427. #endif
  428. #endif /* SLABMGR_H */