From patchwork Sat Apr 18 08:47:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 11496385 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 6F7571392 for ; Sat, 18 Apr 2020 08:47:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 20EB822209 for ; Sat, 18 Apr 2020 08:47:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 20EB822209 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B4E768E0005; Sat, 18 Apr 2020 04:47:10 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id AFF7E8E0003; Sat, 18 Apr 2020 04:47:10 -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 9ED6E8E0005; Sat, 18 Apr 2020 04:47:10 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0237.hostedemail.com [216.40.44.237]) by kanga.kvack.org (Postfix) with ESMTP id 8300B8E0003 for ; Sat, 18 Apr 2020 04:47:10 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3AC3B180ACF76 for ; Sat, 18 Apr 2020 08:47:10 +0000 (UTC) X-FDA: 76720346220.14.roll40_5ba20b74cf436 X-Spam-Summary: 2,0,0,1984da73c42dd958,d41d8cd98f00b204,andrea.righi@canonical.com,,RULES_HIT:41:69:355:379:800:960:973:982:988:989:1260:1277:1312:1313:1314:1345:1431:1437:1516:1518:1519:1535:1543:1593:1594:1595:1596:1711:1730:1747:1777:1792:2393:2559:2562:2895:2912:3138:3139:3140:3141:3142:3355:3653:3865:3866:3867:3868:3870:3871:3874:4117:4250:4605:5007:6119:6261:6630:7875:7903:8603:8634:8784:9121:10004:10400:11026:11232:11233:11473:11658:11914:12297:12438:12517:12519:12555:12679:12895:13161:13229:13439:13895:14181:14394:14721:21060:21063:21080:21324:21444:21451:21627:21939:30003:30054:30055:30056:30064:30070,0,RBL:91.189.89.112:@canonical.com:.lbl8.mailshell.net-64.201.201.201 62.14.15.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:69,LUA_SUMMARY:none X-HE-Tag: roll40_5ba20b74cf436 X-Filterd-Recvd-Size: 6173 Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) by imf32.hostedemail.com (Postfix) with ESMTP for ; Sat, 18 Apr 2020 08:47:09 +0000 (UTC) Received: from mail-wr1-f72.google.com ([209.85.221.72]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jPj80-0006oi-17 for linux-mm@kvack.org; Sat, 18 Apr 2020 08:47:08 +0000 Received: by mail-wr1-f72.google.com with SMTP id x15so328326wrn.0 for ; Sat, 18 Apr 2020 01:47:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=vOPg5rr8Ro1A6zb9Lv2UALWQl5DWsT0FSeERgKCUFKE=; b=fxGtOz6W0qmh8kGEP9px+7MWsdhUObUJG2/xqVTg0QEuJwyvRD4kCRHsiPqU/W1n1T +LNyip4Ej0eH3+3g5cY3Iecp/enHQ0mo8CALVDOak27Edqgw4NHi4+Z8GUmOoM2oB+Yr TfuZLVk/PknqRwxXappYNHueEr1BeIGBJu65d8K+dZZbYMso5LhvrZmXHa4kylAmPwFu johDKR96o/y93BhyzwDhzEDkm51L4jl3IOg88XvSmGOb0hyLgM4Wl3k7/P7eiGKu/5Ei djw3Ac7+nNw/TluK7xY+9DzzpgvUPGkkRN+oGRg9feWhDpxk82AQRCST2H0JaR9LwxTv wRog== X-Gm-Message-State: AGi0Puaf9XiB2CgOAwTPY5f+82rTSRJ15jFmVFnFu7a/UNs1V9FGtnJl cHUyi0iF7TFrSrBhqBd/ZVV2emzPjBqFzIeTVen9n7vTOzfY6eRCg7Z9YeFt+nxx1ANP6tJerG/ 05V5VXKqD5vrQEJClEpmZCzyzmL/9 X-Received: by 2002:adf:e4c5:: with SMTP id v5mr8551992wrm.143.1587199627651; Sat, 18 Apr 2020 01:47:07 -0700 (PDT) X-Google-Smtp-Source: APiQypKpEFeCVqy5Rt0bsSfSYXTR3NDU+MleLjnhB32QOoJMYn3AU4x9EhhyI0g6wBGva+4iCiJ9UQ== X-Received: by 2002:adf:e4c5:: with SMTP id v5mr8551965wrm.143.1587199627338; Sat, 18 Apr 2020 01:47:07 -0700 (PDT) Received: from localhost (host123-127-dynamic.36-79-r.retail.telecomitalia.it. [79.36.127.123]) by smtp.gmail.com with ESMTPSA id k9sm36943698wrd.17.2020.04.18.01.47.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Apr 2020 01:47:06 -0700 (PDT) Date: Sat, 18 Apr 2020 10:47:05 +0200 From: Andrea Righi To: Andrew Morton Cc: Huang Ying , Minchan Kim , Anchal Agarwal , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] mm: swap: properly update readahead statistics in unuse_pte_range() Message-ID: <20200418084705.GA147642@xps-13> MIME-Version: 1.0 Content-Disposition: inline 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: In unuse_pte_range() we blindly swap-in pages without checking if the swap entry is already present in the swap cache. By doing this, the hit/miss ratio used by the swap readahead heuristic is not properly updated and this leads to non-optimal performance during swapoff. Tracing the distribution of the readahead size returned by the swap readahead heuristic during swapoff shows that a small readahead size is used most of the time as if we had only misses (this happens both with cluster and vma readahead), for example: r::swapin_nr_pages(unsigned long offset):unsigned long:$retval COUNT EVENT 36948 $retval = 8 44151 $retval = 4 49290 $retval = 1 527771 $retval = 2 Checking if the swap entry is present in the swap cache, instead, allows to properly update the readahead statistics and the heuristic behaves in a better way during swapoff, selecting a bigger readahead size: r::swapin_nr_pages(unsigned long offset):unsigned long:$retval COUNT EVENT 1618 $retval = 1 4960 $retval = 2 41315 $retval = 4 103521 $retval = 8 In terms of swapoff performance the result is the following: Testing environment =================== - Host: CPU: 1.8GHz Intel Core i7-8565U (quad-core, 8MB cache) HDD: PC401 NVMe SK hynix 512GB MEM: 16GB - Guest (kvm): 8GB of RAM virtio block driver 16GB swap file on ext4 (/swapfile) Test case ========= - allocate 85% of memory - `systemctl hibernate` to force all the pages to be swapped-out to the swap file - resume the system - measure the time that swapoff takes to complete: # /usr/bin/time swapoff /swapfile Result (swapoff time) ====== 5.6 vanilla 5.6 w/ this patch ----------- ----------------- cluster-readahead 22.09s 12.19s vma-readahead 18.20s 15.33s Conclusion ========== The specific use case this patch is addressing is to improve swapoff performance in cloud environments when a VM has been hibernated, resumed and all the memory needs to be forced back to RAM by disabling swap. This change allows to better exploits the advantages of the readahead heuristic during swapoff and this improvement allows to to speed up the resume process of such VMs. Reviewed-by: "Huang, Ying" Signed-off-by: Andrea Righi --- Changes in v4: - added a "conclusion" section in the patch description to better explain the importance of this change (no functional changes) mm/swapfile.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 5871a2aa86a5..f8bf926c9c8f 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1937,10 +1937,14 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, pte_unmap(pte); swap_map = &si->swap_map[offset]; - vmf.vma = vma; - vmf.address = addr; - vmf.pmd = pmd; - page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, &vmf); + page = lookup_swap_cache(entry, vma, addr); + if (!page) { + vmf.vma = vma; + vmf.address = addr; + vmf.pmd = pmd; + page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, + &vmf); + } if (!page) { if (*swap_map == 0 || *swap_map == SWAP_MAP_BAD) goto try_next;