linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
@ 2017-02-09 13:21 peter enderborg
  2017-02-09 13:50 ` Greg Kroah-Hartman
  2017-02-09 19:26 ` Michal Hocko
  0 siblings, 2 replies; 20+ messages in thread
From: peter enderborg @ 2017-02-09 13:21 UTC (permalink / raw)
  To: devel, Greg Kroah-Hartman, linux-kernel,
	Arve Hjønnevåg, Riley Andrews, Linus Torvalds,
	linux-mm

This collects stats for shrinker calls and how much
waste work we do within the lowmemorykiller.

Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>
---
  drivers/staging/android/Kconfig                 | 11 ++++
  drivers/staging/android/Makefile                |  1 +
  drivers/staging/android/lowmemorykiller.c       |  9 ++-
  drivers/staging/android/lowmemorykiller_stats.c | 85 +++++++++++++++++++++++++
  drivers/staging/android/lowmemorykiller_stats.h | 29 +++++++++
  5 files changed, 134 insertions(+), 1 deletion(-)
  create mode 100644 drivers/staging/android/lowmemorykiller_stats.c
  create mode 100644 drivers/staging/android/lowmemorykiller_stats.h

diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
index 6c00d6f..96e86c7 100644
--- a/drivers/staging/android/Kconfig
+++ b/drivers/staging/android/Kconfig
@@ -24,6 +24,17 @@ config ANDROID_LOW_MEMORY_KILLER
        scripts (/init.rc), and it defines priority values with minimum free memory size
        for each priority.

+config ANDROID_LOW_MEMORY_KILLER_STATS
+    bool "Android Low Memory Killer: collect statistics"
+    depends on ANDROID_LOW_MEMORY_KILLER
+    default n
+    help
+      Create a file in /proc/lmkstats that includes
+      collected statistics about kills, scans and counts
+      and  interaction with the shrinker. Its content
+      will be different depeding on lmk implementation used.
+
+
  source "drivers/staging/android/ion/Kconfig"

  endif # if ANDROID
diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
index 7ed1be7..d710eb2 100644
--- a/drivers/staging/android/Makefile
+++ b/drivers/staging/android/Makefile
@@ -4,3 +4,4 @@ obj-y                    += ion/

  obj-$(CONFIG_ASHMEM)            += ashmem.o
  obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER)    += lowmemorykiller.o
+obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS)    += lowmemorykiller_stats.o
diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
index ec3b665..15c1b38 100644
--- a/drivers/staging/android/lowmemorykiller.c
+++ b/drivers/staging/android/lowmemorykiller.c
@@ -42,6 +42,7 @@
  #include <linux/rcupdate.h>
  #include <linux/profile.h>
  #include <linux/notifier.h>
+#include "lowmemorykiller_stats.h"

  static u32 lowmem_debug_level = 1;
  static short lowmem_adj[6] = {
@@ -72,6 +73,7 @@ static unsigned long lowmem_deathpending_timeout;
  static unsigned long lowmem_count(struct shrinker *s,
                    struct shrink_control *sc)
  {
+    lmk_inc_stats(LMK_COUNT);
      return global_node_page_state(NR_ACTIVE_ANON) +
          global_node_page_state(NR_ACTIVE_FILE) +
          global_node_page_state(NR_INACTIVE_ANON) +
@@ -95,6 +97,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
                  global_node_page_state(NR_SHMEM) -
                  total_swapcache_pages();

+    lmk_inc_stats(LMK_SCAN);
      if (lowmem_adj_size < array_size)
          array_size = lowmem_adj_size;
      if (lowmem_minfree_size < array_size)
@@ -134,6 +137,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
          if (task_lmk_waiting(p) &&
              time_before_eq(jiffies, lowmem_deathpending_timeout)) {
              task_unlock(p);
+            lmk_inc_stats(LMK_TIMEOUT);
              rcu_read_unlock();
              return 0;
          }
@@ -179,7 +183,9 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
                   other_free * (long)(PAGE_SIZE / 1024));
          lowmem_deathpending_timeout = jiffies + HZ;
          rem += selected_tasksize;
-    }
+        lmk_inc_stats(LMK_KILL);
+    } else
+        lmk_inc_stats(LMK_WASTE);

      lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
               sc->nr_to_scan, sc->gfp_mask, rem);
