Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp355965ybf; Sat, 29 Feb 2020 05:17:08 -0800 (PST) X-Google-Smtp-Source: APXvYqxrKpoXdZYTZi4/mjbUIIssdLFmDgdXMWzQWGo/cnVCVb3WCLMNiEo47Ms+UqS8hI75O+hH X-Received: by 2002:aca:b183:: with SMTP id a125mr6335241oif.83.1582982228353; Sat, 29 Feb 2020 05:17:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582982228; cv=none; d=google.com; s=arc-20160816; b=E5+scGQlBdgrq6aHD5ZHH5m+QO+2GnYXAL3TBQh/zGWIOMRWFaifcrYgJCzgXPWDkE OoNa3OTRHq/CRKOhWI9i14asMyExqQbKK1OJOV+zYLyCXd0nB+fKP+Q17pfIJSND7H22 A65SlaygSEip5TZEax1IYL0qoRvOZcWX0eu/g3rb9f4OKWPsbJecckM7hSbv0Nwigw7f xa0hVIqw8LtMQVLnkOf4/7loqehWG6gIzbULW/UEKcfxG9jcxJcYxbpM2N3NUDs479et RLoO3/WElClquN2BIWgQqzbYDumFPgE40Ca3B+X/tAgkGvaRKNLu7PC4AaPKFQcgneN4 /2cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=StjHIE38vB9VkgRe5PBuA02+GIccOjh9WWY3dcYX1eA=; b=LaSjot+udtEHuoguXX1wxWNn/mmBHn9MNx07Du/hNQgk1eO9YE1bmWfEBccYpfVXiX 5/pa+10NLETL5OAoAffb3JHtydxFmkYUBRYzTQVnFgaY8rFv/JnWZCbkdyIyRA8dEmIX IUznQw1t2xQxfNWsazInHelyiubRA0rU9E0EfwI5NYmRjaL4zTtVphAzOvrXHR/5ZSWy BAtEKCJQgN1pDYHZ6WDVQ1PBt/KW+1GAGSYCC9m4RWGIjJ6674i7QicjmgzVfonxnU9A GpGY8xO7rd1arwsSjDLURnyYWl62GtkbsRm3ePHg40j6jt8j8QfPnyNryJ7reoDeef2w jxwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=adMOLuCh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n9si3177196ota.103.2020.02.29.05.16.56; Sat, 29 Feb 2020 05:17:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=adMOLuCh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727040AbgB2NPu (ORCPT + 99 others); Sat, 29 Feb 2020 08:15:50 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:50246 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726933AbgB2NPu (ORCPT ); Sat, 29 Feb 2020 08:15:50 -0500 Received: by mail-wm1-f65.google.com with SMTP id a5so6422475wmb.0 for ; Sat, 29 Feb 2020 05:15:47 -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=BTned06rO0+urWoWVmNoV/RJTPIEYuIp8XvYoEBYDH+G5qvrM0DOHaEKwI+5pHek5f VVruBBxhPUNlULk/aMFe+Le9SU89JEHOAcv98jeSOW7xeSxQbVLGcnMsJfIPOKvYppMQ AaYbpgMfwtwAI68vL7acPCVqqydgxIm5Fr8mIuDPvrNuonhHCHdDvN7V8/66qhaKLYE0 dpXXXLTmpwdbUinS1lD0GLAhChurMG4+p7Dm6W2E79uShWaT3N46FZlRJmOLQFX9YVw7 uouCuSa69Jfezzb11xif/TL1i89xdEyJGvq7X5wXiiJtZnnDwB0tYsubWqjiMTDwqW4d LbPw== X-Gm-Message-State: APjAAAUkNkRTzRXUOA2MIwYaazTGVXC4Xy8avI54BwYCcT1d32iWfrXj ea9L+/UHQqTT2aZhvAzEig0= 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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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