Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3781328pxt; Tue, 10 Aug 2021 11:10:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyI5gaqsWCoXlbU8jjmYYcchZJmq1zgCuK7lCEyUViZm/HPA6mKsh+o44P9oyRuBpCES+dD X-Received: by 2002:a17:906:3486:: with SMTP id g6mr1340277ejb.71.1628619009771; Tue, 10 Aug 2021 11:10:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628619009; cv=none; d=google.com; s=arc-20160816; b=0UaSHr0Ang4Yv2x40Lr/4Yg3jZyYX/y3OX071N59tGuyPJGzO9vs6g33dw1uyN0bv0 nxlGKAZc9IDHemMPmuNZdywrO2OJQM5MQZG9dEMdX5T6ky6o07b3RIwGuGFfx1tupxYJ pjb3DSAJtm8qHigytCaNfRAVLyk1Xnn2NyHst4PcZAGG3KwyPFBbp9vMlroI2zLZ9VGY FMm3c/T+NpLVSp4aCCfNmTK1Y25r7cYW1tGjI2iXRCwrlcCXvIsU/yMG6oBJwqtnKbzv WMGKhgaYykwAGRopbCDuXtq+YdAbMLaIvPrbAfdgnYE4oHUfBVTQ33h+uFpc4vQS1IkX o7+A== 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=pnFu0m3ElIXjSxzqNCinXFYUMBFBu4H6thoN1XPYwkA=; b=sySFupx6fiTZyTvP/ngjTGVlwDKSCzDIAf4b+0u9AW6NQDZGPQx9RVJojBCZNB6xr3 EdKktGTrGzKI/+cF/wdMGI6ScPaRprx0wtlv4xpukL09nXixzzroFaPkRK3uKpSAad+x jNuxWH2GjaujU5QdMiabTUJnxCoN7WGu3CYmOQkD9mK0W5rOLKKqCyyDGbhHv9MpJ6AX j+hD0dGZsrtDnCuNaELpKG7ewRLn8dS/L/GpFHwjaZbJOaVHZW+HjOS7jqiRsTjZ669R 5/WlRQVLOqBg1P0LSlx19qmIMyEoE1WHoIdYIX/DThDDqKjRp6audEJI0PPLU1Veij6M 57fQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jyJ0KET9; 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 e24si7146431edq.444.2021.08.10.11.09.41; Tue, 10 Aug 2021 11:10:09 -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=jyJ0KET9; 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 S235938AbhHJSFn (ORCPT + 99 others); Tue, 10 Aug 2021 14:05:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:34390 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238129AbhHJSBw (ORCPT ); Tue, 10 Aug 2021 14:01:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 771BC613DA; Tue, 10 Aug 2021 17:47:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628617643; bh=5M5M3sdq2i2O4Ufgtw5A4/fgVviZ/zq7V4t3gyASRsQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jyJ0KET96Rr1MICeY5GM7RSgG4+TYgmBHm+s5E99eaMlLa87CVqaZCHWZxX7DXdjD X6vp2G6WA5dyvzVFF30whQG68pimOlElRxOlkhad/zZGcFJ6iIINJwYK4i6UOwo+nW mS8ok/QpBsG0Xu7xEmp3wNbzP4uAAMvFJjEWwW5c= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ben Gardon , Sean Christopherson , Jim Mattson , Paolo Bonzini Subject: [PATCH 5.13 150/175] KVM: x86/mmu: Fix per-cpu counter corruption on 32-bit builds Date: Tue, 10 Aug 2021 19:30:58 +0200 Message-Id: <20210810173005.910554769@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810173000.928681411@linuxfoundation.org> References: <20210810173000.928681411@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: Sean Christopherson commit d5aaad6f83420efb8357ac8e11c868708b22d0a9 upstream. Take a signed 'long' instead of an 'unsigned long' for the number of pages to add/subtract to the total number of pages used by the MMU. This fixes a zero-extension bug on 32-bit kernels that effectively corrupts the per-cpu counter used by the shrinker. Per-cpu counters take a signed 64-bit value on both 32-bit and 64-bit kernels, whereas kvm_mod_used_mmu_pages() takes an unsigned long and thus an unsigned 32-bit value on 32-bit kernels. As a result, the value used to adjust the per-cpu counter is zero-extended (unsigned -> signed), not sign-extended (signed -> signed), and so KVM's intended -1 gets morphed to 4294967295 and effectively corrupts the counter. This was found by a staggering amount of sheer dumb luck when running kvm-unit-tests on a 32-bit KVM build. The shrinker just happened to kick in while running tests and do_shrink_slab() logged an error about trying to free a negative number of objects. The truly lucky part is that the kernel just happened to be a slightly stale build, as the shrinker no longer yells about negative objects as of commit 18bb473e5031 ("mm: vmscan: shrink deferred objects proportional to priority"). vmscan: shrink_slab: mmu_shrink_scan+0x0/0x210 [kvm] negative objects to delete nr=-858993460 Fixes: bc8a3d8925a8 ("kvm: mmu: Fix overflow on kvm mmu page limit calculation") Cc: stable@vger.kernel.org Cc: Ben Gardon Signed-off-by: Sean Christopherson Message-Id: <20210804214609.1096003-1-seanjc@google.com> Reviewed-by: Jim Mattson Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/mmu/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1546,7 +1546,7 @@ static int is_empty_shadow_page(u64 *spt * aggregate version in order to make the slab shrinker * faster */ -static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, unsigned long nr) +static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, long nr) { kvm->arch.n_used_mmu_pages += nr; percpu_counter_add(&kvm_total_used_mmu_pages, nr);