@@ -196,6 +202,7 @@ static struct shrinker lowmem_shrinker = {
  static int __init lowmem_init(void)
  {
      register_shrinker(&lowmem_shrinker);
+    init_procfs_lmk();
      return 0;
  }
  device_initcall(lowmem_init);
diff --git a/drivers/staging/android/lowmemorykiller_stats.c b/drivers/staging/android/lowmemorykiller_stats.c
new file mode 100644
index 0000000..673691c
--- /dev/null
+++ b/drivers/staging/android/lowmemorykiller_stats.c
@@ -0,0 +1,85 @@
+/*
+ *  lowmemorykiller_stats
+ *
+ *  Copyright (C) 2017 Sony Mobile Communications Inc.
+ *
+ *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
+ *
+ *  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 code is bookkeeping of statistical information
+ * from lowmemorykiller and provide a node in proc "/proc/lmkstats".
+ */
+
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+#include "lowmemorykiller_stats.h"
+
+struct lmk_stats {
+    atomic_long_t scans; /* counter as in shrinker scans */
+    atomic_long_t kills; /* the number of sigkills sent */
+    atomic_long_t waste; /* the numer of extensive calls that did
+                  * not lead to anything
+                  */
+    atomic_long_t timeout; /* counter for shrinker calls that needed
+                * to be cancelled due to pending kills
+                */
+    atomic_long_t count; /* number of shrinker count calls */
+    atomic_long_t unknown; /* internal */
+} st;
+
+void lmk_inc_stats(int key)
+{
+    switch (key) {
+    case LMK_SCAN:
+        atomic_long_inc(&st.scans);
+        break;
+    case LMK_KILL:
+        atomic_long_inc(&st.kills);
+        break;
+    case LMK_WASTE:
+        atomic_long_inc(&st.waste);
+        break;
+    case LMK_TIMEOUT:
+        atomic_long_inc(&st.timeout);
+        break;
+    case LMK_COUNT:
+        atomic_long_inc(&st.count);
+        break;
+    default:
+        atomic_long_inc(&st.unknown);
+        break;
+    }
+}
+
+static int lmk_proc_show(struct seq_file *m, void *v)
+{
+    seq_printf(m, "kill: %ld\n", atomic_long_read(&st.kills));
+    seq_printf(m, "scan: %ld\n", atomic_long_read(&st.scans));
+    seq_printf(m, "waste: %ld\n", atomic_long_read(&st.waste));
+    seq_printf(m, "timeout: %ld\n", atomic_long_read(&st.timeout));
+    seq_printf(m, "count: %ld\n", atomic_long_read(&st.count));
+    seq_printf(m, "unknown: %ld (internal)\n",
+           atomic_long_read(&st.unknown));
+
+    return 0;
+}
+
+static int lmk_proc_open(struct inode *inode, struct file *file)
+{
+    return single_open(file, lmk_proc_show, PDE_DATA(inode));
+}
+
+static const struct file_operations lmk_proc_fops = {
+    .open        = lmk_proc_open,
+    .read        = seq_read,
+    .release    = single_release
+};
+
+int __init init_procfs_lmk(void)
+{
+    proc_create_data(LMK_PROCFS_NAME, 0444, NULL, &lmk_proc_fops, NULL);
+    return 0;
+}
diff --git a/drivers/staging/android/lowmemorykiller_stats.h b/drivers/staging/android/lowmemorykiller_stats.h
new file mode 100644
index 0000000..abeb6924
--- /dev/null
+++ b/drivers/staging/android/lowmemorykiller_stats.h
@@ -0,0 +1,29 @@
+/*
+ *  lowmemorykiller_stats interface
+ *
+ *  Copyright (C) 2017 Sony Mobile Communications Inc.
+ *
+ *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
+ *
+ *  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.
+ */
+
+enum  lmk_kill_stats {
+    LMK_SCAN = 1,
+    LMK_KILL = 2,
+    LMK_WASTE = 3,
+    LMK_TIMEOUT = 4,
+    LMK_COUNT = 5
+};
+
+#define LMK_PROCFS_NAME "lmkstats"
+
+#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS
+void lmk_inc_stats(int key);
+int __init init_procfs_lmk(void);
+#else
+static inline void lmk_inc_stats(int key) { return; };
+static inline int __init init_procfs_lmk(void) { return 0; };
+#endif
-- 
2.4.2

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-09 13:21 [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller peter enderborg
@ 2017-02-09 13:50 ` Greg Kroah-Hartman
  2017-02-09 19:26 ` Michal Hocko
  1 sibling, 0 replies; 20+ messages in thread
From: Greg Kroah-Hartman @ 2017-02-09 13:50 UTC (permalink / raw)
  To: peter enderborg
  Cc: devel, linux-kernel, Arve Hjønnevåg, Riley Andrews,
	Linus Torvalds, linux-mm

On Thu, Feb 09, 2017 at 02:21:45PM +0100, peter enderborg wrote:
> This collects stats for shrinker calls and how much
> waste work we do within the lowmemorykiller.
> 
> Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>
> ---
>  drivers/staging/android/Kconfig                 | 11 ++++
>  drivers/staging/android/Makefile                |  1 +
>  drivers/staging/android/lowmemorykiller.c       |  9 ++-
>  drivers/staging/android/lowmemorykiller_stats.c | 85 +++++++++++++++++++++++++
>  drivers/staging/android/lowmemorykiller_stats.h | 29 +++++++++
>  5 files changed, 134 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.c
>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.h
> 
> diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
> index 6c00d6f..96e86c7 100644
> --- a/drivers/staging/android/Kconfig
> +++ b/drivers/staging/android/Kconfig
> @@ -24,6 +24,17 @@ config ANDROID_LOW_MEMORY_KILLER
>        scripts (/init.rc), and it defines priority values with minimum free memory size
>        for each priority.
> 
> +config ANDROID_LOW_MEMORY_KILLER_STATS
> +    bool "Android Low Memory Killer: collect statistics"
> +    depends on ANDROID_LOW_MEMORY_KILLER
> +    default n
> +    help
> +      Create a file in /proc/lmkstats that includes
> +      collected statistics about kills, scans and counts
> +      and  interaction with the shrinker. Its content
> +      will be different depeding on lmk implementation used.
> +
> +
>  source "drivers/staging/android/ion/Kconfig"
> 
>  endif # if ANDROID
> diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
> index 7ed1be7..d710eb2 100644
> --- a/drivers/staging/android/Makefile
> +++ b/drivers/staging/android/Makefile
> @@ -4,3 +4,4 @@ obj-y                    += ion/
> 
>  obj-$(CONFIG_ASHMEM)            += ashmem.o
>  obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER)    += lowmemorykiller.o
> +obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS)    += lowmemorykiller_stats.o
> diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
> index ec3b665..15c1b38 100644
> --- a/drivers/staging/android/lowmemorykiller.c
> +++ b/drivers/staging/android/lowmemorykiller.c
> @@ -42,6 +42,7 @@
>  #include <linux/rcupdate.h>
>  #include <linux/profile.h>
>  #include <linux/notifier.h>
> +#include "lowmemorykiller_stats.h"
> 
>  static u32 lowmem_debug_level = 1;
>  static short lowmem_adj[6] = {
> @@ -72,6 +73,7 @@ static unsigned long lowmem_deathpending_timeout;
>  static unsigned long lowmem_count(struct shrinker *s,
>                    struct shrink_control *sc)
>  {
> +    lmk_inc_stats(LMK_COUNT);
>      return global_node_page_state(NR_ACTIVE_ANON) +
>          global_node_page_state(NR_ACTIVE_FILE) +
>          global_node_page_state(NR_INACTIVE_ANON) +

Your patch is corrupted and can not be applied :(

all of them are like this.

greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-09 13:21 [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller peter enderborg
  2017-02-09 13:50 ` Greg Kroah-Hartman
@ 2017-02-09 19:26 ` Michal Hocko
  2017-02-09 20:07   ` Greg Kroah-Hartman
  1 sibling, 1 reply; 20+ messages in thread
From: Michal Hocko @ 2017-02-09 19:26 UTC (permalink / raw)
  To: peter enderborg
  Cc: devel, Greg Kroah-Hartman, linux-kernel,
	Arve Hjønnevåg, Riley Andrews, Linus Torvalds,
	linux-mm

On Thu 09-02-17 14:21:45, peter enderborg wrote:
> This collects stats for shrinker calls and how much
> waste work we do within the lowmemorykiller.

This doesn't explain why do we need this information and who is going to
use it. Not to mention it exports it in /proc which is considered a
stable user API. This is a no-go, especially for something that is still
lingering in the staging tree without any actuall effort to make it
fully supported MM feature. I am actually strongly inclined to simply
drop lmk from the tree completely.

> Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>

Nacked-by: Michal Hocko <mhocko@suse.com>

> ---
>  drivers/staging/android/Kconfig                 | 11 ++++
>  drivers/staging/android/Makefile                |  1 +
>  drivers/staging/android/lowmemorykiller.c       |  9 ++-
>  drivers/staging/android/lowmemorykiller_stats.c | 85 +++++++++++++++++++++++++
>  drivers/staging/android/lowmemorykiller_stats.h | 29 +++++++++
>  5 files changed, 134 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.c
>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.h
> 
> diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
> index 6c00d6f..96e86c7 100644
> --- a/drivers/staging/android/Kconfig
> +++ b/drivers/staging/android/Kconfig
> @@ -24,6 +24,17 @@ config ANDROID_LOW_MEMORY_KILLER
>        scripts (/init.rc), and it defines priority values with minimum free memory size
>        for each priority.
> 
> +config ANDROID_LOW_MEMORY_KILLER_STATS
> +    bool "Android Low Memory Killer: collect statistics"
> +    depends on ANDROID_LOW_MEMORY_KILLER
> +    default n
> +    help
> +      Create a file in /proc/lmkstats that includes
> +      collected statistics about kills, scans and counts
> +      and  interaction with the shrinker. Its content
> +      will be different depeding on lmk implementation used.
> +
> +
>  source "drivers/staging/android/ion/Kconfig"
> 
>  endif # if ANDROID
> diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
> index 7ed1be7..d710eb2 100644
> --- a/drivers/staging/android/Makefile
> +++ b/drivers/staging/android/Makefile
> @@ -4,3 +4,4 @@ obj-y                    += ion/
> 
>  obj-$(CONFIG_ASHMEM)            += ashmem.o
>  obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER)    += lowmemorykiller.o
> +obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS)    += lowmemorykiller_stats.o
> diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
> index ec3b665..15c1b38 100644
> --- a/drivers/staging/android/lowmemorykiller.c
> +++ b/drivers/staging/android/lowmemorykiller.c
> @@ -42,6 +42,7 @@
>  #include <linux/rcupdate.h>
>  #include <linux/profile.h>
>  #include <linux/notifier.h>
> +#include "lowmemorykiller_stats.h"
> 
>  static u32 lowmem_debug_level = 1;
>  static short lowmem_adj[6] = {
> @@ -72,6 +73,7 @@ static unsigned long lowmem_deathpending_timeout;
>  static unsigned long lowmem_count(struct shrinker *s,
>                    struct shrink_control *sc)
>  {
> +    lmk_inc_stats(LMK_COUNT);
>      return global_node_page_state(NR_ACTIVE_ANON) +
>          global_node_page_state(NR_ACTIVE_FILE) +
>          global_node_page_state(NR_INACTIVE_ANON) +
> @@ -95,6 +97,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>                  global_node_page_state(NR_SHMEM) -
>                  total_swapcache_pages();
> 
> +    lmk_inc_stats(LMK_SCAN);
>      if (lowmem_adj_size < array_size)
>          array_size = lowmem_adj_size;
>      if (lowmem_minfree_size < array_size)
> @@ -134,6 +137,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>          if (task_lmk_waiting(p) &&
>              time_before_eq(jiffies, lowmem_deathpending_timeout)) {
>              task_unlock(p);
> +            lmk_inc_stats(LMK_TIMEOUT);
>              rcu_read_unlock();
>              return 0;
>          }
> @@ -179,7 +183,9 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>                   other_free * (long)(PAGE_SIZE / 1024));
>          lowmem_deathpending_timeout = jiffies + HZ;
>          rem += selected_tasksize;
> -    }
> +        lmk_inc_stats(LMK_KILL);
> +    } else
> +        lmk_inc_stats(LMK_WASTE);
> 
>      lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
>               sc->nr_to_scan, sc->gfp_mask, rem);
> @@ -196,6 +202,7 @@ static struct shrinker lowmem_shrinker = {
>  static int __init lowmem_init(void)
>  {
>      register_shrinker(&lowmem_shrinker);
> +    init_procfs_lmk();
>      return 0;
>  }
>  device_initcall(lowmem_init);
> diff --git a/drivers/staging/android/lowmemorykiller_stats.c b/drivers/staging/android/lowmemorykiller_stats.c
> new file mode 100644
> index 0000000..673691c
> --- /dev/null
> +++ b/drivers/staging/android/lowmemorykiller_stats.c
> @@ -0,0 +1,85 @@
> +/*
> + *  lowmemorykiller_stats
> + *
> + *  Copyright (C) 2017 Sony Mobile Communications Inc.
> + *
> + *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
> + *
> + *  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 code is bookkeeping of statistical information
> + * from lowmemorykiller and provide a node in proc "/proc/lmkstats".
> + */
> +
> +#include <linux/proc_fs.h>
> +#include <linux/seq_file.h>
> +#include "lowmemorykiller_stats.h"
> +
> +struct lmk_stats {
> +    atomic_long_t scans; /* counter as in shrinker scans */
> +    atomic_long_t kills; /* the number of sigkills sent */
> +    atomic_long_t waste; /* the numer of extensive calls that did
> +                  * not lead to anything
> +                  */
> +    atomic_long_t timeout; /* counter for shrinker calls that needed
> +                * to be cancelled due to pending kills
> +                */
> +    atomic_long_t count; /* number of shrinker count calls */
> +    atomic_long_t unknown; /* internal */
> +} st;
> +
> +void lmk_inc_stats(int key)
> +{
> +    switch (key) {
> +    case LMK_SCAN:
> +        atomic_long_inc(&st.scans);
> +        break;
> +    case LMK_KILL:
> +        atomic_long_inc(&st.kills);
> +        break;
> +    case LMK_WASTE:
> +        atomic_long_inc(&st.waste);
> +        break;
> +    case LMK_TIMEOUT:
> +        atomic_long_inc(&st.timeout);
> +        break;
> +    case LMK_COUNT:
> +        atomic_long_inc(&st.count);
> +        break;
> +    default:
> +        atomic_long_inc(&st.unknown);
> +        break;
> +    }
> +}
> +
> +static int lmk_proc_show(struct seq_file *m, void *v)
> +{
> +    seq_printf(m, "kill: %ld\n", atomic_long_read(&st.kills));
> +    seq_printf(m, "scan: %ld\n", atomic_long_read(&st.scans));
> +    seq_printf(m, "waste: %ld\n", atomic_long_read(&st.waste));
> +    seq_printf(m, "timeout: %ld\n", atomic_long_read(&st.timeout));
> +    seq_printf(m, "count: %ld\n", atomic_long_read(&st.count));
> +    seq_printf(m, "unknown: %ld (internal)\n",
> +           atomic_long_read(&st.unknown));
> +
> +    return 0;
> +}
> +
> +static int lmk_proc_open(struct inode *inode, struct file *file)
> +{
> +    return single_open(file, lmk_proc_show, PDE_DATA(inode));
> +}
> +
> +static const struct file_operations lmk_proc_fops = {
> +    .open        = lmk_proc_open,
> +    .read        = seq_read,
> +    .release    = single_release
> +};
> +
> +int __init init_procfs_lmk(void)
> +{
> +    proc_create_data(LMK_PROCFS_NAME, 0444, NULL, &lmk_proc_fops, NULL);
> +    return 0;
> +}
> diff --git a/drivers/staging/android/lowmemorykiller_stats.h b/drivers/staging/android/lowmemorykiller_stats.h
> new file mode 100644
> index 0000000..abeb6924
> --- /dev/null
> +++ b/drivers/staging/android/lowmemorykiller_stats.h
> @@ -0,0 +1,29 @@
> +/*
> + *  lowmemorykiller_stats interface
> + *
> + *  Copyright (C) 2017 Sony Mobile Communications Inc.
> + *
> + *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
> + *
> + *  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.
> + */
> +
> +enum  lmk_kill_stats {
> +    LMK_SCAN = 1,
> +    LMK_KILL = 2,
> +    LMK_WASTE = 3,
> +    LMK_TIMEOUT = 4,
> +    LMK_COUNT = 5
> +};
> +
> +#define LMK_PROCFS_NAME "lmkstats"
> +
> +#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS
> +void lmk_inc_stats(int key);
> +int __init init_procfs_lmk(void);
> +#else
> +static inline void lmk_inc_stats(int key) { return; };
> +static inline int __init init_procfs_lmk(void) { return 0; };
> +#endif
> -- 
> 2.4.2
> 
> 
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majordomo@kvack.org.  For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-09 19:26 ` Michal Hocko
@ 2017-02-09 20:07   ` Greg Kroah-Hartman
  2017-02-09 20:54     ` Michal Hocko
  0 siblings, 1 reply; 20+ messages in thread
From: Greg Kroah-Hartman @ 2017-02-09 20:07 UTC (permalink / raw)
  To: Michal Hocko
  Cc: peter enderborg, devel, linux-kernel, Arve Hjønnevåg,
	Riley Andrews, Linus Torvalds, linux-mm

On Thu, Feb 09, 2017 at 08:26:41PM +0100, Michal Hocko wrote:
> On Thu 09-02-17 14:21:45, peter enderborg wrote:
> > This collects stats for shrinker calls and how much
> > waste work we do within the lowmemorykiller.
> 
> This doesn't explain why do we need this information and who is going to
> use it. Not to mention it exports it in /proc which is considered a
> stable user API. This is a no-go, especially for something that is still
> lingering in the staging tree without any actuall effort to make it
> fully supported MM feature. I am actually strongly inclined to simply
> drop lmk from the tree completely.

I thought that someone was working to get the "native" mm features to
work properly with the lmk "feature"  Do you recall if that work got
rejected, or just never happened?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-09 20:07   ` Greg Kroah-Hartman
@ 2017-02-09 20:54     ` Michal Hocko
  2017-02-10  7:21       ` peter enderborg
  0 siblings, 1 reply; 20+ messages in thread
From: Michal Hocko @ 2017-02-09 20:54 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: peter enderborg, devel, linux-kernel, Arve Hjønnevåg,
	Riley Andrews, Linus Torvalds, linux-mm

On Thu 09-02-17 21:07:37, Greg KH wrote:
> On Thu, Feb 09, 2017 at 08:26:41PM +0100, Michal Hocko wrote:
> > On Thu 09-02-17 14:21:45, peter enderborg wrote:
> > > This collects stats for shrinker calls and how much
> > > waste work we do within the lowmemorykiller.
> > 
> > This doesn't explain why do we need this information and who is going to
> > use it. Not to mention it exports it in /proc which is considered a
> > stable user API. This is a no-go, especially for something that is still
> > lingering in the staging tree without any actuall effort to make it
> > fully supported MM feature. I am actually strongly inclined to simply
> > drop lmk from the tree completely.
> 
> I thought that someone was working to get the "native" mm features to
> work properly with the lmk "feature"  Do you recall if that work got
> rejected, or just never happened?

Never happened AFAIR. There were some attempts to tune the current
behavior which has been rejected for one reason or another but I am not
really aware of anybody working on moving the code from staging area.

I already have this in the to-send queue, just didn't get to post it yet
because I planned to polish the reasoning some more.
---
>From 9f871b54a387e0a7cdfaf0fa256d1440093e427c Mon Sep 17 00:00:00 2001
From: Michal Hocko <mhocko@suse.com>
Date: Wed, 1 Feb 2017 10:37:30 +0100
Subject: [PATCH] staging, android: remove lowmemory killer from the tree

Lowmemory killer is sitting in the staging tree since 2008 without any
serious interest for fixing issues brought up by the MM folks. The main
objection is that the implementation is basically broken by design:
	- it hooks into slab shrinker API which is not suitable for this
	  purpose. lowmem_count implementation just shows this nicely.
	  There is no scaling based on the memory pressure and no
	  feedback to the generic shrinker infrastructure.
	- it is not reclaim context aware - no NUMA and/or memcg
	  awareness.

As the code stands right now it just adds a maintenance overhead when
core MM changes have to update lowmemorykiller.c as well.

Signed-off-by: Michal Hocko <mhocko@suse.com>
---
 drivers/staging/android/Kconfig           |  10 --
 drivers/staging/android/Makefile          |   1 -
 drivers/staging/android/lowmemorykiller.c | 212 ------------------------------
 include/linux/sched.h                     |   4 -
 4 files changed, 227 deletions(-)
 delete mode 100644 drivers/staging/android/lowmemorykiller.c

diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
index 6c00d6f765c6..71a50b99caff 100644
--- a/drivers/staging/android/Kconfig
+++ b/drivers/staging/android/Kconfig
@@ -14,16 +14,6 @@ config ASHMEM
 	  It is, in theory, a good memory allocator for low-memory devices,
 	  because it can discard shared memory units when under memory pressure.
 
-config ANDROID_LOW_MEMORY_KILLER
-	bool "Android Low Memory Killer"
-	---help---
-	  Registers processes to be killed when low memory conditions, this is useful
-	  as there is no particular swap space on android.
-
-	  The registered process will kill according to the priorities in android init
-	  scripts (/init.rc), and it defines priority values with minimum free memory size
-	  for each priority.
-
 source "drivers/staging/android/ion/Kconfig"
 
 endif # if ANDROID
diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
index 7ed1be798909..7cf1564a49a5 100644
--- a/drivers/staging/android/Makefile
+++ b/drivers/staging/android/Makefile
@@ -3,4 +3,3 @@ ccflags-y += -I$(src)			# needed for trace events
 obj-y					+= ion/
 
 obj-$(CONFIG_ASHMEM)			+= ashmem.o
-obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER)	+= lowmemorykiller.o
diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
deleted file mode 100644
index ec3b66561412..000000000000
--- a/drivers/staging/android/lowmemorykiller.c
+++ /dev/null
@@ -1,212 +0,0 @@
-/* drivers/misc/lowmemorykiller.c
- *
- * The lowmemorykiller driver lets user-space specify a set of memory thresholds
- * where processes with a range of oom_score_adj values will get killed. Specify
- * the minimum oom_score_adj values in
- * /sys/module/lowmemorykiller/parameters/adj and the number of free pages in
- * /sys/module/lowmemorykiller/parameters/minfree. Both files take a comma
- * separated list of numbers in ascending order.
- *
- * For example, write "0,8" to /sys/module/lowmemorykiller/parameters/adj and
- * "1024,4096" to /sys/module/lowmemorykiller/parameters/minfree to kill
- * processes with a oom_score_adj value of 8 or higher when the free memory
- * drops below 4096 pages and kill processes with a oom_score_adj value of 0 or
- * higher when the free memory drops below 1024 pages.
- *
- * The driver considers memory used for caches to be free, but if a large
- * percentage of the cached memory is locked this can be very inaccurate
- * and processes may not get killed until the normal oom killer is triggered.
- *
- * Copyright (C) 2007-2008 Google, Inc.
- *
- * This software is licensed under the terms of the GNU General Public
- * License version 2, as published by the Free Software Foundation, and
- * may be copied, distributed, and modified under those terms.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- */
-
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
-
-#include <linux/init.h>
-#include <linux/moduleparam.h>
-#include <linux/kernel.h>
-#include <linux/mm.h>
-#include <linux/oom.h>
-#include <linux/sched.h>
-#include <linux/swap.h>
-#include <linux/rcupdate.h>
-#include <linux/profile.h>
-#include <linux/notifier.h>
-
-static u32 lowmem_debug_level = 1;
-static short lowmem_adj[6] = {
-	0,
-	1,
-	6,
-	12,
-};
-
-static int lowmem_adj_size = 4;
-static int lowmem_minfree[6] = {
-	3 * 512,	/* 6MB */
-	2 * 1024,	/* 8MB */
-	4 * 1024,	/* 16MB */
-	16 * 1024,	/* 64MB */
-};
-
-static int lowmem_minfree_size = 4;
-
-static unsigned long lowmem_deathpending_timeout;
-
-#define lowmem_print(level, x...)			\
-	do {						\
-		if (lowmem_debug_level >= (level))	\
-			pr_info(x);			\
-	} while (0)
-
-static unsigned long lowmem_count(struct shrinker *s,
-				  struct shrink_control *sc)
-{
-	return global_node_page_state(NR_ACTIVE_ANON) +
-		global_node_page_state(NR_ACTIVE_FILE) +
-		global_node_page_state(NR_INACTIVE_ANON) +
-		global_node_page_state(NR_INACTIVE_FILE);
-}
-
-static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
-{
-	struct task_struct *tsk;
-	struct task_struct *selected = NULL;
-	unsigned long rem = 0;
-	int tasksize;
-	int i;
-	short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
-	int minfree = 0;
-	int selected_tasksize = 0;
-	short selected_oom_score_adj;
-	int array_size = ARRAY_SIZE(lowmem_adj);
-	int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
-	int other_file = global_node_page_state(NR_FILE_PAGES) -
-				global_node_page_state(NR_SHMEM) -
-				total_swapcache_pages();
-
-	if (lowmem_adj_size < array_size)
-		array_size = lowmem_adj_size;
-	if (lowmem_minfree_size < array_size)
-		array_size = lowmem_minfree_size;
-	for (i = 0; i < array_size; i++) {
-		minfree = lowmem_minfree[i];
-		if (other_free < minfree && other_file < minfree) {
-			min_score_adj = lowmem_adj[i];
-			break;
-		}
-	}
-
-	lowmem_print(3, "lowmem_scan %lu, %x, ofree %d %d, ma %hd\n",
-		     sc->nr_to_scan, sc->gfp_mask, other_free,
-		     other_file, min_score_adj);
-
-	if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
-		lowmem_print(5, "lowmem_scan %lu, %x, return 0\n",
-			     sc->nr_to_scan, sc->gfp_mask);
-		return 0;
-	}
-
-	selected_oom_score_adj = min_score_adj;
-
-	rcu_read_lock();
-	for_each_process(tsk) {
-		struct task_struct *p;
-		short oom_score_adj;
-
-		if (tsk->flags & PF_KTHREAD)
-			continue;
-
-		p = find_lock_task_mm(tsk);
-		if (!p)
-			continue;
-
-		if (task_lmk_waiting(p) &&
-		    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
-			task_unlock(p);
-			rcu_read_unlock();
-			return 0;
-		}
-		oom_score_adj = p->signal->oom_score_adj;
-		if (oom_score_adj < min_score_adj) {
-			task_unlock(p);
-			continue;
-		}
-		tasksize = get_mm_rss(p->mm);
-		task_unlock(p);
-		if (tasksize <= 0)
-			continue;
-		if (selected) {
-			if (oom_score_adj < selected_oom_score_adj)
-				continue;
-			if (oom_score_adj == selected_oom_score_adj &&
-			    tasksize <= selected_tasksize)
-				continue;
-		}
-		selected = p;
-		selected_tasksize = tasksize;
-		selected_oom_score_adj = oom_score_adj;
-		lowmem_print(2, "select '%s' (%d), adj %hd, size %d, to kill\n",
-			     p->comm, p->pid, oom_score_adj, tasksize);
-	}
-	if (selected) {
-		task_lock(selected);
-		send_sig(SIGKILL, selected, 0);
-		if (selected->mm)
-			task_set_lmk_waiting(selected);
-		task_unlock(selected);
-		lowmem_print(1, "Killing '%s' (%d), adj %hd,\n"
-				 "   to free %ldkB on behalf of '%s' (%d) because\n"
-				 "   cache %ldkB is below limit %ldkB for oom_score_adj %hd\n"
-				 "   Free memory is %ldkB above reserved\n",
-			     selected->comm, selected->pid,
-			     selected_oom_score_adj,
-			     selected_tasksize * (long)(PAGE_SIZE / 1024),
-			     current->comm, current->pid,
-			     other_file * (long)(PAGE_SIZE / 1024),
-			     minfree * (long)(PAGE_SIZE / 1024),
-			     min_score_adj,
-			     other_free * (long)(PAGE_SIZE / 1024));
-		lowmem_deathpending_timeout = jiffies + HZ;
-		rem += selected_tasksize;
-	}
-
-	lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
-		     sc->nr_to_scan, sc->gfp_mask, rem);
-	rcu_read_unlock();
-	return rem;
-}
-
-static struct shrinker lowmem_shrinker = {
-	.scan_objects = lowmem_scan,
-	.count_objects = lowmem_count,
-	.seeks = DEFAULT_SEEKS * 16
-};
-
-static int __init lowmem_init(void)
-{
-	register_shrinker(&lowmem_shrinker);
-	return 0;
-}
-device_initcall(lowmem_init);
-
-/*
- * not really modular, but the easiest way to keep compat with existing
- * bootargs behaviour is to continue using module_param here.
- */
-module_param_named(cost, lowmem_shrinker.seeks, int, 0644);
-module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size, 0644);
-module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
-			 0644);
-module_param_named(debug_level, lowmem_debug_level, uint, 0644);
-
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e93594b88130..3cc6c650fa6a 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2347,7 +2347,6 @@ static inline void memalloc_noio_restore(unsigned int flags)
 #define PFA_NO_NEW_PRIVS 0	/* May not gain new privileges. */
 #define PFA_SPREAD_PAGE  1      /* Spread page cache over cpuset */
 #define PFA_SPREAD_SLAB  2      /* Spread some slab caches over cpuset */
-#define PFA_LMK_WAITING  3      /* Lowmemorykiller is waiting */
 
 
 #define TASK_PFA_TEST(name, func)					\
@@ -2371,9 +2370,6 @@ TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
 TASK_PFA_SET(SPREAD_SLAB, spread_slab)
 TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
 
-TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
-TASK_PFA_SET(LMK_WAITING, lmk_waiting)
-
 /*
  * task->jobctl flags
  */
-- 
2.11.0

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-09 20:54     ` Michal Hocko
@ 2017-02-10  7:21       ` peter enderborg
  2017-02-10  7:51         ` Greg Kroah-Hartman
  0 siblings, 1 reply; 20+ messages in thread
From: peter enderborg @ 2017-02-10  7:21 UTC (permalink / raw)
  To: Michal Hocko, Greg Kroah-Hartman
  Cc: devel, linux-kernel, Arve Hjønnevåg, Riley Andrews,
	Linus Torvalds, linux-mm

Im not speaking for google, but I think there is a work ongoing to
replace this with user-space code. Until then we have to polish
this version as good as we can. It is essential for android as it is now.

On 02/09/2017 09:54 PM, Michal Hocko wrote:
> On Thu 09-02-17 21:07:37, Greg KH wrote:
>> On Thu, Feb 09, 2017 at 08:26:41PM +0100, Michal Hocko wrote:
>>> On Thu 09-02-17 14:21:45, peter enderborg wrote:
>>>> This collects stats for shrinker calls and how much
>>>> waste work we do within the lowmemorykiller.
>>> This doesn't explain why do we need this information and who is going to
>>> use it. Not to mention it exports it in /proc which is considered a
>>> stable user API. This is a no-go, especially for something that is still
>>> lingering in the staging tree without any actuall effort to make it
>>> fully supported MM feature. I am actually strongly inclined to simply
>>> drop lmk from the tree completely.
>> I thought that someone was working to get the "native" mm features to
>> work properly with the lmk "feature"  Do you recall if that work got
>> rejected, or just never happened?
> Never happened AFAIR. There were some attempts to tune the current
> behavior which has been rejected for one reason or another but I am not
> really aware of anybody working on moving the code from staging area.
>
> I already have this in the to-send queue, just didn't get to post it yet
> because I planned to polish the reasoning some more.
> ---
> From 9f871b54a387e0a7cdfaf0fa256d1440093e427c Mon Sep 17 00:00:00 2001
> From: Michal Hocko <mhocko@suse.com>
> Date: Wed, 1 Feb 2017 10:37:30 +0100
> Subject: [PATCH] staging, android: remove lowmemory killer from the tree
>
> Lowmemory killer is sitting in the staging tree since 2008 without any
> serious interest for fixing issues brought up by the MM folks. The main
> objection is that the implementation is basically broken by design:
> 	- it hooks into slab shrinker API which is not suitable for this
> 	  purpose. lowmem_count implementation just shows this nicely.
> 	  There is no scaling based on the memory pressure and no
> 	  feedback to the generic shrinker infrastructure.
> 	- it is not reclaim context aware - no NUMA and/or memcg
> 	  awareness.
>
> As the code stands right now it just adds a maintenance overhead when
> core MM changes have to update lowmemorykiller.c as well.
>
> Signed-off-by: Michal Hocko <mhocko@suse.com>
> ---
>  drivers/staging/android/Kconfig           |  10 --
>  drivers/staging/android/Makefile          |   1 -
>  drivers/staging/android/lowmemorykiller.c | 212 ------------------------------
>  include/linux/sched.h                     |   4 -
>  4 files changed, 227 deletions(-)
>  delete mode 100644 drivers/staging/android/lowmemorykiller.c
>
> diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
> index 6c00d6f765c6..71a50b99caff 100644
> --- a/drivers/staging/android/Kconfig
> +++ b/drivers/staging/android/Kconfig
> @@ -14,16 +14,6 @@ config ASHMEM
>  	  It is, in theory, a good memory allocator for low-memory devices,
>  	  because it can discard shared memory units when under memory pressure.
>  
> -config ANDROID_LOW_MEMORY_KILLER
> -	bool "Android Low Memory Killer"
> -	---help---
> -	  Registers processes to be killed when low memory conditions, this is useful
> -	  as there is no particular swap space on android.
> -
> -	  The registered process will kill according to the priorities in android init
> -	  scripts (/init.rc), and it defines priority values with minimum free memory size
> -	  for each priority.
> -
>  source "drivers/staging/android/ion/Kconfig"
>  
>  endif # if ANDROID
> diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
> index 7ed1be798909..7cf1564a49a5 100644
> --- a/drivers/staging/android/Makefile
> +++ b/drivers/staging/android/Makefile
> @@ -3,4 +3,3 @@ ccflags-y += -I$(src)			# needed for trace events
>  obj-y					+= ion/
>  
>  obj-$(CONFIG_ASHMEM)			+= ashmem.o
> -obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER)	+= lowmemorykiller.o
> diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
> deleted file mode 100644
> index ec3b66561412..000000000000
> --- a/drivers/staging/android/lowmemorykiller.c
> +++ /dev/null
> @@ -1,212 +0,0 @@
> -/* drivers/misc/lowmemorykiller.c
> - *
> - * The lowmemorykiller driver lets user-space specify a set of memory thresholds
> - * where processes with a range of oom_score_adj values will get killed. Specify
> - * the minimum oom_score_adj values in
> - * /sys/module/lowmemorykiller/parameters/adj and the number of free pages in
> - * /sys/module/lowmemorykiller/parameters/minfree. Both files take a comma
> - * separated list of numbers in ascending order.
> - *
> - * For example, write "0,8" to /sys/module/lowmemorykiller/parameters/adj and
> - * "1024,4096" to /sys/module/lowmemorykiller/parameters/minfree to kill
> - * processes with a oom_score_adj value of 8 or higher when the free memory
> - * drops below 4096 pages and kill processes with a oom_score_adj value of 0 or
> - * higher when the free memory drops below 1024 pages.
> - *
> - * The driver considers memory used for caches to be free, but if a large
> - * percentage of the cached memory is locked this can be very inaccurate
> - * and processes may not get killed until the normal oom killer is triggered.
> - *
> - * Copyright (C) 2007-2008 Google, Inc.
> - *
> - * This software is licensed under the terms of the GNU General Public
> - * License version 2, as published by the Free Software Foundation, and
> - * may be copied, distributed, and modified under those terms.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> - * GNU General Public License for more details.
> - *
> - */
> -
> -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> -
> -#include <linux/init.h>
> -#include <linux/moduleparam.h>
> -#include <linux/kernel.h>
> -#include <linux/mm.h>
> -#include <linux/oom.h>
> -#include <linux/sched.h>
> -#include <linux/swap.h>
> -#include <linux/rcupdate.h>
> -#include <linux/profile.h>
> -#include <linux/notifier.h>
> -
> -static u32 lowmem_debug_level = 1;
> -static short lowmem_adj[6] = {
> -	0,
> -	1,
> -	6,
> -	12,
> -};
> -
> -static int lowmem_adj_size = 4;
> -static int lowmem_minfree[6] = {
> -	3 * 512,	/* 6MB */
> -	2 * 1024,	/* 8MB */
> -	4 * 1024,	/* 16MB */
> -	16 * 1024,	/* 64MB */
> -};
> -
> -static int lowmem_minfree_size = 4;
> -
> -static unsigned long lowmem_deathpending_timeout;
> -
> -#define lowmem_print(level, x...)			\
> -	do {						\
> -		if (lowmem_debug_level >= (level))	\
> -			pr_info(x);			\
> -	} while (0)
> -
> -static unsigned long lowmem_count(struct shrinker *s,
> -				  struct shrink_control *sc)
> -{
> -	return global_node_page_state(NR_ACTIVE_ANON) +
> -		global_node_page_state(NR_ACTIVE_FILE) +
> -		global_node_page_state(NR_INACTIVE_ANON) +
> -		global_node_page_state(NR_INACTIVE_FILE);
> -}
> -
> -static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
> -{
> -	struct task_struct *tsk;
> -	struct task_struct *selected = NULL;
> -	unsigned long rem = 0;
> -	int tasksize;
> -	int i;
> -	short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
> -	int minfree = 0;
> -	int selected_tasksize = 0;
> -	short selected_oom_score_adj;
> -	int array_size = ARRAY_SIZE(lowmem_adj);
> -	int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
> -	int other_file = global_node_page_state(NR_FILE_PAGES) -
> -				global_node_page_state(NR_SHMEM) -
> -				total_swapcache_pages();
> -
> -	if (lowmem_adj_size < array_size)
> -		array_size = lowmem_adj_size;
> -	if (lowmem_minfree_size < array_size)
> -		array_size = lowmem_minfree_size;
> -	for (i = 0; i < array_size; i++) {
> -		minfree = lowmem_minfree[i];
> -		if (other_free < minfree && other_file < minfree) {
> -			min_score_adj = lowmem_adj[i];
> -			break;
> -		}
> -	}
> -
> -	lowmem_print(3, "lowmem_scan %lu, %x, ofree %d %d, ma %hd\n",
> -		     sc->nr_to_scan, sc->gfp_mask, other_free,
> -		     other_file, min_score_adj);
> -
> -	if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
> -		lowmem_print(5, "lowmem_scan %lu, %x, return 0\n",
> -			     sc->nr_to_scan, sc->gfp_mask);
> -		return 0;
> -	}
> -
> -	selected_oom_score_adj = min_score_adj;
> -
> -	rcu_read_lock();
> -	for_each_process(tsk) {
> -		struct task_struct *p;
> -		short oom_score_adj;
> -
> -		if (tsk->flags & PF_KTHREAD)
> -			continue;
> -
> -		p = find_lock_task_mm(tsk);
> -		if (!p)
> -			continue;
> -
> -		if (task_lmk_waiting(p) &&
> -		    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
> -			task_unlock(p);
> -			rcu_read_unlock();
> -			return 0;
> -		}
> -		oom_score_adj = p->signal->oom_score_adj;
> -		if (oom_score_adj < min_score_adj) {
> -			task_unlock(p);
> -			continue;
> -		}
> -		tasksize = get_mm_rss(p->mm);
> -		task_unlock(p);
> -		if (tasksize <= 0)
> -			continue;
> -		if (selected) {
> -			if (oom_score_adj < selected_oom_score_adj)
> -				continue;
> -			if (oom_score_adj == selected_oom_score_adj &&
> -			    tasksize <= selected_tasksize)
> -				continue;
> -		}
> -		selected = p;
> -		selected_tasksize = tasksize;
> -		selected_oom_score_adj = oom_score_adj;
> -		lowmem_print(2, "select '%s' (%d), adj %hd, size %d, to kill\n",
> -			     p->comm, p->pid, oom_score_adj, tasksize);
> -	}
> -	if (selected) {
> -		task_lock(selected);
> -		send_sig(SIGKILL, selected, 0);
> -		if (selected->mm)
> -			task_set_lmk_waiting(selected);
> -		task_unlock(selected);
> -		lowmem_print(1, "Killing '%s' (%d), adj %hd,\n"
> -				 "   to free %ldkB on behalf of '%s' (%d) because\n"
> -				 "   cache %ldkB is below limit %ldkB for oom_score_adj %hd\n"
> -				 "   Free memory is %ldkB above reserved\n",
> -			     selected->comm, selected->pid,
> -			     selected_oom_score_adj,
> -			     selected_tasksize * (long)(PAGE_SIZE / 1024),
> -			     current->comm, current->pid,
> -			     other_file * (long)(PAGE_SIZE / 1024),
> -			     minfree * (long)(PAGE_SIZE / 1024),
> -			     min_score_adj,
> -			     other_free * (long)(PAGE_SIZE / 1024));
> -		lowmem_deathpending_timeout = jiffies + HZ;
> -		rem += selected_tasksize;
> -	}
> -
> -	lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
> -		     sc->nr_to_scan, sc->gfp_mask, rem);
> -	rcu_read_unlock();
> -	return rem;
> -}
> -
> -static struct shrinker lowmem_shrinker = {
> -	.scan_objects = lowmem_scan,
> -	.count_objects = lowmem_count,
> -	.seeks = DEFAULT_SEEKS * 16
> -};
> -
> -static int __init lowmem_init(void)
> -{
> -	register_shrinker(&lowmem_shrinker);
> -	return 0;
> -}
> -device_initcall(lowmem_init);
> -
> -/*
> - * not really modular, but the easiest way to keep compat with existing
> - * bootargs behaviour is to continue using module_param here.
> - */
> -module_param_named(cost, lowmem_shrinker.seeks, int, 0644);
> -module_param_array_named(adj, lowmem_adj, short, &lowmem_adj_size, 0644);
> -module_param_array_named(minfree, lowmem_minfree, uint, &lowmem_minfree_size,
> -			 0644);
> -module_param_named(debug_level, lowmem_debug_level, uint, 0644);
> -
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index e93594b88130..3cc6c650fa6a 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -2347,7 +2347,6 @@ static inline void memalloc_noio_restore(unsigned int flags)
>  #define PFA_NO_NEW_PRIVS 0	/* May not gain new privileges. */
>  #define PFA_SPREAD_PAGE  1      /* Spread page cache over cpuset */
>  #define PFA_SPREAD_SLAB  2      /* Spread some slab caches over cpuset */
> -#define PFA_LMK_WAITING  3      /* Lowmemorykiller is waiting */
>  
>  
>  #define TASK_PFA_TEST(name, func)					\
> @@ -2371,9 +2370,6 @@ TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
>  TASK_PFA_SET(SPREAD_SLAB, spread_slab)
>  TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
>  
> -TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
> -TASK_PFA_SET(LMK_WAITING, lmk_waiting)
> -
>  /*
>   * task->jobctl flags
>   */

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-10  7:21       ` peter enderborg
@ 2017-02-10  7:51         ` Greg Kroah-Hartman
  2017-02-10  7:59           ` Michal Hocko
  2017-02-10  9:05           ` peter enderborg
  0 siblings, 2 replies; 20+ messages in thread
From: Greg Kroah-Hartman @ 2017-02-10  7:51 UTC (permalink / raw)
  To: peter enderborg
  Cc: Michal Hocko, devel, Riley Andrews, linux-kernel, linux-mm,
	Arve Hjønnevåg, Linus Torvalds

On Fri, Feb 10, 2017 at 08:21:32AM +0100, peter enderborg wrote:
> Im not speaking for google, but I think there is a work ongoing to
> replace this with user-space code.

Really?  I have not heard this at all, any pointers to whom in Google is
doing it?

> Until then we have to polish this version as good as we can. It is
> essential for android as it is now.

But if no one is willing to do the work to fix the reported issues, why
should it remain?  Can you do the work here?  You're already working on
fixing some of the issues in a differnt way, why not do the "real work"
here instead for everyone to benifit from?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-10  7:51         ` Greg Kroah-Hartman
@ 2017-02-10  7:59           ` Michal Hocko
  2017-02-10  9:05             ` peter enderborg
  2017-02-10  9:05           ` peter enderborg
  1 sibling, 1 reply; 20+ messages in thread
From: Michal Hocko @ 2017-02-10  7:59 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: peter enderborg, devel, Riley Andrews, linux-kernel, linux-mm,
	Arve Hjønnevåg, Linus Torvalds

On Fri 10-02-17 08:51:49, Greg KH wrote:
> On Fri, Feb 10, 2017 at 08:21:32AM +0100, peter enderborg wrote:
[...]
> > Until then we have to polish this version as good as we can. It is
> > essential for android as it is now.
> 
> But if no one is willing to do the work to fix the reported issues, why
> should it remain?  Can you do the work here?  You're already working on
> fixing some of the issues in a differnt way, why not do the "real work"
> here instead for everyone to benifit from?

Well, to be honest, I do not think that the current code is easily
fixable. The approach was wrong from the day 1. Abusing slab shrinkers
is just a bad place to stick this logic. This all belongs to the
userspace. For that we need a proper mm pressure notification which is
supposed to be vmpressure but that one also doesn't seem to work all
that great. So rather than trying to fix unfixable I would stronly
suggest focusing on making vmpressure work reliably.
-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-10  7:51         ` Greg Kroah-Hartman
  2017-02-10  7:59           ` Michal Hocko
@ 2017-02-10  9:05           ` peter enderborg
  2017-02-10 10:34             ` Greg Kroah-Hartman
  1 sibling, 1 reply; 20+ messages in thread
From: peter enderborg @ 2017-02-10  9:05 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Michal Hocko, devel, Riley Andrews, linux-kernel, linux-mm,
	Arve Hjønnevåg, Linus Torvalds

On 02/10/2017 08:51 AM, Greg Kroah-Hartman wrote:
> On Fri, Feb 10, 2017 at 08:21:32AM +0100, peter enderborg wrote:
>> Im not speaking for google, but I think there is a work ongoing to
>> replace this with user-space code.
> Really?  I have not heard this at all, any pointers to whom in Google is
> doing it?
>
I think it was mention some of the google conferences. The idea
is the lmkd that uses memory pressure events to trigger this.
>From git log in lmkd i think Colin Cross is involved.

>> Until then we have to polish this version as good as we can. It is
>> essential for android as it is now.
> But if no one is willing to do the work to fix the reported issues, why
> should it remain? 
It is needed by billions of phones.
>  Can you do the work here? 
No. Change the kernel is only one small part of the solution.
>  You're already working on
> fixing some of the issues in a differnt way, why not do the "real work"
> here instead for everyone to benifit from?
The long term solution is something from AOSP.  As you know
we tried to contribute this to AOSP.  As OEM we can't turn android
upside down.  It has to be a step by step.
> thanks,
>
> greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-10  7:59           ` Michal Hocko
@ 2017-02-10  9:05             ` peter enderborg
  2017-02-10  9:15               ` Michal Hocko
  0 siblings, 1 reply; 20+ messages in thread
From: peter enderborg @ 2017-02-10  9:05 UTC (permalink / raw)
  To: Michal Hocko, Greg Kroah-Hartman
  Cc: devel, Riley Andrews, linux-kernel, linux-mm,
	Arve Hjønnevåg, Linus Torvalds

On 02/10/2017 08:59 AM, Michal Hocko wrote:
> On Fri 10-02-17 08:51:49, Greg KH wrote:
>> On Fri, Feb 10, 2017 at 08:21:32AM +0100, peter enderborg wrote:
> [...]
>>> Until then we have to polish this version as good as we can. It is
>>> essential for android as it is now.
>> But if no one is willing to do the work to fix the reported issues, why
>> should it remain?  Can you do the work here?  You're already working on
>> fixing some of the issues in a differnt way, why not do the "real work"
>> here instead for everyone to benifit from?
> Well, to be honest, I do not think that the current code is easily
> fixable. 
This patch improves the current situation and address some
of the issues that makes android devices behaviour different
than other linux systems.
> The approach was wrong from the day 1. Abusing slab shrinkers
> is just a bad place to stick this logic. This all belongs to the
> userspace.
But now it is there and we have to stick with it.
>  For that we need a proper mm pressure notification which is
> supposed to be vmpressure but that one also doesn't seem to work all
> that great. So rather than trying to fix unfixable I would stronly
> suggest focusing on making vmpressure work reliably.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-10  9:05             ` peter enderborg
@ 2017-02-10  9:15               ` Michal Hocko
  2017-02-13 15:42                 ` peter enderborg
  0 siblings, 1 reply; 20+ messages in thread
From: Michal Hocko @ 2017-02-10  9:15 UTC (permalink / raw)
  To: peter enderborg
  Cc: Greg Kroah-Hartman, devel, Riley Andrews, linux-kernel, linux-mm,
	Arve Hjønnevåg, Linus Torvalds

On Fri 10-02-17 10:05:34, peter enderborg wrote:
> On 02/10/2017 08:59 AM, Michal Hocko wrote:
[...]
> > The approach was wrong from the day 1. Abusing slab shrinkers
> > is just a bad place to stick this logic. This all belongs to the
> > userspace.
>
> But now it is there and we have to stick with it.

It is also adding maintenance cost. Just have a look at the git log and
check how many patches were just a result of the core changes which
needed a sync.

I seriously doubt that any of the android devices can run natively on
the Vanilla kernel so insisting on keeping this code in staging doesn't
give much sense to me.
-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-10  9:05           ` peter enderborg
@ 2017-02-10 10:34             ` Greg Kroah-Hartman
  0 siblings, 0 replies; 20+ messages in thread
From: Greg Kroah-Hartman @ 2017-02-10 10:34 UTC (permalink / raw)
  To: peter enderborg
  Cc: devel, Arve Hjønnevåg, linux-kernel, linux-mm,
	Riley Andrews, Michal Hocko, Linus Torvalds

On Fri, Feb 10, 2017 at 10:05:12AM +0100, peter enderborg wrote:
> On 02/10/2017 08:51 AM, Greg Kroah-Hartman wrote:
> > On Fri, Feb 10, 2017 at 08:21:32AM +0100, peter enderborg wrote:
> >> Im not speaking for google, but I think there is a work ongoing to
> >> replace this with user-space code.
> > Really?  I have not heard this at all, any pointers to whom in Google is
> > doing it?
> >
> I think it was mention some of the google conferences. The idea
> is the lmkd that uses memory pressure events to trigger this.
> From git log in lmkd i think Colin Cross is involved.

Great, care to add him to this thread?

> >> Until then we have to polish this version as good as we can. It is
> >> essential for android as it is now.
> > But if no one is willing to do the work to fix the reported issues, why
> > should it remain? 
> It is needed by billions of phones.

Well, something is needed, not necessarily this solution :)

