Skip to contentSkip to conversation

compressed in-memory swapping take3

PATCH In review Nitin Gupta 2 participants 7 messages Mar 24, 2009 – Mar 27, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover compressed in-memory swapping take3

Hi,

Project home: http://code.google.com/p/compcache/

It allows creating a RAM based block device which acts as swap disk. Pages swapped to this device are compressed and stored in memory itself. This is a big win over swapping to slow hard-disk which are typically used as swap disk. For flash, these suffer from wear-leveling issues when used as swap disk - so again its helpful. For swapless systems, it allows more apps to run.

* Changelog: take3 vs take2
xvmalloc changes:
- Use kernel defined macros and constants in xvmalloc and remove equivalent defines for ALIGN, roundup etc.
- Use kernel bitops (set_bit, clear_bit)
- Moved it to drivers/block since its not clear if it has any other user.
ramzswap changes:
- All instances of compcache renamed to ramzswap.
Also renamed module to ramzswap
- Renamed "backing_dev" parameter to "backing_swap"
- Documentation changes to reflect above changes.
- Remove "table index" from object header (4 bytes). This will be needed when memory defragmentation is implemented. So, avoid this (small) overhead for now.

* Changelog: take2 vs initial revision:
xvmalloc changes:
- Use Linux kernel coding style for xvmalloc
- Collapse all individual flag test/set/get to generic {test_set_get}_flag
- Added BLOCK_NEXT() macro to reach next contiguous block
- Other minor cleanups - no functional changes
compcache block device code:
- compcache core changes due to change in xvmalloc interface names

Links to performance numbers, use cases can be found in original thread:
http://lkml.org/lkml/2009/3/17/116

Thanks to Christoph, Pekka and Andrew for feedback on previous revisions.

Thanks,
Nitin

#1 lore raw

PATCH 1/3 xvmalloc memory allocator

 drivers/block/xvmalloc.c     |  564 ++++++++++++++++++++++++++++++++++++++++++
 drivers/block/xvmalloc.h     |   27 ++
 drivers/block/xvmalloc_int.h |   86 +++++++
 3 files changed, 677 insertions(+), 0 deletions(-)

xvmalloc is a memory allocator designed specifically for ramzswap project.

* Features:
- Low metadata overhead (just 4 bytes per object)
- O(1) Alloc/Free - except when we have to call system page allocator to get additional memory.
- Very low fragmentation: In all tests, xvMalloc memory usage is within 12%

    of "Ideal".

One of the main highlights is that it maps pages only when required. So, it does not hog vmalloc area which is very small on 32-bit systems.

SLUB allocator could not be used due to fragmentation issues:
http://code.google.com/p/compcache/wiki/AllocatorsComparison
Data here shows kmalloc using ~43% more memory than TLSF and xvMalloc is showed ~2% more space efficiency than TLSF (due to smaller metadata). Creating various kmem_caches can reduce space efficiency gap but still problem of being limited to low memory exists. Also, it depends on allocating higher order pages to reduce fragmentation - this is not acceptable for ramzswap as it is used under memory crunch (its a swap device!).

SLOB allocator could not be used do to reasons mentioned here:
http://lkml.org/lkml/2009/3/18/210

* Implementation:
It uses two-level bitmap search to find free list containing block of correct size. This idea is taken from TLSF (Two-Level Segregate Fit) allocator and is well explained in its paper (see [Links] below). Highlights:
- Pool based allocator: each pool can grow/shrink.
- Immediate coalescing of free blocks.
- Maps/Unmaps memory pages only when required.

* Limitations:
- Poor scalability: No per-cpu data structures (work in progress).

[Links]
1. Details and Performance data:
http://code.google.com/p/compcache/wiki/xvMalloc
http://code.google.com/p/compcache/wiki/xvMallocPerformance

2. TLSF memory allocator:
home: http://rtportal.upv.es/rtmalloc/
paper: http://rtportal.upv.es/rtmalloc/files/MRBC_2008.pdf

---

