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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53124C433FE for ; Fri, 21 Oct 2022 14:45:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF6EF8E0002; Fri, 21 Oct 2022 10:45:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA7588E0001; Fri, 21 Oct 2022 10:45:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96F0C8E0002; Fri, 21 Oct 2022 10:45:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8439B8E0001 for ; Fri, 21 Oct 2022 10:45:44 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5517E14148C for ; Fri, 21 Oct 2022 14:45:44 +0000 (UTC) X-FDA: 80045230608.06.1A3A7F1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id F293B1C0036 for ; Fri, 21 Oct 2022 14:45:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666363543; 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=V+5vk+FsZOZt4dAwQMlsBXEEzqeSMBHagbdgyXZGpzE=; b=ODYlWlwdQJLPWQBj6Pmp/lZBhHHFmY92gNbv+VP77agh9/KAxoRZpeojGHF23wTaSWJExX I/I8nNXxDhhGdLz7v+crCujTWkrsd86vWh6Pfyqibq3CHUAePoMlp1t6s8HzxeWb+qEpWC lDkTeBLLLIU1cRkRlP+QfA6E9AooTOg= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-96-xzZuuvstMlqVohiwiVP3Lg-1; Fri, 21 Oct 2022 10:45:42 -0400 X-MC-Unique: xzZuuvstMlqVohiwiVP3Lg-1 Received: by mail-lj1-f197.google.com with SMTP id r5-20020a2e9945000000b0026fdd45e530so1129178ljj.17 for ; Fri, 21 Oct 2022 07:45:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=V+5vk+FsZOZt4dAwQMlsBXEEzqeSMBHagbdgyXZGpzE=; b=ChPnC0AKx8pfT5A6v0Hw6Th15xftZ59ovytfpJPO3jTlSDvPVh5p2FcIhlB9VnMPTe aOAy1D72mhQBUknq9Q5DaUFLIkYebOcjei7vT4F2xRoobvbBd8S+h4BVtGm2iuPLdUEz DTKP+v2Z8+KIwvj0tlLG2GfpBW5t/3wVJ0GSEPk2nmAJaviIGH6bzekQNbqbQZeVtgxN BgOEwmekXfOYU6DGs6L1l+xF4vCndF5F21Xz/osFD7kykXJrLMtnrb1W9G/dTKKQUl/m Za6u4PulMHwKm6RVAeO8ID9iXa4xXhk/8kRcGKI0VEh7LA2upfl9qTebtOY9llDKqm5Z 6WCQ== X-Gm-Message-State: ACrzQf1gF6U44nQxxqCvtbjOISEmGBo69FfX4pk3oCWfArtMN14AqZBz eIXItOhlTgoMJ0FAlE3YKvxO8vbNF/7sPTiE9N6OQaTvs0WeCItFhakRgvYX1i92kau3Q4XggRQ ZICVmP7X3wj8= X-Received: by 2002:a05:6512:68d:b0:4a2:a17b:992c with SMTP id t13-20020a056512068d00b004a2a17b992cmr7186003lfe.239.1666363540591; Fri, 21 Oct 2022 07:45:40 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7K8GrmMx0HCsfFXGivlRQ2g6t8ujiKivX5trq4edT9HtEL97U8LBENac8M60/KCI8L4A79dw== X-Received: by 2002:a17:907:16a6:b0:78e:f140:a9b9 with SMTP id hc38-20020a17090716a600b0078ef140a9b9mr15845457ejc.502.1666363529270; Fri, 21 Oct 2022 07:45:29 -0700 (PDT) Received: from ?IPV6:2003:cb:c708:1700:e40d:574c:c991:5f78? (p200300cbc7081700e40d574cc9915f78.dip0.t-ipconnect.de. [2003:cb:c708:1700:e40d:574c:c991:5f78]) by smtp.gmail.com with ESMTPSA id f19-20020a056402195300b00459cd13fd34sm13878628edz.85.2022.10.21.07.45.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Oct 2022 07:45:28 -0700 (PDT) Message-ID: <4e1f4fb4-559e-2be3-c091-40ce0130b6c3@redhat.com> Date: Fri, 21 Oct 2022 16:45:27 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 Subject: Re: Avoiding allocation of unused shmem page To: Peter Xu Cc: Matthew Wilcox , linux-mm@kvack.org, Hugh Dickins References: From: David Hildenbrand Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666363544; a=rsa-sha256; cv=none; b=OeoepzKBYNH+aOU9bJCkN1cd3Os2ly34GPFZTW80anf8D1kY836XURCL0vpZ8SCcICDyZ5 lMgYclgL7lBBLGAkXCOeBIU9tgKB5GKWiUgltkE3Lw3NTArJqhfIJNRc8aHuFgm4FNX7lL ivIr3HZScitdVnzTLFA0g5efzMM0QD0= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ODYlWlwd; spf=pass (imf18.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666363544; h=from:from:sender: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:dkim-signature; bh=V+5vk+FsZOZt4dAwQMlsBXEEzqeSMBHagbdgyXZGpzE=; b=nXyRoeo1UUAvQ6XrXcFsE0TpNRn2YclUkmjrrsLnP6AsPi/JN7dO66Amna9Kk+bo8UCPfM m3zbSKCULEog1bYyAHFqqusXkQHGdXwyNYzN6SxKlAVBSknDRi7SbHe5KvPGbKOEyeXXdO koLcimbQA0HDVezQEQl7rGla/7gygWE= X-Stat-Signature: w3y5j7jgskhb34bi6t8bgmgku8ifsd4n X-Rspamd-Queue-Id: F293B1C0036 X-Rspam-User: X-Rspamd-Server: rspam03 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ODYlWlwd; spf=pass (imf18.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1666363543-474218 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 21.10.22 16:28, Peter Xu wrote: > On Fri, Oct 21, 2022 at 04:10:41PM +0200, David Hildenbrand wrote: >> On 21.10.22 16:01, Peter Xu wrote: >>> On Fri, Oct 21, 2022 at 09:23:00AM +0200, David Hildenbrand wrote: >>>> On 20.10.22 23:10, Peter Xu wrote: >>>>> On Thu, Oct 20, 2022 at 09:14:09PM +0100, Matthew Wilcox wrote: >>>>>> In yesterday's call, David brought up the case where we fallocate a file >>>>>> in shmem, call mmap(MAP_PRIVATE) and then store to a page which is over >>>>>> a hole. That currently causes shmem to allocate a page, zero-fill it, >>>>>> then COW it, resulting in two pages being allocated when only the >>>>>> COW page really needs to be allocated. >>>>>> >>>>>> The path we currently take through the MM when we take the page fault >>>>>> looks like this (correct me if I'm wrong ...): >>>>>> >>>>>> handle_mm_fault() >>>>>> __handle_mm_fault() >>>>>> handle_pte_fault() >>>>>> do_fault() >>>>>> do_cow_fault() >>>>>> __do_fault() >>>>>> vm_ops->fault() >>>>>> >>>>>> ... which is where we come into shmem_fault(). Apart from the >>>>>> horrendous hole-punch handling case, shmem_fault() is quite simple: >>>>>> >>>>>> err = shmem_get_folio_gfp(inode, vmf->pgoff, &folio, SGP_CACHE, >>>>>> gfp, vma, vmf, &ret); >>>>>> if (err) >>>>>> return vmf_error(err); >>>>>> vmf->page = folio_file_page(folio, vmf->pgoff); >>>>>> return ret; >>>>>> >>>>>> What we could do here is detect this case. Something like: >>>>>> >>>>>> enum sgp_type sgp = SGP_CACHE; >>>>>> >>>>>> if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) >>>>>> sgp = SGP_READ; >>>>> >>>>> Yes this will start to save the space, but just to mention this may start >>>>> to break anything that will still depend on the pagecache to work. E.g., >>>>> it'll change behavior if the vma is registered with uffd missing mode; >>>>> we'll start to lose MISSING events for these private mappings. Not sure >>>>> whether there're other side effects. >>>> >>>> I don't follow, can you elaborate? >>>> >>>> hugetlb doesn't perform this kind of unnecessary allocation and should be fine in regards to uffd. Why should it matter here and how exactly would a problematic sequence look like? >>> >>> Hugetlb is special because hugetlb detects pte first and relies on pte at >>> least for uffd. shmem is not. >>> >>> Feel free to also reference the recent fix which relies on the stable >>> hugetlb pte with commit 2ea7ff1e39cbe375. >> >> Sorry to be dense here, but I don't follow how that relates. >> >> Assume we have a MAP_PRIVATE shmem mapping and someone registers uffd >> missing events on that mapping. >> >> Assume we get a page fault on a hole. We detect no page is mapped and check >> if the page cache has a page mapped -- which is also not the case, because >> there is a hole. >> >> So we notify uffd. >> >> Uffd will place a page. It should *not* touch the page cache and only insert >> that page into the page table -- otherwise we'd be violating MAP_PRIVATE >> semantics. > > That's actually exactly what we do right now... we insert into page cache > for the shmem. See shmem_mfill_atomic_pte(). > > Why it violates MAP_PRIVATE? Private pages only guarantee the exclusive > ownership of pages, I don't see why it should restrict uffd behavior. Uffd > missing mode (afaiu) is defined to resolve page cache missings in this > case. Hugetlb is special but not shmem IMO comparing to most of the rest > of the file systems. If a write (or uffd placement) via a MAP_PRIVATE mapping results in other shared/private mappings from observing these modifications, you have a clear violation of MAP_PRIVATE semantics. -- Thanks, David / dhildenb