> >  Can you do the work here? 
> No. Change the kernel is only one small part of the solution.

Why can't you work on the whole thing?

> >  You're already working on
> > fixing some of the issues in a differnt way, why not do the "real work"
> > here instead for everyone to benifit from?
> The long term solution is something from AOSP.  As you know
> we tried to contribute this to AOSP.  As OEM we can't turn android
> upside down.  It has to be a step by step.

I posted in AOSP that you should post the patches here as AOSP shouldn't
be taking patches that the community rejects.  There's no reason you
can't also provide the "fix the userspace side" patches into AOSP at the
same time, and provide the "correct" solution here as well.  The kernel
community doesn't care abotu AOSP, nor should anyone expect it to.  You
are going to have to work across both boundries/communities in order to
resolve this properly.

If not, as the kernel developers have pointed out, the in-kernel stuff
will probably be removed as it's causing problems for the upstream
developers, and no one is stepping up to fix it "correctly".

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-10  9:15               ` Michal Hocko
@ 2017-02-13 15:42                 ` peter enderborg
  0 siblings, 0 replies; 20+ messages in thread
From: peter enderborg @ 2017-02-13 15:42 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Greg Kroah-Hartman, devel, Riley Andrews, linux-kernel, linux-mm,
	Arve Hjønnevåg, Linus Torvalds