drivers/block/xvmalloc.c new file +564 -0
new file mode 100755
index 0000000..15ecce4
@@ -0,0 +1,564 @@
+/*
+ * xvmalloc.c
+ *
+ * Copyright (C) 2008, 2009  Nitin Gupta
+ *
+ * This code is released using a dual license strategy: GPL/LGPL
+ * You can choose the licence that better fits your requirements.
+ *
+ * Released under the terms of the GNU General Public License Version 2.0
+ * Released under the terms of the GNU Lesser General Public License Version 2.1
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/bitops.h>
+#include <linux/errno.h>
+#include <linux/highmem.h>
+#include <linux/init.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+
+#include "xvmalloc.h"
+#include "xvmalloc_int.h"
+
+static void stat_inc(u64 *value)
+{
+	(*value)++;
+}
+
+static void stat_dec(u64 *value)
+{
+	(*value)--;
+}
+
+static u32 test_flag(struct block_header *block, enum blockflags flag)
+{
+	return block->prev & (1 << flag);
+}
+
+static void set_flag(struct block_header *block, enum blockflags flag)
+{
+	block->prev |= (1 << flag);
+}
+
+static void clear_flag(struct block_header *block, enum blockflags flag)
+{
+	block->prev &= ~(1 << flag);
+}
+
+static u32 get_blockprev(struct block_header *block)
+{
+	return block->prev & PREV_MASK;
+}
+
+static void set_blockprev(struct block_header *block, u16 new_offset)
+{
+	block->prev = new_offset | (block->prev & FLAGS_MASK);
+}
+
+static struct block_header *BLOCK_NEXT(struct block_header *block)
+{
+	return (struct block_header *)((char *)block + block->size + XV_ALIGN);
+}
+
+/*
+ * Get index of free list containing blocks of maximum size
+ * which is less than or equal to given size.
+ */
+static u32 get_index_for_insert(u32 size)
+{
+	size = size > XV_MAX_ALLOC_SIZE ? XV_MAX_ALLOC_SIZE : size;
+	size &= ~FL_DELTA_MASK;
+	return (size - XV_MIN_ALLOC_SIZE) >> FL_DELTA_SHIFT;
+}
+
+/*
+ * Get index of free list having blocks of size greater than
+ * or equal to requested size.
+ */
+static u32 get_index(u32 size)
+{
+	size = (size + FL_DELTA_MASK) & ~FL_DELTA_MASK;
+	return (size - XV_MIN_ALLOC_SIZE) >> FL_DELTA_SHIFT;
+}
+
+/*
+ * Given <pagenum, offset> pair, provide a derefrencable pointer.
+ * This is called from xv_malloc/xv_free path, so it needs to be fast.
+ */
+static void *get_ptr_atomic(u32 pagenum, u16 offset, enum km_type type)
+{
+	unsigned char *base;
+
+	base = kmap_atomic(pfn_to_page(pagenum), type);
+	return base + offset;
+}
+
+static void put_ptr_atomic(void *ptr, enum km_type type)
+{
+	kunmap_atomic(ptr, type);
+}
+
+/*
+ * Allocate a memory page. Called when a pool needs to grow.
+ */
+static u32 xv_alloc_page(void)
+{
+	struct page *page;
+
+	page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
+
+	if (unlikely(!page))
+		return 0;
+
+	return page_to_pfn(page);
+}
+
+/*
+ * Called when all objects in a page are freed.
+ */
+static void xv_free_page(u32 pagenum)
+{
+	__free_page(pfn_to_page(pagenum));
+}
+
+/**
+ * find_block - find block of at least given size
+ * @pool: memory pool to search from
+ * @size: size of block required
+ * @pagenum: page no. containing required block
+ * @offset: offset within the page where block is located.
+ *
+ * Searches two level bitmap to locate block of at least
+ * the given size. If such a block is found, it provides
+ * <pagenum, offset> to identify this block and returns index
+ * in freelist where we found this block.
+ * Otherwise, returns 0 and <pagenum, offset> params are not touched.
+ */
+static u32 find_block(struct xv_pool *pool, u32 size,
+			u32 *pagenum, u32 *offset)
+{
+	u32 flbitmap, slbitmap;
+	u32 flindex, slindex, slbitstart;
+
+	/* There are no free blocks in this pool */
+	if (!pool->flbitmap)
+		return 0;
+
+	if (unlikely(size < XV_MIN_ALLOC_SIZE))
+		size = XV_MIN_ALLOC_SIZE;
+
+	/* Get freelist index correspoding to this size */
+	slindex = get_index(size);
+	slbitmap = pool->slbitmap[slindex / BITS_PER_LONG];
+	slbitstart = slindex % BITS_PER_LONG;
+
+	/*
+	 * If freelist is not empty at this index, we found the
+	 * block - head of this list. This is approximate best-fit match.
+	 */
+	if (slbitmap & (1 << slbitstart)) {
+		*pagenum = pool->freelist[slindex].pagenum;
+		*offset = pool->freelist[slindex].offset;
+		return slindex;
+	}
+
+	/*
+	 * No best-fit found. Search a bit further in bitmap for a free block.
+	 * Second level bitmap consists of series of 32-bit chunks. Search
+	 * further in the chunk where we expected a best-fit, starting from
+	 * index location found above.
+	 */
+	slbitstart++;
+	slbitmap >>= slbitstart;
+
+	/* Skip this search if we were already at end of this bitmap chunk */
+	if ((slbitstart != BITS_PER_LONG) && slbitmap) {
+		slindex += ffs(slbitmap);
+		*pagenum = pool->freelist[slindex].pagenum;
+		*offset = pool->freelist[slindex].offset;
+		return slindex;
+	}
+
+	/* Now do a full two-level bitmap search to find next nearest fit */
+	flindex = slindex / BITS_PER_LONG;
+
+	flbitmap = (pool->flbitmap) >> (flindex + 1);
+	if (!flbitmap)
+		return 0;
+
+	flindex += ffs(flbitmap);
+	slbitmap = pool->slbitmap[flindex];
+	slindex = (flindex * BITS_PER_LONG) + ffs(slbitmap) - 1;
+	*pagenum = pool->freelist[slindex].pagenum;
+	*offset = pool->freelist[slindex].offset;
+
+	return slindex;
+}
+
+/*
+ * Insert block at <pagenum, offset> in freelist of given pool.
+ * freelist used depends on block size.
+ */
+static void insert_block(struct xv_pool *pool, u32 pagenum, u32 offset,
+			struct block_header *block)
+{
+	u32 flindex, slindex;
+	struct block_header *nextblock;
+
+	slindex = get_index_for_insert(block->size);
+	flindex = slindex / BITS_PER_LONG;
+
+	block->link.prev_pagenum = 0;
+	block->link.prev_offset = 0;
+	block->link.next_pagenum = pool->freelist[slindex].pagenum;
+	block->link.next_offset = pool->freelist[slindex].offset;
+	pool->freelist[slindex].pagenum = pagenum;
+	pool->freelist[slindex].offset = offset;
+
+	if (block->link.next_pagenum) {
+		nextblock = get_ptr_atomic(block->link.next_pagenum,
+					block->link.next_offset, KM_USER1);
+		nextblock->link.prev_pagenum = pagenum;
+		nextblock->link.prev_offset = offset;
+		put_ptr_atomic(nextblock, KM_USER1);
+	}
+
+	__set_bit(slindex % BITS_PER_LONG, &pool->slbitmap[flindex]);
+	__set_bit(flindex, &pool->flbitmap);
+}
+
+/*
+ * Remove block from head of freelist. Index 'slindex' identifies the freelist.
+ */
+static void remove_block_head(struct xv_pool *pool,
+			struct block_header *block, u32 slindex)
+{
+	struct block_header *tmpblock;
+	u32 flindex = slindex / BITS_PER_LONG;
+
+	pool->freelist[slindex].pagenum = block->link.next_pagenum;
+	pool->freelist[slindex].offset = block->link.next_offset;
+	block->link.prev_pagenum = 0;
+	block->link.prev_offset = 0;
+
+	if (!pool->freelist[slindex].pagenum) {
+		__clear_bit(slindex % BITS_PER_LONG, &pool->slbitmap[flindex]);
+		if (!pool->slbitmap[flindex])
+			__clear_bit(flindex, &pool->flbitmap);
+	} else {
+		/*
+		 * DEBUG ONLY: We need not reinitialize freelist head previous
+		 * pointer to 0 - we never depend on its value. But just for
+		 * sanity, lets do it.
+		 */
+		tmpblock = get_ptr_atomic(pool->freelist[slindex].pagenum,
+				pool->freelist[slindex].offset, KM_USER1);
+		tmpblock->link.prev_pagenum = 0;
+		tmpblock->link.prev_offset = 0;
+		put_ptr_atomic(tmpblock, KM_USER1);
+	}
+}
+
+/*
+ * Remove block from freelist. Index 'slindex' identifies the freelist.
+ */
+static void remove_block(struct xv_pool *pool, u32 pagenum, u32 offset,
+			struct block_header *block, u32 slindex)
+{
+	u32 flindex;
+	struct block_header *tmpblock;
+
+	if (pool->freelist[slindex].pagenum == pagenum
+	   && pool->freelist[slindex].offset == offset) {
+		remove_block_head(pool, block, slindex);
+		return;
+	}
+
+	flindex = slindex / BITS_PER_LONG;
+
+	if (block->link.prev_pagenum) {
+		tmpblock = get_ptr_atomic(block->link.prev_pagenum,
+				block->link.prev_offset, KM_USER1);
+		tmpblock->link.next_pagenum = block->link.next_pagenum;
+		tmpblock->link.next_offset = block->link.next_offset;
+		put_ptr_atomic(tmpblock, KM_USER1);
+	}
+
+	if (block->link.next_pagenum) {
+		tmpblock = get_ptr_atomic(block->link.next_pagenum,
+				block->link.next_offset, KM_USER1);
+		tmpblock->link.prev_pagenum = block->link.prev_pagenum;
+		tmpblock->link.prev_offset = block->link.prev_offset;
+		put_ptr_atomic(tmpblock, KM_USER1);
+	}
+
+	return;
+}
+
+/*
+ * Allocate a page and add it freelist of given pool.
+ */
+static int grow_pool(struct xv_pool *pool)
+{
+	u32 pagenum;
+	struct block_header *block;
+
+	pagenum = xv_alloc_page();
+	if (unlikely(!pagenum))
+		return -ENOMEM;
+
+	stat_inc(&pool->total_pages);
+
+	spin_lock(&pool->lock);
+	block = get_ptr_atomic(pagenum, 0, KM_USER0);
+
+	block->size = PAGE_SIZE - XV_ALIGN;
+	set_flag(block, BLOCK_FREE);
+	clear_flag(block, PREV_FREE);
+	set_blockprev(block, 0);
+
+	insert_block(pool, pagenum, 0, block);
+
+	put_ptr_atomic(block, KM_USER0);
+	spin_unlock(&pool->lock);
+
+	return 0;
+}
+
+/*
+ * Create a memory pool. Allocates freelist, bitmaps and other
+ * per-pool metadata.
+ */
+struct xv_pool *xv_create_pool(void)
+{
+	int i;
+	u32 ovhd_size;
+	struct xv_pool *pool;
+
+	ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
+	pool = kmalloc(ovhd_size, GFP_KERNEL);
+	if (!pool)
+		return NULL;
+
+	memset(pool, 0, ovhd_size);
+
+	for (i = 0; i < NUM_FREE_LISTS; i++) {
+		pool->freelist[i].pagenum = 0;
+		pool->freelist[i].offset = 0;
+	}
+
+	spin_lock_init(&pool->lock);
+
+	return pool;
+}
+EXPORT_SYMBOL_GPL(xv_create_pool);
+
+void xv_destroy_pool(struct xv_pool *pool)
+{
+	kfree(pool);
+}
+EXPORT_SYMBOL_GPL(xv_destroy_pool);
+
+/**
+ * xvMalloc - Allocate block of given size from pool.
+ * @pool: pool to allocate from
+ * @size: size of block to allocate
+ * @pagenum: page no. that holds the object
+ * @offset: location of object within pagenum
+ *
+ * On success, <pagenum, offset> identifies block allocated
+ * and 0 is returned. On failure, <pagenum, offset> is not touched
+ * and -ENOMEM is returned.
+ *
+ * Allocation requests with size > XV_MAX_ALLOC_SIZE will fail.
+ */
+int xv_malloc(struct xv_pool *pool, u32 size, u32 *pagenum, u32 *offset)
+{
+	int error;
+	u32 index, tmpsize, origsize, tmpoffset;
+	struct block_header *block, *tmpblock = NULL;
+
+	*pagenum = 0;
+	*offset = 0;
+	origsize = size;
+
+	if (unlikely(!size || size > XV_MAX_ALLOC_SIZE))
+		return -ENOMEM;
+
+	if (unlikely(size < XV_MIN_ALLOC_SIZE))
+		size = XV_MIN_ALLOC_SIZE;
+	else
+		size = ALIGN(size, XV_ALIGN);
+
+	spin_lock(&pool->lock);
+
+	index = find_block(pool, size, pagenum, offset);
+
+	if (!*pagenum) {
+		spin_unlock(&pool->lock);
+		error = grow_pool(pool);
+		if (unlikely(error))
+			return -ENOMEM;
+
+		spin_lock(&pool->lock);
+		index = find_block(pool, size, pagenum, offset);
+	}
+
+	if (!*pagenum) {
+		spin_unlock(&pool->lock);
+		return -ENOMEM;
+	}
+
+	block = get_ptr_atomic(*pagenum, *offset, KM_USER0);
+
+	remove_block_head(pool, block, index);
+
+	/* Split the block if required */
+	tmpoffset = *offset + size + XV_ALIGN;
+	tmpsize = block->size - size;
+	tmpblock = (struct block_header *)((char *)block + size + XV_ALIGN);
+	if (tmpsize) {
+		tmpblock->size = tmpsize - XV_ALIGN;
+		set_flag(tmpblock, BLOCK_FREE);
+		clear_flag(tmpblock, PREV_FREE);
+
+		set_blockprev(tmpblock, *offset);
+		if (tmpblock->size >= XV_MIN_ALLOC_SIZE)
+			insert_block(pool, *pagenum, tmpoffset, tmpblock);
+
+		if (tmpoffset + XV_ALIGN + tmpblock->size < PAGE_SIZE) {
+			tmpblock = BLOCK_NEXT(tmpblock);
+			set_blockprev(tmpblock, tmpoffset);
+		}
+	} else {
+		/* This block is exact fit */
+		if (tmpoffset < PAGE_SIZE)
+			clear_flag(tmpblock, PREV_FREE);
+	}
+
+	block->size = origsize;
+	clear_flag(block, BLOCK_FREE);
+
+	put_ptr_atomic(block, KM_USER0);
+	spin_unlock(&pool->lock);
+
+	*offset += XV_ALIGN;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xv_malloc);
+
+/*
+ * Free block identified with <pagenum, offset>
+ */
+void xv_free(struct xv_pool *pool, u32 pagenum, u32 offset)
+{
+	void *page;
+	struct block_header *block, *tmpblock;
+
+	offset -= XV_ALIGN;
+
+	spin_lock(&pool->lock);
+
+	page = get_ptr_atomic(pagenum, 0, KM_USER0);
+	block = (struct block_header *)((char *)page + offset);
+
+	if (unlikely(block->size < XV_MIN_ALLOC_SIZE))
+		block->size = XV_MIN_ALLOC_SIZE;
+	else
+		block->size = ALIGN(block->size, XV_ALIGN);
+
+	tmpblock = BLOCK_NEXT(block);
+	if (offset + block->size + XV_ALIGN == PAGE_SIZE)
+		tmpblock = NULL;
+
+	/* Merge next block if its free */
+	if (tmpblock && test_flag(tmpblock, BLOCK_FREE)) {
+		/*
+		 * Blocks smaller than XV_MIN_ALLOC_SIZE
+		 * are not inserted in any free list.
+		 */
+		if (tmpblock->size >= XV_MIN_ALLOC_SIZE) {
+			remove_block(pool, pagenum,
+				    offset + block->size + XV_ALIGN, tmpblock,
+				    get_index_for_insert(tmpblock->size));
+		}
+		block->size += tmpblock->size + XV_ALIGN;
+	}
+
+	/* Merge previous block if its free */
+	if (test_flag(block, PREV_FREE)) {
+		tmpblock = (struct block_header *)((char *)(page) +
+						get_blockprev(block));
+		offset = offset - tmpblock->size - XV_ALIGN;
+
+		if (tmpblock->size >= XV_MIN_ALLOC_SIZE)
+			remove_block(pool, pagenum, offset, tmpblock,
+				    get_index_for_insert(tmpblock->size));
+
+		tmpblock->size += block->size + XV_ALIGN;
+		block = tmpblock;
+	}
+
+	/* No used objects in this page. Free it. */
+	if (block->size == PAGE_SIZE - XV_ALIGN) {
+		put_ptr_atomic(page, KM_USER0);
+		spin_unlock(&pool->lock);
+
+		xv_free_page(pagenum);
+		stat_dec(&pool->total_pages);
+		return;
+	}
+
+	set_flag(block, BLOCK_FREE);
+	insert_block(pool, pagenum, offset, block);
+
+	if (offset + block->size < PAGE_SIZE - XV_ALIGN) {
+		tmpblock = BLOCK_NEXT(block);
+		set_flag(tmpblock, PREV_FREE);
+		set_blockprev(tmpblock, offset);
+	}
+
+	put_ptr_atomic(page, KM_USER0);
+	spin_unlock(&pool->lock);
+
+	return;
+}
+EXPORT_SYMBOL_GPL(xv_free);
+
+u32 xv_get_object_size(void *obj)
+{
+	struct block_header *blk;
+
+	blk = (struct block_header *)((char *)(obj) - XV_ALIGN);
+	return blk->size;
+}
+EXPORT_SYMBOL_GPL(xv_get_object_size);
+
+/*
+ * Returns total memory used by allocator (userdata + metadata)
+ */
+u64 xv_get_total_size_bytes(struct xv_pool *pool)
+{
+	return pool->total_pages << PAGE_SHIFT;
+}
+EXPORT_SYMBOL_GPL(xv_get_total_size_bytes);
+
+static int __init xv_malloc_init(void)
+{
+	return 0;
+}
+
+static void __exit xv_malloc_exit(void)
+{
+	return;
+}
+
+module_init(xv_malloc_init);
+module_exit(xv_malloc_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Nitin Gupta <[email protected]>");
+MODULE_DESCRIPTION("xvmalloc memory allocator");
drivers/block/xvmalloc.h new file +27 -0
new file mode 100755
index 0000000..27aeecb
@@ -0,0 +1,27 @@
+/*
+ * xvmalloc.h
+ *
+ * Copyright (C) 2008, 2009  Nitin Gupta
+ *
+ * This code is released using a dual license strategy: GPL/LGPL
+ * You can choose the licence that better fits your requirements.
+ *
+ * Released under the terms of the GNU General Public License Version 2.0
+ * Released under the terms of the GNU Lesser General Public License Version 2.1
+ */
+
+#ifndef _XVMALLOC_H_
+#define _XVMALLOC_H_
+
+struct xv_pool;
+
+struct xv_pool *xv_create_pool(void);
+void xv_destroy_pool(struct xv_pool *pool);
+
+int xv_malloc(struct xv_pool *pool, u32 size, u32 *pagenum, u32 *offset);
+void xv_free(struct xv_pool *pool, u32 pagenum, u32 offset);
+
+u32 xv_get_object_size(void *obj);
+u64 xv_get_total_size_bytes(struct xv_pool *pool);
+
+#endif
drivers/block/xvmalloc_int.h new file +86 -0

86 lines not on this page · raw message

#2 lore raw

PATCH 2/3 compressed RAM block device

In reply to Nitin Gupta
 drivers/block/Kconfig    |   24 ++
 drivers/block/Makefile   |    1 +
 drivers/block/ramzswap.c |  984 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/block/ramzswap.h |  161 ++++++++
 4 files changed, 1170 insertions(+), 0 deletions(-)

Creates RAM based block device (ramzswap0) which can be used as swap device. Pages swapped to this are compressed and stored in memory itself.

The module is called ramzswap.ko. It depends on:
- xvmalloc.ko: memory allocator
- lzo_compress.ko
- lzo_decompress.ko

See Documentation/blockdev/ramzswap.txt for usage details.

Project home: http://code.google.com/p/compcache/

---

drivers/block/Kconfig +24 -0
index 0344a8a..08c59c4 100644
@@ -348,6 +348,30 @@ config BLK_DEV_RAM_SIZE
 	  The default value is 4096 kilobytes. Only change this if you know
 	  what you are doing.
 
+config BLK_DEV_RAMZSWAP
+	tristate "Compressed RAM swap device"
+	select LZO_COMPRESS
+	select LZO_DECOMPRESS
+	help
+	  Saying Y here will allow you to use in-memory compressed swapping.
+	  It creates a pseudo block device (named ramzswap0) which acts as
+	  swap device. Pages swapped to this device are compressed and stored
+	  in memory itself.
+
+	  Project home: http://code.google.com/p/compcache/
+	  For details, read <file:Documentation/blockdev/ramzswap.txt>
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ramzswap.
+
+config BLK_DEV_RAMZSWAP_STATS
+	bool "Collect statistics"
+	depends on BLK_DEV_RAMZSWAP
+	default y
+	help
+	  If enabled, ramzswap statistics are available via /proc/ramzswap.
+	  If unsure, say Y.
+
 config BLK_DEV_XIP
 	bool "Support XIP filesystems on RAM block device"
 	depends on BLK_DEV_RAM
drivers/block/Makefile +1 -0
index 87e120e..7e2f6b0 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_PS3_VRAM)		+= ps3vram.o
 obj-$(CONFIG_ATARI_FLOPPY)	+= ataflop.o
 obj-$(CONFIG_AMIGA_Z2RAM)	+= z2ram.o
 obj-$(CONFIG_BLK_DEV_RAM)	+= brd.o
