linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/swap: use nr_node_ids for avail_lists in swap_info_struct
@ 2018-11-15  8:38 Aaron Lu
  2018-11-16  8:45 ` Michal Hocko
  0 siblings, 1 reply; 2+ messages in thread
From: Aaron Lu @ 2018-11-15  8:38 UTC (permalink / raw)
  To: linux-mm, lkml; +Cc: Vasily Averin, Michal Hocko, Huang Ying, Andrew Morton

Since commit a2468cc9bfdf ("swap: choose swap device according to
numa node"), avail_lists field of swap_info_struct is changed to
an array with MAX_NUMNODES elements. This made swap_info_struct
size increased to 40KiB and needs an order-4 page to hold it.

This is not optimal in that:
1 Most systems have way less than MAX_NUMNODES(1024) nodes so it
  is a waste of memory;
2 It could cause swapon failure if the swap device is swapped on
  after system has been running for a while, due to no order-4
  page is available as pointed out by Vasily Averin.

Solve the above two issues by using nr_node_ids(which is the actual
possible node number the running system has) for avail_lists instead
of MAX_NUMNODES.

nr_node_ids is unknown at compile time so can't be directly used
when declaring this array. What I did here is to declare avail_lists
as zero element array and allocate space for it when allocating
space for swap_info_struct. The reason why keep using array but
not pointer is plist_for_each_entry needs the field to be part
of the struct, so pointer will not work.

This patch is on top of Vasily Averin's fix commit. I think the
use of kvzalloc for swap_info_struct is still needed in case
nr_node_ids is really big on some systems.

Cc: Vasily Averin <vvs@virtuozzo.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Huang Ying <ying.huang@intel.com>
Signed-off-by: Aaron Lu <aaron.lu@intel.com>
---
 include/linux/swap.h | 11 ++++++++++-
 mm/swapfile.c        |  3 ++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/include/linux/swap.h b/include/linux/swap.h
index d8a07a4f171d..3d3630b3f63d 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -233,7 +233,6 @@ struct swap_info_struct {
 	unsigned long	flags;		/* SWP_USED etc: see above */
 	signed short	prio;		/* swap priority of this type */
 	struct plist_node list;		/* entry in swap_active_head */
-	struct plist_node avail_lists[MAX_NUMNODES];/* entry in swap_avail_heads */
 	signed char	type;		/* strange name for an index */
 	unsigned int	max;		/* extent of the swap_map */
 	unsigned char *swap_map;	/* vmalloc'ed array of usage counts */
@@ -274,6 +273,16 @@ struct swap_info_struct {
 					 */
 	struct work_struct discard_work; /* discard worker */
 	struct swap_cluster_list discard_clusters; /* discard clusters list */
+	struct plist_node avail_lists[0]; /*
+					   * entries in swap_avail_heads, one
+					   * entry per node.
+					   * Must be last as the number of the
+					   * array is nr_node_ids, which is not
+					   * a fixed value so have to allocate
+					   * dynamically.
+					   * And it has to be an array so that
+					   * plist_for_each_* can work.
+					   */
 };
 
 #ifdef CONFIG_64BIT
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 8688ae65ef58..6e06821623f6 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2812,8 +2812,9 @@ static struct swap_info_struct *alloc_swap_info(void)
 	struct swap_info_struct *p;
 	unsigned int type;
 	int i;
+	int size = sizeof(*p) + nr_node_ids * sizeof(struct plist_node);
 
-	p = kvzalloc(sizeof(*p), GFP_KERNEL);
+	p = kvzalloc(size, GFP_KERNEL);
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
-- 
2.17.2


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

* Re: [PATCH] mm/swap: use nr_node_ids for avail_lists in swap_info_struct
  2018-11-15  8:38 [PATCH] mm/swap: use nr_node_ids for avail_lists in swap_info_struct Aaron Lu
@ 2018-11-16  8:45 ` Michal Hocko
  0 siblings, 0 replies; 2+ messages in thread
From: Michal Hocko @ 2018-11-16  8:45 UTC (permalink / raw)
  To: Aaron Lu; +Cc: linux-mm, lkml, Vasily Averin, Huang Ying, Andrew Morton

