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) { From patchwork Mon Jan 6 03:17:10 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: 13926916 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 E07A1E77198 for ; Mon, 6 Jan 2025 03:17:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 772746B0093; Sun, 5 Jan 2025 22:17:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7227B6B0095; Sun, 5 Jan 2025 22:17:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59D746B0096; Sun, 5 Jan 2025 22:17:44 -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 3D2686B0093 for ; Sun, 5 Jan 2025 22:17:44 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EEC2B1C83D1 for ; Mon, 6 Jan 2025 03:17:43 +0000 (UTC) X-FDA: 82975567206.30.A270EA1 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf20.hostedemail.com (Postfix) with ESMTP id 0A4D11C0010 for ; Mon, 6 Jan 2025 03:17:41 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="H/Idv45U"; spf=pass (imf20.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.41 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=1736133462; 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=n3DoQzm517FFIjqDvouc3tzKgfaa0QgvxVLolob4ICo=; b=alJsfqHZGdIhSD+jVsQpE8cI3xylIaj0Nk/04SCIzAh6/q/WVUkDsuhWCqrqeffq5jfdn8 i8yYQTLx2tRHcrx8oEZIw6Ji/J9RewTg8VyAEoX2nvm0HjaX/9sRrjuiyDp7+4E6bApNAK RqFSkbOewP6G6/sz2S4auQlUQA3nX1U= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="H/Idv45U"; spf=pass (imf20.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736133462; a=rsa-sha256; cv=none; b=bdbpjx8L4svNxmsSzIh9ptIQs/yk0zROykS2wBXC7cJtl86ckuvQiLRssoLfm+SmKv8pP3 2XVfWqFR7wq1Cls4/jU/hDggJnkJEULOS23tL7SGRCijMi5tWYuqAlxQgZs8nPduc33MJh jqTfSVk0szRURfUpjGaOIiHTTmEjtis= Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2f43d17b0e3so20346013a91.0 for ; Sun, 05 Jan 2025 19:17:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736133461; x=1736738261; 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=n3DoQzm517FFIjqDvouc3tzKgfaa0QgvxVLolob4ICo=; b=H/Idv45UeIakpL+4VfniHif95Tc5SQ6tBn/+Ef8FT2OR8HY7YT5bzSMLU1Dc/2n2Vg R5MZzDDH4DfmWKZGhkGIp4u7OCIYFeW5fVrI4Y7WG+Z3Nt28CmM1bNrujE59I4Uj1Hym B2l80iQ66I5pjqGQkYdWiuie6we8rLkXLJF29uwRHX1U/DKtRZaw1vV0mYJmHZAMnw5T XNm9zr43v3aWchWY3OKaHd7dliR0m2tpmy/b2ZsxXfESEmwGfNKh7HLCRcB6+Gl6x53G VG5CjxQuuJEiO9hSiFNtLMuq4lGmeRfJSA5ffeb4DsbB8+jrAC56tOXl422Z9amCpF8H VyZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736133461; x=1736738261; 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=n3DoQzm517FFIjqDvouc3tzKgfaa0QgvxVLolob4ICo=; b=NPqXS5z3NkauLdnU3wsEgJmu336lwuXk3mLWMTR+5C4AZMWiGHtymQEu3ioWfSfbrP 7nv+dI0lHHsENdJYvsQh8a3LUrr/rgt/+V4gHar5YSIxyQdgFF7HpeVx98WLEYhArvXI I68PlU+OPmHyJ8bvcZPNAoYWq0kv3USLAOC66nSeVr1cx/PXpanCLmkkwQcAqXDJ2ngd BqYCpOYe0DxoMMzyRoV5Ar7F3KT03Yz9LF8VTmxMM2jQs7cGGwLxdyp6paIzloseYQeU FAUHVRrFl0pK8BVvFyt1eKO6kX/0S14VtMF/A3iOYPHWiXTrcSl5gLGfhbWO0gV8dwUK eF/w== X-Forwarded-Encrypted: i=1; AJvYcCV9R14EeIp55rurQjbBuUSA59FjYb+8PCQDC5eZH/Obks/N0Axgw8Dr1LrpNUpAq5/KvmNRlh17Bg==@kvack.org X-Gm-Message-State: AOJu0Yy65cYs9j4ndClSMZXmQVkPginv/Vhy9LMiYudUtqE0nLkpY78f g8Yiv8lPcLThljDHmPdm/7aMk/TI26mu0zUXSGqmoewUOgPYZ6td X-Gm-Gg: ASbGncv5VSwnnbHu4PyNtsjGLunKmDlikIlU9DqI48POgOT0fGd316VIf39LvIfkmLO GQyybCGY/OqJ2jVMxNjydWrGS8yzSQiLAftpPLIKjtPJwwbRFyOqsenM4P6xOmmV7Nv/sh8iKkR +fAQEJDyjQI1UBTcuGAYA9dxmoXzLyrMpgmMz+ULNkbtq1InN4WCnGgP++ro1KhGutw3cTaJpVJ WJjBaOgcYBQJuS09tWOnah9ZrZ1N7wXq27LIlHhu4IkY4WqJz7QWZ2V9l2xNyUaNKLwX98/TKP2 CPDGK12D X-Google-Smtp-Source: AGHT+IFyfsnRq/TPlAXOEBq4tTLlzzCUGW+jGQSlThHUVYTr85agsMlhfIXWLMv/0CFjghXY79/T8g== X-Received: by 2002:a17:90a:e18b:b0:2ee:fdf3:390d with SMTP id 98e67ed59e1d1-2f452edc2cfmr70372123a91.31.1736133460760; Sun, 05 Jan 2025 19:17:40 -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.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 05 Jan 2025 19:17:40 -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 , Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Anshuman Khandual , Shaoqin Huang , Gavin Shan , Kefeng Wang , Mark Rutland , "Kirill A. Shutemov" , Yosry Ahmed Subject: [PATCH 2/3] mm: Support tlbbatch flush for a range of PTEs Date: Mon, 6 Jan 2025 16:17:10 +1300 Message-Id: <20250106031711.82855-3-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-Rspamd-Server: rspam05 X-Stat-Signature: z7kxp9bgxsiufd7jmgfdzgqdmad974qj X-Rspamd-Queue-Id: 0A4D11C0010 X-Rspam-User: X-HE-Tag: 1736133461-96255 X-HE-Meta: U2FsdGVkX194G4w95u7AFgyznl9ISuYwPBsREwe641n/sRWUyO2SSvfKMlIqIOPt4ISUaGK0F81c4r5UBakazk7UnpoiX8Ps75POg+EfMoTMf/q3Bg29HXGY/DOHqcOkEZ24jPSlBR0jP2n+sVCiG6hTIf0dpl3jDFov1FdsEka99dtS344ona/nLntHho+Gsm53MlMU++oLai7s9fSlDp+E0ycRPa+ICfHCEhQR2t2ADxbc6NX6bhTpWCDqHZgkAQlTHZ/XScy/bFT0C2ZD5v+izGfHp2EG3jlLH6+jUcVfpzVoCrsaXUkLKG9kP+yoNmKHGGUf9JnxRxGSzXsh5EHm9D06pflSoCFd4ie2v1LNmhB6NJlyLF1YXgidV1Bj6hXFM2AdjMqE7MuP2uX42sQIJUZtk8JQ5qdY5hsn1add0phPmxnosf9GE2Pq4sI5NS8SrkofRHZtCnK7AU/Oakx2tx5Cdff0rEBqoJxq7Jt0T0JmZLW9SNgmbuLEkrfWPvHesoK1t8DoKejKbDDakS+Oa6ngikDErjHB/165N30Z+vXf5xP/d+YHFeuavs4i9uYrPumfvrBOAFdJpCWyGAutgj8KbEFmoW37hA8VCkNQgcXeCAwrOn4/DqdG5TrNqkGqsmAMXtplvZP2ilBGrfsN3oPq0L3PG4uRE6bMz45+C1dVM8VcJaLVJsqSX3pGLmNtOkuotwJXNvjno3jgSuee556tE0wBrTL1bcdaLPOyw58AaNZUL3v+78/7j5azzwiV7N2F5actH2q5o8i9eDmOPN4pR39mN1i3PJTw/1+9zULEwhHilCl1r+53CN1XnnUZToQHIsZ6JEnCwBQ9hvPbRRctsOSkDT8Q1vfUBYBhga3TfWJKzIIHlTSGnScPw+o4lMC6+MG15OzPgMQFMTKxvROC2tyEFgI6ZJcWU9LcJWJEZ19DZtSKA50yJHDc01UPDcCcZOqv532cY+Y 0PzsFMxI WHJ7R3WeWP8IbF10gMv3IfdSeEHwf5vTkisKCkRk8q1fHnTItTa0SRvt4UXSoMM2DAMziKGFEx6sANxWzGB7WQSS1l1i7Xs7uI06NaOz+3HnDU3nia8FfsITW9uyZKd4K6Xfx4m+Tu8TkCggaDTWHdLI5B8xd8gMLc/uHL9eBQvkRRtgwv+AZB+2POWHugbQvXvUZm03/k3DWPVyrVjbEqOA5fpWw/YGdSPb9UubacQe2B1cP/w+gyWL7Kz4g0PJaLSDwiIX5qBp9vQuO3MR3mEUnOlWXKV60Iriyfkb7KAucDnEhkzRVLx0JQjtr+hKVLDXVTaGJelKco9KDS36HGqGAVFopWIg4HSAo+6gyvQNBxuPA+1AI4kJtUtfYT2hXRJssa3Jz1aQOgtz9qUwzRVat5VXVGcBo+/c4WmBHxe4G3AkuEvhJQh5cc47faeTTZcvl+gZ6GgSnX/0iVspOqgXLr+r8OkthBa70E1axDxJD4blJX1ExP354jH8+ONFt8UrgtLwNcq2zHQYFyW9Zr5p2JkISmUSSw0/fSwazzkxsVqmkLZEjnFn1MgaeELB0YJVejLINWQzEjDns6WtTkeP4oh2Mi/FxgJou2btzlktFgB6Ht7lbwRbYVaf3oTshqQ+1e4iK0Ta3NEI= 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 This is a preparatory patch to support batch PTE unmapping in `try_to_unmap_one`. It first introduces range handling for `tlbbatch` flush. Currently, the range is always set to the size of PAGE_SIZE. Cc: Catalin Marinas Cc: Will Deacon Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Anshuman Khandual Cc: Ryan Roberts Cc: Shaoqin Huang Cc: Gavin Shan Cc: Kefeng Wang Cc: Mark Rutland Cc: David Hildenbrand Cc: Lance Yang Cc: "Kirill A. Shutemov" Cc: Yosry Ahmed Signed-off-by: Barry Song --- arch/arm64/include/asm/tlbflush.h | 26 ++++++++++++++------------ arch/arm64/mm/contpte.c | 2 +- arch/x86/include/asm/tlbflush.h | 3 ++- mm/rmap.c | 12 +++++++----- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/arch/arm64/include/asm/tlbflush.h b/arch/arm64/include/asm/tlbflush.h index bc94e036a26b..f34e4fab5aa2 100644 --- a/arch/arm64/include/asm/tlbflush.h +++ b/arch/arm64/include/asm/tlbflush.h @@ -322,13 +322,6 @@ static inline bool arch_tlbbatch_should_defer(struct mm_struct *mm) return true; } -static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, - struct mm_struct *mm, - unsigned long uaddr) -{ - __flush_tlb_page_nosync(mm, uaddr); -} - /* * If mprotect/munmap/etc occurs during TLB batched flushing, we need to * synchronise all the TLBI issued with a DSB to avoid the race mentioned in @@ -448,7 +441,7 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start, return false; } -static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma, +static inline void __flush_tlb_range_nosync(struct mm_struct *mm, unsigned long start, unsigned long end, unsigned long stride, bool last_level, int tlb_level) @@ -460,12 +453,12 @@ static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma, pages = (end - start) >> PAGE_SHIFT; if (__flush_tlb_range_limit_excess(start, end, pages, stride)) { - flush_tlb_mm(vma->vm_mm); + flush_tlb_mm(mm); return; } dsb(ishst); - asid = ASID(vma->vm_mm); + asid = ASID(mm); if (last_level) __flush_tlb_range_op(vale1is, start, pages, stride, asid, @@ -474,7 +467,7 @@ static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma, __flush_tlb_range_op(vae1is, start, pages, stride, asid, tlb_level, true, lpa2_is_enabled()); - mmu_notifier_arch_invalidate_secondary_tlbs(vma->vm_mm, start, end); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end); } static inline void __flush_tlb_range(struct vm_area_struct *vma, @@ -482,7 +475,7 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long stride, bool last_level, int tlb_level) { - __flush_tlb_range_nosync(vma, start, end, stride, + __flush_tlb_range_nosync(vma->vm_mm, start, end, stride, last_level, tlb_level); dsb(ish); } @@ -533,6 +526,15 @@ static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr) dsb(ish); isb(); } + +static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, + struct mm_struct *mm, + unsigned long uaddr, + unsigned long size) +{ + __flush_tlb_range_nosync(mm, uaddr, uaddr + size, + PAGE_SIZE, true, 3); +} #endif #endif diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index 55107d27d3f8..bcac4f55f9c1 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -335,7 +335,7 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, * eliding the trailing DSB applies here. */ addr = ALIGN_DOWN(addr, CONT_PTE_SIZE); - __flush_tlb_range_nosync(vma, addr, addr + CONT_PTE_SIZE, + __flush_tlb_range_nosync(vma->vm_mm, addr, addr + CONT_PTE_SIZE, PAGE_SIZE, true, 3); } diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 69e79fff41b8..cda35f53f544 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -279,7 +279,8 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm) static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, struct mm_struct *mm, - unsigned long uaddr) + unsigned long uaddr, + unsignd long size) { inc_mm_tlb_gen(mm); cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); diff --git a/mm/rmap.c b/mm/rmap.c index de6b8c34e98c..365112af5291 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -672,7 +672,8 @@ void try_to_unmap_flush_dirty(void) (TLB_FLUSH_BATCH_PENDING_MASK / 2) static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, - unsigned long uaddr) + unsigned long uaddr, + unsigned long size) { struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; int batch; @@ -681,7 +682,7 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, if (!pte_accessible(mm, pteval)) return; - arch_tlbbatch_add_pending(&tlb_ubc->arch, mm, uaddr); + arch_tlbbatch_add_pending(&tlb_ubc->arch, mm, uaddr, size); tlb_ubc->flush_required = true; /* @@ -757,7 +758,8 @@ void flush_tlb_batched_pending(struct mm_struct *mm) } #else static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, - unsigned long uaddr) + unsigned long uaddr, + unsigned long size) { } @@ -1792,7 +1794,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, */ pteval = ptep_get_and_clear(mm, address, pvmw.pte); - set_tlb_ubc_flush_pending(mm, pteval, address); + set_tlb_ubc_flush_pending(mm, pteval, address, PAGE_SIZE); } else { pteval = ptep_clear_flush(vma, address, pvmw.pte); } @@ -2164,7 +2166,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, */ pteval = ptep_get_and_clear(mm, address, pvmw.pte); - set_tlb_ubc_flush_pending(mm, pteval, address); + set_tlb_ubc_flush_pending(mm, pteval, address, PAGE_SIZE); } else { pteval = ptep_clear_flush(vma, address, pvmw.pte); } From patchwork Mon Jan 6 03:17:11 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: 13926917 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 2537BE77198 for ; Mon, 6 Jan 2025 03:17:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0BBD6B0098; Sun, 5 Jan 2025 22:17:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BB226B0099; Sun, 5 Jan 2025 22:17:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 835596B009A; Sun, 5 Jan 2025 22:17:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 626C06B0098 for ; Sun, 5 Jan 2025 22:17:51 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DCF3AB0313 for ; Mon, 6 Jan 2025 03:17:50 +0000 (UTC) X-FDA: 82975567500.04.36DEBEF Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf24.hostedemail.com (Postfix) with ESMTP id E4CDF180003 for ; Mon, 6 Jan 2025 03:17:48 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Pbsbzhjv; spf=pass (imf24.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.52 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=1736133469; 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=n0uoTlJkT3xJASppF2RyQQeqHMmR9kwA4pIBCxskX/8=; b=PdB85/SkogMHpGcepsz/ZBqI31DpGE43TJ1c2JO1yABhYEbirX4NLtYLE4RI6uYZ22X3lG 933kXrju7o5lbMqTi1OUPYjpRmBqmBBm24KFilFFZBNw+TelOs8vwT/SefrkRPs7YjR+ny JagaQF+GgJlQCAmLG5XycDYZDxvoU5I= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736133469; a=rsa-sha256; cv=none; b=4lUH18NZO74N9dgbjBsuWErnes1UvV/bUW0nyvpEXH/B8reH8OKfKCxx2WBlxszHaWbUdy GwHfsyN6Fnn/enKTIIeJ/36vxGAg3G6TN8ulsCYORogiY4lxEEJMHkwEdfF2+ZJ5lYES16 lZHmzzbLMDK60nPV0YhIXgks9BBj9CQ= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Pbsbzhjv; spf=pass (imf24.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2efb17478adso19841248a91.1 for ; Sun, 05 Jan 2025 19:17:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736133467; x=1736738267; 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=n0uoTlJkT3xJASppF2RyQQeqHMmR9kwA4pIBCxskX/8=; b=PbsbzhjvO0FE5+cSEEjCp6MVYukTQd00x5q1bVycwG/LdLgZRzMpgFKF8RjNF7JH/i EzKrEyoRjaOqPoiB88G/plhJ7v8zfGrvMlINpO8jLALDN7syl9xpQ5hrrmQgrVpnyfV8 k2VkjHx/2brTrp2mihJy1Ddkg6vFYI7ZmmsDsiu9l72u2R/bjs+8UtIlrp+decCiOOFe nL27FDyyJ5jlhVtWAPKV7dKO+ssw8yxGuMffgiOZuEr6SJQSe1uyeoS1yJPmU5Q8A22S aI3RfjaM+tZG0m4ui9+HsxPptaPcbN4fPUjhlzmPiTGDefP/oV3YOt6q72kaSC9Km3y3 jSsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736133467; x=1736738267; 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=n0uoTlJkT3xJASppF2RyQQeqHMmR9kwA4pIBCxskX/8=; b=VoWvGH4bTJTBH+2p9sc6yTHwl2mahEGUk+yjz5pr8G5XXdwMRF+ypICcCDLc6q14ly +BqB7dtGKqz5Vg6Fm8WqZv8D8gjF/wdb6Nu0T1vrKbcGPyf1rMdY6ytydoXXlJS2v94O DCJONCtzoNSeBFdp4dcqS/+XFp8I9Bu6N42ToIiBdD3zCi9gVZepVVewFxQQFRE1i8iC iftAbTq80GzbKV0PpzAkUKPfvg3wcMwcYm3AGeGhLt5B32JFATtSL05DX1xXeAK7pY49 7rcO32R6GkUugV6OcszVKy+B0sHnJG/RN6E2zzZyKYObWiDa055YyRB+6Noih4Mu4tVh w9Ww== X-Forwarded-Encrypted: i=1; AJvYcCX87dZsEJgZ75XqINXjTy2rGhn+PPmnkbFrKBdhN6XJA1+aLK9DzYfHwwePd+4k6UQcVkQGVDqxUw==@kvack.org X-Gm-Message-State: AOJu0YwWfusFJiC81a9tPpTCw/y7aAnlzbVR1g0QBDrq/UCGuswA41E3 9n4pcn0JsV3mY9/9Bjqhiy6I9VagCUbP+aORk9NEVg2SwUrBL2Kd X-Gm-Gg: ASbGncsg7c9EMQPiGoKZi31XbRdLuT6ooe2oFn/vngXaXp1z/NbeODUMhBhxDXw+0Xd 85coWocttpIdNbK3enh2Dr71H8jQDtSEwGS+LQrSTEsl4W8Iv9JMpFpmH0Tk8fgmX3J305kMtTq K/IWDfdPZ9b82SReCltJEDyzAaZXQ6vmMYYqD0IhjqysPkx5li07hHHXCjHihpUvaVoCjV2E0Fe TNMAprrtM9NXgNEdfmPUghYlljQluqaZI82TzRG2HbMGlw41gsp+f7jCj4fEJEi3+u+TR6vFoZ4 8SkibML7 X-Google-Smtp-Source: AGHT+IHWP7zdolqMJNXBm1PXeI0GcejV+nCAy9WuJHL0SUoSVOj1myQWg2/Wp3YfIF+X1TNQvseTgA== X-Received: by 2002:a17:90b:540f:b0:2ee:6db1:21d3 with SMTP id 98e67ed59e1d1-2f452ec922bmr79065291a91.25.1736133467432; Sun, 05 Jan 2025 19:17:47 -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.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 05 Jan 2025 19:17:47 -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 3/3] mm: Support batched unmap for lazyfree large folios during reclamation Date: Mon, 6 Jan 2025 16:17:11 +1300 Message-Id: <20250106031711.82855-4-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-Rspamd-Queue-Id: E4CDF180003 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: he81y9nregr78xfwrcajxsgds7qb3jym X-HE-Tag: 1736133468-972778 X-HE-Meta: U2FsdGVkX19ywWE3Q63B4LAUa+G+X8UhF8E6EU0rT/qxcDq+bhgdU5V2uz7iBJDVNgx5xxtXZkNrOhTRGU0CXHhSJyf8KoKOIzng38KNzR3hbnrpprM9XpVlkKNDvPAXwFqpIOI6kowSetsrpKP/kCT48euhScjAqqk60YE8fZblpeQIrMbZTkwAOEtV48VHRQskK2WTWeg37F8Ggg71rHNoZnVaof8ZYNJKzv64QMEWeMpp60bs6DIh47UKbvwqGwoKT7ImPsyBY9NfmbxGmhBSKqDtDeWbn0e0ucLqj6sN7F2TzYwFzDjFBJ9GUOK8SYHxefJjh2WMLj9jLZfTlQwFDRovmZnWU4QoT/Ma1eto1Vris8FfNGp0tXY5Xsh9pHVOopgTCbSTVwFMEHYZJFXVns8M8IX56rorBSpc2uoW9+BkVMHm0kXNvn4bwO066O2IQmyMpXW4mmGWVPyCkkeS8dLhbF+7thEfakt+33qqXb2mnkU7l7INuoG9Kksas8SuMLfp8PgsCJamtiyWZZHxkK9wJ1KblOy5Z7ST01Y6bqzzHiVj/40VqE2dMa9jl9lSyVpBECqtJpBXc4FxY55dXTVgj9CGKaDGjh5XIvlvtYctf+f2IpDRTvSpybbdpsVL1UD+9hrJs7fuyJxYzEvbk1HtAWGXNXSRJ+RKKAY8nqoFBKNdPG2xvAlkg/Mz8tltmKQ/jhulqUXNZA9w7CAznnSy6eLZkOiAccr+aLIGZan4qkFz3qjBWhC/+Op5d5aa17KYXhgvLehGsq4me6zEzu/sY/KcRhGXI6c8lr03Hmaf5WeKc2C03ocfa8HXCiXu4YJFDr1jDf9ZWLrBkddd7C1/NThNTl7BbWoJhYKlGe9L/bE3FnJpjkmMekeOjO9zQYjr7pBdKodh/x3kKB61CCGfwWAnIlFvnkVX14gbXQzzK5w2OUD8JSRjKtIDLYgnDQbc3pa9CqPY4GU ns6zAmTr Q4gabKFvj7y06LYEIdsodtx+Y2LVhGxo4sxTEshyJm1z3EPfaap6t+dJGtpI5s9ZEutJcHjB5cCC+BL2DDjD7SpbQVznX1u0Rw7Ui5RWQ6OfqjgYh9BClDEVhvAQjoPoKXyWVPIR8+VNm11+BuZOcA1rHSBeysNO5Ed7NKP37rS5M3oydHmoy7liTfKhUNXysHRkqeME8sjvW58ksh8Igtt1/Pq7Xr/M8UL7f528VCGQWIGO2hX6ZhhIXi6PHKXmfIX5BCAaSRRLhErqO1z4qD5Y72KdB2hzJ30c0CHzVZEKMVYEr7ePwGw4ycsf92FJswMyEFSmiT+kkCSorDqAK7ZpADigMu1iMkM/pY66xf6mlWrgDt50mStXSpZ33dCHlwXvkwmOh8Ec4RtDe3ImaZY8MZ1SlC70b9FE/CGP/z3TvHUI= 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 Currently, the PTEs and rmap of a large folio are removed one at a time. This is not only slow but also causes the large folio to be unnecessarily added to deferred_split, which can lead to races between the deferred_split shrinker callback and memory reclamation. This patch releases all PTEs and rmap entries in a batch. Currently, it only handles lazyfree large folios. The below microbench tries to reclaim 128MB lazyfree large folios whose sizes are 64KiB: #include #include #include #include #define SIZE 128*1024*1024 // 128 MB unsigned long read_split_deferred() { FILE *file = fopen("/sys/kernel/mm/transparent_hugepage" "/hugepages-64kB/stats/split_deferred", "r"); if (!file) { perror("Error opening file"); return 0; } unsigned long value; if (fscanf(file, "%lu", &value) != 1) { perror("Error reading value"); fclose(file); return 0; } fclose(file); return value; } int main(int argc, char *argv[]) { while(1) { volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); memset((void *)p, 1, SIZE); madvise((void *)p, SIZE, MADV_FREE); clock_t start_time = clock(); unsigned long start_split = read_split_deferred(); madvise((void *)p, SIZE, MADV_PAGEOUT); clock_t end_time = clock(); unsigned long end_split = read_split_deferred(); double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("Time taken by reclamation: %f seconds, split_deferred: %ld\n", elapsed_time, end_split - start_split); munmap((void *)p, SIZE); } return 0; } w/o patch: ~ # ./a.out Time taken by reclamation: 0.177418 seconds, split_deferred: 2048 Time taken by reclamation: 0.178348 seconds, split_deferred: 2048 Time taken by reclamation: 0.174525 seconds, split_deferred: 2048 Time taken by reclamation: 0.171620 seconds, split_deferred: 2048 Time taken by reclamation: 0.172241 seconds, split_deferred: 2048 Time taken by reclamation: 0.174003 seconds, split_deferred: 2048 Time taken by reclamation: 0.171058 seconds, split_deferred: 2048 Time taken by reclamation: 0.171993 seconds, split_deferred: 2048 Time taken by reclamation: 0.169829 seconds, split_deferred: 2048 Time taken by reclamation: 0.172895 seconds, split_deferred: 2048 Time taken by reclamation: 0.176063 seconds, split_deferred: 2048 Time taken by reclamation: 0.172568 seconds, split_deferred: 2048 Time taken by reclamation: 0.171185 seconds, split_deferred: 2048 Time taken by reclamation: 0.170632 seconds, split_deferred: 2048 Time taken by reclamation: 0.170208 seconds, split_deferred: 2048 Time taken by reclamation: 0.174192 seconds, split_deferred: 2048 ... w/ patch: ~ # ./a.out Time taken by reclamation: 0.074231 seconds, split_deferred: 0 Time taken by reclamation: 0.071026 seconds, split_deferred: 0 Time taken by reclamation: 0.072029 seconds, split_deferred: 0 Time taken by reclamation: 0.071873 seconds, split_deferred: 0 Time taken by reclamation: 0.073573 seconds, split_deferred: 0 Time taken by reclamation: 0.071906 seconds, split_deferred: 0 Time taken by reclamation: 0.073604 seconds, split_deferred: 0 Time taken by reclamation: 0.075903 seconds, split_deferred: 0 Time taken by reclamation: 0.073191 seconds, split_deferred: 0 Time taken by reclamation: 0.071228 seconds, split_deferred: 0 Time taken by reclamation: 0.071391 seconds, split_deferred: 0 Time taken by reclamation: 0.071468 seconds, split_deferred: 0 Time taken by reclamation: 0.071896 seconds, split_deferred: 0 Time taken by reclamation: 0.072508 seconds, split_deferred: 0 Time taken by reclamation: 0.071884 seconds, split_deferred: 0 Time taken by reclamation: 0.072433 seconds, split_deferred: 0 Time taken by reclamation: 0.071939 seconds, split_deferred: 0 ... Signed-off-by: Barry Song --- mm/rmap.c | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 365112af5291..9424b96f8482 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1642,6 +1642,27 @@ void folio_remove_rmap_pmd(struct folio *folio, struct page *page, #endif } +/* We support batch unmapping of PTEs for lazyfree large folios */ +static inline bool can_batch_unmap_folio_ptes(unsigned long addr, + struct folio *folio, pte_t *ptep) +{ + const fpb_t fpb_flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY; + int max_nr = folio_nr_pages(folio); + pte_t pte = ptep_get(ptep); + + if (pte_none(pte)) + return false; + if (!pte_present(pte)) + return false; + if (!folio_test_anon(folio)) + return false; + if (folio_test_swapbacked(folio)) + return false; + + return folio_pte_batch(folio, addr, ptep, pte, max_nr, fpb_flags, NULL, + NULL, NULL) == max_nr; +} + /* * @arg: enum ttu_flags will be passed to this argument */ @@ -1655,6 +1676,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, bool anon_exclusive, ret = true; struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; + int nr_pages = 1; unsigned long pfn; unsigned long hsz = 0; @@ -1780,6 +1802,15 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, hugetlb_vma_unlock_write(vma); } pteval = huge_ptep_clear_flush(vma, address, pvmw.pte); + } else if (folio_test_large(folio) && + can_batch_unmap_folio_ptes(address, folio, pvmw.pte)) { + nr_pages = folio_nr_pages(folio); + flush_cache_range(vma, range.start, range.end); + pteval = get_and_clear_full_ptes(mm, address, pvmw.pte, nr_pages, 0); + if (should_defer_flush(mm, flags)) + set_tlb_ubc_flush_pending(mm, pteval, address, folio_size(folio)); + else + flush_tlb_range(vma, range.start, range.end); } else { flush_cache_page(vma, address, pfn); /* Nuke the page table entry. */ @@ -1875,7 +1906,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, * redirtied either using the page table or a previously * obtained GUP reference. */ - set_pte_at(mm, address, pvmw.pte, pteval); + set_ptes(mm, address, pvmw.pte, pteval, nr_pages); folio_set_swapbacked(folio); goto walk_abort; } else if (ref_count != 1 + map_count) { @@ -1888,10 +1919,10 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, * 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); + set_ptes(mm, address, pvmw.pte, pteval, nr_pages); goto walk_abort; } - dec_mm_counter(mm, MM_ANONPAGES); + add_mm_counter(mm, MM_ANONPAGES, -nr_pages); goto discard; } @@ -1943,13 +1974,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, dec_mm_counter(mm, mm_counter_file(folio)); } discard: - if (unlikely(folio_test_hugetlb(folio))) + if (unlikely(folio_test_hugetlb(folio))) { hugetlb_remove_rmap(folio); - else - folio_remove_rmap_pte(folio, subpage, vma); + } else { + folio_remove_rmap_ptes(folio, subpage, nr_pages, vma); + folio_ref_sub(folio, nr_pages - 1); + } if (vma->vm_flags & VM_LOCKED) mlock_drain_local(); folio_put(folio); + /* We have already batched the entire folio */ + if (nr_pages > 1) + goto walk_done; continue; walk_abort: ret = false;