Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1699554pxb; Thu, 16 Sep 2021 13:19:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWyTzKS+0YkkF+RwEW0dj18L22rJBB2xLXLf+bbSAey2WQQSSVBMOWCWiWIw/8v0mvr5LX X-Received: by 2002:a05:6402:3514:: with SMTP id b20mr8531333edd.152.1631823558744; Thu, 16 Sep 2021 13:19:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631823558; cv=none; d=google.com; s=arc-20160816; b=CZt2rWKdb4PcR2qezEKxgV/VJel3aFJkpZ9KE1RcAwHVuDurLGzlzVmMXxzCt6Gpsj YsI0IDhQdXDnZHXw5ASRuK5X9UsDWjwDEb3rrOXLKaXqrO24BmwCjFNkoqRyRFVWknOT CexOdGVONNp5+NqoggxrM9E3Zj85UtSuIDsdzyc/yRT4qJhlqwwzPxp+JXhibJcraiCP /OCwCIWOsyEO7uj6a6BM6/pwsa25YLG+/sOl3PmecF3HdPdHOfn0nTCPGhmsqMUK50ob dQ6oix0mpJh7eDGQnBoenhb58hrnYBm8YtTP35quclk9u7CARxYnwtxVbZnAPQMFnNil qkZg== 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=FyPV01vSUCoKWaCTS2AJe92tYbe8Id03BKZJMZep5hs=; b=xWcE/tq5uBuNlfWgn2VJAp7l3JBw7quGfw6ipDuqZTfqY3G2uB6J/OTjYMibCHmV4k bMTi3Ed9lgdyycVaOz/kROoNaY5m/YKyyPRI00WY6WvGDqfkP26mSGlLT2mx3LVtu0CP Z/d8hU5ISrSr/sqVrEM9ZLa6G8X7TCGyurkSFMBRXuUpQmIn+2PZAPp7VZ7OWySOccsb Dxrmo96cupXcbXInUlLlWa3k4OayQnqwJFGlvdzCzSwFiHKjT1v5S3YYtHls+gkQV9Bj 8YQqtpTj9TNLJ9VqzglUQrFHSDzyVDuuQtF4+SExaEjPnDJJM/J86FTTa1f3Uja7ehL6 s6rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oXYl8g0X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x13si4204811edd.531.2021.09.16.13.18.54; Thu, 16 Sep 2021 13:19:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oXYl8g0X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234526AbhIPQPf (ORCPT + 99 others); Thu, 16 Sep 2021 12:15:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:48498 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236184AbhIPQJP (ORCPT ); Thu, 16 Sep 2021 12:09:15 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 12A4B61368; Thu, 16 Sep 2021 16:07:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631808466; bh=YYBho95QdOM5VhV5o1hM07Vz0fnKXDen22OQtXq+tsM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oXYl8g0X6m3kbNJvlDOEfwLx+jVAvovSwRmM2UqEEleTuD/Jcy9DhbM8E4m2nfj59 QdOOJIHcIhlp2iwRg3cfbDsbvFY7x4+rpq6TGqdh66rlHFtDIs2UDjhF0vFCGZ7K5W kLXnk3HHV58I+C+VeLbS9g2FFsnozXK2aolSYUwc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexey Kardashevskiy , Michael Ellerman , Sasha Levin Subject: [PATCH 5.10 100/306] KVM: PPC: Fix clearing never mapped TCEs in realmode Date: Thu, 16 Sep 2021 17:57:25 +0200 Message-Id: <20210916155757.480379205@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155753.903069397@linuxfoundation.org> References: <20210916155753.903069397@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexey Kardashevskiy [ Upstream commit 1d78dfde33a02da1d816279c2e3452978b7abd39 ] Since commit e1a1ef84cd07 ("KVM: PPC: Book3S: Allocate guest TCEs on demand too"), pages for TCE tables for KVM guests are allocated only when needed. This allows skipping any update when clearing TCEs. This works mostly fine as TCE updates are handled when the MMU is enabled. The realmode handlers fail with H_TOO_HARD when pages are not yet allocated, except when clearing a TCE in which case KVM prints a warning and proceeds to dereference a NULL pointer, which crashes the host OS. This has not been caught so far as the change in commit e1a1ef84cd07 is reasonably new, and POWER9 runs mostly radix which does not use realmode handlers. With hash, the default TCE table is memset() by QEMU when the machine is reset which triggers page faults and the KVM TCE device's kvm_spapr_tce_fault() handles those with MMU on. And the huge DMA windows are not cleared by VMs which instead successfully create a DMA window big enough to map the VM memory 1:1 and then VMs just map everything without clearing. This started crashing now as commit 381ceda88c4c ("powerpc/pseries/iommu: Make use of DDW for indirect mapping") added a mode when a dymanic DMA window not big enough to map the VM memory 1:1 but it is used anyway, and the VM now is the first (i.e. not QEMU) to clear a just created table. Note that upstream QEMU needs to be modified to trigger the VM to trigger the host OS crash. This replaces WARN_ON_ONCE_RM() with a check and return, and adds another warning if TCE is not being cleared. Fixes: e1a1ef84cd07 ("KVM: PPC: Book3S: Allocate guest TCEs on demand too") Signed-off-by: Alexey Kardashevskiy Signed-off-by: Michael Ellerman Link: https://lore.kernel.org/r/20210827040706.517652-1-aik@ozlabs.ru Signed-off-by: Sasha Levin --- arch/powerpc/kvm/book3s_64_vio_hv.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/kvm/book3s_64_vio_hv.c b/arch/powerpc/kvm/book3s_64_vio_hv.c index 083a4e037718..e5ba96c41f3f 100644 --- a/arch/powerpc/kvm/book3s_64_vio_hv.c +++ b/arch/powerpc/kvm/book3s_64_vio_hv.c @@ -173,10 +173,13 @@ static void kvmppc_rm_tce_put(struct kvmppc_spapr_tce_table *stt, idx -= stt->offset; page = stt->pages[idx / TCES_PER_PAGE]; /* - * page must not be NULL in real mode, - * kvmppc_rm_ioba_validate() must have taken care of this. + * kvmppc_rm_ioba_validate() allows pages not be allocated if TCE is + * being cleared, otherwise it returns H_TOO_HARD and we skip this. */ - WARN_ON_ONCE_RM(!page); + if (!page) { + WARN_ON_ONCE_RM(tce != 0); + return; + } tbl = kvmppc_page_address(page); tbl[idx % TCES_PER_PAGE] = tce; -- 2.30.2