diff mbox series

[RFC,1/2] mm: swap: check if swap backing device is congested or not

Message ID 1545115948-25467-1-git-send-email-yang.shi@linux.alibaba.com (mailing list archive)
State New, archived
Headers show
Series [RFC,1/2] mm: swap: check if swap backing device is congested or not | expand

Commit Message

Yang Shi Dec. 18, 2018, 6:52 a.m. UTC
Swap readahead would read in a few pages regardless if the underlying
device is busy or not.  It may incur long waiting time if the device is
congested, and it may also exacerbate the congestion.

Use inode_read_congested() to check if the underlying device is busy or
not like what file page readahead does.  Get inode from swap_info_struct.
Although we can add inode information in swap_address_space
(address_space->host), it may lead some unexpected side effect, i.e.
it may break mapping_cap_account_dirty().  Using inode from
swap_info_struct seems simple and good enough.

Just does the check in vma_cluster_readahead() since
swap_vma_readahead() is just used for non-rotational device which
much less likely has congestion than traditional HDD.

Although swap slots may be consecutive on swap partition, it still may be
fragmented on swap file. This check would help to reduce excessive stall
for such case.

Cc: Huang Ying <ying.huang@intel.com>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Minchan Kim <minchan@kernel.org>
Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com>
---
 mm/swap_state.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Tim Chen Dec. 18, 2018, 7:29 p.m. UTC | #1
On 12/17/18 10:52 PM, Yang Shi wrote:

> 
> diff --git a/mm/swap_state.c b/mm/swap_state.c
> index fd2f21e..7cc3c29 100644
> --- a/mm/swap_state.c
> +++ b/mm/swap_state.c
> @@ -538,11 +538,15 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>  	bool do_poll = true, page_allocated;
>  	struct vm_area_struct *vma = vmf->vma;
>  	unsigned long addr = vmf->address;
> +	struct inode *inode = si->swap_file->f_mapping->host;
>  
>  	mask = swapin_nr_pages(offset) - 1;
>  	if (!mask)
>  		goto skip;
>  

Shmem will also be using this function and I don't think the inode_read_congested
logic is relevant for that case.

So probably change the check to

	if (swp_type(entry) < nr_swapfiles &&
	    inode_read_congested(si->swap_file->f_mapping->host))
		goto skip;
		
> +	if (inode_read_congested(inode))
> +		goto skip;
> +
>  	do_poll = false;
>  	/* Read a page_cluster sized and aligned cluster around offset. */
>  	start_offset = offset & ~mask;
> 

Thanks.

Tim
Yang Shi Dec. 18, 2018, 11:43 p.m. UTC | #2
On 12/18/18 11:29 AM, Tim Chen wrote:
> On 12/17/18 10:52 PM, Yang Shi wrote:
>
>> diff --git a/mm/swap_state.c b/mm/swap_state.c
>> index fd2f21e..7cc3c29 100644
>> --- a/mm/swap_state.c
>> +++ b/mm/swap_state.c
>> @@ -538,11 +538,15 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>>   	bool do_poll = true, page_allocated;
>>   	struct vm_area_struct *vma = vmf->vma;
>>   	unsigned long addr = vmf->address;
>> +	struct inode *inode = si->swap_file->f_mapping->host;
>>   
>>   	mask = swapin_nr_pages(offset) - 1;
>>   	if (!mask)
>>   		goto skip;
>>   
> Shmem will also be using this function and I don't think the inode_read_congested
> logic is relevant for that case.

IMHO, shmem is also relevant. As long as it is trying to readahead from 
swap, it should check if the underlying device is busy or not regardless 
of shmem or anon page.

Just like mem_cgroup_try_charge_delay(), which throttles swap rate for 
both swap page fault and shmem.

Thanks,
Yang

