From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CBDFC433F5 for ; Wed, 10 Nov 2021 13:59:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E029D61241 for ; Wed, 10 Nov 2021 13:59:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E029D61241 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 219EA6B006C; Wed, 10 Nov 2021 08:59:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C93C6B0071; Wed, 10 Nov 2021 08:59:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B8E56B0072; Wed, 10 Nov 2021 08:59:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0073.hostedemail.com [216.40.44.73]) by kanga.kvack.org (Postfix) with ESMTP id F071D6B006C for ; Wed, 10 Nov 2021 08:59:47 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id AC6DC82258 for ; Wed, 10 Nov 2021 13:59:47 +0000 (UTC) X-FDA: 78793178772.27.0D8571A Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf21.hostedemail.com (Postfix) with ESMTP id 528BBD036A53 for ; Wed, 10 Nov 2021 13:59:41 +0000 (UTC) Received: by mail-pl1-f178.google.com with SMTP id y7so3201317plp.0 for ; Wed, 10 Nov 2021 05:59:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:to:cc:references :from:in-reply-to:content-transfer-encoding; bh=tFIzrVfWxUu1s11DMzseTciEZacVYJOeZPAxD7PkBnc=; b=fMUDtoK0LHbZtB4+J67U/8RChvQYDgrgNEChmKP8uO3RjuUAAPdRq004ML+AfuGCgn 7MiZdzrDqODFsUVAXsjvQbTS/61YMYBifNacfpXUDICni4DdnTEeLEFGKR/87CCW/0U4 zeNPkt0ejt+JePwX3vMqp3ENHuGTLdSU3Cm9wmTZBRBOfGntCJUTe8MeF5dTmjmkplAp 4rCCHYaU77kyY00DemZ765mAN/XVPiVOMsnMyWDUJNg5z8Om+kvq5wFA+Ecgjq3gJ5WM SS/01f4YZrlmPyGz2n9DzCXkaca2eVq3nnMTUjfEPJ1FDqFlOwMEHSEM6/OjZldlEO44 SDLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :to:cc:references:from:in-reply-to:content-transfer-encoding; bh=tFIzrVfWxUu1s11DMzseTciEZacVYJOeZPAxD7PkBnc=; b=3RHVr6qPbGOhaGg2UkcXOyEG/qhKIQJ4gREcY1O0suAc6yI66mLXZVCGV5ZqaEf+X7 ESH3hhOmN3uO1cOZHZMJ0dyd+LPRFiCDPMUgaNcnVqnPaOJiZKAqxkgx2n7C49wcZ6VJ i7tdiOeHprTwTXCkIh4kSNoFyGbm6r7ISblvtqfZwQ3h9RoT6ohD8Dqcf/oA8esx9bV5 gNaafT/ljS7HFpU1YpNMK7UFgFTgQOK8P4KQDWBsqUwdbiLRSx6sEac+qvC7SX+pJDCR p/k0iOs+qeu+B4fcGnGxXtE75zxJfGh9OdAWeD1lMRggaAMQkEZ4xP0UCsxn4Qdi/tse iyhA== X-Gm-Message-State: AOAM531+JQu0jVuz+IC2g5CkkGtvjgTXkVDDZVuEer6PdYqWwy+6PiPW MOisLpNKWneZjaFlPPzkByUCAg== X-Google-Smtp-Source: ABdhPJyhZgAb2msPG/ckhwytGG7U2KKGRU4QdobBz5WlQJnXbF5X4E026bM/Jw/kMzmZVHJF6VUNhw== X-Received: by 2002:a17:90a:c58f:: with SMTP id l15mr98890pjt.168.1636552786017; Wed, 10 Nov 2021 05:59:46 -0800 (PST) Received: from [10.254.189.129] ([139.177.225.251]) by smtp.gmail.com with ESMTPSA id w12sm6189400pjq.2.2021.11.10.05.59.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 10 Nov 2021 05:59:45 -0800 (PST) Message-ID: <1dac28b1-9a35-25bd-08f4-e1594fdb35d1@bytedance.com> Date: Wed, 10 Nov 2021 21:59:38 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.3.0 Subject: Re: [PATCH v3 00/15] Free user PTE page table pages To: David Hildenbrand , Jason Gunthorpe Cc: akpm@linux-foundation.org, tglx@linutronix.de, kirill.shutemov@linux.intel.com, mika.penttila@nextfour.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, songmuchun@bytedance.com, zhouchengming@bytedance.com References: <20211110105428.32458-1-zhengqi.arch@bytedance.com> <20211110125601.GQ1740502@nvidia.com> <8d0bc258-58ba-52c5-2e0d-a588489f2572@redhat.com> From: Qi Zheng In-Reply-To: <8d0bc258-58ba-52c5-2e0d-a588489f2572@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=fMUDtoK0; spf=pass (imf21.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 528BBD036A53 X-Stat-Signature: 57y55idebd69qeo5scgm85dx4zy54h96 X-HE-Tag: 1636552781-680045 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: On 11/10/21 9:25 PM, David Hildenbrand wrote: > On 10.11.21 13:56, Jason Gunthorpe wrote: >> On Wed, Nov 10, 2021 at 06:54:13PM +0800, Qi Zheng wrote: >> >>> In this patch series, we add a pte_refcount field to the struct page of page >>> table to track how many users of PTE page table. Similar to the mechanism of >>> page refcount, the user of PTE page table should hold a refcount to it before >>> accessing. The PTE page table page will be freed when the last refcount is >>> dropped. >> >> So, this approach basically adds two atomics on every PTE map >> >> If I have it right the reason that zap cannot clean the PTEs today is >> because zap cannot obtain the mmap lock due to a lock ordering issue >> with the inode lock vs mmap lock. > > There are different ways to zap: madvise(DONTNEED) vs > fallocate(PUNCH_HOLE). It depends on "from where" we're actually > comming: a process page table walker or the rmap. > > The way locking currently works doesn't allow to remove a page table > just by holding the mmap lock, not even in write mode. You'll also need > to hold the respective rmap locks -- which implies that reclaiming apge > tables crossing VMAs is "problematic". Take a look at khugepaged which > has to play quite some tricks to remove a page table. > > And there are other ways we can create empty page tables via the rmap, > like reclaim/writeback, although they are rather a secondary concern mostly. > >> >> If it could obtain the mmap lock then it could do the zap using the >> write side as unmapping a vma does. >> >> Rather than adding a new "lock" to ever PTE I wonder if it would be >> more efficient to break up the mmap lock and introduce a specific >> rwsem for the page table itself, in addition to the PTL. Currently the >> mmap lock is protecting both the vma list and the page table. > > There is the rmap side of things as well. At least the rmap won't > reclaim alloc/free page tables, but it will walk page tables while > holding the respective rmap lock. > >> >> I think that would allow the lock ordering issue to be resolved and >> zap could obtain a page table rwsem. >> >> Compared to two atomics per PTE this would just be two atomic per >> page table walk operation, it is conceptually a lot simpler, and would >> allow freeing all the page table levels, not just PTEs. > > Another alternative is to not do it in the kernel automatically, but > instead have a madvise(MADV_CLEANUP_PGTABLE) mechanism that will get > called by user space explicitly once it's reasonable. While this will > work for the obvious madvise(DONTNEED) users -- like memory allocators > -- that zap memory, it's a bit more complicated once shared memory is > involved and we're fallocate(PUNCH_HOLE) memory. But it would at least > work for many use cases that want to optimize memory consumption for > sparse memory mappings. > > Note that PTEs are the biggest memory consumer. On x86-64, a 1 TiB area > will consume 2 GiB of PTE tables and only 4 MiB of PMD tables. So PTEs > are most certainly the most important part piece. > total agree! Thanks, Qi