Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp969679pxa; Wed, 19 Aug 2020 21:57:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx33oA8wjCoeyHXTrHDugwpZ/MZcZGVvdWWxnDuTasVRoQMefMQ5Meif4QQk3EnAp0qpXFS X-Received: by 2002:a50:ba85:: with SMTP id x5mr1216203ede.38.1597899430166; Wed, 19 Aug 2020 21:57:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597899430; cv=none; d=google.com; s=arc-20160816; b=Me1OGE1S6A49RG1zHuM7wQ5MrvYSB1yGrXOsZgughAwcKXMa9QIdBtrKngXrk+opyx r1LW7gt9FLscFswymhRxm2sa2qrrroGSOH/E1cOPdVyGpnR3HPPkpiMxjS5V0GOaGeGU Uh/P5IPp26/Hi4r0m7fkT76mLsxfRRWKXbDStQAjyGRZUf0D/nYYxsKLZSr7uqK0aAI/ uX8waZyxnUpMTTETToAQA/gj857RfDJ9Mxq/mOHy/wnW6iaFQpBKXLu3LzryMbGePpua jEaPrNzUk6oEaT8Rb+EIU08oPJKtUbrOB0c7q7jFgdIAituzWPkA050aZW0O1jOOMJkh fE9Q== 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=5+VTUkZxM5oNmypKkhPEwWwEokFyqkG+NZrtkR/m294=; b=KTnYEilVlWqtZqcv/oVJfBqz63O41a1KKW5UJJ6yVx2PYrhAsYnlj7sJnaTfbqs1cK H+HHtQHAMr0TXpcB1dVixcDQ0v3PSOshSRZFZa/feOI9NfkRDhyBF+2pdN0ZGT4k7Fua Oaepso0MFDpYEIDxP3iTE3ZOf+yhYVS5Bnr7HS2CtUAQas4AS8EPbicalzCtM1wHg/dN jdWGc3F8QoVh5wAdv7fjb3I3EnEjfXTIdmk4q0gRiFp7RoyCXZwWRvJM8oV0BbqnTtTw yDgW0jV4FSF5NBo968I50MkP1umNXiXt0j0Odb/jF7iN9R32YM3gxgCF+yP5zCSZH8oW fsDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zb7Txy+f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f27si596472ejt.13.2020.08.19.21.56.46; Wed, 19 Aug 2020 21:57:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zb7Txy+f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725838AbgHTEyP (ORCPT + 99 others); Thu, 20 Aug 2020 00:54:15 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:50625 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725772AbgHTEyM (ORCPT ); Thu, 20 Aug 2020 00:54:12 -0400 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; bh=5+VTUkZxM5oNmypKkhPEwWwEokFyqkG+NZrtkR/m294=; b=Zb7Txy+fp+vxslDJ6GB8hwDM3iZgxSi05FGUTnVz5kSJQidCogqbbWi67M7al43pBJbjzH bSW6vqhr+wURzZa83Mhh8W4wSfLetn8Gftsgsefn0cuVuileGEJabfkc0W68iqTSSGRuvs BMwaYX0P+IPhWRA1muhs26Q4moBD1EU= Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-259-60R8k3HFNHiNfGm_uPeHsg-1; Thu, 20 Aug 2020 00:54:08 -0400 X-MC-Unique: 60R8k3HFNHiNfGm_uPeHsg-1 Received: by mail-pg1-f198.google.com with SMTP id h36so541028pgl.14 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=W5CWBR2VRSh9BoxVZSS2MDe/eCIBFzJb6eRk1WqZfUhX0wDAWqVsPVQo3UJZlbRZJM aXfXxCYxEfKcqoX3D/EnQObg0L3ISdd5khSUYb8z4h9i/KOMLEnFTfyPcZyAg7lss8jo A22CK9EGOl/xQBBP2eb4LIRspC3mZ8LE9ZKxoZC0EIJeyDUl6hvBFVo726shx0vQ/2dK X+CZ7z7eCp+uSuVGOS8cXLGrJteqGOdxoRkTEWvdta26oYPUEPRYsMZba6khSq2y7hxD OR+nKwfXWpLRwi3UA8h0cArer/zPhTb8VHi43h3zctn8N2SzoOazkRSeICJc2EBskFvr YbIA== X-Gm-Message-State: AOAM531v7JrsIOuAB4dbGHveL8+8dDVTus46uSAgL9tueodS2bP3Z87j bdjNyg1dosMbxQHl7+GagoNNq/K5F1oRgmMPcF/nUdGmfUOvKz2v9qpFWf002iptlolI2J8S/tw ngfMs6qi/7VlveaL0VfiXKLAV X-Received: by 2002:a62:5a87:: with SMTP id o129mr954888pfb.204.1597899247837; Wed, 19 Aug 2020 21:54:07 -0700 (PDT) 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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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, -- 2.18.1