On 02/10/2017 10:15 AM, Michal Hocko wrote:
> On Fri 10-02-17 10:05:34, peter enderborg wrote:
>> On 02/10/2017 08:59 AM, Michal Hocko wrote:
> [...]
>>> The approach was wrong from the day 1. Abusing slab shrinkers
>>> is just a bad place to stick this logic. This all belongs to the
>>> userspace.
>> But now it is there and we have to stick with it.
> It is also adding maintenance cost. Just have a look at the git log and
> check how many patches were just a result of the core changes which
> needed a sync.
>
> I seriously doubt that any of the android devices can run natively on
> the Vanilla kernel so insisting on keeping this code in staging doesn't
> give much sense to me.

I guess that we more than a few that would like to see that.

We have

http://developer.sonymobile.com/open-devices/how-to-build-and-flash-a-linux-kernel/how-to-build-mainline-linux-for-xperia-devices/

It is not the latest on anything and it is not on par with commercial bundled software.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-15  8:21   ` peter enderborg
@ 2017-02-15 14:31     ` Greg KH
  0 siblings, 0 replies; 20+ messages in thread
From: Greg KH @ 2017-02-15 14:31 UTC (permalink / raw)
  To: peter enderborg; +Cc: devel, riandrews, linux-kernel, linux-mm, arve, torvalds

