xref: /openbmc/linux/include/linux/zsmalloc.h (revision 860c707dca155a56dfa115ddd6c00959296144a6)
1bcf1647dSMinchan Kim /*
2bcf1647dSMinchan Kim  * zsmalloc memory allocator
3bcf1647dSMinchan Kim  *
4bcf1647dSMinchan Kim  * Copyright (C) 2011  Nitin Gupta
531fc00bbSMinchan Kim  * Copyright (C) 2012, 2013 Minchan Kim
6bcf1647dSMinchan Kim  *
7bcf1647dSMinchan Kim  * This code is released using a dual license strategy: BSD/GPL
8bcf1647dSMinchan Kim  * You can choose the license that better fits your requirements.
9bcf1647dSMinchan Kim  *
10bcf1647dSMinchan Kim  * Released under the terms of 3-clause BSD License
11bcf1647dSMinchan Kim  * Released under the terms of GNU General Public License Version 2.0
12bcf1647dSMinchan Kim  */
13bcf1647dSMinchan Kim 
14bcf1647dSMinchan Kim #ifndef _ZS_MALLOC_H_
15bcf1647dSMinchan Kim #define _ZS_MALLOC_H_
16bcf1647dSMinchan Kim 
17bcf1647dSMinchan Kim #include <linux/types.h>
18bcf1647dSMinchan Kim 
19bcf1647dSMinchan Kim /*
20bcf1647dSMinchan Kim  * zsmalloc mapping modes
21bcf1647dSMinchan Kim  *
22bcf1647dSMinchan Kim  * NOTE: These only make a difference when a mapped object spans pages.
23bcf1647dSMinchan Kim  * They also have no effect when PGTABLE_MAPPING is selected.
24bcf1647dSMinchan Kim  */
25bcf1647dSMinchan Kim enum zs_mapmode {
26bcf1647dSMinchan Kim 	ZS_MM_RW, /* normal read-write mapping */
27bcf1647dSMinchan Kim 	ZS_MM_RO, /* read-only (no copy-out at unmap time) */
28bcf1647dSMinchan Kim 	ZS_MM_WO /* write-only (no copy-in at map time) */
29bcf1647dSMinchan Kim 	/*
30bcf1647dSMinchan Kim 	 * NOTE: ZS_MM_WO should only be used for initializing new
31bcf1647dSMinchan Kim 	 * (uninitialized) allocations.  Partial writes to already
32bcf1647dSMinchan Kim 	 * initialized allocations should use ZS_MM_RW to preserve the
33bcf1647dSMinchan Kim 	 * existing data.
34bcf1647dSMinchan Kim 	 */
35bcf1647dSMinchan Kim };
36bcf1647dSMinchan Kim 
377d3f3938SSergey Senozhatsky struct zs_pool_stats {
38*860c707dSSergey Senozhatsky 	/* How many pages were migrated (freed) */
39*860c707dSSergey Senozhatsky 	unsigned long pages_compacted;
407d3f3938SSergey Senozhatsky };
417d3f3938SSergey Senozhatsky 
42bcf1647dSMinchan Kim struct zs_pool;
43bcf1647dSMinchan Kim 
443eba0c6aSGanesh Mahendran struct zs_pool *zs_create_pool(char *name, gfp_t flags);
45bcf1647dSMinchan Kim void zs_destroy_pool(struct zs_pool *pool);
46bcf1647dSMinchan Kim 
47bcf1647dSMinchan Kim unsigned long zs_malloc(struct zs_pool *pool, size_t size);
48bcf1647dSMinchan Kim void zs_free(struct zs_pool *pool, unsigned long obj);
49bcf1647dSMinchan Kim 
50bcf1647dSMinchan Kim void *zs_map_object(struct zs_pool *pool, unsigned long handle,
51bcf1647dSMinchan Kim 			enum zs_mapmode mm);
52bcf1647dSMinchan Kim void zs_unmap_object(struct zs_pool *pool, unsigned long handle);
53bcf1647dSMinchan Kim 
54722cdc17SMinchan Kim unsigned long zs_get_total_pages(struct zs_pool *pool);
55312fcae2SMinchan Kim unsigned long zs_compact(struct zs_pool *pool);
56bcf1647dSMinchan Kim 
577d3f3938SSergey Senozhatsky void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats);
58bcf1647dSMinchan Kim #endif
59