Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3464161lqp; Tue, 26 Mar 2024 09:46:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUH1LmA7NgVlx/TyoaFA3kFzIYBAYc8DYb1U7XpsTSW2JZQOd8Y0frMBNfbai8rQ0eGToaIBo9uSVJains3lcP2YQzdRKqTYElE0NZGZA== X-Google-Smtp-Source: AGHT+IFOidJoaptT7mTya+3uLxgWWmDf2GAFXyO6RsAxExjw6OZ7r5b9ByMr2C8zqd/MCJPCi7xu X-Received: by 2002:a05:6a20:8906:b0:1a3:7e72:316c with SMTP id i6-20020a056a20890600b001a37e72316cmr7904852pzg.31.1711471570014; Tue, 26 Mar 2024 09:46:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711471570; cv=pass; d=google.com; s=arc-20160816; b=B5KYzaO3Lg6STy4bCI9xvZ6DraRmNqd2WDyZvQ21s4G74v3r3jf8jEE4BS8h8BU5Il iswG+qIpk+e+24W+6qp6e9h7fiabi1LvWl/1Wj/TRQFEaRxyGsNLX+c64tWAGZsUhM/n mL5Zl4AIIPaHAMas/eUm1LeGEQP/cIS93NRqpRQ1X7bpZoY42w/G5NLOH2hR0Z9qVUYP 3kRhvRXNHlnK3Zwk2XCFBMof+GuXS6fyz1WLU0H2PZavup1sTjThFH+rmW6GMGJAiCxe +dIb6SLcSBC4U4F28/BZmxgl2yE6MqM1NuoD3/smyd7lCjnNOgNXEziNwPoBuB4tmL8Z /qUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=ikxlqLFrhwL3EOrncByoqKBD1HwIxZdjUWHsNw9ELgc=; fh=2DpClSFvN4kJ0W/z3zbvRnSTXypfVlM5rpUT60jUUgY=; b=XrKeK+2Gat6zLRAbmaa5hnv3lm+0txObob/q17J/Vb94721r0o3KJ63jw6Exkdjwg/ h+aSDrdL0ZYw1DadEIZ+Orx2vaUhPUDq000LjyMvJ9P1QrMfR+ZwjR7fcq3Nz2Fnp07n AKq0q0FuN7opVmPYVum5mW3egcuCTJqAeDgLzxX6sFqVP0eAzNlsTRsJfxR0SWZ+sao9 6627uz3pJtRic4yHyovy7mpTPrcmC3YWRoWpdUvUcdCRhd732lHIhtr3qu/H+3xg2SSE lvwEqwMb/Q9T7S/hsJG9lOMUOIwT+pEDN3I2Gswuv7LKRG9cxPKJqyG+YPWFjlXDeG6T VD4w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-119437-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119437-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id t26-20020a056a00139a00b006e6a6851c90si8032625pfg.166.2024.03.26.09.46.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 09:46:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119437-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-119437-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119437-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 97CCEB245C5 for ; Tue, 26 Mar 2024 16:17:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D56A113CA9D; Tue, 26 Mar 2024 16:17:22 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF12513C8E2 for ; Tue, 26 Mar 2024 16:17:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711469842; cv=none; b=KTrb117Wdrn/Hp4YDeSm10ME0pYKqO/6IDqcJlJXvv2UlvRKHNHpmCSMowgAx4SbEIMaYZryQ+5Fn34ZXJ+CMUuPeSwk1DLMDNb73ZX4hQtmBHYatPHm6NKt6wdq8ra5AY7Kw9y+Kd3s5qoTdrkgjIA8Dq1tSXB6LCDl5r1ecFA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711469842; c=relaxed/simple; bh=Eo4uqpIyoqavyiinY7O48ueSVpp+jY+UAtDVkrKX52s=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=fDItizFHp9a8OV5JH3N54KV1bjpFIwetgd797eZnVXMHfHJI7Q4VFR7/YuN+2HF50d41k25muL2d4cyEslvNz3AjD/ZCRydRryMcSPEinh2heAzipp/R2sZFf4X7T/FGNyMNfqmL/bryoiE97iCnht/u1zfnyga3Isi29Tvn33w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6F8792F4; Tue, 26 Mar 2024 09:17:52 -0700 (PDT) Received: from [10.1.29.179] (XHFQ2J9959.cambridge.arm.com [10.1.29.179]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1BF9A3F694; Tue, 26 Mar 2024 09:17:15 -0700 (PDT) Message-ID: Date: Tue, 26 Mar 2024 16:17:14 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] mm: hold PTL from the first PTE while reclaiming a large folio Content-Language: en-GB To: David Hildenbrand , Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org Cc: chrisl@kernel.org, hanchuanhua@oppo.com, hughd@google.com, linux-kernel@vger.kernel.org, mhocko@suse.com, shy828301@gmail.com, v-songbaohua@oppo.com, wangkefeng.wang@huawei.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yuzhao@google.com References: <20240306095219.71086-1-21cnbao@gmail.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 26/03/2024 16:10, David Hildenbrand wrote: > On 06.03.24 10:52, Barry Song wrote: >> From: Barry Song >> >> Within try_to_unmap_one(), page_vma_mapped_walk() races with other >> PTE modifications preceded by pte clear. While iterating over PTEs >> of a large folio, it only starts acquiring PTL from the first valid >> (present) PTE. PTE modifications can temporarily set PTEs to >> pte_none. Consequently, the initial PTEs of a large folio might >> be skipped in try_to_unmap_one(). >> For example, for an anon folio, if we skip PTE0, we may have PTE0 >> which is still present, while PTE1 ~ PTE(nr_pages - 1) are swap >> entries after try_to_unmap_one(). >> So folio will be still mapped, the folio fails to be reclaimed >> and is put back to LRU in this round. >> This also breaks up PTEs optimization such as CONT-PTE on this >> large folio and may lead to accident folio_split() afterwards. >> And since a part of PTEs are now swap entries, accessing those >> parts will introduce overhead - do_swap_page. >> Although the kernel can withstand all of the above issues, the >> situation still seems quite awkward and warrants making it more >> ideal. >> The same race also occurs with small folios, but they have only >> one PTE, thus, it won't be possible for them to be partially >> unmapped. >> This patch holds PTL from PTE0, allowing us to avoid reading PTE >> values that are in the process of being transformed. With stable >> PTE values, we can ensure that this large folio is either >> completely reclaimed or that all PTEs remain untouched in this >> round. >> A corner case is that if we hold PTL from PTE0 and most initial >> PTEs have been really unmapped before that, we may increase the >> duration of holding PTL. Thus we only apply this optimization to >> folios which are still entirely mapped (not in deferred_split >> list). >> >> Cc: Hugh Dickins >> Signed-off-by: Barry Song >> --- >>   v2: >>    * Refine commit message and code comment after reading all comments >>      from Ryan and David, thanks! >>    * Avoid increasing the duration of PTL by applying optimization >>      on folios not in deferred_split_list with respect to Ying's >>      comment, thanks! >> >>   mm/vmscan.c | 12 ++++++++++++ >>   1 file changed, 12 insertions(+) >> >> diff --git a/mm/vmscan.c b/mm/vmscan.c >> index 0b888a2afa58..7106741387e8 100644 >> --- a/mm/vmscan.c >> +++ b/mm/vmscan.c >> @@ -1270,6 +1270,18 @@ static unsigned int shrink_folio_list(struct list_head >> *folio_list, >>                 if (folio_test_pmd_mappable(folio)) >>                   flags |= TTU_SPLIT_HUGE_PMD; >> +            /* >> +             * Without TTU_SYNC, try_to_unmap will only begin to hold PTL >> +             * from the first present PTE within a large folio. Some initial >> +             * PTEs might be skipped due to races with parallel PTE writes >> +             * in which PTEs can be cleared temporarily before being written >> +             * new present values. This will lead to a large folio is still >> +             * mapped while some subpages have been partially unmapped after >> +             * try_to_unmap; TTU_SYNC helps try_to_unmap acquire PTL from the >> +             * first PTE, eliminating the influence of temporary PTE values. >> +             */ >> +            if (folio_test_large(folio) && list_empty(&folio->_deferred_list)) >> +                flags |= TTU_SYNC; >>                 try_to_unmap(folio, flags); >>               if (folio_mapped(folio)) { > > Hopefully this won't have unexpected performance "surprises". > > I do wonder if we should really care about the "_deferred_list" optimization > here, though, I'd just drop it. I also concluded that we do need the data_race() annotation around list_empty() if you do wind up keeping it. But I agree with David about dropping it. > > In any case > > Acked-by: David Hildenbrand >