Re: [PATCH V5 01/10] Add kswapd descriptor

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Ying,

I have some comments and nitpick about coding style.

On Sat, Apr 16, 2011 at 8:23 AM, Ying Han <yinghan@xxxxxxxxxx> wrote:
> There is a kswapd kernel thread for each numa node. We will add a different
> kswapd for each memcg. The kswapd is sleeping in the wait queue headed at

Why?

Easily, many kernel developers raise an eyebrow to increase kernel thread.
So you should justify why we need new kernel thread, why we can't
handle it with workqueue.

Maybe you explained it and I didn't know it. If it is, sorry.
But at least, the patch description included _why_ is much mergeable
to maintainers and helpful to review the code to reviewers.

> kswapd_wait field of a kswapd descriptor. The kswapd descriptor stores
> information of node or memcg and it allows the global and per-memcg background
> reclaim to share common reclaim algorithms.
>
> This patch adds the kswapd descriptor and moves the per-node kswapd to use the
> new structure.
>
> changelog v5..v4:
> 1. add comment on kswapds_spinlock
> 2. remove the kswapds_spinlock. we don't need it here since the kswapd and pgdat
> have 1:1 mapping.
>
> changelog v3..v2:
> 1. move the struct mem_cgroup *kswapd_mem in kswapd sruct to later patch.
> 2. rename thr in kswapd_run to something else.
>
> changelog v2..v1:
> 1. dynamic allocate kswapd descriptor and initialize the wait_queue_head of pgdat
> at kswapd_run.
> 2. add helper macro is_node_kswapd to distinguish per-node/per-cgroup kswapd
> descriptor.
>
> Signed-off-by: Ying Han <yinghan@xxxxxxxxxx>
> ---
> Âinclude/linux/mmzone.h | Â Â3 +-
> Âinclude/linux/swap.h  |  Â7 ++++
> Âmm/page_alloc.c    Â|  Â1 -
> Âmm/vmscan.c      Â|  89 +++++++++++++++++++++++++++++++++++------------
> Â4 files changed, 74 insertions(+), 26 deletions(-)
>
> diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
> index 628f07b..6cba7d2 100644
> --- a/include/linux/mmzone.h
> +++ b/include/linux/mmzone.h
> @@ -640,8 +640,7 @@ typedef struct pglist_data {
> Â Â Â Âunsigned long node_spanned_pages; /* total size of physical page
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â range, including holes */
> Â Â Â Âint node_id;
> - Â Â Â wait_queue_head_t kswapd_wait;
> - Â Â Â struct task_struct *kswapd;
> + Â Â Â wait_queue_head_t *kswapd_wait;

Personally, I prefer kswapd not kswapd_wait.
It's more readable and straightforward.

> Â Â Â Âint kswapd_max_order;
> Â Â Â Âenum zone_type classzone_idx;
> Â} pg_data_t;
> diff --git a/include/linux/swap.h b/include/linux/swap.h
> index ed6ebe6..f43d406 100644
> --- a/include/linux/swap.h
> +++ b/include/linux/swap.h
> @@ -26,6 +26,13 @@ static inline int current_is_kswapd(void)
> Â Â Â Âreturn current->flags & PF_KSWAPD;
> Â}
>
> +struct kswapd {
> + Â Â Â struct task_struct *kswapd_task;
> + Â Â Â wait_queue_head_t kswapd_wait;
> + Â Â Â pg_data_t *kswapd_pgdat;
> +};
> +
> +int kswapd(void *p);
> Â/*
> Â* MAX_SWAPFILES defines the maximum number of swaptypes: things which can
> Â* be swapped to. ÂThe swap type and the offset into that swap type are
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 6e1b52a..6340865 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -4205,7 +4205,6 @@ static void __paginginit free_area_init_core(struct pglist_data *pgdat,
>
> Â Â Â Âpgdat_resize_init(pgdat);
> Â Â Â Âpgdat->nr_zones = 0;
> - Â Â Â init_waitqueue_head(&pgdat->kswapd_wait);
> Â Â Â Âpgdat->kswapd_max_order = 0;
> Â Â Â Âpgdat_page_cgroup_init(pgdat);
>
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 060e4c1..61fb96e 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -2242,12 +2242,13 @@ static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages,
> Â}
>
> Â/* is kswapd sleeping prematurely? */
> -static bool sleeping_prematurely(pg_data_t *pgdat, int order, long remaining,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int classzone_idx)
> +static int sleeping_prematurely(struct kswapd *kswapd, int order,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â long remaining, int classzone_idx)
> Â{
> Â Â Â Âint i;
> Â Â Â Âunsigned long balanced = 0;
> Â Â Â Âbool all_zones_ok = true;
> + Â Â Â pg_data_t *pgdat = kswapd->kswapd_pgdat;
>
> Â Â Â Â/* If a direct reclaimer woke kswapd within HZ/10, it's premature */
> Â Â Â Âif (remaining)
> @@ -2570,28 +2571,31 @@ out:
> Â Â Â Âreturn order;
> Â}
>
> -static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
> +static void kswapd_try_to_sleep(struct kswapd *kswapd_p, int order,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int classzone_idx)
> Â{
> Â Â Â Âlong remaining = 0;
> Â Â Â ÂDEFINE_WAIT(wait);
> + Â Â Â pg_data_t *pgdat = kswapd_p->kswapd_pgdat;
> + Â Â Â wait_queue_head_t *wait_h = &kswapd_p->kswapd_wait;

kswapd_p? p means pointer?
wait_h? h means header?
Hmm.. Of course, it's trivial and we can understand easily in such
context but we don't have been used such words so it's rather awkward
to me.

How about kswapd instead of kswapd_p, kswapd_wait instead of wait_h?

>
> Â Â Â Âif (freezing(current) || kthread_should_stop())
> Â Â Â Â Â Â Â Âreturn;
>
> - Â Â Â prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
> + Â Â Â prepare_to_wait(wait_h, &wait, TASK_INTERRUPTIBLE);
>
> Â Â Â Â/* Try to sleep for a short interval */
> - Â Â Â if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) {
> + Â Â Â if (!sleeping_prematurely(kswapd_p, order, remaining, classzone_idx)) {
> Â Â Â Â Â Â Â Âremaining = schedule_timeout(HZ/10);
> - Â Â Â Â Â Â Â finish_wait(&pgdat->kswapd_wait, &wait);
> - Â Â Â Â Â Â Â prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
> + Â Â Â Â Â Â Â finish_wait(wait_h, &wait);
> + Â Â Â Â Â Â Â prepare_to_wait(wait_h, &wait, TASK_INTERRUPTIBLE);
> Â Â Â Â}
>
> Â Â Â Â/*
> Â Â Â Â * After a short sleep, check if it was a premature sleep. If not, then
> Â Â Â Â * go fully to sleep until explicitly woken up.
> Â Â Â Â */
> - Â Â Â if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) {
> + Â Â Â if (!sleeping_prematurely(kswapd_p, order, remaining, classzone_idx)) {
> Â Â Â Â Â Â Â Âtrace_mm_vmscan_kswapd_sleep(pgdat->node_id);
>
> Â Â Â Â Â Â Â Â/*
> @@ -2611,7 +2615,7 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
> Â Â Â Â Â Â Â Âelse
> Â Â Â Â Â Â Â Â Â Â Â Âcount_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
> Â Â Â Â}
> - Â Â Â finish_wait(&pgdat->kswapd_wait, &wait);
> + Â Â Â finish_wait(wait_h, &wait);
> Â}
>
> Â/*
> @@ -2627,20 +2631,24 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
> Â* If there are applications that are active memory-allocators
> Â* (most normal use), this basically shouldn't matter.
> Â*/
> -static int kswapd(void *p)
> +int kswapd(void *p)
> Â{
> Â Â Â Âunsigned long order;
> Â Â Â Âint classzone_idx;
> - Â Â Â pg_data_t *pgdat = (pg_data_t*)p;
> + Â Â Â struct kswapd *kswapd_p = (struct kswapd *)p;
> + Â Â Â pg_data_t *pgdat = kswapd_p->kswapd_pgdat;
> + Â Â Â wait_queue_head_t *wait_h = &kswapd_p->kswapd_wait;
> Â Â Â Âstruct task_struct *tsk = current;
>
> Â Â Â Âstruct reclaim_state reclaim_state = {
> Â Â Â Â Â Â Â Â.reclaimed_slab = 0,
> Â Â Â Â};
> - Â Â Â const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
> + Â Â Â const struct cpumask *cpumask;
>
> Â Â Â Âlockdep_set_current_reclaim_state(GFP_KERNEL);
>
> + Â Â Â BUG_ON(pgdat->kswapd_wait != wait_h);

If we include kswapd instead of kswapd_wait in pgdat, maybe we could
remove the check?

> + Â Â Â cpumask = cpumask_of_node(pgdat->node_id);
> Â Â Â Âif (!cpumask_empty(cpumask))
> Â Â Â Â Â Â Â Âset_cpus_allowed_ptr(tsk, cpumask);
> Â Â Â Âcurrent->reclaim_state = &reclaim_state;
> @@ -2679,7 +2687,7 @@ static int kswapd(void *p)
> Â Â Â Â Â Â Â Â Â Â Â Âorder = new_order;
> Â Â Â Â Â Â Â Â Â Â Â Âclasszone_idx = new_classzone_idx;
> Â Â Â Â Â Â Â Â} else {
> - Â Â Â Â Â Â Â Â Â Â Â kswapd_try_to_sleep(pgdat, order, classzone_idx);
> + Â Â Â Â Â Â Â Â Â Â Â kswapd_try_to_sleep(kswapd_p, order, classzone_idx);
> Â Â Â Â Â Â Â Â Â Â Â Âorder = pgdat->kswapd_max_order;
> Â Â Â Â Â Â Â Â Â Â Â Âclasszone_idx = pgdat->classzone_idx;
> Â Â Â Â Â Â Â Â Â Â Â Âpgdat->kswapd_max_order = 0;
> @@ -2719,13 +2727,13 @@ void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
> Â Â Â Â Â Â Â Âpgdat->kswapd_max_order = order;
> Â Â Â Â Â Â Â Âpgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
> Â Â Â Â}
> - Â Â Â if (!waitqueue_active(&pgdat->kswapd_wait))
> + Â Â Â if (!waitqueue_active(pgdat->kswapd_wait))
> Â Â Â Â Â Â Â Âreturn;
> Â Â Â Âif (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
> Â Â Â Â Â Â Â Âreturn;
>
> Â Â Â Âtrace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
> - Â Â Â wake_up_interruptible(&pgdat->kswapd_wait);
> + Â Â Â wake_up_interruptible(pgdat->kswapd_wait);
> Â}
>
> Â/*
> @@ -2817,12 +2825,21 @@ static int __devinit cpu_callback(struct notifier_block *nfb,
> Â Â Â Â Â Â Â Âfor_each_node_state(nid, N_HIGH_MEMORY) {
> Â Â Â Â Â Â Â Â Â Â Â Âpg_data_t *pgdat = NODE_DATA(nid);
> Â Â Â Â Â Â Â Â Â Â Â Âconst struct cpumask *mask;
> + Â Â Â Â Â Â Â Â Â Â Â struct kswapd *kswapd_p;
> + Â Â Â Â Â Â Â Â Â Â Â struct task_struct *kswapd_thr;
> + Â Â Â Â Â Â Â Â Â Â Â wait_queue_head_t *wait;
>
> Â Â Â Â Â Â Â Â Â Â Â Âmask = cpumask_of_node(pgdat->node_id);
>
> + Â Â Â Â Â Â Â Â Â Â Â wait = pgdat->kswapd_wait;
> + Â Â Â Â Â Â Â Â Â Â Â kswapd_p = container_of(wait, struct kswapd,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â kswapd_wait);
> + Â Â Â Â Â Â Â Â Â Â Â kswapd_thr = kswapd_p->kswapd_task;

kswapd_thr? thr means thread?
How about tsk?

> +
If we include kswapd instead of kswapd_wait in pgdat, don't we make this simple?

struct kswapd *kswapd = pgdat->kswapd;
struct task_struct *kswapd_tsk = kswapd->kswapd_task;


> Â Â Â Â Â Â Â Â Â Â Â Âif (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â/* One of our CPUs online: restore mask */
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â set_cpus_allowed_ptr(pgdat->kswapd, mask);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (kswapd_thr)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â set_cpus_allowed_ptr(kswapd_thr, mask);
> Â Â Â Â Â Â Â Â}
> Â Â Â Â}
> Â Â Â Âreturn NOTIFY_OK;
> @@ -2835,18 +2852,31 @@ static int __devinit cpu_callback(struct notifier_block *nfb,
> Âint kswapd_run(int nid)
> Â{
> Â Â Â Âpg_data_t *pgdat = NODE_DATA(nid);
> + Â Â Â struct task_struct *kswapd_thr;
> + Â Â Â struct kswapd *kswapd_p;
> Â Â Â Âint ret = 0;
>
> - Â Â Â if (pgdat->kswapd)
> + Â Â Â if (pgdat->kswapd_wait)
> Â Â Â Â Â Â Â Âreturn 0;
>
> - Â Â Â pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
> - Â Â Â if (IS_ERR(pgdat->kswapd)) {
> + Â Â Â kswapd_p = kzalloc(sizeof(struct kswapd), GFP_KERNEL);
> + Â Â Â if (!kswapd_p)
> + Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â init_waitqueue_head(&kswapd_p->kswapd_wait);
> + Â Â Â pgdat->kswapd_wait = &kswapd_p->kswapd_wait;
> + Â Â Â kswapd_p->kswapd_pgdat = pgdat;
> +
> + Â Â Â kswapd_thr = kthread_run(kswapd, kswapd_p, "kswapd%d", nid);
> + Â Â Â if (IS_ERR(kswapd_thr)) {
> Â Â Â Â Â Â Â Â/* failure at boot is fatal */
> Â Â Â Â Â Â Â ÂBUG_ON(system_state == SYSTEM_BOOTING);
> Â Â Â Â Â Â Â Âprintk("Failed to start kswapd on node %d\n",nid);
> + Â Â Â Â Â Â Â pgdat->kswapd_wait = NULL;
> + Â Â Â Â Â Â Â kfree(kswapd_p);
> Â Â Â Â Â Â Â Âret = -1;
> - Â Â Â }
> + Â Â Â } else
> + Â Â Â Â Â Â Â kswapd_p->kswapd_task = kswapd_thr;
> Â Â Â Âreturn ret;
> Â}
>
> @@ -2855,10 +2885,23 @@ int kswapd_run(int nid)
> Â*/
> Âvoid kswapd_stop(int nid)
> Â{
> - Â Â Â struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
> + Â Â Â struct task_struct *kswapd_thr = NULL;
> + Â Â Â struct kswapd *kswapd_p = NULL;
> + Â Â Â wait_queue_head_t *wait;
> +
> + Â Â Â pg_data_t *pgdat = NODE_DATA(nid);
> +
> + Â Â Â wait = pgdat->kswapd_wait;
> + Â Â Â if (wait) {
> + Â Â Â Â Â Â Â kswapd_p = container_of(wait, struct kswapd, kswapd_wait);
> + Â Â Â Â Â Â Â kswapd_thr = kswapd_p->kswapd_task;
> + Â Â Â Â Â Â Â kswapd_p->kswapd_task = NULL;
> + Â Â Â }
> +
> + Â Â Â if (kswapd_thr)
> + Â Â Â Â Â Â Â kthread_stop(kswapd_thr);
>
> - Â Â Â if (kswapd)
> - Â Â Â Â Â Â Â kthread_stop(kswapd);
> + Â Â Â kfree(kswapd_p);
> Â}
>
> Âstatic int __init kswapd_init(void)
> --
> 1.7.3.1
>
>

Hmm, I don't like kswapd_p, kswapd_thr, wait_h and kswapd_wait of pgdat.
But it's just my personal opinion. :)


-- 
Kind regards,
Minchan Kim

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


[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]