Skip to contentSkip to conversation

out of memory notifier.

PATCH New Martin Schwidefsky 1 participant 1 message May 3, 2006

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

PATCH 1/1 out of memory notifier.

[patch] out of memory notifier.

Add a notifer chain to the out of memory killer. If one of the registered callbacks could release some memory, do not kill the process but return and retry the allocation that forced the oom killer to run.

The purpose of the notifier is to add a safety net in the presence of memory ballooners. If the resource manager inflated the balloon to a size where memory allocations can not be satisfied anymore, it is better to deflate the balloon a bit instead of killing processes.

The implementation for the s390 ballooner is included.

---
 arch/s390/mm/cmm.c   |   51 +++++++++++++++++++++++++++++++++++++++++++--------
 include/linux/swap.h |    4 ++++
 mm/oom_kill.c        |   21 +++++++++++++++++++++
 3 files changed, 68 insertions(+), 8 deletions(-)
arch/s390/mm/cmm.c +43 -8
diff -urpN linux-2.6/arch/s390/mm/cmm.c linux-2.6-oom/arch/s390/mm/cmm.c
@@ -16,6 +16,7 @@
 #include <linux/sched.h>
 #include <linux/sysctl.h>
 #include <linux/ctype.h>
+#include <linux/swap.h>
 
 #include <asm/pgalloc.h>
 #include <asm/uaccess.h>
@@ -44,6 +45,7 @@ static long cmm_timeout_seconds = 0;
 
 static struct cmm_page_array *cmm_page_list = NULL;
 static struct cmm_page_array *cmm_timed_page_list = NULL;
+static DEFINE_SPINLOCK(cmm_lock);
 
 static unsigned long cmm_thread_active = 0;
 static struct work_struct cmm_thread_starter;
@@ -72,40 +74,50 @@ cmm_strtoul(const char *cp, char **endp)
 static long
 cmm_alloc_pages(long pages, long *counter, struct cmm_page_array **list)
 {
-	struct cmm_page_array *pa;
+	struct cmm_page_array *pa, *npa;
 	unsigned long page;
 
-	pa = *list;
 	while (pages) {
 		page = __get_free_page(GFP_NOIO);
 		if (!page)
 			break;
+		spin_lock(&cmm_lock);
+		pa = *list;
 		if (!pa || pa->index >= CMM_NR_PAGES) {
 			/* Need a new page for the page list. */
-			pa = (struct cmm_page_array *)
+			spin_unlock(&cmm_lock);
+			npa = (struct cmm_page_array *)
 				__get_free_page(GFP_NOIO);
-			if (!pa) {
+			if (!npa) {
 				free_page(page);
 				break;
 			}
-			pa->next = *list;
-			pa->index = 0;
-			*list = pa;
+			spin_lock(&cmm_lock);
+			pa = *list;
+			if (!pa || pa->index >= CMM_NR_PAGES) {
+				npa->next = pa;
+				npa->index = 0;
+				pa = npa;
+				*list = pa;
+			} else
+				free_page((unsigned long) npa);
 		}
 		diag10(page);
 		pa->pages[pa->index++] = page;
 		(*counter)++;
+		spin_unlock(&cmm_lock);
 		pages--;
 	}
 	return pages;
 }
 
-static void
+static long
 cmm_free_pages(long pages, long *counter, struct cmm_page_array **list)
 {
 	struct cmm_page_array *pa;
 	unsigned long page;
 
+	spin_lock(&cmm_lock);
 	pa = *list;
 	while (pages) {
 		if (!pa || pa->index <= 0)
@@ -120,8 +132,29 @@ cmm_free_pages(long pages, long *counter
 		(*counter)--;
 		pages--;
 	}
+	spin_unlock(&cmm_lock);
+	return pages;
 }
 
+static int cmm_oom_notify(struct notifier_block *self,
+			  unsigned long dummy, void *parm)
+{
+	unsigned long *freed = parm;
+	long pages = 256;
+
+	pages = cmm_free_pages(pages, &cmm_timed_pages, &cmm_timed_page_list);
+	if (pages > 0)
+		pages = cmm_free_pages(pages, &cmm_pages, &cmm_page_list);
+	cmm_pages_target = cmm_pages;
+	cmm_timed_pages_target = cmm_timed_pages;
+	*freed += 256 - pages;
+	return NOTIFY_OK;
+}
+
+static struct notifier_block cmm_oom_nb = {
+	.notifier_call = cmm_oom_notify
+};
+
 static int
 cmm_thread(void *dummy)
 {
@@ -419,6 +452,7 @@ cmm_init (void)
 #ifdef CONFIG_CMM_IUCV
 	smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
 #endif
+	register_oom_notifier(&cmm_oom_nb);
 	INIT_WORK(&cmm_thread_starter, (void *) cmm_start_thread, NULL);
 	init_waitqueue_head(&cmm_thread_wait);
 	init_timer(&cmm_timer);
@@ -428,6 +462,7 @@ cmm_init (void)
 static void
 cmm_exit(void)
 {
+	unregister_oom_notifier(&cmm_oom_nb);
 	cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
 	cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
 #ifdef CONFIG_CMM_PROC
include/linux/swap.h +4 -0
diff -urpN linux-2.6/include/linux/swap.h linux-2.6-oom/include/linux/swap.h
@@ -7,6 +7,7 @@
 #include <linux/mmzone.h>
 #include <linux/list.h>
 #include <linux/sched.h>
+#include <linux/notifier.h>
 
 #include <asm/atomic.h>
 #include <asm/page.h>
@@ -157,6 +158,9 @@ struct swap_list_t {
 
 /* linux/mm/oom_kill.c */
 extern void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order);
+extern int register_oom_notifier(struct notifier_block *nb);
+extern int unregister_oom_notifier(struct notifier_block *nb);
+
 
 /* linux/mm/memory.c */
 extern void swapin_readahead(swp_entry_t, unsigned long, struct vm_area_struct *);
mm/oom_kill.c +21 -0
diff -urpN linux-2.6/mm/oom_kill.c linux-2.6-oom/mm/oom_kill.c
@@ -21,6 +21,7 @@
 #include <linux/timex.h>
 #include <linux/jiffies.h>
 #include <linux/cpuset.h>
+#include <linux/module.h>
 
 int sysctl_panic_on_oom;
 /* #define DEBUG */
@@ -306,6 +307,20 @@ static int oom_kill_process(struct task_
 	return oom_kill_task(p, message);
 }
 
+static struct notifier_block *oom_notify_list = 0;
+
+int register_oom_notifier(struct notifier_block *nb)
+{
+	return notifier_chain_register(&oom_notify_list, nb);
+}
+EXPORT_SYMBOL_GPL(register_oom_notifier);
+
+int unregister_oom_notifier(struct notifier_block *nb)
+{
+	return notifier_chain_unregister(&oom_notify_list, nb);
+}
+EXPORT_SYMBOL_GPL(unregister_oom_notifier);
+
 /**
  * out_of_memory - kill the "best" process when we run out of memory
  *
@@ -318,6 +333,12 @@ void out_of_memory(struct zonelist *zone
 {
 	task_t *p;
 	unsigned long points = 0;
+	unsigned long freed = 0;
+
+	notifier_call_chain(&oom_notify_list, 0, &freed);
+	if (freed > 0)
+		/* Got some memory back in the last second. */
+		return;
 
 	if (printk_ratelimit()) {
 		printk("oom-killer: gfp_mask=0x%x, order=%d\n",

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>