+obj-$(CONFIG_BLK_DEV_RAMZSWAP)	+= ramzswap.o xvmalloc.o
 obj-$(CONFIG_BLK_DEV_LOOP)	+= loop.o
 obj-$(CONFIG_BLK_DEV_XD)	+= xd.o
 obj-$(CONFIG_BLK_CPQ_DA)	+= cpqarray.o
drivers/block/ramzswap.c new file +984 -0

984 lines not on this page · raw message

drivers/block/ramzswap.h new file +161 -0

161 lines not on this page · raw message

#3 lore raw

PATCH 3/3 ramzswap documentation

In reply to Nitin Gupta
 Documentation/blockdev/00-INDEX     |    2 +
 Documentation/blockdev/ramzswap.txt |   67 +++++++++++++++++++++++++++++++++++
 Documentation/kernel-parameters.txt |    9 +++++
 3 files changed, 78 insertions(+), 0 deletions(-)

Documentation on how to use ramzswap module.

---

Documentation/blockdev/00-INDEX +2 -0
index 86f054c..8751488 100644
@@ -4,6 +4,8 @@ README.DAC960
 	- info on Mylex DAC960/DAC1100 PCI RAID Controller Driver for Linux.
 cciss.txt
 	- info, major/minor #'s for Compaq's SMART Array Controllers.