On Wed, Feb 15, 2017 at 09:21:56AM +0100, peter enderborg wrote:
> On 02/14/2017 05:51 PM, Greg KH wrote:
> > On Tue, Feb 14, 2017 at 05:09:30PM +0100, peter.enderborg@sonymobile.com wrote:
> >> From: Peter Enderborg <peter.enderborg@sonymobile.com>
> >>
> >> This collects stats for shrinker calls and how much
> >> waste work we do within the lowmemorykiller.
> >>
> >> Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>
> > Wait, what changed from the previous versions of this patch?  Did you
> > take the review comments into consideration, or is this just a resend of
> > the original patches in a format that isn't corrupted?
> >
> > thanks,
> >
> > greg k-h
> 
> This is just a send with git-send-email that seems to work better. Nothing
> else than tab-spaces should be different. I would like to have some positive
> feedback from google/android before I start to send updated patches to the list.
> If google are ready for the userspace solution this patch set is pointless for
> upstream kernel.
> 
> Michal Hocko is very negative to hole thing, but we have addressed at least some
> issues he pointed out on the list in 2015. Is there any idea to continue?

If Michal rejected this solution, then I wouldn't be spending much time
on it at all.  Instead, I strongly suggest you try to do what he pointed
out should be done instead.  If that requires userspace help, great, try
that and see what happens.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-15  8:22   ` peter enderborg
@ 2017-02-15 14:29     ` Greg KH
  0 siblings, 0 replies; 20+ messages in thread
