z3fold: add kref refcounting
With both coming and already present locking optimizations, introducing kref to reference-count z3fold objects is the right thing to do. Moreover, it makes buddied list no longer necessary, and allows for a simpler handling of headless pages. [akpm@linux-foundation.org: coding-style fixes] Link: http://lkml.kernel.org/r/20170131214650.8ea78033d91ded233f552bc0@gmail.com Signed-off-by: Vitaly Wool <vitalywool@gmail.com> Reviewed-by: Dan Streetman <ddstreet@ieee.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
2f1e5e4d84
commit
5a27aa8220
159
mm/z3fold.c
159
mm/z3fold.c
|
@ -52,6 +52,7 @@ enum buddy {
|
||||||
* z3fold page, except for HEADLESS pages
|
* z3fold page, except for HEADLESS pages
|
||||||
* @buddy: links the z3fold page into the relevant list in the pool
|
* @buddy: links the z3fold page into the relevant list in the pool
|
||||||
* @page_lock: per-page lock
|
* @page_lock: per-page lock
|
||||||
|
* @refcount: reference cound for the z3fold page
|
||||||
* @first_chunks: the size of the first buddy in chunks, 0 if free
|
* @first_chunks: the size of the first buddy in chunks, 0 if free
|
||||||
* @middle_chunks: the size of the middle buddy in chunks, 0 if free
|
* @middle_chunks: the size of the middle buddy in chunks, 0 if free
|
||||||
* @last_chunks: the size of the last buddy in chunks, 0 if free
|
* @last_chunks: the size of the last buddy in chunks, 0 if free
|
||||||
|
@ -60,6 +61,7 @@ enum buddy {
|
||||||
struct z3fold_header {
|
struct z3fold_header {
|
||||||
struct list_head buddy;
|
struct list_head buddy;
|
||||||
spinlock_t page_lock;
|
spinlock_t page_lock;
|
||||||
|
struct kref refcount;
|
||||||
unsigned short first_chunks;
|
unsigned short first_chunks;
|
||||||
unsigned short middle_chunks;
|
unsigned short middle_chunks;
|
||||||
unsigned short last_chunks;
|
unsigned short last_chunks;
|
||||||
|
@ -95,8 +97,6 @@ struct z3fold_header {
|
||||||
* @unbuddied: array of lists tracking z3fold pages that contain 2- buddies;
|
* @unbuddied: array of lists tracking z3fold pages that contain 2- buddies;
|
||||||
* the lists each z3fold page is added to depends on the size of
|
* the lists each z3fold page is added to depends on the size of
|
||||||
* its free region.
|
* its free region.
|
||||||
* @buddied: list tracking the z3fold pages that contain 3 buddies;
|
|
||||||
* these z3fold pages are full
|
|
||||||
* @lru: list tracking the z3fold pages in LRU order by most recently
|
* @lru: list tracking the z3fold pages in LRU order by most recently
|
||||||
* added buddy.
|
* added buddy.
|
||||||
* @pages_nr: number of z3fold pages in the pool.
|
* @pages_nr: number of z3fold pages in the pool.
|
||||||
|
@ -109,7 +109,6 @@ struct z3fold_header {
|
||||||
struct z3fold_pool {
|
struct z3fold_pool {
|
||||||
spinlock_t lock;
|
spinlock_t lock;
|
||||||
struct list_head unbuddied[NCHUNKS];
|
struct list_head unbuddied[NCHUNKS];
|
||||||
struct list_head buddied;
|
|
||||||
struct list_head lru;
|
struct list_head lru;
|
||||||
atomic64_t pages_nr;
|
atomic64_t pages_nr;
|
||||||
const struct z3fold_ops *ops;
|
const struct z3fold_ops *ops;
|
||||||
|
@ -121,8 +120,7 @@ struct z3fold_pool {
|
||||||
* Internal z3fold page flags
|
* Internal z3fold page flags
|
||||||
*/
|
*/
|
||||||
enum z3fold_page_flags {
|
enum z3fold_page_flags {
|
||||||
UNDER_RECLAIM = 0,
|
PAGE_HEADLESS = 0,
|
||||||
PAGE_HEADLESS,
|
|
||||||
MIDDLE_CHUNK_MAPPED,
|
MIDDLE_CHUNK_MAPPED,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -146,11 +144,11 @@ static struct z3fold_header *init_z3fold_page(struct page *page)
|
||||||
struct z3fold_header *zhdr = page_address(page);
|
struct z3fold_header *zhdr = page_address(page);
|
||||||
|
|
||||||
INIT_LIST_HEAD(&page->lru);
|
INIT_LIST_HEAD(&page->lru);
|
||||||
clear_bit(UNDER_RECLAIM, &page->private);
|
|
||||||
clear_bit(PAGE_HEADLESS, &page->private);
|
clear_bit(PAGE_HEADLESS, &page->private);
|
||||||
clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
|
clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
|
||||||
|
|
||||||
spin_lock_init(&zhdr->page_lock);
|
spin_lock_init(&zhdr->page_lock);
|
||||||
|
kref_init(&zhdr->refcount);
|
||||||
zhdr->first_chunks = 0;
|
zhdr->first_chunks = 0;
|
||||||
zhdr->middle_chunks = 0;
|
zhdr->middle_chunks = 0;
|
||||||
zhdr->last_chunks = 0;
|
zhdr->last_chunks = 0;
|
||||||
|
@ -161,9 +159,24 @@ static struct z3fold_header *init_z3fold_page(struct page *page)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Resets the struct page fields and frees the page */
|
/* Resets the struct page fields and frees the page */
|
||||||
static void free_z3fold_page(struct z3fold_header *zhdr)
|
static void free_z3fold_page(struct page *page)
|
||||||
{
|
{
|
||||||
__free_page(virt_to_page(zhdr));
|
__free_page(page);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void release_z3fold_page(struct kref *ref)
|
||||||
|
{
|
||||||
|
struct z3fold_header *zhdr;
|
||||||
|
struct page *page;
|
||||||
|
|
||||||
|
zhdr = container_of(ref, struct z3fold_header, refcount);
|
||||||
|
page = virt_to_page(zhdr);
|
||||||
|
|
||||||
|
if (!list_empty(&zhdr->buddy))
|
||||||
|
list_del(&zhdr->buddy);
|
||||||
|
if (!list_empty(&page->lru))
|
||||||
|
list_del(&page->lru);
|
||||||
|
free_z3fold_page(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Lock a z3fold page */
|
/* Lock a z3fold page */
|
||||||
|
@ -178,7 +191,6 @@ static inline void z3fold_page_unlock(struct z3fold_header *zhdr)
|
||||||
spin_unlock(&zhdr->page_lock);
|
spin_unlock(&zhdr->page_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Encodes the handle of a particular buddy within a z3fold page
|
* Encodes the handle of a particular buddy within a z3fold page
|
||||||
* Pool lock should be held as this function accesses first_num
|
* Pool lock should be held as this function accesses first_num
|
||||||
|
@ -257,7 +269,6 @@ static struct z3fold_pool *z3fold_create_pool(gfp_t gfp,
|
||||||
spin_lock_init(&pool->lock);
|
spin_lock_init(&pool->lock);
|
||||||
for_each_unbuddied_list(i, 0)
|
for_each_unbuddied_list(i, 0)
|
||||||
INIT_LIST_HEAD(&pool->unbuddied[i]);
|
INIT_LIST_HEAD(&pool->unbuddied[i]);
|
||||||
INIT_LIST_HEAD(&pool->buddied);
|
|
||||||
INIT_LIST_HEAD(&pool->lru);
|
INIT_LIST_HEAD(&pool->lru);
|
||||||
atomic64_set(&pool->pages_nr, 0);
|
atomic64_set(&pool->pages_nr, 0);
|
||||||
pool->ops = ops;
|
pool->ops = ops;
|
||||||
|
@ -378,6 +389,7 @@ static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
|
||||||
spin_unlock(&pool->lock);
|
spin_unlock(&pool->lock);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
kref_get(&zhdr->refcount);
|
||||||
list_del_init(&zhdr->buddy);
|
list_del_init(&zhdr->buddy);
|
||||||
spin_unlock(&pool->lock);
|
spin_unlock(&pool->lock);
|
||||||
|
|
||||||
|
@ -394,10 +406,12 @@ static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
|
||||||
else if (zhdr->middle_chunks == 0)
|
else if (zhdr->middle_chunks == 0)
|
||||||
bud = MIDDLE;
|
bud = MIDDLE;
|
||||||
else {
|
else {
|
||||||
spin_lock(&pool->lock);
|
|
||||||
list_add(&zhdr->buddy, &pool->buddied);
|
|
||||||
spin_unlock(&pool->lock);
|
|
||||||
z3fold_page_unlock(zhdr);
|
z3fold_page_unlock(zhdr);
|
||||||
|
spin_lock(&pool->lock);
|
||||||
|
if (kref_put(&zhdr->refcount,
|
||||||
|
release_z3fold_page))
|
||||||
|
atomic64_dec(&pool->pages_nr);
|
||||||
|
spin_unlock(&pool->lock);
|
||||||
pr_err("No free chunks in unbuddied\n");
|
pr_err("No free chunks in unbuddied\n");
|
||||||
WARN_ON(1);
|
WARN_ON(1);
|
||||||
continue;
|
continue;
|
||||||
|
@ -438,9 +452,6 @@ found:
|
||||||
/* Add to unbuddied list */
|
/* Add to unbuddied list */
|
||||||
freechunks = num_free_chunks(zhdr);
|
freechunks = num_free_chunks(zhdr);
|
||||||
list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
|
list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
|
||||||
} else {
|
|
||||||
/* Add to buddied list */
|
|
||||||
list_add(&zhdr->buddy, &pool->buddied);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
headless:
|
headless:
|
||||||
|
@ -504,52 +515,29 @@ static void z3fold_free(struct z3fold_pool *pool, unsigned long handle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (test_bit(UNDER_RECLAIM, &page->private)) {
|
if (bud == HEADLESS) {
|
||||||
/* z3fold page is under reclaim, reclaim will free */
|
|
||||||
if (bud != HEADLESS)
|
|
||||||
z3fold_page_unlock(zhdr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Remove from existing buddy list */
|
|
||||||
if (bud != HEADLESS) {
|
|
||||||
spin_lock(&pool->lock);
|
|
||||||
/*
|
|
||||||
* this object may have been removed from its list by
|
|
||||||
* z3fold_alloc(). In that case we just do nothing,
|
|
||||||
* z3fold_alloc() will allocate an object and add the page
|
|
||||||
* to the relevant list.
|
|
||||||
*/
|
|
||||||
if (!list_empty(&zhdr->buddy)) {
|
|
||||||
list_del(&zhdr->buddy);
|
|
||||||
} else {
|
|
||||||
spin_unlock(&pool->lock);
|
|
||||||
z3fold_page_unlock(zhdr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
spin_unlock(&pool->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bud == HEADLESS ||
|
|
||||||
(zhdr->first_chunks == 0 && zhdr->middle_chunks == 0 &&
|
|
||||||
zhdr->last_chunks == 0)) {
|
|
||||||
/* z3fold page is empty, free */
|
|
||||||
spin_lock(&pool->lock);
|
spin_lock(&pool->lock);
|
||||||
list_del(&page->lru);
|
list_del(&page->lru);
|
||||||
spin_unlock(&pool->lock);
|
spin_unlock(&pool->lock);
|
||||||
clear_bit(PAGE_HEADLESS, &page->private);
|
free_z3fold_page(page);
|
||||||
if (bud != HEADLESS)
|
|
||||||
z3fold_page_unlock(zhdr);
|
|
||||||
free_z3fold_page(zhdr);
|
|
||||||
atomic64_dec(&pool->pages_nr);
|
atomic64_dec(&pool->pages_nr);
|
||||||
} else {
|
} else {
|
||||||
z3fold_compact_page(zhdr);
|
if (zhdr->first_chunks != 0 || zhdr->middle_chunks != 0 ||
|
||||||
/* Add to the unbuddied list */
|
zhdr->last_chunks != 0) {
|
||||||
spin_lock(&pool->lock);
|
z3fold_compact_page(zhdr);
|
||||||
freechunks = num_free_chunks(zhdr);
|
/* Add to the unbuddied list */
|
||||||
list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
|
spin_lock(&pool->lock);
|
||||||
spin_unlock(&pool->lock);
|
if (!list_empty(&zhdr->buddy))
|
||||||
|
list_del(&zhdr->buddy);
|
||||||
|
freechunks = num_free_chunks(zhdr);
|
||||||
|
list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
|
||||||
|
spin_unlock(&pool->lock);
|
||||||
|
}
|
||||||
z3fold_page_unlock(zhdr);
|
z3fold_page_unlock(zhdr);
|
||||||
|
spin_lock(&pool->lock);
|
||||||
|
if (kref_put(&zhdr->refcount, release_z3fold_page))
|
||||||
|
atomic64_dec(&pool->pages_nr);
|
||||||
|
spin_unlock(&pool->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -608,13 +596,13 @@ static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
page = list_last_entry(&pool->lru, struct page, lru);
|
page = list_last_entry(&pool->lru, struct page, lru);
|
||||||
list_del(&page->lru);
|
list_del_init(&page->lru);
|
||||||
|
|
||||||
/* Protect z3fold page against free */
|
|
||||||
set_bit(UNDER_RECLAIM, &page->private);
|
|
||||||
zhdr = page_address(page);
|
zhdr = page_address(page);
|
||||||
if (!test_bit(PAGE_HEADLESS, &page->private)) {
|
if (!test_bit(PAGE_HEADLESS, &page->private)) {
|
||||||
list_del(&zhdr->buddy);
|
if (!list_empty(&zhdr->buddy))
|
||||||
|
list_del_init(&zhdr->buddy);
|
||||||
|
kref_get(&zhdr->refcount);
|
||||||
spin_unlock(&pool->lock);
|
spin_unlock(&pool->lock);
|
||||||
z3fold_page_lock(zhdr);
|
z3fold_page_lock(zhdr);
|
||||||
/*
|
/*
|
||||||
|
@ -655,30 +643,19 @@ static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
|
||||||
goto next;
|
goto next;
|
||||||
}
|
}
|
||||||
next:
|
next:
|
||||||
if (!test_bit(PAGE_HEADLESS, &page->private))
|
if (test_bit(PAGE_HEADLESS, &page->private)) {
|
||||||
z3fold_page_lock(zhdr);
|
if (ret == 0) {
|
||||||
clear_bit(UNDER_RECLAIM, &page->private);
|
free_z3fold_page(page);
|
||||||
if ((test_bit(PAGE_HEADLESS, &page->private) && ret == 0) ||
|
return 0;
|
||||||
(zhdr->first_chunks == 0 && zhdr->last_chunks == 0 &&
|
|
||||||
zhdr->middle_chunks == 0)) {
|
|
||||||
/*
|
|
||||||
* All buddies are now free, free the z3fold page and
|
|
||||||
* return success.
|
|
||||||
*/
|
|
||||||
if (!test_and_clear_bit(PAGE_HEADLESS, &page->private))
|
|
||||||
z3fold_page_unlock(zhdr);
|
|
||||||
free_z3fold_page(zhdr);
|
|
||||||
atomic64_dec(&pool->pages_nr);
|
|
||||||
return 0;
|
|
||||||
} else if (!test_bit(PAGE_HEADLESS, &page->private)) {
|
|
||||||
if (zhdr->first_chunks != 0 &&
|
|
||||||
zhdr->last_chunks != 0 &&
|
|
||||||
zhdr->middle_chunks != 0) {
|
|
||||||
/* Full, add to buddied list */
|
|
||||||
spin_lock(&pool->lock);
|
|
||||||
list_add(&zhdr->buddy, &pool->buddied);
|
|
||||||
spin_unlock(&pool->lock);
|
|
||||||
} else {
|
} else {
|
||||||
|
spin_lock(&pool->lock);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
z3fold_page_lock(zhdr);
|
||||||
|
if ((zhdr->first_chunks || zhdr->last_chunks ||
|
||||||
|
zhdr->middle_chunks) &&
|
||||||
|
!(zhdr->first_chunks && zhdr->last_chunks &&
|
||||||
|
zhdr->middle_chunks)) {
|
||||||
z3fold_compact_page(zhdr);
|
z3fold_compact_page(zhdr);
|
||||||
/* add to unbuddied list */
|
/* add to unbuddied list */
|
||||||
spin_lock(&pool->lock);
|
spin_lock(&pool->lock);
|
||||||
|
@ -687,13 +664,19 @@ next:
|
||||||
&pool->unbuddied[freechunks]);
|
&pool->unbuddied[freechunks]);
|
||||||
spin_unlock(&pool->lock);
|
spin_unlock(&pool->lock);
|
||||||
}
|
}
|
||||||
|
z3fold_page_unlock(zhdr);
|
||||||
|
spin_lock(&pool->lock);
|
||||||
|
if (kref_put(&zhdr->refcount, release_z3fold_page)) {
|
||||||
|
atomic64_dec(&pool->pages_nr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!test_bit(PAGE_HEADLESS, &page->private))
|
/*
|
||||||
z3fold_page_unlock(zhdr);
|
* Add to the beginning of LRU.
|
||||||
|
* Pool lock has to be kept here to ensure the page has
|
||||||
spin_lock(&pool->lock);
|
* not already been released
|
||||||
/* add to beginning of LRU */
|
*/
|
||||||
list_add(&page->lru, &pool->lru);
|
list_add(&page->lru, &pool->lru);
|
||||||
}
|
}
|
||||||
spin_unlock(&pool->lock);
|
spin_unlock(&pool->lock);
|
||||||
|
|
Loading…
Reference in New Issue