Skip to contentSkip to conversation

mm: use kvzalloc for swap_info_struct allocation

PATCH Superseded Vasily Averin 4 participants 8 messages Nov 4, 2018 – Nov 5, 2018

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

PATCH 1/2 mm: use kvzalloc for swap_info_struct allocation

commit a2468cc9bfdf ("swap: choose swap device according to numa node") increased size of swap_info_struct up to 44 Kbytes, now it requires 4th order page. Switch to kvzmalloc allows to avoid unexpected allocation failures.

---
 mm/swapfile.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/swapfile.c +3 -3
index 644f746e167a..8688ae65ef58 100644
@@ -2813,7 +2813,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	unsigned int type;
 	int i;
 
-	p = kzalloc(sizeof(*p), GFP_KERNEL);
+	p = kvzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
@@ -2824,7 +2824,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	}
 	if (type >= MAX_SWAPFILES) {
 		spin_unlock(&swap_lock);
-		kfree(p);
+		kvfree(p);
 		return ERR_PTR(-EPERM);
 	}
 	if (type >= nr_swapfiles) {
@@ -2838,7 +2838,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 		smp_wmb();
 		nr_swapfiles++;
 	} else {
-		kfree(p);
+		kvfree(p);
 		p = swap_info[type];
 		/*
 		 * Do not memset this entry: a racing procfs swap_next()
Signature · 1 line
2.17.1
#1 lore raw
Quoted 3 lines · Vasily Averin

commit a2468cc9bfdf ("swap: choose swap device according to numa node") increased size of swap_info_struct up to 44 Kbytes, now it requires 4th order page.

Why swap_info_struct could be so large? Because MAX_NUMNODES could be thousands so that 'avail_lists' field could be tens KB? If so, I think it's fair to use kvzalloc(). Can you add one line comment? Because struct swap_info_struct is quite small in default configuration.

Best Regards,
Huang, Ying

Quoted 38 lines

Switch to kvzmalloc allows to avoid unexpected allocation failures.

---
 mm/swapfile.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/swapfile.c +3 -3
index 644f746e167a..8688ae65ef58 100644
@@ -2813,7 +2813,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	unsigned int type;
 	int i;
 
-	p = kzalloc(sizeof(*p), GFP_KERNEL);
+	p = kvzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
@@ -2824,7 +2824,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	}
 	if (type >= MAX_SWAPFILES) {
 		spin_unlock(&swap_lock);
-		kfree(p);
+		kvfree(p);
 		return ERR_PTR(-EPERM);
 	}
 	if (type >= nr_swapfiles) {
@@ -2838,7 +2838,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 		smp_wmb();
 		nr_swapfiles++;
 	} else {
-		kfree(p);
+		kvfree(p);
 		p = swap_info[type];
 		/*
 		 * Do not memset this entry: a racing procfs swap_next()
#2 lore raw
Quoted 10 lines · Ying
Quoted 3 lines · Vasily Averin

commit a2468cc9bfdf ("swap: choose swap device according to numa node") increased size of swap_info_struct up to 44 Kbytes, now it requires 4th order page.

Why swap_info_struct could be so large? Because MAX_NUMNODES could be thousands so that 'avail_lists' field could be tens KB? If so, I think it's fair to use kvzalloc(). Can you add one line comment? Because struct swap_info_struct is quite small in default configuration.

I was incorrect not 44Kb but 40kb should be here.
We have found CONFIG_NODES_SHIFT=10 in new RHEL7 update 6 kernel, default ubuntu kernels have the same setting too.

crash> struct swap_info_struct -o

struct swap_info_struct {
      [0] unsigned long flags;
      [8] short prio;
           ...
    [140] spinlock_t lock;
    [144] struct plist_node list;
    [184] struct plist_node avail_lists[1024]; <<<< here
  [41144] struct swap_cluster_info *cluster_info;
  [41152] struct swap_cluster_list free_clusters;
          ...

[41224] spinlock_t cont_lock;
}
SIZE: 41232

struct swap_info_struct {
        ...
        RH_KABI_EXTEND(struct plist_node avail_lists[MAX_NUMNODES]) /* entry in swap_avail_head */
        ...
}

#define MAX_NUMNODES (1 << NODES_SHIFT)

#ifdef CONFIG_NODES_SHIFT
#define NODES_SHIFT CONFIG_NODES_SHIFT
#else
#define NODES_SHIFT 0
#endif

/boot/config-4.15.0-38-generic:CONFIG_NODES_SHIFT=10

#3 lore raw
Quoted 39 lines · Vasily Averin
Quoted 10 lines · Ying
Quoted 3 lines · Vasily Averin

commit a2468cc9bfdf ("swap: choose swap device according to numa node") increased size of swap_info_struct up to 44 Kbytes, now it requires 4th order page.

Why swap_info_struct could be so large? Because MAX_NUMNODES could be thousands so that 'avail_lists' field could be tens KB? If so, I think it's fair to use kvzalloc(). Can you add one line comment? Because struct swap_info_struct is quite small in default configuration.

