Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp3170983lqt; Tue, 23 Apr 2024 12:15:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUwYPaaTpEDzPLBbuWs945+KzXn0/bsmKbezVsxIEH5YJUba91Yb21QNHg1aqKE6FKaS92xFu1A/5wgTe2aj8wRkOR1Kr+hNyIMkbYvzA== X-Google-Smtp-Source: AGHT+IGI4aFHbfpVJI2Be3DiFlsjpB4UoIuOiNCH9pkjlgbtvN+Gqg9R9CBi3bVd5r+9b2rSPsWb X-Received: by 2002:a17:902:d549:b0:1e7:fc49:69cc with SMTP id z9-20020a170902d54900b001e7fc4969ccmr460460plf.36.1713899757762; Tue, 23 Apr 2024 12:15:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713899757; cv=pass; d=google.com; s=arc-20160816; b=DH1r/0UhaMbP28iQNVKm3/kzSRE5qG2IJ7ZKWIpAu9+smm/9zrF1TQOrKYii1mgjOQ EridDqDBijqhetjQAYgA//G3xE+LzCy8mLbGLcmx3L9WSi6B2oxLar/RsHTNfzh00NjR h08g5VXw8XmgQATRLjK5OEPPFvi4DNE86XDFicl5O7k8E+IW4k69IHon/UczCZCnVUod JqAvyvKdAI/a2hL0hoGKw2LPnVoxC1E6dCNzZmgq7df/r6ZBngIY2PhxUpVtkIt8NWdk e82EEQsQxqpqrxDBM0XgKXSNpMLFULhNr3yPs8YiI5ZKHQ/5CjXK34959mvTXgF5h/Ki 5/lA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :in-reply-to:date:dkim-signature; bh=tK2a7Yr/IypNmsegnHAW+Bnt+5x2FeODOQAPDg+FlVY=; fh=b5t4/7Q5BGLF8cV+sIq//PCcBV51wl0WlCiipk2GQa4=; b=lgzjUVIUQqkH216wfgkcEIix3nkar0eOH0Nq9Rpa7cPiuiB8tqMt37/cqFFUavj0zC 7reHP22NjDIKV8ER9ZznlKt1mp6i9SQ8eZGjrzhJklOjUvAXecQmA6ZCL7NjOMsQYkyo uJQ2MvOnAfkyK7tD3mBULRe75YfRX/MAFz776/b3cFfZnCe3DOrqES4AiWvjMR5JWHfg 0LLwVia09mkXJkGJMV1jiwp65K1nzwQ3uMtwo3pFlmOfxoYmhNm0YzLolTV9DGQrEipg ZCW7DiP+oUxnEvvqB2fbyvg5dhDitQ63YBEavvzAv9HwxxuXDVyQIts1BCqYTi/bUZJW 2P1g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=H91vs80+; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-155787-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-155787-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id u16-20020a17090341d000b001ddbde8f59bsi10095998ple.582.2024.04.23.12.15.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 12:15:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-155787-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=H91vs80+; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-155787-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-155787-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 1B9C228F9FA for ; Tue, 23 Apr 2024 19:15:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5C972140394; Tue, 23 Apr 2024 19:15:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="H91vs80+" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B4A013FD75 for ; Tue, 23 Apr 2024 19:15:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713899751; cv=none; b=sD42MgTUbU49mA9L3nd5Hw7U33TrNwtpcHbGFjrxSY6soYh2nOHrSHdgG0ZBMjC4FuYDSjpSxZjnegp4WFSTVYsH4slYOtsebpzaPsmVUearEPvHZhuoElsOS4vAgYE8Q5IsAIxzv+Uli+rLPi2zTN5StmU96LYA1dvqAMXvxOw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713899751; c=relaxed/simple; bh=pyZkFd0ABGXFz76xB8ZWfDAQUqX2Kd3JVR9/s4wDQ0Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SvKbMwsU+9eZ6KHHrFMkfQjJ29OYcXcHJ2unF1mU6tNhP+DBhQJCEXYLrh27B5DCkptcvw4MDIUd+E9P6Qbw94WPs4gzUCU9ks1ErImuwWYKZ2iAx1eeYcaeXKy9djXOIuiMyQvVeEjmnToSaK+K2wsNvScB7Im4fXHgWfP5KZQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=H91vs80+; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-61b6f415d0fso37285307b3.2 for ; Tue, 23 Apr 2024 12:15:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713899749; x=1714504549; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=tK2a7Yr/IypNmsegnHAW+Bnt+5x2FeODOQAPDg+FlVY=; b=H91vs80+0vhLlQRld5mKn0esvXzuo53+damSa4uR9KQk3+XTq7XIfiHK7tixMkn6Nh mvTytOiFPvDlordDYdYMFT0xAFTdBvgEqF0xd6mWrg/9+bjFznB5YYT2VULbt494w6mj Ln5GDvEqcw8uaYc8edieTZKSlIEXOEEsHD8P3F63tLh0o8S0owdhnEI6cklZGVtXVxkI ER+xFbFkzBn+WGCwKYJ6gs3ZOZkmQUrzavGU4zvCGqN6g4sh0eZsRLRoSO9gCN4/p60D QDPQwoBjvhrq8vfQhA0NfbEBZ19M5EMGV1BCF8pJIDenLFhZebVXnW7aikXenjIbHI/y nYPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713899749; x=1714504549; h=content-transfer-encoding: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=tK2a7Yr/IypNmsegnHAW+Bnt+5x2FeODOQAPDg+FlVY=; b=GNz8tHdvNlCwZic9BQ283Gug2i6fsU/AAkDf0jBapp5PSm5rzks4O0oiWJRcNGHGpl VGG3PgQ3tUnrGtXCjJZ/oHjhMkCR24hc0mJGy4axLhkyrIEn9f3+gKTbmx/K8Iw19EEZ aMo9Xww2/sw+XPERE12IjhmE4Uj16q3IZAEC6TXzq0vr4jBXZbC4GUZeK1PLxsKHLyB/ CMwGarrbE2PXshhbnVSQborm7BFWopsQoFXz22+sViYrzqZ2WbmBKzmojrpEUF0s2IWo njpTjk7c/JFrFeoJIIPucYtmOqrCfid+9PrKaJRiQobUMdJk823QgcF6eLcmQniS6pXG lTzw== X-Forwarded-Encrypted: i=1; AJvYcCXhEsgLtke7cLOSzzn5t6aSZ2RMUbvXe0dq3B5uvmyS9I6LjEFns9kswdunsOJ2NrMeMDdvChmB45kT3Ws2j/QH9z8mhzXEpNmIn6iI X-Gm-Message-State: AOJu0YyrSFnjwgD6JP+63ujk5TynqY8DPsYxI+TA53TZiWxNBvC0tdIO 4TBiG3jvo5xbEYmii0Ng7OHFoR44yii06n/oftiwHATaUMmxFvJl59dbSm8XamJnZA6kdQwvFVk Z6Q== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:52d1:0:b0:61a:d420:3b3e with SMTP id g200-20020a8152d1000000b0061ad4203b3emr106057ywb.5.1713899749077; Tue, 23 Apr 2024 12:15:49 -0700 (PDT) Date: Tue, 23 Apr 2024 12:15:47 -0700 In-Reply-To: <20240423-0db9024011213dcffe815c5c@orel> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240423073952.2001989-1-chentao@kylinos.cn> <878bf83c-cd5b-48d0-8b4e-77223f1806dc@web.de> <20240423-0db9024011213dcffe815c5c@orel> Message-ID: Subject: Re: [PATCH] KVM: selftests: Add 'malloc' failure check in test_vmx_nested_state From: Sean Christopherson To: Andrew Jones Cc: Markus Elfring , Kunwu Chan , linux-kselftest@vger.kernel.org, kvm@vger.kernel.org, kernel-janitors@vger.kernel.org, Muhammad Usama Anjum , Paolo Bonzini , Shuah Khan , LKML , Kunwu Chan , Anup Patel , Thomas Huth , Oliver Upton Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Tue, Apr 23, 2024, Andrew Jones wrote: > On Tue, Apr 23, 2024 at 07:56:01AM -0700, Sean Christopherson wrote: > > +others > >=20 > > On Tue, Apr 23, 2024, Markus Elfring wrote: > > > =E2=80=A6 > > > > This patch will add the malloc failure checking > > > =E2=80=A6 > > >=20 > > > * Please use a corresponding imperative wording for the change descri= ption. > > >=20 > > > * Would you like to add the tag =E2=80=9CFixes=E2=80=9D accordingly? > >=20 > > Nah, don't bother with Fixes. OOM will cause the test to fail regardle= ss, the > > fact that it gets an assert instead a NULL pointer deref is nice to hav= e, but by > > no means does it fix a bug. > >=20 > > > > +++ b/tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.= c > > > > @@ -91,6 +91,7 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu) > > > > const int state_sz =3D sizeof(struct kvm_nested_state) + getpages= ize(); > > > > struct kvm_nested_state *state =3D > > > > (struct kvm_nested_state *)malloc(state_sz); > > > > + TEST_ASSERT(state, "-ENOMEM when allocating kvm state"); > > > =E2=80=A6 > > >=20 > > > Can =E2=80=9Cerrno=E2=80=9D be relevant for the error message constru= ction? > >=20 > > Probably not, but there's also no reason to assume ENOMEM. TEST_ASSERT= () spits > > out the actual errno, and we can just say something like "malloc() fail= ed for > > blah blah blah". =20 > >=20 > > But rather than keeping playing whack-a-mole, what if we add macros to = perform > > allocations and assert on the result? I have zero interest in chasing = down all > > of the "unsafe" allocations, and odds are very good that we'll collecti= vely fail > > to enforce checking on new code. > >=20 > > E.g. something like (obviously won't compile, just for demonstration pu= rposes) > >=20 > > #define kvm_malloc(x) > > ({ > > void *__ret; > >=20 > > __ret =3D malloc(x); > > TEST_ASSERT(__ret, "Failed malloc(" #x ")\n"); > > __ret; > > }) > >=20 > > #define kvm_calloc(x, y) > > ({ > > void *__ret; > >=20 > > __ret =3D calloc(x, y); > > TEST_ASSERT(__ret, "Failed calloc(" #x ", " #y ")\n"); > > __ret; > > }) >=20 > Sounds good to me, but I'd call them test_malloc, test_calloc, etc. and > put them in include/test_util.h Possibly terrible idea: what if we used kmalloc() and kcalloc()? K is for = KVM :-) I like test_* more than kvm_*, but I'm mildly concerned that readers will b= e confused by "test", e.g. initially thinking the "test" means it's just "tes= ting" if allocation is possible. The obvious counter-argument is that people might also get tripped by kmall= oc(), e.g. thinking that selftests is somehow doing a kernel allocation. I almost wonder if we should just pick a prefix that's less obviously conne= cted to KVM and/or selftests, but unique and short. Hmm, tmalloc(), i.e t[est]malloc()? tcalloc() gets a bit close to Google's TCMalloc[*], but I suspect that any confusion would be entirely limited to Googlers, and I'll volunteer us to suck it up and deal with it :-) [*] https://github.com/google/tcmalloc