From patchwork Mon Jan 6 03:17:09 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: 13926915 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 C67FAE77198 for ; Mon, 6 Jan 2025 03:17:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 305FC6B008A; Sun, 5 Jan 2025 22:17:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B6456B008C; Sun, 5 Jan 2025 22:17:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1585C6B0092; Sun, 5 Jan 2025 22:17:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E96946B008A for ; Sun, 5 Jan 2025 22:17:32 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6D5F51C85B1 for ; Mon, 6 Jan 2025 03:17:32 +0000 (UTC) X-FDA: 82975566744.05.2A756C5 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf30.hostedemail.com (Postfix) with ESMTP id 825458000D for ; Mon, 6 Jan 2025 03:17:30 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZGFK3WwG; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736133450; 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=IPcqXqaTIBo3x10L2Zu0olCr3NMK/mAmrBeyqDgRKcc=; b=5JgCP+H6OtMbic4eUiguPVf9BwQcR3400FVhjSD4JhwqoMzb3WjnEYHGppE3JfYfDPgz0B X+fMHoNN2/eNl34Azz54+NUob1JkBGn8oublZlb9AgMJfNghp4sVWgUdzIhzW6vEeG8AvS XxtBy0b5UtO2X1clKVJgKekPYrcQzx0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736133450; a=rsa-sha256; cv=none; b=yCfwieEeedU9zE2XBgCg4E2ohq5R4A/Atsd/UPY9H4L3oIFDNZw22qm+TfRm4OyMEQutBm Id4VLf/ktY/jlA7Q6AwCNScd9rtbauStxo49AKD+zwb8S+RadalEGGcXxFHZoQqMG1oeGx /mPfu6X22kB1UvxAUckbzdyrGnHq3TE= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZGFK3WwG; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=21cnbao@gmail.com Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-21669fd5c7cso200331355ad.3 for ; Sun, 05 Jan 2025 19:17:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736133449; x=1736738249; 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=IPcqXqaTIBo3x10L2Zu0olCr3NMK/mAmrBeyqDgRKcc=; b=ZGFK3WwGo2O85DTh4l4haJbNUGJsCSEefnUI3zUAi4tC4wz/LQ4ioRjWfUTcI9EvPk I/22/WFbSpA8W0bVbH8otsqHJdDCxMzKy57ykQ/CXoE8JZaxqkQCr1r5DvxUNKl0iYC8 f+BNGNkbKY2+6Io/xrAIiP9dyu9BQhZSVvLLFf1aFJCuJemXKEa7pBKIxdNY9ud4++b3 eRUfiA7ff9hQoJ9oy7nYw37F2SOBmhwbJJp1GEJLxMHs8c2IuloKZnmWJP6MmTnzUcJd /DBtZfwZGYnZtRlmoKXTt7EJS+7Y6ptrTwsZRm0LslLndCSm4GXp2xmgEew8tcJM2GAa 0mCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736133449; x=1736738249; 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=IPcqXqaTIBo3x10L2Zu0olCr3NMK/mAmrBeyqDgRKcc=; b=ZPglNhrMOHBXtEPvd8osjrejgmNkDy6NN5DLHdfqmDDUFSyvsXTbMrBRFmITcVkUQV Wu0B180GelukR8a9dK5Mntivo+ZVrmOLPOnQJELHiX1OCJh1JpVJkZzo9PtIX3+bwB/G pReqM4VhFhtq8ZkIk9+hL0Hr4d0ZvY8dNj4NEz6Rf1F52H31r988aPA4YOuMxfH1StyC 0ko6I8Vsd1JyTs+78LO+DTEjUBWKB9n0WwlLkanJ3m0BIfBg7TRjYR+2IS0SiqdyaOlK vIWhaGR5z9aagGOBg//nZee9j/egTzIFMFrQ4/kfz8w10D/zH+QEBt2Yz/HIrS0I8dhu 8qwQ== X-Forwarded-Encrypted: i=1; AJvYcCWAyBXomNSCwu7fSPKlqdGKBjdFhieJ7nMg60TA1O/3QwiJHX72bFH+u2YZSHor6/Js6X24R5zq1w==@kvack.org X-Gm-Message-State: AOJu0Yyw/7FtkPnrVBCPBjr3OwCeaBsVtZ81YxJW6sTB1dHRXUdnSKmD W0JoZcuGWvTkXj42Kaa03mJicKxQVvLSQuOKu3WsxlEWqLM4YqEi X-Gm-Gg: ASbGncuJccglpu6BI2ofsOLsawVJjcZQVlu+f5o2e3V4pdj12xcHgNyxCCv0crudfEB V84dKiXVvXGvTQnFO97HHe9NbDddMCiVZMnRmTf3UEP9KXHG/cwYr0Se9ReNIFxedbGhzjPE+0g 85f2oiwmSmLldZXT4eHJ+8v76YLwXRprjuJ1/pJt4xJZy2BWrMfaPlpQoPwXTXZlovPqRU0VFFp Ak5it8Ja0s2lpSeI/VX1uNrRs+PKIfeVf550kg8fTAsPn1/15GJPF4mWpJcs0Pt/kuwoKxvvC0s eHOetgW4 X-Google-Smtp-Source: AGHT+IGUcWPca8wiecs9NrdPulrIQkPrhd7bNDv5OvIdHp7U8jnY2abuTVpzr1KXpxDPDL37Vp5CVw== X-Received: by 2002:a17:902:e852:b0:216:386e:dbc with SMTP id d9443c01a7336-219e6ea1d22mr898673135ad.13.1736133449268; Sun, 05 Jan 2025 19:17:29 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:af65:8200:a54c:5ad3:ad27:edb7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f2ed62cde6sm38471399a91.13.2025.01.05.19.17.24 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 05 Jan 2025 19:17:28 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-arm-kernel@lists.infradead.org, x86@kernel.org, linux-kernel@vger.kernel.org, ioworker0@gmail.com, david@redhat.com, ryan.roberts@arm.com, zhengtangquan@oppo.com, ying.huang@intel.com, kasong@tencent.com, chrisl@kernel.org, baolin.wang@linux.alibaba.com, Barry Song Subject: [PATCH 1/3] mm: set folio swapbacked iff folios are dirty in try_to_unmap_one Date: Mon, 6 Jan 2025 16:17:09 +1300 Message-Id: <20250106031711.82855-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250106031711.82855-1-21cnbao@gmail.com> References: <20250106031711.82855-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: nn575en7mxqcgtrod1hkpkgqs5qarxi3 X-Rspamd-Queue-Id: 825458000D X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736133450-178871 X-HE-Meta: U2FsdGVkX1+3cEwWzllUW8lhVdCsKMHXRQSLy9fsw7SRn0LTjrKOCaiYnik9wyhQFqDIeHhndGbkkpt8B17YoxPPTWhEsh0BfTEP5THRVgwkvka42VjbUb9vYyyJSWKtAPMX3ziwhLSrZ8fyOFE+P6Gqdv3y4z474Yz3xB6owWHlqo804+oo2rU3KR+a01Qd313KRNFztI1OieV3nG+wnA/auye6YErm4kFjpiZBrhaAQE1PZVBsevO5T4bJwCaHSDZNh86CYgWn1MmpzZVoLOg5HjItfYcK7S5k6U8LTmiOAz9qthqPZIeHM9HNVj+Lw9SlU11Vq99SbwKUvO/YZ9RryxbfRnVhzEUJ/GOLflq/xSr425PD3DN5Dj+3YFt27W7KRwj/OpjeMSftNNgSRFZD2O1PYhPJgAMMA9UuO5AafuEZj/TIWPvMccq4rusRS5rRwAU/Y5tF8rkGX4foqJpy7RT6dczjfNPx9KqKivAoEcoH+oSZexQEILjfE0Dfd3K1reeJtiulSbZ5E2hyG0oupUzlzqjiXhXbYexN7nZ69zj7OdmbOgmpkTb4+V1vvuRyohv+VOeqhVoNS5d8KC1WFxVnpyM80pLbaXI67+9RmIVZ5Dff7Ful59QGF2fBzxIaAzPKQ9dsQ0t8eRxLXkU1LDbdTB3ICVYRWL3QH5VPM8gVmbT3dpz0AraK362i1J+gvK3eWnRaSxQ7bRiIAGvKluJPyva2LjBzoSy0kqQ2nuBdyiGdKbUtTNRjVyWllNfKhwD7tM6yocjSIwICnbLhYzyeXFlE9o6Hbo2Hy4aK0Dq9rsnO7knvi5pPL941i2hgDhh1qZnDyaAKSeKWdB2DElPcY2a3+yfqP6+pz3hTp7FXtIM6nSFdykR/DiNYZcCO4xhIDF1RFpGTXBJGJYnMQvi3TEPwyeb5oeCE+7ycYDekgTE7MMaFSg8cg9zq5+GTV5LwLWIxUPnOle1 pnWonHxJ 6EpoFU3jfhzLE5CNv+j8WpN4faToM3qm2o2u7MzluQ9YBMwravkjIEEPjXHyEKc0biPNSSvj8ceYkgTZYCiH2N5aeWyeoaz2RaZVXnugPbWqBEfpfywPnpywvusD4Rzx7Ste+yQvK8BUjYHn5TgXgvG4YpivZ8VHY+IuGF5//8USTAuI4EgrlNj/G2Limty4FulqGxtmVTMv+hQWpK3ws6DGZxygwrFcbBqE95gT5fGsIjcJ1YbASJD5k0B8vIHNbYLurtNfJS00sipa0YEp3BB25jFze9XR5lKmqHN9CSAgt3kNHGeRfN5wMFAOsBIKK7xbGtCMvlgjOLOnhQ488dyfXcj6IxL2oQxzdu2zzPOIcBVVlzmz0I8sCuegSWNoUR+ZlQCo07xBMGh04CLK126jpCzbJmGE51E73KvjrmEVXlQSPnQqavXDosZl/tDS7qO0u4rWCzvL4fLGErhMEyFNWIF5N4rYZ+hJLkJn2pTXx+jE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000013, 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. Suggested-by: David Hildenbrand Signed-off-by: Barry Song --- 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) {