From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85193C433EF for ; Tue, 10 May 2022 00:46:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D549F6B0072; Mon, 9 May 2022 20:46:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D01136B0073; Mon, 9 May 2022 20:46:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BCAE46B0074; Mon, 9 May 2022 20:46:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AD05B6B0072 for ; Mon, 9 May 2022 20:46:02 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 82CD73141F for ; Tue, 10 May 2022 00:46:02 +0000 (UTC) X-FDA: 79447991364.17.B07D4C6 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf30.hostedemail.com (Postfix) with ESMTP id EA0898009E for ; Tue, 10 May 2022 00:45:44 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id CF1761F9EF; Tue, 10 May 2022 00:46:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1652143560; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CtMQXVZDrYDub2elu5mYBR/xnlKkD34z85QlnnWHBJE=; b=FXY11OxPVLDYqHQzuMY59q4TCVfC7yzyWSH1yeNLJFpRxpSa8dhwob1xPdf1uOWH3onGI9 DeS3f4mzjbSdaPYz+mATUc+Pe/8LmX/Q13uZdWEzqiH9lT+wf06bnhDe5fbfB82s7fL1do SI7IidNC6s9xQnuhks07IrMpag1bO6o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1652143560; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CtMQXVZDrYDub2elu5mYBR/xnlKkD34z85QlnnWHBJE=; b=+cXAQWYZjnP/LmKKet27Ult+XaSpd+yjQVIMlwlnGxRrorlBEeLqSE7b9BFAaOw0BqrAkd v2sTeS/J5aI93BBQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 3429F13A76; Tue, 10 May 2022 00:45:56 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 8AvuN8S1eWJvagAAMHmgww (envelope-from ); Tue, 10 May 2022 00:45:56 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 From: "NeilBrown" To: "Miaohe Lin" Cc: akpm@linux-foundation.org, willy@infradead.org, vbabka@suse.cz, dhowells@redhat.com, apopple@nvidia.com, david@redhat.com, surenb@google.com, peterx@redhat.com, naoya.horiguchi@nec.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linmiaohe@huawei.com Subject: Re: [PATCH 11/15] mm/swap: add helper swap_offset_available() In-reply-to: <20220509131416.17553-12-linmiaohe@huawei.com> References: <20220509131416.17553-1-linmiaohe@huawei.com>, <20220509131416.17553-12-linmiaohe@huawei.com> Date: Tue, 10 May 2022 10:45:54 +1000 Message-id: <165214355418.14782.13896859043718755300@noble.neil.brown.name> X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: EA0898009E X-Stat-Signature: pdx1rihn73czoyfi1mdkcdrzoz5c8tet Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=FXY11OxP; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=+cXAQWYZ; spf=pass (imf30.hostedemail.com: domain of neilb@suse.de designates 195.135.220.29 as permitted sender) smtp.mailfrom=neilb@suse.de; dmarc=pass (policy=none) header.from=suse.de X-HE-Tag: 1652143544-2797 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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() it might be ok. Otherwise I would rather the spin_lock() was called when the function returned true. Thanks, NeilBrown >=20 > Signed-off-by: Miaohe Lin > --- > mm/swapfile.c | 33 +++++++++++++++++---------------- > 1 file changed, 17 insertions(+), 16 deletions(-) >=20 > 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); > } > =20 > +static inline bool swap_offset_available(struct swap_info_struct *si, unsi= gned 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]) =3D=3D SWAP_HAS_CAC= HE) { > + 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 <=3D 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]) =3D=3D SWAP_HAS_CACHE) { > - spin_lock(&si->lock); > - goto checks; > - } > if (unlikely(--latency_ration < 0)) { > cond_resched(); > latency_ration =3D LATENCY_LIMIT; > @@ -970,15 +978,8 @@ static int scan_swap_map_slots(struct swap_info_struct= *si, > } > offset =3D 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]) =3D=3D SWAP_HAS_CACHE) { > - spin_lock(&si->lock); > - goto checks; > - } > if (unlikely(--latency_ration < 0)) { > cond_resched(); > latency_ration =3D LATENCY_LIMIT; > --=20 > 2.23.0 >=20 >=20