+ramzswap.txt
+	- short guide on how to setup compressed RAM swap device.
 cpqarray.txt
 	- info on using Compaq's SMART2 Intelligent Disk Array Controllers.
 floppy.txt
Documentation/blockdev/ramzswap.txt new file +67 -0

67 lines not on this page · raw message

Documentation/kernel-parameters.txt +9 -0
index 54f21a5..90c0d87 100644
@@ -414,6 +414,15 @@ and is between 256 and 4096 characters. It is defined in the file
 			possible to determine what the correct size should be.
 			This option provides an override for these situations.
 
+	ramzswap.memlimit_kb=
+			See Documentation/blockdev/ramzswap.txt.
+
+	ramzswap.disksize_kb=
+			See Documentation/blockdev/ramzswap.txt.
+
+	ramzswap.backing_swap=
+			See Documentation/blockdev/ramzswap.txt.
+
 	security=	[SECURITY] Choose a security module to enable at boot.
 			If this boot parameter is not specified, only the first
 			security module asking for security registration will be
#4 lore raw
In reply to Nitin Gupta

Hi,

Maybe it simply got lost in LKML traffic.
Any chance it can make it into 2.6.30??

Thanks,
Nitin

Quoted 46 lines · Nitin Gupta

Project home: http://code.google.com/p/compcache/