On Thu 15-11-18 16:38:47, Aaron Lu wrote:
> Since commit a2468cc9bfdf ("swap: choose swap device according to
> numa node"), avail_lists field of swap_info_struct is changed to
> an array with MAX_NUMNODES elements. This made swap_info_struct
> size increased to 40KiB and needs an order-4 page to hold it.
> 
> This is not optimal in that:
> 1 Most systems have way less than MAX_NUMNODES(1024) nodes so it
>   is a waste of memory;
> 2 It could cause swapon failure if the swap device is swapped on
>   after system has been running for a while, due to no order-4
>   page is available as pointed out by Vasily Averin.
> 
> Solve the above two issues by using nr_node_ids(which is the actual
> possible node number the running system has) for avail_lists instead
> of MAX_NUMNODES.
> 
> nr_node_ids is unknown at compile time so can't be directly used
> when declaring this array. What I did here is to declare avail_lists
> as zero element array and allocate space for it when allocating
> space for swap_info_struct. The reason why keep using array but
> not pointer is plist_for_each_entry needs the field to be part
> of the struct, so pointer will not work.
> 
> This patch is on top of Vasily Averin's fix commit. I think the
> use of kvzalloc for swap_info_struct is still needed in case
> nr_node_ids is really big on some systems.

I haven't seen any system with a huge nr_node_ids but using kvzalloc
is not harmfull.

> Cc: Vasily Averin <vvs@virtuozzo.com>
> Cc: Michal Hocko <mhocko@suse.com>
> Cc: Huang Ying <ying.huang@intel.com>
> Signed-off-by: Aaron Lu <aaron.lu@intel.com>

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

> ---
>  include/linux/swap.h | 11 ++++++++++-
>  mm/swapfile.c        |  3 ++-
>  2 files changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/swap.h b/include/linux/swap.h
> index d8a07a4f171d..3d3630b3f63d 100644
> --- a/include/linux/swap.h
> +++ b/include/linux/swap.h
> @@ -233,7 +233,6 @@ struct swap_info_struct {
>  	unsigned long	flags;		/* SWP_USED etc: see above */
>  	signed short	prio;		/* swap priority of this type */
>  	struct plist_node list;		/* entry in swap_active_head */
> -	struct plist_node avail_lists[MAX_NUMNODES];/* entry in swap_avail_heads */
>  	signed char	type;		/* strange name for an index */
>  	unsigned int	max;		/* extent of the swap_map */
>  	unsigned char *swap_map;	/* vmalloc'ed array of usage counts */
> @@ -274,6 +273,16 @@ struct swap_info_struct {
>  					 */
>  	struct work_struct discard_work; /* discard worker */
>  	struct swap_cluster_list discard_clusters; /* discard clusters list */
> +	struct plist_node avail_lists[0]; /*
> +					   * entries in swap_avail_heads, one
> +					   * entry per node.
> +					   * Must be last as the number of the
> +					   * array is nr_node_ids, which is not
> +					   * a fixed value so have to allocate
> +					   * dynamically.
> +					   * And it has to be an array so that
> +					   * plist_for_each_* can work.
> +					   */
>  };
>  
>  #ifdef CONFIG_64BIT
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 8688ae65ef58..6e06821623f6 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -2812,8 +2812,9 @@ static struct swap_info_struct *alloc_swap_info(void)
>  	struct swap_info_struct *p;
>  	unsigned int type;
>  	int i;
> +	int size = sizeof(*p) + nr_node_ids * sizeof(struct plist_node);
>  
> -	p = kvzalloc(sizeof(*p), GFP_KERNEL);
> +	p = kvzalloc(size, GFP_KERNEL);
>  	if (!p)
>  		return ERR_PTR(-ENOMEM);
>  
> -- 
> 2.17.2
> 

-- 
Michal Hocko
SUSE Labs

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

end of thread, other threads:[~2018-11-16  8:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-15  8:38 [PATCH] mm/swap: use nr_node_ids for avail_lists in swap_info_struct Aaron Lu
2018-11-16  8:45 ` Michal Hocko

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