From patchwork Mon May 31 17:46:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12290375 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.2 required=3.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 278CAC47082 for ; Tue, 1 Jun 2021 03:31:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF3F561090 for ; Tue, 1 Jun 2021 03:31:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232842AbhFADdD (ORCPT ); Mon, 31 May 2021 23:33:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232663AbhFADdC (ORCPT ); Mon, 31 May 2021 23:33:02 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7738C061574; Mon, 31 May 2021 20:31:21 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id 133so4632137pgf.2; Mon, 31 May 2021 20:31:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=p/jZUfwCFBPckxbdbh+7RUuX92UxFL0EGZoB0VYPQFg=; b=NLJbbkYsskv7euY+iC/duSDT8A3cUew6l/8zNBURHuO3auvYRLki4WkXyK4qlKCnxi yROL7GPERMc2Px/V+LDNMWwjRkca9caJzlQ4OZNt/vgzlYp+LCKrFaC6fg0UYBKa8KCc Z6Fq/jTUOPDor5pPCpaFA9SD89IZp6LATQDilPL5L9P93TCNcVhLQ2d5Gizjnj7cewHV TehGCYbctHfANFnkljk7HAa8KW18lQLnTBCfU2oZFvutznLV8+bwRtm5zQXCSrxoLdjb MsZ9JC1/olnZLER3HAoYh/dkxGJUczHNrGmLeff46c2Zm09Sle936/p5zwq8fF0ubYOe wWvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=p/jZUfwCFBPckxbdbh+7RUuX92UxFL0EGZoB0VYPQFg=; b=WHDBGZ3Jq95o0TWd/vKt/lh0crZZ1lFIE3LyTEm5QisnSwGD3Q6kNOIr0Y+84Rr2Oy JzpghA883I/gAFD0YfpD5Af5VDQdz8+3FWu63zWaUa7rRGJJTBF5joofsdtHyksHhhHW qcqpQ0H5en0z5dtoTbpLrGAl1IndqCMlUp3eSeMADmIYxXkUJ6f7Y7k9lz10PYBD6pc9 G1patmVUP+k7gaXNtn4JRsHKEMDlxDRedgE/e6fjjQ2JJb3EOmfVwhdvfTA/SMNjEp47 MWne8fhZ16zaNxuRHd8jz7v2/P9NdcXTDy8NF3PI8n993TCr8QfXSr5lPJHABt5NOnp+ OIVw== X-Gm-Message-State: AOAM531IGSD+DmZcXc9Aojngd/hJLi5hyh92QxmXOSA6H5H1OAps/Vgo 6BWR7owSNEClXjLBFsxH+LmjmDnzBes= X-Google-Smtp-Source: ABdhPJzjJiJONtWWBkuTi7UE/6NVJzFi9hjG8wz4DH3OqHXyDk/E69kQFs19T+T6EHwUItVBNR+A3w== X-Received: by 2002:a63:b25d:: with SMTP id t29mr10248864pgo.449.1622518281150; Mon, 31 May 2021 20:31:21 -0700 (PDT) Received: from localhost ([47.251.4.198]) by smtp.gmail.com with ESMTPSA id c11sm12190588pjr.32.2021.05.31.20.31.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 May 2021 20:31:20 -0700 (PDT) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: Lai Jiangshan , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org Subject: [PATCH] KVM: X86: reset and read st->preempted in atomic way Date: Tue, 1 Jun 2021 01:46:28 +0800 Message-Id: <20210531174628.10265-1-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan In record_steal_time(), st->preempted is read twice, and trace_kvm_pv_tlb_flush() might output result inconsistent if kvm_vcpu_flush_tlb_guest() see a different st->preempted later. It is a very trivial problem and hardly has actual harm and can be avoided by reseting and reading st->preempted in atomic way via xchg(). Signed-off-by: Lai Jiangshan Reviewed-by: Sean Christopherson diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0087d3532c98..fba39fe162da 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3117,9 +3117,11 @@ static void record_steal_time(struct kvm_vcpu *vcpu) * expensive IPIs. */ if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) { + u8 st_preempted = xchg(&st->preempted, 0); + trace_kvm_pv_tlb_flush(vcpu->vcpu_id, - st->preempted & KVM_VCPU_FLUSH_TLB); - if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) + st_preempted & KVM_VCPU_FLUSH_TLB); + if (st_preempted & KVM_VCPU_FLUSH_TLB) kvm_vcpu_flush_tlb_guest(vcpu); } else { st->preempted = 0;