It allows creating a RAM based block device which acts as swap disk. Pages swapped to this device are compressed and stored in memory itself. This is a big win over swapping to slow hard-disk which are typically used as swap disk. For flash, these suffer from wear-leveling issues when used as swap disk - so again its helpful. For swapless systems, it allows more apps to run.

* Changelog: take3 vs take2
xvmalloc changes:
- Use kernel defined macros and constants in xvmalloc and remove equivalent defines for ALIGN, roundup etc.
- Use kernel bitops (set_bit, clear_bit)
- Moved it to drivers/block since its not clear if it has any other user.
ramzswap changes:
- All instances of compcache renamed to ramzswap.
Also renamed module to ramzswap
- Renamed "backing_dev" parameter to "backing_swap"
- Documentation changes to reflect above changes.
- Remove "table index" from object header (4 bytes). This will be needed when memory defragmentation is implemented. So, avoid this (small) overhead for now.

* Changelog: take2 vs initial revision:
xvmalloc changes:
- Use Linux kernel coding style for xvmalloc
- Collapse all individual flag test/set/get to generic {test_set_get}_flag
- Added BLOCK_NEXT() macro to reach next contiguous block
- Other minor cleanups - no functional changes
compcache block device code:
- compcache core changes due to change in xvmalloc interface names

