Skip to contentSkip to conversation

mm: frontswap: core code

PATCH Superseded Dan Magenheimer 2 participants 4 messages Aug 29, 2011 – Sep 8, 2011 Memory management

v8 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 2/4 mm: frontswap: core code

From: Dan Magenheimer <[email protected]>
Subject: [PATCH V8 2/4] mm: frontswap: core code

This second patch of four in this frontswap series provides the core code for frontswap that interfaces between the hooks in the swap subsystem and a frontswap backend via frontswap_ops.

Two new files are added: mm/frontswap.c and include/linux/frontswap.h

Credits: Frontswap_ops design derived from Jeremy Fitzhardinge design for tmem; sysfs code modelled after mm/ksm.c

[v8: rebase to 3.0-rc4]
[v8: [email protected]: change count to atomic_t to avoid races]
[v7: rebase to 3.0-rc3]
[v7: [email protected]: new static inlines resolve to no-ops if not config'd]
[v7: [email protected]: avoid redundant shifts/divides for *_bit lib calls]
[v6: rebase to 3.1-rc1]
[v6: [email protected]: fix null pointer deref if vzalloc fails]
[v6: [email protected]: various checks and code clarifications/comments]
[v5: no change from v4]
[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]>
include/linux/frontswap.h +125 -0
@@ -0,0 +1,125 @@
+#ifndef _LINUX_FRONTSWAP_H
+#define _LINUX_FRONTSWAP_H
+
+#include <linux/swap.h>
+#include <linux/mm.h>
+
+struct frontswap_ops {
+	void (*init)(unsigned);
+	int (*put_page)(unsigned, pgoff_t, struct page *);
+	int (*get_page)(unsigned, pgoff_t, struct page *);
+	void (*flush_page)(unsigned, pgoff_t);
+	void (*flush_area)(unsigned);
+};
+
+extern int frontswap_enabled;
+extern struct frontswap_ops
+	frontswap_register_ops(struct frontswap_ops *ops);
+extern void frontswap_shrink(unsigned long);
+extern unsigned long frontswap_curr_pages(void);
+
+extern void __frontswap_init(unsigned type);
+extern int __frontswap_put_page(struct page *page);
+extern int __frontswap_get_page(struct page *page);
+extern void __frontswap_flush_page(unsigned, pgoff_t);
+extern void __frontswap_flush_area(unsigned);
+
+#ifdef CONFIG_FRONTSWAP
+
+static inline int frontswap_test(struct swap_info_struct *sis, pgoff_t offset)
+{
+	int ret = 0;
+
+	if (frontswap_enabled && sis->frontswap_map)
+		ret = test_bit(offset, sis->frontswap_map);
+	return ret;
+}
+
+static inline void frontswap_set(struct swap_info_struct *sis, pgoff_t offset)
+{
+	if (frontswap_enabled && sis->frontswap_map)
+		set_bit(offset, sis->frontswap_map);
+}
+
+static inline void frontswap_clear(struct swap_info_struct *sis, pgoff_t offset)
+{
+	if (frontswap_enabled && sis->frontswap_map)
+		clear_bit(offset, sis->frontswap_map);
+}
+
+static inline void frontswap_map_set(struct swap_info_struct *p,
+				     unsigned long *map)
+{
+	p->frontswap_map = map;
+}
+
+static inline unsigned long *frontswap_map_get(struct swap_info_struct *p)
+{
+	return p->frontswap_map;
+}
+#else
+/* all inline routines become no-ops and all externs are ignored */
+
+#define frontswap_enabled (0)
+
+static inline int frontswap_test(struct swap_info_struct *sis, pgoff_t offset)
+{
+	return 0;
+}
+
+static inline void frontswap_set(struct swap_info_struct *sis, pgoff_t offset)
+{
+}
+
+static inline void frontswap_clear(struct swap_info_struct *sis, pgoff_t offset)
+{
+}
+
+static inline void frontswap_map_set(struct swap_info_struct *p,
+				     unsigned long *map)
+{
+}
+
+static inline unsigned long *frontswap_map_get(struct swap_info_struct *p)
+{
+	return NULL;
+}
+#endif
+
+static inline int frontswap_put_page(struct page *page)
+{
+	int ret = -1;
+
+	if (frontswap_enabled)
+		ret = __frontswap_put_page(page);
+	return ret;
+}
+
+static inline int frontswap_get_page(struct page *page)
+{
+	int ret = -1;
+
+	if (frontswap_enabled)
+		ret = __frontswap_get_page(page);
+	return ret;
+}
+
+static inline void frontswap_flush_page(unsigned type, pgoff_t offset)
+{
+	if (frontswap_enabled)
+		__frontswap_flush_page(type, offset);
+}
+
+static inline void frontswap_flush_area(unsigned type)
+{
+	if (frontswap_enabled)
+		__frontswap_flush_area(type);
+}
+
+static inline void frontswap_init(unsigned type)
+{
+	if (frontswap_enabled)
+		__frontswap_init(type);
+}
+
+#endif /* _LINUX_FRONTSWAP_H */
mm/frontswap.c +351 -0

351 lines not on this page · raw message

--
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>

maintainer
#1 lore raw
Quoted 21 lines · On Mon, 29 Aug 2011 09:49:09 -0700 Dan Magenheimer…

From: Dan Magenheimer <[email protected]>
Subject: [PATCH V8 2/4] mm: frontswap: core code

This second patch of four in this frontswap series provides the core code for frontswap that interfaces between the hooks in the swap subsystem and a frontswap backend via frontswap_ops.

Two new files are added: mm/frontswap.c and include/linux/frontswap.h

Credits: Frontswap_ops design derived from Jeremy Fitzhardinge design for tmem; sysfs code modelled after mm/ksm.c

...

include/linux/frontswap.h +3 -0
@@ -0,0 +1,125 @@
+#ifndef _LINUX_FRONTSWAP_H
+#define _LINUX_FRONTSWAP_H
+

include/linux/frontswap.h line 13

Quoted 10 lines
include/linux/frontswap.h +10 -0
+#include <linux/swap.h>
+#include <linux/mm.h>
+
+struct frontswap_ops {
+	void (*init)(unsigned);
+	int (*put_page)(unsigned, pgoff_t, struct page *);
+	int (*get_page)(unsigned, pgoff_t, struct page *);
+	void (*flush_page)(unsigned, pgoff_t);
+	void (*flush_area)(unsigned);
+};

Please don't use the term "flush". In both the pagecache code and the pte code it is interchangably used to refer to both writeback and invalidation. The way to avoid this ambiguity and confusion is to use the terms "writeback" and "invalidate" instead.

Here, you're referring to invalidation.

include/linux/frontswap.h

Quoted 8 lines

...

include/linux/frontswap.h +5 -0
+/*
+ * 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?

include/linux/frontswap.h

Quoted 6 lines
include/linux/frontswap.h +6 -0
+/*
+ * 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?

bool?

include/linux/frontswap.h

Quoted 10 lines
include/linux/frontswap.h +10 -0
+EXPORT_SYMBOL(frontswap_enabled);
+
+/*
+ * Useful stats available in /sys/kernel/mm/frontswap.  These are for
+ * information only so are not protected against increment/decrement races.
+ */
+static unsigned long frontswap_gets;
+static unsigned long frontswap_succ_puts;
+static unsigned long frontswap_failed_puts;
+static unsigned long frontswap_flushes;

If they're in /sys/kernel/mm then they rather become permanent parts of the exported kernel interface. We're stuck with them. Plus they're inaccurate and updating them might be inefficient, so we don't want to be stuck with them.

I suggest moving these to debugfs from where we can remove them if we feel like doing so.

include/linux/frontswap.h

Quoted 11 lines

...

include/linux/frontswap.h +8 -0
+/*
+ * 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
+ */
+void frontswap_shrink(unsigned long target_pages)

It's unclear whether `target_pages' refers to the number of pages to remove or to the number of pages to retain. A comment is needed.

include/linux/frontswap.h

Quoted 5 lines
include/linux/frontswap.h +5 -0
+{
+	int wrapped = 0;
+	bool locked = false;
+
+	/* try a few times to maximize chance of try_to_unuse success */

Why? Is this necessary? How often does try_to_unuse fail?

include/linux/frontswap.h

Quoted 1 line
include/linux/frontswap.h +1 -0
+	for (wrapped = 0; wrapped < 3; wrapped++) {

`wrapped' seems an inappropriate identifier.

Quoted 1 line

+

unneeded newline

Quoted 21 lines
include/linux/frontswap.h +21 -0
+		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;
+
+		/*
+		 * 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;

include/linux/frontswap.h

Quoted 10 lines
include/linux/frontswap.h +10 -0
+		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 */
+			}
+			if (security_vm_enough_memory_kern(pages))

What's this doing here? Needs a comment please.

include/linux/frontswap.h

Quoted 2 lines
include/linux/frontswap.h +2 -0
+				continue;
+			vm_unacct_memory(pages);

hm, is that accurate? Or should we account for the pages which try_to_unuse() actually unused?

Quoted 8 lines
include/linux/frontswap.h +8 -0
+			break;
+		}
+		if (type < 0)
+			goto out;
+		locked = false;
+		spin_unlock(&swap_lock);
+		try_to_unuse(type, true, pages_to_unuse);
+	}

include/linux/frontswap.h

Quoted 10 lines
include/linux/frontswap.h +10 -0
+
+out:
+	if (locked)
+		spin_unlock(&swap_lock);
+	return;
+}
+EXPORT_SYMBOL(frontswap_shrink);
+
+/*
+ * Count and return the number of pages frontswap pages across all

s/pages//

Quoted 2 lines

+ * swap devices. This is exported so that a kernel module can
+ * determine current usage without reading sysfs.

Which kernel module might want to do this?

Quoted 9 lines

+ */

include/linux/frontswap.h +8 -0
+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) {

include/linux/frontswap.h

Quoted 10 lines
include/linux/frontswap.h +10 -0
+		si = swap_info[type];
+		if (si != NULL)
+			totalpages += atomic_read(&si->frontswap_pages);
+	}
+	spin_unlock(&swap_lock);
+	return totalpages;
+}
+EXPORT_SYMBOL(frontswap_curr_pages);
+
+#ifdef CONFIG_SYSFS

Has the code been tested with CONFIG_SYSFS=n?

Quoted 3 lines

...

--
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>

#2 lore raw
Quoted 2 lines

From: Andrew Morton [mailto:[email protected]]
Subject: Re: [PATCH V8 2/4] mm: frontswap: core code

Thanks very much for taking the time for this feedback!

Please correct me if I am presumptuous or misreading SubmittingPatches, but after making the changes below, I am thinking this constitutes a "Reviewed-by"?

Quoted 19 lines

include/linux/frontswap.h line 13

Quoted 13 lines

From: Dan Magenheimer <[email protected]>
Subject: [PATCH V8 2/4] mm: frontswap: core code

This second patch of four in this frontswap series provides the core code for frontswap that interfaces between the hooks in the swap subsystem and

include/linux/frontswap.h +8 -0
+
+struct frontswap_ops {
+	void (*init)(unsigned);
+	int (*put_page)(unsigned, pgoff_t, struct page *);
+	int (*get_page)(unsigned, pgoff_t, struct page *);
+	void (*flush_page)(unsigned, pgoff_t);
+	void (*flush_area)(unsigned);
+};

Please don't use the term "flush". In both the pagecache code and the pte code it is interchangably used to refer to both writeback and invalidation. The way to avoid this ambiguity and confusion is to use the terms "writeback" and "invalidate" instead.

Quoted 1 line

Here, you're referring to invalidation.

While the different name is OK, changing this consistently would now require simultaneous patches in cleancache, zcache, and xen (not to mention lots of docs inside and outside the kernel). I suspect it would be cleaner to do this later across all affected code with a single commit. Hope that's OK.

(Personally, I find "invalidate" to be inaccurate because common usage of the term doesn't imply that the space used in the cache is recovered, i.e. garbage collection, which is the case here. To me, "flush" implies invalidate PLUS recover space.)

Quoted 3 lines
Quoted 1 line

+static struct frontswap_ops frontswap_ops;

__read_mostly?

Yep. Will fix.

Quoted 8 lines

mm/frontswap.c line 38

Quoted 6 lines
mm/frontswap.c +6 -0
+/*
+ * 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?

Yep. Will fix.

Quoted 14 lines

mm/frontswap.c line 48

Quoted 8 lines
mm/frontswap.c +8 -0
+/*
+ * Useful stats available in /sys/kernel/mm/frontswap.  These are for
+ * information only so are not protected against increment/decrement races.
+ */
+static unsigned long frontswap_gets;
+static unsigned long frontswap_succ_puts;
+static unsigned long frontswap_failed_puts;
+static unsigned long frontswap_flushes;

If they're in /sys/kernel/mm then they rather become permanent parts of the exported kernel interface. We're stuck with them. Plus they're inaccurate and updating them might be inefficient, so we don't want to be stuck with them.

Quoted 2 lines

I suggest moving these to debugfs from where we can remove them if we feel like doing so.

The style (and code) for this was mimicked from ksm and hugepages, which expose the stats the same way... as does cleancache now. slub is also similar. I'm OK with using a different approach (e.g. debugfs), but think it would be inconsistent and confusing to expose these stats differently than cleancache (or ksm and hugepages). I'd support and help with a massive cleanup commit across all of mm later though. Hope that's OK for now.

Quoted 11 lines

mm/frontswap.c line 181

Quoted 8 lines
mm/frontswap.c +8 -0
+/*
+ * 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
+ */
+void frontswap_shrink(unsigned long target_pages)

It's unclear whether `target_pages' refers to the number of pages to remove or to the number of pages to retain. A comment is needed.

OK. Will fix.

Quoted 11 lines

mm/frontswap.c line 186

Quoted 5 lines
mm/frontswap.c +5 -0
+{
+	int wrapped = 0;
+	bool locked = false;
+
+	/* try a few times to maximize chance of try_to_unuse success */

Why? Is this necessary? How often does try_to_unuse fail?

mm/frontswap.c line 187

Quoted 1 line
mm/frontswap.c +1 -0
+	for (wrapped = 0; wrapped < 3; wrapped++) {

`wrapped' seems an inappropriate identifier.

Hmmm... this loop was mimicking some swap code that now seems to be long gone. I agree it's not really necessary and will remove the loop (which also removes the identifier).

Quoted 3 lines
Quoted 1 line

+

unneeded newline

Doh! Will fix.

Quoted 9 lines

mm/frontswap.c line 219

Quoted 1 line
mm/frontswap.c +1 -0
+			if (security_vm_enough_memory_kern(pages))

What's this doing here? Needs a comment please.

mm/frontswap.c line 221

Quoted 2 lines
mm/frontswap.c +2 -0
+				continue;
+			vm_unacct_memory(pages);

hm, is that accurate? Or should we account for the pages which try_to_unuse() actually unused?

These are mimicking code in swapoff. I think the code is correct but agree a clarifying comment or two is in order.

Quoted 4 lines
Quoted 2 lines

+/*
+ * Count and return the number of pages frontswap pages across all

s/pages//

Doh! Will fix.

Quoted 4 lines
Quoted 2 lines

+ * swap devices. This is exported so that a kernel module can
+ * determine current usage without reading sysfs.

Which kernel module might want to do this?

The tmem backends (currently zcache and xen tmem). I'll change the wording in the comment to clarify.

Quoted 3 lines
Quoted 1 line

+#ifdef CONFIG_SYSFS

Has the code been tested with CONFIG_SYSFS=n?

Yes, in a previous version. I'll doublecheck though.

Thanks again for the feedback! I'll publish a V9 with the corrections no later than early next week.

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>

maintainer
#3 lore raw
Quoted 8 lines · On Thu, 8 Sep 2011 08:00:36 -0700 (PDT) Dan Magenheimer…
Quoted 2 lines

From: Andrew Morton [mailto:[email protected]]
Subject: Re: [PATCH V8 2/4] mm: frontswap: core code

Thanks very much for taking the time for this feedback!

Please correct me if I am presumptuous or misreading SubmittingPatches, but after making the changes below, I am thinking this constitutes a "Reviewed-by"?

Not really. More like Briefly-browsed-by:.

Quoted 19 lines
Quoted 19 lines

include/linux/frontswap.h line 13

Quoted 13 lines

From: Dan Magenheimer <[email protected]>
Subject: [PATCH V8 2/4] mm: frontswap: core code

This second patch of four in this frontswap series provides the core code for frontswap that interfaces between the hooks in the swap subsystem and

include/linux/frontswap.h +8 -0
+
+struct frontswap_ops {
+	void (*init)(unsigned);
+	int (*put_page)(unsigned, pgoff_t, struct page *);
+	int (*get_page)(unsigned, pgoff_t, struct page *);
+	void (*flush_page)(unsigned, pgoff_t);
+	void (*flush_area)(unsigned);
+};

Please don't use the term "flush". In both the pagecache code and the pte code it is interchangably used to refer to both writeback and invalidation. The way to avoid this ambiguity and confusion is to use the terms "writeback" and "invalidate" instead.

Quoted 7 lines
Quoted 1 line

Here, you're referring to invalidation.

While the different name is OK, changing this consistently would now require simultaneous patches in cleancache, zcache, and xen (not to mention lots of docs inside and outside the kernel). I suspect it would be cleaner to do this later across all affected code with a single commit. Hope that's OK.

Well, if you can make that happen...

Quoted 4 lines

(Personally, I find "invalidate" to be inaccurate because common usage of the term doesn't imply that the space used in the cache is recovered, i.e. garbage collection, which is the case here. To me, "flush" implies invalidate PLUS recover space.)

invalidate is close enough. Consider block/blk-flush.c, sigh.

Quoted 18 lines
Quoted 15 lines

mm/frontswap.c line 48

Quoted 9 lines
mm/frontswap.c +8 -0
+/*
+ * Useful stats available in /sys/kernel/mm/frontswap.  These are for
+ * information only so are not protected against increment/decrement races.
+ */
+static unsigned long frontswap_gets;
+static unsigned long frontswap_succ_puts;
+static unsigned long frontswap_failed_puts;
+static unsigned long frontswap_flushes;

If they're in /sys/kernel/mm then they rather become permanent parts of the exported kernel interface. We're stuck with them. Plus they're inaccurate and updating them might be inefficient, so we don't want to be stuck with them.

Quoted 2 lines

I suggest moving these to debugfs from where we can remove them if we feel like doing so.

Quoted 7 lines

The style (and code) for this was mimicked from ksm and hugepages, which expose the stats the same way... as does cleancache now. slub is also similar. I'm OK with using a different approach (e.g. debugfs), but think it would be inconsistent and confusing to expose these stats differently than cleancache (or ksm and hugepages). I'd support and help with a massive cleanup commit across all of mm later though. Hope that's OK for now.

These are boring internal counters for a few developers. They're so uninteresting to end users that the developer didn't even bother to document them ;)

They should be in debugfs. Probably some/all of the existing cleancache/ksm/hugepage stats should be in debugfs too. This a mistake we often make. Please let's be extremely miserly with the kernel API.

--
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>