OpenCloudOS-Kernel/drivers/block/zram/zram_memcg.h

80 lines
2.2 KiB
C

/*
* ZRAM memcg support.
*
* Released under the terms of GNU General Public License Version 2.0
*/
#ifndef _ZRAM_MEMCG_H_
#define _ZRAM_MEMCG_H_
#include <linux/memcontrol.h>
#include "zram_drv.h"
#if defined(CONFIG_ZRAM_MEMCG)
/*
* Use last bit of objcg pointer as tag.
* When set, don't release this objcg on uncharge, eg. when
* doing recompression, put/re-get the objcg may lead to UAF if we
* are the last user of the objcg and takes extra efforts.
*/
#define ZRAM_OBJCG_PINNED 0
static inline void zram_memcg_set_objcg_pinned(struct zram *zram, u32 index)
{
unsigned long objcg_val = (unsigned long)zram->table[index].objcg;
if (!objcg_val)
return;
objcg_val |= BIT(ZRAM_OBJCG_PINNED);
zram->table[index].objcg = (void*)objcg_val;
}
static inline bool zram_memcg_is_objcg_pinned(struct zram *zram, u32 index)
{
unsigned long objcg_val = (unsigned long)zram->table[index].objcg;
return objcg_val & BIT(ZRAM_OBJCG_PINNED);
}
static inline void zram_memcg_clear_objcg_pinned(struct zram *zram, u32 index)
{
unsigned long objcg_val = (unsigned long)zram->table[index].objcg;
objcg_val &= ~BIT(ZRAM_OBJCG_PINNED);
zram->table[index].objcg = (void*)objcg_val;
}
extern int zram_memcg_get_prio(struct zram *zram, struct page *page);
extern bool zram_memcg_overlimit(struct page *page);
extern int zram_memcg_charge(struct zram *zram, u32 index, struct page *page, int comp_len);
extern void zram_memcg_uncharge(struct zram *zram, u32 index, int comp_len);
extern void zram_memcg_recharge(struct zram *zram, u32 index, int comp_len_old, int comp_len_new);
#else /* CONFIG_ZRAM_MEMCG */
static inline void zram_memcg_set_objcg_pinned(struct zram *zram, u32 index)
{
}
extern int zram_memcg_get_prio(struct zram *zram, struct page *page)
{
return ZRAM_PRIMARY_COMP;
}
static inline bool zram_memcg_overlimit(struct page *page)
{
return false;
}
static inline int zram_memcg_charge(struct zram *zram, u32 index, struct page *page, int comp_len)
{
return 0;
}
static inline void zram_memcg_uncharge(struct zram *zram, u32 index, int comp_len)
{
}
static inline void zram_memcg_recharge(struct zram *zram, u32 index, int comp_len_old, int comp_len_new)
{
}
#endif /* CONFIG_ZRAM_MEMCG */
#endif /* _ZRAM_MEMCG_H_ */