From: Greg KH @ 2017-02-15 14:29 UTC (permalink / raw)
  To: peter enderborg; +Cc: devel, riandrews, linux-kernel, linux-mm, arve, torvalds

On Wed, Feb 15, 2017 at 09:22:10AM +0100, peter enderborg wrote:
> On 02/14/2017 05:50 PM, Greg KH wrote:
> > On Tue, Feb 14, 2017 at 05:09:30PM +0100, peter.enderborg@sonymobile.com wrote:
> >> From: Peter Enderborg <peter.enderborg@sonymobile.com>
> >>
> >> This collects stats for shrinker calls and how much
> >> waste work we do within the lowmemorykiller.
> >>
> >> Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>
> >> ---
> >>  drivers/staging/android/Kconfig                 | 11 ++++
> >>  drivers/staging/android/Makefile                |  1 +
> >>  drivers/staging/android/lowmemorykiller.c       |  9 ++-
> >>  drivers/staging/android/lowmemorykiller_stats.c | 85 +++++++++++++++++++++++++
> >>  drivers/staging/android/lowmemorykiller_stats.h | 29 +++++++++
> >>  5 files changed, 134 insertions(+), 1 deletion(-)
> >>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.c
> >>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.h
> >>
> >> diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
> >> index 6c00d6f..96e86c7 100644
> >> --- a/drivers/staging/android/Kconfig
> >> +++ b/drivers/staging/android/Kconfig
> >> @@ -24,6 +24,17 @@ config ANDROID_LOW_MEMORY_KILLER
> >>  	  scripts (/init.rc), and it defines priority values with minimum free memory size
> >>  	  for each priority.
> >>  
> >> +config ANDROID_LOW_MEMORY_KILLER_STATS
> >> +	bool "Android Low Memory Killer: collect statistics"
> >> +	depends on ANDROID_LOW_MEMORY_KILLER
> >> +	default n
> >> +	help
> >> +	  Create a file in /proc/lmkstats that includes
> >> +	  collected statistics about kills, scans and counts
> >> +	  and  interaction with the shrinker. Its content
> >> +	  will be different depeding on lmk implementation used.
> > Ick, no new /proc files please, this isn't a "process" value.  What's
> > wrong with debugfs?
> This is intended for android. Android users are very limited in their access
> to linux part of the system on commercial models and lmk activity has a bad impact on the performance
> of the device. Even the application developers has not much access so it seems to be fair to give
> the users the information about why there is a problem.

Why would you want to give "all users" this information at all?  This is
a debugging tool, your debugging userspace framework can use whatever
interface you create to access it (i.e. debugfs).

Again, do not add debugging stuff to /proc/ that's not ok, sorry.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-14 16:50 ` Greg KH
@ 2017-02-15  8:22   ` peter enderborg
  2017-02-15 14:29     ` Greg KH
  0 siblings, 1 reply; 20+ messages in thread
From: peter enderborg @ 2017-02-15  8:22 UTC (permalink / raw)
  To: Greg KH; +Cc: devel, linux-kernel, arve, riandrews, torvalds, linux-mm

