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 X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04C18C55191 for ; Thu, 23 Apr 2020 05:57:44 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C683A214AF for ; Thu, 23 Apr 2020 05:57:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C683A214AF Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 66E038E000C; Thu, 23 Apr 2020 01:57:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 61DD48E0008; Thu, 23 Apr 2020 01:57:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 533728E000C; Thu, 23 Apr 2020 01:57:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0054.hostedemail.com [216.40.44.54]) by kanga.kvack.org (Postfix) with ESMTP id 3BC368E0008 for ; Thu, 23 Apr 2020 01:57:43 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 0490B83FA for ; Thu, 23 Apr 2020 05:57:43 +0000 (UTC) X-FDA: 76738063206.06.shock30_434ea9c91f10d X-HE-Tag: shock30_434ea9c91f10d X-Filterd-Recvd-Size: 3544 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Thu, 23 Apr 2020 05:57:42 +0000 (UTC) IronPort-SDR: nfNMM46VRxRCVR0gJ3zbZk0fq7a7WMNHkI1k4mlElPXhnvn99ud04VWXFS8uJikzdxPHCk0LYK 1LZEZVBy4Gkg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Apr 2020 22:57:41 -0700 IronPort-SDR: ejDVh1zgi7oy/DpflGw3T9rYQg4ba8uEkByvioeoGK7V4VDJqnLN1m14ZQi7+kcx49k+a7vYcp UAC8mZg8SZPQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,305,1583222400"; d="scan'208";a="456786339" Received: from yhuang-dev.sh.intel.com (HELO yhuang-dev) ([10.239.159.23]) by fmsmga005.fm.intel.com with ESMTP; 22 Apr 2020 22:57:39 -0700 From: "Huang\, Ying" To: Wei Yang Cc: , , , Hugh Dickins Subject: Re: [PATCH v2] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots() In-Reply-To: <20200422214111.19370-1-richard.weiyang@gmail.com> (Wei Yang's message of "Wed, 22 Apr 2020 21:41:11 +0000") References: <20200422214111.19370-1-richard.weiyang@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Date: Thu, 23 Apr 2020 13:57:34 +0800 Message-ID: <87d07y2181.fsf@yhuang-dev.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ascii 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: Wei Yang writes: > After commit c60aa176c6de8 ("swapfile: swap allocation cycle if > nonrot"), swap allocation is cyclic. Current approach is done with two > separate loop on the upper and lower half. This looks a little > redundant. I can understand that the redundant code doesn't smell good. But I don't think the new code is easier to be understood than the original one. > From another point of view, the loop iterates [lowest_bit, highest_bit] > range starting with (offset + 1) but except scan_base. So we can > simplify the loop with condition (next_offset() != scan_base) by > introducing next_offset() which makes sure offset fit in that range > with correct order. > > Signed-off-by: Wei Yang > CC: Hugh Dickins > CC: "Huang, Ying" > > --- > v2: > * return scan_base if the lower part is eaten > * only start over when iterating on the upper part > --- > mm/swapfile.c | 31 ++++++++++++++----------------- > 1 file changed, 14 insertions(+), 17 deletions(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index f903e5a165d5..0005a4a1c1b4 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -729,6 +729,19 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, > } > } > > +static unsigned long next_offset(struct swap_info_struct *si, > + unsigned long *offset, unsigned long scan_base) > +{ > + /* only start over when iterating on the upper part */ > + if (++(*offset) > si->highest_bit && *offset > scan_base) { > + *offset = si->lowest_bit; > + /* someone has eaten the lower part */ > + if (si->lowest_bit >= scan_base) > + return scan_base; > + } if "offset > si->highest_bit" is true and "offset < scan_base" is true, scan_base need to be returned. Again, the new code doesn't make it easier to find this kind of issues. Best Regards, Huang, Ying