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 1E54AC4332F for ; Fri, 21 Oct 2022 15:08:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 78B738E0002; Fri, 21 Oct 2022 11:08:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7148C8E0001; Fri, 21 Oct 2022 11:08:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B50B8E0002; Fri, 21 Oct 2022 11:08:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 48CD78E0001 for ; Fri, 21 Oct 2022 11:08:35 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F40E3C1316 for ; Fri, 21 Oct 2022 15:08:34 +0000 (UTC) X-FDA: 80045288190.01.85A69E2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 41FCEA0039 for ; Fri, 21 Oct 2022 15:08:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666364913; 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: in-reply-to:in-reply-to:references:references; bh=m6SocxnYjQugzizo76WEIPkutjB4f3XenRrtu3rHTUk=; b=Vmo9jgHeOTRjRqd0r+bWo2hEcRaqlQuiIocOVUNxqO0KkCWcLLKYJ/12jHbG1bx4h1Rt/V mMwuRQXjBrDTMAEq9IRB0tne0VfZ/4hpz6Tn8zUnouvEwsNohCWsWkqE+zcuy1Bo05piBk OHfsFutpZWUnu3ZCVvGIZlO5cBl6Ijc= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-217-3vSMqweEPZC3hr9ZJ87lRA-1; Fri, 21 Oct 2022 11:08:32 -0400 X-MC-Unique: 3vSMqweEPZC3hr9ZJ87lRA-1 Received: by mail-qk1-f198.google.com with SMTP id n13-20020a05620a294d00b006cf933c40feso3738696qkp.20 for ; Fri, 21 Oct 2022 08:08:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=m6SocxnYjQugzizo76WEIPkutjB4f3XenRrtu3rHTUk=; b=NBFPyLeGPcC7fWRy6Gtd/I76vWTxGdLSZryJFVO/SAIdbh4maNRcMlUfyhGd9mUWd4 79IJCPBHKJ0oO8tUVd5Z9V6qaQ5f8RLuacXXOTQEt7+5l6b9Dv/s5bQIN3oGZXpavaVa ZcArzvEYwSJFnVOEZoBnYaud62R7KhfU32y7gi4kzNlHL1t6Y+INLQzMRyCbggC5yOPl 0UuH/zUaeHYvzYMHydatI4Fb7U+EzCN+Hd+ZOM0t5EU6J5Q/Qk3dYsIqzZNZk/ZNCgkP 1s7gWBTGofR8iE8xUlbtDpAcqblGNY48kNcnWFMLDVxxjvRwKFW7hAvIY4g7hFjJDnWp L4hA== X-Gm-Message-State: ACrzQf1+NwV4d/4dYBjnxS/ONdBMLZ3AroeodUbCXRaYdOylwsG/03+o oujn7pi0j9NiAxLukFagDAzww0bp5ZaRUI2Lm/JFgkKvW2pqi2yOI32kvTKi1Yt9zFKSkD1yw13 KmnUhAm1jhSQ= X-Received: by 2002:ac8:5a8d:0:b0:39c:f9c3:6575 with SMTP id c13-20020ac85a8d000000b0039cf9c36575mr14941779qtc.655.1666364912102; Fri, 21 Oct 2022 08:08:32 -0700 (PDT) X-Google-Smtp-Source: AMsMyM71xxI3WERCIEqCrbjzf9U3MdTv8nq3izP0hfvbOVNqA1Ig4I51EFRMT85XVMR50SC30M/gPA== X-Received: by 2002:ac8:5a8d:0:b0:39c:f9c3:6575 with SMTP id c13-20020ac85a8d000000b0039cf9c36575mr14941746qtc.655.1666364911822; Fri, 21 Oct 2022 08:08:31 -0700 (PDT) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id cd15-20020a05622a418f00b0035bafecff78sm8103059qtb.74.2022.10.21.08.08.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Oct 2022 08:08:31 -0700 (PDT) Date: Fri, 21 Oct 2022 11:08:30 -0400 From: Peter Xu To: David Hildenbrand Cc: Matthew Wilcox , linux-mm@kvack.org, Hugh Dickins Subject: Re: Avoiding allocation of unused shmem page Message-ID: References: <4e1f4fb4-559e-2be3-c091-40ce0130b6c3@redhat.com> MIME-Version: 1.0 In-Reply-To: <4e1f4fb4-559e-2be3-c091-40ce0130b6c3@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Vmo9jgHe; spf=pass (imf15.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666364914; a=rsa-sha256; cv=none; b=b0bNzKMTSL+Zpz2/oB4IvF7HBU+EUA3L8BQRRhsWH3oPvg+keJzzffQqiEpAZlU3IR5Q4Y ce9nGXn0XV9awq1IT6ncypcw2ukFpV+Ln38Jc2dVs9DgADpMbImxkOsUBTc/+1pYUSTzKL fh8ZIDxB2webE8NGQI/6b2ZcO6p7qTE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666364914; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=m6SocxnYjQugzizo76WEIPkutjB4f3XenRrtu3rHTUk=; b=Oj/YVDxwwbO3z36gDTrn5ZGr0iBLFyaSs0WO1L3W6K2qN2xYbjGkVCH4wOFvKSIYQ7An01 aNfxK1iCQL7fMUuSoz1yKVVbcY7cJ2Fm1878xFtthy6PnLSEFtFXItCSmSIHDpZZOY1lL+ tA6FFScRQq7pcljPlgrNazppNGHGzG0= X-Rspamd-Queue-Id: 41FCEA0039 Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Vmo9jgHe; spf=pass (imf15.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam09 X-Stat-Signature: d1zs7fdn34b7xadit3e46fioqj4rambg X-HE-Tag: 1666364914-793175 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 Fri, Oct 21, 2022 at 04:45:27PM +0200, David Hildenbrand wrote: > 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. I think I understand what you meant, but just to mention again that I think it's a matter of how we defined the uffd missing semantics when shmem missing mode was introduced years ago. It does not need to be the same semantic as writting directly to a private mapping. -- Peter Xu