From patchwork Thu Feb 15 01:00:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 201220 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp89668dyb; Wed, 14 Feb 2024 17:01:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU+y3c24K7H2dkI5mXrDHKOvuzFD7+V22vEaqBU68F9+gfeDrT+ftroARxrZhEhI6Yt0sT8dxfAp9iqjrz6d5tdX7s4cQ== X-Google-Smtp-Source: AGHT+IFcV0AHbRaxGT/Hm6qXEft3abMhamf7wziTAQ/zP39VNxlJzKwu6vSltZhe39bGN3i35H/u X-Received: by 2002:a17:906:7141:b0:a3d:48d0:d518 with SMTP id z1-20020a170906714100b00a3d48d0d518mr120631ejj.53.1707958867655; Wed, 14 Feb 2024 17:01:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707958867; cv=pass; d=google.com; s=arc-20160816; b=FTIMHGlJL7MQRUYYnNYguuEERrkEbzfykdblJbNZjKfjdKDwYqnoKQIWcHV6+cJL/w +pICgr7zdrlYkcewEOIdlyyBNjmQ95N5n30/g5bifrTtHmy8YfbxAndJeCzdOBmtdkJ7 rfs7S3m0gWcpXJMXpN3InIppWwQrFYSOVh5MtU10k5sAYeHJfCux/OksgB7fteZ4njhP lRASX2RpKc40cODSdwFt5Qa5cbIvUO5DJ5oA/cqYUoRw4Nc1OYKLcQf5f5BUZfPYfbUH fgDtwMe7ge2dSkmvPRs7bY1LvsaONMtsLW6ma3j6mwvXnJn6FEYSkBXl6/nkAu/ZIT3O +5eg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=THBGYEvhQX0rLSUutlty+Y9Xa/noirUlucPH6YPlG7k=; fh=aStdmg4/F3tA+525q8Twd43P2H906zN9Ce86j7SJ1zE=; b=xrrtHjj2eGvLsCZDHB3SIY1VqCH/JcTm0glWVq4SCOprHgxUiYuEZHct5En/4X5Wf6 U1yUNQnGtyp1iM2AcKUgBqKUVIvcx0Dhpb6TJiGZlxCp7J5u3JcFO7vcOSSHmwKCAXVB w/Oo/9NfhdQzGoPTkmXBHKMHUrZW+5CIDEB+BN55cLJtTWaLISEYZLi0MBL3x3kc6ujO dSRlkDHbzYbgmtAmROoJvTRa/Y/mPFKFeLi1aQHo9F2FevODbyZFd1c2XP64rbq0LMlR zCHe7a20fwVkQvZashQdSC5/qbPT55xUeL4q49kZGvU9F2n/N+7n/axeWaP6t6AHbzMS 4fkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=LYlYTTS6; 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-66208-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66208-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id re12-20020a170906d8cc00b00a3d789ed928si75560ejb.645.2024.02.14.17.01.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 17:01:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66208-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=LYlYTTS6; 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-66208-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66208-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 40D3A1F27886 for ; Thu, 15 Feb 2024 01:01:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9B3338F6E; Thu, 15 Feb 2024 01:00:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LYlYTTS6" Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 454193C39 for ; Thu, 15 Feb 2024 01:00:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707958814; cv=none; b=NkBSCzDiQHTMSZZGqn8un5AjdhJIfkUtpYEKLj+gc0exIdCXn4LuPQmrlnV8zu+HMjicIR/zVGP6kVaY7r4LAIjSPlbxXZCJGOI8GO5ReswAQqh4JRyckIDPIuJQ2NXb9R13naRRtJ7uEctaIg37q6eGYeluRNPaFiOjUk8XsAQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707958814; c=relaxed/simple; bh=Wf4UDyoxn2kT2aM4troTO93XPUaqMcnsOBDPFbW/SkM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PB+Pf+QOT1gooN/alYe5yfg/1fsfXwNZQjvujoETg9RQHOk5XlrchwxzfTgGW+6o8694/gywpbrduaZ4bn9n0rwfEp+LkpcAANQozeW+g6+RaSljGDbFWNhuaJTyaYasYa4OpUs9l6nHVRcv+gBIAPUbaO0TC3flS/ZdfKJZKvY= 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=LYlYTTS6; arc=none smtp.client-ip=209.85.210.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-pf1-f201.google.com with SMTP id d2e1a72fcca58-6e0a9b28359so455611b3a.0 for ; Wed, 14 Feb 2024 17:00:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707958812; x=1708563612; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=THBGYEvhQX0rLSUutlty+Y9Xa/noirUlucPH6YPlG7k=; b=LYlYTTS61aYDoyYVk1Rn6qB7AKtPQbPvtdiVI5xm5zO+MfFVFNETmY3NT5HDAvMoJ8 20ndF524d/cg50tNvpxafVHuzqzMBOMmbhyEzMUJcDZkgQLcdUQcumyxp+LYqnRWzT25 psfq5vAPudyTDzYt/FowvBXyHCRprh3AZthk0DODfeaSNnE2AqL+WB4M4y++57sl1C6l XLQ2Quzy6iwpxNO2efCIEogsJa2jU4bIvWi64D/Sf7Wkdm1lJAVHmF0YRhtbVf8WmXmE QefMR8Mjx+yh8UvAUVvIlGopJbgD2MgyiztVvV5gAliZp8l0CM8CfMt0/+mTqZQv0/GZ AZsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707958812; x=1708563612; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=THBGYEvhQX0rLSUutlty+Y9Xa/noirUlucPH6YPlG7k=; b=fHMeUjkvOhdgzpvK1yWsPpiV6pi38roQb6kDzEXq5aFrGN3SxSDnziBrFN1+200JYy ZbTLeu0Jg7vdFOIc/0VlfrnkWKOGPQ93Ntbk61Q0ujA1PatFsnEjkZoBB6Yw3/JiGyUM R+QwG57DA3HK1yOvcxP3Jds7N/tXOxoC/ZV603+tZqXxagrxhnQc/+NY/CSvg8VRf0+B FAKJV+1D5CrKL1+F4jtjHU6+XS02D0x8cb1rRHtoqtPgg4T5d8NMDxIQ/7CEwcghmU27 JhCJDmkyZZTHsz9SAvgm+jHabksZHzkbta91MwQ2Nq0RQ9jBN06KXbqFp2m7N51XRKpW q4DA== X-Forwarded-Encrypted: i=1; AJvYcCWTvpKBMvq4S/iJxkuwTZSMJTiZ/UulhHZJ/6/m0UwUD0b5ek4kYfZ3X4jvHlYJdO67IMja2r5lDAUpGKM8xcCqrZkILNNArYwRAg6m X-Gm-Message-State: AOJu0YzczkdqoB3xTe//NEjruew9Oaagizn0Ic08rDi2MYQISxFM50yC hruEvtDxbSDLM2f5aF1P7b+ghRHjFwvtdO7FNxkN0hnEiY/3O02mMsTm1Rseau0UqoWsjXl9Idf /jA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1898:b0:6e0:e2f8:cf30 with SMTP id x24-20020a056a00189800b006e0e2f8cf30mr36898pfh.0.1707958812693; Wed, 14 Feb 2024 17:00:12 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 14 Feb 2024 17:00:04 -0800 In-Reply-To: <20240215010004.1456078-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240215010004.1456078-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240215010004.1456078-3-seanjc@google.com> Subject: [PATCH 2/2] KVM: selftests: Test forced instruction emulation in dirty log test (x86 only) From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Pasha Tatashin , Michael Krebs X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790924677802271382 X-GMAIL-MSGID: 1790924677802271382 Add forced emulation of MOV and LOCK CMPXCHG instructions in the dirty log test's guest code to verify that KVM's emulator marks pages dirty as expected (and obviously to verify the emulator works at all). In the long term, the guest code would ideally hammer more of KVM's emulator, but for starters, cover the two major paths: writes and atomics. To minimize #ifdeffery, wrap only the related code that is x86 specific, unnecessariliy synchronizing an extra boolean to the guest is far from the end of the world. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 36 ++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c index eaad5b20854c..ff1d1c7f05d8 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -92,6 +92,29 @@ static uint64_t guest_test_phys_mem; */ static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM; +static bool is_forced_emulation_enabled; + +static void guest_write_memory(uint64_t *mem, uint64_t val, uint64_t rand) +{ +#ifdef __x86_64__ + if (is_forced_emulation_enabled && (rand & 1)) { + if (rand & 2) { + __asm__ __volatile__(KVM_FEP "movq %1, %0" + : "+m" (*mem) + : "r" (val) : "memory"); + } else { + uint64_t __old = READ_ONCE(*mem); + + __asm__ __volatile__(KVM_FEP LOCK_PREFIX "cmpxchgq %[new], %[ptr]" + : [ptr] "+m" (*mem), [old] "+a" (__old) + : [new]"r" (val) : "memory", "cc"); + } + } else +#endif + + *mem = val; +} + /* * Continuously write to the first 8 bytes of a random pages within * the testing memory region. @@ -114,11 +137,13 @@ static void guest_code(void) while (true) { for (i = 0; i < TEST_PAGES_PER_LOOP; i++) { + uint64_t rand = READ_ONCE(random_array[i]); + addr = guest_test_virt_mem; - addr += (READ_ONCE(random_array[i]) % guest_num_pages) - * guest_page_size; + addr += (rand % guest_num_pages) * guest_page_size; addr = align_down(addr, host_page_size); - *(uint64_t *)addr = READ_ONCE(iteration); + + guest_write_memory((void *)addr, READ_ONCE(iteration), rand); } /* Tell the host that we need more random numbers */ @@ -772,6 +797,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) sync_global_to_guest(vm, guest_page_size); sync_global_to_guest(vm, guest_test_virt_mem); sync_global_to_guest(vm, guest_num_pages); + sync_global_to_guest(vm, is_forced_emulation_enabled); /* Start the iterations */ iteration = 1; @@ -875,6 +901,10 @@ int main(int argc, char *argv[]) int opt, i; sigset_t sigset; +#ifdef __x86_64__ + is_forced_emulation_enabled = kvm_is_forced_emulation_enabled(); +#endif + sem_init(&sem_vcpu_stop, 0, 0); sem_init(&sem_vcpu_cont, 0, 0);