From patchwork Wed Aug 28 07:56:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ani Sinha X-Patchwork-Id: 13780866 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 A43C714D6EB for ; Wed, 28 Aug 2024 07:56:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724831809; cv=none; b=nxuPO7ke2N7YYTrjj2Q9KA+aVjQ2DjHSbb3UMb5R368YVmwOVvznGwBAXfgJSk/jaRVlT8ROVpr3zgCoICE2uCSRNtxzy+fuAhW77aj802pdJvq7klR4j1pjkrPtuCkJhXogmT9LLAtEsVWJ1PNzCHUHAo1YBLftuqViMgpGPmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724831809; c=relaxed/simple; bh=F+2ualCaU6lYRASEfaKKFQIE3pbDTI7gaBA5gOx78K8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nCDbcwSUZ5M/Dro3vvv5CxRcFQIHj9jT5qtv9fHbAvJywwGQTiL8Vjwbi3ph6v9MJ3rpbjnBhg/C3le/jwIfFVcNMCwdN2qdKZX5UBazYwv9DTMRvt/U15OWwwKBBU6bTE3mss/T5csJxM6CupNsgrlKLhtTw8arT9UiareG0vo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MhYmjhv0; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MhYmjhv0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724831805; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=19Zh4Zt/ZVNN5Ajo9sSQWEIPYsVM5bJD7k1vakhLQ/o=; b=MhYmjhv010YmAqOdtuZBh+yIscWMJonTqbY36dn1vSzwLzB9yxQNswh+Ml+0uTKGVYQEiV jOV3lNNhC0bbuPMykIwbVRHFKquaFYzARHJie/7TS7db1pMVyS/Gu0VsafPVuCKnEXHBse IHh+GuAIpfTSAHQYcFM9tTA1Kuiq4Ww= Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-649-ay_i7oymPTukfHpGwwMynQ-1; Wed, 28 Aug 2024 03:56:43 -0400 X-MC-Unique: ay_i7oymPTukfHpGwwMynQ-1 Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-714318beb82so6768713b3a.2 for ; Wed, 28 Aug 2024 00:56:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724831802; x=1725436602; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=19Zh4Zt/ZVNN5Ajo9sSQWEIPYsVM5bJD7k1vakhLQ/o=; b=XKx1LQ+vvt/mx7ELq/ye+Y88hG70cVdYBjoPU9wv5N95mz17P5Ql0ee7pgUVhbk9or MAwwJNcDTftLRGJUJZDwcYXoZEYitpv3weOA3AmkBlggtrKOSiCBvf2UhsC30sNxaaB3 3XWw4cG7DssM2Mb00x4j3r0PTlfpve9Cq0NENjL8hYrrrL63S82Vimo60zS4qWLtVSCy wdYFWUq18jetW/iaCQL5a+RiYT2vlxkCwC8F7AzOpReiIZBVZPKpvuLbFLNMXDWM9ilT huO9YfimUAFpA2XOU2JwjQ4B6Q26Jg+FTzaPg0idTvzCFEljCkMlfrs8kls/91EZoPiY AtUQ== X-Forwarded-Encrypted: i=1; AJvYcCXSYzC3+nzsMjdZOxbdKobBjxGLjJIkPFAYJPCvYJI/rSyW15LbpsIM64Z17VKMx8Aa+tM=@vger.kernel.org X-Gm-Message-State: AOJu0YyIFkztrk3B6lVwEwBK67umzpgnsljlFw5ve2YGDCShQjWvnGps Ws82Wjc492chHs66uB136QP36l6Ig8QyVdQL/U1i5/h0XxQdNumbaFxQue8l118QHReLQx+bzao pO1Xrykl/4Miw7UDLRUAbhqC9b9l4c+YgiY7OPrTNIlnQmYT5YmmbO8AYy82R X-Received: by 2002:a05:6a21:3409:b0:1c4:d4b2:ffe6 with SMTP id adf61e73a8af0-1ccd288d5d0mr1009071637.19.1724831801982; Wed, 28 Aug 2024 00:56:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG/HVRKqcTrIT/WM260RBM0Z07SE8tr4atgSV/pWEwV/3FDxDJ6N7qyGLfHX0GADHsgfQ5siA== X-Received: by 2002:a05:6a21:3409:b0:1c4:d4b2:ffe6 with SMTP id adf61e73a8af0-1ccd288d5d0mr1009052637.19.1724831801581; Wed, 28 Aug 2024 00:56:41 -0700 (PDT) Received: from localhost.localdomain ([115.96.157.236]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2d8445fbf0dsm1013534a91.19.2024.08.28.00.56.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 00:56:41 -0700 (PDT) From: Ani Sinha To: Paolo Bonzini Cc: Ani Sinha , qemu-trivial@nongnu.org, zhao1.liu@intel.com, armbru@redhat.com, kvm@vger.kernel.org, qemu-devel@nongnu.org Subject: [PATCH v5 1/2] kvm: replace fprintf with error_report()/printf() in kvm_init() Date: Wed, 28 Aug 2024 13:26:28 +0530 Message-ID: <20240828075630.7754-2-anisinha@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240828075630.7754-1-anisinha@redhat.com> References: <20240828075630.7754-1-anisinha@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 error_report() is more appropriate for error situations. Replace fprintf with error_report() and error_printf() as appropriate. Cosmetic. No functional change. CC: qemu-trivial@nongnu.org CC: zhao1.liu@intel.com CC: armbru@redhat.com Reviewed-by: Zhao Liu Signed-off-by: Ani Sinha Reviewed-by: Markus Armbruster --- accel/kvm/kvm-all.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) changelog: v2: fix a bug. v3: replace one instance of error_report() with error_printf(). added tags. v4: changes suggested by Markus. v5: more changes from Markus's comments on v4. diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 75d11a07b2..fcc157f0e6 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2427,7 +2427,7 @@ static int kvm_init(MachineState *ms) QLIST_INIT(&s->kvm_parked_vcpus); s->fd = qemu_open_old(s->device ?: "/dev/kvm", O_RDWR); if (s->fd == -1) { - fprintf(stderr, "Could not access KVM kernel module: %m\n"); + error_report("Could not access KVM kernel module: %m"); ret = -errno; goto err; } @@ -2437,13 +2437,13 @@ static int kvm_init(MachineState *ms) if (ret >= 0) { ret = -EINVAL; } - fprintf(stderr, "kvm version too old\n"); + error_report("kvm version too old"); goto err; } if (ret > KVM_API_VERSION) { ret = -EINVAL; - fprintf(stderr, "kvm version not supported\n"); + error_report("kvm version not supported"); goto err; } @@ -2488,26 +2488,22 @@ static int kvm_init(MachineState *ms) } while (ret == -EINTR); if (ret < 0) { - fprintf(stderr, "ioctl(KVM_CREATE_VM) failed: %d %s\n", -ret, - strerror(-ret)); + error_report("ioctl(KVM_CREATE_VM) failed: %s", strerror(-ret)); #ifdef TARGET_S390X if (ret == -EINVAL) { - fprintf(stderr, - "Host kernel setup problem detected. Please verify:\n"); - fprintf(stderr, "- for kernels supporting the switch_amode or" - " user_mode parameters, whether\n"); - fprintf(stderr, - " user space is running in primary address space\n"); - fprintf(stderr, - "- for kernels supporting the vm.allocate_pgste sysctl, " - "whether it is enabled\n"); + error_printf("Host kernel setup problem detected." + " Please verify:\n"); + error_printf("- for kernels supporting the" + " switch_amode or user_mode parameters, whether"); + error_printf(" user space is running in primary address space\n"); + error_printf("- for kernels supporting the vm.allocate_pgste" + " sysctl, whether it is enabled\n"); } #elif defined(TARGET_PPC) if (ret == -EINVAL) { - fprintf(stderr, - "PPC KVM module is not loaded. Try modprobe kvm_%s.\n", - (type == 2) ? "pr" : "hv"); + error_printf("PPC KVM module is not loaded. Try modprobe kvm_%s.\n", + (type == 2) ? "pr" : "hv"); } #endif goto err; @@ -2526,9 +2522,9 @@ static int kvm_init(MachineState *ms) nc->name, nc->num, soft_vcpus_limit); if (nc->num > hard_vcpus_limit) { - fprintf(stderr, "Number of %s cpus requested (%d) exceeds " - "the maximum cpus supported by KVM (%d)\n", - nc->name, nc->num, hard_vcpus_limit); + error_report("Number of %s cpus requested (%d) exceeds " + "the maximum cpus supported by KVM (%d)", + nc->name, nc->num, hard_vcpus_limit); exit(1); } } @@ -2542,8 +2538,8 @@ static int kvm_init(MachineState *ms) } if (missing_cap) { ret = -EINVAL; - fprintf(stderr, "kvm does not support %s\n%s", - missing_cap->name, upgrade_note); + error_report("kvm does not support %s", missing_cap->name); + error_printf("%s", upgrade_note); goto err; } From patchwork Wed Aug 28 07:56:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ani Sinha X-Patchwork-Id: 13780867 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 87C5D15B10E for ; Wed, 28 Aug 2024 07:56:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724831812; cv=none; b=tD2627lIOk8Gao5IFfjGbM2lGD/yLeQpp3kPrkFLNFbh4iDS/VeVrb9zjJPK0FFVU3TfJ8bzFpqjdKMy/E7cXHkFQdkmQvRKdrL0AFndZncX7jpSyeSHXXkNP+dEkLpdVCgQq5WMQVXnK47K7kIl23RBuHErTQfr1ssJni08zao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724831812; c=relaxed/simple; bh=I70JZdMDL1O7ekktfEOR+rSvYTxjzC2eN40c0XnBc0U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mNlFnylbPZGRVcLyTX2w+iNwBClxD3vA+lmGReGe+XhXtlN/TBJlx5rzXe7XvO1ZtOIe4jxszsnjJqzIfSpq58ghI//VmgUe2oQL9/lzJoKctJUuZe0MoZNYosjqnUoJsFxjqoTLNDV5bQKT/dcZ6XxpPn+zFueXKKuu5a0csr4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gKUQIaDN; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gKUQIaDN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724831809; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zYl3uaCkxHjT2nDUPL1WDle3wL/fOkENv7M02v+JPJY=; b=gKUQIaDN87zxthEiqYIYbBSEqQwloMn60Fxtbv0sMIJcszFEEOSQ+clgybuHEzIg4/7+H3 ZYfi4q+Ha/a/sd47dDjGFccjOJapRZdxtbGi3tgoPVeksH3AaoCT75laGVjhpApjv8RRoT dFgocKvPQShPJBMqkjozIto3vxE8yn8= Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-96-OUWt5Bv2Oriai92le_RE2Q-1; Wed, 28 Aug 2024 03:56:46 -0400 X-MC-Unique: OUWt5Bv2Oriai92le_RE2Q-1 Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-2d3bd8a0bd4so7319018a91.0 for ; Wed, 28 Aug 2024 00:56:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724831805; x=1725436605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zYl3uaCkxHjT2nDUPL1WDle3wL/fOkENv7M02v+JPJY=; b=B07EgZGuRichet8NlkslcwX3rhB1jpSWo+gI5vbDkMvNTBdXz0aJ05Ad8Nmhxxt7hl jDWISJMa6ne4bzWI8OGKkXDIfgq4MQa0mwnsZ7NdPiJLby/oGwLpKwKvTICLmDTOxE5L 67vseEKeoRS2h880O+Nr5TCJke4Owa7OFE7g0boEDB7157jgGZMsVGaZ7DoLeubmqBxm OPYv6h8s/6AbG37Bff9KnNg+Rv1BYaCMv5H6+7LyM+xjxwIG19NZr/9zvwnAsN3wlz6T xZ+mNcVaGV04xStMG0mntOnSmuVmSU3chXFEGO9/y5S+sW4ZYsuWruPn2GbzZ2WRns6K mWfA== X-Forwarded-Encrypted: i=1; AJvYcCXgzoufc9sDmu0b47SrN+y1ByifQYcyBqqTIrMU7vGFShm/Dqleqzh26x5Bwzu8uoa229o=@vger.kernel.org X-Gm-Message-State: AOJu0YynVdPLFRMc/tp+PHflNboZ3q/16w5iLLq3Xxyb2cQXo0yAqq7U JyVwesNG0nXyGq+AtkXjm2BpEJ4mVGWKjA+NJQ0EDrT9thYyLT0vIbHrfmXmLUqBMQFqfA98+IA Qn19QAZB+55mpnlm3e8+IkNQRoGP42D7yP3/E7WMXpjlYPxNngQ== X-Received: by 2002:a17:90a:7408:b0:2d3:cc3e:4d6d with SMTP id 98e67ed59e1d1-2d646b91092mr14510030a91.9.1724831805451; Wed, 28 Aug 2024 00:56:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnS9wmiNKpmg/gLxiSQVdrbd8UyjUO09X7ptuxTKXWPb9mBB1kZo5ol5VgojZc/qD+8UvY6w== X-Received: by 2002:a17:90a:7408:b0:2d3:cc3e:4d6d with SMTP id 98e67ed59e1d1-2d646b91092mr14510013a91.9.1724831805035; Wed, 28 Aug 2024 00:56:45 -0700 (PDT) Received: from localhost.localdomain ([115.96.157.236]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2d8445fbf0dsm1013534a91.19.2024.08.28.00.56.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 00:56:44 -0700 (PDT) From: Ani Sinha To: Paolo Bonzini Cc: Ani Sinha , zhao1.liu@intel.com, cfontana@suse.de, armbru@redhat.com, qemu-trivial@nongnu.org, kvm@vger.kernel.org, qemu-devel@nongnu.org Subject: [PATCH v5 2/2] kvm: refactor core virtual machine creation into its own function Date: Wed, 28 Aug 2024 13:26:29 +0530 Message-ID: <20240828075630.7754-3-anisinha@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240828075630.7754-1-anisinha@redhat.com> References: <20240828075630.7754-1-anisinha@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Refactoring the core logic around KVM_CREATE_VM into its own separate function so that it can be called from other functions in future patches. There is no functional change in this patch. CC: pbonzini@redhat.com CC: zhao1.liu@intel.com CC: cfontana@suse.de CC: armbru@redhat.com CC: qemu-trivial@nongnu.org Reviewed-by: Zhao Liu Reviewed-by: Claudio Fontana Signed-off-by: Ani Sinha --- accel/kvm/kvm-all.c | 86 ++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 33 deletions(-) v2: s/fprintf/warn_report as suggested by zhao v3: s/warn_report/error_report. function names adjusted to conform to other names. fprintf -> error_report() moved to its own patch. v4: added tags and rebased. v5: rebased. diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index fcc157f0e6..cf3d820b94 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2385,6 +2385,57 @@ uint32_t kvm_dirty_ring_size(void) return kvm_state->kvm_dirty_ring_size; } +static int kvm_create_vm(MachineState *ms, KVMState *s, int type) +{ + int ret; + + do { + ret = kvm_ioctl(s, KVM_CREATE_VM, type); + } while (ret == -EINTR); + + if (ret < 0) { + error_report("ioctl(KVM_CREATE_VM) failed: %s", strerror(-ret)); + +#ifdef TARGET_S390X + if (ret == -EINVAL) { + error_printf("Host kernel setup problem detected." + " Please verify:\n"); + error_printf("- for kernels supporting the" + " switch_amode or user_mode parameters, whether"); + error_printf(" user space is running in primary address space\n"); + error_printf("- for kernels supporting the vm.allocate_pgste" + " sysctl, whether it is enabled\n"); + } +#elif defined(TARGET_PPC) + if (ret == -EINVAL) { + error_printf("PPC KVM module is not loaded. Try modprobe kvm_%s.\n", + (type == 2) ? "pr" : "hv"); + } +#endif + } + + return ret; +} + +static int kvm_machine_type(MachineState *ms) +{ + MachineClass *mc = MACHINE_GET_CLASS(ms); + int type; + + if (object_property_find(OBJECT(current_machine), "kvm-type")) { + g_autofree char *kvm_type; + kvm_type = object_property_get_str(OBJECT(current_machine), + "kvm-type", + &error_abort); + type = mc->kvm_type(ms, kvm_type); + } else if (mc->kvm_type) { + type = mc->kvm_type(ms, NULL); + } else { + type = kvm_arch_get_default_type(ms); + } + return type; +} + static int kvm_init(MachineState *ms) { MachineClass *mc = MACHINE_GET_CLASS(ms); @@ -2467,45 +2518,14 @@ static int kvm_init(MachineState *ms) } s->as = g_new0(struct KVMAs, s->nr_as); - if (object_property_find(OBJECT(current_machine), "kvm-type")) { - g_autofree char *kvm_type = object_property_get_str(OBJECT(current_machine), - "kvm-type", - &error_abort); - type = mc->kvm_type(ms, kvm_type); - } else if (mc->kvm_type) { - type = mc->kvm_type(ms, NULL); - } else { - type = kvm_arch_get_default_type(ms); - } - + type = kvm_machine_type(ms); if (type < 0) { ret = -EINVAL; goto err; } - do { - ret = kvm_ioctl(s, KVM_CREATE_VM, type); - } while (ret == -EINTR); - + ret = kvm_create_vm(ms, s, type); if (ret < 0) { - error_report("ioctl(KVM_CREATE_VM) failed: %s", strerror(-ret)); - -#ifdef TARGET_S390X - if (ret == -EINVAL) { - error_printf("Host kernel setup problem detected." - " Please verify:\n"); - error_printf("- for kernels supporting the" - " switch_amode or user_mode parameters, whether"); - error_printf(" user space is running in primary address space\n"); - error_printf("- for kernels supporting the vm.allocate_pgste" - " sysctl, whether it is enabled\n"); - } -#elif defined(TARGET_PPC) - if (ret == -EINVAL) { - error_printf("PPC KVM module is not loaded. Try modprobe kvm_%s.\n", - (type == 2) ? "pr" : "hv"); - } -#endif goto err; }