linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/2] mm: avoid unnecessary swap_info_struct allocation
@ 2018-11-04 22:13 Vasily Averin
  2018-11-05  0:57 ` Huang, Ying
  0 siblings, 1 reply; 4+ messages in thread
From: Vasily Averin @ 2018-11-04 22:13 UTC (permalink / raw)
  To: linux-mm, Andrew Morton; +Cc: Huang Ying, linux-kernel, Aaron Lu

Currently newly allocated swap_info_struct can be quickly freed.
This patch avoid uneccessary high-order page allocation and helps
to decrease the memory pressure.

Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
---
 mm/swapfile.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 8688ae65ef58..53ec2f0cdf26 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2809,14 +2809,17 @@ late_initcall(max_swapfiles_check);
 
 static struct swap_info_struct *alloc_swap_info(void)
 {
-	struct swap_info_struct *p;
+	struct swap_info_struct *p = NULL;
 	unsigned int type;
 	int i;
+	bool force_alloc = false;
 
-	p = kvzalloc(sizeof(*p), GFP_KERNEL);
-	if (!p)
-		return ERR_PTR(-ENOMEM);
-
+retry:
+	if (force_alloc) {
+		p = kvzalloc(sizeof(*p), GFP_KERNEL);
+		if (!p)
+			return ERR_PTR(-ENOMEM);
+	}
 	spin_lock(&swap_lock);
 	for (type = 0; type < nr_swapfiles; type++) {
 		if (!(swap_info[type]->flags & SWP_USED))
@@ -2828,6 +2831,11 @@ static struct swap_info_struct *alloc_swap_info(void)
 		return ERR_PTR(-EPERM);
 	}
 	if (type >= nr_swapfiles) {
+		if (!force_alloc) {
+			force_alloc = true;
+			spin_unlock(&swap_lock);
+			goto retry;
+		}
 		p->type = type;
 		swap_info[type] = p;
 		/*
-- 
2.17.1



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

* Re: [PATCH 2/2] mm: avoid unnecessary swap_info_struct allocation
  2018-11-04 22:13 [PATCH 2/2] mm: avoid unnecessary swap_info_struct allocation Vasily Averin
@ 2018-11-05  0:57 ` Huang, Ying
  2018-11-05  5:19   ` Vasily Averin
  0 siblings, 1 reply; 4+ messages in thread
From: Huang, Ying @ 2018-11-05  0:57 UTC (permalink / raw)
  To: Vasily Averin; +Cc: linux-mm, Andrew Morton, linux-kernel, Aaron Lu

Vasily Averin <vvs@virtuozzo.com> writes:

> Currently newly allocated swap_info_struct can be quickly freed.
> This patch avoid uneccessary high-order page allocation and helps
> to decrease the memory pressure.

I think swapon/swapoff are rare operations, so it will not increase the
memory pressure much.  

Best Regards,
Huang, Ying

> Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
> ---
>  mm/swapfile.c | 18 +++++++++++++-----
>  1 file changed, 13 insertions(+), 5 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 8688ae65ef58..53ec2f0cdf26 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -2809,14 +2809,17 @@ late_initcall(max_swapfiles_check);
>  
>  static struct swap_info_struct *alloc_swap_info(void)
>  {
> -	struct swap_info_struct *p;
> +	struct swap_info_struct *p = NULL;
>  	unsigned int type;
>  	int i;
> +	bool force_alloc = false;
>  
> -	p = kvzalloc(sizeof(*p), GFP_KERNEL);
> -	if (!p)
> -		return ERR_PTR(-ENOMEM);
> -
> +retry:
> +	if (force_alloc) {
> +		p = kvzalloc(sizeof(*p), GFP_KERNEL);
> +		if (!p)
> +			return ERR_PTR(-ENOMEM);
> +	}
>  	spin_lock(&swap_lock);
>  	for (type = 0; type < nr_swapfiles; type++) {
>  		if (!(swap_info[type]->flags & SWP_USED))
> @@ -2828,6 +2831,11 @@ static struct swap_info_struct *alloc_swap_info(void)
>  		return ERR_PTR(-EPERM);
>  	}
>  	if (type >= nr_swapfiles) {
> +		if (!force_alloc) {
> +			force_alloc = true;
> +			spin_unlock(&swap_lock);
> +			goto retry;
> +		}
>  		p->type = type;
>  		swap_info[type] = p;
>  		/*

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

* Re: [PATCH 2/2] mm: avoid unnecessary swap_info_struct allocation
  2018-11-05  0:57 ` Huang, Ying
@ 2018-11-05  5:19   ` Vasily Averin
  2018-11-05 10:41     ` Vasily Averin
  0 siblings, 1 reply; 4+ messages in thread
From: Vasily Averin @ 2018-11-05  5:19 UTC (permalink / raw)
  To: Huang, Ying; +Cc: linux-mm, Andrew Morton, linux-kernel, Aaron Lu

On 11/5/18 3:57 AM, Huang, Ying wrote:
> Vasily Averin <vvs@virtuozzo.com> writes:
> 
>> Currently newly allocated swap_info_struct can be quickly freed.
>> This patch avoid uneccessary high-order page allocation and helps
>> to decrease the memory pressure.
> 
> I think swapon/swapoff are rare operations, so it will not increase the
> memory pressure much.

You are right, typically it should not affect usual nodes.

It's OpenVz-specific usecase.

OpenVz allows hosters to run hundreds of non-trusted containers per node.
Our containers have enabled "virtual swap" functionality, 
and container's owners can call sys_swapon without any limits.
Containers can be restarted in any time and we would like to 
decrease number of unnecessary high-order memory allocations.

>> Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
>> ---
>>  mm/swapfile.c | 18 +++++++++++++-----
>>  1 file changed, 13 insertions(+), 5 deletions(-)
>>
>> diff --git a/mm/swapfile.c b/mm/swapfile.c
>> index 8688ae65ef58..53ec2f0cdf26 100644
>> --- a/mm/swapfile.c
>> +++ b/mm/swapfile.c
>> @@ -2809,14 +2809,17 @@ late_initcall(max_swapfiles_check);
>>  
>>  static struct swap_info_struct *alloc_swap_info(void)
>>  {
>> -	struct swap_info_struct *p;
>> +	struct swap_info_struct *p = NULL;
>>  	unsigned int type;
>>  	int i;
>> +	bool force_alloc = false;
>>  
>> -	p = kvzalloc(sizeof(*p), GFP_KERNEL);
>> -	if (!p)
>> -		return ERR_PTR(-ENOMEM);
>> -
>> +retry:
>> +	if (force_alloc) {
>> +		p = kvzalloc(sizeof(*p), GFP_KERNEL);
>> +		if (!p)
>> +			return ERR_PTR(-ENOMEM);
>> +	}
>>  	spin_lock(&swap_lock);
>>  	for (type = 0; type < nr_swapfiles; type++) {
>>  		if (!(swap_info[type]->flags & SWP_USED))
>> @@ -2828,6 +2831,11 @@ static struct swap_info_struct *alloc_swap_info(void)
>>  		return ERR_PTR(-EPERM);
>>  	}
>>  	if (type >= nr_swapfiles) {
>> +		if (!force_alloc) {
>> +			force_alloc = true;
>> +			spin_unlock(&swap_lock);
>> +			goto retry;
>> +		}
>>  		p->type = type;
>>  		swap_info[type] = p;
>>  		/*
> 

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

* Re: [PATCH 2/2] mm: avoid unnecessary swap_info_struct allocation
  2018-11-05  5:19   ` Vasily Averin
@ 2018-11-05 10:41     ` Vasily Averin
  0 siblings, 0 replies; 4+ messages in thread
From: Vasily Averin @ 2018-11-05 10:41 UTC (permalink / raw)
  To: Huang, Ying; +Cc: linux-mm, Andrew Morton, linux-kernel, Aaron Lu

I was wrong, openVz blocks sys_swapon/swapoff syscalls inside containers.
Our kernel just emulates /proc/swaps output inside containers,
it is enough for 'swapon' userspace to do not fail and show required info.

So I do not have any special arguments for proposed patch.

On 11/5/18 8:19 AM, Vasily Averin wrote:
> On 11/5/18 3:57 AM, Huang, Ying wrote:
>> Vasily Averin <vvs@virtuozzo.com> writes:
>>
>>> Currently newly allocated swap_info_struct can be quickly freed.
>>> This patch avoid uneccessary high-order page allocation and helps
>>> to decrease the memory pressure.
>>
>> I think swapon/swapoff are rare operations, so it will not increase the
>> memory pressure much.
> 
> You are right, typically it should not affect usual nodes.
> 
> It's OpenVz-specific usecase.
> 
> OpenVz allows hosters to run hundreds of non-trusted containers per node.
> Our containers have enabled "virtual swap" functionality, 
> and container's owners can call sys_swapon without any limits.
> Containers can be restarted in any time and we would like to 
> decrease number of unnecessary high-order memory allocations.
> 
>>> Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
>>> ---
>>>  mm/swapfile.c | 18 +++++++++++++-----
>>>  1 file changed, 13 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/mm/swapfile.c b/mm/swapfile.c
>>> index 8688ae65ef58..53ec2f0cdf26 100644
>>> --- a/mm/swapfile.c
>>> +++ b/mm/swapfile.c
>>> @@ -2809,14 +2809,17 @@ late_initcall(max_swapfiles_check);
>>>  
>>>  static struct swap_info_struct *alloc_swap_info(void)
>>>  {
>>> -	struct swap_info_struct *p;
>>> +	struct swap_info_struct *p = NULL;
>>>  	unsigned int type;
>>>  	int i;
>>> +	bool force_alloc = false;
>>>  
>>> -	p = kvzalloc(sizeof(*p), GFP_KERNEL);
>>> -	if (!p)
>>> -		return ERR_PTR(-ENOMEM);
>>> -
>>> +retry:
>>> +	if (force_alloc) {
>>> +		p = kvzalloc(sizeof(*p), GFP_KERNEL);
>>> +		if (!p)
>>> +			return ERR_PTR(-ENOMEM);
>>> +	}
>>>  	spin_lock(&swap_lock);
>>>  	for (type = 0; type < nr_swapfiles; type++) {
>>>  		if (!(swap_info[type]->flags & SWP_USED))
>>> @@ -2828,6 +2831,11 @@ static struct swap_info_struct *alloc_swap_info(void)
>>>  		return ERR_PTR(-EPERM);
>>>  	}
>>>  	if (type >= nr_swapfiles) {
>>> +		if (!force_alloc) {
>>> +			force_alloc = true;
>>> +			spin_unlock(&swap_lock);
>>> +			goto retry;
>>> +		}
>>>  		p->type = type;
>>>  		swap_info[type] = p;
>>>  		/*
>>

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

end of thread, other threads:[~2018-11-05 10:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-04 22:13 [PATCH 2/2] mm: avoid unnecessary swap_info_struct allocation Vasily Averin
2018-11-05  0:57 ` Huang, Ying
2018-11-05  5:19   ` Vasily Averin
2018-11-05 10:41     ` Vasily Averin

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