zsmalloc.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582
  1. /*
  2. * zsmalloc memory allocator
  3. *
  4. * Copyright (C) 2011 Nitin Gupta
  5. * Copyright (C) 2012, 2013 Minchan Kim
  6. *
  7. * This code is released using a dual license strategy: BSD/GPL
  8. * You can choose the license that better fits your requirements.
  9. *
  10. * Released under the terms of 3-clause BSD License
  11. * Released under the terms of GNU General Public License Version 2.0
  12. */
  13. /*
  14. * Following is how we use various fields and flags of underlying
  15. * struct page(s) to form a zspage.
  16. *
  17. * Usage of struct page fields:
  18. * page->private: points to zspage
  19. * page->freelist(index): links together all component pages of a zspage
  20. * For the huge page, this is always 0, so we use this field
  21. * to store handle.
  22. *
  23. * Usage of struct page flags:
  24. * PG_private: identifies the first component page
  25. * PG_private2: identifies the last component page
  26. * PG_owner_priv_1: indentifies the huge component page
  27. *
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/sched.h>
  33. #include <linux/bitops.h>
  34. #include <linux/errno.h>
  35. #include <linux/highmem.h>
  36. #include <linux/string.h>
  37. #include <linux/slab.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/pgtable.h>
  40. #include <linux/cpumask.h>
  41. #include <linux/cpu.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/preempt.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/types.h>
  46. #include <linux/debugfs.h>
  47. #include <linux/zsmalloc.h>
  48. #include <linux/zpool.h>
  49. #include <linux/mount.h>
  50. #include <linux/compaction.h>
  51. #include <linux/pagemap.h>
  52. #define ZSPAGE_MAGIC 0x58
  53. /*
  54. * This must be power of 2 and greater than of equal to sizeof(link_free).
  55. * These two conditions ensure that any 'struct link_free' itself doesn't
  56. * span more than 1 page which avoids complex case of mapping 2 pages simply
  57. * to restore link_free pointer values.
  58. */
  59. #define ZS_ALIGN 8
  60. /*
  61. * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
  62. * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
  63. */
  64. #define ZS_MAX_ZSPAGE_ORDER 2
  65. #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
  66. #define ZS_HANDLE_SIZE (sizeof(unsigned long))
  67. /*
  68. * Object location (<PFN>, <obj_idx>) is encoded as
  69. * as single (unsigned long) handle value.
  70. *
  71. * Note that object index <obj_idx> starts from 0.
  72. *
  73. * This is made more complicated by various memory models and PAE.
  74. */
  75. #ifndef MAX_PHYSMEM_BITS
  76. #ifdef CONFIG_HIGHMEM64G
  77. #define MAX_PHYSMEM_BITS 36
  78. #else /* !CONFIG_HIGHMEM64G */
  79. /*
  80. * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
  81. * be PAGE_SHIFT
  82. */
  83. #define MAX_PHYSMEM_BITS BITS_PER_LONG
  84. #endif
  85. #endif
  86. #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
  87. /*
  88. * Memory for allocating for handle keeps object position by
  89. * encoding <page, obj_idx> and the encoded value has a room
  90. * in least bit(ie, look at obj_to_location).
  91. * We use the bit to synchronize between object access by
  92. * user and migration.
  93. */
  94. #define HANDLE_PIN_BIT 0
  95. /*
  96. * Head in allocated object should have OBJ_ALLOCATED_TAG
  97. * to identify the object was allocated or not.
  98. * It's okay to add the status bit in the least bit because
  99. * header keeps handle which is 4byte-aligned address so we
  100. * have room for two bit at least.
  101. */
  102. #define OBJ_ALLOCATED_TAG 1
  103. #define OBJ_TAG_BITS 1
  104. #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
  105. #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
  106. #define MAX(a, b) ((a) >= (b) ? (a) : (b))
  107. /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
  108. #define ZS_MIN_ALLOC_SIZE \
  109. MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
  110. /* each chunk includes extra space to keep handle */
  111. #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
  112. /*
  113. * On systems with 4K page size, this gives 255 size classes! There is a
  114. * trader-off here:
  115. * - Large number of size classes is potentially wasteful as free page are
  116. * spread across these classes
  117. * - Small number of size classes causes large internal fragmentation
  118. * - Probably its better to use specific size classes (empirically
  119. * determined). NOTE: all those class sizes must be set as multiple of
  120. * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
  121. *
  122. * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
  123. * (reason above)
  124. */
  125. #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
  126. /*
  127. * We do not maintain any list for completely empty or full pages
  128. */
  129. enum fullness_group {
  130. ZS_EMPTY,
  131. ZS_ALMOST_EMPTY,
  132. ZS_ALMOST_FULL,
  133. ZS_FULL,
  134. NR_ZS_FULLNESS,
  135. };
  136. enum zs_stat_type {
  137. CLASS_EMPTY,
  138. CLASS_ALMOST_EMPTY,
  139. CLASS_ALMOST_FULL,
  140. CLASS_FULL,
  141. OBJ_ALLOCATED,
  142. OBJ_USED,
  143. NR_ZS_STAT_TYPE,
  144. };
  145. struct zs_size_stat {
  146. unsigned long objs[NR_ZS_STAT_TYPE];
  147. };
  148. #ifdef CONFIG_ZSMALLOC_STAT
  149. static struct dentry *zs_stat_root;
  150. #endif
  151. #ifdef CONFIG_COMPACTION
  152. static struct vfsmount *zsmalloc_mnt;
  153. #endif
  154. /*
  155. * number of size_classes
  156. */
  157. static int zs_size_classes;
  158. /*
  159. * We assign a page to ZS_ALMOST_EMPTY fullness group when:
  160. * n <= N / f, where
  161. * n = number of allocated objects
  162. * N = total number of objects zspage can store
  163. * f = fullness_threshold_frac
  164. *
  165. * Similarly, we assign zspage to:
  166. * ZS_ALMOST_FULL when n > N / f
  167. * ZS_EMPTY when n == 0
  168. * ZS_FULL when n == N
  169. *
  170. * (see: fix_fullness_group())
  171. */
  172. static const int fullness_threshold_frac = 4;
  173. struct size_class {
  174. spinlock_t lock;
  175. struct list_head fullness_list[NR_ZS_FULLNESS];
  176. /*
  177. * Size of objects stored in this class. Must be multiple
  178. * of ZS_ALIGN.
  179. */
  180. int size;
  181. int objs_per_zspage;
  182. /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
  183. int pages_per_zspage;
  184. unsigned int index;
  185. struct zs_size_stat stats;
  186. };
  187. /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
  188. static void SetPageHugeObject(struct page *page)
  189. {
  190. SetPageOwnerPriv1(page);
  191. }
  192. static void ClearPageHugeObject(struct page *page)
  193. {
  194. ClearPageOwnerPriv1(page);
  195. }
  196. static int PageHugeObject(struct page *page)
  197. {
  198. return PageOwnerPriv1(page);
  199. }
  200. /*
  201. * Placed within free objects to form a singly linked list.
  202. * For every zspage, zspage->freeobj gives head of this list.
  203. *
  204. * This must be power of 2 and less than or equal to ZS_ALIGN
  205. */
  206. struct link_free {
  207. union {
  208. /*
  209. * Free object index;
  210. * It's valid for non-allocated object
  211. */
  212. unsigned long next;
  213. /*
  214. * Handle of allocated object.
  215. */
  216. unsigned long handle;
  217. };
  218. };
  219. struct zs_pool {
  220. const char *name;
  221. struct size_class **size_class;
  222. struct kmem_cache *handle_cachep;
  223. struct kmem_cache *zspage_cachep;
  224. atomic_long_t pages_allocated;
  225. struct zs_pool_stats stats;
  226. /* Compact classes */
  227. struct shrinker shrinker;
  228. /*
  229. * To signify that register_shrinker() was successful
  230. * and unregister_shrinker() will not Oops.
  231. */
  232. bool shrinker_enabled;
  233. #ifdef CONFIG_ZSMALLOC_STAT
  234. struct dentry *stat_dentry;
  235. #endif
  236. #ifdef CONFIG_COMPACTION
  237. struct inode *inode;
  238. struct work_struct free_work;
  239. #endif
  240. };
  241. /*
  242. * A zspage's class index and fullness group
  243. * are encoded in its (first)page->mapping
  244. */
  245. #define FULLNESS_BITS 2
  246. #define CLASS_BITS 8
  247. #define ISOLATED_BITS 3
  248. #define MAGIC_VAL_BITS 8
  249. struct zspage {
  250. struct {
  251. unsigned int fullness:FULLNESS_BITS;
  252. unsigned int class:CLASS_BITS;
  253. unsigned int isolated:ISOLATED_BITS;
  254. unsigned int magic:MAGIC_VAL_BITS;
  255. };
  256. unsigned int inuse;
  257. unsigned int freeobj;
  258. struct page *first_page;
  259. struct list_head list; /* fullness list */
  260. #ifdef CONFIG_COMPACTION
  261. rwlock_t lock;
  262. #endif
  263. };
  264. struct mapping_area {
  265. #ifdef CONFIG_PGTABLE_MAPPING
  266. struct vm_struct *vm; /* vm area for mapping object that span pages */
  267. #else
  268. char *vm_buf; /* copy buffer for objects that span pages */
  269. #endif
  270. char *vm_addr; /* address of kmap_atomic()'ed pages */
  271. enum zs_mapmode vm_mm; /* mapping mode */
  272. };
  273. #ifdef CONFIG_COMPACTION
  274. static int zs_register_migration(struct zs_pool *pool);
  275. static void zs_unregister_migration(struct zs_pool *pool);
  276. static void migrate_lock_init(struct zspage *zspage);
  277. static void migrate_read_lock(struct zspage *zspage);
  278. static void migrate_read_unlock(struct zspage *zspage);
  279. static void kick_deferred_free(struct zs_pool *pool);
  280. static void init_deferred_free(struct zs_pool *pool);
  281. static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
  282. #else
  283. static int zsmalloc_mount(void) { return 0; }
  284. static void zsmalloc_unmount(void) {}
  285. static int zs_register_migration(struct zs_pool *pool) { return 0; }
  286. static void zs_unregister_migration(struct zs_pool *pool) {}
  287. static void migrate_lock_init(struct zspage *zspage) {}
  288. static void migrate_read_lock(struct zspage *zspage) {}
  289. static void migrate_read_unlock(struct zspage *zspage) {}
  290. static void kick_deferred_free(struct zs_pool *pool) {}
  291. static void init_deferred_free(struct zs_pool *pool) {}
  292. static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
  293. #endif
  294. static int create_cache(struct zs_pool *pool)
  295. {
  296. pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
  297. 0, 0, NULL);
  298. if (!pool->handle_cachep)
  299. return 1;
  300. pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
  301. 0, 0, NULL);
  302. if (!pool->zspage_cachep) {
  303. kmem_cache_destroy(pool->handle_cachep);
  304. pool->handle_cachep = NULL;
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. static void destroy_cache(struct zs_pool *pool)
  310. {
  311. kmem_cache_destroy(pool->handle_cachep);
  312. kmem_cache_destroy(pool->zspage_cachep);
  313. }
  314. static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
  315. {
  316. return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
  317. gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
  318. }
  319. static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
  320. {
  321. kmem_cache_free(pool->handle_cachep, (void *)handle);
  322. }
  323. static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
  324. {
  325. return kmem_cache_alloc(pool->zspage_cachep,
  326. flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
  327. };
  328. static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
  329. {
  330. kmem_cache_free(pool->zspage_cachep, zspage);
  331. }
  332. static void record_obj(unsigned long handle, unsigned long obj)
  333. {
  334. /*
  335. * lsb of @obj represents handle lock while other bits
  336. * represent object value the handle is pointing so
  337. * updating shouldn't do store tearing.
  338. */
  339. WRITE_ONCE(*(unsigned long *)handle, obj);
  340. }
  341. /* zpool driver */
  342. #ifdef CONFIG_ZPOOL
  343. static void *zs_zpool_create(const char *name, gfp_t gfp,
  344. const struct zpool_ops *zpool_ops,
  345. struct zpool *zpool)
  346. {
  347. /*
  348. * Ignore global gfp flags: zs_malloc() may be invoked from
  349. * different contexts and its caller must provide a valid
  350. * gfp mask.
  351. */
  352. return zs_create_pool(name);
  353. }
  354. static void zs_zpool_destroy(void *pool)
  355. {
  356. zs_destroy_pool(pool);
  357. }
  358. static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
  359. unsigned long *handle)
  360. {
  361. *handle = zs_malloc(pool, size, gfp);
  362. return *handle ? 0 : -1;
  363. }
  364. static void zs_zpool_free(void *pool, unsigned long handle)
  365. {
  366. zs_free(pool, handle);
  367. }
  368. static int zs_zpool_shrink(void *pool, unsigned int pages,
  369. unsigned int *reclaimed)
  370. {
  371. return -EINVAL;
  372. }
  373. static void *zs_zpool_map(void *pool, unsigned long handle,
  374. enum zpool_mapmode mm)
  375. {
  376. enum zs_mapmode zs_mm;
  377. switch (mm) {
  378. case ZPOOL_MM_RO:
  379. zs_mm = ZS_MM_RO;
  380. break;
  381. case ZPOOL_MM_WO:
  382. zs_mm = ZS_MM_WO;
  383. break;
  384. case ZPOOL_MM_RW: /* fallthru */
  385. default:
  386. zs_mm = ZS_MM_RW;
  387. break;
  388. }
  389. return zs_map_object(pool, handle, zs_mm);
  390. }
  391. static void zs_zpool_unmap(void *pool, unsigned long handle)
  392. {
  393. zs_unmap_object(pool, handle);
  394. }
  395. static u64 zs_zpool_total_size(void *pool)
  396. {
  397. return zs_get_total_pages(pool) << PAGE_SHIFT;
  398. }
  399. static struct zpool_driver zs_zpool_driver = {
  400. .type = "zsmalloc",
  401. .owner = THIS_MODULE,
  402. .create = zs_zpool_create,
  403. .destroy = zs_zpool_destroy,
  404. .malloc = zs_zpool_malloc,
  405. .free = zs_zpool_free,
  406. .shrink = zs_zpool_shrink,
  407. .map = zs_zpool_map,
  408. .unmap = zs_zpool_unmap,
  409. .total_size = zs_zpool_total_size,
  410. };
  411. MODULE_ALIAS("zpool-zsmalloc");
  412. #endif /* CONFIG_ZPOOL */
  413. static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
  414. {
  415. return pages_per_zspage * PAGE_SIZE / size;
  416. }
  417. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  418. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  419. static bool is_zspage_isolated(struct zspage *zspage)
  420. {
  421. return zspage->isolated;
  422. }
  423. static int is_first_page(struct page *page)
  424. {
  425. return PagePrivate(page);
  426. }
  427. /* Protected by class->lock */
  428. static inline int get_zspage_inuse(struct zspage *zspage)
  429. {
  430. return zspage->inuse;
  431. }
  432. static inline void set_zspage_inuse(struct zspage *zspage, int val)
  433. {
  434. zspage->inuse = val;
  435. }
  436. static inline void mod_zspage_inuse(struct zspage *zspage, int val)
  437. {
  438. zspage->inuse += val;
  439. }
  440. static inline struct page *get_first_page(struct zspage *zspage)
  441. {
  442. struct page *first_page = zspage->first_page;
  443. VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
  444. return first_page;
  445. }
  446. static inline int get_first_obj_offset(struct page *page)
  447. {
  448. return page->units;
  449. }
  450. static inline void set_first_obj_offset(struct page *page, int offset)
  451. {
  452. page->units = offset;
  453. }
  454. static inline unsigned int get_freeobj(struct zspage *zspage)
  455. {
  456. return zspage->freeobj;
  457. }
  458. static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
  459. {
  460. zspage->freeobj = obj;
  461. }
  462. static void get_zspage_mapping(struct zspage *zspage,
  463. unsigned int *class_idx,
  464. enum fullness_group *fullness)
  465. {
  466. BUG_ON(zspage->magic != ZSPAGE_MAGIC);
  467. *fullness = zspage->fullness;
  468. *class_idx = zspage->class;
  469. }
  470. static void set_zspage_mapping(struct zspage *zspage,
  471. unsigned int class_idx,
  472. enum fullness_group fullness)
  473. {
  474. zspage->class = class_idx;
  475. zspage->fullness = fullness;
  476. }
  477. /*
  478. * zsmalloc divides the pool into various size classes where each
  479. * class maintains a list of zspages where each zspage is divided
  480. * into equal sized chunks. Each allocation falls into one of these
  481. * classes depending on its size. This function returns index of the
  482. * size class which has chunk size big enough to hold the give size.
  483. */
  484. static int get_size_class_index(int size)
  485. {
  486. int idx = 0;
  487. if (likely(size > ZS_MIN_ALLOC_SIZE))
  488. idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
  489. ZS_SIZE_CLASS_DELTA);
  490. return min(zs_size_classes - 1, idx);
  491. }
  492. static inline void zs_stat_inc(struct size_class *class,
  493. enum zs_stat_type type, unsigned long cnt)
  494. {
  495. class->stats.objs[type] += cnt;
  496. }
  497. static inline void zs_stat_dec(struct size_class *class,
  498. enum zs_stat_type type, unsigned long cnt)
  499. {
  500. class->stats.objs[type] -= cnt;
  501. }
  502. static inline unsigned long zs_stat_get(struct size_class *class,
  503. enum zs_stat_type type)
  504. {
  505. return class->stats.objs[type];
  506. }
  507. #ifdef CONFIG_ZSMALLOC_STAT
  508. static void __init zs_stat_init(void)
  509. {
  510. if (!debugfs_initialized()) {
  511. pr_warn("debugfs not available, stat dir not created\n");
  512. return;
  513. }
  514. zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
  515. if (!zs_stat_root)
  516. pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
  517. }
  518. static void __exit zs_stat_exit(void)
  519. {
  520. debugfs_remove_recursive(zs_stat_root);
  521. }
  522. static unsigned long zs_can_compact(struct size_class *class);
  523. static int zs_stats_size_show(struct seq_file *s, void *v)
  524. {
  525. int i;
  526. struct zs_pool *pool = s->private;
  527. struct size_class *class;
  528. int objs_per_zspage;
  529. unsigned long class_almost_full, class_almost_empty;
  530. unsigned long obj_allocated, obj_used, pages_used, freeable;
  531. unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
  532. unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
  533. unsigned long total_freeable = 0;
  534. seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
  535. "class", "size", "almost_full", "almost_empty",
  536. "obj_allocated", "obj_used", "pages_used",
  537. "pages_per_zspage", "freeable");
  538. for (i = 0; i < zs_size_classes; i++) {
  539. class = pool->size_class[i];
  540. if (class->index != i)
  541. continue;
  542. spin_lock(&class->lock);
  543. class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
  544. class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
  545. obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
  546. obj_used = zs_stat_get(class, OBJ_USED);
  547. freeable = zs_can_compact(class);
  548. spin_unlock(&class->lock);
  549. objs_per_zspage = get_maxobj_per_zspage(class->size,
  550. class->pages_per_zspage);
  551. pages_used = obj_allocated / objs_per_zspage *
  552. class->pages_per_zspage;
  553. seq_printf(s, " %5u %5u %11lu %12lu %13lu"
  554. " %10lu %10lu %16d %8lu\n",
  555. i, class->size, class_almost_full, class_almost_empty,
  556. obj_allocated, obj_used, pages_used,
  557. class->pages_per_zspage, freeable);
  558. total_class_almost_full += class_almost_full;
  559. total_class_almost_empty += class_almost_empty;
  560. total_objs += obj_allocated;
  561. total_used_objs += obj_used;
  562. total_pages += pages_used;
  563. total_freeable += freeable;
  564. }
  565. seq_puts(s, "\n");
  566. seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
  567. "Total", "", total_class_almost_full,
  568. total_class_almost_empty, total_objs,
  569. total_used_objs, total_pages, "", total_freeable);
  570. return 0;
  571. }
  572. static int zs_stats_size_open(struct inode *inode, struct file *file)
  573. {
  574. return single_open(file, zs_stats_size_show, inode->i_private);
  575. }
  576. static const struct file_operations zs_stat_size_ops = {
  577. .open = zs_stats_size_open,
  578. .read = seq_read,
  579. .llseek = seq_lseek,
  580. .release = single_release,
  581. };
  582. static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
  583. {
  584. struct dentry *entry;
  585. if (!zs_stat_root) {
  586. pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
  587. return;
  588. }
  589. entry = debugfs_create_dir(name, zs_stat_root);
  590. if (!entry) {
  591. pr_warn("debugfs dir <%s> creation failed\n", name);
  592. return;
  593. }
  594. pool->stat_dentry = entry;
  595. entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
  596. pool->stat_dentry, pool, &zs_stat_size_ops);
  597. if (!entry) {
  598. pr_warn("%s: debugfs file entry <%s> creation failed\n",
  599. name, "classes");
  600. debugfs_remove_recursive(pool->stat_dentry);
  601. pool->stat_dentry = NULL;
  602. }
  603. }
  604. static void zs_pool_stat_destroy(struct zs_pool *pool)
  605. {
  606. debugfs_remove_recursive(pool->stat_dentry);
  607. }
  608. #else /* CONFIG_ZSMALLOC_STAT */
  609. static void __init zs_stat_init(void)
  610. {
  611. }
  612. static void __exit zs_stat_exit(void)
  613. {
  614. }
  615. static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
  616. {
  617. }
  618. static inline void zs_pool_stat_destroy(struct zs_pool *pool)
  619. {
  620. }
  621. #endif
  622. /*
  623. * For each size class, zspages are divided into different groups
  624. * depending on how "full" they are. This was done so that we could
  625. * easily find empty or nearly empty zspages when we try to shrink
  626. * the pool (not yet implemented). This function returns fullness
  627. * status of the given page.
  628. */
  629. static enum fullness_group get_fullness_group(struct size_class *class,
  630. struct zspage *zspage)
  631. {
  632. int inuse, objs_per_zspage;
  633. enum fullness_group fg;
  634. inuse = get_zspage_inuse(zspage);
  635. objs_per_zspage = class->objs_per_zspage;
  636. if (inuse == 0)
  637. fg = ZS_EMPTY;
  638. else if (inuse == objs_per_zspage)
  639. fg = ZS_FULL;
  640. else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
  641. fg = ZS_ALMOST_EMPTY;
  642. else
  643. fg = ZS_ALMOST_FULL;
  644. return fg;
  645. }
  646. /*
  647. * Each size class maintains various freelists and zspages are assigned
  648. * to one of these freelists based on the number of live objects they
  649. * have. This functions inserts the given zspage into the freelist
  650. * identified by <class, fullness_group>.
  651. */
  652. static void insert_zspage(struct size_class *class,
  653. struct zspage *zspage,
  654. enum fullness_group fullness)
  655. {
  656. struct zspage *head;
  657. zs_stat_inc(class, fullness, 1);
  658. head = list_first_entry_or_null(&class->fullness_list[fullness],
  659. struct zspage, list);
  660. /*
  661. * We want to see more ZS_FULL pages and less almost empty/full.
  662. * Put pages with higher ->inuse first.
  663. */
  664. if (head) {
  665. if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
  666. list_add(&zspage->list, &head->list);
  667. return;
  668. }
  669. }
  670. list_add(&zspage->list, &class->fullness_list[fullness]);
  671. }
  672. /*
  673. * This function removes the given zspage from the freelist identified
  674. * by <class, fullness_group>.
  675. */
  676. static void remove_zspage(struct size_class *class,
  677. struct zspage *zspage,
  678. enum fullness_group fullness)
  679. {
  680. VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
  681. VM_BUG_ON(is_zspage_isolated(zspage));
  682. list_del_init(&zspage->list);
  683. zs_stat_dec(class, fullness, 1);
  684. }
  685. /*
  686. * Each size class maintains zspages in different fullness groups depending
  687. * on the number of live objects they contain. When allocating or freeing
  688. * objects, the fullness status of the page can change, say, from ALMOST_FULL
  689. * to ALMOST_EMPTY when freeing an object. This function checks if such
  690. * a status change has occurred for the given page and accordingly moves the
  691. * page from the freelist of the old fullness group to that of the new
  692. * fullness group.
  693. */
  694. static enum fullness_group fix_fullness_group(struct size_class *class,
  695. struct zspage *zspage)
  696. {
  697. int class_idx;
  698. enum fullness_group currfg, newfg;
  699. get_zspage_mapping(zspage, &class_idx, &currfg);
  700. newfg = get_fullness_group(class, zspage);
  701. if (newfg == currfg)
  702. goto out;
  703. if (!is_zspage_isolated(zspage)) {
  704. remove_zspage(class, zspage, currfg);
  705. insert_zspage(class, zspage, newfg);
  706. }
  707. set_zspage_mapping(zspage, class_idx, newfg);
  708. out:
  709. return newfg;
  710. }
  711. /*
  712. * We have to decide on how many pages to link together
  713. * to form a zspage for each size class. This is important
  714. * to reduce wastage due to unusable space left at end of
  715. * each zspage which is given as:
  716. * wastage = Zp % class_size
  717. * usage = Zp - wastage
  718. * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
  719. *
  720. * For example, for size class of 3/8 * PAGE_SIZE, we should
  721. * link together 3 PAGE_SIZE sized pages to form a zspage
  722. * since then we can perfectly fit in 8 such objects.
  723. */
  724. static int get_pages_per_zspage(int class_size)
  725. {
  726. int i, max_usedpc = 0;
  727. /* zspage order which gives maximum used size per KB */
  728. int max_usedpc_order = 1;
  729. for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
  730. int zspage_size;
  731. int waste, usedpc;
  732. zspage_size = i * PAGE_SIZE;
  733. waste = zspage_size % class_size;
  734. usedpc = (zspage_size - waste) * 100 / zspage_size;
  735. if (usedpc > max_usedpc) {
  736. max_usedpc = usedpc;
  737. max_usedpc_order = i;
  738. }
  739. }
  740. return max_usedpc_order;
  741. }
  742. static struct zspage *get_zspage(struct page *page)
  743. {
  744. struct zspage *zspage = (struct zspage *)page->private;
  745. BUG_ON(zspage->magic != ZSPAGE_MAGIC);
  746. return zspage;
  747. }
  748. static struct page *get_next_page(struct page *page)
  749. {
  750. if (unlikely(PageHugeObject(page)))
  751. return NULL;
  752. return page->freelist;
  753. }
  754. /**
  755. * obj_to_location - get (<page>, <obj_idx>) from encoded object value
  756. * @page: page object resides in zspage
  757. * @obj_idx: object index
  758. */
  759. static void obj_to_location(unsigned long obj, struct page **page,
  760. unsigned int *obj_idx)
  761. {
  762. obj >>= OBJ_TAG_BITS;
  763. *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
  764. *obj_idx = (obj & OBJ_INDEX_MASK);
  765. }
  766. /**
  767. * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
  768. * @page: page object resides in zspage
  769. * @obj_idx: object index
  770. */
  771. static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
  772. {
  773. unsigned long obj;
  774. obj = page_to_pfn(page) << OBJ_INDEX_BITS;
  775. obj |= obj_idx & OBJ_INDEX_MASK;
  776. obj <<= OBJ_TAG_BITS;
  777. return obj;
  778. }
  779. static unsigned long handle_to_obj(unsigned long handle)
  780. {
  781. return *(unsigned long *)handle;
  782. }
  783. static unsigned long obj_to_head(struct page *page, void *obj)
  784. {
  785. if (unlikely(PageHugeObject(page))) {
  786. VM_BUG_ON_PAGE(!is_first_page(page), page);
  787. return page->index;
  788. } else
  789. return *(unsigned long *)obj;
  790. }
  791. static inline int testpin_tag(unsigned long handle)
  792. {
  793. return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
  794. }
  795. static inline int trypin_tag(unsigned long handle)
  796. {
  797. return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
  798. }
  799. static void pin_tag(unsigned long handle)
  800. {
  801. bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
  802. }
  803. static void unpin_tag(unsigned long handle)
  804. {
  805. bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
  806. }
  807. static void reset_page(struct page *page)
  808. {
  809. __ClearPageMovable(page);
  810. clear_bit(PG_private, &page->flags);
  811. clear_bit(PG_private_2, &page->flags);
  812. set_page_private(page, 0);
  813. page_mapcount_reset(page);
  814. ClearPageHugeObject(page);
  815. page->freelist = NULL;
  816. }
  817. /*
  818. * To prevent zspage destroy during migration, zspage freeing should
  819. * hold locks of all pages in the zspage.
  820. */
  821. void lock_zspage(struct zspage *zspage)
  822. {
  823. struct page *page = get_first_page(zspage);
  824. do {
  825. lock_page(page);
  826. } while ((page = get_next_page(page)) != NULL);
  827. }
  828. int trylock_zspage(struct zspage *zspage)
  829. {
  830. struct page *cursor, *fail;
  831. for (cursor = get_first_page(zspage); cursor != NULL; cursor =
  832. get_next_page(cursor)) {
  833. if (!trylock_page(cursor)) {
  834. fail = cursor;
  835. goto unlock;
  836. }
  837. }
  838. return 1;
  839. unlock:
  840. for (cursor = get_first_page(zspage); cursor != fail; cursor =
  841. get_next_page(cursor))
  842. unlock_page(cursor);
  843. return 0;
  844. }
  845. static void __free_zspage(struct zs_pool *pool, struct size_class *class,
  846. struct zspage *zspage)
  847. {
  848. struct page *page, *next;
  849. enum fullness_group fg;
  850. unsigned int class_idx;
  851. get_zspage_mapping(zspage, &class_idx, &fg);
  852. assert_spin_locked(&class->lock);
  853. VM_BUG_ON(get_zspage_inuse(zspage));
  854. VM_BUG_ON(fg != ZS_EMPTY);
  855. next = page = get_first_page(zspage);
  856. do {
  857. VM_BUG_ON_PAGE(!PageLocked(page), page);
  858. next = get_next_page(page);
  859. reset_page(page);
  860. unlock_page(page);
  861. dec_zone_page_state(page, NR_ZSPAGES);
  862. put_page(page);
  863. page = next;
  864. } while (page != NULL);
  865. cache_free_zspage(pool, zspage);
  866. zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  867. class->size, class->pages_per_zspage));
  868. atomic_long_sub(class->pages_per_zspage,
  869. &pool->pages_allocated);
  870. }
  871. static void free_zspage(struct zs_pool *pool, struct size_class *class,
  872. struct zspage *zspage)
  873. {
  874. VM_BUG_ON(get_zspage_inuse(zspage));
  875. VM_BUG_ON(list_empty(&zspage->list));
  876. if (!trylock_zspage(zspage)) {
  877. kick_deferred_free(pool);
  878. return;
  879. }
  880. remove_zspage(class, zspage, ZS_EMPTY);
  881. __free_zspage(pool, class, zspage);
  882. }
  883. /* Initialize a newly allocated zspage */
  884. static void init_zspage(struct size_class *class, struct zspage *zspage)
  885. {
  886. unsigned int freeobj = 1;
  887. unsigned long off = 0;
  888. struct page *page = get_first_page(zspage);
  889. while (page) {
  890. struct page *next_page;
  891. struct link_free *link;
  892. void *vaddr;
  893. set_first_obj_offset(page, off);
  894. vaddr = kmap_atomic(page);
  895. link = (struct link_free *)vaddr + off / sizeof(*link);
  896. while ((off += class->size) < PAGE_SIZE) {
  897. link->next = freeobj++ << OBJ_TAG_BITS;
  898. link += class->size / sizeof(*link);
  899. }
  900. /*
  901. * We now come to the last (full or partial) object on this
  902. * page, which must point to the first object on the next
  903. * page (if present)
  904. */
  905. next_page = get_next_page(page);
  906. if (next_page) {
  907. link->next = freeobj++ << OBJ_TAG_BITS;
  908. } else {
  909. /*
  910. * Reset OBJ_TAG_BITS bit to last link to tell
  911. * whether it's allocated object or not.
  912. */
  913. link->next = -1 << OBJ_TAG_BITS;
  914. }
  915. kunmap_atomic(vaddr);
  916. page = next_page;
  917. off %= PAGE_SIZE;
  918. }
  919. set_freeobj(zspage, 0);
  920. }
  921. static void create_page_chain(struct size_class *class, struct zspage *zspage,
  922. struct page *pages[])
  923. {
  924. int i;
  925. struct page *page;
  926. struct page *prev_page = NULL;
  927. int nr_pages = class->pages_per_zspage;
  928. /*
  929. * Allocate individual pages and link them together as:
  930. * 1. all pages are linked together using page->freelist
  931. * 2. each sub-page point to zspage using page->private
  932. *
  933. * we set PG_private to identify the first page (i.e. no other sub-page
  934. * has this flag set) and PG_private_2 to identify the last page.
  935. */
  936. for (i = 0; i < nr_pages; i++) {
  937. page = pages[i];
  938. set_page_private(page, (unsigned long)zspage);
  939. page->freelist = NULL;
  940. if (i == 0) {
  941. zspage->first_page = page;
  942. SetPagePrivate(page);
  943. if (unlikely(class->objs_per_zspage == 1 &&
  944. class->pages_per_zspage == 1))
  945. SetPageHugeObject(page);
  946. } else {
  947. prev_page->freelist = page;
  948. }
  949. if (i == nr_pages - 1)
  950. SetPagePrivate2(page);
  951. prev_page = page;
  952. }
  953. }
  954. /*
  955. * Allocate a zspage for the given size class
  956. */
  957. static struct zspage *alloc_zspage(struct zs_pool *pool,
  958. struct size_class *class,
  959. gfp_t gfp)
  960. {
  961. int i;
  962. struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
  963. struct zspage *zspage = cache_alloc_zspage(pool, gfp);
  964. if (!zspage)
  965. return NULL;
  966. memset(zspage, 0, sizeof(struct zspage));
  967. zspage->magic = ZSPAGE_MAGIC;
  968. migrate_lock_init(zspage);
  969. for (i = 0; i < class->pages_per_zspage; i++) {
  970. struct page *page;
  971. page = alloc_page(gfp);
  972. if (!page) {
  973. while (--i >= 0) {
  974. dec_zone_page_state(pages[i], NR_ZSPAGES);
  975. __free_page(pages[i]);
  976. }
  977. cache_free_zspage(pool, zspage);
  978. return NULL;
  979. }
  980. inc_zone_page_state(page, NR_ZSPAGES);
  981. pages[i] = page;
  982. }
  983. create_page_chain(class, zspage, pages);
  984. init_zspage(class, zspage);
  985. return zspage;
  986. }
  987. static struct zspage *find_get_zspage(struct size_class *class)
  988. {
  989. int i;
  990. struct zspage *zspage;
  991. for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
  992. zspage = list_first_entry_or_null(&class->fullness_list[i],
  993. struct zspage, list);
  994. if (zspage)
  995. break;
  996. }
  997. return zspage;
  998. }
  999. #ifdef CONFIG_PGTABLE_MAPPING
  1000. static inline int __zs_cpu_up(struct mapping_area *area)
  1001. {
  1002. /*
  1003. * Make sure we don't leak memory if a cpu UP notification
  1004. * and zs_init() race and both call zs_cpu_up() on the same cpu
  1005. */
  1006. if (area->vm)
  1007. return 0;
  1008. area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
  1009. if (!area->vm)
  1010. return -ENOMEM;
  1011. return 0;
  1012. }
  1013. static inline void __zs_cpu_down(struct mapping_area *area)
  1014. {
  1015. if (area->vm)
  1016. free_vm_area(area->vm);
  1017. area->vm = NULL;
  1018. }
  1019. static inline void *__zs_map_object(struct mapping_area *area,
  1020. struct page *pages[2], int off, int size)
  1021. {
  1022. BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
  1023. area->vm_addr = area->vm->addr;
  1024. return area->vm_addr + off;
  1025. }
  1026. static inline void __zs_unmap_object(struct mapping_area *area,
  1027. struct page *pages[2], int off, int size)
  1028. {
  1029. unsigned long addr = (unsigned long)area->vm_addr;
  1030. unmap_kernel_range(addr, PAGE_SIZE * 2);
  1031. }
  1032. #else /* CONFIG_PGTABLE_MAPPING */
  1033. static inline int __zs_cpu_up(struct mapping_area *area)
  1034. {
  1035. /*
  1036. * Make sure we don't leak memory if a cpu UP notification
  1037. * and zs_init() race and both call zs_cpu_up() on the same cpu
  1038. */
  1039. if (area->vm_buf)
  1040. return 0;
  1041. area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
  1042. if (!area->vm_buf)
  1043. return -ENOMEM;
  1044. return 0;
  1045. }
  1046. static inline void __zs_cpu_down(struct mapping_area *area)
  1047. {
  1048. kfree(area->vm_buf);
  1049. area->vm_buf = NULL;
  1050. }
  1051. static void *__zs_map_object(struct mapping_area *area,
  1052. struct page *pages[2], int off, int size)
  1053. {
  1054. int sizes[2];
  1055. void *addr;
  1056. char *buf = area->vm_buf;
  1057. /* disable page faults to match kmap_atomic() return conditions */
  1058. pagefault_disable();
  1059. /* no read fastpath */
  1060. if (area->vm_mm == ZS_MM_WO)
  1061. goto out;
  1062. sizes[0] = PAGE_SIZE - off;
  1063. sizes[1] = size - sizes[0];
  1064. /* copy object to per-cpu buffer */
  1065. addr = kmap_atomic(pages[0]);
  1066. memcpy(buf, addr + off, sizes[0]);
  1067. kunmap_atomic(addr);
  1068. addr = kmap_atomic(pages[1]);
  1069. memcpy(buf + sizes[0], addr, sizes[1]);
  1070. kunmap_atomic(addr);
  1071. out:
  1072. return area->vm_buf;
  1073. }
  1074. static void __zs_unmap_object(struct mapping_area *area,
  1075. struct page *pages[2], int off, int size)
  1076. {
  1077. int sizes[2];
  1078. void *addr;
  1079. char *buf;
  1080. /* no write fastpath */
  1081. if (area->vm_mm == ZS_MM_RO)
  1082. goto out;
  1083. buf = area->vm_buf;
  1084. buf = buf + ZS_HANDLE_SIZE;
  1085. size -= ZS_HANDLE_SIZE;
  1086. off += ZS_HANDLE_SIZE;
  1087. sizes[0] = PAGE_SIZE - off;
  1088. sizes[1] = size - sizes[0];
  1089. /* copy per-cpu buffer to object */
  1090. addr = kmap_atomic(pages[0]);
  1091. memcpy(addr + off, buf, sizes[0]);
  1092. kunmap_atomic(addr);
  1093. addr = kmap_atomic(pages[1]);
  1094. memcpy(addr, buf + sizes[0], sizes[1]);
  1095. kunmap_atomic(addr);
  1096. out:
  1097. /* enable page faults to match kunmap_atomic() return conditions */
  1098. pagefault_enable();
  1099. }
  1100. #endif /* CONFIG_PGTABLE_MAPPING */
  1101. static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
  1102. void *pcpu)
  1103. {
  1104. int ret, cpu = (long)pcpu;
  1105. struct mapping_area *area;
  1106. switch (action) {
  1107. case CPU_UP_PREPARE:
  1108. area = &per_cpu(zs_map_area, cpu);
  1109. ret = __zs_cpu_up(area);
  1110. if (ret)
  1111. return notifier_from_errno(ret);
  1112. break;
  1113. case CPU_DEAD:
  1114. case CPU_UP_CANCELED:
  1115. area = &per_cpu(zs_map_area, cpu);
  1116. __zs_cpu_down(area);
  1117. break;
  1118. }
  1119. return NOTIFY_OK;
  1120. }
  1121. static struct notifier_block zs_cpu_nb = {
  1122. .notifier_call = zs_cpu_notifier
  1123. };
  1124. static int zs_register_cpu_notifier(void)
  1125. {
  1126. int cpu, uninitialized_var(ret);
  1127. cpu_notifier_register_begin();
  1128. __register_cpu_notifier(&zs_cpu_nb);
  1129. for_each_online_cpu(cpu) {
  1130. ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  1131. if (notifier_to_errno(ret))
  1132. break;
  1133. }
  1134. cpu_notifier_register_done();
  1135. return notifier_to_errno(ret);
  1136. }
  1137. static void zs_unregister_cpu_notifier(void)
  1138. {
  1139. int cpu;
  1140. cpu_notifier_register_begin();
  1141. for_each_online_cpu(cpu)
  1142. zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
  1143. __unregister_cpu_notifier(&zs_cpu_nb);
  1144. cpu_notifier_register_done();
  1145. }
  1146. static void init_zs_size_classes(void)
  1147. {
  1148. int nr;
  1149. nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
  1150. if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
  1151. nr += 1;
  1152. zs_size_classes = nr;
  1153. }
  1154. static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
  1155. {
  1156. if (prev->pages_per_zspage != pages_per_zspage)
  1157. return false;
  1158. if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
  1159. != get_maxobj_per_zspage(size, pages_per_zspage))
  1160. return false;
  1161. return true;
  1162. }
  1163. static bool zspage_full(struct size_class *class, struct zspage *zspage)
  1164. {
  1165. return get_zspage_inuse(zspage) == class->objs_per_zspage;
  1166. }
  1167. unsigned long zs_get_total_pages(struct zs_pool *pool)
  1168. {
  1169. return atomic_long_read(&pool->pages_allocated);
  1170. }
  1171. EXPORT_SYMBOL_GPL(zs_get_total_pages);
  1172. /**
  1173. * zs_map_object - get address of allocated object from handle.
  1174. * @pool: pool from which the object was allocated
  1175. * @handle: handle returned from zs_malloc
  1176. *
  1177. * Before using an object allocated from zs_malloc, it must be mapped using
  1178. * this function. When done with the object, it must be unmapped using
  1179. * zs_unmap_object.
  1180. *
  1181. * Only one object can be mapped per cpu at a time. There is no protection
  1182. * against nested mappings.
  1183. *
  1184. * This function returns with preemption and page faults disabled.
  1185. */
  1186. void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  1187. enum zs_mapmode mm)
  1188. {
  1189. struct zspage *zspage;
  1190. struct page *page;
  1191. unsigned long obj, off;
  1192. unsigned int obj_idx;
  1193. unsigned int class_idx;
  1194. enum fullness_group fg;
  1195. struct size_class *class;
  1196. struct mapping_area *area;
  1197. struct page *pages[2];
  1198. void *ret;
  1199. /*
  1200. * Because we use per-cpu mapping areas shared among the
  1201. * pools/users, we can't allow mapping in interrupt context
  1202. * because it can corrupt another users mappings.
  1203. */
  1204. WARN_ON_ONCE(in_interrupt());
  1205. /* From now on, migration cannot move the object */
  1206. pin_tag(handle);
  1207. obj = handle_to_obj(handle);
  1208. obj_to_location(obj, &page, &obj_idx);
  1209. zspage = get_zspage(page);
  1210. /* migration cannot move any subpage in this zspage */
  1211. migrate_read_lock(zspage);
  1212. get_zspage_mapping(zspage, &class_idx, &fg);
  1213. class = pool->size_class[class_idx];
  1214. off = (class->size * obj_idx) & ~PAGE_MASK;
  1215. area = &get_cpu_var(zs_map_area);
  1216. area->vm_mm = mm;
  1217. if (off + class->size <= PAGE_SIZE) {
  1218. /* this object is contained entirely within a page */
  1219. area->vm_addr = kmap_atomic(page);
  1220. ret = area->vm_addr + off;
  1221. goto out;
  1222. }
  1223. /* this object spans two pages */
  1224. pages[0] = page;
  1225. pages[1] = get_next_page(page);
  1226. BUG_ON(!pages[1]);
  1227. ret = __zs_map_object(area, pages, off, class->size);
  1228. out:
  1229. if (likely(!PageHugeObject(page)))
  1230. ret += ZS_HANDLE_SIZE;
  1231. return ret;
  1232. }
  1233. EXPORT_SYMBOL_GPL(zs_map_object);
  1234. void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  1235. {
  1236. struct zspage *zspage;
  1237. struct page *page;
  1238. unsigned long obj, off;
  1239. unsigned int obj_idx;
  1240. unsigned int class_idx;
  1241. enum fullness_group fg;
  1242. struct size_class *class;
  1243. struct mapping_area *area;
  1244. obj = handle_to_obj(handle);
  1245. obj_to_location(obj, &page, &obj_idx);
  1246. zspage = get_zspage(page);
  1247. get_zspage_mapping(zspage, &class_idx, &fg);
  1248. class = pool->size_class[class_idx];
  1249. off = (class->size * obj_idx) & ~PAGE_MASK;
  1250. area = this_cpu_ptr(&zs_map_area);
  1251. if (off + class->size <= PAGE_SIZE)
  1252. kunmap_atomic(area->vm_addr);
  1253. else {
  1254. struct page *pages[2];
  1255. pages[0] = page;
  1256. pages[1] = get_next_page(page);
  1257. BUG_ON(!pages[1]);
  1258. __zs_unmap_object(area, pages, off, class->size);
  1259. }
  1260. put_cpu_var(zs_map_area);
  1261. migrate_read_unlock(zspage);
  1262. unpin_tag(handle);
  1263. }
  1264. EXPORT_SYMBOL_GPL(zs_unmap_object);
  1265. static unsigned long obj_malloc(struct size_class *class,
  1266. struct zspage *zspage, unsigned long handle)
  1267. {
  1268. int i, nr_page, offset;
  1269. unsigned long obj;
  1270. struct link_free *link;
  1271. struct page *m_page;
  1272. unsigned long m_offset;
  1273. void *vaddr;
  1274. handle |= OBJ_ALLOCATED_TAG;
  1275. obj = get_freeobj(zspage);
  1276. offset = obj * class->size;
  1277. nr_page = offset >> PAGE_SHIFT;
  1278. m_offset = offset & ~PAGE_MASK;
  1279. m_page = get_first_page(zspage);
  1280. for (i = 0; i < nr_page; i++)
  1281. m_page = get_next_page(m_page);
  1282. vaddr = kmap_atomic(m_page);
  1283. link = (struct link_free *)vaddr + m_offset / sizeof(*link);
  1284. set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
  1285. if (likely(!PageHugeObject(m_page)))
  1286. /* record handle in the header of allocated chunk */
  1287. link->handle = handle;
  1288. else
  1289. /* record handle to page->index */
  1290. zspage->first_page->index = handle;
  1291. kunmap_atomic(vaddr);
  1292. mod_zspage_inuse(zspage, 1);
  1293. zs_stat_inc(class, OBJ_USED, 1);
  1294. obj = location_to_obj(m_page, obj);
  1295. return obj;
  1296. }
  1297. /**
  1298. * zs_malloc - Allocate block of given size from pool.
  1299. * @pool: pool to allocate from
  1300. * @size: size of block to allocate
  1301. *
  1302. * On success, handle to the allocated object is returned,
  1303. * otherwise 0.
  1304. * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
  1305. */
  1306. unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
  1307. {
  1308. unsigned long handle, obj;
  1309. struct size_class *class;
  1310. enum fullness_group newfg;
  1311. struct zspage *zspage;
  1312. if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
  1313. return 0;
  1314. handle = cache_alloc_handle(pool, gfp);
  1315. if (!handle)
  1316. return 0;
  1317. /* extra space in chunk to keep the handle */
  1318. size += ZS_HANDLE_SIZE;
  1319. class = pool->size_class[get_size_class_index(size)];
  1320. spin_lock(&class->lock);
  1321. zspage = find_get_zspage(class);
  1322. if (likely(zspage)) {
  1323. obj = obj_malloc(class, zspage, handle);
  1324. /* Now move the zspage to another fullness group, if required */
  1325. fix_fullness_group(class, zspage);
  1326. record_obj(handle, obj);
  1327. spin_unlock(&class->lock);
  1328. return handle;
  1329. }
  1330. spin_unlock(&class->lock);
  1331. zspage = alloc_zspage(pool, class, gfp);
  1332. if (!zspage) {
  1333. cache_free_handle(pool, handle);
  1334. return 0;
  1335. }
  1336. spin_lock(&class->lock);
  1337. obj = obj_malloc(class, zspage, handle);
  1338. newfg = get_fullness_group(class, zspage);
  1339. insert_zspage(class, zspage, newfg);
  1340. set_zspage_mapping(zspage, class->index, newfg);
  1341. record_obj(handle, obj);
  1342. atomic_long_add(class->pages_per_zspage,
  1343. &pool->pages_allocated);
  1344. zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1345. class->size, class->pages_per_zspage));
  1346. /* We completely set up zspage so mark them as movable */
  1347. SetZsPageMovable(pool, zspage);
  1348. spin_unlock(&class->lock);
  1349. return handle;
  1350. }
  1351. EXPORT_SYMBOL_GPL(zs_malloc);
  1352. static void obj_free(struct size_class *class, unsigned long obj)
  1353. {
  1354. struct link_free *link;
  1355. struct zspage *zspage;
  1356. struct page *f_page;
  1357. unsigned long f_offset;
  1358. unsigned int f_objidx;
  1359. void *vaddr;
  1360. obj &= ~OBJ_ALLOCATED_TAG;
  1361. obj_to_location(obj, &f_page, &f_objidx);
  1362. f_offset = (class->size * f_objidx) & ~PAGE_MASK;
  1363. zspage = get_zspage(f_page);
  1364. vaddr = kmap_atomic(f_page);
  1365. /* Insert this object in containing zspage's freelist */
  1366. link = (struct link_free *)(vaddr + f_offset);
  1367. link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
  1368. kunmap_atomic(vaddr);
  1369. set_freeobj(zspage, f_objidx);
  1370. mod_zspage_inuse(zspage, -1);
  1371. zs_stat_dec(class, OBJ_USED, 1);
  1372. }
  1373. void zs_free(struct zs_pool *pool, unsigned long handle)
  1374. {
  1375. struct zspage *zspage;
  1376. struct page *f_page;
  1377. unsigned long obj;
  1378. unsigned int f_objidx;
  1379. int class_idx;
  1380. struct size_class *class;
  1381. enum fullness_group fullness;
  1382. bool isolated;
  1383. if (unlikely(!handle))
  1384. return;
  1385. pin_tag(handle);
  1386. obj = handle_to_obj(handle);
  1387. obj_to_location(obj, &f_page, &f_objidx);
  1388. zspage = get_zspage(f_page);
  1389. migrate_read_lock(zspage);
  1390. get_zspage_mapping(zspage, &class_idx, &fullness);
  1391. class = pool->size_class[class_idx];
  1392. spin_lock(&class->lock);
  1393. obj_free(class, obj);
  1394. fullness = fix_fullness_group(class, zspage);
  1395. if (fullness != ZS_EMPTY) {
  1396. migrate_read_unlock(zspage);
  1397. goto out;
  1398. }
  1399. isolated = is_zspage_isolated(zspage);
  1400. migrate_read_unlock(zspage);
  1401. /* If zspage is isolated, zs_page_putback will free the zspage */
  1402. if (likely(!isolated))
  1403. free_zspage(pool, class, zspage);
  1404. out:
  1405. spin_unlock(&class->lock);
  1406. unpin_tag(handle);
  1407. cache_free_handle(pool, handle);
  1408. }
  1409. EXPORT_SYMBOL_GPL(zs_free);
  1410. static void zs_object_copy(struct size_class *class, unsigned long dst,
  1411. unsigned long src)
  1412. {
  1413. struct page *s_page, *d_page;
  1414. unsigned int s_objidx, d_objidx;
  1415. unsigned long s_off, d_off;
  1416. void *s_addr, *d_addr;
  1417. int s_size, d_size, size;
  1418. int written = 0;
  1419. s_size = d_size = class->size;
  1420. obj_to_location(src, &s_page, &s_objidx);
  1421. obj_to_location(dst, &d_page, &d_objidx);
  1422. s_off = (class->size * s_objidx) & ~PAGE_MASK;
  1423. d_off = (class->size * d_objidx) & ~PAGE_MASK;
  1424. if (s_off + class->size > PAGE_SIZE)
  1425. s_size = PAGE_SIZE - s_off;
  1426. if (d_off + class->size > PAGE_SIZE)
  1427. d_size = PAGE_SIZE - d_off;
  1428. s_addr = kmap_atomic(s_page);
  1429. d_addr = kmap_atomic(d_page);
  1430. while (1) {
  1431. size = min(s_size, d_size);
  1432. memcpy(d_addr + d_off, s_addr + s_off, size);
  1433. written += size;
  1434. if (written == class->size)
  1435. break;
  1436. s_off += size;
  1437. s_size -= size;
  1438. d_off += size;
  1439. d_size -= size;
  1440. if (s_off >= PAGE_SIZE) {
  1441. kunmap_atomic(d_addr);
  1442. kunmap_atomic(s_addr);
  1443. s_page = get_next_page(s_page);
  1444. s_addr = kmap_atomic(s_page);
  1445. d_addr = kmap_atomic(d_page);
  1446. s_size = class->size - written;
  1447. s_off = 0;
  1448. }
  1449. if (d_off >= PAGE_SIZE) {
  1450. kunmap_atomic(d_addr);
  1451. d_page = get_next_page(d_page);
  1452. d_addr = kmap_atomic(d_page);
  1453. d_size = class->size - written;
  1454. d_off = 0;
  1455. }
  1456. }
  1457. kunmap_atomic(d_addr);
  1458. kunmap_atomic(s_addr);
  1459. }
  1460. /*
  1461. * Find alloced object in zspage from index object and
  1462. * return handle.
  1463. */
  1464. static unsigned long find_alloced_obj(struct size_class *class,
  1465. struct page *page, int index)
  1466. {
  1467. unsigned long head;
  1468. int offset = 0;
  1469. unsigned long handle = 0;
  1470. void *addr = kmap_atomic(page);
  1471. offset = get_first_obj_offset(page);
  1472. offset += class->size * index;
  1473. while (offset < PAGE_SIZE) {
  1474. head = obj_to_head(page, addr + offset);
  1475. if (head & OBJ_ALLOCATED_TAG) {
  1476. handle = head & ~OBJ_ALLOCATED_TAG;
  1477. if (trypin_tag(handle))
  1478. break;
  1479. handle = 0;
  1480. }
  1481. offset += class->size;
  1482. index++;
  1483. }
  1484. kunmap_atomic(addr);
  1485. return handle;
  1486. }
  1487. struct zs_compact_control {
  1488. /* Source spage for migration which could be a subpage of zspage */
  1489. struct page *s_page;
  1490. /* Destination page for migration which should be a first page
  1491. * of zspage. */
  1492. struct page *d_page;
  1493. /* Starting object index within @s_page which used for live object
  1494. * in the subpage. */
  1495. int index;
  1496. };
  1497. static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
  1498. struct zs_compact_control *cc)
  1499. {
  1500. unsigned long used_obj, free_obj;
  1501. unsigned long handle;
  1502. struct page *s_page = cc->s_page;
  1503. struct page *d_page = cc->d_page;
  1504. unsigned long index = cc->index;
  1505. int ret = 0;
  1506. while (1) {
  1507. handle = find_alloced_obj(class, s_page, index);
  1508. if (!handle) {
  1509. s_page = get_next_page(s_page);
  1510. if (!s_page)
  1511. break;
  1512. index = 0;
  1513. continue;
  1514. }
  1515. /* Stop if there is no more space */
  1516. if (zspage_full(class, get_zspage(d_page))) {
  1517. unpin_tag(handle);
  1518. ret = -ENOMEM;
  1519. break;
  1520. }
  1521. used_obj = handle_to_obj(handle);
  1522. free_obj = obj_malloc(class, get_zspage(d_page), handle);
  1523. zs_object_copy(class, free_obj, used_obj);
  1524. index++;
  1525. /*
  1526. * record_obj updates handle's value to free_obj and it will
  1527. * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
  1528. * breaks synchronization using pin_tag(e,g, zs_free) so
  1529. * let's keep the lock bit.
  1530. */
  1531. free_obj |= BIT(HANDLE_PIN_BIT);
  1532. record_obj(handle, free_obj);
  1533. unpin_tag(handle);
  1534. obj_free(class, used_obj);
  1535. }
  1536. /* Remember last position in this iteration */
  1537. cc->s_page = s_page;
  1538. cc->index = index;
  1539. return ret;
  1540. }
  1541. static struct zspage *isolate_zspage(struct size_class *class, bool source)
  1542. {
  1543. int i;
  1544. struct zspage *zspage;
  1545. enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
  1546. if (!source) {
  1547. fg[0] = ZS_ALMOST_FULL;
  1548. fg[1] = ZS_ALMOST_EMPTY;
  1549. }
  1550. for (i = 0; i < 2; i++) {
  1551. zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
  1552. struct zspage, list);
  1553. if (zspage) {
  1554. VM_BUG_ON(is_zspage_isolated(zspage));
  1555. remove_zspage(class, zspage, fg[i]);
  1556. return zspage;
  1557. }
  1558. }
  1559. return zspage;
  1560. }
  1561. /*
  1562. * putback_zspage - add @zspage into right class's fullness list
  1563. * @class: destination class
  1564. * @zspage: target page
  1565. *
  1566. * Return @zspage's fullness_group
  1567. */
  1568. static enum fullness_group putback_zspage(struct size_class *class,
  1569. struct zspage *zspage)
  1570. {
  1571. enum fullness_group fullness;
  1572. VM_BUG_ON(is_zspage_isolated(zspage));
  1573. fullness = get_fullness_group(class, zspage);
  1574. insert_zspage(class, zspage, fullness);
  1575. set_zspage_mapping(zspage, class->index, fullness);
  1576. return fullness;
  1577. }
  1578. #ifdef CONFIG_COMPACTION
  1579. static struct dentry *zs_mount(struct file_system_type *fs_type,
  1580. int flags, const char *dev_name, void *data)
  1581. {
  1582. static const struct dentry_operations ops = {
  1583. .d_dname = simple_dname,
  1584. };
  1585. return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
  1586. }
  1587. static struct file_system_type zsmalloc_fs = {
  1588. .name = "zsmalloc",
  1589. .mount = zs_mount,
  1590. .kill_sb = kill_anon_super,
  1591. };
  1592. static int zsmalloc_mount(void)
  1593. {
  1594. int ret = 0;
  1595. zsmalloc_mnt = kern_mount(&zsmalloc_fs);
  1596. if (IS_ERR(zsmalloc_mnt))
  1597. ret = PTR_ERR(zsmalloc_mnt);
  1598. return ret;
  1599. }
  1600. static void zsmalloc_unmount(void)
  1601. {
  1602. kern_unmount(zsmalloc_mnt);
  1603. }
  1604. static void migrate_lock_init(struct zspage *zspage)
  1605. {
  1606. rwlock_init(&zspage->lock);
  1607. }
  1608. static void migrate_read_lock(struct zspage *zspage)
  1609. {
  1610. read_lock(&zspage->lock);
  1611. }
  1612. static void migrate_read_unlock(struct zspage *zspage)
  1613. {
  1614. read_unlock(&zspage->lock);
  1615. }
  1616. static void migrate_write_lock(struct zspage *zspage)
  1617. {
  1618. write_lock(&zspage->lock);
  1619. }
  1620. static void migrate_write_unlock(struct zspage *zspage)
  1621. {
  1622. write_unlock(&zspage->lock);
  1623. }
  1624. /* Number of isolated subpage for *page migration* in this zspage */
  1625. static void inc_zspage_isolation(struct zspage *zspage)
  1626. {
  1627. zspage->isolated++;
  1628. }
  1629. static void dec_zspage_isolation(struct zspage *zspage)
  1630. {
  1631. zspage->isolated--;
  1632. }
  1633. static void replace_sub_page(struct size_class *class, struct zspage *zspage,
  1634. struct page *newpage, struct page *oldpage)
  1635. {
  1636. struct page *page;
  1637. struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
  1638. int idx = 0;
  1639. page = get_first_page(zspage);
  1640. do {
  1641. if (page == oldpage)
  1642. pages[idx] = newpage;
  1643. else
  1644. pages[idx] = page;
  1645. idx++;
  1646. } while ((page = get_next_page(page)) != NULL);
  1647. create_page_chain(class, zspage, pages);
  1648. set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
  1649. if (unlikely(PageHugeObject(oldpage)))
  1650. newpage->index = oldpage->index;
  1651. __SetPageMovable(newpage, page_mapping(oldpage));
  1652. }
  1653. bool zs_page_isolate(struct page *page, isolate_mode_t mode)
  1654. {
  1655. struct zs_pool *pool;
  1656. struct size_class *class;
  1657. int class_idx;
  1658. enum fullness_group fullness;
  1659. struct zspage *zspage;
  1660. struct address_space *mapping;
  1661. /*
  1662. * Page is locked so zspage couldn't be destroyed. For detail, look at
  1663. * lock_zspage in free_zspage.
  1664. */
  1665. VM_BUG_ON_PAGE(!PageMovable(page), page);
  1666. VM_BUG_ON_PAGE(PageIsolated(page), page);
  1667. zspage = get_zspage(page);
  1668. /*
  1669. * Without class lock, fullness could be stale while class_idx is okay
  1670. * because class_idx is constant unless page is freed so we should get
  1671. * fullness again under class lock.
  1672. */
  1673. get_zspage_mapping(zspage, &class_idx, &fullness);
  1674. mapping = page_mapping(page);
  1675. pool = mapping->private_data;
  1676. class = pool->size_class[class_idx];
  1677. spin_lock(&class->lock);
  1678. if (get_zspage_inuse(zspage) == 0) {
  1679. spin_unlock(&class->lock);
  1680. return false;
  1681. }
  1682. /* zspage is isolated for object migration */
  1683. if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
  1684. spin_unlock(&class->lock);
  1685. return false;
  1686. }
  1687. /*
  1688. * If this is first time isolation for the zspage, isolate zspage from
  1689. * size_class to prevent further object allocation from the zspage.
  1690. */
  1691. if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
  1692. get_zspage_mapping(zspage, &class_idx, &fullness);
  1693. remove_zspage(class, zspage, fullness);
  1694. }
  1695. inc_zspage_isolation(zspage);
  1696. spin_unlock(&class->lock);
  1697. return true;
  1698. }
  1699. int zs_page_migrate(struct address_space *mapping, struct page *newpage,
  1700. struct page *page, enum migrate_mode mode)
  1701. {
  1702. struct zs_pool *pool;
  1703. struct size_class *class;
  1704. int class_idx;
  1705. enum fullness_group fullness;
  1706. struct zspage *zspage;
  1707. struct page *dummy;
  1708. void *s_addr, *d_addr, *addr;
  1709. int offset, pos;
  1710. unsigned long handle, head;
  1711. unsigned long old_obj, new_obj;
  1712. unsigned int obj_idx;
  1713. int ret = -EAGAIN;
  1714. VM_BUG_ON_PAGE(!PageMovable(page), page);
  1715. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  1716. zspage = get_zspage(page);
  1717. /* Concurrent compactor cannot migrate any subpage in zspage */
  1718. migrate_write_lock(zspage);
  1719. get_zspage_mapping(zspage, &class_idx, &fullness);
  1720. pool = mapping->private_data;
  1721. class = pool->size_class[class_idx];
  1722. offset = get_first_obj_offset(page);
  1723. spin_lock(&class->lock);
  1724. if (!get_zspage_inuse(zspage)) {
  1725. ret = -EBUSY;
  1726. goto unlock_class;
  1727. }
  1728. pos = offset;
  1729. s_addr = kmap_atomic(page);
  1730. while (pos < PAGE_SIZE) {
  1731. head = obj_to_head(page, s_addr + pos);
  1732. if (head & OBJ_ALLOCATED_TAG) {
  1733. handle = head & ~OBJ_ALLOCATED_TAG;
  1734. if (!trypin_tag(handle))
  1735. goto unpin_objects;
  1736. }
  1737. pos += class->size;
  1738. }
  1739. /*
  1740. * Here, any user cannot access all objects in the zspage so let's move.
  1741. */
  1742. d_addr = kmap_atomic(newpage);
  1743. memcpy(d_addr, s_addr, PAGE_SIZE);
  1744. kunmap_atomic(d_addr);
  1745. for (addr = s_addr + offset; addr < s_addr + pos;
  1746. addr += class->size) {
  1747. head = obj_to_head(page, addr);
  1748. if (head & OBJ_ALLOCATED_TAG) {
  1749. handle = head & ~OBJ_ALLOCATED_TAG;
  1750. if (!testpin_tag(handle))
  1751. BUG();
  1752. old_obj = handle_to_obj(handle);
  1753. obj_to_location(old_obj, &dummy, &obj_idx);
  1754. new_obj = (unsigned long)location_to_obj(newpage,
  1755. obj_idx);
  1756. new_obj |= BIT(HANDLE_PIN_BIT);
  1757. record_obj(handle, new_obj);
  1758. }
  1759. }
  1760. replace_sub_page(class, zspage, newpage, page);
  1761. get_page(newpage);
  1762. dec_zspage_isolation(zspage);
  1763. /*
  1764. * Page migration is done so let's putback isolated zspage to
  1765. * the list if @page is final isolated subpage in the zspage.
  1766. */
  1767. if (!is_zspage_isolated(zspage))
  1768. putback_zspage(class, zspage);
  1769. reset_page(page);
  1770. put_page(page);
  1771. page = newpage;
  1772. ret = 0;
  1773. unpin_objects:
  1774. for (addr = s_addr + offset; addr < s_addr + pos;
  1775. addr += class->size) {
  1776. head = obj_to_head(page, addr);
  1777. if (head & OBJ_ALLOCATED_TAG) {
  1778. handle = head & ~OBJ_ALLOCATED_TAG;
  1779. if (!testpin_tag(handle))
  1780. BUG();
  1781. unpin_tag(handle);
  1782. }
  1783. }
  1784. kunmap_atomic(s_addr);
  1785. unlock_class:
  1786. spin_unlock(&class->lock);
  1787. migrate_write_unlock(zspage);
  1788. return ret;
  1789. }
  1790. void zs_page_putback(struct page *page)
  1791. {
  1792. struct zs_pool *pool;
  1793. struct size_class *class;
  1794. int class_idx;
  1795. enum fullness_group fg;
  1796. struct address_space *mapping;
  1797. struct zspage *zspage;
  1798. VM_BUG_ON_PAGE(!PageMovable(page), page);
  1799. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  1800. zspage = get_zspage(page);
  1801. get_zspage_mapping(zspage, &class_idx, &fg);
  1802. mapping = page_mapping(page);
  1803. pool = mapping->private_data;
  1804. class = pool->size_class[class_idx];
  1805. spin_lock(&class->lock);
  1806. dec_zspage_isolation(zspage);
  1807. if (!is_zspage_isolated(zspage)) {
  1808. fg = putback_zspage(class, zspage);
  1809. /*
  1810. * Due to page_lock, we cannot free zspage immediately
  1811. * so let's defer.
  1812. */
  1813. if (fg == ZS_EMPTY)
  1814. schedule_work(&pool->free_work);
  1815. }
  1816. spin_unlock(&class->lock);
  1817. }
  1818. const struct address_space_operations zsmalloc_aops = {
  1819. .isolate_page = zs_page_isolate,
  1820. .migratepage = zs_page_migrate,
  1821. .putback_page = zs_page_putback,
  1822. };
  1823. static int zs_register_migration(struct zs_pool *pool)
  1824. {
  1825. pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
  1826. if (IS_ERR(pool->inode)) {
  1827. pool->inode = NULL;
  1828. return 1;
  1829. }
  1830. pool->inode->i_mapping->private_data = pool;
  1831. pool->inode->i_mapping->a_ops = &zsmalloc_aops;
  1832. return 0;
  1833. }
  1834. static void zs_unregister_migration(struct zs_pool *pool)
  1835. {
  1836. flush_work(&pool->free_work);
  1837. if (pool->inode)
  1838. iput(pool->inode);
  1839. }
  1840. /*
  1841. * Caller should hold page_lock of all pages in the zspage
  1842. * In here, we cannot use zspage meta data.
  1843. */
  1844. static void async_free_zspage(struct work_struct *work)
  1845. {
  1846. int i;
  1847. struct size_class *class;
  1848. unsigned int class_idx;
  1849. enum fullness_group fullness;
  1850. struct zspage *zspage, *tmp;
  1851. LIST_HEAD(free_pages);
  1852. struct zs_pool *pool = container_of(work, struct zs_pool,
  1853. free_work);
  1854. for (i = 0; i < zs_size_classes; i++) {
  1855. class = pool->size_class[i];
  1856. if (class->index != i)
  1857. continue;
  1858. spin_lock(&class->lock);
  1859. list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
  1860. spin_unlock(&class->lock);
  1861. }
  1862. list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
  1863. list_del(&zspage->list);
  1864. lock_zspage(zspage);
  1865. get_zspage_mapping(zspage, &class_idx, &fullness);
  1866. VM_BUG_ON(fullness != ZS_EMPTY);
  1867. class = pool->size_class[class_idx];
  1868. spin_lock(&class->lock);
  1869. __free_zspage(pool, pool->size_class[class_idx], zspage);
  1870. spin_unlock(&class->lock);
  1871. }
  1872. };
  1873. static void kick_deferred_free(struct zs_pool *pool)
  1874. {
  1875. schedule_work(&pool->free_work);
  1876. }
  1877. static void init_deferred_free(struct zs_pool *pool)
  1878. {
  1879. INIT_WORK(&pool->free_work, async_free_zspage);
  1880. }
  1881. static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
  1882. {
  1883. struct page *page = get_first_page(zspage);
  1884. do {
  1885. WARN_ON(!trylock_page(page));
  1886. __SetPageMovable(page, pool->inode->i_mapping);
  1887. unlock_page(page);
  1888. } while ((page = get_next_page(page)) != NULL);
  1889. }
  1890. #endif
  1891. /*
  1892. *
  1893. * Based on the number of unused allocated objects calculate
  1894. * and return the number of pages that we can free.
  1895. */
  1896. static unsigned long zs_can_compact(struct size_class *class)
  1897. {
  1898. unsigned long obj_wasted;
  1899. unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
  1900. unsigned long obj_used = zs_stat_get(class, OBJ_USED);
  1901. if (obj_allocated <= obj_used)
  1902. return 0;
  1903. obj_wasted = obj_allocated - obj_used;
  1904. obj_wasted /= get_maxobj_per_zspage(class->size,
  1905. class->pages_per_zspage);
  1906. return obj_wasted * class->pages_per_zspage;
  1907. }
  1908. static void __zs_compact(struct zs_pool *pool, struct size_class *class)
  1909. {
  1910. struct zs_compact_control cc;
  1911. struct zspage *src_zspage;
  1912. struct zspage *dst_zspage = NULL;
  1913. spin_lock(&class->lock);
  1914. while ((src_zspage = isolate_zspage(class, true))) {
  1915. if (!zs_can_compact(class))
  1916. break;
  1917. cc.index = 0;
  1918. cc.s_page = get_first_page(src_zspage);
  1919. while ((dst_zspage = isolate_zspage(class, false))) {
  1920. cc.d_page = get_first_page(dst_zspage);
  1921. /*
  1922. * If there is no more space in dst_page, resched
  1923. * and see if anyone had allocated another zspage.
  1924. */
  1925. if (!migrate_zspage(pool, class, &cc))
  1926. break;
  1927. putback_zspage(class, dst_zspage);
  1928. }
  1929. /* Stop if we couldn't find slot */
  1930. if (dst_zspage == NULL)
  1931. break;
  1932. putback_zspage(class, dst_zspage);
  1933. if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
  1934. free_zspage(pool, class, src_zspage);
  1935. pool->stats.pages_compacted += class->pages_per_zspage;
  1936. }
  1937. spin_unlock(&class->lock);
  1938. cond_resched();
  1939. spin_lock(&class->lock);
  1940. }
  1941. if (src_zspage)
  1942. putback_zspage(class, src_zspage);
  1943. spin_unlock(&class->lock);
  1944. }
  1945. unsigned long zs_compact(struct zs_pool *pool)
  1946. {
  1947. int i;
  1948. struct size_class *class;
  1949. for (i = zs_size_classes - 1; i >= 0; i--) {
  1950. class = pool->size_class[i];
  1951. if (!class)
  1952. continue;
  1953. if (class->index != i)
  1954. continue;
  1955. __zs_compact(pool, class);
  1956. }
  1957. return pool->stats.pages_compacted;
  1958. }
  1959. EXPORT_SYMBOL_GPL(zs_compact);
  1960. void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
  1961. {
  1962. memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
  1963. }
  1964. EXPORT_SYMBOL_GPL(zs_pool_stats);
  1965. static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
  1966. struct shrink_control *sc)
  1967. {
  1968. unsigned long pages_freed;
  1969. struct zs_pool *pool = container_of(shrinker, struct zs_pool,
  1970. shrinker);
  1971. pages_freed = pool->stats.pages_compacted;
  1972. /*
  1973. * Compact classes and calculate compaction delta.
  1974. * Can run concurrently with a manually triggered
  1975. * (by user) compaction.
  1976. */
  1977. pages_freed = zs_compact(pool) - pages_freed;
  1978. return pages_freed ? pages_freed : SHRINK_STOP;
  1979. }
  1980. static unsigned long zs_shrinker_count(struct shrinker *shrinker,
  1981. struct shrink_control *sc)
  1982. {
  1983. int i;
  1984. struct size_class *class;
  1985. unsigned long pages_to_free = 0;
  1986. struct zs_pool *pool = container_of(shrinker, struct zs_pool,
  1987. shrinker);
  1988. for (i = zs_size_classes - 1; i >= 0; i--) {
  1989. class = pool->size_class[i];
  1990. if (!class)
  1991. continue;
  1992. if (class->index != i)
  1993. continue;
  1994. pages_to_free += zs_can_compact(class);
  1995. }
  1996. return pages_to_free;
  1997. }
  1998. static void zs_unregister_shrinker(struct zs_pool *pool)
  1999. {
  2000. if (pool->shrinker_enabled) {
  2001. unregister_shrinker(&pool->shrinker);
  2002. pool->shrinker_enabled = false;
  2003. }
  2004. }
  2005. static int zs_register_shrinker(struct zs_pool *pool)
  2006. {
  2007. pool->shrinker.scan_objects = zs_shrinker_scan;
  2008. pool->shrinker.count_objects = zs_shrinker_count;
  2009. pool->shrinker.batch = 0;
  2010. pool->shrinker.seeks = DEFAULT_SEEKS;
  2011. return register_shrinker(&pool->shrinker);
  2012. }
  2013. /**
  2014. * zs_create_pool - Creates an allocation pool to work from.
  2015. * @flags: allocation flags used to allocate pool metadata
  2016. *
  2017. * This function must be called before anything when using
  2018. * the zsmalloc allocator.
  2019. *
  2020. * On success, a pointer to the newly created pool is returned,
  2021. * otherwise NULL.
  2022. */
  2023. struct zs_pool *zs_create_pool(const char *name)
  2024. {
  2025. int i;
  2026. struct zs_pool *pool;
  2027. struct size_class *prev_class = NULL;
  2028. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  2029. if (!pool)
  2030. return NULL;
  2031. init_deferred_free(pool);
  2032. pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
  2033. GFP_KERNEL);
  2034. if (!pool->size_class) {
  2035. kfree(pool);
  2036. return NULL;
  2037. }
  2038. pool->name = kstrdup(name, GFP_KERNEL);
  2039. if (!pool->name)
  2040. goto err;
  2041. if (create_cache(pool))
  2042. goto err;
  2043. /*
  2044. * Iterate reversly, because, size of size_class that we want to use
  2045. * for merging should be larger or equal to current size.
  2046. */
  2047. for (i = zs_size_classes - 1; i >= 0; i--) {
  2048. int size;
  2049. int pages_per_zspage;
  2050. struct size_class *class;
  2051. int fullness = 0;
  2052. size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
  2053. if (size > ZS_MAX_ALLOC_SIZE)
  2054. size = ZS_MAX_ALLOC_SIZE;
  2055. pages_per_zspage = get_pages_per_zspage(size);
  2056. /*
  2057. * size_class is used for normal zsmalloc operation such
  2058. * as alloc/free for that size. Although it is natural that we
  2059. * have one size_class for each size, there is a chance that we
  2060. * can get more memory utilization if we use one size_class for
  2061. * many different sizes whose size_class have same
  2062. * characteristics. So, we makes size_class point to
  2063. * previous size_class if possible.
  2064. */
  2065. if (prev_class) {
  2066. if (can_merge(prev_class, size, pages_per_zspage)) {
  2067. pool->size_class[i] = prev_class;
  2068. continue;
  2069. }
  2070. }
  2071. class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
  2072. if (!class)
  2073. goto err;
  2074. class->size = size;
  2075. class->index = i;
  2076. class->pages_per_zspage = pages_per_zspage;
  2077. class->objs_per_zspage = class->pages_per_zspage *
  2078. PAGE_SIZE / class->size;
  2079. spin_lock_init(&class->lock);
  2080. pool->size_class[i] = class;
  2081. for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
  2082. fullness++)
  2083. INIT_LIST_HEAD(&class->fullness_list[fullness]);
  2084. prev_class = class;
  2085. }
  2086. /* debug only, don't abort if it fails */
  2087. zs_pool_stat_create(pool, name);
  2088. if (zs_register_migration(pool))
  2089. goto err;
  2090. /*
  2091. * Not critical, we still can use the pool
  2092. * and user can trigger compaction manually.
  2093. */
  2094. if (zs_register_shrinker(pool) == 0)
  2095. pool->shrinker_enabled = true;
  2096. return pool;
  2097. err:
  2098. zs_destroy_pool(pool);
  2099. return NULL;
  2100. }
  2101. EXPORT_SYMBOL_GPL(zs_create_pool);
  2102. void zs_destroy_pool(struct zs_pool *pool)
  2103. {
  2104. int i;
  2105. zs_unregister_shrinker(pool);
  2106. zs_unregister_migration(pool);
  2107. zs_pool_stat_destroy(pool);
  2108. for (i = 0; i < zs_size_classes; i++) {
  2109. int fg;
  2110. struct size_class *class = pool->size_class[i];
  2111. if (!class)
  2112. continue;
  2113. if (class->index != i)
  2114. continue;
  2115. for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
  2116. if (!list_empty(&class->fullness_list[fg])) {
  2117. pr_info("Freeing non-empty class with size %db, fullness group %d\n",
  2118. class->size, fg);
  2119. }
  2120. }
  2121. kfree(class);
  2122. }
  2123. destroy_cache(pool);
  2124. kfree(pool->size_class);
  2125. kfree(pool->name);
  2126. kfree(pool);
  2127. }
  2128. EXPORT_SYMBOL_GPL(zs_destroy_pool);
  2129. static int __init zs_init(void)
  2130. {
  2131. int ret;
  2132. ret = zsmalloc_mount();
  2133. if (ret)
  2134. goto out;
  2135. ret = zs_register_cpu_notifier();
  2136. if (ret)
  2137. goto notifier_fail;
  2138. init_zs_size_classes();
  2139. #ifdef CONFIG_ZPOOL
  2140. zpool_register_driver(&zs_zpool_driver);
  2141. #endif
  2142. zs_stat_init();
  2143. return 0;
  2144. notifier_fail:
  2145. zs_unregister_cpu_notifier();
  2146. zsmalloc_unmount();
  2147. out:
  2148. return ret;
  2149. }
  2150. static void __exit zs_exit(void)
  2151. {
  2152. #ifdef CONFIG_ZPOOL
  2153. zpool_unregister_driver(&zs_zpool_driver);
  2154. #endif
  2155. zsmalloc_unmount();
  2156. zs_unregister_cpu_notifier();
  2157. zs_stat_exit();
  2158. }
  2159. module_init(zs_init);
  2160. module_exit(zs_exit);
  2161. MODULE_LICENSE("Dual BSD/GPL");
  2162. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");