From patchwork Sat Feb 29 13:15:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 11413837 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 1EC4714B4 for ; Sat, 29 Feb 2020 13:15:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B97CA246A2 for ; Sat, 29 Feb 2020 13:15:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="adMOLuCh" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B97CA246A2 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 351356B0003; Sat, 29 Feb 2020 08:15:49 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2D7AF6B0006; Sat, 29 Feb 2020 08:15:49 -0500 (EST) 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 1A0ED6B0007; Sat, 29 Feb 2020 08:15:49 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0141.hostedemail.com [216.40.44.141]) by kanga.kvack.org (Postfix) with ESMTP id F2C4E6B0003 for ; Sat, 29 Feb 2020 08:15:48 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 91AD7582C for ; Sat, 29 Feb 2020 13:15:48 +0000 (UTC) X-FDA: 76543211976.07.act78_1594d63236731 X-Spam-Summary: 2,0,0,0defa79b47c3673a,d41d8cd98f00b204,richard.weiyang@gmail.com,,RULES_HIT:41:69:355:379:541:800:960:966:973:988:989:1260:1345:1437:1534: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:4385:4605:5007:6261:6653:7514:7558:9413:10004:11026:11473:11658:11914:12114:12296:12297:12517:12519:12555:12679:12895:14093:14181:14394:14687:14721:21080:21444:21627:21666:21790:21990:30054,0,RBL:209.85.128.67:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.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:24,LUA_SUMMARY:none X-HE-Tag: act78_1594d63236731 X-Filterd-Recvd-Size: 4768 Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Sat, 29 Feb 2020 13:15:48 +0000 (UTC) Received: by mail-wm1-f67.google.com with SMTP id m10so11397609wmc.0 for ; Sat, 29 Feb 2020 05:15:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=StjHIE38vB9VkgRe5PBuA02+GIccOjh9WWY3dcYX1eA=; b=adMOLuCh06HYJjXSThzhaz1rU+fuueacqGN6n9pXhCp5nvsESAVrSbFoBx/0UHeH3K 0SQ2yRT9USWF1odw949iBSRHEED2wf0BmydNR87cqP6eP5Bvw+LxtkpzvrNFiEEMv/u5 oB4CyYYrx0x64hZpgPOADZ0/erRjqiKWPyNgiVVhf+56z4iTSTeknJnZJG236n+9mP5t lTjDCiBgq1+PWdOMkriAoASal+hWEan5YzTFGMC80Ov7CzKk5znRE32xqB/89xHK8dXL cce8Ib3TGvsApG9WFJ+cw95QojcAFgbWHv6TT68EVDFiTpv3Gnxcaoo1I/x8f0hBU5ac 3Ryg== 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=StjHIE38vB9VkgRe5PBuA02+GIccOjh9WWY3dcYX1eA=; b=AbdCtZywadQNRSpi4qxMAIUcdl7d3yFnCJ82UIggd1iH/hpQrz4yaymy5YXS/sAaYZ LApfvKH4LdId1a1easQBBcL5bTBE8l/z/Jau46LwW6YtS2Zf+4fiSGG7tPK+MJjbTzTx vVAQFWjMb9IzAguNdNi5liMAkK04FeOFd+iJdCObBn7FxijKoPQsgnbUSquWsXmDKgOo C1gPYdpo66hvaEElFc58yEdBto10OTSBcy0otVvP2mo5hw7ZMRC/595CBeSRbawraZJw z10wYZRMZh1i+/r3b9+hyHHX0nzPn1YewJlXpoJS87BGOQEAnjV5DlvIs1yGIzY7FjAk rh6A== X-Gm-Message-State: APjAAAWTuUCPUSrV7ImR+vICWWq/6cDa7qjU1qZSRk5kG5WMjVtHUQVm DJphjfNdcg6mJ+Jk7nGX+/Q= X-Google-Smtp-Source: APXvYqwid96ZnU/kQzW9Q6WbNv3G7eYYzbUuU01TLOjfXlMXmXHcKC0x2TQQy3YRbQbUsrZlRlpRnA== X-Received: by 2002:a05:600c:21c6:: with SMTP id x6mr9803679wmj.17.1582982147118; Sat, 29 Feb 2020 05:15:47 -0800 (PST) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id t187sm6702694wmt.25.2020.02.29.05.15.46 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 29 Feb 2020 05:15:46 -0800 (PST) From: Wei Yang To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Wei Yang , Hugh Dickins Subject: [PATCH] mm/swapfile.c: simplify the scan loop in scan_swap_map_slots() Date: Sat, 29 Feb 2020 13:15:37 +0000 Message-Id: <20200229131537.3475-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 --- mm/swapfile.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 95024f9b691a..42c5c2010bfc 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -729,6 +729,14 @@ 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) +{ + if (++(*offset) > si->highest_bit) + *offset = si->lowest_bit; + return *offset; +} + static int scan_swap_map_slots(struct swap_info_struct *si, unsigned char usage, int nr, swp_entry_t slots[]) @@ -883,7 +891,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si, scan: spin_unlock(&si->lock); - while (++offset <= si->highest_bit) { + while (next_offset(si, &offset) != scan_base) { if (!si->swap_map[offset]) { spin_lock(&si->lock); goto checks; @@ -897,22 +905,6 @@ static int scan_swap_map_slots(struct swap_info_struct *si, latency_ration = LATENCY_LIMIT; } } - offset = si->lowest_bit; - while (offset < scan_base) { - 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++; - } spin_lock(&si->lock); no_page: