Skip to contentSkip to conversation

mm: frontswap: core frontswap functionality

PATCH Superseded Dan Magenheimer 3 participants 5 messages Sep 13, 2011 Memory management

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v9 3/6 mm: frontswap: core frontswap functionality

From: Dan Magenheimer <[email protected]>
Subject: [PATCH V9 3/6] mm: frontswap: core frontswap functionality

(Note to earlier reviewers: This patchset has been reorganized due to feedback from Kame Hiroyuki and Andrew Morton. This patch contains part of patch 3of4 from the previous series.)

This third patch of six in the frontswap series provides the core frontswap code that interfaces between the hooks in the swap subsystem and a frontswap backend via frontswap_ops.

[v9: [email protected]: change "flush" to "invalidate", part 1]
[v9: [email protected]: mark some statics __read_mostly]
[v9: [email protected]: add clarifying comments]
[v9: [email protected]: no need to loop repeating try_to_unuse]
[v9: [email protected]: remove superfluous check for NULL]
[v8: rebase to 3.0-rc4]
[v8: [email protected]: add comment to clarify find_next_to_unuse]
[v7: rebase to 3.0-rc3]
[v7: [email protected]: use new static inlines, no-ops if not config'd]
[v6: rebase to 3.1-rc1]
[v6: [email protected]: use vzalloc]
[v6: [email protected]: fix null pointer deref if vzalloc fails]
[v6: [email protected]: various checks and code clarifications/comments]
[v4: rebase to 2.6.39]
Cc Jeremy Fitzhardinge <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Nitin Gupta <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Chris Mason <[email protected]>
Cc Rik Riel <[email protected]>
Cc Andrew Morton <[email protected]>
mm/frontswap.c +275 -0
@@ -0,0 +1,275 @@
+/*
+ * Frontswap frontend
+ *
+ * This code provides the generic "frontend" layer to call a matching
+ * "backend" driver implementation of frontswap.  See
+ * Documentation/vm/frontswap.txt for more information.
+ *
+ * Copyright (C) 2009-2010 Oracle Corp.  All rights reserved.
+ * Author: Dan Magenheimer
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.
+ */
+
+#include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
+#include <linux/proc_fs.h>
+#include <linux/security.h>
+#include <linux/capability.h>
+#include <linux/module.h>
+#include <linux/uaccess.h>
+#include <linux/debugfs.h>
+#include <linux/frontswap.h>
+#include <linux/swapfile.h>
+
+/*
+ * frontswap_ops is set by frontswap_register_ops to contain the pointers
+ * to the frontswap "backend" implementation functions.
+ */
+static struct frontswap_ops frontswap_ops __read_mostly;
+
+/*
+ * This global enablement flag reduces overhead on systems where frontswap_ops
+ * has not been registered, so is preferred to the slower alternative: a
+ * function call that checks a non-global.
+ */
+int frontswap_enabled __read_mostly;
+EXPORT_SYMBOL(frontswap_enabled);
+
+/*
+ * Counters available via /sys/kernel/debug/frontswap (if debugfs is
+ * properly configured.  These are for information only so are not protected
+ * against increment races.
+ */
+static unsigned long frontswap_gets;
+static unsigned long frontswap_succ_puts;
+static unsigned long frontswap_failed_puts;
+static unsigned long frontswap_invalidates;
+
+/*
+ * Register operations for frontswap, returning previous thus allowing
+ * detection of multiple backends and possible nesting
+ */
+struct frontswap_ops frontswap_register_ops(struct frontswap_ops *ops)
+{
+	struct frontswap_ops old = frontswap_ops;
+
+	frontswap_ops = *ops;
+	frontswap_enabled = 1;
+	return old;
+}
+EXPORT_SYMBOL(frontswap_register_ops);
+
+/* Called when a swap device is swapon'd */
+void __frontswap_init(unsigned type)
+{
+	struct swap_info_struct *sis = swap_info[type];
+
+	BUG_ON(sis == NULL);
+	if (sis->frontswap_map == NULL)
+		return;
+	if (frontswap_enabled)
+		(*frontswap_ops.init)(type);
+}
+EXPORT_SYMBOL(__frontswap_init);
+
+/*
+ * "Put" data from a page to frontswap and associate it with the page's
+ * swaptype and offset.  Page must be locked and in the swap cache.
+ * If frontswap already contains a page with matching swaptype and
+ * offset, the frontswap implmentation may either overwrite the data and
+ * return success or invalidate the page from frontswap and return failure
+ */
+int __frontswap_put_page(struct page *page)
+{
+	int ret = -1, dup = 0;
+	swp_entry_t entry = { .val = page_private(page), };
+	int type = swp_type(entry);
+	struct swap_info_struct *sis = swap_info[type];
+	pgoff_t offset = swp_offset(entry);
+
+	BUG_ON(!PageLocked(page));
+	BUG_ON(sis == NULL);
+	if (frontswap_test(sis, offset))
+		dup = 1;
+	ret = (*frontswap_ops.put_page)(type, offset, page);
+	if (ret == 0) {
+		frontswap_set(sis, offset);
+		frontswap_succ_puts++;
+		if (!dup)
+			atomic_inc(&sis->frontswap_pages);
+	} else if (dup) {
+		/*
+		  failed dup always results in automatic invalidate of
+		  the (older) page from frontswap
+		 */
+		frontswap_clear(sis, offset);
+		atomic_dec(&sis->frontswap_pages);
+		frontswap_failed_puts++;
+	} else
+		frontswap_failed_puts++;
+	return ret;
+}
+EXPORT_SYMBOL(__frontswap_put_page);
+
+/*
+ * "Get" data from frontswap associated with swaptype and offset that were
+ * specified when the data was put to frontswap and use it to fill the
+ * specified page with data. Page must be locked and in the swap cache
+ */
+int __frontswap_get_page(struct page *page)
+{
+	int ret = -1;
+	swp_entry_t entry = { .val = page_private(page), };
+	int type = swp_type(entry);
+	struct swap_info_struct *sis = swap_info[type];
+	pgoff_t offset = swp_offset(entry);
+
+	BUG_ON(!PageLocked(page));
+	BUG_ON(sis == NULL);
+	if (frontswap_test(sis, offset))
+		ret = (*frontswap_ops.get_page)(type, offset, page);
+	if (ret == 0)
+		frontswap_gets++;
+	return ret;
+}
+EXPORT_SYMBOL(__frontswap_get_page);
+
+/*
+ * Invalidate any data from frontswap associated with the specified swaptype
+ * and offset so that a subsequent "get" will fail.
+ */
+void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
+{
+	struct swap_info_struct *sis = swap_info[type];
+
+	BUG_ON(sis == NULL);
+	if (frontswap_test(sis, offset)) {
+		(*frontswap_ops.flush_page)(type, offset);
+		atomic_dec(&sis->frontswap_pages);
+		frontswap_clear(sis, offset);
+		frontswap_invalidates++;
+	}
+}
+EXPORT_SYMBOL(__frontswap_invalidate_page);
+
+/*
+ * Invalidate all data from frontswap associated with all offsets for the
+ * specified swaptype.
+ */
+void __frontswap_invalidate_area(unsigned type)
+{
+	struct swap_info_struct *sis = swap_info[type];
+
+	BUG_ON(sis == NULL);
+	if (sis->frontswap_map == NULL)
+		return;
+	(*frontswap_ops.flush_area)(type);
+	atomic_set(&sis->frontswap_pages, 0);
+	memset(sis->frontswap_map, 0, sis->max / sizeof(long));
+}
+EXPORT_SYMBOL(__frontswap_invalidate_area);
+
+/*
+ * Frontswap, like a true swap device, may unnecessarily retain pages
+ * under certain circumstances; "shrink" frontswap is essentially a
+ * "partial swapoff" and works by calling try_to_unuse to attempt to
+ * unuse enough frontswap pages to attempt to -- subject to memory
+ * constraints -- reduce the number of pages in frontswap to the
+ * number given in the parameter target_pages.
+ */
+void frontswap_shrink(unsigned long target_pages)
+{
+	struct swap_info_struct *si = NULL;
+	int si_frontswap_pages;
+	unsigned long total_pages = 0, total_pages_to_unuse;
+	unsigned long pages = 0, pages_to_unuse = 0;
+	int type;
+	bool locked = false;
+
+	/*
+	 * we don't want to hold swap_lock while doing a very
+	 * lengthy try_to_unuse, but swap_list may change
+	 * so restart scan from swap_list.head each time
+	 */
+	spin_lock(&swap_lock);
+	locked = true;
+	total_pages = 0;
+	for (type = swap_list.head; type >= 0; type = si->next) {
+		si = swap_info[type];
+		total_pages += atomic_read(&si->frontswap_pages);
+	}
+	if (total_pages <= target_pages)
+		goto out;
+	total_pages_to_unuse = total_pages - target_pages;
+	for (type = swap_list.head; type >= 0; type = si->next) {
+		si = swap_info[type];
+		si_frontswap_pages = atomic_read(&si->frontswap_pages);
+		if (total_pages_to_unuse < si_frontswap_pages)
+			pages = pages_to_unuse = total_pages_to_unuse;
+		else {
+			pages = si_frontswap_pages;
+			pages_to_unuse = 0; /* unuse all */
+		}
+		/* ensure there is enough RAM to fetch pages from frontswap */
+		if (security_vm_enough_memory_kern(pages))
+			continue;
+		vm_unacct_memory(pages);
+		break;
+	}
+	if (type < 0)
+		goto out;
+	locked = false;
+	spin_unlock(&swap_lock);
+	try_to_unuse(type, true, pages_to_unuse);
+out:
+	if (locked)
+		spin_unlock(&swap_lock);
+	return;
+}
+EXPORT_SYMBOL(frontswap_shrink);
+
+/*
+ * Count and return the number of frontswap pages across all
+ * swap devices.  This is exported so that backend drivers can
+ * determine current usage without reading debugfs.
+ */
+unsigned long frontswap_curr_pages(void)
+{
+	int type;
+	unsigned long totalpages = 0;
+	struct swap_info_struct *si = NULL;
+
+	spin_lock(&swap_lock);
+	for (type = swap_list.head; type >= 0; type = si->next) {
+		si = swap_info[type];
+		totalpages += atomic_read(&si->frontswap_pages);
+	}
+	spin_unlock(&swap_lock);
+	return totalpages;
+}
+EXPORT_SYMBOL(frontswap_curr_pages);
+
+static int __init init_frontswap(void)
+{
+	int err = 0;
+
+#ifdef CONFIG_DEBUG_FS
+	struct dentry *root = debugfs_create_dir("frontswap", NULL);
+	if (root == NULL)
+		return -ENXIO;
+	debugfs_create_size_t("gets", S_IRUGO,
+				root, &frontswap_gets);
+	debugfs_create_size_t("succ_puts", S_IRUGO,
+				root, &frontswap_succ_puts);
+	debugfs_create_size_t("puts", S_IRUGO,
+				root, &frontswap_failed_puts);
+	debugfs_create_size_t("invalidates", S_IRUGO,
+				root, &frontswap_invalidates);
+#endif
+	return err;
+}
+
+module_init(init_frontswap);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

Hey Dan,

I get the following compile warnings:

mm/frontswap.c: In function ‘init_frontswap’:
mm/frontswap.c:264:5: warning: passing argument 4 of ‘debugfs_create_size_t’ from incompatible pointer type include/linux/debugfs.h:68:16: note: expected ‘size_t *’ but argument is of type ‘long unsigned int *’ mm/frontswap.c:266:5: warning: passing argument 4 of ‘debugfs_create_size_t’ from incompatible pointer type include/linux/debugfs.h:68:16: note: expected ‘size_t *’ but argument is of type ‘long unsigned int *’ mm/frontswap.c:268:5: warning: passing argument 4 of ‘debugfs_create_size_t’ from incompatible pointer type include/linux/debugfs.h:68:16: note: expected ‘size_t *’ but argument is of type ‘long unsigned int *’ mm/frontswap.c:270:5: warning: passing argument 4 of ‘debugfs_create_size_t’ from incompatible pointer type include/linux/debugfs.h:68:16: note: expected ‘size_t *’ but argument is of type ‘long unsigned int *’

size_t is platform dependent but is generally "unsigned int" for 32-bit and "unsigned long" for 64-bit.

I think just typecasting these to size_t * would fix it.

Quoted 13 lines · Dan Magenheimer
mm/frontswap.c +13 -0
+#ifdef CONFIG_DEBUG_FS
+	struct dentry *root = debugfs_create_dir("frontswap", NULL);
+	if (root == NULL)
+		return -ENXIO;
+	debugfs_create_size_t("gets", S_IRUGO,
+				root, &frontswap_gets);
+	debugfs_create_size_t("succ_puts", S_IRUGO,
+				root, &frontswap_succ_puts);
+	debugfs_create_size_t("puts", S_IRUGO,
+				root, &frontswap_failed_puts);
+	debugfs_create_size_t("invalidates", S_IRUGO,
+				root, &frontswap_invalidates);
+#endif
#2 lore raw
Quoted 7 lines

From: Seth Jennings [mailto:[email protected]]
Subject: Re: [PATCH V9 3/6] mm: frontswap: core frontswap functionality

Hey Dan,

I get the following compile warnings:

Quoted 13 lines

mm/frontswap.c: In function 'init_frontswap':
mm/frontswap.c:264:5: warning: passing argument 4 of 'debugfs_create_size_t' from incompatible pointer type
include/linux/debugfs.h:68:16: note: expected 'size_t *' but argument is of type 'long unsigned int *' mm/frontswap.c:266:5: warning: passing argument 4 of 'debugfs_create_size_t' from incompatible pointer type
include/linux/debugfs.h:68:16: note: expected 'size_t *' but argument is of type 'long unsigned int *' mm/frontswap.c:268:5: warning: passing argument 4 of 'debugfs_create_size_t' from incompatible pointer type
include/linux/debugfs.h:68:16: note: expected 'size_t *' but argument is of type 'long unsigned int *' mm/frontswap.c:270:5: warning: passing argument 4 of 'debugfs_create_size_t' from incompatible pointer type
include/linux/debugfs.h:68:16: note: expected 'size_t *' but argument is of type 'long unsigned int *'

Thanks for checking on 32-bit!

Quoted 4 lines

size_t is platform dependent but is generally "unsigned int" for 32-bit and "unsigned long" for 64-bit.

I think just typecasting these to size_t * would fix it.

Actually, I think the best fix is likely to change the variables and the debugfs calls to u64 since even on 32-bit, the counters may exceed 2**32 on a heavily-loaded long-running system.

I'll give it a day or two to see if anyone else has any feedback before I fix this for V10.

Dan

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
In reply to Seth Jennings
Quoted 14 lines · On Tue, 13 Sep 2011 15:40:36 -0500 Seth Jennings <[email protected]>…

Hey Dan,

I get the following compile warnings:

mm/frontswap.c: In function a??init_frontswapa??:
mm/frontswap.c:264:5: warning: passing argument 4 of a??debugfs_create_size_ta?? from incompatible pointer type include/linux/debugfs.h:68:16: note: expected a??size_t *a?? but argument is of type a??long unsigned int *a?? mm/frontswap.c:266:5: warning: passing argument 4 of a??debugfs_create_size_ta?? from incompatible pointer type include/linux/debugfs.h:68:16: note: expected a??size_t *a?? but argument is of type a??long unsigned int *a?? mm/frontswap.c:268:5: warning: passing argument 4 of a??debugfs_create_size_ta?? from incompatible pointer type include/linux/debugfs.h:68:16: note: expected a??size_t *a?? but argument is of type a??long unsigned int *a?? mm/frontswap.c:270:5: warning: passing argument 4 of a??debugfs_create_size_ta?? from incompatible pointer type include/linux/debugfs.h:68:16: note: expected a??size_t *a?? but argument is of type a??long unsigned int *a??

Quoted 4 lines

size_t is platform dependent but is generally "unsigned int" for 32-bit and "unsigned long" for 64-bit.

I think just typecasting these to size_t * would fix it.

That's very risky.

Quoted 6 lines · Dan Magenheimer
Quoted 5 lines
mm/frontswap.c +5 -0
+#ifdef CONFIG_DEBUG_FS
+	struct dentry *root = debugfs_create_dir("frontswap", NULL);
+	if (root == NULL)
+		return -ENXIO;
+	debugfs_create_size_t("gets", S_IRUGO,
Quoted 8 lines

mm/frontswap.c line 271

Quoted 8 lines
mm/frontswap.c +8 -0
+				root, &frontswap_gets);
+	debugfs_create_size_t("succ_puts", S_IRUGO,
+				root, &frontswap_succ_puts);
+	debugfs_create_size_t("puts", S_IRUGO,
+				root, &frontswap_failed_puts);
+	debugfs_create_size_t("invalidates", S_IRUGO,
+				root, &frontswap_invalidates);
+#endif

Make them u32 and use debugfs_create_x32(), perhaps. Or create debugfs_create_ulong().

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
In reply to Dan Magenheimer
Quoted 26 lines · Dan Magenheimer
Quoted 7 lines

From: Seth Jennings [mailto:[email protected]]
Subject: Re: [PATCH V9 3/6] mm: frontswap: core frontswap functionality

Hey Dan,

I get the following compile warnings:

Quoted 13 lines

mm/frontswap.c: In function 'init_frontswap':
mm/frontswap.c:264:5: warning: passing argument 4 of 'debugfs_create_size_t' from incompatible pointer type
include/linux/debugfs.h:68:16: note: expected 'size_t *' but argument is of type 'long unsigned int *' mm/frontswap.c:266:5: warning: passing argument 4 of 'debugfs_create_size_t' from incompatible pointer type
include/linux/debugfs.h:68:16: note: expected 'size_t *' but argument is of type 'long unsigned int *' mm/frontswap.c:268:5: warning: passing argument 4 of 'debugfs_create_size_t' from incompatible pointer type
include/linux/debugfs.h:68:16: note: expected 'size_t *' but argument is of type 'long unsigned int *' mm/frontswap.c:270:5: warning: passing argument 4 of 'debugfs_create_size_t' from incompatible pointer type
include/linux/debugfs.h:68:16: note: expected 'size_t *' but argument is of type 'long unsigned int *'

Thanks for checking on 32-bit!

Quoted 3 lines

size_t is platform dependent but is generally "unsigned int" for 32-bit and "unsigned long" for 64-bit.

Quoted 7 lines
Quoted 1 line

I think just typecasting these to size_t * would fix it.

Actually, I think the best fix is likely to change the variables and the debugfs calls to u64 since even on 32-bit, the counters may exceed 2**32 on a heavily-loaded long-running system.

That was going to be my other suggestion :) I thought I'd suggest the route that didn't involve you having to retype the counters. But the u64 solution is cleaner and, as Andrew pointed out, less risky.

Quoted 4 lines

I'll give it a day or two to see if anyone else has any feedback before I fix this for V10.

Dan