I was incorrect not 44Kb but 40kb should be here.
We have found CONFIG_NODES_SHIFT=10 in new RHEL7 update 6 kernel, default ubuntu kernels have the same setting too.

crash> struct swap_info_struct -o

struct swap_info_struct {
      [0] unsigned long flags;
      [8] short prio;
           ...
    [140] spinlock_t lock;
    [144] struct plist_node list;
    [184] struct plist_node avail_lists[1024]; <<<< here
  [41144] struct swap_cluster_info *cluster_info;
  [41152] struct swap_cluster_list free_clusters;
          ...

[41224] spinlock_t cont_lock;
}
SIZE: 41232

struct swap_info_struct {
        ...
        RH_KABI_EXTEND(struct plist_node avail_lists[MAX_NUMNODES]) /* entry in swap_avail_head */
        ...
}

#define MAX_NUMNODES (1 << NODES_SHIFT)

Quoted 7 lines

#ifdef CONFIG_NODES_SHIFT
#define NODES_SHIFT CONFIG_NODES_SHIFT
#else
#define NODES_SHIFT 0
#endif

/boot/config-4.15.0-38-generic:CONFIG_NODES_SHIFT=10

I see. So this is a more practical issue than my original imagination.

But for default config, I mean

$ make defconfig

And it turns out,

CONFIG_NODES_SHIFT=6

Best Regards,
Huang, Ying

Acked-by
#4 lore raw
In reply to Vasily Averin
Quoted 18 lines · Vasily Averin
Quoted 10 lines · Ying
Quoted 3 lines · Vasily Averin

commit a2468cc9bfdf ("swap: choose swap device according to numa node") increased size of swap_info_struct up to 44 Kbytes, now it requires 4th order page.

Why swap_info_struct could be so large? Because MAX_NUMNODES could be thousands so that 'avail_lists' field could be tens KB? If so, I think it's fair to use kvzalloc(). Can you add one line comment? Because struct swap_info_struct is quite small in default configuration.

I was incorrect not 44Kb but 40kb should be here.
We have found CONFIG_NODES_SHIFT=10 in new RHEL7 update 6 kernel, default ubuntu kernels have the same setting too.

Quoted 8 lines

crash> struct swap_info_struct -o

