From patchwork Mon Mar 4 10:37:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13580386 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 74731C5478C for ; Mon, 4 Mar 2024 10:38:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D39676B0072; Mon, 4 Mar 2024 05:38:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CE8A96B0074; Mon, 4 Mar 2024 05:38:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BAFBB6B0075; Mon, 4 Mar 2024 05:38:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A5ACB6B0072 for ; Mon, 4 Mar 2024 05:38:56 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 58BE9A060E for ; Mon, 4 Mar 2024 10:38:56 +0000 (UTC) X-FDA: 81859008672.30.39CF2E0 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf06.hostedemail.com (Postfix) with ESMTP id B1FF8180007 for ; Mon, 4 Mar 2024 10:38:54 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NWmkoxZM; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.174 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=1709548734; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=u9xib0zn6DxdjtfkNrv9JmBdK0ttpN1YNsPL7qmo/GU=; b=Q/XhyS1pM3wEyt7o060o9GxzCH9ecxP8Oa8Masprr5eWN5YYsF9dN80W27apvZn5z+v1jV xw/Feq7MuM2XNflab0n63VBLRA4jXVJPl2Fa7bl3/xzSNT5DZ2zqnFgeBSVoh0EagHqlkZ 5sfPRwzsf8JYEEXh1OuWF26Z3+dihdc= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NWmkoxZM; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709548734; a=rsa-sha256; cv=none; b=x5sOBZoigA/91s4aPXtZ0Kw4Kia/XATFT1nf2wtRKQaygkHGc4YVKj95yDf+H1xqaJtcC7 9dwYs+PWTXJC4VB/1iWncu/aM/QqVIuu4/4MvG7JD26wGSD40QK9EMkPtlg8NNXIfOIVTq 4sxlxD5b2nftT5/+M3vJo7E8VaSHerM= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1dba177c596so25900965ad.0 for ; Mon, 04 Mar 2024 02:38:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709548733; x=1710153533; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=u9xib0zn6DxdjtfkNrv9JmBdK0ttpN1YNsPL7qmo/GU=; b=NWmkoxZMFDf6HLA9e4XPFWr33YGU2OyZAwl2fI+pW+YVNkW7fuWh0xX1C07f9ae9SJ mLnpT5Z7kSTt6Fn69wmPESYszyZpBAxcN8nDFdrnXnv659gehhr0e8LLvcEQopqHTWC4 ejXta6ZqA1Ymid0sXOEgG30uZuQWASMnXqI4f0A24e8AvLSHH3hy9Nz6H36W21+7F9Gv 38PZnfp/A0fNLIL67gZYgJbucHcMSUu/vZK5Cd4TGCkBU8Fb08XndJRmsD0q7fno6jWR 2QMlFt9itvg2Z/pq6UYGygRsr1anpq6Lo9Vl1duIrggNlQw+wO5bkUkukeORA3eqFWgl HiqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709548733; x=1710153533; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=u9xib0zn6DxdjtfkNrv9JmBdK0ttpN1YNsPL7qmo/GU=; b=hZWVDBF+9NnUAKoqJaNihSfbI5RPol1GYEdNMnwqGCAkzG9aH4cF39BZ2U7qKwIkmD Hgg9T57gimT/BvLqFqcShcartFuxyFjWvWvDMn7pnkSM+kIFrXmeIiU69QQOZrT9Z3xs +7JCg1Umv7Ul66/FDd5Oj87nVpgCvtfY2mW4j1SyKHUqPALvVkw2ai3z5H98v75dbSrc JE1NRyBpKcORkT9+42flCK8D0Nx1XSx6uM09gM/sHJYLIflpBa65y+6gO9VI2N6Z4i+U R5C5CXEaMW5sdMRvZNw9SpN0E6Q3dSWEWX90VXXoD/2ogFrTgszS/u0qoeqkVSFYkPAy Io9Q== X-Forwarded-Encrypted: i=1; AJvYcCUGef2iJt7T7898bAdYE/MmTJkAtMC+lOVjX/chxuhWIM3OxbwSUfNRKiGZULKuhZla48i4H6ucTW5dLus9AjsrFdQ= X-Gm-Message-State: AOJu0YwdDXlUsx6gyA++VUGhx8fjjdvEftkojv9Ke8Fcxpcz3/LdXFYq Hu00AgG7/VeqtG59tT8l19O31MFoTBJ8pDY7kVrm3qnJE6eKYUxR X-Google-Smtp-Source: AGHT+IFmoKVAtF+GXy5ODBZzD42hce3IaMAp4LlRX2Yzk0I94KqRWvyUY7YAzw9qGGvI48UNdNdKag== X-Received: by 2002:a17:902:e54a:b0:1dd:4cb:cc57 with SMTP id n10-20020a170902e54a00b001dd04cbcc57mr6344173plf.0.1709548733322; Mon, 04 Mar 2024 02:38:53 -0800 (PST) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id lh6-20020a170903290600b001dc23e877c1sm8201654plb.265.2024.03.04.02.38.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 02:38:26 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: david@redhat.com, ryan.roberts@arm.com, chrisl@kernel.org, yuzhao@google.com, hanchuanhua@oppo.com, linux-kernel@vger.kernel.org, willy@infradead.org, ying.huang@intel.com, xiang@kernel.org, mhocko@suse.com, shy828301@gmail.com, wangkefeng.wang@huawei.com, Barry Song , Hugh Dickins Subject: [RFC PATCH] mm: hold PTL from the first PTE while reclaiming a large folio Date: Mon, 4 Mar 2024 23:37:57 +1300 Message-Id: <20240304103757.235352-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: B1FF8180007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: nbjr4i4zfm9j96ba9idhwbqj6ecihtfx X-HE-Tag: 1709548734-673439 X-HE-Meta: U2FsdGVkX19v2LoaYcDRCGYr1aZwcRnIOggr/WrEcu91tu+iman0XqR0k5Z0YH59p0+coaZld98krjWUur4vpqUArxDtNvcjJXg/HBLIehYnROrOguT5XJQMHEhh7ALZPbacMRYuxFAl4q1bAHBc9SPk0WICZOd5+Ze+gWHeXKi5BQq+KsRsb3M4xIOy62zqOhY8GTl4BlHExbA7jfPDD5rc2iL0nJ8gq52fCk5ttrruJbPRO4/zkPY1N+/0lHjL50r+W34QvpXJ3MoD6hQnc0x+nA9tO9D+CBrnL5DQDrCQBw5cXQyV0r9VVcsIQy2nTo+d6RqMtEflmC2Mc0J590KnbFpRtoa8fmOpy1i5G+D+yhTsLN++zxYfwZCPjK17nssQqBFVvLmEYjNP3hnS9aRZDgyCDbpSKcM9cVcwNUxyQgP/nVUfdEOJzJ6ycXtY/H2ghyuPph0Dqs/yfjHkIYNQzKvWF+O3ZLtl0U+hNpjK6GssJc7K7iLJzAt6sz4kA6iFiET+rMPGZYnWuvDOzZS7H2eIYTYQPtVqoUfMlpk2J7o8i7sKVKIU3H0PvvedV6AQhtF4UCmWp27w2R3ARRhT4ekzkavlgPYzLQOPOPVbGyDpZiR9E+u/NqFHai0v+fS0ah5RSgsitl8zL35gFVZoTN/thZyQuPJHW2LqMKRXH4bYlqLRoM0NTr5rmlggVZOsqLCGntDS1XuiWQ521jxK90SfPH4Fy02bziAr6pJypiTeVaiXjzW8D7C4+zFtRpK9F71vaZaIt8RyqRB+Z0uK+klwxl/198J5PjuE8VBiKGbKeBTc/vV9m9YPoLmfABbpnzocdGBY1U85ezYGnD6ST/qCGVP1J3fqNvrfbkhchLRpm3fD6tjAEEwUQGBHAHWuReUUinVi4jqb/v0iT22X0F5BzB3z6ieU6SAUlfEkcUgsLPs5+Ouoy8dTMyeodsUZkKOtOhVmn5hKAZq 8/ogah72 yUB0vAcPiiv+Vp991/2FwRR19n0JvRrgITaZh6E8Fbsy29CLR2MGNkPCgCBXq6LDeR1ImjwlXjzlKdTw= 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 page_vma_mapped_walk() within try_to_unmap_one() races with other PTEs modification such as break-before-make, while iterating PTEs of a large folio, it will only begin to acquire PTL after it gets a valid(present) PTE. break-before-make intermediately sets PTEs to pte_none. Thus, a large folio's PTEs might be partially skipped in try_to_unmap_one(). For example, for an anon folio, after try_to_unmap_one(), we may have PTE0 present, while PTE1 ~ PTE(nr_pages - 1) are swap entries. So folio will be still mapped, the folio fails to be reclaimed. What’s even more worrying is, its PTEs are no longer in a unified state. This might lead to accident folio_split() afterwards. And since a part of PTEs are now swap entries, accessing them will incur page fault - do_swap_page. It creates both anxiety and more expense. While we can't avoid userspace's unmap to break up unified PTEs such as CONT-PTE for a large folio, we can indeed keep away from kernel's breaking up them due to its code design. This patch is holding PTL from PTE0, thus, the folio will either be entirely reclaimed or entirely kept. On the other hand, this approach doesn't increase PTL contention. Even w/o the patch, page_vma_mapped_walk() will always get PTL after it sometimes skips one or two PTEs because intermediate break-before-makes are short, according to test. Of course, even w/o this patch, the vast majority of try_to_unmap_one still can get PTL from PTE0. This patch makes the number 100%. The other option is that we can give up in try_to_unmap_one once we find PTE0 is not the first entry we get PTL, we call page_vma_mapped_walk_done() to end the iteration at this case. This will keep the unified PTEs while the folio isn't reclaimed. The result is quite similar with small folios with one PTE - either entirely reclaimed or entirely kept. Reclaiming large folios by holding PTL from PTE0 seems a better option comparing to giving up after detecting PTL begins from non-PTE0. Cc: Hugh Dickins Signed-off-by: Barry Song --- mm/vmscan.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/vmscan.c b/mm/vmscan.c index 0b888a2afa58..e4722fbbcd0c 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1270,6 +1270,17 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (folio_test_pmd_mappable(folio)) flags |= TTU_SPLIT_HUGE_PMD; + /* + * if page table lock is not held from the first PTE of + * a large folio, some PTEs might be skipped because of + * races with break-before-make, for example, PTEs can + * be pte_none intermediately, thus one or more PTEs + * might be skipped in try_to_unmap_one, we might result + * in a large folio is partially mapped and partially + * unmapped after try_to_unmap + */ + if (folio_test_large(folio)) + flags |= TTU_SYNC; try_to_unmap(folio, flags); if (folio_mapped(folio)) {