zsmalloc.c 60 KB

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