From patchwork Thu Aug 20 04:53:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gao Xiang X-Patchwork-Id: 11725407 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 1508C618 for ; Thu, 20 Aug 2020 04:54:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D49CF2078B for ; Thu, 20 Aug 2020 04:54:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="O2AmTQ1w" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D49CF2078B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id CFB626B009B; Thu, 20 Aug 2020 00:54:12 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C84536B009C; Thu, 20 Aug 2020 00:54:12 -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 AD7E38D0003; Thu, 20 Aug 2020 00:54:12 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0154.hostedemail.com [216.40.44.154]) by kanga.kvack.org (Postfix) with ESMTP id 8DE1E6B009B for ; Thu, 20 Aug 2020 00:54:12 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 3D329362A for ; Thu, 20 Aug 2020 04:54:12 +0000 (UTC) X-FDA: 77169730344.24.brick49_250a3042702d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id 0B59B1A4A0 for ; Thu, 20 Aug 2020 04:54:12 +0000 (UTC) X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,hsiangkao@redhat.com,,RULES_HIT:30054:30064,0,RBL:207.211.31.81:@redhat.com:.lbl8.mailshell.net-66.10.201.10 62.18.0.100;04y8q5kbru8hbimn4kro8y67pa4b6ocatofscmns16iq7nsk7zz17jp4qpzunf5.cyxp9f6ker5wb8hepe6fgb3gk53q13ofer7x87q9yfkc9qpc6xjptcogp5hoby4.1-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:1:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: brick49_250a3042702d X-Filterd-Recvd-Size: 5942 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Thu, 20 Aug 2020 04:54:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1597899250; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:content-type:content-type; bh=5+VTUkZxM5oNmypKkhPEwWwEokFyqkG+NZrtkR/m294=; b=O2AmTQ1wLPEsoeW1LIpsUsv/luRP19mbmfclp0y2aZjsKzyMC8HZ4Vt68E8IJy5ReJkaMc DLicAkLO8taQ1aHDJDJNNrEQid7yEwBIL61A9cTnALOEH0l0s05PiWoS+uNmtFYj6VZq+6 9yS1CQXdqFzRralgZm+mv7yd8bpbaro= Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-259-1cICd3EUPKe3uqyrK4Z9-w-1; Thu, 20 Aug 2020 00:54:08 -0400 X-MC-Unique: 1cICd3EUPKe3uqyrK4Z9-w-1 Received: by mail-pl1-f199.google.com with SMTP id v10so819667plp.5 for ; Wed, 19 Aug 2020 21:54: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:from:to:cc:subject:date:message-id; bh=5+VTUkZxM5oNmypKkhPEwWwEokFyqkG+NZrtkR/m294=; b=o1kpgr4EU0Px0z2+uW5dx4aTtUrHBM/LAeuXprphPosgJ0lfHPLtblSIMk9iXmMBAo mNfVKVtrAFqGl3OdHQe3YBMAzuKTyorf+r7eyY1GYcNfUie6+UE5nRKrnEajEbImhrQw QBM/sZswO5nWzESTuaNyBgPX4w27N2aTdLLbOU9NgNt2/RQ/aYvzcYAYwQJNh0WvC0Zg 4F/QdKI0rud1FmGFwYfqBFWQD81YimdNfU80BtPQgI8d+kH0VgI65nNOi7EsRKkyd+qc 0YY2Qp+UilmlZ/eXppUhNnxTodDMgMdYyqRBXQFxCQV1Tzjgs/G9Efe1WdfUniQtNgfB Flpw== X-Gm-Message-State: AOAM530cNTqhFrNFniK7NdFnMctlCC4n9LcnSz2zZmRVVu3JC95UKFH4 C/ILTK+yyiwhqkJKdjDJ/INoAyTFnOgzZ8N1PqFrkxhi0Q8a8l8UrnNaOLdctBDQbG+e4d8t4s3 d6QGPzH57/Rk= X-Received: by 2002:a62:5a87:: with SMTP id o129mr954896pfb.204.1597899247841; Wed, 19 Aug 2020 21:54:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz+eEuz/lXHLcXBgHgYxHWdcGo0cQru9Km40vz5Mcw+NKZD7EVUPE35k7tZi3N+gJsdzQjD7g== X-Received: by 2002:a62:5a87:: with SMTP id o129mr954876pfb.204.1597899247575; Wed, 19 Aug 2020 21:54:07 -0700 (PDT) Received: from xiangao.remote.csb ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id q16sm1017900pfg.153.2020.08.19.21.54.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Aug 2020 21:54:07 -0700 (PDT) From: Gao Xiang To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Carlos Maiolino , Eric Sandeen , Gao Xiang , "Huang, Ying" , Yang Shi , Rafael Aquini , Dave Chinner , stable Subject: [PATCH v2] mm, THP, swap: fix allocating cluster for swapfile by mistake Date: Thu, 20 Aug 2020 12:53:23 +0800 Message-Id: <20200820045323.7809-1-hsiangkao@redhat.com> X-Mailer: git-send-email 2.18.1 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=hsiangkao@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 0B59B1A4A0 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: SWP_FS is used to make swap_{read,write}page() go through the filesystem, and it's only used for swap files over NFS. So, !SWP_FS means non NFS for now, it could be either file backed or device backed. Something similar goes with legacy SWP_FILE. So in order to achieve the goal of the original patch, SWP_BLKDEV should be used instead. FS corruption can be observed with SSD device + XFS + fragmented swapfile due to CONFIG_THP_SWAP=y. I reproduced the issue with the following details: Environment: QEMU + upstream kernel + buildroot + NVMe (2 GB) Kernel config: CONFIG_BLK_DEV_NVME=y CONFIG_THP_SWAP=y Some reproducable steps: mkfs.xfs -f /dev/nvme0n1 mkdir /tmp/mnt mount /dev/nvme0n1 /tmp/mnt bs="32k" sz="1024m" # doesn't matter too much, I also tried 16m xfs_io -f -c "pwrite -R -b $bs 0 $sz" -c "fdatasync" /tmp/mnt/sw xfs_io -f -c "pwrite -R -b $bs 0 $sz" -c "fdatasync" /tmp/mnt/sw xfs_io -f -c "pwrite -R -b $bs 0 $sz" -c "fdatasync" /tmp/mnt/sw xfs_io -f -c "pwrite -F -S 0 -b $bs 0 $sz" -c "fdatasync" /tmp/mnt/sw xfs_io -f -c "pwrite -R -b $bs 0 $sz" -c "fsync" /tmp/mnt/sw mkswap /tmp/mnt/sw swapon /tmp/mnt/sw stress --vm 2 --vm-bytes 600M # doesn't matter too much as well Symptoms: - FS corruption (e.g. checksum failure) - memory corruption at: 0xd2808010 - segfault Fixes: f0eea189e8e9 ("mm, THP, swap: Don't allocate huge cluster for file backed swap device") Fixes: 38d8b4e6bdc8 ("mm, THP, swap: delay splitting THP during swap out") Cc: "Huang, Ying" Cc: Yang Shi Cc: Rafael Aquini Cc: Dave Chinner Cc: stable Signed-off-by: Gao Xiang Reviewed-by: "Huang, Ying" Acked-by: Rafael Aquini --- v1: https://lore.kernel.org/r/20200819195613.24269-1-hsiangkao@redhat.com changes since v1: - improve commit message description Hi Andrew, Kindly consider this one instead if no other concerns... Thanks, Gao Xiang mm/swapfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 6c26916e95fd..2937daf3ca02 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1074,7 +1074,7 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size) goto nextsi; } if (size == SWAPFILE_CLUSTER) { - if (!(si->flags & SWP_FS)) + if (si->flags & SWP_BLKDEV) n_ret = swap_alloc_cluster(si, swp_entries); } else n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE,