From patchwork Mon May 13 07:47:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13663096 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 11409C25B10 for ; Mon, 13 May 2024 07:47:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A9C76B0275; Mon, 13 May 2024 03:47:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 859DC6B0276; Mon, 13 May 2024 03:47:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D4326B0277; Mon, 13 May 2024 03:47:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4F8686B0275 for ; Mon, 13 May 2024 03:47:58 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 17262A0CFD for ; Mon, 13 May 2024 07:47:58 +0000 (UTC) X-FDA: 82112593836.20.29CBD73 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) by imf01.hostedemail.com (Postfix) with ESMTP id 469D040007 for ; Mon, 13 May 2024 07:47:55 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dMlAbzA2; spf=pass (imf01.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.167.176 as permitted sender) smtp.mailfrom=ioworker0@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=1715586475; 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=CRqSGx+LZLYZmMM1dtm6DE8QjEfHEddJV2Al3d0Qb2M=; b=CARMaNwx8I4gn+LbC4qAJMRNaSsIBjjCnx4hlvtP8Zw7A+fu4ibc5CAfURIHgtVK/kzuwA 87warVfUFWLqy8Pg34rslIvOGBlk9Pon5K6FwYZz6OzTnBbbfrVlO/xpaMBA/dDczsVdn0 v2VlNlAyAZvbCKO3s/M/hJ4H8FNuH9o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715586475; a=rsa-sha256; cv=none; b=foIwuj+zEI5Bp1iTmyRYHVmW2ibyYd+7fSovuPwhZu4UZnNDhj+xMmLJgVuUFY7Al2GCMY UUma+Y6YfxIhJk1HW5RnCw5o3iedWUAw5mKsa0MtSYswGoqbPB7cFQcCOeAxWl8xAOVeDt 9oxEg3JnHayEwklvhhwgfSwKT0FlROg= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dMlAbzA2; spf=pass (imf01.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.167.176 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3c999d53e04so1650915b6e.2 for ; Mon, 13 May 2024 00:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715586474; x=1716191274; 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=CRqSGx+LZLYZmMM1dtm6DE8QjEfHEddJV2Al3d0Qb2M=; b=dMlAbzA2KYces3wPl4QvwJ4+nzVECQPXUIOggiZ2lRtusGVPoEJuc41ZPeezT+YiUT f0lYE37d8ZHjNDZ6gMGryaS4piGgNR0+JaKC4XDFgn9rUD1hWtmmHyI8k6uP+97D8wNU jNmaKGfQnrUh/6fFB42QOFHrR6yaJwBoC/V0esuVyu5ZvPLxv0OJ8EZ9yZRSuvUtlu0G 1RKHv9uIo1/aMHtXa3+nSuHhhATLutwjr+vxQQ1IN03mX47Abn3abNmZm3D40+6AVX7g 7nOXVlDzujZxODxXy+4gcQOSjeoHtf3FMwfjNRVWcfYE+tFnYpBB7UxTPn71RmG27oNX 2MrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715586474; x=1716191274; 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=CRqSGx+LZLYZmMM1dtm6DE8QjEfHEddJV2Al3d0Qb2M=; b=dj+zIf6i0NHEa/gSSBiOGsSALa9hxoNdIAeMDEQVIdh6vnrAcqdZTEquGpz0LsPGCA 3JAcRD6RRJG0gTwPPvYIOYwCbc0uVTX3sIR6QfYNVDV3bCKc/rZfHt/6dHvve0gc8qbi jlgXpyQjmcYH8H7AoipBNwRNjVyqjXIOm7WYSK8xR+JEihmcd27Tz2c6l1pWGw2ZjbZv TzkVRl0I01U08167mJwSVRjuP6N48/7Dl3gEs+EgWLtaUPuG2eIaqR4t6JcbvRyJzahO IuHT2qoppGnVxzlwm3GGV8hD2q9Q8GwVcQVbDFVniCg7Bl/KZQ2iXaC+sREHWkar+l6D ZwHA== X-Forwarded-Encrypted: i=1; AJvYcCWQInEl9/XMtRslh0e+k7e2vVZ1Sl3TptNSDU6iZd2ERkTMhpX4gTl0g15rniD4FTCGnbsp1vaOUUDwhtJSu9CgKh8= X-Gm-Message-State: AOJu0YwwsdkkyhxIkd2Dv+sFIgH407KUPaukFIIVs7uz5uO6FHG6mpFr Oe/aRYXSPLlPvhRG8mdHcUhbZfdvU7EN2YkALXzAo/oM4At10JH5 X-Google-Smtp-Source: AGHT+IGY+Awn4udMPhFiQz9yDb+p0ggUx+6DZOemAXrpevayvvkrNDj+S4e4smtqNCGCuoBLk6sOgQ== X-Received: by 2002:a05:6808:3099:b0:3c7:5146:c4e5 with SMTP id 5614622812f47-3c9970b0ff3mr11673420b6e.41.1715586474027; Mon, 13 May 2024 00:47:54 -0700 (PDT) Received: from LancedeMBP.lan.lan ([2403:2c80:6::304b]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66621sm7008967b3a.13.2024.05.13.00.47.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 00:47:53 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: willy@infradead.org, sj@kernel.org, baolin.wang@linux.alibaba.com, maskray@google.com, ziy@nvidia.com, ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, libang.li@antgroup.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH RESEND v5 1/4] mm/rmap: remove duplicated exit code in pagewalk loop Date: Mon, 13 May 2024 15:47:09 +0800 Message-Id: <20240513074712.7608-2-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240513074712.7608-1-ioworker0@gmail.com> References: <20240513074712.7608-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 469D040007 X-Rspam-User: X-Stat-Signature: oemgx3j1i5z4w4padkbzurgoo1enjcz9 X-HE-Tag: 1715586475-807422 X-HE-Meta: U2FsdGVkX180cTG6HilAWVvjEMg+YSKYJ0GCO5at9Auo59MikNQdSuUz/xAue3GwYGeV4d9dVDiLO3aZ5I+7DTfaLZUsgiYcE1WLjkZysU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MocyZDqWTcaq9pA+HubB8o6JEVy0I9W3RatnjzJoIgL0GvTcstz1+p5fCR0Gc54A7+3P8jVAmcIr1C7Zrn92bP3SSKwz4gCBbfGTplCrFARqBoNV8gVs+cAwKtmW2awS051m1L14WOy8+1jOjbPI52H8aTLq6DxGWgx9PNu00DTGfm9R14w3mKVW4BsKw7Yk+eIv0fB8hcIjZVrRD67xZHENFrkXxJqgcKgIONVe4wzVXOYOEVPd5FhKigKYzo6zg153Ma/9sFECHKoVqu2+8qpfZWNjrjcC30e 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: Introduce the labels walk_done and walk_done_err as exit points to eliminate duplicated exit code in the pagewalk loop. Reviewed-by: Zi Yan Signed-off-by: Lance Yang Reviewed-by: Baolin Wang --- mm/rmap.c | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index e8fc5ecb59b2..ddffa30c79fb 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1679,9 +1679,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, /* Restore the mlock which got missed */ if (!folio_test_large(folio)) mlock_vma_folio(folio, vma); - page_vma_mapped_walk_done(&pvmw); - ret = false; - break; + goto walk_done_err; } pfn = pte_pfn(ptep_get(pvmw.pte)); @@ -1719,11 +1717,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, */ if (!anon) { VM_BUG_ON(!(flags & TTU_RMAP_LOCKED)); - if (!hugetlb_vma_trylock_write(vma)) { - page_vma_mapped_walk_done(&pvmw); - ret = false; - break; - } + if (!hugetlb_vma_trylock_write(vma)) + goto walk_done_err; if (huge_pmd_unshare(mm, vma, address, pvmw.pte)) { hugetlb_vma_unlock_write(vma); flush_tlb_range(vma, @@ -1738,8 +1733,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, * actual page and drop map count * to zero. */ - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done; } hugetlb_vma_unlock_write(vma); } @@ -1811,9 +1805,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, if (unlikely(folio_test_swapbacked(folio) != folio_test_swapcache(folio))) { WARN_ON_ONCE(1); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } /* MADV_FREE page check */ @@ -1852,23 +1844,17 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, */ set_pte_at(mm, address, pvmw.pte, pteval); folio_set_swapbacked(folio); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } if (swap_duplicate(entry) < 0) { set_pte_at(mm, address, pvmw.pte, pteval); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } if (arch_unmap_one(mm, vma, address, pteval) < 0) { swap_free(entry); set_pte_at(mm, address, pvmw.pte, pteval); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } /* See folio_try_share_anon_rmap(): clear PTE first. */ @@ -1876,9 +1862,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, folio_try_share_anon_rmap_pte(folio, subpage)) { swap_free(entry); set_pte_at(mm, address, pvmw.pte, pteval); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } if (list_empty(&mm->mmlist)) { spin_lock(&mmlist_lock); @@ -1918,6 +1902,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, if (vma->vm_flags & VM_LOCKED) mlock_drain_local(); folio_put(folio); + continue; +walk_done_err: + ret = false; +walk_done: + page_vma_mapped_walk_done(&pvmw); + break; } mmu_notifier_invalidate_range_end(&range); From patchwork Mon May 13 07:47:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13663097 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 BE580C25B74 for ; Mon, 13 May 2024 07:48:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C8886B0277; Mon, 13 May 2024 03:48:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3778F6B0278; Mon, 13 May 2024 03:48:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F2296B0279; Mon, 13 May 2024 03:48:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id F2A026B0277 for ; Mon, 13 May 2024 03:48:03 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7311BC0C79 for ; Mon, 13 May 2024 07:48:03 +0000 (UTC) X-FDA: 82112594046.13.E3A1182 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf06.hostedemail.com (Postfix) with ESMTP id A1484180007 for ; Mon, 13 May 2024 07:48:01 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RwCNOFRJ; spf=pass (imf06.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=ioworker0@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=1715586481; 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=VW0xsCDY7mre2haGnv0Xg6zNVS1x+avy2x5g7mykcSU=; b=yG61G0h9RHRar7ua/I3SCxv8DO8y0o5ZVM8ANH8+0AsDYeJWF9P/JW/zPKVz0tgaRXjK34 LOkwzCDp8x1D0RSYiO4K3XhvzbdNSbASDTL5mk9Vy0MxYhtM0YKyKVdweFwpblMJFTWjN7 VOCts3hP1VnYAv6qgRtpvbVwK+1pyxA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715586481; a=rsa-sha256; cv=none; b=AqKlfDYSBGaqCUSTX0P2boljqnXYarEfqSndETVeAyqiZDgc8CNzKERnH43AHFUsywmHYo Tq8xLLYzCp9pQdH27hDZ10XmW3s1vGusPhk2Zl9IuGjQdlhup939i2oBaTVdXVrQgLjLvB jpOZisef09LS4plADZXngPpnTei4v2s= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RwCNOFRJ; spf=pass (imf06.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6f5053dc057so536316b3a.2 for ; Mon, 13 May 2024 00:48:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715586480; x=1716191280; 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=VW0xsCDY7mre2haGnv0Xg6zNVS1x+avy2x5g7mykcSU=; b=RwCNOFRJzwUE6VCP79U0wm+rO1fE67nTJa+HdcaA28yPNVHc2vsTLU8TqMOAFdmu3V wGjlGePE2SFSy4RbPZzbD80xHXpWlnbmBgQHjMupWuI8GJhjnP1mVTQwQmyKAr4y5d4z Jv0B+25oXLQkNBm8RWMeGzcZDSJY9lJrqbtnHfy6YcwJaytkLffDme7dVxD//kGnRhPL q5AsbIsHeWzl43cXU6s/SUPfFJBIMdp45k8rzA8r5zjPfeEZjk2iabDbn4SD6AGehLhZ aanYXrTB4O6CarYh1rnY5+skiWdTDYTq1MEGBhKpcb1/GLvs9I5HpSlwnl2r7xzk5yGP eXfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715586480; x=1716191280; 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=VW0xsCDY7mre2haGnv0Xg6zNVS1x+avy2x5g7mykcSU=; b=powHRtZNqvMPYgFnI+ViqxZylejxukMFMFV1CJk9L7c0st1QcVhiGKlPoMvkoVCtqb gsfKJ85jcSZ5wlavFCGpxMFNgIRuOmaChZbxkPzuEsaLEgA32/tE9K5ujS2/sTxgj24b 3dL/kosDQVDeAJ2IC6KcdTTidQFB9jMsqc0RDHc3hX143pBuQ37cgYH0nTMNKs6LsazU EvoC1r/9c/BN1t2LBqJ/JmV2Uz73lT8snTjMMWDLItSFglrO+c+tz3wGCfZCjlPmTXUP ScFGGxCUWEDqCGtOod18gFau2ooakkK6rVLsh2Ad6CMg/8rk6Poy/Pr3ZCzntPEh1mOd 5SdA== X-Forwarded-Encrypted: i=1; AJvYcCWvsAGHLd0XkXrrmRO4MK+ix4PM/QzLID85bGa8+65Oz1K0layk4Pkul+/gkXvwnk0grQt9kDusmUOxI6t5K8TDPOg= X-Gm-Message-State: AOJu0YyRejAm8Jg2Onxh3mdrjtAuSWAYUI5f3ID9LlBeWKDAEtJ/6TRe sH0RMgQfS53o9f8XcBnGC3gqP9ZGu+sfPVzqo39Poh8Jk/Ckvpas X-Google-Smtp-Source: AGHT+IEIpuabuqonhYE79on+MmpCBMiXOyD1MynjuJ+zgMt6P3Xc90pOg4tUdHHFiZ9X3J2Nmmenng== X-Received: by 2002:a05:6a00:4f8f:b0:6ea:d114:5ea1 with SMTP id d2e1a72fcca58-6f4e02cae54mr13273237b3a.17.1715586480465; Mon, 13 May 2024 00:48:00 -0700 (PDT) Received: from LancedeMBP.lan.lan ([2403:2c80:6::304b]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66621sm7008967b3a.13.2024.05.13.00.47.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 00:48:00 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: willy@infradead.org, sj@kernel.org, baolin.wang@linux.alibaba.com, maskray@google.com, ziy@nvidia.com, ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, libang.li@antgroup.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH RESEND v5 2/4] mm/rmap: integrate PMD-mapped folio splitting into pagewalk loop Date: Mon, 13 May 2024 15:47:10 +0800 Message-Id: <20240513074712.7608-3-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240513074712.7608-1-ioworker0@gmail.com> References: <20240513074712.7608-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A1484180007 X-Rspam-User: X-Stat-Signature: 6fjyi3srxn61rpof9cqgs9zp3jmrea7s X-HE-Tag: 1715586481-830930 X-HE-Meta: U2FsdGVkX1/rh39iH4kPeeHRvbUCwgoVaRT3uswrcKbxDdcZcPJHgb4aZvcKvcGoSaZpDoyEev4YAeQ6KHewXPN+vlPrN69Iohj9lJNKymh+8cjoWVG9V3+Ynnjx6JL/n0oc5j+KqdOZ6AOBG+ytuK47GN/9WsJqD0DkXOYNVUBVPr9fBlZIWg5Kw7OKBr3dLmWRb6exq8pVSm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EWQsIqHtnLVonNlvF2jijab9SuPpibzAz4baP5/hY+hHBIVWiK+YQGQ5quZarQw2eh2BomBo9JU0dVgqU6bSgSTmhOaJLPacwfAAkm0diPhhD5wYiHb9zzSgasvbdcUj6/yWwdxYgHpsjGFLwEttkWjmJc20S8ON21HcoYlL4BZMfOP3bMOEEGHRH1FaCrVYQ7j 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: In preparation for supporting try_to_unmap_one() to unmap PMD-mapped folios, start the pagewalk first, then call split_huge_pmd_address() to split the folio. Suggested-by: David Hildenbrand Signed-off-by: Lance Yang --- include/linux/huge_mm.h | 6 ++++++ mm/huge_memory.c | 42 +++++++++++++++++++++-------------------- mm/rmap.c | 23 ++++++++++++++++------ 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index c8d3ec116e29..9fcb0b0b6ed1 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -409,6 +409,9 @@ static inline bool thp_migration_supported(void) return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION); } +void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, + pmd_t *pmd, bool freeze, struct folio *folio); + #else /* CONFIG_TRANSPARENT_HUGEPAGE */ static inline bool folio_test_pmd_mappable(struct folio *folio) @@ -471,6 +474,9 @@ static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long address, bool freeze, struct folio *folio) {} static inline void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, bool freeze, struct folio *folio) {} +static inline void split_huge_pmd_locked(struct vm_area_struct *vma, + unsigned long address, pmd_t *pmd, + bool freeze, struct folio *folio) {} #define split_huge_pud(__vma, __pmd, __address) \ do { } while (0) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 317de2afd371..425272c6c50b 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2581,6 +2581,27 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, pmd_populate(mm, pmd, pgtable); } +void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, + pmd_t *pmd, bool freeze, struct folio *folio) +{ + VM_WARN_ON_ONCE(folio && !folio_test_pmd_mappable(folio)); + VM_WARN_ON_ONCE(!IS_ALIGNED(address, HPAGE_PMD_SIZE)); + VM_WARN_ON_ONCE(folio && !folio_test_locked(folio)); + VM_BUG_ON(freeze && !folio); + + /* + * When the caller requests to set up a migration entry, we + * require a folio to check the PMD against. Otherwise, there + * is a risk of replacing the wrong folio. + */ + if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd) || + is_pmd_migration_entry(*pmd)) { + if (folio && folio != pmd_folio(*pmd)) + return; + __split_huge_pmd_locked(vma, pmd, address, freeze); + } +} + void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long address, bool freeze, struct folio *folio) { @@ -2592,26 +2613,7 @@ void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE); mmu_notifier_invalidate_range_start(&range); ptl = pmd_lock(vma->vm_mm, pmd); - - /* - * If caller asks to setup a migration entry, we need a folio to check - * pmd against. Otherwise we can end up replacing wrong folio. - */ - VM_BUG_ON(freeze && !folio); - VM_WARN_ON_ONCE(folio && !folio_test_locked(folio)); - - if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd) || - is_pmd_migration_entry(*pmd)) { - /* - * It's safe to call pmd_page when folio is set because it's - * guaranteed that pmd is present. - */ - if (folio && folio != pmd_folio(*pmd)) - goto out; - __split_huge_pmd_locked(vma, pmd, range.start, freeze); - } - -out: + split_huge_pmd_locked(vma, range.start, pmd, freeze, folio); spin_unlock(ptl); mmu_notifier_invalidate_range_end(&range); } diff --git a/mm/rmap.c b/mm/rmap.c index ddffa30c79fb..4c4d14325f2e 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1640,9 +1640,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, if (flags & TTU_SYNC) pvmw.flags = PVMW_SYNC; - if (flags & TTU_SPLIT_HUGE_PMD) - split_huge_pmd_address(vma, address, false, folio); - /* * For THP, we have to assume the worse case ie pmd for invalidation. * For hugetlb, it could be much worse if we need to do pud @@ -1668,9 +1665,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, mmu_notifier_invalidate_range_start(&range); while (page_vma_mapped_walk(&pvmw)) { - /* Unexpected PMD-mapped THP? */ - VM_BUG_ON_FOLIO(!pvmw.pte, folio); - /* * If the folio is in an mlock()d vma, we must not swap it out. */ @@ -1682,6 +1676,23 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, goto walk_done_err; } + if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) { + /* + * We temporarily have to drop the PTL and start once + * again from that now-PTE-mapped page table. + */ + split_huge_pmd_locked(vma, range.start, pvmw.pmd, false, + folio); + pvmw.pmd = NULL; + spin_unlock(pvmw.ptl); + pvmw.ptl = NULL; + flags &= ~TTU_SPLIT_HUGE_PMD; + continue; + } + + /* Unexpected PMD-mapped THP? */ + VM_BUG_ON_FOLIO(!pvmw.pte, folio); + pfn = pte_pfn(ptep_get(pvmw.pte)); subpage = folio_page(folio, pfn - folio_pfn(folio)); address = pvmw.address; From patchwork Mon May 13 07:47:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13663098 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 62829C25B10 for ; Mon, 13 May 2024 07:48:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6B736B0279; Mon, 13 May 2024 03:48:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B19A16B027A; Mon, 13 May 2024 03:48:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 992C56B027B; Mon, 13 May 2024 03:48:10 -0400 (EDT) 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 7861B6B0279 for ; Mon, 13 May 2024 03:48:10 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E9AC3120CF6 for ; Mon, 13 May 2024 07:48:09 +0000 (UTC) X-FDA: 82112594298.16.A7BFC3D Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf08.hostedemail.com (Postfix) with ESMTP id 25A2216001D for ; Mon, 13 May 2024 07:48:07 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="XoIIRTS/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715586488; 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=d2aMd7RSjLCt9aorWWhiNJrgbBDqFo4vIbYySyvRoqM=; b=c75u6SszD0PxMnEFnjJlxC6ex5qAe0ADoK4xlOgZg8v67jeHIpw9IOY6lvqZZkgjiE+Kty Ft/w1iY4F6zahJnrYJe5abTw9/71GCJBpgFNp1jOtbXEN/N9NLCj9Z6+gGxpUzrZJH6obi p5NcuKgbLe10aGAKFZEDHyFsXdTfmLk= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="XoIIRTS/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715586488; a=rsa-sha256; cv=none; b=jnUgTrA9TNHdmXUXtz8eEenSpmyx/dRXejuqLcbQRT/lk8/yHPUkMOvbChPXveB2xDtQMx s1QsvZhZ4OxJEapuGXUREsCCRsxLSkwC1XIX9cLrF9QC5Cn3Xfa8jZJAKZNuaZtheit9xx 7NG0wN/rVn3JAee0/hwB8OAM29VF5wY= Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-6f44ed6e82fso3628207b3a.3 for ; Mon, 13 May 2024 00:48:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715586487; x=1716191287; 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=d2aMd7RSjLCt9aorWWhiNJrgbBDqFo4vIbYySyvRoqM=; b=XoIIRTS/4ZsKKQseHQVuNr6ejHdMUf18DOzyzgHEjzgQKo1r7fDVtuqmDineLatgzz BdVeFsHHWOFWR9q7X2VdMBduR5M1hfo897yUaxB3gBf8CNGNIM+4TuyKzaoEJ4RQ1mIY YrALwx56K4bkIu4iLZDrX1WcNmhxFGkkW3xPcTaxnsN3eDciPs5AXd2dMy4kM7eRptC/ sKVb4T4YZUNl9hkBQURQdINLiBm8/PnkjTrrUIE0+Rk96os49RC4uMoZxl6UfNlrXYXk ofJQHmO/NizFKm7MBkbPFtOLvnAujMIgREv4GfCifNR7po4n8oj1lqNKuk6McDJttXKR T2Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715586487; x=1716191287; 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=d2aMd7RSjLCt9aorWWhiNJrgbBDqFo4vIbYySyvRoqM=; b=X/VoQz0Ax5LFh7mlkKVNh2f2zWTZ3p6ByUfNKRYEH7F2OZ595X8hrUl9ORzhwVCGik mQMXbvGfF4WXJQggHb6vjkBxqhxesB3txCFeBeXSM23AoWAkcvs93BmBrfT3gpDeUu32 rGTCUvJsq1cjkRlMuweo8jIC4lgCZMqQ4UyNgTpLBp4oTpFNngkelt8Y1FRDHzAJRfsz BHWwGJYa/n3KYH//aO4Tg34zZDTWopt6BJRySDuxKk+PNNAPAn2ZHnlk9Gtg6Qxl284G S6632xAu7j2SncpVlBidcrVP9AMD+iQqMCVuiIyD7PI0FUvpeqEhFC2MHl/6MHKaWPzO nnjw== X-Forwarded-Encrypted: i=1; AJvYcCXBf6nfae8vrRoxiiUSSwADSVwu+FEejt8PZu5lsD0rmMUe8xcoCWbcAX7bN5Iw2x2oOBr1v7JvCQkFMTnQQJPlnwA= X-Gm-Message-State: AOJu0YwIG80QC3OgwtQ0L+Uw/yXWvM5beFglplqffD07Ri/MHxUc4dCX xNu3CjILq4RtZ5LHQofOoeL+/YbgYlVx0/H4H4qImKWKMgvVudpL X-Google-Smtp-Source: AGHT+IFpk9hHf0b9JsRoNRMxoqauhjaBA5WO539N9iC0llVow5ZRKrgoRxRAavfqAmqPSX2Tdv4EqQ== X-Received: by 2002:a05:6a00:2195:b0:6ec:f28b:659f with SMTP id d2e1a72fcca58-6f4e02988b7mr11861591b3a.3.1715586487009; Mon, 13 May 2024 00:48:07 -0700 (PDT) Received: from LancedeMBP.lan.lan ([2403:2c80:6::304b]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66621sm7008967b3a.13.2024.05.13.00.48.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 00:48:06 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: willy@infradead.org, sj@kernel.org, baolin.wang@linux.alibaba.com, maskray@google.com, ziy@nvidia.com, ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, libang.li@antgroup.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH RESEND v5 3/4] mm/mlock: check for THP missing the mlock in try_to_unmap_one() Date: Mon, 13 May 2024 15:47:11 +0800 Message-Id: <20240513074712.7608-4-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240513074712.7608-1-ioworker0@gmail.com> References: <20240513074712.7608-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 25A2216001D X-Stat-Signature: zj961szexrx6b4ngnd7cyua9h9q4cnho X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1715586487-963576 X-HE-Meta: U2FsdGVkX1+ZykHAwRo47lxGuQx2QWAk9GF3iHKqYybzx6Qi5+aBExbLON8Uc9ZAoMp7kpAJVij6rTiji/f0hQnVID2jUjRfdjxoloKZ8S+tHA0+cfCpAaxU+E40Omm8wrk72oKHkrfqBkHd8U8H61vWJ1HIZFHIVoHFlbUz8oOmacTRSmT22+WdZ/sT6TK+NKktc+2TNaIHOblMcbTDQ3VqYDYBa1ZjZ839h8YvLfz8BPbJHutvK3Yq64JTH74RsMxOO5jb+pO8Aj+nZ5hCU9qWwd5QBIc1JwXGiL/eM6RdWtK89OMiwTleq6ROmfI4C/QZeS1Xpu6Ah/v98auKggYDI6kbv2crJevoYFGX77ksceeokgRoHpPppQm/AehOMimunypsb6cwC+/NZd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xzVju0nhHJeVlPmIdlGjSjXlhUc4CxqCHg7y/lqXt0hos4wP5c10c46pZXz82slhy/iA8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The TTU_SPLIT_HUGE_PMD will no longer perform immediately, so we might encounter a PMD-mapped THP missing the mlock in the VM_LOCKED range during the pagewalk. It's likely necessary to mlock this THP to prevent it from being picked up during page reclaim. Suggested-by: Baolin Wang Signed-off-by: Lance Yang --- mm/rmap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/rmap.c b/mm/rmap.c index 4c4d14325f2e..08a93347f283 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1671,7 +1671,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, if (!(flags & TTU_IGNORE_MLOCK) && (vma->vm_flags & VM_LOCKED)) { /* Restore the mlock which got missed */ - if (!folio_test_large(folio)) + if (!folio_test_large(folio) || + (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD))) mlock_vma_folio(folio, vma); goto walk_done_err; } From patchwork Mon May 13 07:47:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13663099 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 B6ABEC25B74 for ; Mon, 13 May 2024 07:48:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0937F6B027B; Mon, 13 May 2024 03:48:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 043E06B027C; Mon, 13 May 2024 03:48:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E277C6B027D; Mon, 13 May 2024 03:48:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C13386B027B for ; Mon, 13 May 2024 03:48:16 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 804F31C0C0C for ; Mon, 13 May 2024 07:48:16 +0000 (UTC) X-FDA: 82112594592.28.196113D Received: from mail-oa1-f41.google.com (mail-oa1-f41.google.com [209.85.160.41]) by imf25.hostedemail.com (Postfix) with ESMTP id AFCA1A000F for ; Mon, 13 May 2024 07:48:14 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Au2QOZjN; spf=pass (imf25.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.160.41 as permitted sender) smtp.mailfrom=ioworker0@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=1715586494; 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=oHDREbDidUboYr6GnUNHG4W7oH+tz8aegBIo3ouG4XE=; b=aP+sT77oG323VNffkBRWl4s5q5DnH6JGREi0x04GXUKI4zeMVVAKKUQP+q4LYAotDK3I9H /jZ12wtKhlycCcT7tFKeIyxhkyGihTaN3WpARFBGR9aGuo5rghqEq0RsgjaaGrweoJJ080 5jqOkw3v335q1c975OBwpoZJ+LnVuxY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715586494; a=rsa-sha256; cv=none; b=38+KKQSJsOBs+Q7lbmMW12Rw11cZwj8zCVr2bfvzax83zT7HOiFkx+ySCEHe2z7rCV5+bL 89b6H4VU3475FLk5Kmg+rRzsgz3QAq2HFKhiqg6u2ugzSqIsQhujSpkrbQPBP1EHX5S7NX F38hOj4i6EapCuckii2Z82CM/UqTVE8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Au2QOZjN; spf=pass (imf25.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.160.41 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-oa1-f41.google.com with SMTP id 586e51a60fabf-23bd61fbd64so2580911fac.0 for ; Mon, 13 May 2024 00:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715586494; x=1716191294; 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=oHDREbDidUboYr6GnUNHG4W7oH+tz8aegBIo3ouG4XE=; b=Au2QOZjNAbzaetVaj5BPQcyh87DsJ6fkPXSJZg/MuGTd0cpxl+2V5zlZJVyKus9X2v tYYOWgziqV8dJ03PNOe6VGEs6mGUqhe8uZuSr3OyPqwDbuisPIjbcaNJwlqTRG9U6WdK VMRAin/iJhae0Q0Yl9qKkDigMvGoGhXpwbmemhckZRD2wRsGC9YDsZbrc9w9kTbH05vV DZcwOuqNn8J+BcNjyD8XoM3SiwwofkRTSk3+7ZKiiIhfHkq6k98zXA2odJvka9KnJepk agJ5XkEWqdqE8qZr7eQAC69qC/bmOTxo6A1I9TV8P7394CGjm+Ykmc7OkFmNDE1QQ8wY Aipw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715586494; x=1716191294; 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=oHDREbDidUboYr6GnUNHG4W7oH+tz8aegBIo3ouG4XE=; b=bic+O3RDCA6IOk1mT5FVP5DUYYpLj2uK58QVZ9SoE6RFhIQLrVX20/GVQAs5jHTNV7 wUOLuxbOQTd9f/zjnHgEzZuCzJPDuh5hGxY2wwzbiKsJPp5uLd61fwk67irSr9YFIYQg GlmRcqRZoqfIIitYQ2yMA7oRr/6CKRyEhfKRO+yizpM6hcJ4ovphn15OvD7hWyDuju2J uinIPL4Om4YEuTYTY6dsJI0ye5H6AQ8mdZnf5CruJWJ4frylITAN2IPJCjoSgurVZEA/ oj/tP9sQtXdPSm7v4CwRQa/+wlRNAMcWg2nbkW0988pq7BERtZR+D8OckT0ylsIGF+h/ Xt8w== X-Forwarded-Encrypted: i=1; AJvYcCW+wzkRTcAQY7IZeSraTmxBbn8GBtqPZnPydTC6nkBamiwgbZ9+6D1utrF93qrMfFlJ44stVnqXIr4IqA9aYIts0W8= X-Gm-Message-State: AOJu0YzTtxCb+RVSp7Ly/oNTned+OPI2nVYIw+e9rCjHyxL+VdL000Om wZAsdeWB1kCzIDBB9E0TfSf6d7y7HGvMIRMtT8CUIvYHS+c5EnLh X-Google-Smtp-Source: AGHT+IGWP4QQxcq2qCQ2sZa1iPOyt28+tz+E8mRQ1zmnhWzt3PbQUI3aIYx7WgDuIqeI+fL5E7v2Ug== X-Received: by 2002:a05:6871:a9c3:b0:23e:b42a:49d with SMTP id 586e51a60fabf-24172f67492mr11117705fac.43.1715586493775; Mon, 13 May 2024 00:48:13 -0700 (PDT) Received: from LancedeMBP.lan.lan ([2403:2c80:6::304b]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66621sm7008967b3a.13.2024.05.13.00.48.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 00:48:13 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: willy@infradead.org, sj@kernel.org, baolin.wang@linux.alibaba.com, maskray@google.com, ziy@nvidia.com, ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, libang.li@antgroup.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH RESEND v5 4/4] mm/vmscan: avoid split lazyfree THP during shrink_folio_list() Date: Mon, 13 May 2024 15:47:12 +0800 Message-Id: <20240513074712.7608-5-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240513074712.7608-1-ioworker0@gmail.com> References: <20240513074712.7608-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: AFCA1A000F X-Rspam-User: X-Stat-Signature: kfwejkci1wpfdi9ripi4j61wn5o6ogn5 X-HE-Tag: 1715586494-980570 X-HE-Meta: U2FsdGVkX1+EmIc5b6Yx8w3KOiwX7R9ebn9Ytj0ApJuPrVi8mpzTOcHr6eZT5TyqWVYPB92siLd0jVrFeqUP/zo7H9fyANFAalKCl5JNkl7Iv8fl1reIW2N4l1bdQg76pHgvSogJNXxv4MdblWGGqOCshGUlQvzB8W+iXyqptFW21Uk2NFSkv1StGjb7q6iIbSQTYyPuILRwrGJuGhtfd6HTHQvX3ikj/rJd8SZ8y8r4OQjSVMQO9xHCy9ESgrsBgpOi0UNV+0Vvk+TcdPYuOpX4Rj5v7837MXd8xeTodoiNPGab3LnSVfpMQ1Jn/jlTinnvJbSTb+F7P3bbsKkYwfyNGekX522YnVawotDfM24XIVD6dDRSRbsQ9dL22fK9MckLNbcEqqPCz269JQ9KrHe8Bi/2S3MwRtigjG4ALZo08b4mPF+zRQOJzRuEh5QP0DaFXaDd8OmV1jkLGfmoFLcp/IfsW8aXWEcXecxANrI8X9b+5RsbJJl6m8iCPbLBjyEDpwKs1hW/FsnyIP2gisU0LoNXIMBSH++YkVDvntV/qiV2OIqkd2gIowPORb/Pjt2SYHLVUoThlHRyXHCoa2Vs6n5j8IorCnkCwYsWGjZNfpgREznjdMSZkzmJD2FMuZnkriy5cRr5qRop4KNNJYTsOZsNLk/7pLYLqcvXkQtma8gyl7nUDbwUag7V/GmktUl+ybtc4hRvBDUKx90NiTOqgf9qeg6fWXYkjt6B5UWHolPkh2kJHTbiN0tN4RaR9TawEtbXUrB5mM1B5Pl5XhM5E6SYjjeyTY9mKZZvov0ioPiz3uZAYFvAZq+3IP5LXN8c6+bbYgkOaxNvuXpXkFDc1Ua6quzLxVW/8GbKDZBJwtFcnQsYT7BvLSTeQjkEvYFYBD3z/cQgEcWp5RMjCnd2lJjo70nOVpOV7Wz2Gz/XEJi7qs95g57b/9SG2nmmIMIsifedFFxCNqDHsQL vShbt9wu ZOl5VFrFgXFDY4bLlZQz3/J7HEc7Vupm+Ul8+ZfMZ4MijFGTWnxmGeYsMd9xJQY+VjvYmcdAoUWMkrZ6ssCkbY50Z5mOth2vFnrsrJH9iiRgEQT19UmTt7T7NMJv4OSywogDG3gndAQI4v4F+jcSoDc6NPJWIxOhDvHrB1soJVtUpA2TN5+W0XPVDZ19+DK9veYZyB/+TQnHyptpvzy096KbGTLrkM+aSrXE1soS8Z8GP7tDtMOqMuXuTLfbqp1HlSCN3mzFx2eqfDUDSeU+ZaKy1Bpj8r1o6zb8twGVk2dv7JTFCc6O0rDUTbTOXr96eHrLy0eZt033P/S4xd3BP16eZohM7pDuFeZ1JKzc690BTWOdmX8tEBpHE4cWEnMEVdiEnMy6DJJuAetywil9y5DWLu/CWFLVnS9K+q0OucwpecM4wXod+JjeuSbi6MY47vJ2KXdp5+LgzWY63gz6oxcDYHJxVuQrHrdUXxui+9ppLpYHCT/W/19y+pfjvsVljqrXsoXGUspg2Zis0UPvOTA1RdjpkrFqRa+goX15XvG7M3jo= 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: When the user no longer requires the pages, they would use madvise(MADV_FREE) to mark the pages as lazy free. Subsequently, they typically would not re-write to that memory again. During memory reclaim, if we detect that the large folio and its PMD are both still marked as clean and there are no unexpected references (such as GUP), so we can just discard the memory lazily, improving the efficiency of memory reclamation in this case. On an Intel i5 CPU, reclaiming 1GiB of lazyfree THPs using mem_cgroup_force_empty() results in the following runtimes in seconds (shorter is better): -------------------------------------------- | Old | New | Change | -------------------------------------------- | 0.683426 | 0.049197 | -92.80% | -------------------------------------------- Suggested-by: Zi Yan Suggested-by: David Hildenbrand Signed-off-by: Lance Yang --- include/linux/huge_mm.h | 9 +++++ mm/huge_memory.c | 75 +++++++++++++++++++++++++++++++++++++++++ mm/rmap.c | 31 ++++++++++------- 3 files changed, 103 insertions(+), 12 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 9fcb0b0b6ed1..cfd7ec2b6d0a 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -411,6 +411,8 @@ static inline bool thp_migration_supported(void) void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze, struct folio *folio); +bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmdp, struct folio *folio); #else /* CONFIG_TRANSPARENT_HUGEPAGE */ @@ -478,6 +480,13 @@ static inline void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze, struct folio *folio) {} +static inline bool unmap_huge_pmd_locked(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmdp, + struct folio *folio) +{ + return false; +} + #define split_huge_pud(__vma, __pmd, __address) \ do { } while (0) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 425272c6c50b..3ceeeb2f42d4 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2687,6 +2687,81 @@ static void unmap_folio(struct folio *folio) try_to_unmap_flush(); } +static bool __discard_trans_pmd_locked(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmdp, + struct folio *folio) +{ + struct mm_struct *mm = vma->vm_mm; + int ref_count, map_count; + pmd_t orig_pmd = *pmdp; + struct mmu_gather tlb; + struct page *page; + + if (pmd_dirty(orig_pmd) || folio_test_dirty(folio)) + return false; + if (unlikely(!pmd_present(orig_pmd) || !pmd_trans_huge(orig_pmd))) + return false; + + page = pmd_page(orig_pmd); + if (unlikely(page_folio(page) != folio)) + return false; + + tlb_gather_mmu(&tlb, mm); + orig_pmd = pmdp_huge_get_and_clear(mm, addr, pmdp); + tlb_remove_pmd_tlb_entry(&tlb, pmdp, addr); + + /* + * Syncing against concurrent GUP-fast: + * - clear PMD; barrier; read refcount + * - inc refcount; barrier; read PMD + */ + smp_mb(); + + ref_count = folio_ref_count(folio); + map_count = folio_mapcount(folio); + + /* + * Order reads for folio refcount and dirty flag + * (see comments in __remove_mapping()). + */ + smp_rmb(); + + /* + * If the PMD or folio is redirtied at this point, or if there are + * unexpected references, we will give up to discard this folio + * and remap it. + * + * The only folio refs must be one from isolation plus the rmap(s). + */ + if (ref_count != map_count + 1 || folio_test_dirty(folio) || + pmd_dirty(orig_pmd)) { + set_pmd_at(mm, addr, pmdp, orig_pmd); + return false; + } + + folio_remove_rmap_pmd(folio, page, vma); + zap_deposited_table(mm, pmdp); + add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR); + if (vma->vm_flags & VM_LOCKED) + mlock_drain_local(); + folio_put(folio); + + return true; +} + +bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmdp, struct folio *folio) +{ + VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio); + VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE)); + + if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) + return __discard_trans_pmd_locked(vma, addr, pmdp, folio); + + return false; +} + static void remap_page(struct folio *folio, unsigned long nr) { int i = 0; diff --git a/mm/rmap.c b/mm/rmap.c index 08a93347f283..e09f2141b8dc 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1677,18 +1677,25 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, goto walk_done_err; } - if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) { - /* - * We temporarily have to drop the PTL and start once - * again from that now-PTE-mapped page table. - */ - split_huge_pmd_locked(vma, range.start, pvmw.pmd, false, - folio); - pvmw.pmd = NULL; - spin_unlock(pvmw.ptl); - pvmw.ptl = NULL; - flags &= ~TTU_SPLIT_HUGE_PMD; - continue; + if (!pvmw.pte) { + if (unmap_huge_pmd_locked(vma, range.start, pvmw.pmd, + folio)) + goto walk_done; + + if (flags & TTU_SPLIT_HUGE_PMD) { + /* + * We temporarily have to drop the PTL and start + * once again from that now-PTE-mapped page + * table. + */ + split_huge_pmd_locked(vma, range.start, + pvmw.pmd, false, folio); + pvmw.pmd = NULL; + spin_unlock(pvmw.ptl); + pvmw.ptl = NULL; + flags &= ~TTU_SPLIT_HUGE_PMD; + continue; + } } /* Unexpected PMD-mapped THP? */