Skip to contentSkip to conversation

Memory notification pseudo-device module

PATCH In review Leonid Moiseichuk 4 participants 14 messages Jan 17, 2012 – Jan 18, 2012

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

v2 cover Memory notification pseudo-device module

Hello,

That is a continuation of Used Memory Meter (UMM) started as [1] and re-designed according to inputs I can implement.

The main idea of memnotify is to provide low-cost interface for user-space to update with specified granularity and timeout required memory usulization values in specified moment of time. It is not a low memory interface as [2] nor OOM killer but it could be used for situations related to "close-to-OOM" handling. The examples of usage could be discovered in libmemnotify [3] or test case code below this intro.

During the previous discussion two biggest disappointments were discovered:
1. hooking MM -- now it is removed, the new solution has 0 MM changes, only extra shrinker added to re-enforce timer to re-check memory situation as soon as possible
2. extendability -- to add any other tracked value you need to modify 2 places in code (memtypes[] and get_memory_status() function).

As Kosaki-san said for activity tracking I added "active" page set. You can subscribe for one or several values for tracking simultaneously.

To periodic re-read vm_stat (using global_page_state() interface) I use deferred timer, expecting if cpu sleeps timer could be delayed and use-time will be not affected. If this assumption is not correct I can add register_cpu_notifier() call. Otherwise, when no clients connected - no activity or subscription in module performed.

The number of module parameters could be specified to adjust reaction time or granularity of changes. The interface pseudo-device hardcoded as /dev/memnotify.

As in previous time module tested using arm, x86-32 and x86-64 for typical (10 clients) and stress (10K clients) cases.

With Best Regards,
Leonid

References:
1. https://lkml.org/lkml/2012/1/4/208
2. https://lkml.org/lkml/2012/1/17/34
3. http://maemo.gitorious.org/maemo-tools/libmemnotify

Leonid Moiseichuk (2):
Making si_swapinfo exportable
Memory notification pseudo-device module

 drivers/misc/Kconfig     |   11 +
 drivers/misc/Makefile    |    1 +
 drivers/misc/memnotify.c |  582 ++++++++++++++++++++++++++++++++++++++++++++++
 mm/swapfile.c            |    3 +
 4 files changed, 597 insertions(+), 0 deletions(-)
 create mode 100644 drivers/misc/memnotify.c
Signature · 99 lines
1.7.7.3

/*
 * mn_test.c - test for system-wide memory notification/meter implementation
 * 
 * Usage:
 *    $gcc -o mn_test mn_test.c
 *    $mn_test
 *  or with pointing pages as threshold(s)
 *    $mn_test "used 5000"
 *  or
 *    $mn_test "active 8000 used 16000"
 *
 * Copyright (C) 2012 Nokia Corporation.
 *      Leonid Moiseichuk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 * kind, whether express or implied; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(const int argc, const char* argv[])
{
	const char *dev = "/dev/memnotify";
	const int fd = open(dev,O_RDWR);
	const char *lim = (2 == argc && argv[1] ? argv[1] : NULL);
	ssize_t ret;
	struct pollfd fds[1];
	char tmp[256];

	printf ("%s -- test for system-wide memory notification/meter %s\n", argv[0], dev);
	if (lim && 0 == strcmp(lim, "--help")) {
		printf ("usage: %s [threshold(s)_in_pages]\n", argv[0]);
		printf ("example of threshold(s) available in %s\n", dev);
		return -1;
	}

	if (fd < 0) {
		printf ("cannot open device %s, do you have memnotify.ko loaded?\n", dev);
		return -1;
	} else {
		printf ("device %s opened successfuly with fd = %d\n", dev, fd);
	}

	if ((ret = read(fd, tmp, sizeof(tmp))) < 0) {
		printf ("reading from %s failed\n", dev);
		return -1;
	}
	tmp[ret - 1] = 0;
	printf ("read from %s %d bytes: '%s'\n", dev, ret, tmp);

	/* do we have a threshold? */
	if ( !lim )
		return 0;

	printf ("establishing threshold '%s' and waiting\n", lim);
	if ((ret = write(fd, argv[1], strlen(argv[1]))) < 0) {
		printf ("cannot set threshold '%s' for device %s\n", lim, dev);
		return -1;
	}
	
	printf ("polling for threshold '%s' to be changed to up/down\n", lim);
	fds->fd      = fd;
	fds->events  = POLLIN;
	fds->revents = 0;
	ret = poll(fds, 1, -1);
	printf ("poll(%d) returned %d\n", fd, ret);

	if ((ret = read(fd, tmp, sizeof(tmp))) < 0) {
		printf ("reading from %s failed\n", dev);
		return -1;
	}
	tmp[ret - 1] = 0;
	printf ("--> memory figures reached '%s'\n", tmp);

	return 0;
}
/* ---< end of mn_test.c >---- */

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

