Received: by 2002:a05:7412:1703:b0:e2:908c:2ebd with SMTP id dm3csp4127394rdb; Wed, 30 Aug 2023 17:12:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFS0KBxYCeUq46OyArx26oIqpd7+KOCMiFFrl7eCMjTbovbM/Kp4bYIuJ1wHL6hGYqm77JD X-Received: by 2002:a5d:4f8f:0:b0:314:36f0:2214 with SMTP id d15-20020a5d4f8f000000b0031436f02214mr2521021wru.6.1693440761735; Wed, 30 Aug 2023 17:12:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693440761; cv=none; d=google.com; s=arc-20160816; b=wuHfCsNEHdsM4NGuxoI7TME18igFdF1nPCrFAffZBkijEJfoglMlqnPecEFa/HlPVB dF6ccJkEcH17Q0oml0QM3immSjOSN4is9Lbx2jMJdcVdl2g8cRYaW/QoA0QdLiQwB4PZ GJZNUeSMx+Iv5gKd+0PUN+pFJSuWw4I2nVSGUsy5pbeXgxKSs1sncfJp+YDS8AdbMakI OYPGMfyUvUJkV7Lh09Rvgl9+dZruky0+DLulQxw0ojYWyeFq/7UMZNB5Iiq1O1SiYZ2E N3yDIS5oNUxZYYcGU5wJ7XTA8Dfh4ym3TOdX9HqaaeZRerXQ7RN8DO0xcXEC1/JTfBOi miPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version :in-reply-to:date:dkim-signature; bh=Q7U5WETyc0Qx792+SyKBjLV43d3DHT0ObMpMrFVoeXw=; fh=uwY8H9jRxIJcZpPb1xPCVYrj1lOHHQQe44f4QvKq7es=; b=Brb0VQMQDTG3RqVJI0k0qpLg4alBrBlo94nzIc6JCIgmw55hkAbRa1sZw1dTxpzDpb Jlpeeguj1W1Or6f/Cdlg6DrOUubSVU+d/NqzONGjaZYw/q3Zf68aCJvZ4tPV4Y+eL/6d PWtw/IVlCPRdKnISTV3TqBKvuNDjhMt2DOEpIfl4Q8ssqLFO+QmmWEDiGVH5KYgPp05D KcD+KKSfDxhIbO92IoN9m/t7QoYhy701F2KF7CeOEtgBp3Ui60VCVA7SiY9K/Jzye2NC XB5UbrMQJqf024MDuBIHeyglG2x+cMeGz3kTUP3nI2zg4o3ZLZOP2HkFZmNxwgOVXPel f3/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Vrqu6jdV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c16-20020aa7c750000000b00529fae6ac14si239298eds.158.2023.08.30.17.12.13; Wed, 30 Aug 2023 17:12:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Vrqu6jdV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244347AbjH3TVV (ORCPT + 99 others); Wed, 30 Aug 2023 15:21:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343760AbjH3Qo5 (ORCPT ); Wed, 30 Aug 2023 12:44:57 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7DAE1A3 for ; Wed, 30 Aug 2023 09:44:53 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58e4d2b7d16so81993247b3.0 for ; Wed, 30 Aug 2023 09:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1693413893; x=1694018693; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date:from:to :cc:subject:date:message-id:reply-to; bh=Q7U5WETyc0Qx792+SyKBjLV43d3DHT0ObMpMrFVoeXw=; b=Vrqu6jdVSxGa28aE1migk5YVHh9PHn+K3BQigSsynTqZy+1FdBGBQl1nQH4In9WVRs KF+yQOaHMyhG/7vylvJ5xFxpXhxbdaWq++yLYUP/fN2VQ82tBRyWoQyVgRPj/MMHCpZe jO+0V9PUab495XMFnTawLOr1RRUfzotX6k9V6ER1+dm9TYtelUtxG92oglhZ+G48OHTA bw77cd5ga08rV8S2iervR+8tUMuXpINKvMhop7uskl+N/C/qylZpwCA4vMTQyd1AjsSG 0geLp1xw/hWTZsN/r98ZAoO7y0K3ubKpSdpRpvOm2LmVQAtpWFwGs0HAi3r6ak9bzn/q y+rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693413893; x=1694018693; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Q7U5WETyc0Qx792+SyKBjLV43d3DHT0ObMpMrFVoeXw=; b=CigOJhR2dj5Zbc38gAlevb/OcFX1UbTN3D4Cj1Lx2ObXrY8HxdlJ6FMYatFvmv6m+t 6Eaep/rik1p0P1IfoZBIGuFI8MRKVRIQ+7KcnYC1qy8VkycossgSwZJpaKvtEpXgyXS1 sBPKnHaNfQiF8u+qnymE7VwRlpBNXq8ANDkOgawf35ALqgCcTpuoIJzG9grZ+x1rL0oM djjUiochj5NCzAAUCH4d4jzoX56O3CYu6yFJ+wKVySzrF6qHywOTiU4Sjs+hUqI2RyJW qCpeSWFFAL3FOBWlgsoAmLvWghSlBOlYfP54d+fzPM+oHmnTKOZ6XB9JYWRfDUz1JDfu kRsA== X-Gm-Message-State: AOJu0Yx6gBSIUgMk6kIE5tHrzyo6A2YOmROF+8ee05M6XUD1IsjgVH9h FqkKLhzvzZ7L0DFh2vooWALaJFoEY0KgHAlazg== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a81:eb0b:0:b0:57a:793:7fb0 with SMTP id n11-20020a81eb0b000000b0057a07937fb0mr78438ywm.3.1693413893177; Wed, 30 Aug 2023 09:44:53 -0700 (PDT) Date: Wed, 30 Aug 2023 16:44:51 +0000 In-Reply-To: <30ffe039-c9e2-b996-500d-5e11bf6ea789@linux.intel.com> (message from Binbin Wu on Wed, 30 Aug 2023 23:12:19 +0800) Mime-Version: 1.0 Message-ID: Subject: Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory From: Ackerley Tng To: Binbin Wu Cc: seanjc@google.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, maz@kernel.org, oliver.upton@linux.dev, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, willy@infradead.org, akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, chao.p.peng@linux.intel.com, tabba@google.com, jarkko@kernel.org, yu.c.zhang@linux.intel.com, vannapurve@google.com, mail@maciej.szmigiero.name, vbabka@suse.cz, david@redhat.com, qperret@google.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Binbin Wu writes: >> >> >> +static long kvm_gmem_allocate(struct inode *inode, loff_t offset, loff_t len) >> +{ >> + struct address_space *mapping = inode->i_mapping; >> + pgoff_t start, index, end; >> + int r; >> + >> + /* Dedicated guest is immutable by default. */ >> + if (offset + len > i_size_read(inode)) >> + return -EINVAL; >> + >> + filemap_invalidate_lock_shared(mapping); >> + >> + start = offset >> PAGE_SHIFT; >> + end = (offset + len) >> PAGE_SHIFT; >> + >> + r = 0; >> + for (index = start; index < end; ) { >> + struct folio *folio; >> + >> + if (signal_pending(current)) { >> + r = -EINTR; >> + break; >> + } >> + >> + folio = kvm_gmem_get_folio(inode, index); >> + if (!folio) { >> + r = -ENOMEM; >> + break; >> + } >> + >> + index = folio_next_index(folio); >> + >> + folio_unlock(folio); >> + folio_put(folio); > May be a dumb question, why we get the folio and then put it immediately? > Will it make the folio be released back to the page allocator? > I was wondering this too, but it is correct. In filemap_grab_folio(), the refcount is incremented in three places: + When the folio is created in filemap_alloc_folio(), it is given a refcount of 1 in filemap_alloc_folio() -> folio_alloc() -> __folio_alloc_node() -> __folio_alloc() -> __alloc_pages() -> get_page_from_freelist() -> prep_new_page() -> post_alloc_hook() -> set_page_refcounted() + Then, in filemap_add_folio(), the refcount is incremented twice: + The first is from the filemap (1 refcount per page if this is a hugepage): filemap_add_folio() -> __filemap_add_folio() -> folio_ref_add() + The second is a refcount from the lru list filemap_add_folio() -> folio_add_lru() -> folio_get() -> folio_ref_inc() In the other path, if the folio exists in the page cache (filemap), the refcount is also incremented through filemap_grab_folio() -> __filemap_get_folio() -> filemap_get_entry() -> folio_try_get_rcu() I believe all the branches in kvm_gmem_get_folio() are taking a refcount on the folio while the kernel does some work on the folio like clearing the folio in clear_highpage() or getting the next index, and then when done, the kernel does folio_put(). This pattern is also used in shmem and hugetlb. :) I'm not sure whose refcount the folio_put() in kvm_gmem_allocate() is dropping though: + The refcount for the filemap depends on whether this is a hugepage or not, but folio_put() strictly drops a refcount of 1. + The refcount for the lru list is just 1, but doesn't the page still remain in the lru list? >> + >> + /* 64-bit only, wrapping the index should be impossible. */ >> + if (WARN_ON_ONCE(!index)) >> + break; >> + >> + cond_resched(); >> + } >> + >> + filemap_invalidate_unlock_shared(mapping); >> + >> + return r; >> +} >> + >> >>