From patchwork Mon Jun 17 23:11:36 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: 13701543 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 12482C2BA15 for ; Mon, 17 Jun 2024 23:12:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 772028D0008; Mon, 17 Jun 2024 19:12:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FF8A8D0001; Mon, 17 Jun 2024 19:12:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21AB98D0008; Mon, 17 Jun 2024 19:12:11 -0400 (EDT) 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 DD7068D0001 for ; Mon, 17 Jun 2024 19:12:10 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 59C08C015E for ; Mon, 17 Jun 2024 23:12:10 +0000 (UTC) X-FDA: 82241930820.14.049AF03 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf11.hostedemail.com (Postfix) with ESMTP id 7000240011 for ; Mon, 17 Jun 2024 23:12:08 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RRMcu7ve; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718665923; a=rsa-sha256; cv=none; b=N/gjDe2WwEl5yuxekH/V8Uoo0fLGCerPMnxsgUtJmKlaEGxLKcpKkA6SbIuMlXRpmbe+y0 8W5flqXMQudKpYLfEU6/TyKAW+OmuL15KTTZ3IKOmb3HxPengBGswxSwse4gnbWosXemtc 28t32RP/bzF6ogjc3IE9CnUqp3lY7D0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RRMcu7ve; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.172 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=1718665923; 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:in-reply-to:references:references:dkim-signature; bh=nnwDyt6wGwOClIOvqiBxhppNdg8blfjOHv9/ogWAFm0=; b=5vObuOkptTNtnkxHRmTshylxl/PmSkByNJkGZFZnCGmM65SCKX4nmMZhQqS4vpOdpO4eti 03bepZ5Mdm9OAw1SD2/3B0ILlhw4BmlzZ5vLlwxB50TCWVt+f+bRyEqiUA1kEMhML0yypU HDYHUo0FKCb7ES1L3GdNspx2JFg2VdI= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1f64ecb1766so34717655ad.1 for ; Mon, 17 Jun 2024 16:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718665927; x=1719270727; 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=nnwDyt6wGwOClIOvqiBxhppNdg8blfjOHv9/ogWAFm0=; b=RRMcu7veTnByEKSfDYO570W683xtTTQNgSSaAvuL8KW4T8Mk62DoDMeY/zi0gacRgm iawz/q7QdMJzCnjYpz3f4HY3IySkl83N00U8oAiqmBkyIPGyAByOfziThhFGh8fiBJcO hfurc6zXwfgf8dX+Y1OK8scDBquKrMmuNkOUV9CW7x2P0inZuqpnwT/l6TKAEAb6E1bh nv4FPXIS4MTTXCr9fVsdGG9M8CrGHDDdhLcCDcdEh9WzP5BsY0j6UDTa/2gbe7r54yHr TYuBTN4Q5VbGcMqrguFJNRm+MGLDgun5yX+VBhnb6ZBw/tj0OZJIb7alDz5yCGNN2LMv JaTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718665927; x=1719270727; 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=nnwDyt6wGwOClIOvqiBxhppNdg8blfjOHv9/ogWAFm0=; b=COyvRV813Bv9Pr0Szgi46BlhhnXKYGZwdq/af/xSynSBAgX+GpQ2oChIdsno3TBqEI oo8FLMrWQTG2qu327VWkJRYnTtrQQ8eb0l1fkxYMjo/S1HXrlE3HaVXXYmQPq4SOePRM IZcq4W5d+VdqhCTCwg18yTu8+1arrWMHkzGsrnQjwrI6YnD6F64swFvf2CB6luPc20cR la24cdQFz4YgWtejVOETBtvyj469Z7e98mb2BwV4zU4BLBJ71b5A7VFZv2NwtDlsWxQ5 Ot35friSn2YDcYDca15nSdV3e6usDixCfXoyT4GezLRK5/sxrDRSEY2GPNP05lqMHQRp CfqQ== X-Forwarded-Encrypted: i=1; AJvYcCWRWfZU6dGSv2q/c/l+2Mud03CeZK19C25Mjv1F1g/pPLNLdSUYHa0qP0NZQxmw5FzWWTDYJ3XBXLvo2c6L3MU2oOE= X-Gm-Message-State: AOJu0YxaxSylMBCcpPVbwkbGLGMVL+SKZiMEvErccCjrRR3lFz0waqMP I3tdEAoZaFL+oygfIqUBDJnMz2+Pns2Os511LQ0U5bQUG/7+cJsq X-Google-Smtp-Source: AGHT+IHq7JHs/BU8iswnoca29J8kwvvCjAMVVEyjUdJyYgosbrLVDRGHuiSVkNgDJ8EmnZbLmCegbw== X-Received: by 2002:a17:902:c086:b0:1f8:3f13:196a with SMTP id d9443c01a7336-1f8628062b0mr84791845ad.45.1718665927204; Mon, 17 Jun 2024 16:12:07 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855e5a0d5sm84658605ad.43.2024.06.17.16.12.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jun 2024 16:12:06 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, david@redhat.com, linux-mm@kvack.org Cc: baolin.wang@linux.alibaba.com, chrisl@kernel.org, linux-kernel@vger.kernel.org, mhocko@suse.com, ryan.roberts@arm.com, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, ying.huang@intel.com, yosryahmed@google.com, yuzhao@google.com, Shuai Yuan Subject: [PATCH v2 2/3] mm: use folio_add_new_anon_rmap() if folio_test_anon(folio)==false Date: Tue, 18 Jun 2024 11:11:36 +1200 Message-Id: <20240617231137.80726-3-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617231137.80726-1-21cnbao@gmail.com> References: <20240617231137.80726-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 7000240011 X-Stat-Signature: xmuueybpetuamozq9tg43an1uhk84oq9 X-Rspam-User: X-HE-Tag: 1718665928-234476 X-HE-Meta: U2FsdGVkX19JJcrWKh+F7cN6AMJKVF92LhGseODLzYlAw/yxO+KD93YRvxMuXpw0hZb0YdZ97oSJqMKYnLJWmfuLgVTOQdpsePeCV3erA6DWb6XTI6b02gsJuQyN3DQ9CVKT+6tjI/M/RZowSb6S6O6jumo9DlFtf0fMzsz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ehqyPlWIkgNTD42BEerBmZ38IiyG7kb3IC/7tDRNCYgHACO4zGCEnNFWaPulaA3PwnQXwftg/6mUTSqH2O3tq2Aj1htamOfhh5uLd4nxoQklyzhgJWLh2vxeB/OFeAoh45QmJdQJCtle+dMuf+39JOiT47cYNnEhWPijQ0ud5bMC+1aAdFgamUObrRnDwDYIJHmQcWMjWsdCTBMddjmCF8hMby+raaU4+hf/b/qXexdZFZMbJ8pZ+6fiAZyLEhDLjh2V5eq0Ue2Lz5DOs2a++aF/T7prC2rXeuRzJgMQ0oq2j4G3/vOl6D 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 For the !folio_test_anon(folio) case, we can now invoke folio_add_new_anon_rmap() with the rmap flags set to either EXCLUSIVE or non-EXCLUSIVE. This action will suppress the VM_WARN_ON_FOLIO check within __folio_add_anon_rmap() while initiating the process of bringing up mTHP swapin. static __always_inline void __folio_add_anon_rmap(struct folio *folio, struct page *page, int nr_pages, struct vm_area_struct *vma, unsigned long address, rmap_t flags, enum rmap_level level) { ... if (unlikely(!folio_test_anon(folio))) { VM_WARN_ON_FOLIO(folio_test_large(folio) && level != RMAP_LEVEL_PMD, folio); } ... } It also improves the code’s readability. Currently, all new anonymous folios calling folio_add_anon_rmap_ptes() are order-0. This ensures that new folios cannot be partially exclusive; they are either entirely exclusive or entirely shared. Suggested-by: David Hildenbrand Signed-off-by: Barry Song Tested-by: Shuai Yuan Acked-by: David Hildenbrand Signed-off-by: Barry Song --- mm/memory.c | 8 ++++++++ mm/swapfile.c | 13 +++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 1f24ecdafe05..620654c13b2f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4339,6 +4339,14 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (unlikely(folio != swapcache && swapcache)) { folio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE); folio_add_lru_vma(folio, vma); + } else if (!folio_test_anon(folio)) { + /* + * We currently only expect small !anon folios, for which we now + * that they are either fully exclusive or fully shared. If we + * ever get large folios here, we have to be careful. + */ + VM_WARN_ON_ONCE(folio_test_large(folio)); + folio_add_new_anon_rmap(folio, vma, address, rmap_flags); } else { folio_add_anon_rmap_ptes(folio, page, nr_pages, vma, address, rmap_flags); diff --git a/mm/swapfile.c b/mm/swapfile.c index ae1d2700f6a3..69efa1a57087 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1908,8 +1908,17 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, VM_BUG_ON_FOLIO(folio_test_writeback(folio), folio); if (pte_swp_exclusive(old_pte)) rmap_flags |= RMAP_EXCLUSIVE; - - folio_add_anon_rmap_pte(folio, page, vma, addr, rmap_flags); + /* + * We currently only expect small !anon folios, for which we now that + * they are either fully exclusive or fully shared. If we ever get + * large folios here, we have to be careful. + */ + if (!folio_test_anon(folio)) { + VM_WARN_ON_ONCE(folio_test_large(folio)); + folio_add_new_anon_rmap(folio, vma, addr, rmap_flags); + } else { + folio_add_anon_rmap_pte(folio, page, vma, addr, rmap_flags); + } } else { /* ksm created a completely new copy */ folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE); folio_add_lru_vma(folio, vma);