PATCH v2 1/2 Making si_swapinfo exportable

If we will make si_swapinfo() exportable it could be called from modules. Otherwise modules have no interface to obtain information about swap usage. Change made in the same way as si_meminfo() declared.

---
 mm/swapfile.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
mm/swapfile.c +3 -0
index b1cd120..192cc25 100644
@@ -5,10 +5,12 @@
  *  Swap reorganised 29.12.95, Stephen Tweedie
  */
 
+#include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/hugetlb.h>
 #include <linux/mman.h>
 #include <linux/slab.h>
+#include <linux/kernel.h>
 #include <linux/kernel_stat.h>
 #include <linux/swap.h>
 #include <linux/vmalloc.h>
@@ -2177,6 +2179,7 @@ void si_swapinfo(struct sysinfo *val)
 	val->totalswap = total_swap_pages + nr_to_be_unused;
 	spin_unlock(&swap_lock);
 }
+EXPORT_SYMBOL(si_swapinfo);
 
 /*
  * Verify that a swap entry is valid and increment its swap map count.
Signature · 8 lines
1.7.7.3

--
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>
Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] Making si_swapinfo exportable

Quoted 27 lines · Leonid Moiseichuk

If we will make si_swapinfo() exportable it could be called from modules. Otherwise modules have no interface to obtain information about swap usage. Change made in the same way as si_meminfo() declared.

---
 mm/swapfile.c |    3 +++

 1 files changed, 3 insertions(+), 0 deletions(-)
mm/swapfile.c +2 -0
index b1cd120..192cc25 100644
@@ -5,10 +5,12 @@
  *  Swap reorganised 29.12.95, Stephen Tweedie
  */
 
+#include <linux/export.h>
  #include <linux/mm.h>
  #include <linux/hugetlb.h>
  #include <linux/mman.h>
  #include <linux/slab.h>
+#include <linux/kernel.h>
  #include <linux/kernel_stat.h>
  #include <linux/swap.h>
  #include <linux/vmalloc.h>

mm/swapfile.c void si_swapinfo(struct sysinfo *val) · line 2182

Quoted 4 lines
mm/swapfile.c +1 -0
@@ -2177,6 +2179,7 @@ void si_swapinfo(struct sysinfo *val)
        val->totalswap = total_swap_pages + nr_to_be_unused;
        spin_unlock(&swap_lock);
  }
+EXPORT_SYMBOL(si_swapinfo);

FWIW, I'm completely OK with this export:

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

Re: [PATCH v2 1/2] Making si_swapinfo exportable

Quoted 19 lines · Pekka Enberg
Quoted 17 lines · Leonid Moiseichuk

If we will make si_swapinfo() exportable it could be called from modules. Otherwise modules have no interface to obtain information about swap usage. Change made in the same way as si_meminfo() declared.

---
 mm/swapfile.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
mm/swapfile.c +0 -0
index b1cd120..192cc25 100644
@@ -5,10 +5,12 @@
 *  Swap reorganised 29.12.95, Stephen Tweedie
 */
 
Quoted 14 lines
Quoted 10 lines
mm/swapfile.c +2 -0
+#include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/hugetlb.h>
 #include <linux/mman.h>
 #include <linux/slab.h>
+#include <linux/kernel.h>
 #include <linux/kernel_stat.h>
 #include <linux/swap.h>
 #include <linux/vmalloc.h>

mm/swapfile.c void si_swapinfo(struct sysinfo *val) · line 2182

Quoted 4 lines
mm/swapfile.c +1 -0
@@ -2177,6 +2179,7 @@ void si_swapinfo(struct sysinfo *val)
       val->totalswap = total_swap_pages + nr_to_be_unused;
       spin_unlock(&swap_lock);
 }
+EXPORT_SYMBOL(si_swapinfo);

EXPORT_SYMBOL_GPL() perhaps?

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

RE: [PATCH v2 1/2] Making si_swapinfo exportable

Quoted 3 lines

