From patchwork Wed Jan 15 03:38:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13939796 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC348C02180 for ; Wed, 15 Jan 2025 03:38:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 709A3280003; Tue, 14 Jan 2025 22:38:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B9C7280001; Tue, 14 Jan 2025 22:38:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55A3F280003; Tue, 14 Jan 2025 22:38:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 370E9280001 for ; Tue, 14 Jan 2025 22:38:37 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B2B0FAFA82 for ; Wed, 15 Jan 2025 03:38:36 +0000 (UTC) X-FDA: 83008279032.20.0CFE102 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf25.hostedemail.com (Postfix) with ESMTP id CF722A0009 for ; Wed, 15 Jan 2025 03:38:34 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="SgalNc/9"; spf=pass (imf25.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736912314; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KJIQvsAs93F2Rgx1xFn4Hzm1URQ2cAp3NCo21K6rE7w=; b=qMFa92j8GBud8yAdQYUakoEzwvwzjCAGyDj5c9gBupLB+OB6UINWN501STH0ciCtIeMh0/ BWjJzdwoc48rrP7XCwFthVDucRnpmV+8IG9gFAXowReQ5cjiPY2HN4PRhfUIWBZEIIoaXV HqktBwuxFzZQfNWIagGysaIjwHifSCQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736912314; a=rsa-sha256; cv=none; b=I5R8O27wF6fgxz/U0BDY41Cl8uoLh2RCVpKdXvZrrDU94UWBQkhlBfyyZ8BbTMziC0H7bV 3qU7WHdEVa7Rstfmc0vb4+ktUsMol1+1EDy1yzrG3R8hUKcgUHTpSgRceXxXAXZQMi1ToY pM817u+mfgyjnHNVirZCd/zZc67DOzo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="SgalNc/9"; spf=pass (imf25.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-219f8263ae0so102948085ad.0 for ; Tue, 14 Jan 2025 19:38:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736912313; x=1737517113; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KJIQvsAs93F2Rgx1xFn4Hzm1URQ2cAp3NCo21K6rE7w=; b=SgalNc/9uznh4qcfmpUtF5wf4ttNYaFA8FjPP5WTmMZKuWXe6Fmf50zT+UjuVsJDLW fr9N/P+vxu3tv2f9kSoBJ2jPkZLHYYFKA96H3V/7M98TD+bTK3rXcukGG5XeyQP1/MT+ IZ/8Y3laDnVsFnb07rCE/zlh+LRJkwdjMCXe6LwXfmhxr26EqTf+FGXpP5F6j6EufyzR DmjothK3yWNTrq6YLVscTTa8DNk+/4Mb/TFhhhEx1ZMilnmkZaArq2Zb/aP5CwXWUSRC 9iFp7bW1jvq6gGYM+jLOVDVEamgzO5KaIYHZdRrZ+E/ms2WpLTQh6EQU8a3Ru2BvyIR1 Hziw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736912313; x=1737517113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KJIQvsAs93F2Rgx1xFn4Hzm1URQ2cAp3NCo21K6rE7w=; b=ZMd4w42E11pUzFTFFvztmoKQDg2JRXNtXHcp1QBoZaCVFd5uyRJkpd2z8fTykcD1or 2T8qVBBG7M8lo9hFEFnOrFFxQCsEmeiYyZ3K3DFYYSe94W50cJdIewDMwd/Rb/GS+r6H qeCH6bF6nOEuiqOtqs/idDXfDl6VvFg4ruA4hR05a99z5gawjcIBmRC4zwWMVbiLL2uz 6BwPuxokbTi8B6aPeZ0ukUq+SigY0ZYJyIrs16fQVKRYHZmM63oly41mF6vpteAiyzKi SQjlLi71ThvcPtXEJgSmL2rfuoQlrlHLj4TwcXdFea4u8AxEOKi1SyiUKLQdCc+JjeX7 NiYQ== X-Forwarded-Encrypted: i=1; AJvYcCVbFhTUAY0V6byNqMg987iqWywpxuxp282Ef7vj6cB7XUqcN01JJRMIV91BxGBtriI3zCmGkBGaOw==@kvack.org X-Gm-Message-State: AOJu0YxwytC0PTfrUNJgXoXbM2XdgD1gw1cDW1/VBvmXhqWJz4P8HJzb qAiqN+u5i9gCxZcK5JJjFpqF/KO/jKNkmu00b3hAPuPbFGRX3RkE X-Gm-Gg: ASbGncvPWnLuaPkAGP6Nlfni5ruTellrigmQRsPfiXeqKb849hM8MSuWj73q/1q0c2G MLeRfSQS3CNYQSkl2eW2MphDFCq1SdxYLr3Sduw15xBak0kHq8+mjo1wXHDFQwFgMViJQU75grw r+D+76bxdmG68mHoHv6xba/deQ38bHbLUkI+BuWHp27oDJspB+x3FbKLVVDPV6YgXajz05Ow4Zm XTXGXkNHNYCrdC/PkExdYM0mC49h5MdxtTm629cvNrfcf2Mytl1daxN4qhC1qQkM/vL0cWSiN1Z 3HiaOzW1 X-Google-Smtp-Source: AGHT+IHnFl4K6iDBiLirFwWAnLa88ULHZZM7psfa+Le3bhG1J3ARcy1prtquvYLzB7/JWDl2R1Bcjw== X-Received: by 2002:a17:903:1cf:b0:216:39fa:5cb4 with SMTP id d9443c01a7336-21a83f67c50mr435295355ad.25.1736912313634; Tue, 14 Jan 2025 19:38:33 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:af65:8200:e5d5:b870:ca9b:78f8]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f10dffbsm73368195ad.49.2025.01.14.19.38.26 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 14 Jan 2025 19:38:32 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 21cnbao@gmail.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, ioworker0@gmail.com, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, lorenzo.stoakes@oracle.com, ryan.roberts@arm.com, v-songbaohua@oppo.com, x86@kernel.org, ying.huang@intel.com, zhengtangquan@oppo.com, Mauricio Faria de Oliveira Subject: [PATCH v3 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one Date: Wed, 15 Jan 2025 16:38:05 +1300 Message-Id: <20250115033808.40641-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250115033808.40641-1-21cnbao@gmail.com> References: <20250115033808.40641-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: CF722A0009 X-Stat-Signature: wqkkb9gspaonccjoweb1mfx6wzym75sx X-Rspam-User: X-HE-Tag: 1736912314-495973 X-HE-Meta: U2FsdGVkX1/WmJ1c2Kz+rQFD3U2mdMcrL7w78bf+Kmw43PHoVXrWkgIrsxa+e+yE2PlUdrkRipPd9Y71Q+7iHNXYREKThxprdKpIaDMZwwGwxMLqER5VOJrvvdcCSW6u3inKvIwVjMHer/SkiMLmFdxBzbIsBFIqCR/UiGiLbyIa0q2zCsKuHZZmo1DvKucSMBtYJDVT0qBOwaMpQrPmvacbeovLKv9pBQJ5jyePRf5LpPUoxelzaDLQesJTbJr6NDoVXE41YFCMpFl+fFkgSt7yV5o8+dQP67YTeqkxLo7NU8N/99woVOqQwHg2MD7ZstIm+xJu9VgqL4Vs+mwMYjjOXwGQg1L5GfCi6dw1Ft7ih5E46zDGujyr16lAHB61elDQf0wFQuNdqO3mqsMeRNZIklxGmcbz0f5n8Imz2R58jwCuo9dCyEz1Sp8gtExgKuyGrsGjZqb208yDx/f4IyzLglAAXj1agMa7pek0LEfgLR9FbP4fPngjpOQeJLdKetlRakerhiCb9Zwm2H0Bkb6hjn4YRmyGcVo3r8aDTwoPcsg5YA0blDglAH/JP3XM4skQXPvkSxU+z/3NO2vB/Iqc4vmAtH65AETyGQL5KaShmtkuw04dFVfUvmQC/haWL0sGNuq72dtUR9wpHTYdHJ11U/lDOglcvBWXwPTeSnY/vxV1gZwgOM8Nh11aP+pSY78YoKw5MmDWFg8m2FU521OcLYXNNwPaDGUayrEWmFc9LwXMebTXRDCizAtQAusmv1D77TqWqzUHSV3BkJN87DLWWDcc+/L+GrWqWzCvNw6DFPL+POgByBqcPsz4y5eM3dy2fFpqRZMcG1kvtSRjxbFHEz/Mk0FTOEgZoB71/pmCj/1szeCSD/MivZk1acqFaVLls1xD3PpsAVDvjFVopWeuxt+bsP/G6G3NlpF84tJRWffjqdYijB1TnWEKZXjrpX8WQIYn79CW0BwX7Vq TmWGld2f Wk7zq/nw2vRopC5yvjDgHJWtwRM3snSHEwwbhSJn8AykOxrpHIyHCuvdd/rEnGabep/237y9IskCInuOkPu/AVfhqPv5ErKNUAau4YmmtntiolQqaFZuGKhbcL8OC/4n84sA8/BCPm45ZF3DCNx/TciJ6KBq+lL18B86eC7ltfa1J7FICMxeGTAyPwHqsKh6nvLzT3frteho9xj5gNYpQlo1r/HmYM7Yn3CgXIjT/NQMASaJJqnqe0w/MbXYm0p5p7AliXYBgX29SefuY95z57DufKVT4Zu4wPiXEG92DoqhTFU3cmien6DLj6OQfaYufV3VnCtotI3792c6GgmNoHWcjR0h6/N+aRV4G70CAjfYbuv9mktUpCru3c1ZiENZENtN2jZjK7Uwxz5M5LSqssmLNN6CAI5tOXYlhLu1mto0jorJ5l0l0E9lDTvL77aye4dnHVqlMWliYknyajhRBDEY3by1POGJuEJ/7yCQkoJMcLM9NcAprkzsoUcW0Imgy+SczRzf5p8U4dqch3nYpS1fW3Y4Y4qOseR33UF3E9h8gDjFh9A6C5RJQWA== 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: List-Subscribe: List-Unsubscribe: From: Barry Song The refcount may be temporarily or long-term increased, but this does not change the fundamental nature of the folio already being lazy- freed. Therefore, we only reset 'swapbacked' when we are certain the folio is dirty and not droppable. Fixes: 6c8e2a256915 ("mm: fix race between MADV_FREE reclaim and blkdev direct IO read") Suggested-by: David Hildenbrand Signed-off-by: Barry Song Cc: Mauricio Faria de Oliveira Acked-by: David Hildenbrand Reviewed-by: Baolin Wang Reviewed-by: Lance Yang --- mm/rmap.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index c6c4d4ea29a7..de6b8c34e98c 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1868,34 +1868,29 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, */ smp_rmb(); - /* - * The only page refs must be one from isolation - * plus the rmap(s) (dropped by discard:). - */ - if (ref_count == 1 + map_count && - (!folio_test_dirty(folio) || - /* - * Unlike MADV_FREE mappings, VM_DROPPABLE - * ones can be dropped even if they've - * been dirtied. - */ - (vma->vm_flags & VM_DROPPABLE))) { - dec_mm_counter(mm, MM_ANONPAGES); - goto discard; - } - - /* - * If the folio was redirtied, it cannot be - * discarded. Remap the page to page table. - */ - set_pte_at(mm, address, pvmw.pte, pteval); - /* - * Unlike MADV_FREE mappings, VM_DROPPABLE ones - * never get swap backed on failure to drop. - */ - if (!(vma->vm_flags & VM_DROPPABLE)) + if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) { + /* + * redirtied either using the page table or a previously + * obtained GUP reference. + */ + set_pte_at(mm, address, pvmw.pte, pteval); folio_set_swapbacked(folio); - goto walk_abort; + goto walk_abort; + } else if (ref_count != 1 + map_count) { + /* + * Additional reference. Could be a GUP reference or any + * speculative reference. GUP users must mark the folio + * dirty if there was a modification. This folio cannot be + * reclaimed right now either way, so act just like nothing + * happened. + * We'll come back here later and detect if the folio was + * dirtied when the additional reference is gone. + */ + set_pte_at(mm, address, pvmw.pte, pteval); + goto walk_abort; + } + dec_mm_counter(mm, MM_ANONPAGES); + goto discard; } if (swap_duplicate(entry) < 0) {