linux-stable/include/linux/zpool.h
Tian Tao fc6697a89f mm/zswap: add the flag can_sleep_mapped
Patch series "Fix the compatibility of zsmalloc and zswap".

Patch #1 adds a flag to zpool, then zswap used to determine if zpool
drivers such as zbud/z3fold/zsmalloc will enter an atomic context after
mapping.

The difference between zbud/z3fold and zsmalloc is that zsmalloc requires
an atomic context that since its map function holds a preempt-disabled,
but zbud/z3fold don't require an atomic context.  So patch #2 sets flag
sleep_mapped to true indicating that zbud/z3fold can sleep after mapping.
zsmalloc didn't support sleep after mapping, so don't set that flag to
true.

This patch (of 2):

Add a flag to zpool, named is "can_sleep_mapped", and have it set true for
zbud/z3fold, not set this flag for zsmalloc, so its default value is
false.  Then zswap could go the current path if the flag is true; and if
it's false, copy data from src to a temporary buffer, then unmap the
handle, take the mutex, process the buffer instead of src to avoid
sleeping function called from atomic context.

[natechancellor@gmail.com: add return value in zswap_frontswap_load]
  Link: https://lkml.kernel.org/r/20210121214804.926843-1-natechancellor@gmail.com
[tiantao6@hisilicon.com: fix potential memory leak]
  Link: https://lkml.kernel.org/r/1611538365-51811-1-git-send-email-tiantao6@hisilicon.com
[colin.king@canonical.com: fix potential uninitialized pointer read on tmp]
  Link: https://lkml.kernel.org/r/20210128141728.639030-1-colin.king@canonical.com
[tiantao6@hisilicon.com: fix variable 'entry' is uninitialized when used]
  Link: https://lkml.kernel.org/r/1611223030-58346-1-git-send-email-tiantao6@hisilicon.comLink: https://lkml.kernel.org/r/1611035683-12732-1-git-send-email-tiantao6@hisilicon.com

Link: https://lkml.kernel.org/r/1611035683-12732-2-git-send-email-tiantao6@hisilicon.com
Signed-off-by: Tian Tao <tiantao6@hisilicon.com>
Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Reviewed-by: Vitaly Wool <vitaly.wool@konsulko.com>
Acked-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Reported-by: Mike Galbraith <efault@gmx.de>
Cc: Barry Song <song.bao.hua@hisilicon.com>
Cc: Dan Streetman <ddstreet@ieee.org>
Cc: Seth Jennings <sjenning@redhat.com>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-02-26 09:41:01 -08:00

119 lines
3.2 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
/*
* zpool memory storage api
*
* Copyright (C) 2014 Dan Streetman
*
* This is a common frontend for the zbud and zsmalloc memory
* storage pool implementations. Typically, this is used to
* store compressed memory.
*/
#ifndef _ZPOOL_H_
#define _ZPOOL_H_
struct zpool;
struct zpool_ops {
int (*evict)(struct zpool *pool, unsigned long handle);
};
/*
* Control how a handle is mapped. It will be ignored if the
* implementation does not support it. Its use is optional.
* Note that this does not refer to memory protection, it
* refers to how the memory will be copied in/out if copying
* is necessary during mapping; read-write is the safest as
* it copies the existing memory in on map, and copies the
* changed memory back out on unmap. Write-only does not copy
* in the memory and should only be used for initialization.
* If in doubt, use ZPOOL_MM_DEFAULT which is read-write.
*/
enum zpool_mapmode {
ZPOOL_MM_RW, /* normal read-write mapping */
ZPOOL_MM_RO, /* read-only (no copy-out at unmap time) */
ZPOOL_MM_WO, /* write-only (no copy-in at map time) */
ZPOOL_MM_DEFAULT = ZPOOL_MM_RW
};
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);
const char *zpool_get_type(struct zpool *pool);
void zpool_destroy_pool(struct zpool *pool);
bool zpool_malloc_support_movable(struct zpool *pool);
int zpool_malloc(struct zpool *pool, size_t size, gfp_t gfp,
unsigned long *handle);
void zpool_free(struct zpool *pool, unsigned long handle);
int zpool_shrink(struct zpool *pool, unsigned int pages,
unsigned int *reclaimed);
void *zpool_map_handle(struct zpool *pool, unsigned long handle,
enum zpool_mapmode mm);
void zpool_unmap_handle(struct zpool *pool, unsigned long handle);
u64 zpool_get_total_size(struct zpool *pool);
/**
* struct zpool_driver - driver implementation for zpool
* @type: name of the driver.
* @list: entry in the list of zpool drivers.
* @create: create a new pool.
* @destroy: destroy a pool.
* @malloc: allocate mem from a pool.
* @free: free mem from a pool.
* @shrink: shrink the pool.
* @sleep_mapped: whether zpool driver can sleep during map.
* @map: map a handle.
* @unmap: unmap a handle.
* @total_size: get total size of a pool.
*
* This is created by a zpool implementation and registered
* with zpool.
*/
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);
};
void zpool_register_driver(struct zpool_driver *driver);
int zpool_unregister_driver(struct zpool_driver *driver);
bool zpool_evictable(struct zpool *pool);
bool zpool_can_sleep_mapped(struct zpool *pool);
#endif