-----Original Message-----
From: ext Greg KH [mailto:[email protected]]
Sent: 18 January, 2012 16:09

...

Quoted 3 lines
Quoted 1 line
Quoted 1 line

+EXPORT_SYMBOL(si_swapinfo);

EXPORT_SYMBOL_GPL() perhaps?

I followed si_meminfo which is uses EXPORT_SYMBOL.

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

#5 lore raw

Re: [PATCH v2 1/2] Making si_swapinfo exportable

Quoted 10 lines · On Wed, Jan 18, 2012 at 02:47:47PM +0000, [email protected] wrote:
Quoted 3 lines

-----Original Message-----
From: ext Greg KH [mailto:[email protected]]
Sent: 18 January, 2012 16:09

...

Quoted 3 lines
Quoted 1 line
Quoted 1 line

+EXPORT_SYMBOL(si_swapinfo);

EXPORT_SYMBOL_GPL() perhaps?

I followed si_meminfo which is uses EXPORT_SYMBOL.

Ah, good point.

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

#6 lore raw

Re: [PATCH v2 1/2] Making si_swapinfo exportable

Quoted 6 lines · Greg KH
Quoted 5 lines · On Wed, Jan 18, 2012 at 02:47:47PM +0000, [email protected] wrote:
Quoted 3 lines

-----Original Message-----
From: ext Greg KH [mailto:[email protected]]
Sent: 18 January, 2012 16:09

...

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

+EXPORT_SYMBOL(si_swapinfo);

EXPORT_SYMBOL_GPL() perhaps?

I followed si_meminfo which is uses EXPORT_SYMBOL.

Ah, good point.

Yup, I think EXPORT_SYMBOL is appropriate here.

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

#7 lore raw

Re: [PATCH v2 1/2] Making si_swapinfo exportable

In reply to Greg KH
Quoted 3 lines
Quoted 1 line
Quoted 1 line

+EXPORT_SYMBOL(si_swapinfo);

EXPORT_SYMBOL_GPL() perhaps?

Greg,

So.. could you tell when are suppose to do _GPL and when not? Is there a policy of "new code must be _GPL" ? Or is there some extra "if .. then" conditions?

Thanks!

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

#8 lore raw

Re: [PATCH v2 1/2] Making si_swapinfo exportable

Quoted 9 lines · Konrad Rzeszutek Wilk
Quoted 3 lines
Quoted 1 line
Quoted 1 line

+EXPORT_SYMBOL(si_swapinfo);

EXPORT_SYMBOL_GPL() perhaps?

Greg,

So.. could you tell when are suppose to do _GPL and when not? Is there a policy of "new code must be _GPL" ? Or is there some extra "if .. then" conditions?

It's up to the author of the code, what their preference is.

I just generally prefer _GPL for new exports.

thanks,

greg k-h

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

#9 lore raw

PATCH v2 2/2 Memory notification pseudo-device module

In reply to Leonid Moiseichuk

The memory notification (memnotify) device tracks level of memory utilization, active page set and notifies subscribed processes when consumption crossed specified threshold(s) up or down. It could be used on embedded devices to implementation of performance-cheap memory reacting by using
e.g. libmemnotify or similar user-space component.

The minimal (250 ms) and maximal (15s) periods of reaction and granularity (~1.4% of memory size) could be tuned using module options.

---
 drivers/misc/Kconfig     |   11 +
 drivers/misc/Makefile    |    1 +
 drivers/misc/memnotify.c |  582 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 594 insertions(+), 0 deletions(-)
 create mode 100644 drivers/misc/memnotify.c
drivers/misc/Kconfig +11 -0
index 5664696..eefda14 100644
@@ -500,6 +500,17 @@ config USB_SWITCH_FSA9480
 	  stereo and mono audio, video, microphone and UART data to use
 	  a common connector port.
 
+config MEMNOTIFY
+	tristate "Enables memory notification pseudo-device"
+	default n
+	help
+	  This option enables pseudo-device /dev/memnotify for tracking
+	  system memory utilization and updating state to subscribed clients
+	  when specified threshold reached.
+
+	  Say Y here if you want to support memory monitoring.
+	  If unsure, say N.
+
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
 source "drivers/misc/cb710/Kconfig"
