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 X-Spam-Level: X-Spam-Status: No, score=-6.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06087C4320E for ; Wed, 1 Sep 2021 12:32:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9BFC561164 for ; Wed, 1 Sep 2021 12:32:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9BFC561164 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 0F9AF8D0001; Wed, 1 Sep 2021 08:32:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A9A56B0071; Wed, 1 Sep 2021 08:32:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDA778D0001; Wed, 1 Sep 2021 08:32:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0230.hostedemail.com [216.40.44.230]) by kanga.kvack.org (Postfix) with ESMTP id E04766B006C for ; Wed, 1 Sep 2021 08:32:13 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 953FE824999B for ; Wed, 1 Sep 2021 12:32:13 +0000 (UTC) X-FDA: 78538942146.04.1BFDFA0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 34F2AF000090 for ; Wed, 1 Sep 2021 12:32:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630499532; h=from:from: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; bh=6Dbvlu5ZSAjtRqG1EufqlUFTIvkyoUz8z7Cc+P05CuE=; b=DfwZjh2lGxeQQaF7DKVtU05jIxIl9veN/drFt8hmQzbhDWEoP/WKVIga9VKIVHrppREiE+ ktoEBGSw+ocWto/vNd7djrM49k3tSTm+D9h5Klt6h9nkyxWcBXfzDCd+a+1NwF6hUB8LJA BB/GOJ98ZA9Hvgae11islIRcHkUPtDE= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-24-HmTLrdlHOKOWKdZi4IuWvg-1; Wed, 01 Sep 2021 08:32:11 -0400 X-MC-Unique: HmTLrdlHOKOWKdZi4IuWvg-1 Received: by mail-wm1-f69.google.com with SMTP id g3-20020a1c2003000000b002e751c4f439so2744853wmg.7 for ; Wed, 01 Sep 2021 05:32:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:references:from:organization:subject :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=6Dbvlu5ZSAjtRqG1EufqlUFTIvkyoUz8z7Cc+P05CuE=; b=VJOh+P9dYwNmAArnN2rvOASVb24q5oukjHvMf2r82ggYlI0ZOypDC5fbuWpfD9ZFq8 jGn7rMsij3D8WrRufh5emDzif9mT/mT9EJP27mjvhsNq+Yln9VGxXrz6XQzuxK72M4ir jbnuWxhN4p3ijBGNwrfNY8jTXTIdGrTydozlEC6O1EVfm1fS+2P3Q8mtJlWnAGxAsEV9 6hN5E3mvGpCRq50e7LmMYsesvtPcVdmk/mcvDF+e1vbSMmZhYA/elPTRBx+gH7D6Cvra mggBRSW4S2uw/sxIgxAm+iJD23lqGwUmLEBPM0A8t8kKShiWXfGUq5WT76TS9eR+arT4 Y7yg== X-Gm-Message-State: AOAM532twctsiKKG7T38DpBqELJC/5PqbDSoJB6AD4TCP2EwW9txQBrB cxEVkSBGJcfrKpW1+7/HIofEqjTsbCUdYKuiIltd/0OGuIeTSdcn8+1mdu8uzOn3eaqMMAqNB3e 8st9ZF/syY2k= X-Received: by 2002:adf:d239:: with SMTP id k25mr9857055wrh.314.1630499530028; Wed, 01 Sep 2021 05:32:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgQ3pleHAmLNS3ldiuaJSpYI6aLKAmT2AqtxZ6KQPbyagrUETSbVHG4/qEex808Z3/iRij4A== X-Received: by 2002:adf:d239:: with SMTP id k25mr9857023wrh.314.1630499529802; Wed, 01 Sep 2021 05:32:09 -0700 (PDT) Received: from [192.168.3.132] (p4ff23f71.dip0.t-ipconnect.de. [79.242.63.113]) by smtp.gmail.com with ESMTPSA id l15sm4846090wms.38.2021.09.01.05.32.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Sep 2021 05:32:09 -0700 (PDT) To: Qi Zheng , akpm@linux-foundation.org, tglx@linutronix.de, hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, kirill.shutemov@linux.intel.com, mika.penttila@nextfour.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, songmuchun@bytedance.com References: <20210819031858.98043-1-zhengqi.arch@bytedance.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v2 0/9] Free user PTE page table pages Message-ID: <5b9348fc-95fe-5be2-e9df-7c906e0c9b81@redhat.com> Date: Wed, 1 Sep 2021 14:32:08 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210819031858.98043-1-zhengqi.arch@bytedance.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DfwZjh2l; spf=none (imf17.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: 1zkkz6txztg34uewwwj3djpyf7wty5y4 X-Rspamd-Queue-Id: 34F2AF000090 X-Rspamd-Server: rspam04 X-HE-Tag: 1630499533-923371 Content-Transfer-Encoding: quoted-printable 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 19.08.21 05:18, Qi Zheng wrote: > Hi, >=20 > This patch series aims to free user PTE page table pages when all PTE e= ntries > are empty. >=20 > The beginning of this story is that some malloc libraries(e.g. jemalloc= or > tcmalloc) usually allocate the amount of VAs by mmap() and do not unmap= those VAs. > They will use madvise(MADV_DONTNEED) to free physical memory if they wa= nt. > But the page tables do not be freed by madvise(), so it can produce man= y > page tables when the process touches an enormous virtual address space. >=20 > The following figures are a memory usage snapshot of one process which = actually > happened on our server: >=20 > VIRT: 55t > RES: 590g > VmPTE: 110g >=20 > As we can see, the PTE page tables size is 110g, while the RES is 590g.= In > theory, the process only need 1.2g PTE page tables to map those physica= l > memory. The reason why PTE page tables occupy a lot of memory is that > madvise(MADV_DONTNEED) only empty the PTE and free physical memory but > doesn't free the PTE page table pages. So we can free those empty PTE p= age > tables to save memory. In the above cases, we can save memory about 108= g(best > case). And the larger the difference between the size of VIRT and RES, = the > more memory we save. >=20 > 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 mechani= sm 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. >=20 > Testing: >=20 > The following code snippet can show the effect of optimization: >=20 > mmap 50G > while (1) { > for (; i < 1024 * 25; i++) { > touch 2M memory > madvise MADV_DONTNEED 2M > } > } >=20 > As we can see, the memory usage of VmPTE is reduced: >=20 > before after > VIRT 50.0 GB 50.0 GB > RES 3.1 MB 3.6 MB > VmPTE 102640 kB 248 kB >=20 > I also have tested the stability by LTP[1] for several weeks. I have no= t seen > any crash so far. >=20 > The performance of page fault can be affected because of the allocation= /freeing > of PTE page table pages. The following is the test result by using a mi= cro > benchmark[2]: >=20 > root@~# perf stat -e page-faults --repeat 5 ./multi-fault $threads: >=20 > threads before (pf/min) after (pf/min) > 1 32,085,255 31,880,833 (-0= .64%) > 8 101,674,967 100,588,311 (-1= .17%) > 16 113,207,000 112,801,832 (-0= .36%) >=20 > (The "pfn/min" means how many page faults in one minute.) >=20 > The performance of page fault is ~1% slower than before. >=20 > This series is based on next-20210812. >=20 > Comments and suggestions are welcome. >=20 > Thanks, > Qi. >=20 Some high-level feedback after studying the code: 1. Try introducing the new dummy primitives ("API") first, and then=20 convert each subsystem individually; especially, maybe convert the whole=20 pagefault handling in a single patch, because it's far from trivial.=20 This will make this series much easier to digest. Then, have a patch that adds actual logic to the dummy primitives via a=20 config option. 2. Minimize the API. a) pte_alloc_get{,_map,_map_lock}() is really ugly. Maybe restrict it to=20 pte_alloc_get(). b) pmd_trans_unstable_or_pte_try_get() and friends are really ugly. Handle it independently for now, even if it implies duplicate runtime=20 checks. if (pmd_trans_unstable() || !pte_try_get()) ... We can always optimize later, once we can come up with something cleaner. 3. Merge #6, and #7, after factoring out all changes to other subsystems=20 to use the API 4. Merge #8 into #6. There is a lot of unnecessary code churn back and=20 forth, and IMHO the whole approach might not make sense without RCU due=20 to the additional locking overhead. Or at least, try to not modify the API you introduced in patch #6 or #7=20 in #8 again. Converting all call sites back and forth just makes review=20 quite hard. I am preparing some some cleanups that will make get_locked_pte() and=20 similar a little nicer to handle. I'll send them out this or next week. --=20 Thanks, David / dhildenb