Linux Kernel-Zpool 接口阅读

yyi
yyi
2023-09-09 / 0 评论 / 113 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年09月13日,已超过241天没有更新,若内容或图片失效,请留言反馈。

Linux Kernel-Zpool 接口阅读

最近还是在做zswap的迁移,需要整理一下zpool的接口功能,以决定是迁移过去一个现成的还是自己实现一个简单的。

1 Zpool作用

Zpool只是一个接口层,由zbud、zsmalloc等进行具体实现。Zpool提供了一个内存池,用以存放被压缩的内存。

因为它是一个接口,其实我们只需要简单的关注注册数据结构,就能了解到它的一些关键作用。

2 Driver Struct

struct zpool_driver {
    char *type;
    struct module *owner;
    atomic_t refcount;
    struct list_head list;

    void *(*create)(const char *name,
            gfp_t gfp,
            const struct zpool_ops *ops,
            struct zpool *zpool);
    void (*destroy)(void *pool);

    bool malloc_support_movable;
    int (*malloc)(void *pool, size_t size, gfp_t gfp,
                unsigned long *handle);
    void (*free)(void *pool, unsigned long handle);

    int (*shrink)(void *pool, unsigned int pages,
                unsigned int *reclaimed);

    bool sleep_mapped;
    void *(*map)(void *pool, unsigned long handle,
                enum zpool_mapmode mm);
    void (*unmap)(void *pool, unsigned long handle);

    u64 (*total_size)(void *pool);
};

refcount、list的作用都是字面意思。Type是当前driver的名字,索引driver的时候会用到。

下面的函数是zpool后端提供给接口的实现,我们会在下一节里介绍其作用。

3 暴露接口

3.1 zpool_register_driver

void zpool_register_driver(struct zpool_driver *driver);
int zpool_unregister_driver(struct zpool_driver *driver)

该函数暴露给所有zpool的实现,由他们在启动时注册。该函数初始化了引用计数、将被注册的driver加入到zpool的链表中。与其相对应的是zpool_unregister_driver,注销driver

该接口评估为不需要迁移。

3.2 zpool_get_driver

static struct zpool_driver *zpool_get_driver(const char *type)
static void zpool_put_driver(struct zpool_driver *driver)

给定一个zpool驱动名字,返回对应的zpool_driver。并且增加对应driver的引用计数。

与其相对应的是zpool_put_driver

该接口评估为不需要迁移。

3.3 zpool_has_pool、zpool_create_pool、zpool_destry_pool

bool zpool_has_pool(char *type)
struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp,
        const struct zpool_ops *ops)
void zpool_destroy_pool(struct zpool *zpool)

分别承担检测给定的driver是否可用、创建一个指定类型的zpool和销毁一个已经存在的zpool。

该接口评估为不需要迁移

3.4 zpool_malloc_support_movable

bool zpool_malloc_support_movable(struct zpool *zpool)

返回driver是否支持迁移的字段,评估为不需要迁移。

3.5 driver实现函数

3.5.1 zpool_malloc、zpool_free、zpool_shrink

int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
            unsigned long *handle)
void zpool_free(struct zpool *zpool, unsigned long handle)

int zpool_shrink(struct zpool *zpool, unsigned int pages,
            unsigned int *reclaimed)

在给定的zpool中申请大小为size的空间,并把申请得到的句柄放在handle中

在给定的zpool中释放给定的handle的空间。

zpool_shrink可能未被实现,如果实现,尝试驱逐当前正在使用的句柄。

3.5.2 zpool_map_handle、zpool_unmap_handle

void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
            enum zpool_mapmode mapmode)
void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)

根据给定的pool和handle,映射到连续的地址上

取消映射,解锁相关的锁

3.5.3 zpool_get_total_size

u64 zpool_get_total_size(struct zpool *zpool)

获取zpool所占用的内存空间

0

评论 (0)

取消