Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6001104iob; Tue, 10 May 2022 08:15:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUDcdpGLnj1zMZNPFOHaeqd/er9DSSvoeG/gcX7Lbs5lM1fxs/lpjWYh097Z64WdgpH2we X-Received: by 2002:a62:31c1:0:b0:50a:4909:2691 with SMTP id x184-20020a6231c1000000b0050a49092691mr21317315pfx.64.1652195752500; Tue, 10 May 2022 08:15:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652195752; cv=none; d=google.com; s=arc-20160816; b=V0AODQ+CPQvz6hMdd6RNqu5opH+cnV2M9J2MpBFW+XtUmhWFrWV4UB9kNc2kVmW7CZ p33wEZnSviXjbGVIHxXWKCZjH7jzb0cBLhGP7lu9uMDaJ55X7BQfYa9ep8oVYMLmk44x JmMyfx/4dV8D+rJOEVkeSjPmhn8hDZUZ4m5pKMW8sESwI9AE0fHi79RP0BTJ7aqUfsqL qBEkPHsWwpWMUxIx5CoTc7EZsaSKATjbcglnKUHJ2Qf/+E6tmo0RpgB+rw43rp406Hph b4scFIbbPF61DyDjc+J7MZWwfdNvZkJYTZnPB6qZyQaRYmLO8k/CKnlqJDhFMqgjzKsd wBRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=3Q8TnGKMlxKb9rbLql73qcK6AAgQ0t0m24UvYZANDEs=; b=j5rIeVuh8krVOXfqAjg42ojTm/vG8lhI+YTXqzQHaayQXa1L8EZRjSHVj2rIvp+4El wIpHWuQ/91/zWmfWrNIndndfqdaQanIaFgumw4m7Hj+GIayh32sCJThndtabND34di4J tXXcsqMmKvI57N2C8xV0F2iO+IRBY3ZJRNV4jXDhvJbyvMwcrG35/FHkcKHdptBvoM0e TKPdwlIkPJlpnxCYbMXI0nQjsBdAxMH5SCSxMjulwtxcxs9fgWFu+F2svkSWmhFvZaMD A1lLX36hKo/hT0mGdhZhOwDbOyawaxjx4CnHMGxbo/2BiABTwnNuCJHDeyLrDfHLcksx p6aQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v190-20020a6389c7000000b003c659f2efe3si2363506pgd.245.2022.05.10.08.15.37; Tue, 10 May 2022 08:15:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234472AbiEJCHW (ORCPT + 99 others); Mon, 9 May 2022 22:07:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230437AbiEJCHS (ORCPT ); Mon, 9 May 2022 22:07:18 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB30282452 for ; Mon, 9 May 2022 19:03:21 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Ky1X30h4dzhYxZ; Tue, 10 May 2022 10:02:43 +0800 (CST) Received: from [10.174.177.76] (10.174.177.76) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 10 May 2022 10:03:19 +0800 Subject: Re: [PATCH 11/15] mm/swap: add helper swap_offset_available() To: NeilBrown CC: , , , , , , , , , , References: <20220509131416.17553-1-linmiaohe@huawei.com> <20220509131416.17553-12-linmiaohe@huawei.com> <165214355418.14782.13896859043718755300@noble.neil.brown.name> From: Miaohe Lin Message-ID: <9319a62b-f43d-8ee9-77b9-a1afee7dbc10@huawei.com> Date: Tue, 10 May 2022 10:03:19 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <165214355418.14782.13896859043718755300@noble.neil.brown.name> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.177.76] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-5.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/5/10 8:45, NeilBrown wrote: > On Mon, 09 May 2022, Miaohe Lin wrote: >> Add helper swap_offset_available() to remove some duplicated codes. >> Minor readability improvement. > > I don't think that putting the spin_lock() inside the inline helper is > good for readability. > If the function was called > swap_offset_available_and_locked() Yes, swap_offset_available_and_locked should be more suitable as we do the spin_lock inside it. Will do this in next version. Thanks! > > it might be ok. Otherwise I would rather the spin_lock() was called > when the function returned true. > > Thanks, > NeilBrown > >> >> Signed-off-by: Miaohe Lin >> --- >> mm/swapfile.c | 33 +++++++++++++++++---------------- >> 1 file changed, 17 insertions(+), 16 deletions(-) >> >> diff --git a/mm/swapfile.c b/mm/swapfile.c >> index c90298a0561a..d5d3e2d03d28 100644 >> --- a/mm/swapfile.c >> +++ b/mm/swapfile.c >> @@ -776,6 +776,21 @@ static void set_cluster_next(struct swap_info_struct *si, unsigned long next) >> this_cpu_write(*si->cluster_next_cpu, next); >> } >> >> +static inline bool swap_offset_available(struct swap_info_struct *si, unsigned long offset) >> +{ >> + if (data_race(!si->swap_map[offset])) { >> + spin_lock(&si->lock); >> + return true; >> + } >> + >> + if (vm_swap_full() && READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) { >> + spin_lock(&si->lock); >> + return true; >> + } >> + >> + return false; >> +} >> + >> static int scan_swap_map_slots(struct swap_info_struct *si, >> unsigned char usage, int nr, >> swp_entry_t slots[]) >> @@ -953,15 +968,8 @@ static int scan_swap_map_slots(struct swap_info_struct *si, >> scan: >> spin_unlock(&si->lock); >> while (++offset <= READ_ONCE(si->highest_bit)) { >> - if (data_race(!si->swap_map[offset])) { >> - spin_lock(&si->lock); >> + if (swap_offset_available(si, offset)) >> goto checks; >> - } >> - if (vm_swap_full() && >> - READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) { >> - spin_lock(&si->lock); >> - goto checks; >> - } >> if (unlikely(--latency_ration < 0)) { >> cond_resched(); >> latency_ration = LATENCY_LIMIT; >> @@ -970,15 +978,8 @@ static int scan_swap_map_slots(struct swap_info_struct *si, >> } >> offset = si->lowest_bit; >> while (offset < scan_base) { >> - if (data_race(!si->swap_map[offset])) { >> - spin_lock(&si->lock); >> + if (swap_offset_available(si, offset)) >> goto checks; >> - } >> - if (vm_swap_full() && >> - READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) { >> - spin_lock(&si->lock); >> - goto checks; >> - } >> if (unlikely(--latency_ration < 0)) { >> cond_resched(); >> latency_ration = LATENCY_LIMIT; >> -- >> 2.23.0 >> >> > . >