From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="37bbQvFD" Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5782F1AA for ; Tue, 12 Dec 2023 18:22:02 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-28a30542c37so2284272a91.1 for ; Tue, 12 Dec 2023 18:22:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702434122; x=1703038922; 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=H3dbLwG3pwrVNc+flPqWL0ROEahdeYkxOItBObS2igQ=; b=37bbQvFDGMCUMMmZAr6gnZV6N9L25995j3u+veppJTxeYq/glZSQPf/JqGcWsBupsH 4aAxmcHxa4PBX4wpSMjZEjG9Ou0D9n11VzOdnGE7fWAx+cqGwrKrjuYG08NPNy6OwH6M Sd0Eyh+C/LsHQtQqVWiryloo/wA/YVYu+GMtDT4Oio/RSmodHMiAxdE32iibzs8CcE4A Wn6Fxthx7HQF/QvqNJf634cY64ed/8gUcc8ZftZ7myx5GRt7C+oRIyIYNdaOe0hJSH9f 0fQWrAy0yEz7P9/jB75hj/9Y5/xYSqQrCfxFhiHOGw4nN6CF58Lg/DMgUaOfhp5uERkU ecOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702434122; x=1703038922; 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=H3dbLwG3pwrVNc+flPqWL0ROEahdeYkxOItBObS2igQ=; b=sKLGjw2FF0rNmcrOFj2piqQwkfoJ2lIOOGZcWALsgmKrQSkjRwCRktJ3iGZxxR0Juc 5NM40Sjn/xW7JOdY+rSgpn8SjLoWenfXnPCTUvD4xDVqUOxRtOJeh9OY4L5B2MD+VpGo GNBGlXRTdyBWMtWVcwuDZUMB/5616wZha0PbYJjrODXXHg3PdQfYROOhzYSNpLVmIIlz wEdmXFxSu4YT+5tyIb4+gfDSoVPOCzm+i0NZvVPr9NQ2thbPcPzv+tGoukcU5lZn9xiY q6Db8bsdBQlTAwgAulFu08wVqSlPAAV59j8mETX8IOEtTBwJ9I4ghcQd99IrrvZdw1VD jXrg== X-Gm-Message-State: AOJu0YwG/bWWxkmsh4ksKVoVSfHSrCEbeQAvOHhi0Yx3me4QtVcIK8sY Td7uDQFMU3zvdSUuyiyllKojhbmzHKs= X-Google-Smtp-Source: AGHT+IEMdNbXZWy9ISUMeWXjYiRzXRSqXuBEtLtB2quA6rPLGk41btEuk2vriof3H/B+MEphfzV6B3hdgbQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:41c9:b0:1d0:cd87:64dd with SMTP id u9-20020a17090341c900b001d0cd8764ddmr52067ple.3.1702434121579; Tue, 12 Dec 2023 18:22:01 -0800 (PST) Date: Tue, 12 Dec 2023 18:22:00 -0800 In-Reply-To: <20231203140756.GI1489931@ziepe.ca> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20230916003118.2540661-1-seanjc@google.com> <20230916003118.2540661-6-seanjc@google.com> <20230918152110.GI13795@ziepe.ca> <20230918160258.GL13795@ziepe.ca> <20231203140756.GI1489931@ziepe.ca> Message-ID: Subject: Re: [PATCH 05/26] vfio: KVM: Pass get/put helpers from KVM to VFIO, don't do circular lookup From: Sean Christopherson To: Jason Gunthorpe Cc: Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Peter Zijlstra , Arnaldo Carvalho de Melo , Paolo Bonzini , Tony Krowiak , Halil Pasic , Jason Herne , Harald Freudenberger , Alex Williamson , Andy Lutomirski , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, kvm@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Anish Ghulati , Venkatesh Srinivas , Andrew Thornton Content-Type: text/plain; charset="us-ascii" On Sun, Dec 03, 2023, Jason Gunthorpe wrote: > On Fri, Dec 01, 2023 at 04:51:55PM -0800, Sean Christopherson wrote: > > > There's one more wrinkle: this patch is buggy in that it doesn't ensure the liveliness > > of KVM-the-module, i.e. nothing prevents userspace from unloading kvm.ko while VFIO > > still holds a reference to a kvm structure, and so invoking ->put_kvm() could jump > > into freed code. To fix that, KVM would also need to pass along a module pointer :-( > > Maybe we should be refcounting the struct file not the struct kvm? > > Then we don't need special helpers and it keeps the module alive correctly. Huh. It took my brain a while to catch up, but this seems comically obvious in hindsight. I *love* this approach, both conceptually and from a code perspective. Handing VFIO (and any other external entities) a file makes it so that KVM effectively interacts with users via files, regardless of whether the user lives in userspace or the kernel. That makes it easier to reason about the safety of operations, e.g. in addition to ensuring KVM-the-module is pinned, having a file pointer allows KVM to verify that the incoming pointer does indeed represent a VM. Which isn't necessary by any means, but it's a nice sanity check. >From a code perspective, it's far cleaner than manually grabbing module references, and having only a file pointers makes it a wee bit harder for non-KVM code to poke into KVM, e.g. keeps us honest. Assuming nothing blows up in testing, I'll go this route for v2. Thanks!