drivers/misc/Makefile +1 -0
index b26495a..86f6199 100644
@@ -48,3 +48,4 @@ obj-y				+= lis3lv02d/
 obj-y				+= carma/
 obj-$(CONFIG_USB_SWITCH_FSA9480) += fsa9480.o
 obj-$(CONFIG_ALTERA_STAPL)	+=altera-stapl/
+obj-$(CONFIG_MEMNOTIFY)		+= memnotify.o
drivers/misc/memnotify.c new file +582 -0

582 lines not on this page · raw message

Signature · 8 lines
1.7.7.3

--
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>
#10 lore raw
Quoted 10 lines · Leonid Moiseichuk

The memory notification (memnotify) device tracks level of memory utilization, active page set and notifies subscribed processes when consumption crossed specified threshold(s) up or down. It could be used on embedded devices to implementation of performance-cheap memory reacting by using
e.g. libmemnotify or similar user-space component.

The minimal (250 ms) and maximal (15s) periods of reaction and granularity (~1.4% of memory size) could be tuned using module options.

Is the point of making this a misc device to keep the ABI compatible with N9? Is the ABI documented?

Pekka

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

#11 lore raw

Not only, here is many reasons:
1. publish code for review by people who knows how to do things and who can advise something valuable
2. to have one more update source in addition to memcg I used in n9 for libmemnotify and which works not I like to see (ideally drop memcg)
3. maybe someone needs similar solution, keep it internally = kill it. Now module looks pretty simple for me and maintainable. Plus one small issue fixed for swapinfo()

So at least now it could be used for tracking activity and it is a good improvement. It also could be extended to report "memory pressure value" similar to Minchan's patch does if necessary.

With Best Wishes,
Leonid

Quoted 25 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of ext Pekka Enberg
Sent: 17 January, 2012 15:33
To: Moiseichuk Leonid (Nokia-MP/Helsinki)
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Jaaskelainen Vesa (Nokia-MP/Helsinki)
Subject: Re: [PATCH v2 2/2] Memory notification pseudo-device module

Quoted 4 lines · Leonid Moiseichuk

The memory notification (memnotify) device tracks level of memory utilization, active page set and notifies subscribed processes when consumption crossed specified threshold(s) up or down. It could be used on embedded devices to implementation of performance-cheap

memory

Quoted 5 lines

reacting by using e.g. libmemnotify or similar user-space component.

The minimal (250 ms) and maximal (15s) periods of reaction and granularity (~1.4% of memory size) could be tuned using module options.

Quoted 6 lines
Quoted 1 line

Is the point of making this a misc device to keep the ABI compatible with N9? Is the ABI documented?

Pekka

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

#12 lore raw
Quoted 3 lines · On Tue, Jan 17, 2012 at 3:45 PM, <[email protected]> wrote:

3. maybe someone needs similar solution, keep it internally = kill it. Now module looks pretty simple for me and maintainable. Plus one small issue fixed for swapinfo()

If you're serious about making this a generic thing, it must live in mm/mem_notify.c. No ifs or buts about it.

I'm also not completely convinced we need to put memnotify policy in the kernel. Why can't we extend Minchan's patch to report the relevant numbers and let the userspace figure out when pressure is above some interesting threshold?

Pekka

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

#13 lore raw
Quoted 4 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of ext Pekka Enberg
Sent: 17 January, 2012 15:59

...

Quoted 6 lines

If you're serious about making this a generic thing, it must live in mm/mem_notify.c. No ifs or buts about it.
I'm also not completely convinced we need to put memnotify policy in the kernel. Why can't we extend Minchan's patch to report the relevant numbers and let the userspace figure out when pressure is above some interesting threshold?

I do not insist to have it as a part mm, but if you have 1-2-3 items what should be done in this or Minchan's patch I can participate.

Quoted 1 line

From my point of view Minchan's patch is not ideal due to required:

1. depends on cgroups (at least as I see it from patch in shrink_mem_cgroup_zone()part)
2. reports only memory pressure based on relation in between free and file pages which is means by active file IO you may get lowmem
3. swapping should not produce lowmem, but active swapping - must, is it checked there?
+ if (nr[LRU_INACTIVE_ANON])
+ low_mem = true;
4. required changes in vmscan

I think, due to everyone based on his experience/working area profile has own understanding what is "low memory" (or similar situation which needs to be tracked) it should be some generic or extendable API, not like ON/OFF trigger for something happened inside VM. From another point of view it should not be too generic due to tasks could be solved using memcg, ionice, OOM killer or variations of soft-OOM-patches.

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