From patchwork Mon Jan 13 03:38:58 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: 13936678 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 98296E77188 for ; Mon, 13 Jan 2025 03:39:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3377D6B0085; Sun, 12 Jan 2025 22:39:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2BF7A6B0088; Sun, 12 Jan 2025 22:39:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1119B6B0089; Sun, 12 Jan 2025 22:39:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E46C26B0085 for ; Sun, 12 Jan 2025 22:39:28 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8B37181A85 for ; Mon, 13 Jan 2025 03:39:28 +0000 (UTC) X-FDA: 83001023616.13.1B14B2A Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf05.hostedemail.com (Postfix) with ESMTP id B02F3100010 for ; Mon, 13 Jan 2025 03:39:26 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=drXssHAZ; spf=pass (imf05.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.174 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=1736739566; 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=46D4oal8mFPlkoWVhiKKKEAsT5bOWVsfEegbHcgtxRtOvVU1pjqT4W8Z6cInzhtrBYTQFl 2QHKUhBn/V09Xc2na9nyBzyX/r3O9RWfjq2t197AmQDvxI2uDMUXQNZSRkhbax5mQJqGVk FyVbdrPjfa4JhSKTKyPfeuF0k1Vr9Z0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736739566; a=rsa-sha256; cv=none; b=rwnsts0PMKY1SFoW/uJelse9TiPAwq8J77qB1NNf+TP8ycPBG66B7+sbPFdQNxyNgEyqW5 KiZHEF7I9H3ZWdxi8sWM+dW5BlWVaOHrsKDRk6W0CwpHetqgvvisWFZCrdgAT2FJSsAjF9 n0suM7QdImN07D/Lg2aeYskWtLnvILI= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=drXssHAZ; spf=pass (imf05.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-21649a7bcdcso62730135ad.1 for ; Sun, 12 Jan 2025 19:39:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736739565; x=1737344365; 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=drXssHAZUruRYspEs09d+T/MqsaMWjxMdTNLICgvVUYoUNIsfri65JuF3kVg2odvPR lenbBTwsNNtuUckQVMgWWwOH/wo0y4VA/AsA1UAM3qGISsQ7EEi4CdsN2ob61lnn+bBP 6q0v98wtyaV2yd8FjSQ2YJyvC5d7ka+FsWMqriF6OgXeKSvsazbHMgvLuKgrekJV115S QdHiwNucRn4pQI7s+3/IdJNZzpTMyYcqwn60WtroIEii5JOaEiAHZTIxTCubfcWaM6sK u/UCZ5eG1RuaOrmkt2EKb8aJTBuoECoMOOcji6DMm13QlFfWprQuhLF6US+zAxIDrKZT JuRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736739565; x=1737344365; 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=Sbqmw9K5IIM2MbAQhDRZ6DSDjwHdP0/uBubczRiA1KR9t79bXOuVJGx1gX2bp7CIon HSF73ZC9tT8Hfgjx4JLpb7ues4wM8WvE3Xwm6BM++2XmEQHMvrbwLRgri9FXYQu7RcPX 6d/3gV7E9F1nqwgQolkd32KVQ6VHE2rgI5jfvDlgBucMrduEo0mOcJ9PmxXjauFbjUfu +IhjUR2INE0kUUuaahYS1VwBnTRi1xbjniPelWMboJf2Nh/B7asAIIbsKFPZvL9UTDsc p0cdtqa0jnxR0IEwlpDE6FHRv0SxFGJ/ML7Mo30iCL6X21DEWa8RhqtbhYEW3dgYfQgP SzpQ== X-Forwarded-Encrypted: i=1; AJvYcCXY2mkb0N9m4zPXVdkzZUz3vjyEYhMIQuUTIpKm5fUYFz68R86iUCjooDfSjOV/AgOsc4Iuleqr9Q==@kvack.org X-Gm-Message-State: AOJu0YyPYBAZUJ2Z0N34MC7XEmWdxz7YQbiarn4k9MaJuH28X5nAOsRL yeYv8AJPhAYR5ZLuvAR8sSmiUoe2UnHDPCZyW+qPq/LJxbeqT7fO X-Gm-Gg: ASbGncvO99bYdiI3S1MP2XsB7Fht4Te/I2/JdQr0QVThnaAaAWFxRTjJjSztx1xLIdN Nx70EXPmJXE5ObH8n7B/D4eOvnzP72Y0Ay96M1Hr3T9FdK+OLzz9Ypj4QnWlS4tFrN9NmgkQ+xM 06REL65gKRba5ibSGxZBoAj3inEukhqPbyrXbvRXxvdMzvNDLgwR0Bb+gNDiaq0cYw80Bx5hUqA CSgUAcgf4tmbjrAonH/YCeaAUD+TpasOadk9WjPkEHrDOUsRjktTtl4Ey3/1pFYgYpaKdVgc53m Xoj7eqGK X-Google-Smtp-Source: AGHT+IHhm0qTbSZrWVTQXUqJ8k0+E+zfqHExh6ZNZDkVUuwdyBd3fwtM3FFGJhBiv45zTGk0mUeVsQ== X-Received: by 2002:a17:902:d2ca:b0:211:8404:a957 with SMTP id d9443c01a7336-21a83fc0619mr292344105ad.41.1736739565411; Sun, 12 Jan 2025 19:39:25 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:af65:8200:39b5:3f0b:acf3:9158]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f25aabfsm44368405ad.246.2025.01.12.19.39.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 12 Jan 2025 19:39:25 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 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, ryan.roberts@arm.com, v-songbaohua@oppo.com, x86@kernel.org, linux-riscv@lists.infradead.org, ying.huang@intel.com, zhengtangquan@oppo.com, lorenzo.stoakes@oracle.com Subject: [PATCH v2 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one Date: Mon, 13 Jan 2025 16:38:58 +1300 Message-Id: <20250113033901.68951-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250113033901.68951-1-21cnbao@gmail.com> References: <20250113033901.68951-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: B02F3100010 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: jakf6wyo6rqwggi38rxu5yctnji5m3fh X-HE-Tag: 1736739566-644248 X-HE-Meta: U2FsdGVkX1+lUCNXDuARFIYSiOnMgUqPpkp/lmSEXQaB7KtzXG3OoMUBUn5BBPSjg1+xEw0tDfIo3GNC8249eq6NfZ/gkr/2WWkAn4n4+Up11WyHGwWQm3kTYdy5cSedB1eG/HwRE29mUiirBcvthNRbTeUfPyQXMQbgvGkLF+Qeo4Qkb6eeT4xHZRPqV8Qo7SqsdfhBFCh1cmvMLcChKIk0iw3mNintFjBx89dQ7Hn1kvgH4gzmQ0mb58xw1d+OPSAzNiD/QlU0pIoj6GCLfvbzuVOiyDJjJN3uLBT4M4ET/KpY5Cc9TOaGczRGawG1wrAU+SD3eNyKm+ZAm4ePurBRWRi1gV29XFZfnAS4lYzINSG68fr77i+DSkZI9aIgt93M97s3S+D5K3u8D4gRLHO5UWcIX+ubmcFzTR4KwTyR+wCfUlgExnQFtpfRBW0gA5GZgu6fQr/Bzl+H+7+LhV1a+lX4YxiA8INronrzCxHkPIMGjJ1nwjjRTfo2InqGAC4mkPyMyI/kQt6KYGS+G4/DmcRjLWdklEY9v4RwzhLss/4jsM0Sc/pgv77VCSq/V7KaR+UoYvMUIAGHYdRgEV7iG3G/r1LSmKchF44or3EF2kdiNzDztORIZbg1ycEJqzeo9NHJOqC+vjVSX15vCjVBi806xP7qTeO4HyFnf+Ktt/sGj4XkeMeGnuM1t3FufJUbXE0NjjYnhRxldd9nXMpzmvK+iQFaZUeJaHMepyVrz1tu1S/ilgOz0mWNoghcg1FqR7sbXe9if2JQk/s5brgNHNV8EcA58RmoqwPsO225D8dpy1rfa6YfCcOhx2Lme7wJWx2pPKrt6p4GY7p1IUZzoDBRL9fwu85avKZbBoXKgxnsrzvngu6EVPDHzqz6roUFLVAd5c1S3p9XWXYM1hdgA+3SQVAMu4t7wdV3lNMR/b7edXsbqWzZiPtqP2nvuJXiqDL2A+Dbt6HRDbJ Z2Au/atM wLqDyS70hCC7cCUUHqMNOgqcAP8IFuPlTr29nPFNWO1PFvreS0EaZ82FC5rTfLJKNbkBXVul3xVtLsXP6fqzBLw7qQ2P5z6s0VAIFOP5IHRspCA8CTv7QJGIE/vAFNchvjJOdF9Z2EkWSEVh8I68nwWohYMVC5caj5DmEOVsz1q5iZb9P2z4fc4ORlpeUaSa0UI9JG0re4f2aa0verpYC/IPpOZl61elFqxTeTH1b+NzwlSs/sFeIzqm+QhQrfgKw0UMYRAqNV0uUQ+LdIRJI3BLLx0K7qre9VpT1SPEMXp+c0AeT8gK9SE6LEZNGHTV9Ip2MUKMPbV0H+L8LTiHadwZdcG1NJriRfiV0ms+a1lSo7o9auSMT+qtqQV00o3O+Fe+r64qc2hWgxbnhIk/tYO+HwiKWmpK0Tn2/YrERAU2tRUFHxfytkjGxBPPKUroQ+729iMQD/SkIDNk2NLSIme+Li71quisI7q8Gm3WfJjv5f+M= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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) {