struct swap_info_struct {
      [0] unsigned long flags;
      [8] short prio;
           ...
    [140] spinlock_t lock;
    [144] struct plist_node list;
    [184] struct plist_node avail_lists[1024]; <<<< here

So every 'struct plist_node' takes 40 bytes and 1024 of them take a total of 40960 bytes, which is 10 pages and need an order-4 page to host them. It looks a little too much, especially consider most of the space will left be unused since most systems have nodes <= 4. I didn't realize this problem when developing this patch, thanks for pointing this out.

I think using kvzalloc() as is done by your patch is better here as it can avoid possible failure of swapon.

BTW, for systems with few swap devices this may not be a big deal, but according to your description, your workload will create a lot of swap devices and each of them will likely cause an order-4 unmovable pages allocated(when kvzalloc() doesn't fallback). I was thinking maybe we should convert avail_lists to a pointer in swap_info_struct and use vzalloc() for it.

Thanks,
Aaron

Quoted 23 lines

[41144] struct swap_cluster_info *cluster_info;

  [41152] struct swap_cluster_list free_clusters;
          ...

[41224] spinlock_t cont_lock;
}
SIZE: 41232

struct swap_info_struct {
        ...
        RH_KABI_EXTEND(struct plist_node avail_lists[MAX_NUMNODES]) /* entry in swap_avail_head */
        ...
}

#define MAX_NUMNODES (1 << NODES_SHIFT)

#ifdef CONFIG_NODES_SHIFT
#define NODES_SHIFT CONFIG_NODES_SHIFT
#else
#define NODES_SHIFT 0
#endif

/boot/config-4.15.0-38-generic:CONFIG_NODES_SHIFT=10

#5 lore raw

PATCH v2 1/1 mm: use kvzalloc for swap_info_struct allocation

commit a2468cc9bfdf ("swap: choose swap device according to numa node") changed 'avail_lists' field of 'struct swap_info_struct' to an array. In popular linux distros it increased size of swap_info_struct up to 40 Kbytes and now swap_info_struct allocation requires order-4 page. Switch to kvzmalloc allows to avoid unexpected allocation failures.

---
 mm/swapfile.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/swapfile.c +3 -3
index 644f746e167a..8688ae65ef58 100644
@@ -2813,7 +2813,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	unsigned int type;
 	int i;
 
-	p = kzalloc(sizeof(*p), GFP_KERNEL);
+	p = kvzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
@@ -2824,7 +2824,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	}
 	if (type >= MAX_SWAPFILES) {
 		spin_unlock(&swap_lock);
-		kfree(p);
+		kvfree(p);
 		return ERR_PTR(-EPERM);
 	}
 	if (type >= nr_swapfiles) {
@@ -2838,7 +2838,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 		smp_wmb();
 		nr_swapfiles++;
 	} else {
-		kfree(p);
+		kvfree(p);
 		p = swap_info[type];
 		/*
 		 * Do not memset this entry: a racing procfs swap_next()
Signature · 1 line
2.17.1
Acked-by on v2 1/1
#6 lore raw
Quoted 5 lines · Vasily Averin

commit a2468cc9bfdf ("swap: choose swap device according to numa node") changed 'avail_lists' field of 'struct swap_info_struct' to an array. In popular linux distros it increased size of swap_info_struct up to 40 Kbytes and now swap_info_struct allocation requires order-4 page. Switch to kvzmalloc allows to avoid unexpected allocation failures.

While this fixes the most visible issue is this a good long term solution? Aren't we wasting memory without a good reason? IIRC our limit for swap files/devices is much smaller than potential NUMA nodes numbers so we can safely expect that would be only few numa affine nodes. I am not really familiar with the rework which has added numa node awareness but I wouls assueme that we should either go with one global table with a linked list of possible swap_info structure per numa node or use a sparse array.

That being said I am not really objecting to this patch as it is simple and backportable to older (stable kernels).

I would even dare to add

Fixes a2468cc9bfdf ("swap: choose swap device according to numa node")

because not being able to add a swap space on a fragmented system looks like a regression to me.

Quoted 2 lines
Quoted 37 lines
---
 mm/swapfile.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/swapfile.c +3 -3
index 644f746e167a..8688ae65ef58 100644
@@ -2813,7 +2813,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	unsigned int type;
 	int i;
 
-	p = kzalloc(sizeof(*p), GFP_KERNEL);
+	p = kvzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
@@ -2824,7 +2824,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	}
 	if (type >= MAX_SWAPFILES) {
 		spin_unlock(&swap_lock);
-		kfree(p);
+		kvfree(p);
 		return ERR_PTR(-EPERM);
 	}
 	if (type >= nr_swapfiles) {
@@ -2838,7 +2838,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 		smp_wmb();
 		nr_swapfiles++;
 	} else {
-		kfree(p);
+		kvfree(p);
 		p = swap_info[type];
 		/*
 		 * Do not memset this entry: a racing procfs swap_next()
Signature · 1 line
2.17.1
Signature · 2 lines
Michal Hocko
SUSE Labs
#7 lore raw
Quoted 9 lines · Michal Hocko
Quoted 5 lines · Vasily Averin

commit a2468cc9bfdf ("swap: choose swap device according to numa node") changed 'avail_lists' field of 'struct swap_info_struct' to an array. In popular linux distros it increased size of swap_info_struct up to 40 Kbytes and now swap_info_struct allocation requires order-4 page. Switch to kvzmalloc allows to avoid unexpected allocation failures.

While this fixes the most visible issue is this a good long term solution? Aren't we wasting memory without a good reason? IIRC our limit

That's right, we need a better way of handling this in the long term.

Quoted 6 lines

for swap files/devices is much smaller than potential NUMA nodes numbers so we can safely expect that would be only few numa affine nodes. I am not really familiar with the rework which has added numa node awareness but I wouls assueme that we should either go with one global table with a linked list of possible swap_info structure per numa node or use a sparse array.

There is a per-numa-node plist of available swap devices, so every swap device needs an entry on those per-numa-node plist.

I think we can convert avail_lists from array to pointer and use vzalloc to allocate the needed memory. MAX_NUMANODES can be used for a simple implementation, or use the precise online node number but then we will need to handle node online/offline events.

sparse array sounds promising, I'll take a look, thanks for the pointer.

Quoted 8 lines

That being said I am not really objecting to this patch as it is simple and backportable to older (stable kernels).

I would even dare to add

Fixes a2468cc9bfdf ("swap: choose swap device according to numa node")

because not being able to add a swap space on a fragmented system looks like a regression to me.

Agree, especially it used to work.

Regards,
Aaron

Quoted 46 lines
Quoted 2 lines
Quoted 37 lines
---
 mm/swapfile.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/swapfile.c +3 -3
index 644f746e167a..8688ae65ef58 100644
@@ -2813,7 +2813,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	unsigned int type;
 	int i;
 
-	p = kzalloc(sizeof(*p), GFP_KERNEL);
+	p = kvzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
@@ -2824,7 +2824,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 	}
 	if (type >= MAX_SWAPFILES) {
 		spin_unlock(&swap_lock);
-		kfree(p);
+		kvfree(p);
 		return ERR_PTR(-EPERM);
 	}
 	if (type >= nr_swapfiles) {
@@ -2838,7 +2838,7 @@ static struct swap_info_struct *alloc_swap_info(void)
 		smp_wmb();
 		nr_swapfiles++;
 	} else {
-		kfree(p);
+		kvfree(p);
 		p = swap_info[type];
 		/*
 		 * Do not memset this entry: a racing procfs swap_next()
Signature · 1 line
2.17.1
Signature · 2 lines
Michal Hocko
SUSE Labs