Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5554116iob; Mon, 9 May 2022 20:30:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxSpz1vuplQKH2NKUv0tm/TMJPmqrgsQy/jPaVF5boNiXFfASCahAeSylF2xE81slf8CqON X-Received: by 2002:a05:6402:84a:b0:423:fe99:8c53 with SMTP id b10-20020a056402084a00b00423fe998c53mr20564043edz.195.1652153434509; Mon, 09 May 2022 20:30:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652153434; cv=none; d=google.com; s=arc-20160816; b=zPhVOz1uO6gxLcjqbKDl/pdjwtGOcwJ3MKnIl3wq/kZXBVTSjeuw1nb5UaWgH7uc6S Q99rsabzA2bTUYrJorqpE9Qu+HJ6dwa7OrFviR6WNeY96tgJngi532JvtBrcXw/YlW1D uIRWWF7Jx3F5uDkZYV2tIXJN0vGDwmTsYOh+Xcp/0VOxCukeV+QWuBNR3k/MCfOJBG78 QtDd4R5zkMUeb5auahzIRTB4RC74oqFM0MOrqDueTP2AQMvyIS1tVihsk5paJSsm6mMM 2k/Clb1QhU1ZuSpNjFF3ysqdP/enM3c3U41iIH56k9WuDO34p3pBJGEN33nBWoOFFWzT CPtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:references:in-reply-to:subject :cc:to:from:mime-version:content-transfer-encoding:dkim-signature :dkim-signature; bh=CtMQXVZDrYDub2elu5mYBR/xnlKkD34z85QlnnWHBJE=; b=H7qlQ4vsRq572jqp7BkolCjXoo3D+xuoJc26K8Jp97yhcboB7LuoPqstoO6pkH4EKg gQ4jdIc0QE6KitrVFtBTvv5t84Q9z2hV0qH3gCIZGL9lPgO9u6jpsHFXOnhwUnFTU/Y2 MDYiCB7MbdRpBVbl4UNu6puiQBC1Ib7GsNaO0JFzGDB+AtT5Oud5FN+zcV780CLVU8FA GRJ8lSX5AVLS0qpH/mAp6AYXOerIIfBQUXQI3pmnQQakSaRRza06m5QC8sy64VifQdKX OLx4YeCg3ys1aoHh9/7gb7IsWrrN6Layk/26aOwlpkdtysbeFhaTdzC7OjpoQib2dViH K0sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=FXY11OxP; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v1-20020a50c401000000b0041d70e6ea5bsi12491329edf.533.2022.05.09.20.30.11; Mon, 09 May 2022 20:30:34 -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; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=FXY11OxP; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232942AbiEJAuH (ORCPT + 99 others); Mon, 9 May 2022 20:50:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229991AbiEJAuD (ORCPT ); Mon, 9 May 2022 20:50:03 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FAA72380CC for ; Mon, 9 May 2022 17:46:02 -0700 (PDT) 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-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 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