>
> So probably change the check to
>
> 	if (swp_type(entry) < nr_swapfiles &&
> 	    inode_read_congested(si->swap_file->f_mapping->host))
> 		goto skip;
> 		
>> +	if (inode_read_congested(inode))
>> +		goto skip;
>> +
>>   	do_poll = false;
>>   	/* Read a page_cluster sized and aligned cluster around offset. */
>>   	start_offset = offset & ~mask;
>>
> Thanks.
>
> Tim
Tim Chen Dec. 19, 2018, 12:16 a.m. UTC | #3
On 12/18/18 3:43 PM, Yang Shi wrote:
> 
> 
> On 12/18/18 11:29 AM, Tim Chen wrote:
>> On 12/17/18 10:52 PM, Yang Shi wrote:
>>
>>> diff --git a/mm/swap_state.c b/mm/swap_state.c
>>> index fd2f21e..7cc3c29 100644
>>> --- a/mm/swap_state.c
>>> +++ b/mm/swap_state.c
>>> @@ -538,11 +538,15 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>>>       bool do_poll = true, page_allocated;
>>>       struct vm_area_struct *vma = vmf->vma;
>>>       unsigned long addr = vmf->address;
>>> +    struct inode *inode = si->swap_file->f_mapping->host;
>>>         mask = swapin_nr_pages(offset) - 1;
>>>       if (!mask)
>>>           goto skip;
>>>   
>> Shmem will also be using this function and I don't think the inode_read_congested
>> logic is relevant for that case.
> 
> IMHO, shmem is also relevant. As long as it is trying to readahead from swap, it should check if the underlying device is busy or not regardless of shmem or anon page.
> 

I don't think your dereference inode = si->swap_file->f_mapping->host
is always safe.  You should do it only when (si->flags & SWP_FS) is true.

Tim
Yang Shi Dec. 19, 2018, 5:56 a.m. UTC | #4
On 12/18/18 4:16 PM, Tim Chen wrote:
> On 12/18/18 3:43 PM, Yang Shi wrote:
>>
>> On 12/18/18 11:29 AM, Tim Chen wrote:
>>> On 12/17/18 10:52 PM, Yang Shi wrote:
>>>
>>>> diff --git a/mm/swap_state.c b/mm/swap_state.c
>>>> index fd2f21e..7cc3c29 100644
>>>> --- a/mm/swap_state.c
>>>> +++ b/mm/swap_state.c
>>>> @@ -538,11 +538,15 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>>>>        bool do_poll = true, page_allocated;
>>>>        struct vm_area_struct *vma = vmf->vma;
>>>>        unsigned long addr = vmf->address;
>>>> +    struct inode *inode = si->swap_file->f_mapping->host;
>>>>          mask = swapin_nr_pages(offset) - 1;
>>>>        if (!mask)
>>>>            goto skip;
>>>>    
>>> Shmem will also be using this function and I don't think the inode_read_congested
>>> logic is relevant for that case.
>> IMHO, shmem is also relevant. As long as it is trying to readahead from swap, it should check if the underlying device is busy or not regardless of shmem or anon page.
>>
> I don't think your dereference inode = si->swap_file->f_mapping->host
> is always safe.  You should do it only when (si->flags & SWP_FS) is true.

Do you mean it is not safe for swap partition?

I tested with swap partition too. It looks fine. Opening block device 
also gets inode.

Filename                                Type            Size Used    
Priority
/dev/sdb1                               partition 20970492        850168  -2

Thanks,
Yang

>
> Tim
Tim Chen Dec. 19, 2018, 5:28 p.m. UTC | #5
On 12/18/18 9:56 PM, Yang Shi wrote:
> 
> 
> On 12/18/18 4:16 PM, Tim Chen wrote:
>> On 12/18/18 3:43 PM, Yang Shi wrote:
>>>
>>> On 12/18/18 11:29 AM, Tim Chen wrote:
>>>> On 12/17/18 10:52 PM, Yang Shi wrote:
>>>>
>>>>> diff --git a/mm/swap_state.c b/mm/swap_state.c
>>>>> index fd2f21e..7cc3c29 100644
>>>>> --- a/mm/swap_state.c
>>>>> +++ b/mm/swap_state.c
>>>>> @@ -538,11 +538,15 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>>>>>        bool do_poll = true, page_allocated;
>>>>>        struct vm_area_struct *vma = vmf->vma;
>>>>>        unsigned long addr = vmf->address;
>>>>> +    struct inode *inode = si->swap_file->f_mapping->host;
>>>>>          mask = swapin_nr_pages(offset) - 1;
>>>>>        if (!mask)
>>>>>            goto skip;
>>>>>    
>>>> Shmem will also be using this function and I don't think the inode_read_congested
>>>> logic is relevant for that case.
>>> IMHO, shmem is also relevant. As long as it is trying to readahead from swap, it should check if the underlying device is busy or not regardless of shmem or anon page.
>>>
>> I don't think your dereference inode = si->swap_file->f_mapping->host
>> is always safe.  You should do it only when (si->flags & SWP_FS) is true.
> 
> Do you mean it is not safe for swap partition?

The f_mapping may not be instantiated.  It is only done for SWP_FS.