Links to performance numbers, use cases can be found in original thread:
http://lkml.org/lkml/2009/3/17/116

Thanks to Christoph, Pekka and Andrew for feedback on previous revisions.

Thanks,
Nitin

#5 lore raw
In reply to Nitin Gupta
Quoted 3 lines · On Tuesday 24 March 2009 15:01:40 Nitin Gupta wrote:

Hi,

Project home: http://code.google.com/p/compcache/

I applied this to 2.6.29 and its not working correctly

I end up with lots of:

[69054.063788] Write-error on swap-device (254:0:13912)
[69054.063820] Write-error on swap-device (254:0:13920)
[69054.063853] Write-error on swap-device (254:0:13928)
[69054.063888] Write-error on swap-device (254:0:13936)
[69054.063904] Write-error on swap-device (254:0:13944)

type messages and trying to cat /proc/ramzswap causes an opps

[85235.133090] divide error: 0000 [#1] PREEMPT SMP
[85235.133510] last sysfs file: /sys/devices/pci0000:00/0000:00:01.0/0000:01:05.0/enable
[85235.133510] CPU 0
[85235.133510] Modules linked in: ipt_MASQUERADE iptable_nat nf_nat nf_conntrack_ipv4 nf_conntrack nf_defrag_ipv4 ip_tables x_tables btrfs zlib_deflate zlib_inflate crc32c libcrc32c radeon drm ramzswap xvmalloc lzo_decompress lzo_compress bridge stp llc rfcomm bnep l2cap bluetooth tun sit tunnel4 ipv6 af_packet snd_seq_dummy snd_seq_oss snd_seq_midi_event snd_seq snd_seq_device snd_pcm_oss snd_mixer_oss lgdt330x em28xx_dvb dvb_core em28xx_alsa tuner_xc2028 edac_core tuner tvp5150 kvm_amd v4l2_common kvm video1394 sbp2 em28xx ir_common videodev v4l2_compat_ioctl32 videobuf_vmalloc videobuf_core tveeprom i2c_core pl2303 usbserial usbhid snd_hda_codec_atihdmi snd_hda_codec_realtek snd_hda_intel snd_hda_codec sr_mod usb_storage cdrom snd_pcm ohci1394 snd_timer ieee1394 psmouse snd ata_generic thermal sky2 processor wmi button soundcore pata_atiixp rtc snd_page_alloc pcspkr ehci_hcd ohci_hcd evdev unix [last unloaded: floppy]
[85235.217935] Pid: 3910, comm: cat Not tainted 2.6.29-crc-00006-gcff995c #5 System Product Name
[85235.217935] RIP: 0010:[<ffffffffa0364146>]  [<ffffffffa0364146>] proc_ramzswap_read+0xe6/0x1e0 [ramzswap]
[85235.217935] RSP: 0018:ffff8800ce1a3dc8  EFLAGS: 00010246
[85235.217935] RAX: 0000000000000000 RBX: 000000000000002c RCX: 00000000000006b5
[85235.217935] RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000064
[85235.217935] RBP: ffff8800ce1a3e48 R08: 0000000000000064 R09: 0000000000000665
[85235.217935] R10: ffff8800ed84f015 R11: ffff8800ce1a3ce8 R12: ffff8800ce1a3e8c
[85235.217935] R13: 0000000000000000 R14: ffff88006d84f000 R15: 0000000000665000
[85235.217935] FS:  00007fb6ae1e66f0(0000) GS:ffffffff8073c040(0000) knlGS:00000000f071bb90
[85235.217935] CS:  0010 DS: 0000 ES: 0000 CR0: 000000008005003b
[85235.217935] CR2: 000000000060c438 CR3: 0000000007bae000 CR4: 00000000000006e0
[85235.217935] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[85235.217935] DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
[85235.217935] Process cat (pid: 3910, threadinfo ffff8800ce1a2000, task ffff880148071620)
[85235.217935] Stack:
[85235.217935]  000a00d0000800d0 0000000000000001 0000000200000010 0000000000000000
[85235.217935]  ffffffff806b5f88 ffffffff8050ca15 ffff880149029a80 0000000000000001
[85235.217935]  ffff8801445e4060 0000000000000000 0000000000000400 0000000000000000
[85235.217935] Call Trace:
[85235.217935]  [<ffffffff8050ca15>] ? do_page_fault+0x345/0xa90
[85235.217935]  [<ffffffff80309d5f>] proc_file_read+0x1af/0x2e0
[85235.217935]  [<ffffffff80309bb0>] ? proc_file_read+0x0/0x2e0
[85235.217935]  [<ffffffff8030439c>] proc_reg_read+0x7c/0xc0
[85235.217935]  [<ffffffff802bcb88>] vfs_read+0xc8/0x180
[85235.217935]  [<ffffffff802bcd30>] sys_read+0x50/0x90
[85235.217935]  [<ffffffff8020bd1b>] system_call_fastpath+0x16/0x1b
[85235.217935] Code: 0d 20 27 00 00 4c 39 c9 0f 84 ff 00 00 00 8b 05 31 27 00 00 41 b8 64 00 00 00 8b 35 21 27 00 00 31 d2 bf 64 00 00 00 41 0f af c0 <f7> f6 31 d2 41 89 c2 8b 05 11 27 00 00 0f af c7 f7 f6 41 89 c0
[85235.217935] RIP  [<ffffffffa0364146>] proc_ramzswap_read+0xe6/0x1e0 [ramzswap]
[85235.217935]  RSP <ffff8800ce1a3dc8>
[85235.218474] ---[ end trace aa4e537802c34b22 ]---

System is amd64 with 3 cpus, 8G with hugepa and page migration enabled and used by java (always active) and kvm (on demand).

I start it with (sdb2 is a 1G swapfile):

modprobe ramzswap backing_swap=/dev/sdb2
swapon /dev/ramzswap0 -p 100

After its loaded /proc/ramzcat works. Once I've seen write error on swapdevice message it fails.

I would love to see this working.

Thanks,
Ed Tomlinson

#6 lore raw
Quoted 15 lines · Ed Tomlinson
Quoted 3 lines · On Tuesday 24 March 2009 15:01:40 Nitin Gupta wrote:

Hi,

Project home: http://code.google.com/p/compcache/

I applied this to 2.6.29 and its not working correctly

I end up with lots of:

[69054.063788] Write-error on swap-device (254:0:13912)
[69054.063820] Write-error on swap-device (254:0:13920)
[69054.063853] Write-error on swap-device (254:0:13928)
[69054.063888] Write-error on swap-device (254:0:13936)
[69054.063904] Write-error on swap-device (254:0:13944)
Quoted 5 lines

type messages and trying to cat /proc/ramzswap causes an opps

[85235.133090] divide error: 0000 [#1] PREEMPT SMP
[85235.133510] last sysfs file: /sys/devices/pci0000:00/0000:00:01.0/0000:01:05.0/enable
[85235.133510] CPU 0

Fixed. I will send out 'take4' with this fix and more cleanups.

Thanks a lot for reporting this issue.

- Nitin