Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3115331pxm; Mon, 28 Feb 2022 12:20:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJzj5AEIH7YcHYV+6eeHml1ang+4F8eM8wBJN3VNfyhxhW97tnBIi8JD+c2AGG7Ywv10Gica X-Received: by 2002:a63:515d:0:b0:373:8aca:7a40 with SMTP id r29-20020a63515d000000b003738aca7a40mr18667531pgl.284.1646079614001; Mon, 28 Feb 2022 12:20:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646079613; cv=none; d=google.com; s=arc-20160816; b=FCWECAEcf/51ZImA8Th7g8UI/0SkJWUhTfhMuMW9g9WXOVGDpG1N+lTmCkaB2IOiob Hq7tXCpjq2cDOh7Gro+ZvfjQ23x96cjyuig+/+m90AhS9esX7/hkbkjlVS6Y6HXHYi4G Oy9/Yjx8IuXuiz1oGF9pCVf30y832H0jcdAqcGJqkYZdiavn/TdMaWIv83BAB4JQa1kv rB2oWLCmASRvfIAziiadmRGpbTYQw2dC+1yEcFFLscV5FPR6UDpgAwWP7dOtOUAn5B7M GNUsh9yDWcNRmYcfa7/AuMRTA16/WCc0GGJ3QpO5ufatTrUiMuUn359kK1aT7TF1pGlo 6jeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=vHyp/HdP6EKoE2lNho2fp5r+pKrE3pUt+iCFlO52HYQ=; b=B3RKpRJkTNN4tFhL+XAkftOpQzg4kwI5uuMwQYx5+speihpMSeCgcZ/nj/p72jmUBE XWt7vHCsX0teEnk1pXYWJgfhgiBB09cbsept2FoFUsBR2331bK/IqenoeeOWvQM5lbZf H79ZXCUbXYbomUZN6K4Sytffm3HH3VYf8ZqjzukgQ7kdd/8NSspNGZA0VYTzR8n989x+ oW/qRe6Zg06F76dDohQX6PhwVLG5c+jNK+7K3DyokMeVpNjcL50PMwpLpIFkK5cB9isO ckRkAcD6X56zHsuZFHkbhTIQWPSURf0JPnvOqVAfyLhL8vA3EH1jFBmDNV3112AXJVBt bslA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Yv2YexqF; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id bi12-20020a170902bf0c00b0014fd360c0e8si9797347plb.372.2022.02.28.12.20.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 12:20:13 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Yv2YexqF; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9EDEF1B65D3; Mon, 28 Feb 2022 11:40:48 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231793AbiB1Rwf (ORCPT + 99 others); Mon, 28 Feb 2022 12:52:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238806AbiB1Rrp (ORCPT ); Mon, 28 Feb 2022 12:47:45 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92EA7888F1; Mon, 28 Feb 2022 09:38:08 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 33B58B815A6; Mon, 28 Feb 2022 17:38:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 893E3C340E7; Mon, 28 Feb 2022 17:38:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646069885; bh=Om8+rVBysDWELT0kes9emjuIoy4hLxF5bRKgmKIMhJ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Yv2YexqFLVayHLCtlPky01pb38tzwHe9ZxWy+ZXpu6PPwwyijIpXzwQ7jmZ+zuZwx UPLG0rq4W/nODMYDyN199KbqvlbeUViI2pS20C1VMTrVCbitvci195n9ztDn00DDJV 9N2gKaLn8PkpoB2leLJJ9seSuef/ea/o9ZfC+0gA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Liang Zhang , Paolo Bonzini Subject: [PATCH 5.15 014/139] KVM: x86/mmu: make apf token non-zero to fix bug Date: Mon, 28 Feb 2022 18:23:08 +0100 Message-Id: <20220228172349.184013379@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220228172347.614588246@linuxfoundation.org> References: <20220228172347.614588246@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Liang Zhang commit 6f3c1fc53d86d580d8d6d749c4af23705e4f6f79 upstream. In current async pagefault logic, when a page is ready, KVM relies on kvm_arch_can_dequeue_async_page_present() to determine whether to deliver a READY event to the Guest. This function test token value of struct kvm_vcpu_pv_apf_data, which must be reset to zero by Guest kernel when a READY event is finished by Guest. If value is zero meaning that a READY event is done, so the KVM can deliver another. But the kvm_arch_setup_async_pf() may produce a valid token with zero value, which is confused with previous mention and may lead the loss of this READY event. This bug may cause task blocked forever in Guest: INFO: task stress:7532 blocked for more than 1254 seconds. Not tainted 5.10.0 #16 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. task:stress state:D stack: 0 pid: 7532 ppid: 1409 flags:0x00000080 Call Trace: __schedule+0x1e7/0x650 schedule+0x46/0xb0 kvm_async_pf_task_wait_schedule+0xad/0xe0 ? exit_to_user_mode_prepare+0x60/0x70 __kvm_handle_async_pf+0x4f/0xb0 ? asm_exc_page_fault+0x8/0x30 exc_page_fault+0x6f/0x110 ? asm_exc_page_fault+0x8/0x30 asm_exc_page_fault+0x1e/0x30 RIP: 0033:0x402d00 RSP: 002b:00007ffd31912500 EFLAGS: 00010206 RAX: 0000000000071000 RBX: ffffffffffffffff RCX: 00000000021a32b0 RDX: 000000000007d011 RSI: 000000000007d000 RDI: 00000000021262b0 RBP: 00000000021262b0 R08: 0000000000000003 R09: 0000000000000086 R10: 00000000000000eb R11: 00007fefbdf2baa0 R12: 0000000000000000 R13: 0000000000000002 R14: 000000000007d000 R15: 0000000000001000 Signed-off-by: Liang Zhang Message-Id: <20220222031239.1076682-1-zhangliang5@huawei.com> Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/mmu/mmu.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3889,12 +3889,23 @@ static void shadow_page_table_clear_floo walk_shadow_page_lockless_end(vcpu); } +static u32 alloc_apf_token(struct kvm_vcpu *vcpu) +{ + /* make sure the token value is not 0 */ + u32 id = vcpu->arch.apf.id; + + if (id << 12 == 0) + vcpu->arch.apf.id = 1; + + return (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; +} + static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, gfn_t gfn) { struct kvm_arch_async_pf arch; - arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; + arch.token = alloc_apf_token(vcpu); arch.gfn = gfn; arch.direct_map = vcpu->arch.mmu->direct_map; arch.cr3 = vcpu->arch.mmu->get_guest_pgd(vcpu);