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=-9.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham 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 9F6CCC54FCB for ; Wed, 22 Apr 2020 21:41:22 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3BE8720781 for ; Wed, 22 Apr 2020 21:41:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="St0hXYA7" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3BE8720781 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 93EC28E0006; Wed, 22 Apr 2020 17:41:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C9038E0003; Wed, 22 Apr 2020 17:41:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 790908E0006; Wed, 22 Apr 2020 17:41:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0160.hostedemail.com [216.40.44.160]) by kanga.kvack.org (Postfix) with ESMTP id 5DF798E0003 for ; Wed, 22 Apr 2020 17:41:21 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 292E16418 for ; Wed, 22 Apr 2020 21:41:21 +0000 (UTC) X-FDA: 76736812362.17.robin56_63142336c9361 X-HE-Tag: robin56_63142336c9361 X-Filterd-Recvd-Size: 5190 Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Wed, 22 Apr 2020 21:41:20 +0000 (UTC) Received: by mail-wr1-f66.google.com with SMTP id f13so4342162wrm.13 for ; Wed, 22 Apr 2020 14:41:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=ThhukyQINKI9whzNRVjFk4vyyNMAskuRdMfXhDNMzHw=; b=St0hXYA70gpzdeNRTuM1ZDupzmvZsnJzUQA8kIcp5aafnhKa+vEsAj/3nxKZQEOnnm BHXMvwLE+vNxlAADZO5QgdCRij+dAh5MvvzX/gPMC2Vjr9UvUx6gldOefKGLpccaSavG gNGFH+1ZXsvoizKnp4UNk/hg3jTr1hrKvxq1TFHITicRhe6sz4vgCNPm20QCdZ2qFFvI E3aygkvT7qRdYOCol7WIO9i1ZoUX7qdCWDQJKf6Wll8t7F+hUQPfJmuuhNAm2wKbVceH WA3UK980MibzA7++XQvnLHTwxpjaAnL8hdoLCbDGuuNOdww4Mz/lJR3US6LBEGI+ucAR t5lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ThhukyQINKI9whzNRVjFk4vyyNMAskuRdMfXhDNMzHw=; b=J8MzwPlhUMCf5aKlZULjSbCxD4PA55PMY/OmxH7DvDKK7mllsYlS+AoftyDGByA8wK Ng/kaBeQe4NEhpQwaMm6DEBWE78UwvMu7XUUgkNc5+wC90F8F8lXfYXI76/MvmKfmhJj CHef/11513reSpSnw4bRPEVg5XHuF3cJVK55/pkvnl9jGRPq2HA4hKP9tQTtxoArJ/Za heseswWllJJFLMF8N5B85kBSw5caJV8fz+aQPCOZ7yYSh0nUKZjEfBomjdDyb2FZDNps uTMTivaF+TBLmjOGJzpaGyEo5uLW0xIZqlBkFBIl1SYHOiVyxm6pLwzLllQV9T2g5j8N BzUQ== X-Gm-Message-State: AGi0Pua/71KvollLP9SAqSORKRrvcJSlvJytQyQQ9RCEGBGGEJfCvW3H LLKlu6L0kiQOnVouj1WCVpg= X-Google-Smtp-Source: APiQypJFdVUUcvCoLGEcck1TA1kOWxee40GrKRRTfNUBhRVTWDz51oR2Z4lREoX1Ws5xzVPoifqFSg== X-Received: by 2002:a5d:4748:: with SMTP id o8mr1148817wrs.422.1587591679490; Wed, 22 Apr 2020 14:41:19 -0700 (PDT) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id h17sm746578wmm.6.2020.04.22.14.41.18 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 22 Apr 2020 14:41:18 -0700 (PDT) From: Wei Yang To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, ying.huang@intel.com, Wei Yang , Hugh Dickins Subject: [PATCH v2] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots() Date: Wed, 22 Apr 2020 21:41:11 +0000 Message-Id: <20200422214111.19370-1-richard.weiyang@gmail.com> X-Mailer: git-send-email 2.11.0 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: 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. >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; + } + return *offset; +} + static int scan_swap_map_slots(struct swap_info_struct *si, unsigned char usage, int nr, swp_entry_t slots[]) @@ -876,22 +889,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si, scan: spin_unlock(&si->lock); - while (++offset <= si->highest_bit) { - if (!si->swap_map[offset]) { - spin_lock(&si->lock); - goto checks; - } - if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { - spin_lock(&si->lock); - goto checks; - } - if (unlikely(--latency_ration < 0)) { - cond_resched(); - latency_ration = LATENCY_LIMIT; - } - } - offset = si->lowest_bit; - while (offset < scan_base) { + while (next_offset(si, &offset, scan_base) != scan_base) { if (!si->swap_map[offset]) { spin_lock(&si->lock); goto checks; @@ -904,7 +902,6 @@ static int scan_swap_map_slots(struct swap_info_struct *si, cond_resched(); latency_ration = LATENCY_LIMIT; } - offset++; } spin_lock(&si->lock); -- 2.23.0