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.8 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 4236AC3F2CE 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 BA076246B9 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 BA076246B9 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) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D7AF6B0006; Sat, 29 Feb 2020 08:15:49 -0500 (EST) 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-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-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: -- 2.23.0