Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp844227rdb; Fri, 26 Jan 2024 12:26:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwaI1QyuZQ69E+RmKEoVff+KVHu5CiptCOOFNF29GtSQdhzWH4fnCVYHi1HljknKQCfv47 X-Received: by 2002:a17:90a:668f:b0:290:b326:7e73 with SMTP id m15-20020a17090a668f00b00290b3267e73mr436290pjj.84.1706300772032; Fri, 26 Jan 2024 12:26:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706300772; cv=pass; d=google.com; s=arc-20160816; b=U9JQvvVFTbBsSlY/bK6fn3yvcW4lNKnR2XNvLo0zm4SkuV5lOcJGxhq2rkdg/B2Wul CKykq/qjOEkumNlMet8wTWWt/FfxEmZgaq+onL1sNjWLUB/AWh0bIrzip0iE+nUQV2p4 qE/GHhghdzkjpmOkPG+1oTOKRu1OsFUVnCnJht9fze0wd5HRjCVNZZUcMwv5b8ZgqO0k ygIF/rf9Ogq8A+zXj+F336WjQD2ZJuCNvc7Vmjl5WVxXDUGCfbReBgxiqa7agplU5u5f zTHiSPVRhZzAAs+RNz0CllvQ5h50bJnHSnP27IoICPTU61Jp/lLxEWbjVjxIZUAnxsyx L9Pg== 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 :dkim-signature; bh=Oz9w/MGSJCfVJoFxXdjwOXlp0YlOCXLVVNfaqF8/1eE=; fh=fSXVLHwLVZUSMdZDg9HEkUgoYaHdB2a/uUJhk+aU25A=; b=alQukwUAbLXNUjGq6aM5fBWYuOChkfhiJJelWY9pyk8s2hQll3syXZ51v+uQX1fFQd p0wbG3e3BPZeeQegQgMskvwPHITJqgAHhKB5mVsMarsv+0R3Gn7i3LpH/hw0lTEnmUxq qgEzsET1JyEvlrENUtfHm05zdgOz0FHNGzp7wYtBTodMae9ZiUQvXeB8xKjZd0i91j3b Ouve6Z0nus/I+/e+8aYbp5vzk4H59eMiNuEq1bJp/Olkk9c61/OK9Fn/QKTaY4ckKz5B HL9B+ntEKypECg2WUNz720zcJ2zQVSo43zLQJJu/gs4VFNGN8Tf6T913xsDOnraiG+1a BcEQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mbrIcWtj; 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-40625-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40625-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s61-20020a17090a69c300b0029028f583fbsi1676099pjj.104.2024.01.26.12.26.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 12:26:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40625-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mbrIcWtj; 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-40625-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40625-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 81A862855DB for ; Fri, 26 Jan 2024 20:25:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 773B22374F; Fri, 26 Jan 2024 20:25:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mbrIcWtj" Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 E67C322EF6 for ; Fri, 26 Jan 2024 20:25:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706300745; cv=none; b=Fi5LG1o5sGXNu2vD1b5fGkWagSeiPykE55pY4SqVyKkEZxwHKtsh7sf6HQZyX/8S4mAtIp1VpIOs6OHvLQU9yzaQA6dj5XHiEq65uwmi2H9/LLfDwg8GkUYQdT9OM3nOiw68kHAWeukK9QDu5isNrfqGeP8Df5iQf21yA16/IDY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706300745; c=relaxed/simple; bh=3cIgTjRr4HktkHh94CWSDggXBSkYHF5KchgT06kgK8E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=R3PY4ZDb8ed0CZbS+FnasSef6eW4sf28pCvKYJHyDsDkJ/s1hTLph8/LVAwT32y7FeJ7OXu39mYR098J4fErtBLy7FfOm6Y8zbCBPQHyC7R5NtFM/YELidZlTLPMe9kOB5SdX+azlIGdeiKukHGwIyRCx7MfzaX8gRUYeweZyzw= 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=mbrIcWtj; arc=none smtp.client-ip=209.85.215.202 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-pg1-f202.google.com with SMTP id 41be03b00d2f7-5ce632b2adfso908141a12.0 for ; Fri, 26 Jan 2024 12:25:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706300743; x=1706905543; 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=Oz9w/MGSJCfVJoFxXdjwOXlp0YlOCXLVVNfaqF8/1eE=; b=mbrIcWtjdN77LO/573eRyQAH0VvCYfhC2nHyg6CIIcd69DwESQE6AUgI/tdyWfm7Px kAEYrjAlkdSeoAhVh80jS2goOcyWynSzTQ5sao0qHBnC/M3joZhi4oyYVifix4NM9VAW Q0EnPDSVUZCo8r1scxBMJZ+CBSt7Oh678NZhExGxOf2YeI3dgh71xr35XdrEWYunCKCy P9f0NOHtbkEWyyXnoPwpkZ/WfJhJbWVTjbS/0k8rwa8v/u5vMXn66EVsFtM0A4f1UVrP GVo2lx6TdiQ1FeWl+lU1blCBMyMrg9UDs/HZZjzFJd+7WV+ZC3gQ5Ozi5FrJ41/+x8U1 tEsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706300743; x=1706905543; 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=Oz9w/MGSJCfVJoFxXdjwOXlp0YlOCXLVVNfaqF8/1eE=; b=ttoYy8o7q7OxhW99mc6jcjCDdyGVEFyUgFMJPhW46B/1eNKuYVD9NVLe7BWlG45Jl8 0xvmz36FvhG6PGHnGd27BA27FFcKEztrlmEzkE8fVY1lW2Cej0EVE+Z1t1twhMA2IDe1 ILpndSmqjRzHBBJgTL1A0e+CuQgpk5+x1tntRMydEs7KaZ9bm7dXWtwRFkXU88jd55KH o22xBNZQU1hBptxELz5L79zGQDSREThJbmBFpXzKjg8PwAxjXWHkcu4RCI916m2oNO1x wrfOT0KS7Y9wqw7B3qdBpuImjBY7BS44Oiw/LMnKUfYr8ZjYWOBE9E3jbxN9NeSPiDi8 jNuQ== X-Gm-Message-State: AOJu0Yx3PxSl9v3vNeyQvzENQiUkBGxfE0EdT13L++G+2eoyq7EFK0WO O6/L27cT7XzuAeyzATJsk3JKcICzkQgok/WctnMbSiMPfqqJsr9QOk29/j5p1nN9DAS0M0tuAF2 O3A== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:78a:b0:5d7:97b9:3084 with SMTP id cd10-20020a056a02078a00b005d797b93084mr2394pgb.0.1706300742964; Fri, 26 Jan 2024 12:25:42 -0800 (PST) Date: Fri, 26 Jan 2024 12:25:41 -0800 In-Reply-To: <20231117052210.26396-1-shahuang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231117052210.26396-1-shahuang@redhat.com> Message-ID: Subject: Re: [PATCH v2] KVM: selftests: Fix the dirty_log_test semaphore imbalance From: Sean Christopherson To: Shaoqin Huang Cc: kvm@vger.kernel.org, kvmarm@lists.linux.dev, Paolo Bonzini , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu Content-Type: text/plain; charset="us-ascii" +Peter On Fri, Nov 17, 2023, Shaoqin Huang wrote: > When execute the dirty_log_test on some aarch64 machine, it sometimes > trigger the ASSERT: > > ==== Test Assertion Failure ==== > dirty_log_test.c:384: dirty_ring_vcpu_ring_full > pid=14854 tid=14854 errno=22 - Invalid argument > 1 0x00000000004033eb: dirty_ring_collect_dirty_pages at dirty_log_test.c:384 > 2 0x0000000000402d27: log_mode_collect_dirty_pages at dirty_log_test.c:505 > 3 (inlined by) run_test at dirty_log_test.c:802 > 4 0x0000000000403dc7: for_each_guest_mode at guest_modes.c:100 > 5 0x0000000000401dff: main at dirty_log_test.c:941 (discriminator 3) > 6 0x0000ffff9be173c7: ?? ??:0 > 7 0x0000ffff9be1749f: ?? ??:0 > 8 0x000000000040206f: _start at ??:? > Didn't continue vcpu even without ring full > > The dirty_log_test fails when execute the dirty-ring test, this is > because the sem_vcpu_cont and the sem_vcpu_stop is non-zero value when > execute the dirty_ring_collect_dirty_pages() function. When those two > sem_t variables are non-zero, the dirty_ring_wait_vcpu() at the > beginning of the dirty_ring_collect_dirty_pages() will not wait for the > vcpu to stop, but continue to execute the following code. In this case, > before vcpu stop, if the dirty_ring_vcpu_ring_full is true, and the > dirty_ring_collect_dirty_pages() has passed the check for the > dirty_ring_vcpu_ring_full but hasn't execute the check for the > continued_vcpu, the vcpu stop, and set the dirty_ring_vcpu_ring_full to > false. Then dirty_ring_collect_dirty_pages() will trigger the ASSERT. > > Why sem_vcpu_cont and sem_vcpu_stop can be non-zero value? It's because > the dirty_ring_before_vcpu_join() execute the sem_post(&sem_vcpu_cont) > at the end of each dirty-ring test. It can cause two cases: > > 1. sem_vcpu_cont be non-zero. When we set the host_quit to be true, > the vcpu_worker directly see the host_quit to be true, it quit. So > the log_mode_before_vcpu_join() function will set the sem_vcpu_cont > to 1, since the vcpu_worker has quit, it won't consume it. > 2. sem_vcpu_stop be non-zero. When we set the host_quit to be true, > the vcpu_worker has entered the guest state, the next time it exit > from guest state, it will set the sem_vcpu_stop to 1, and then see > the host_quit, no one will consume the sem_vcpu_stop. > > When execute more and more dirty-ring tests, the sem_vcpu_cont and > sem_vcpu_stop can be larger and larger, which makes many code paths > don't wait for the sem_t. Thus finally cause the problem. > > To fix this problem, we can wait a while before set the host_quit to > true, which gives the vcpu time to enter the guest state, so it will > exit again. Then we can wait the vcpu to exit, and let it continue > again, then the vcpu will see the host_quit. Thus the sem_vcpu_cont and > sem_vcpu_stop will be both zero when test finished. > > Signed-off-by: Shaoqin Huang > --- > v1->v2: > - Fix the real logic bug, not just fresh the context. > > v1: https://lore.kernel.org/all/20231116093536.22256-1-shahuang@redhat.com/ > --- > tools/testing/selftests/kvm/dirty_log_test.c | 16 +++++++++++++++- > 1 file changed, 15 insertions(+), 1 deletion(-) > > diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c > index 936f3a8d1b83..a6e0ff46a07c 100644 > --- a/tools/testing/selftests/kvm/dirty_log_test.c > +++ b/tools/testing/selftests/kvm/dirty_log_test.c > @@ -417,7 +417,8 @@ static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err) > > static void dirty_ring_before_vcpu_join(void) > { > - /* Kick another round of vcpu just to make sure it will quit */ > + /* Wait vcpu exit, and let it continue to see the host_quit. */ > + dirty_ring_wait_vcpu(); > sem_post(&sem_vcpu_cont); > } > > @@ -719,6 +720,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) > struct kvm_vm *vm; > unsigned long *bmap; > uint32_t ring_buf_idx = 0; > + int sem_val; > > if (!log_mode_supported()) { > print_skip("Log mode '%s' not supported", > @@ -726,6 +728,11 @@ static void run_test(enum vm_guest_mode mode, void *arg) > return; > } > > + sem_getvalue(&sem_vcpu_stop, &sem_val); > + assert(sem_val == 0); > + sem_getvalue(&sem_vcpu_cont, &sem_val); > + assert(sem_val == 0); Never use bare assert() in selftests, they'll either get compiled out or IIRC, will cause the test to silently exit if they fail. Either do nothing, or use TEST_ASSERT(). > + > /* > * We reserve page table for 2 times of extra dirty mem which > * will definitely cover the original (1G+) test range. Here > @@ -825,6 +832,13 @@ static void run_test(enum vm_guest_mode mode, void *arg) > sync_global_to_guest(vm, iteration); > } > > + /* > + * > + * Before we set the host_quit, let the vcpu has time to run, to make > + * sure we consume the sem_vcpu_stop and the vcpu consume the > + * sem_vcpu_cont, to keep the semaphore balance. > + */ > + usleep(p->interval * 1000); Please no. "Wait for a while" is never a complete solution for fixing races. In rare cases, adding a delay might be the only sane workaround, but I doubt that's the case here.