On 02/14/2017 05:50 PM, Greg KH wrote:
> On Tue, Feb 14, 2017 at 05:09:30PM +0100, peter.enderborg@sonymobile.com wrote:
>> From: Peter Enderborg <peter.enderborg@sonymobile.com>
>>
>> This collects stats for shrinker calls and how much
>> waste work we do within the lowmemorykiller.
>>
>> Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>
>> ---
>>  drivers/staging/android/Kconfig                 | 11 ++++
>>  drivers/staging/android/Makefile                |  1 +
>>  drivers/staging/android/lowmemorykiller.c       |  9 ++-
>>  drivers/staging/android/lowmemorykiller_stats.c | 85 +++++++++++++++++++++++++
>>  drivers/staging/android/lowmemorykiller_stats.h | 29 +++++++++
>>  5 files changed, 134 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.c
>>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.h
>>
>> diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
>> index 6c00d6f..96e86c7 100644
>> --- a/drivers/staging/android/Kconfig
>> +++ b/drivers/staging/android/Kconfig
>> @@ -24,6 +24,17 @@ config ANDROID_LOW_MEMORY_KILLER
>>  	  scripts (/init.rc), and it defines priority values with minimum free memory size
>>  	  for each priority.
>>  
>> +config ANDROID_LOW_MEMORY_KILLER_STATS
>> +	bool "Android Low Memory Killer: collect statistics"
>> +	depends on ANDROID_LOW_MEMORY_KILLER
>> +	default n
>> +	help
>> +	  Create a file in /proc/lmkstats that includes
>> +	  collected statistics about kills, scans and counts
>> +	  and  interaction with the shrinker. Its content
>> +	  will be different depeding on lmk implementation used.
> Ick, no new /proc files please, this isn't a "process" value.  What's
> wrong with debugfs?
This is intended for android. Android users are very limited in their access
to linux part of the system on commercial models and lmk activity has a bad impact on the performance
of the device. Even the application developers has not much access so it seems to be fair to give
the users the information about why there is a problem.
> Also note the minor '  ' usage in your first sentence of the help text.
>
>>  source "drivers/staging/android/ion/Kconfig"
>>  
>>  endif # if ANDROID
>> diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
>> index 7ed1be7..d710eb2 100644
>> --- a/drivers/staging/android/Makefile
>> +++ b/drivers/staging/android/Makefile
>> @@ -4,3 +4,4 @@ obj-y					+= ion/
>>  
>>  obj-$(CONFIG_ASHMEM)			+= ashmem.o
>>  obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER)	+= lowmemorykiller.o
>> +obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS)	+= lowmemorykiller_stats.o
>> diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
>> index ec3b665..15c1b38 100644
>> --- a/drivers/staging/android/lowmemorykiller.c
>> +++ b/drivers/staging/android/lowmemorykiller.c
>> @@ -42,6 +42,7 @@
>>  #include <linux/rcupdate.h>
>>  #include <linux/profile.h>
>>  #include <linux/notifier.h>
>> +#include "lowmemorykiller_stats.h"
>>  
>>  static u32 lowmem_debug_level = 1;
>>  static short lowmem_adj[6] = {
>> @@ -72,6 +73,7 @@ static unsigned long lowmem_deathpending_timeout;
>>  static unsigned long lowmem_count(struct shrinker *s,
>>  				  struct shrink_control *sc)
>>  {
>> +	lmk_inc_stats(LMK_COUNT);
>>  	return global_node_page_state(NR_ACTIVE_ANON) +
>>  		global_node_page_state(NR_ACTIVE_FILE) +
>>  		global_node_page_state(NR_INACTIVE_ANON) +
>> @@ -95,6 +97,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>>  				global_node_page_state(NR_SHMEM) -
>>  				total_swapcache_pages();
>>  
>> +	lmk_inc_stats(LMK_SCAN);
>>  	if (lowmem_adj_size < array_size)
>>  		array_size = lowmem_adj_size;
>>  	if (lowmem_minfree_size < array_size)
>> @@ -134,6 +137,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>>  		if (task_lmk_waiting(p) &&
>>  		    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
>>  			task_unlock(p);
>> +			lmk_inc_stats(LMK_TIMEOUT);
>>  			rcu_read_unlock();
>>  			return 0;
>>  		}
>> @@ -179,7 +183,9 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>>  			     other_free * (long)(PAGE_SIZE / 1024));
>>  		lowmem_deathpending_timeout = jiffies + HZ;
>>  		rem += selected_tasksize;
>> -	}
>> +		lmk_inc_stats(LMK_KILL);
>> +	} else
>> +		lmk_inc_stats(LMK_WASTE);
>>  
>>  	lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
>>  		     sc->nr_to_scan, sc->gfp_mask, rem);
>> @@ -196,6 +202,7 @@ static struct shrinker lowmem_shrinker = {
>>  static int __init lowmem_init(void)
>>  {
>>  	register_shrinker(&lowmem_shrinker);
>> +	init_procfs_lmk();
>>  	return 0;
>>  }
>>  device_initcall(lowmem_init);
>> diff --git a/drivers/staging/android/lowmemorykiller_stats.c b/drivers/staging/android/lowmemorykiller_stats.c
>> new file mode 100644
>> index 0000000..673691c
>> --- /dev/null
>> +++ b/drivers/staging/android/lowmemorykiller_stats.c
>> @@ -0,0 +1,85 @@
>> +/*
>> + *  lowmemorykiller_stats
>> + *
>> + *  Copyright (C) 2017 Sony Mobile Communications Inc.
>> + *
>> + *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
>> + *
>> + *  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 code is bookkeeping of statistical information
>> + * from lowmemorykiller and provide a node in proc "/proc/lmkstats".
>> + */
>> +
>> +#include <linux/proc_fs.h>
>> +#include <linux/seq_file.h>
>> +#include "lowmemorykiller_stats.h"
>> +
>> +struct lmk_stats {
>> +	atomic_long_t scans; /* counter as in shrinker scans */
>> +	atomic_long_t kills; /* the number of sigkills sent */
>> +	atomic_long_t waste; /* the numer of extensive calls that did
>> +			      * not lead to anything
>> +			      */
>> +	atomic_long_t timeout; /* counter for shrinker calls that needed
>> +				* to be cancelled due to pending kills
>> +				*/
>> +	atomic_long_t count; /* number of shrinker count calls */
>> +	atomic_long_t unknown; /* internal */
> Why not document the structure in the proper kernel-doc format?
>
> Also, do you really need atomic variables here?
I think so. The lmk are usually called from kswapd0,
but can also be called from other parts. At least for
arm the cost is low so better safe than sorry and
it is better to focus on have the spin_lock held as short
time as possible. 
>
>> +} st;
>> +
>> +void lmk_inc_stats(int key)
>> +{
>> +	switch (key) {
>> +	case LMK_SCAN:
>> +		atomic_long_inc(&st.scans);
>> +		break;
>> +	case LMK_KILL:
>> +		atomic_long_inc(&st.kills);
>> +		break;
>> +	case LMK_WASTE:
>> +		atomic_long_inc(&st.waste);
>> +		break;
>> +	case LMK_TIMEOUT:
>> +		atomic_long_inc(&st.timeout);
>> +		break;
>> +	case LMK_COUNT:
>> +		atomic_long_inc(&st.count);
>> +		break;
>> +	default:
>> +		atomic_long_inc(&st.unknown);
>> +		break;
>> +	}
>> +}
>> +
>> +static int lmk_proc_show(struct seq_file *m, void *v)
>> +{
>> +	seq_printf(m, "kill: %ld\n", atomic_long_read(&st.kills));
>> +	seq_printf(m, "scan: %ld\n", atomic_long_read(&st.scans));
>> +	seq_printf(m, "waste: %ld\n", atomic_long_read(&st.waste));
>> +	seq_printf(m, "timeout: %ld\n", atomic_long_read(&st.timeout));
>> +	seq_printf(m, "count: %ld\n", atomic_long_read(&st.count));
>> +	seq_printf(m, "unknown: %ld (internal)\n",
>> +		   atomic_long_read(&st.unknown));
>> +
>> +	return 0;
>> +}
>> +
>> +static int lmk_proc_open(struct inode *inode, struct file *file)
>> +{
>> +	return single_open(file, lmk_proc_show, PDE_DATA(inode));
>> +}
>> +
>> +static const struct file_operations lmk_proc_fops = {
>> +	.open		= lmk_proc_open,
>> +	.read		= seq_read,
>> +	.release	= single_release
>> +};
>> +
>> +int __init init_procfs_lmk(void)
>> +{
>> +	proc_create_data(LMK_PROCFS_NAME, 0444, NULL, &lmk_proc_fops, NULL);
>> +	return 0;
>> +}
>> diff --git a/drivers/staging/android/lowmemorykiller_stats.h b/drivers/staging/android/lowmemorykiller_stats.h
>> new file mode 100644
>> index 0000000..abeb6924
>> --- /dev/null
>> +++ b/drivers/staging/android/lowmemorykiller_stats.h
>> @@ -0,0 +1,29 @@
>> +/*
>> + *  lowmemorykiller_stats interface
>> + *
>> + *  Copyright (C) 2017 Sony Mobile Communications Inc.
>> + *
>> + *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
>> + *
>> + *  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.
>> + */
>> +
>> +enum  lmk_kill_stats {
> nit, two spaces?
>
>> +	LMK_SCAN = 1,
>> +	LMK_KILL = 2,
>> +	LMK_WASTE = 3,
>> +	LMK_TIMEOUT = 4,
>> +	LMK_COUNT = 5
>> +};
>> +
>> +#define LMK_PROCFS_NAME "lmkstats"
> You only use it once, why create a #define?  Anyway, debugfs is better
> for debugging stuff like this, that is what it was created for.
>From android selinux:
neverallow untrusted_app debugfs_type:file read;

And the define was created upon requests from internal reviews that prefer to
see this in headers.

I have debugfs patch ongoing, but it's more about the internal structure
for debugging than statistical data from the lmk.
> thanks,
>
> greg k-h

thanks

/Peter

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-14 16:51 ` Greg KH
@ 2017-02-15  8:21   ` peter enderborg
  2017-02-15 14:31     ` Greg KH
  0 siblings, 1 reply; 20+ messages in thread
From: peter enderborg @ 2017-02-15  8:21 UTC (permalink / raw)
  To: Greg KH; +Cc: devel, linux-kernel, arve, riandrews, torvalds, linux-mm

On 02/14/2017 05:51 PM, Greg KH wrote:
> On Tue, Feb 14, 2017 at 05:09:30PM +0100, peter.enderborg@sonymobile.com wrote:
>> From: Peter Enderborg <peter.enderborg@sonymobile.com>
>>
>> This collects stats for shrinker calls and how much
>> waste work we do within the lowmemorykiller.
>>
>> Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>
> Wait, what changed from the previous versions of this patch?  Did you
> take the review comments into consideration, or is this just a resend of
> the original patches in a format that isn't corrupted?
>
> thanks,
>
> greg k-h

This is just a send with git-send-email that seems to work better. Nothing
else than tab-spaces should be different. I would like to have some positive
feedback from google/android before I start to send updated patches to the list.
If google are ready for the userspace solution this patch set is pointless for
upstream kernel.

Michal Hocko is very negative to hole thing, but we have addressed at least some
issues he pointed out on the list in 2015. Is there any idea to continue?

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-14 16:09 peter.enderborg
  2017-02-14 16:50 ` Greg KH
@ 2017-02-14 16:51 ` Greg KH
  2017-02-15  8:21   ` peter enderborg
  1 sibling, 1 reply; 20+ messages in thread
From: Greg KH @ 2017-02-14 16:51 UTC (permalink / raw)
  To: peter.enderborg; +Cc: devel, linux-kernel, arve, riandrews, torvalds, linux-mm

On Tue, Feb 14, 2017 at 05:09:30PM +0100, peter.enderborg@sonymobile.com wrote:
> From: Peter Enderborg <peter.enderborg@sonymobile.com>
> 
> This collects stats for shrinker calls and how much
> waste work we do within the lowmemorykiller.
> 
> Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>

Wait, what changed from the previous versions of this patch?  Did you
take the review comments into consideration, or is this just a resend of
the original patches in a format that isn't corrupted?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
  2017-02-14 16:09 peter.enderborg
@ 2017-02-14 16:50 ` Greg KH
  2017-02-15  8:22   ` peter enderborg
  2017-02-14 16:51 ` Greg KH
  1 sibling, 1 reply; 20+ messages in thread
From: Greg KH @ 2017-02-14 16:50 UTC (permalink / raw)
  To: peter.enderborg; +Cc: devel, linux-kernel, arve, riandrews, torvalds, linux-mm

On Tue, Feb 14, 2017 at 05:09:30PM +0100, peter.enderborg@sonymobile.com wrote:
> From: Peter Enderborg <peter.enderborg@sonymobile.com>
> 
> This collects stats for shrinker calls and how much
> waste work we do within the lowmemorykiller.
> 
> Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>
> ---
>  drivers/staging/android/Kconfig                 | 11 ++++
>  drivers/staging/android/Makefile                |  1 +
>  drivers/staging/android/lowmemorykiller.c       |  9 ++-
>  drivers/staging/android/lowmemorykiller_stats.c | 85 +++++++++++++++++++++++++
>  drivers/staging/android/lowmemorykiller_stats.h | 29 +++++++++
>  5 files changed, 134 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.c
>  create mode 100644 drivers/staging/android/lowmemorykiller_stats.h
> 
> diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
> index 6c00d6f..96e86c7 100644
> --- a/drivers/staging/android/Kconfig
> +++ b/drivers/staging/android/Kconfig
> @@ -24,6 +24,17 @@ config ANDROID_LOW_MEMORY_KILLER
>  	  scripts (/init.rc), and it defines priority values with minimum free memory size
>  	  for each priority.
>  
> +config ANDROID_LOW_MEMORY_KILLER_STATS
> +	bool "Android Low Memory Killer: collect statistics"
> +	depends on ANDROID_LOW_MEMORY_KILLER
> +	default n
> +	help
> +	  Create a file in /proc/lmkstats that includes
> +	  collected statistics about kills, scans and counts
> +	  and  interaction with the shrinker. Its content
> +	  will be different depeding on lmk implementation used.

Ick, no new /proc files please, this isn't a "process" value.  What's
wrong with debugfs?

Also note the minor '  ' usage in your first sentence of the help text.

>  source "drivers/staging/android/ion/Kconfig"
>  
>  endif # if ANDROID
> diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
> index 7ed1be7..d710eb2 100644
> --- a/drivers/staging/android/Makefile
> +++ b/drivers/staging/android/Makefile
> @@ -4,3 +4,4 @@ obj-y					+= ion/
>  
>  obj-$(CONFIG_ASHMEM)			+= ashmem.o
>  obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER)	+= lowmemorykiller.o
> +obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS)	+= lowmemorykiller_stats.o
> diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
> index ec3b665..15c1b38 100644
> --- a/drivers/staging/android/lowmemorykiller.c
> +++ b/drivers/staging/android/lowmemorykiller.c
> @@ -42,6 +42,7 @@
>  #include <linux/rcupdate.h>
>  #include <linux/profile.h>
>  #include <linux/notifier.h>
> +#include "lowmemorykiller_stats.h"
>  
>  static u32 lowmem_debug_level = 1;
>  static short lowmem_adj[6] = {
> @@ -72,6 +73,7 @@ static unsigned long lowmem_deathpending_timeout;
>  static unsigned long lowmem_count(struct shrinker *s,
>  				  struct shrink_control *sc)
>  {
> +	lmk_inc_stats(LMK_COUNT);
>  	return global_node_page_state(NR_ACTIVE_ANON) +
>  		global_node_page_state(NR_ACTIVE_FILE) +
>  		global_node_page_state(NR_INACTIVE_ANON) +
> @@ -95,6 +97,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>  				global_node_page_state(NR_SHMEM) -
>  				total_swapcache_pages();
>  
> +	lmk_inc_stats(LMK_SCAN);
>  	if (lowmem_adj_size < array_size)
>  		array_size = lowmem_adj_size;
>  	if (lowmem_minfree_size < array_size)
> @@ -134,6 +137,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>  		if (task_lmk_waiting(p) &&
>  		    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
>  			task_unlock(p);
> +			lmk_inc_stats(LMK_TIMEOUT);
>  			rcu_read_unlock();
>  			return 0;
>  		}
> @@ -179,7 +183,9 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
>  			     other_free * (long)(PAGE_SIZE / 1024));
>  		lowmem_deathpending_timeout = jiffies + HZ;
>  		rem += selected_tasksize;
> -	}
> +		lmk_inc_stats(LMK_KILL);
> +	} else
> +		lmk_inc_stats(LMK_WASTE);
>  
>  	lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
>  		     sc->nr_to_scan, sc->gfp_mask, rem);
> @@ -196,6 +202,7 @@ static struct shrinker lowmem_shrinker = {
>  static int __init lowmem_init(void)
>  {
>  	register_shrinker(&lowmem_shrinker);
> +	init_procfs_lmk();
>  	return 0;
>  }
>  device_initcall(lowmem_init);
> diff --git a/drivers/staging/android/lowmemorykiller_stats.c b/drivers/staging/android/lowmemorykiller_stats.c
> new file mode 100644
> index 0000000..673691c
> --- /dev/null
> +++ b/drivers/staging/android/lowmemorykiller_stats.c
> @@ -0,0 +1,85 @@
> +/*
> + *  lowmemorykiller_stats
> + *
> + *  Copyright (C) 2017 Sony Mobile Communications Inc.
> + *
> + *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
> + *
> + *  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 code is bookkeeping of statistical information
> + * from lowmemorykiller and provide a node in proc "/proc/lmkstats".
> + */
> +
> +#include <linux/proc_fs.h>
> +#include <linux/seq_file.h>
> +#include "lowmemorykiller_stats.h"
> +
> +struct lmk_stats {
> +	atomic_long_t scans; /* counter as in shrinker scans */
> +	atomic_long_t kills; /* the number of sigkills sent */
> +	atomic_long_t waste; /* the numer of extensive calls that did
> +			      * not lead to anything
> +			      */
> +	atomic_long_t timeout; /* counter for shrinker calls that needed
> +				* to be cancelled due to pending kills
> +				*/
> +	atomic_long_t count; /* number of shrinker count calls */
> +	atomic_long_t unknown; /* internal */

Why not document the structure in the proper kernel-doc format?

Also, do you really need atomic variables here?

> +} st;
> +
> +void lmk_inc_stats(int key)
> +{
> +	switch (key) {
> +	case LMK_SCAN:
> +		atomic_long_inc(&st.scans);
> +		break;
> +	case LMK_KILL:
> +		atomic_long_inc(&st.kills);
> +		break;
> +	case LMK_WASTE:
> +		atomic_long_inc(&st.waste);
> +		break;
> +	case LMK_TIMEOUT:
> +		atomic_long_inc(&st.timeout);
> +		break;
> +	case LMK_COUNT:
> +		atomic_long_inc(&st.count);
> +		break;
> +	default:
> +		atomic_long_inc(&st.unknown);
> +		break;
> +	}
> +}
> +
> +static int lmk_proc_show(struct seq_file *m, void *v)
> +{
> +	seq_printf(m, "kill: %ld\n", atomic_long_read(&st.kills));
> +	seq_printf(m, "scan: %ld\n", atomic_long_read(&st.scans));
> +	seq_printf(m, "waste: %ld\n", atomic_long_read(&st.waste));
> +	seq_printf(m, "timeout: %ld\n", atomic_long_read(&st.timeout));
> +	seq_printf(m, "count: %ld\n", atomic_long_read(&st.count));
> +	seq_printf(m, "unknown: %ld (internal)\n",
> +		   atomic_long_read(&st.unknown));
> +
> +	return 0;
> +}
> +
> +static int lmk_proc_open(struct inode *inode, struct file *file)
> +{
> +	return single_open(file, lmk_proc_show, PDE_DATA(inode));
> +}
> +
> +static const struct file_operations lmk_proc_fops = {
> +	.open		= lmk_proc_open,
> +	.read		= seq_read,
> +	.release	= single_release
> +};
> +
> +int __init init_procfs_lmk(void)
> +{
> +	proc_create_data(LMK_PROCFS_NAME, 0444, NULL, &lmk_proc_fops, NULL);
> +	return 0;
> +}
> diff --git a/drivers/staging/android/lowmemorykiller_stats.h b/drivers/staging/android/lowmemorykiller_stats.h
> new file mode 100644
> index 0000000..abeb6924
> --- /dev/null
> +++ b/drivers/staging/android/lowmemorykiller_stats.h
> @@ -0,0 +1,29 @@
> +/*
> + *  lowmemorykiller_stats interface
> + *
> + *  Copyright (C) 2017 Sony Mobile Communications Inc.
> + *
> + *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
> + *
> + *  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.
> + */
> +
> +enum  lmk_kill_stats {

nit, two spaces?

> +	LMK_SCAN = 1,
> +	LMK_KILL = 2,
> +	LMK_WASTE = 3,
> +	LMK_TIMEOUT = 4,
> +	LMK_COUNT = 5
> +};
> +
> +#define LMK_PROCFS_NAME "lmkstats"

You only use it once, why create a #define?  Anyway, debugfs is better
for debugging stuff like this, that is what it was created for.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller
@ 2017-02-14 16:09 peter.enderborg
  2017-02-14 16:50 ` Greg KH
  2017-02-14 16:51 ` Greg KH
  0 siblings, 2 replies; 20+ messages in thread
From: peter.enderborg @ 2017-02-14 16:09 UTC (permalink / raw)
  To: devel, gregkh, linux-kernel, arve, riandrews, torvalds, linux-mm
  Cc: Peter Enderborg

From: Peter Enderborg <peter.enderborg@sonymobile.com>

This collects stats for shrinker calls and how much
waste work we do within the lowmemorykiller.

Signed-off-by: Peter Enderborg <peter.enderborg@sonymobile.com>
---
 drivers/staging/android/Kconfig                 | 11 ++++
 drivers/staging/android/Makefile                |  1 +
 drivers/staging/android/lowmemorykiller.c       |  9 ++-
 drivers/staging/android/lowmemorykiller_stats.c | 85 +++++++++++++++++++++++++
 drivers/staging/android/lowmemorykiller_stats.h | 29 +++++++++
 5 files changed, 134 insertions(+), 1 deletion(-)
 create mode 100644 drivers/staging/android/lowmemorykiller_stats.c
 create mode 100644 drivers/staging/android/lowmemorykiller_stats.h

diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
index 6c00d6f..96e86c7 100644
--- a/drivers/staging/android/Kconfig
+++ b/drivers/staging/android/Kconfig
@@ -24,6 +24,17 @@ config ANDROID_LOW_MEMORY_KILLER
 	  scripts (/init.rc), and it defines priority values with minimum free memory size
 	  for each priority.
 
+config ANDROID_LOW_MEMORY_KILLER_STATS
+	bool "Android Low Memory Killer: collect statistics"
+	depends on ANDROID_LOW_MEMORY_KILLER
+	default n
+	help
+	  Create a file in /proc/lmkstats that includes
+	  collected statistics about kills, scans and counts
+	  and  interaction with the shrinker. Its content
+	  will be different depeding on lmk implementation used.
+
+
 source "drivers/staging/android/ion/Kconfig"
 
 endif # if ANDROID
diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
index 7ed1be7..d710eb2 100644
--- a/drivers/staging/android/Makefile
+++ b/drivers/staging/android/Makefile
@@ -4,3 +4,4 @@ obj-y					+= ion/
 
 obj-$(CONFIG_ASHMEM)			+= ashmem.o
 obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER)	+= lowmemorykiller.o
+obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS)	+= lowmemorykiller_stats.o
diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
index ec3b665..15c1b38 100644
--- a/drivers/staging/android/lowmemorykiller.c
+++ b/drivers/staging/android/lowmemorykiller.c
@@ -42,6 +42,7 @@
 #include <linux/rcupdate.h>
 #include <linux/profile.h>
 #include <linux/notifier.h>
+#include "lowmemorykiller_stats.h"
 
 static u32 lowmem_debug_level = 1;
 static short lowmem_adj[6] = {
@@ -72,6 +73,7 @@ static unsigned long lowmem_deathpending_timeout;
 static unsigned long lowmem_count(struct shrinker *s,
 				  struct shrink_control *sc)
 {
+	lmk_inc_stats(LMK_COUNT);
 	return global_node_page_state(NR_ACTIVE_ANON) +
 		global_node_page_state(NR_ACTIVE_FILE) +
 		global_node_page_state(NR_INACTIVE_ANON) +
@@ -95,6 +97,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
 				global_node_page_state(NR_SHMEM) -
 				total_swapcache_pages();
 
+	lmk_inc_stats(LMK_SCAN);
 	if (lowmem_adj_size < array_size)
 		array_size = lowmem_adj_size;
 	if (lowmem_minfree_size < array_size)
@@ -134,6 +137,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
 		if (task_lmk_waiting(p) &&
 		    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
 			task_unlock(p);
+			lmk_inc_stats(LMK_TIMEOUT);
 			rcu_read_unlock();
 			return 0;
 		}
@@ -179,7 +183,9 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
 			     other_free * (long)(PAGE_SIZE / 1024));
 		lowmem_deathpending_timeout = jiffies + HZ;
 		rem += selected_tasksize;
-	}
+		lmk_inc_stats(LMK_KILL);
+	} else
+		lmk_inc_stats(LMK_WASTE);
 
 	lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
 		     sc->nr_to_scan, sc->gfp_mask, rem);
@@ -196,6 +202,7 @@ static struct shrinker lowmem_shrinker = {
 static int __init lowmem_init(void)
 {
 	register_shrinker(&lowmem_shrinker);
+	init_procfs_lmk();
 	return 0;
 }
 device_initcall(lowmem_init);
diff --git a/drivers/staging/android/lowmemorykiller_stats.c b/drivers/staging/android/lowmemorykiller_stats.c
new file mode 100644
index 0000000..673691c
--- /dev/null
+++ b/drivers/staging/android/lowmemorykiller_stats.c
@@ -0,0 +1,85 @@
+/*
+ *  lowmemorykiller_stats
+ *
+ *  Copyright (C) 2017 Sony Mobile Communications Inc.
+ *
+ *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
+ *
+ *  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 code is bookkeeping of statistical information
+ * from lowmemorykiller and provide a node in proc "/proc/lmkstats".
+ */
+
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+#include "lowmemorykiller_stats.h"
+
+struct lmk_stats {
+	atomic_long_t scans; /* counter as in shrinker scans */
+	atomic_long_t kills; /* the number of sigkills sent */
+	atomic_long_t waste; /* the numer of extensive calls that did
+			      * not lead to anything
+			      */
+	atomic_long_t timeout; /* counter for shrinker calls that needed
+				* to be cancelled due to pending kills
+				*/
+	atomic_long_t count; /* number of shrinker count calls */
+	atomic_long_t unknown; /* internal */
+} st;
+
+void lmk_inc_stats(int key)
+{
+	switch (key) {
+	case LMK_SCAN:
+		atomic_long_inc(&st.scans);
+		break;
+	case LMK_KILL:
+		atomic_long_inc(&st.kills);
+		break;
+	case LMK_WASTE:
+		atomic_long_inc(&st.waste);
+		break;
+	case LMK_TIMEOUT:
+		atomic_long_inc(&st.timeout);
+		break;
+	case LMK_COUNT:
+		atomic_long_inc(&st.count);
+		break;
+	default:
+		atomic_long_inc(&st.unknown);
+		break;
+	}
+}
+
+static int lmk_proc_show(struct seq_file *m, void *v)
+{
+	seq_printf(m, "kill: %ld\n", atomic_long_read(&st.kills));
+	seq_printf(m, "scan: %ld\n", atomic_long_read(&st.scans));
+	seq_printf(m, "waste: %ld\n", atomic_long_read(&st.waste));
+	seq_printf(m, "timeout: %ld\n", atomic_long_read(&st.timeout));
+	seq_printf(m, "count: %ld\n", atomic_long_read(&st.count));
+	seq_printf(m, "unknown: %ld (internal)\n",
+		   atomic_long_read(&st.unknown));
+
+	return 0;
+}
+
+static int lmk_proc_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, lmk_proc_show, PDE_DATA(inode));
+}
+
+static const struct file_operations lmk_proc_fops = {
+	.open		= lmk_proc_open,
+	.read		= seq_read,
+	.release	= single_release
+};
+
+int __init init_procfs_lmk(void)
+{
+	proc_create_data(LMK_PROCFS_NAME, 0444, NULL, &lmk_proc_fops, NULL);
+	return 0;
+}
diff --git a/drivers/staging/android/lowmemorykiller_stats.h b/drivers/staging/android/lowmemorykiller_stats.h
new file mode 100644
index 0000000..abeb6924
--- /dev/null
+++ b/drivers/staging/android/lowmemorykiller_stats.h
@@ -0,0 +1,29 @@
+/*
+ *  lowmemorykiller_stats interface
+ *
+ *  Copyright (C) 2017 Sony Mobile Communications Inc.
+ *
+ *  Author: Peter Enderborg <peter.enderborg@sonymobile.com>
+ *
+ *  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.
+ */
+
+enum  lmk_kill_stats {
+	LMK_SCAN = 1,
+	LMK_KILL = 2,
+	LMK_WASTE = 3,
+	LMK_TIMEOUT = 4,
+	LMK_COUNT = 5
+};
+
+#define LMK_PROCFS_NAME "lmkstats"
+
+#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER_STATS
+void lmk_inc_stats(int key);
+int __init init_procfs_lmk(void);
+#else
+static inline void lmk_inc_stats(int key) { return; };
+static inline int __init init_procfs_lmk(void) { return 0; };
+#endif
-- 
2.4.2

^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2017-02-15 14:31 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-09 13:21 [PATCH 1/3 staging-next] android: Collect statistics from lowmemorykiller peter enderborg
2017-02-09 13:50 ` Greg Kroah-Hartman
2017-02-09 19:26 ` Michal Hocko
2017-02-09 20:07   ` Greg Kroah-Hartman
2017-02-09 20:54     ` Michal Hocko
2017-02-10  7:21       ` peter enderborg
2017-02-10  7:51         ` Greg Kroah-Hartman
2017-02-10  7:59           ` Michal Hocko
2017-02-10  9:05             ` peter enderborg
2017-02-10  9:15               ` Michal Hocko
2017-02-13 15:42                 ` peter enderborg
2017-02-10  9:05           ` peter enderborg
2017-02-10 10:34             ` Greg Kroah-Hartman
2017-02-14 16:09 peter.enderborg
2017-02-14 16:50 ` Greg KH
2017-02-15  8:22   ` peter enderborg
2017-02-15 14:29     ` Greg KH
2017-02-14 16:51 ` Greg KH
2017-02-15  8:21   ` peter enderborg
2017-02-15 14:31     ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).