From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 672E01DDE0 for ; Tue, 31 Oct 2023 14:10:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="z0MqeRGv" Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24B5B102 for ; Tue, 31 Oct 2023 07:10:11 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5afacc566f3so49349277b3.3 for ; Tue, 31 Oct 2023 07:10:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698761410; x=1699366210; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qnQOjymnTrZBwkJL50GP/1+n+ilT5bo3LZ+viwIL69A=; b=z0MqeRGvW14GWNa8pBelMmpO5+NOF/ApuojoVC/wg81KjdUsKW6wOPu//fOextVE0Z Qt6UX9h2DGe3YLEoN3v6XDAtiLFYP7oGE/y8JZuBhkBwhQRiPEUtwoQ87l5ttGRT6jmv qI7Xpi1BD3/JUOgEf/QDlBlZmbDWKmYL6uX2WvhboDMh1GE/x3IB2wNflFafzbNEdKbI niKUgr+KIj25mNFMmbtDlI550afgDLnfAVYF08BBDzODC7Sx8lnhlNYkosEQ4/FiifKv V1nzmKlbqP+6hEueCBMfqWOm/cJ68GySDPMzlcA6iBeM0WUymUuVtz3q/hO9uHELfIRR JhGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698761410; x=1699366210; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qnQOjymnTrZBwkJL50GP/1+n+ilT5bo3LZ+viwIL69A=; b=CqAAC5cphCJ1FRkAlO/tuoY7Rb6n+V3JKvHiCMKwVgaWOKhX/oNxl2AlbsVlpwx9U6 qL+t2Zn9LCDpp13fFRueOOkxcvwDKR5QaRDmUPTLnmGhfKuiW+onpyQzq145xt/uhlQJ 84qa3TjhiuhVgH5cUYsqVw43rL/FXthUsjlRUdU9uLvYF7CYD9ykwGA5td2xOUR5I2fL 94TWSYSI7xFlIVy/77EJd51KzJ4Ss4TTuqj0z6xGk8Q7NhqBelUHd6JeyJiKnwOe15Ys dn3OyXGbcTrT6gXBl1JQ/b/7j6osAN7jSDECG8S40ZaDuaxBasJTKI2g4a8z9JfYbUcG sHXw== X-Gm-Message-State: AOJu0YyAmI+Ra+eov352G1u/2S2BuGCYxNiT2xy3Pa76NeqoDVzROmzQ bssLPUclTY19DUuPu7H1x9sy+BrEP3c= X-Google-Smtp-Source: AGHT+IHZDXQi7UJsz3IrNX3ZHQxUCVwQKR+0lKBMLeJ46WZVaPkGJ08yzTLT3JODvkoGCJ0IGZA0c9r9iJw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:a14c:0:b0:59b:eea4:a5a6 with SMTP id y73-20020a81a14c000000b0059beea4a5a6mr274540ywg.0.1698761410163; Tue, 31 Oct 2023 07:10:10 -0700 (PDT) Date: Tue, 31 Oct 2023 07:10:08 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> <20231027182217.3615211-17-seanjc@google.com> Message-ID: Subject: Re: [PATCH v13 16/35] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory From: Sean Christopherson To: Chao Gao Cc: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton , 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-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , "=?utf-8?Q?Micka=C3=ABl_Sala=C3=BCn?=" , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="us-ascii" On Tue, Oct 31, 2023, Chao Gao wrote: > >+int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) > >+{ > >+ loff_t size = args->size; > >+ u64 flags = args->flags; > >+ u64 valid_flags = 0; > >+ > >+ if (flags & ~valid_flags) > >+ return -EINVAL; > >+ > >+ if (size < 0 || !PAGE_ALIGNED(size)) > >+ return -EINVAL; > > is size == 0 a valid case? Nope, this is a bug. > >+ if (!xa_empty(&gmem->bindings) && > >+ xa_find(&gmem->bindings, &start, end - 1, XA_PRESENT)) { > >+ filemap_invalidate_unlock(inode->i_mapping); > >+ goto err; > >+ } > >+ > >+ /* > >+ * No synchronize_rcu() needed, any in-flight readers are guaranteed to > >+ * be see either a NULL file or this new file, no need for them to go > >+ * away. > >+ */ > >+ rcu_assign_pointer(slot->gmem.file, file); > >+ slot->gmem.pgoff = start; > >+ > >+ xa_store_range(&gmem->bindings, start, end - 1, slot, GFP_KERNEL); > >+ filemap_invalidate_unlock(inode->i_mapping); > >+ > >+ /* > >+ * Drop the reference to the file, even on success. The file pins KVM, > >+ * not the other way 'round. Active bindings are invalidated if the > >+ * file is closed before memslots are destroyed. > >+ */ > >+ fput(file); > >+ return 0; > >+ > >+err: > >+ fput(file); > >+ return -EINVAL; > > The cleanup, i.e., filemap_invalidate_unlock() and fput(), is common. So, I think it > may be slightly better to consolidate the common part e.g., I would prefer to keep this as-is. Only goto needs the unlock, and I find it easier to understand the success vs. error paths with explicit returns. But it's not a super strong preference.