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=-5.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 45A1DC433DB for ; Mon, 22 Mar 2021 09:34:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BFA4160233 for ; Mon, 22 Mar 2021 09:34:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BFA4160233 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6741D6B009F; Mon, 22 Mar 2021 05:15:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 622186B00A0; Mon, 22 Mar 2021 05:15:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C3156B00A1; Mon, 22 Mar 2021 05:15:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0193.hostedemail.com [216.40.44.193]) by kanga.kvack.org (Postfix) with ESMTP id 30D786B009F for ; Mon, 22 Mar 2021 05:15:47 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 884F78249980 for ; Mon, 22 Mar 2021 09:34:37 +0000 (UTC) X-FDA: 77947000194.24.738716B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf21.hostedemail.com (Postfix) with ESMTP id ED562E0011F4 for ; Mon, 22 Mar 2021 09:34:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616405676; 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=Ry44D9E+qZ0XK98n0VE4JTxTsKSOtGnab81CpAKAB0E=; b=iMVdZV3oS/Vk7aNJKbML8GjodrpNEDamjj3rThPkoss4AQTGfpmweC362PMgMYmdI4oZsC 8KNx9fBAwTk4DqvXQ2P2ThL3NgoV238pchTf9pVq5Df0pg8/lIQUGf6yDZzoIO099eh0Cm 5O3iEvthj4XBDs5iflal75qKLRKmYDQ= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-182-IcQ77zTTP_u35v1qlZ0-zw-1; Mon, 22 Mar 2021 05:34:34 -0400 X-MC-Unique: IcQ77zTTP_u35v1qlZ0-zw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4C44C18C8C02; Mon, 22 Mar 2021 09:34:33 +0000 (UTC) Received: from [10.36.115.54] (ovpn-115-54.ams2.redhat.com [10.36.115.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id 26CED60C04; Mon, 22 Mar 2021 09:34:27 +0000 (UTC) To: Yang Shi Cc: Peter Xu , Linux Memory Management List , Minchan Kim , Matthew Wilcox , Rik van Riel , Michal Hocko , Andrea Arcangeli References: <20210311212628.GK194839@xz-x1> <634820ea-33bf-7423-cdea-ab3e63aa9729@redhat.com> From: David Hildenbrand Organization: Red Hat GmbH Subject: Re: Page zapping and page table reclaim Message-ID: <53b83aae-01d7-156d-d1bb-836002521005@redhat.com> Date: Mon, 22 Mar 2021 10:34:27 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: ED562E0011F4 X-Stat-Signature: w8ib1jugqqfzqg1pj63k9z1xwt5kwb39 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf21; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=170.10.133.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616405676-727982 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.03.21 18:04, Yang Shi wrote: > On Thu, Mar 11, 2021 at 1:35 PM David Hildenbrand wr= ote: >> >> On 11.03.21 22:26, Peter Xu wrote: >>> On Thu, Mar 11, 2021 at 07:14:02PM +0100, David Hildenbrand wrote: >>>> I was wondering, is there any mechanism that reclaims basically empt= y page >>>> tables in a running process? >>> >>> Would munmap() count? :) >> >> Haha, no -- also not mmap(FIXED) or mremap(FIXED) ;) >> >> As so often lately, the use case is sparse memory mappings where we >> >> a) may want to reuse the area later. >> b) don't want to hold the mmap lock in write while optimizing >> c) don't want to create a lot of individual mappings that we might not >> be able to merge again. >=20 > Will the below work for you? >=20 > 1. acquire write mmap lock > 2. unlink vmas from the list and rbtree (so the vmas won't be visible > to any concurrent readers/writers) > 3. downgrade write lock to read lock > 4. zap page tables and free page tables > 5. upgrade to write lock > 6. relink vmas back to list and rbtree >=20 > Actually the current implementation of munmap() does the first 5 steps. That's almost mmap(MAP_FIXED) for the cases where we can merge VMAs. But=20 I don't think this is actually what we want. We don't want to do such=20 optimizations while we're in mmap-read-locked MADV_DONTNEED etc. Simple example: QEMU implements memory ballooning for its VMs via=20 virtio-balloon. When the guest inflates/deflates 4k pages and we're=20 using anonymous memory, we issue madvise(MADV_DONTNEED) syscalls for=20 each 4k page. At some point, we might be able to reclaim page tables -=20 but we don't want to suddenly take the mmap lock in write during=20 madvise() when there is no actual memory pressure, or scan for=20 optimization opportunities during every syscall. User space pretty much=20 relies on madvise(DONTNEED) being fast and little intrusive. I think there might be other cases where we can reclaim page tables as=20 well, not necessarily triggered by user space. For example, after we=20 wrote back/evicted a sequence of file-mapped pages, I would assume that=20 we might also be able to reclaim page tables, but I haven't looked into=20 it yet. For now, I mostly care about page table reclaim for the cases=20 where we discard pages from page tables completely (MADV_DONTNEED,=20 MADV_FREE, MADV_REMOVE, fallocate(PUNCH_HOLE)). I envision page table reclaim to happen asynchronously, either=20 periodically once under memory pressure, or once sufficient evidence is=20 there that reclaim might make sense. There, similarly to khugepaged, we=20 might have to temporarily take the mmap lock in write for a short period=20 in time, but I'll have to look into the details first. --=20 Thanks, David / dhildenb