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 C3C48C433DB for ; Thu, 18 Feb 2021 10:45:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2AE6464EAF for ; Thu, 18 Feb 2021 10:45:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2AE6464EAF 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 7363B6B0006; Thu, 18 Feb 2021 05:45:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E7DF6B006C; Thu, 18 Feb 2021 05:45:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E1006B006E; Thu, 18 Feb 2021 05:45:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0025.hostedemail.com [216.40.44.25]) by kanga.kvack.org (Postfix) with ESMTP id 471666B0006 for ; Thu, 18 Feb 2021 05:45:01 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id EDEA4180AD820 for ; Thu, 18 Feb 2021 10:45:00 +0000 (UTC) X-FDA: 77831055960.30.C06BD64 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 2283AC0001F7 for ; Thu, 18 Feb 2021 10:44:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613645099; 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=BMH8S4T7u+QFmoTuKd8zzsW1EwjP83j2mvXgWWp0OT0=; b=HBofra4iKdCbY7r1xj9jayR+yXaFThEnxBitgPwMC1wZy17HHq94F9T0wXrbXj8rTz5SOO bmqy7wjsL8g970IGRiupn2a2WBbwTMrXw4kzoYcoyIYepalbRP1Nnf7iMGr0n4Vd9ghrZy dBvU7rAcQbcIUifLDXbgVwyyWbe2uRo= 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-558-EbN0LvXNPNW1e9tnCjCIdA-1; Thu, 18 Feb 2021 05:44:55 -0500 X-MC-Unique: EbN0LvXNPNW1e9tnCjCIdA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 11B17107ACE6; Thu, 18 Feb 2021 10:44:52 +0000 (UTC) Received: from [10.36.114.59] (ovpn-114-59.ams2.redhat.com [10.36.114.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4016110016FD; Thu, 18 Feb 2021 10:44:42 +0000 (UTC) To: Michal Hocko Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Arnd Bergmann , Oscar Salvador , Matthew Wilcox , Andrea Arcangeli , Minchan Kim , Jann Horn , Jason Gunthorpe , Dave Hansen , Hugh Dickins , Rik van Riel , "Michael S . Tsirkin" , "Kirill A . Shutemov" , Vlastimil Babka , Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Chris Zankel , Max Filippov , linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-xtensa@linux-xtensa.org, linux-arch@vger.kernel.org References: <20210217154844.12392-1-david@redhat.com> From: David Hildenbrand Organization: Red Hat GmbH Subject: Re: [PATCH RFC] mm/madvise: introduce MADV_POPULATE to prefault/prealloc memory Message-ID: <3763a505-02d6-5efe-a9f5-40381acfbdfd@redhat.com> Date: Thu, 18 Feb 2021 11:44:41 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2283AC0001F7 X-Stat-Signature: pxfai5b1un3ggmhcrjyr7ynfzdtykhwe Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf03; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=63.128.21.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1613645098-534587 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 18.02.21 11:25, Michal Hocko wrote: > On Wed 17-02-21 16:48:44, David Hildenbrand wrote: >> When we manage sparse memory mappings dynamically in user space - also >> sometimes involving MADV_NORESERVE - we want to dynamically populate/ >=20 > Just wondering what is MADV_NORESERVE? I do not see anything like that > in the Linus tree. Did you mean MAP_NORESERVE? Most certainly, thanks :) >=20 >> discard memory inside such a sparse memory region. Example users are >> hypervisors (especially implementing memory ballooning or similar >> technologies like virtio-mem) and memory allocators. In addition, we w= ant >> to fail in a nice way if populating does not succeed because we are ou= t of >> backend memory (which can happen easily with file-based mappings, >> especially tmpfs and hugetlbfs). >=20 > by "fail in a nice way" you mean before a #PF would fail and SIGBUS > which would be harder to handle? Yes. >=20 > [...] >> Because we don't have a proper interface, what applications >> (like QEMU and databases) end up doing is touching (i.e., writing) all >> individual pages. However, it requires expensive signal handling (SIGB= US); >> for example, this is problematic in hypervisors like QEMU where SIGBUS >> handlers might already be used by other subsystems concurrently to e.g= , >> handle hardware errors. "Simply" doing preallocation from another thre= ad >> is not that easy. >=20 > OK, that clarifies my above question. >=20 >> >> Let's introduce MADV_POPULATE with the following semantics >> 1. MADV_POPULATED does not work on PROT_NONE and special VMAs. It work= s >> on everything else. >=20 > This would better clarify what "does not work" means. I assume those ar= e > ignored and do not report any error? I'm currently preparing the man page. "Fail with -ENOMEM" (like=20 MADV_DONTNEED or MADV_REMOVE) >=20 >> 2. Errors during MADV_POPULATED (especially OOM) are reported. >=20 > How do you want to achieve that? gup/page fault handler will allocate > memory and trigger the oom without caller noticing that. You would > somehow have to weaken the allocation context to GFP_RETRY_MAYFAIL or > NORETRY to achieve the error handling. Okay, I should be more clear here (again, I'm realizing this as well=20 while I create the man page), OOM is confusing: avoid SIGBUS at runtime=20 - like we would get on actual file systems/shmem/hugetlbfs when=20 preallocating. It cannot save us from the actual OOM killer. To handle anonymous memory=20 more reliable I'll need other means as well (dynamic swap space=20 allocation for sparse mappings). >=20 >> If we hit >> hardware errors on pages, ignore them - nothing we really can or >> should do. >> 3. On errors during MADV_POPULATED, some memory might have been >> populated. Callers have to clean up if they care. >=20 > How does caller find out? madvise reports 0 on success so how do you > find out how much has been populated? If there is an error, something might have been populated. In my QEMU=20 implementation, I simply discard the range again, good enough. I don't=20 think we need to really indicate "error and populated" or "error and not=20 populated". >=20 >> 4. Concurrent changes to the virtual memory layour are tolerated - we >> process each and every PFN only once, though. >=20 > I do not understand this. madvise is about virtual address space not a > physical address space. What I wanted to express: if we detect a change in the mapping we don't=20 restart at the beginning, we always make forward progress. We process=20 each virtual address once (on a per-page basis, thus I accidentally used=20 "PFN"). >=20 >> 5. If MADV_POPULATE succeeds, all memory in the range can be accessed >> without SIGBUS. (of course, not if user space changed mappings in = the >> meantime or KSM kicked in on anonymous memory). >=20 > I do not see how KSM would change anything here and maybe it is not > really important to mention it. KSM should be really transparent from > the users space POV. Parallel and destructive virtual address space > operations are also expected to change the outcome and there is nothing > kernel do about at and provide any meaningful guarantees. I guess we > want to assume a reasonable userspace behavior here. It's just a note that we cannot protect from someone interfering=20 (discard/ksm/whatever). I'm making that clearer in the cover letter. Thanks! --=20 Thanks, David / dhildenb