Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp32675lqs; Mon, 4 Mar 2024 14:03:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXbarlY9PCv4si9+cFosD16K53VamJO2Nvwdv9Y3hOGpntPy7uVQwwl/qpCSLfZlbY+sAdStsdinF17T8/hyVTAAHDv9nQ12C9RKDnhNA== X-Google-Smtp-Source: AGHT+IGJJJSK5Zg2wEs2ifeUt2Uquc8LIHLLD89XuceM41qUol2G2Y6qjayo7mO5wAEj5oJbIPbO X-Received: by 2002:a05:6a00:4fc9:b0:6e6:189b:a3a4 with SMTP id le9-20020a056a004fc900b006e6189ba3a4mr4370902pfb.5.1709589783932; Mon, 04 Mar 2024 14:03:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709589783; cv=pass; d=google.com; s=arc-20160816; b=MSYdo4qnfcd13aNpqJ5jXc3lY3BXNm7GeAd8ULOC7lsUmw2LQsXXNHjMYZdOjnIGSn /CqNRjxZ9TG9pK43OG6P/qoRfKoPN714Z/J5tkf8bIF/qLQ58+xpIiuOcJw7+NbQ9Qau lvFA7OXUEIAohjZ1mdZc7Jk9rsNnL5+6un3Eipeh0bqMMqkRBCZV88ZR8dY3ZIlINiPh Wbo26vCFKDbz2kUcdd79fM9vqY8r+0bXypnhTjTHlUR/HNQka2aD2kH64dMF/6UTxWgu /Y/FxD/gaIIUlnJ894bCcretmxVfNPKFv1lXg/LjQ2filClPDcfN6b6qQfv+6uhkNI42 TyIg== 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=yHRiuOxJeocwzxWF/cwYC5tgDXtkYyQV5fYMYDHP9OI=; fh=nXaY1rY8jbJeBXR4F7nLdUzJHYxuPj0Bms54mliLqK8=; b=lZ6+mLsMhmXZXd2Xf9Cb6ea7H7dKpa/K+xLpHsD+01hNeqDwIUjFtPzGavtmQ2yhM5 b+2TTYHpSsphj35k/H4Qj0iUpMQPp1X11TSQTCBlhysVeLFIubEDJq6koV6AEteaEe9H KqoRY6eafsVujU7rPjwYDb5fDJpnrZcl58iSU2u7UXvVT2DWzBUOSV51dEYzPZ3GRGu5 BlS6kbiHlmsoeIw6eyJ76lwnTJahDQz4RFw7T6mkpYrjk2gjvaWSBEzPmaDl+r6Zdqvx fywiqfxC071E8Yt17NmViPE/hrDEca3juu6PIG/2CHFLzB0B7bjtLrsYD6iERPwgUZV1 lVLw==; 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-91336-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91336-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id m66-20020a632645000000b005e42b580aadsi8655882pgm.393.2024.03.04.14.03.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:03:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91336-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; 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-91336-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91336-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B4C38280E6D for ; Mon, 4 Mar 2024 22:02:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8CCEA7B3DE; Mon, 4 Mar 2024 22:02:43 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2E9D079DCA for ; Mon, 4 Mar 2024 22:02:40 +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=1709589763; cv=none; b=KD05FI0PoD0OIMNyV+0nKVJq1OO0cBlmJej7vNo3M2ULaV7O8Tp/HUYL0Vk1r+qTmQdLkVovCiyhI/nZBDCyazkFM4vtXjqSYj0wyoF8b4PimAMlP3p3mR/rY8LdEYOjBfnrsgSbWJzV9gxMlFGLFv2RKA5ItaIuzMI15DVEDos= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709589763; c=relaxed/simple; bh=yHRiuOxJeocwzxWF/cwYC5tgDXtkYyQV5fYMYDHP9OI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Gl8UT5uM1Xb1CxBpFrXHHAiME9q9dZou9cJbaneY1NgrFYw+GmfV7t8BYD64TBNj1BPCdMzDokd7CfutT1PB12pX4aCMxqSeqz3U6jCsUXlgeskCYMWhr1mcyHG72sazAmHzXHeyvGZamzoocDA3bY1Q+fQYa/Rzq2x1RylwN3o= 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 F3AD02F4; Mon, 4 Mar 2024 14:03:16 -0800 (PST) Received: from [10.57.68.92] (unknown [10.57.68.92]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 923EF3F73F; Mon, 4 Mar 2024 14:02:37 -0800 (PST) Message-ID: Date: Mon, 4 Mar 2024 22:02:36 +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: [RFC PATCH] mm: hold PTL from the first PTE while reclaiming a large folio Content-Language: en-GB To: Barry Song <21cnbao@gmail.com>, David Hildenbrand Cc: akpm@linux-foundation.org, linux-mm@kvack.org, 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 References: <20240304103757.235352-1-21cnbao@gmail.com> <706b7129-85f6-4470-9fd9-f955a8e6bd7c@arm.com> <37f1e6da-412b-4bb4-88b7-4c49f21f5fe9@redhat.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 04/03/2024 21:04, Barry Song wrote: > On Tue, Mar 5, 2024 at 1:41 AM David Hildenbrand wrote: >> >> On 04.03.24 13:20, Ryan Roberts wrote: >>> Hi Barry, >>> >>> On 04/03/2024 10:37, Barry Song wrote: >>>> 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(). >>> >>> I just want to check my understanding here - I think the problem occurs for >>> PTE-mapped, PMD-sized folios as well as smaller-than-PMD-size large folios? Now >>> that I've had a look at the code and have a better understanding, I think that >>> must be the case? And therefore this problem exists independently of my work to >>> support swap-out of mTHP? (From your previous report I was under the impression >>> that it only affected mTHP). >>> >>> Its just that the problem is becoming more pronounced because with mTHP, >>> PTE-mapped large folios are much more common? >> >> That is my understanding. >> >>> >>>> 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. >>>> >> >> I'm sure that wall of text can be formatted in a better way :) . Also, I >> think we can drop some of the details, >> >> If you need some inspiration, I can give it a shot. >> >>>> Cc: Hugh Dickins >>>> Signed-off-by: Barry Song >>> >>> Do we need a Fixes tag? It seems my original question has snowballed a bit. I was conflating this change with other reports Barry has made where the kernel was panicking (I think?). Given we are not seeing any incorrect functional behaviour that this change fixes, I agree we don't need a Fixes tag here.