From patchwork Wed Apr 22 21:41:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 11504647 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AEFE1912 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 7ADFA20857 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 7ADFA20857 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) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8C9038E0003; Wed, 22 Apr 2020 17:41:21 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org 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-Original-To: linux-mm@kvack.org 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-Spam-Summary: 2,0,0,d113451f77586ef3,d41d8cd98f00b204,richard.weiyang@gmail.com,,RULES_HIT:41:69:355:379:541:800:960:966:973:988:989:1260:1345:1437:1535:1542:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2731:3138:3139:3140:3141:3142:3353:3865:3866:3868:3870:3871:3874:4321:4385:4605:5007:6261:6653:7514:7558:9413:10004:11026:11473:11658:11914:12043:12114:12296:12297:12517:12519:12555:12679:12683:12895:14093:14181:14394:14687:14721:21080:21444:21611:21627:21666:21790:21990:30054:30064,0,RBL:209.85.221.66:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:25,LUA_SUMMARY:none 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);