Tim
Yang Shi Dec. 19, 2018, 6:40 p.m. UTC | #6
On 12/19/18 9:28 AM, Tim Chen wrote:
> On 12/18/18 9:56 PM, Yang Shi wrote:
>>
>> On 12/18/18 4:16 PM, Tim Chen wrote:
>>> On 12/18/18 3:43 PM, Yang Shi wrote:
>>>> On 12/18/18 11:29 AM, Tim Chen wrote:
>>>>> On 12/17/18 10:52 PM, Yang Shi wrote:
>>>>>
>>>>>> diff --git a/mm/swap_state.c b/mm/swap_state.c
>>>>>> index fd2f21e..7cc3c29 100644
>>>>>> --- a/mm/swap_state.c
>>>>>> +++ b/mm/swap_state.c
>>>>>> @@ -538,11 +538,15 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>>>>>>         bool do_poll = true, page_allocated;
>>>>>>         struct vm_area_struct *vma = vmf->vma;
>>>>>>         unsigned long addr = vmf->address;
>>>>>> +    struct inode *inode = si->swap_file->f_mapping->host;
>>>>>>           mask = swapin_nr_pages(offset) - 1;
>>>>>>         if (!mask)
>>>>>>             goto skip;
>>>>>>     
>>>>> Shmem will also be using this function and I don't think the inode_read_congested
>>>>> logic is relevant for that case.
>>>> IMHO, shmem is also relevant. As long as it is trying to readahead from swap, it should check if the underlying device is busy or not regardless of shmem or anon page.
>>>>
>>> I don't think your dereference inode = si->swap_file->f_mapping->host
>>> is always safe.  You should do it only when (si->flags & SWP_FS) is true.
>> Do you mean it is not safe for swap partition?
> The f_mapping may not be instantiated.  It is only done for SWP_FS.

Really? I saw the below calls in swapon:

swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
...
p->swap_file = swap_file;
mapping = swap_file->f_mapping;
inode = mapping->host;
...

Then the below code manipulates the inode.

And, trace shows file_open_name() does call blkdev_open if it is turning 
block device swap on. And, blkdev_open() would return instantiated 
address_space and inode.

Am I missing something?

Thanks,
Yang

>
> Tim
>
Tim Chen Dec. 19, 2018, 7 p.m. UTC | #7
On 12/19/18 10:40 AM, Yang Shi wrote:
> 
> 

>>>> I don't think your dereference inode = si->swap_file->f_mapping->host
>>>> is always safe.  You should do it only when (si->flags & SWP_FS) is true.
>>> Do you mean it is not safe for swap partition?
>> The f_mapping may not be instantiated.  It is only done for SWP_FS.
> 
> Really? I saw the below calls in swapon:
> 
> swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
> ...
> p->swap_file = swap_file;
> mapping = swap_file->f_mapping;
> inode = mapping->host;
> ...
> 
> Then the below code manipulates the inode.
> 
> And, trace shows file_open_name() does call blkdev_open if it is turning block device swap on. And, blkdev_open() would return instantiated address_space and inode.
> 
> Am I missing something?
> 

I was trying to limit the congestion logic for block devices backed swap.
So the check I had in mind should really be "si->flags & SWP_BLKDEV"
instead of si->flags & SWP_FS.  I was concerned that there could
be other use cases where the inode dereference is invalid.

Looking at the code a bit more, looks like swap_cluster_readahead is not
used for other special case swap usage (like page migration).  So
you would a proper swapfile and inode here.  But I think it is still
a good idea to have a check for SWP_BLKDEV in si->flags.

Thanks.

Tim
Yang Shi Dec. 19, 2018, 11:48 p.m. UTC | #8
On 12/19/18 11:00 AM, Tim Chen wrote:
> On 12/19/18 10:40 AM, Yang Shi wrote:
>>
>>>>> I don't think your dereference inode = si->swap_file->f_mapping->host
>>>>> is always safe.  You should do it only when (si->flags & SWP_FS) is true.
>>>> Do you mean it is not safe for swap partition?
>>> The f_mapping may not be instantiated.  It is only done for SWP_FS.
>> Really? I saw the below calls in swapon:
>>
>> swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
>> ...
>> p->swap_file = swap_file;
>> mapping = swap_file->f_mapping;
>> inode = mapping->host;
>> ...
>>
>> Then the below code manipulates the inode.
>>
>> And, trace shows file_open_name() does call blkdev_open if it is turning block device swap on. And, blkdev_open() would return instantiated address_space and inode.
>>
>> Am I missing something?
>>
> I was trying to limit the congestion logic for block devices backed swap.
> So the check I had in mind should really be "si->flags & SWP_BLKDEV"
> instead of si->flags & SWP_FS.  I was concerned that there could
> be other use cases where the inode dereference is invalid.
>
> Looking at the code a bit more, looks like swap_cluster_readahead is not
> used for other special case swap usage (like page migration).  So
> you would a proper swapfile and inode here.  But I think it is still

