From patchwork Fri Apr 15 20:15:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12815265 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF12DC433EF for ; Fri, 15 Apr 2022 20:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345491AbiDOUSU (ORCPT ); Fri, 15 Apr 2022 16:18:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244871AbiDOUSS (ORCPT ); Fri, 15 Apr 2022 16:18:18 -0400 Received: from mail-io1-xd49.google.com (mail-io1-xd49.google.com [IPv6:2607:f8b0:4864:20::d49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1562A3B287 for ; Fri, 15 Apr 2022 13:15:50 -0700 (PDT) Received: by mail-io1-xd49.google.com with SMTP id b15-20020a05660214cf00b00648a910b964so5323315iow.19 for ; Fri, 15 Apr 2022 13:15:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=/w+lI2clq4aNvOBAeM7isWx8sEkr2kSrYxVwFxgdH8c=; b=PTb/sr8g/+4WvlVNyz1ZQ16eC0D/jMtPUZOzTMdo30fC7qDZJ3N+DJU4ag58CGX+Kb n2/yTJpFO84bciYqJJN8jguqU8y4v8T9M5vsOjExn1Jxoy+GViUWdBS32oHP0C0TGdvT T30F2O2h/JS6L8vldgGZwowmLlud+OTUNOUajIn1+XItVq10zDYw5HXShHfdf81XutPl IJ9abUzriaLGciLZHolzxnhuCj6GEG20xEiYTw2VXbT4vV2qSVnktSZqvu+7eaogcyPb LTY/+JDglaqfXrR2ra/EK9BA2CAAzYx6AZxzKJ2E9+a1US+ZnlbYXdqfWScNocFyu9Hb ByQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/w+lI2clq4aNvOBAeM7isWx8sEkr2kSrYxVwFxgdH8c=; b=yID0lOv7ftwI8CDPvzWtWQCrjHeJuHKpeeCPaQHcNdHgKUHKkPX3nEVcdbdPje7T57 wa+t4KSyuKTK/lh9HT4+1AWM33WmbYS6CxISxlY14RSHilh5Xbbt9qR8x2/ybrSo7xYL p/LbIE6dZztoLs8KTOF60aOkBtArk1GzIZbd8mK6tVb/kY0GF9dIahv3UYUFyu9RJYeD NcuXDi3bXfxfJ+v5x2JnuvPcmrr6gn4Aq4VArK0Jg4X0dMFOIyEiHbR8y/Ni7m8l9rV5 KHI4OOFZ86CpdknzEhjtY1nUSeJWpto4+Oa7c5Xa+KSce338qmswJizbhtA9pZ3+lClU vyEg== X-Gm-Message-State: AOAM533FuOf11o32ULvfjILegXQRo9lUv+I7VuNi9FhEMlCKhRQcvWcz g3uB/AIRU+kwpdWinANEgj51LZCZz9U3Uc6r2y2RCa9qLs0wJ3GKaI6T/sneWFQfrdB+AplVg/F nw+yMwPEmEJatQcecMMR0tOIc5sv65zBTEuai6X8I51hyk1sRnuLyPt0VsA== X-Google-Smtp-Source: ABdhPJytbJcWTyREI5pIGYxtYH7uU6jRUcLhUJUmVsFvBMtpc5jJDsv51yk6RHwLISubEgAAn4yoS5KyWGQ= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a02:9999:0:b0:314:4a83:ac2e with SMTP id a25-20020a029999000000b003144a83ac2emr310808jal.37.1650053749367; Fri, 15 Apr 2022 13:15:49 -0700 (PDT) Date: Fri, 15 Apr 2022 20:15:38 +0000 In-Reply-To: <20220415201542.1496582-1-oupton@google.com> Message-Id: <20220415201542.1496582-2-oupton@google.com> Mime-Version: 1.0 References: <20220415201542.1496582-1-oupton@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH 1/5] KVM: Shove vm stats_id init into kvm_create_vm_debugfs() From: Oliver Upton To: kvm@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, maz@kernel.org, kvmarm@vger.kernel.org, Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The field is only ever used for debugfs; put the initialization where it belongs. Signed-off-by: Oliver Upton --- virt/kvm/kvm_main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d292c4397579..ec9c6aad041b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -955,6 +955,9 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc + kvm_vcpu_stats_header.num_desc; + snprintf(kvm->stats_id, sizeof(kvm->stats_id), + "kvm-%d", task_pid_nr(current)); + if (!debugfs_initialized()) return 0; @@ -4765,9 +4768,6 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) if (r < 0) goto put_kvm; - snprintf(kvm->stats_id, sizeof(kvm->stats_id), - "kvm-%d", task_pid_nr(current)); - file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR); if (IS_ERR(file)) { put_unused_fd(r); From patchwork Fri Apr 15 20:15:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12815266 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85C0BC433EF for ; Fri, 15 Apr 2022 20:15:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351201AbiDOUSX (ORCPT ); Fri, 15 Apr 2022 16:18:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345497AbiDOUSU (ORCPT ); Fri, 15 Apr 2022 16:18:20 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 456423B287 for ; Fri, 15 Apr 2022 13:15:51 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id n10-20020a25da0a000000b0064149e56ecaso7500485ybf.2 for ; Fri, 15 Apr 2022 13:15:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=C00yhYhZJTIznYJGDu1kufq84AOu9nR23r5srBdqR58=; b=hZSGBh4QeBitpIRiMTt4RYNSsHhcsxGXnYBEULXcOW1qRzaJyrjHBNl7LwqkNYWO4P 9W60VTXkTaIfZsjKvzQ+OqkL33rmPplyGQfLqqEvZKTrKmZqlbr2bl25VwCZNjG+3TfJ TFDhp67zqYCZZ+LX3PRuzY4sZqXJF1uNFgW3dlTcLVl0wqlS8AapjC3+b7kod41pNCU6 wOxSdaSae1XuUNA23ITwGNFvmgS93/HdY4gKFhwJGQrCQ966NY+DbG8kmtJrwLqHA0f3 APo3hvlfxCaAfg4DtBhrEQhe0XDtJWkI0imVYc1U4QpPMo9nB/nr+LLQmOEqVOhBCc1r p8YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=C00yhYhZJTIznYJGDu1kufq84AOu9nR23r5srBdqR58=; b=zG6hXHCRb4zr/Arb5dPVTAaaOVFs0k35s61/fYmQyZhpr0/9lK3jX8Q7CWMQTe8yFt KMq2ZSVBV/TCR8E2W8FQXNw/0RrNWeC2kVApAHdt/PO9K+K5J0jjtoVsg9kroYXPmsqw xdmko+5ty+6YR0zPWTTSz6zWqNAnCgGDTIqLf4q+Kyyna2H67xec2bMGQHh9mLctU/qt BldTjWYP1x06GpYewcYuRkcZ5qUz0Ixf2pdFnPpnMQ4w3eZw3frnqpIaW9pw3o8VdB4S RZmEOTpsVYN/2LHgPR/jKHaa6+E/jap99cp3yi5K3nC/jszLVKMlsTvf8SKCVKqoEgal f88A== X-Gm-Message-State: AOAM533R6qWngdUEN0hhYw2yJX5n7kDsjqsxZuZHHA1+W3todh8Dhpx4 0oOS1zKHWHx7htFLcSzIpTn0jF4464Z94dIEwe3tgNdf02mrV1D9pvkkxt9asHfl5n7XH5eFM/z c4IA9LI28KwTOoi4huTcFaz6LChyrjInujiHKw1KQevQCG/dhBkipYPc88w== X-Google-Smtp-Source: ABdhPJyOcGCkIqp1pWjden0iCg5PcvHYAwXsxyQf+onwNk21j4c99OIyx6MfRpZuBe3x8E/mhQdI78SlKG4= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a25:1ec5:0:b0:641:1bf5:d4ba with SMTP id e188-20020a251ec5000000b006411bf5d4bamr835196ybe.410.1650053750363; Fri, 15 Apr 2022 13:15:50 -0700 (PDT) Date: Fri, 15 Apr 2022 20:15:39 +0000 In-Reply-To: <20220415201542.1496582-1-oupton@google.com> Message-Id: <20220415201542.1496582-3-oupton@google.com> Mime-Version: 1.0 References: <20220415201542.1496582-1-oupton@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH 2/5] KVM: Shove vcpu stats_id init into kvm_vcpu_create_debugfs() From: Oliver Upton To: kvm@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, maz@kernel.org, kvmarm@vger.kernel.org, Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Again, the stats_id is only ever used by the stats code; put it where it belongs with the rest of the stats initialization. No functional change intended. Signed-off-by: Oliver Upton --- virt/kvm/kvm_main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ec9c6aad041b..aaf8de62b897 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3711,6 +3711,10 @@ static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) struct dentry *debugfs_dentry; char dir_name[ITOA_MAX_LEN * 2]; + /* Fill the stats id string for the vcpu */ + snprintf(vcpu->stats_id, sizeof(vcpu->stats_id), "kvm-%d/vcpu-%d", + task_pid_nr(current), vcpu->vcpu_id); + if (!debugfs_initialized()) return; @@ -3786,10 +3790,6 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) if (r) goto unlock_vcpu_destroy; - /* Fill the stats id string for the vcpu */ - snprintf(vcpu->stats_id, sizeof(vcpu->stats_id), "kvm-%d/vcpu-%d", - task_pid_nr(current), id); - /* Now it's all set up, let userspace reach it */ kvm_get_kvm(kvm); r = create_vcpu_fd(vcpu); From patchwork Fri Apr 15 20:15:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12815267 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBEF1C433F5 for ; Fri, 15 Apr 2022 20:15:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239141AbiDOUSY (ORCPT ); Fri, 15 Apr 2022 16:18:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350744AbiDOUSV (ORCPT ); Fri, 15 Apr 2022 16:18:21 -0400 Received: from mail-il1-x149.google.com (mail-il1-x149.google.com [IPv6:2607:f8b0:4864:20::149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 548F33B285 for ; Fri, 15 Apr 2022 13:15:52 -0700 (PDT) Received: by mail-il1-x149.google.com with SMTP id o17-20020a92c691000000b002c2c04aebe7so5332121ilg.8 for ; Fri, 15 Apr 2022 13:15:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3xc2EKUu7uEnFMNYX7NeITwrWrqypsePgkOpoddgFwU=; b=tmcVwYAmxAJM9UgwFDjZQ16XftORi9A9sl5AipqBPgT7NRV1vrOnjEP4zBug6x3tA/ DN4CsZ9KXfZDT8gu9DbnRwt0D4GZg3DaNX2xejBjQc63+fdl+m20G5n4eRDLJlE7ptoS +cgZSE8MECVoWsQy2G86Y0zvgzuEGh4Z1vJK1lUuclinVwoejHm+4dYquAjp6t9uAZLL gihMbvvSjbUfX4Kfn17gkfh3TOyIoOad17RuR3n/FbAcahRyPo5Dc0TSTLCS53aFLJaF 1UOQ7gYhGtWFV31HrYDLgQoH0fNkaMsrR4lggvDWgewho2MM9QCgfxen2mNOZAC+agzw Q4CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3xc2EKUu7uEnFMNYX7NeITwrWrqypsePgkOpoddgFwU=; b=3aPYK7+iM6T1XBWbfZt1S5QmcJVm6V+Oymua1HpfTCN0I7ZuQ/Ii5VcYtM9bSAyPwW CI7iPx51FGc8lEGRdE5EMLs5E8lNHoGYk00gtlJtXSKMSNdcCrZsPZc32ZK4ChknTnHz 2gRoL8GX3Ae20GekMIKAZRMNi6Vihkfb6SOugH8qrcS+AlKah5PtiFR3PWjTGjjf74u0 kLyYudmoM6deVJUGKHmV37BMGEUCXhVap5+jtq705co+ZgLkkiD89onNnMsRa+wq9fWE 6ho8ll6aBJEXFVxqRLPEtznrLDThPKYnFBYTa+p7C+5Fyg22SUEOAteNqm7czSMkjoZz eBuw== X-Gm-Message-State: AOAM533HJWpIulPX+eOiya6RLXOHHDlTG+bseBejL+w88A+19yc4GlnY gVdRqvRWxA1k+AVPXgwXiHAz5Sat9VBHT9NhDGRfY0yYdeb/TuSjZlTp9ni90VxQdpiFQKnS6eo iXqBuIYCM5xVISj7qN4Nojr4NygR6yK20WhEg1iTlRwrTDzkkW4H0SXS+Qw== X-Google-Smtp-Source: ABdhPJyw2If8M9jrUYo8GVts2nmzPWzbxeagQwSB0CGagwUUffSrDGf7s83sV+p4canIjwMHVePF0iQWEQA= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6602:2c46:b0:653:879e:4490 with SMTP id x6-20020a0566022c4600b00653879e4490mr194901iov.130.1650053751645; Fri, 15 Apr 2022 13:15:51 -0700 (PDT) Date: Fri, 15 Apr 2022 20:15:40 +0000 In-Reply-To: <20220415201542.1496582-1-oupton@google.com> Message-Id: <20220415201542.1496582-4-oupton@google.com> Mime-Version: 1.0 References: <20220415201542.1496582-1-oupton@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH 3/5] KVM: Get an fd before creating the VM From: Oliver Upton To: kvm@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, maz@kernel.org, kvmarm@vger.kernel.org, Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Hoist fd init to the very beginning of kvm_create_vm() so we can make use of it in other init routines. Signed-off-by: Oliver Upton --- virt/kvm/kvm_main.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index aaf8de62b897..1abbc6b07c19 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4752,25 +4752,27 @@ EXPORT_SYMBOL_GPL(file_is_kvm); static int kvm_dev_ioctl_create_vm(unsigned long type) { - int r; + int r, fd; struct kvm *kvm; struct file *file; + fd = get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) + return fd; + kvm = kvm_create_vm(type); - if (IS_ERR(kvm)) - return PTR_ERR(kvm); + if (IS_ERR(kvm)) { + r = PTR_ERR(kvm); + goto put_fd; + } + #ifdef CONFIG_KVM_MMIO r = kvm_coalesced_mmio_init(kvm); if (r < 0) goto put_kvm; #endif - r = get_unused_fd_flags(O_CLOEXEC); - if (r < 0) - goto put_kvm; - file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR); if (IS_ERR(file)) { - put_unused_fd(r); r = PTR_ERR(file); goto put_kvm; } @@ -4782,17 +4784,19 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) * care of doing kvm_put_kvm(kvm). */ if (kvm_create_vm_debugfs(kvm, r) < 0) { - put_unused_fd(r); fput(file); - return -ENOMEM; + r = -ENOMEM; + goto put_fd; } kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm); - fd_install(r, file); - return r; + fd_install(fd, file); + return fd; put_kvm: kvm_put_kvm(kvm); +put_fd: + put_unused_fd(fd); return r; } From patchwork Fri Apr 15 20:15:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12815268 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D347EC433EF for ; Fri, 15 Apr 2022 20:15:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351153AbiDOUSZ (ORCPT ); Fri, 15 Apr 2022 16:18:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351314AbiDOUSX (ORCPT ); Fri, 15 Apr 2022 16:18:23 -0400 Received: from mail-il1-x149.google.com (mail-il1-x149.google.com [IPv6:2607:f8b0:4864:20::149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F7DF3B287 for ; Fri, 15 Apr 2022 13:15:53 -0700 (PDT) Received: by mail-il1-x149.google.com with SMTP id h1-20020a056e021b8100b002cbec2c4261so2642112ili.16 for ; Fri, 15 Apr 2022 13:15:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=TP+Vw2YmOyLMd/0Sd7/tEhTMR//dkU6blEXQoP9cxUM=; b=fom0iclVJ707UMdJtlbHG3VB4+v16ns5IL6h+9f1nm/Tm5tNfdaRwcdxTUphO4o00/ K4BcufuuO7NAdrk7BVvEgq1JG/Yk7lJT1x+z+0VxL0wYp++6C8iX4YASCruPmLQUJO77 63E2xE/paqGMpfImD7Wofj5Frq92t+DRdR7a1Gw8OIEHI5YaxzT+U5n0lmR0M3BEldwb yBh8Jm+sLIsaExIOezOQSDcyT5/mI+SlxXUVcujdALzvwa38mUR5LSxRi3s5+SE7+171 R2ZzpyTG7o9JzBNvhlZRLVl+Ih4o50be6U83qHi8bLdJ16LC1p11VDmJ9whPHnq/FIrf TsFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=TP+Vw2YmOyLMd/0Sd7/tEhTMR//dkU6blEXQoP9cxUM=; b=JENWx6FWnk/rNzEUyRzuYX0GGQUo8u2viRDOT8QUSnObnHSYdBlLHscObVctSx4iPM fNC203fESpkXKO1gQCr1uFzWx+H1sAVhLEW2d/E5FLvS/gxP6UyJGyDfDQBd1de6KSs1 Rbvl9vR575VOAiMRWgNaT8ldZ/+SiQtR78W+RAxp5Z65bNjdNpQHSmRh0AAD/3lNViwM Mhh+q3XpnCBl4zDO5yVA/YPRnPydbYiMmZ944sb/t2H+KTOMIT/BiCRSWt9ZIt4Tzbs7 fYmtdieQwVe9U7O1bA/GtUFrkKNP5a4L5YJMLVZQWmMPp9p6is1C/y+ejZfVzW+8f1hL KmVw== X-Gm-Message-State: AOAM5318735cvwA8crKyWBpI7fLDYiR8Zlx1+5csJYmktN5l9dTOZlVw b8xmzylAf+nty0PqbMpfMqbMv/ADnenyk5tlEUeSchouhxMqp2z5pCF9cagcAFOxKMFfQIp7LSg f5Xmh8RJsOZw3cN2jJUGIORutfCP3CAuJOKXb6XbhqVS6QJgSoTAzcP1Hqw== X-Google-Smtp-Source: ABdhPJxSjjZ2alPFCRYbFvOAZLQqJNDqV9QtONmMd0c5KpJXRj8/jvnflAIZTQZYKnr+LN2LzqIt2l1ZLeY= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a92:130b:0:b0:2c5:66a6:cad8 with SMTP id 11-20020a92130b000000b002c566a6cad8mr180991ilt.285.1650053752602; Fri, 15 Apr 2022 13:15:52 -0700 (PDT) Date: Fri, 15 Apr 2022 20:15:41 +0000 In-Reply-To: <20220415201542.1496582-1-oupton@google.com> Message-Id: <20220415201542.1496582-5-oupton@google.com> Mime-Version: 1.0 References: <20220415201542.1496582-1-oupton@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH 4/5] KVM: Actually create debugfs in kvm_create_vm() From: Oliver Upton To: kvm@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, maz@kernel.org, kvmarm@vger.kernel.org, Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Doing debugfs creation after vm creation leaves things in a quasi-initialized state for a while. This is further complicated by the fact that we tear down debugfs from kvm_destroy_vm(). Align debugfs and stats init/destroy with the vm init/destroy pattern to avoid any headaches. Note the fix for a benign mistake in error handling for calls to kvm_arch_create_vm_debugfs() rolled in. Since all implementations of the function return 0 unconditionally it isn't actually a bug at the moment. Lastly, tear down debugfs/stats data in the kvm_create_vm_debugfs() error path. Previously it was safe to assume that kvm_destroy_vm() would take out the garbage, that is no longer the case. Signed-off-by: Oliver Upton --- virt/kvm/kvm_main.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1abbc6b07c19..54793de42d14 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -951,7 +951,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) char dir_name[ITOA_MAX_LEN * 2]; struct kvm_stat_data *stat_data; const struct _kvm_stats_desc *pdesc; - int i, ret; + int i, ret = -ENOMEM; int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc + kvm_vcpu_stats_header.num_desc; @@ -980,13 +980,13 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) sizeof(*kvm->debugfs_stat_data), GFP_KERNEL_ACCOUNT); if (!kvm->debugfs_stat_data) - return -ENOMEM; + goto out_err; for (i = 0; i < kvm_vm_stats_header.num_desc; ++i) { pdesc = &kvm_vm_stats_desc[i]; stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT); if (!stat_data) - return -ENOMEM; + goto out_err; stat_data->kvm = kvm; stat_data->desc = pdesc; @@ -1001,7 +1001,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) pdesc = &kvm_vcpu_stats_desc[i]; stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT); if (!stat_data) - return -ENOMEM; + goto out_err; stat_data->kvm = kvm; stat_data->desc = pdesc; @@ -1013,12 +1013,13 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) } ret = kvm_arch_create_vm_debugfs(kvm); - if (ret) { - kvm_destroy_vm_debugfs(kvm); - return i; - } + if (ret) + goto out_err; return 0; +out_err: + kvm_destroy_vm_debugfs(kvm); + return ret; } /* @@ -1049,7 +1050,7 @@ int __weak kvm_arch_create_vm_debugfs(struct kvm *kvm) return 0; } -static struct kvm *kvm_create_vm(unsigned long type) +static struct kvm *kvm_create_vm(unsigned long type, int fd) { struct kvm *kvm = kvm_arch_alloc_vm(); struct kvm_memslots *slots; @@ -1134,7 +1135,7 @@ static struct kvm *kvm_create_vm(unsigned long type) r = kvm_arch_post_init_vm(kvm); if (r) - goto out_err; + goto out_err_mmu_notifier; mutex_lock(&kvm_lock); list_add(&kvm->vm_list, &vm_list); @@ -1150,12 +1151,18 @@ static struct kvm *kvm_create_vm(unsigned long type) */ if (!try_module_get(kvm_chardev_ops.owner)) { r = -ENODEV; - goto out_err; + goto out_err_mmu_notifier; } + r = kvm_create_vm_debugfs(kvm, fd); + if (r) + goto out_err; + return kvm; out_err: + module_put(kvm_chardev_ops.owner); +out_err_mmu_notifier: #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) if (kvm->mmu_notifier.ops) mmu_notifier_unregister(&kvm->mmu_notifier, current->mm); @@ -4760,7 +4767,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) if (fd < 0) return fd; - kvm = kvm_create_vm(type); + kvm = kvm_create_vm(type, fd); if (IS_ERR(kvm)) { r = PTR_ERR(kvm); goto put_fd; @@ -4777,17 +4784,6 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) goto put_kvm; } - /* - * Don't call kvm_put_kvm anymore at this point; file->f_op is - * already set, with ->release() being kvm_vm_release(). In error - * cases it will be called by the final fput(file) and will take - * care of doing kvm_put_kvm(kvm). - */ - if (kvm_create_vm_debugfs(kvm, r) < 0) { - fput(file); - r = -ENOMEM; - goto put_fd; - } kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm); fd_install(fd, file); From patchwork Fri Apr 15 20:15:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12815269 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D662FC433F5 for ; Fri, 15 Apr 2022 20:15:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351551AbiDOUS0 (ORCPT ); Fri, 15 Apr 2022 16:18:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345497AbiDOUSX (ORCPT ); Fri, 15 Apr 2022 16:18:23 -0400 Received: from mail-il1-x14a.google.com (mail-il1-x14a.google.com [IPv6:2607:f8b0:4864:20::14a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DA463B3C7 for ; Fri, 15 Apr 2022 13:15:54 -0700 (PDT) Received: by mail-il1-x14a.google.com with SMTP id j16-20020a056e02219000b002cbe5b76195so3229459ila.9 for ; Fri, 15 Apr 2022 13:15:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=2P7U2NEtS/Cp/THuiM6rLt3ESGgtGBw6Gg5JS/l1HlE=; b=ktBA0tSikHczq2jsT5qtlGoVcRRU+LeKZjJ4u0/Zq1rulIc8BvdmnVgI0WiHuM+qRv IKEb/rfSST/LULx3nOen8bgTVSQ1vMXRm/7ZmVq/+8bqRLrdjEIMjoZRFwWwqwl/eYxG 7gybCPxoHtIJ6o3GMGHupHt9SLK/3X1jRQEvZxvxw7FL/DogcdJN3cgCWChiUesr/coD skyQba57JxMoOatXugBj6ve0CY5s/QYWEjC0BlxoecWgrjDsNaaFVb+N+YkghcQztTJE Uk9m6uqtAMQjO4juQbkA3pOXRy6ocC70oUNUdwfp0UQh/9aN6i60uDkLKYwyrhWTDwNB Nu8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=2P7U2NEtS/Cp/THuiM6rLt3ESGgtGBw6Gg5JS/l1HlE=; b=iapEnNFiPkB70JjXdeJeR9lfY5Vnmjwno9mN1ac9BovYs0DLBsaj3HOaFPzycCaaEp 5LkNRS7PDZ3PFL1kq/d1djSXSp2a094w3vcYtrgyvVR1ls7DQaf0NC1QapE0m7PrJptX A/q6gSRx+k4KxWTLghoinwg2AISa0nr1YE3GdyjRkIQA+hdTrZMPfyZylRuW1bspEHVy bspyFFYuzoPBIQ9hCAbPEEht6tLtMgHGQH1YK71EByKrz33hCCu2WlJnxWllDdF2viv2 /iP8FxcL5YK3YeHCB6IRndRjk6qfyspkRTAOvtbvpqmETmRwXwSfkmTD5LFx7USYo1lL 79aQ== X-Gm-Message-State: AOAM533kfyTVex9m1HIcMFivrh0PvxYNZxLbQOXCH24VWH6oZ3VfXQxx 2xKiHdL0Ix1dkyVJJ75bEcOo6JgIDgO+uDcvHDTa6QgQCI9g9s5H6MsOYHoujZieZlOI5TYBUsd xtcFnqRR/+e79Y9aYENoA5tmOEksypS2xLn95IA+dtv+ZunP4Di1EH8OIJQ== X-Google-Smtp-Source: ABdhPJwWIM2QCoyFORiOq2O8Zlc32BElm/bMDJiqc7MA80gnPY6pSE62MySlc2E0fRwLbbKogCZV6wuIsGs= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6e02:1607:b0:2cb:e6f7:a7bb with SMTP id t7-20020a056e02160700b002cbe6f7a7bbmr200350ilu.12.1650053753741; Fri, 15 Apr 2022 13:15:53 -0700 (PDT) Date: Fri, 15 Apr 2022 20:15:42 +0000 In-Reply-To: <20220415201542.1496582-1-oupton@google.com> Message-Id: <20220415201542.1496582-6-oupton@google.com> Mime-Version: 1.0 References: <20220415201542.1496582-1-oupton@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH 5/5] KVM: Hoist debugfs_dentry init to kvm_create_vm_debugfs() (again) From: Oliver Upton To: kvm@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, maz@kernel.org, kvmarm@vger.kernel.org, Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Since KVM now sanely handles debugfs init/destroy w.r.t. the VM, it is safe to hoist kvm_create_vm_debugfs() back into kvm_create_vm(). The author of this commit remains bitter for having been burned by the old wreck in commit a44a4cc1c969 ("KVM: Don't create VM debugfs files outside of the VM directory"). Signed-off-by: Oliver Upton --- virt/kvm/kvm_main.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 54793de42d14..61e727c15c1a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -958,6 +958,12 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) snprintf(kvm->stats_id, sizeof(kvm->stats_id), "kvm-%d", task_pid_nr(current)); + /* + * Force subsequent debugfs file creations to fail if the VM directory + * is not created. + */ + kvm->debugfs_dentry = ERR_PTR(-ENOENT); + if (!debugfs_initialized()) return 0; @@ -1079,12 +1085,6 @@ static struct kvm *kvm_create_vm(unsigned long type, int fd) BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); - /* - * Force subsequent debugfs file creations to fail if the VM directory - * is not created (by kvm_create_vm_debugfs()). - */ - kvm->debugfs_dentry = ERR_PTR(-ENOENT); - if (init_srcu_struct(&kvm->srcu)) goto out_err_no_srcu; if (init_srcu_struct(&kvm->irq_srcu))