Re: [PATCH] swap: cleanup get/put_swap_device usage

From: Huang, Ying
Date: Tue May 16 2023 - 20:28:48 EST


David Hildenbrand <david@xxxxxxxxxx> writes:

> On 16.05.23 07:29, Huang Ying wrote:
>> The general rule to use a swap entry is as follows.
>> When we get a swap entry, if there isn't some other way to prevent
>> swapoff, such as page lock for swap cache, page table lock, etc., the
>> swap entry may become invalid because of swapoff. Then, we need to
>> enclose all swap related functions with get_swap_device() and
>> put_swap_device(), unless the swap functions call
>> get/put_swap_device() by themselves.
>> Add the rule as comments of get_swap_device(), and cleanup some
>> functions which call get/put_swap_device().
>> 1. Enlarge the get/put_swap_device() protection range in
>> __read_swap_cache_async(). This makes the function a little easier to
>> be understood because we don't need to consider swapoff. And this
>> makes it possible to remove get/put_swap_device() calling in some
>> function called by __read_swap_cache_async().
>> 2. Remove get/put_swap_device() in __swap_count(). Which is call in
>> do_swap_page() only, which encloses the call with get/put_swap_device()
>> already.
>> 3. Remove get/put_swap_device() in __swp_swapcount(). Which is call
>> in __read_swap_cache_async() only, which encloses the call with
>> get/put_swap_device() already.
>> 4. Remove get/put_swap_device() in __swap_duplicate(). Which is
>> called
>> by
>> - swap_shmem_alloc(): the swap cache is locked.
>> - copy_nonpresent_pte() -> swap_duplicate() and try_to_unmap_one()
>> ->
>> swap_duplicate(): the page table lock is held.
>> - __read_swap_cache_async() -> swapcache_prepare(): enclosed with
>> get/put_swap_device() already.
>> Other get/put_swap_device() usages are checked too.
>
> I suggest splitting this patch up into logical pieces as outlined here
> by you already.

OK. Will do that in the next version.

Best Regards,
Huang, Ying