Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3108414pxm; Mon, 28 Feb 2022 12:10:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJxh5D+ipk287LpetKk73DfkBfz8FKKtNiPjU8Oz9eef2zP19WyyMK69ACs5aT4AQoKsZYrx X-Received: by 2002:a17:902:b183:b0:14f:c266:20d5 with SMTP id s3-20020a170902b18300b0014fc26620d5mr22463154plr.136.1646079048783; Mon, 28 Feb 2022 12:10:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646079048; cv=none; d=google.com; s=arc-20160816; b=zK6ZHYvoRIBbRmzY8mbo1JU/LenKEGYhaCEyyS61AGPADOVBszCzUWmZo2gcQp+ldK +0mtGXJ2PoXZnyXQrwI1R6Jn4s2UpQqr/MjVf1bbXcFsY5xPAbr8ZJOReIS7jpsQYq/j Rd6vPoZRIjMxZpX7XMBN5PafdmzbJkxPIXKKtrtIRKQN8DXwlCkeeH3wIWVZoFwShO/o PHKOEQ6eeI9MQqgIr6iQjq3OtA+D8joKJWis4kWysZsDjp1PyAROiyKCLaLy1zA7asb6 DR8QAdHIk+bh61sxUY7tyEiPwtgD0XHcbdp4JOvwh5Lsk0sGjsIOW7hA3CL38ZjfXlYw ANyg== 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=rRFPa/x9ZIIR7XUde4QjHMTapSNVL47Jubo6wNN6Q5U=; b=G59BDw9jQL1HUXiH7nVroLEre9sMi3MX+TFSMLIdtNTXpokX6fhEQLXGqnD8AuQgyE WWr2aZFFvVD0GFgeD+TYVecWPYmRa6OsOvPsRASYIx6fqA1sf054FpU33w1GhVRGJE60 CuVfhQ71hawcXvrwbvnxsNllSbZ4JPI6os9CPVX1FiXePaOOcvD3qifu/ETmsWOjVtAC ksb3BHOOtNJDwzP2hMf8L8zV/WltMvMzxSHPUm5LcoT4IFTP5ZR5tQz/UXBz+/AT5YeQ L9+I3YhM0EQLuq8I9QHyCz86snec8ghju/eF/UkXgLLCaPxCjSCPpARV810kJopt6W5J p4tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aMHp8gdf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 4-20020a630404000000b0036c0c054c44si10387508pge.363.2022.02.28.12.10.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 12:10:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aMHp8gdf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 A3C7614D708; Mon, 28 Feb 2022 11:34:38 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239616AbiB1SBq (ORCPT + 99 others); Mon, 28 Feb 2022 13:01:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240756AbiB1Rye (ORCPT ); Mon, 28 Feb 2022 12:54:34 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A68F550065; Mon, 28 Feb 2022 09:42:58 -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 5D769B815B8; Mon, 28 Feb 2022 17:42:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AEB00C340E7; Mon, 28 Feb 2022 17:42:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646070176; bh=v+vqe5cFqVXm0aqjTyrSV3VL+XcjaDQMvZr8cnMPM2g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aMHp8gdfqNTT3NarGqEYlAWhBm9CHwD/QLVgvAGqaPd6gzMb/gUwKy6MumoOqpio0 Mmrs3SNSkt+5+nWt4O7qMiTZNyTiKQru8oaW99sZjuVyGPyLN4DiyoeSbp6hBy7tXD 9e59Aj2CBs/qWnLMnm1F4R4sAaJDZkHH0UOBw2bM= 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.16 014/164] KVM: x86/mmu: make apf token non-zero to fix bug Date: Mon, 28 Feb 2022 18:22:56 +0100 Message-Id: <20220228172401.184361652@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220228172359.567256961@linuxfoundation.org> References: <20220228172359.567256961@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 @@ -3905,12 +3905,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);