Yes, just swap page fault and shmem calls this function. Actually, your 
above concern is valid if the inode were added into swap_address_space 
(address_space->host). I did this in my first attempt, and found out it 
may break some assumption in clear_page_dirty_for_io() and migration.

So, I made the patch as it is.

> a good idea to have a check for SWP_BLKDEV in si->flags.

I don't get your point why it should be block dev swap only. IMHO, block 
dev swap should be less likely fragmented and congested than swap file, 
right? Block dev swap could be a dedicated physical device, but swap 
file has to be with filesystem.

It sounds reasonable to me to have this check for swap file only. 
However, to be honest, it sounds not hurt to have both.

Thanks,
Yang

>
> Thanks.
>
> Tim
>   
>
Tim Chen Dec. 20, 2018, 1:05 a.m. UTC | #9
On 12/19/18 3:48 PM, Yang Shi wrote:
> 
> 
> On 12/19/18 11:00 AM, Tim Chen wrote:
>> On 12/19/18 10:40 AM, Yang Shi wrote:
>>>
>>>>>> I don't think your dereference inode = si->swap_file->f_mapping->host
>>>>>> is always safe.  You should do it only when (si->flags & SWP_FS) is true.
>>>>> Do you mean it is not safe for swap partition?
>>>> The f_mapping may not be instantiated.  It is only done for SWP_FS.
>>> Really? I saw the below calls in swapon:
>>>
>>> swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
>>> ...
>>> p->swap_file = swap_file;
>>> mapping = swap_file->f_mapping;
>>> inode = mapping->host;
>>> ...
>>>
>>> Then the below code manipulates the inode.
>>>
>>> And, trace shows file_open_name() does call blkdev_open if it is turning block device swap on. And, blkdev_open() would return instantiated address_space and inode.
>>>
>>> Am I missing something?
>>>
>> I was trying to limit the congestion logic for block devices backed swap.
>> So the check I had in mind should really be "si->flags & SWP_BLKDEV"
>> instead of si->flags & SWP_FS.  I was concerned that there could
>> be other use cases where the inode dereference is invalid.
>>
>> Looking at the code a bit more, looks like swap_cluster_readahead is not
>> used for other special case swap usage (like page migration).  So
>> you would a proper swapfile and inode here.  But I think it is still
> 
> Yes, just swap page fault and shmem calls this function. Actually, your above concern is valid if the inode were added into swap_address_space (address_space->host). I did this in my first attempt, and found out it may break some assumption in clear_page_dirty_for_io() and migration.
> 
> So, I made the patch as it is.
> 
>> a good idea to have a check for SWP_BLKDEV in si->flags.
> 
> I don't get your point why it should be block dev swap only. IMHO, block dev swap should be less likely fragmented and congested than swap file, right? Block dev swap could be a dedicated physical device, but swap file has to be with filesystem.
> 
> It sounds reasonable to me to have this check for swap file only. However, to be honest, it sounds not hurt to have both.
> 

Yes, I think we want to do it for both cases.

My original concern was that the backing store was not sitting on
a block device for some special case swap usage.  And si->swap_file->f_mapping->host
may fails to dereference to a host inode that's a valid block device.

It looks like on the call paths si->flags should either be SWP_BLKDEV or SWP_FS
on all the call paths. So si->swap_file->f_mapping->host should be valid
and your code is safe.

If we want to be paranoid we may do

	if (si->flags & (SWP_BLKDEV | SWP_FS)) {
		if (inode_read_congested(si->swap_file->f_mapping->host))
			goto skip;
	}

Thanks.

Tim
diff mbox series

Patch

diff --git a/mm/swap_state.c b/mm/swap_state.c
index fd2f21e..7cc3c29 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -538,11 +538,15 @@  struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	bool do_poll = true, page_allocated;
 	struct vm_area_struct *vma = vmf->vma;
 	unsigned long addr = vmf->address;
+	struct inode *inode = si->swap_file->f_mapping->host;
 
 	mask = swapin_nr_pages(offset) - 1;
 	if (!mask)
 		goto skip;
 
+	if (inode_read_congested(inode))
+		goto skip;
+
 	do_poll = false;
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;