Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp723319pxt; Thu, 5 Aug 2021 10:02:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwOFQd3SCM6ml/WTzCO7d5YE/EdKHN4LOEJIPP17j8X3A4BKga8XuU2BcwDjvAW8JWtFCYO X-Received: by 2002:a17:906:5ac5:: with SMTP id x5mr5737605ejs.271.1628182968864; Thu, 05 Aug 2021 10:02:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628182968; cv=none; d=google.com; s=arc-20160816; b=XBT+vZuympl6rPMYKshIvwCDccS+amETbpWXGDNsYTWp1CzCqwzB8OSFzLXHApWQtu yq6EWpIZxVpTRAMGJHuf+IH0tQ88O+zpFnIMLMJ5+orImTQ+sIH9adSIrUVo5bTesdrT 2SJgmFhUEkkrBS3g5hS0Pd7f2VYyfdhnZbOuRloD5HqDhIfINeHyAQspDQJBL7uU9wEX j4s8QsK9lc2X+C6ks849JUjwbGJiDagLxmzeFLVHHVikNBgL07eWzkEBUxjPzzgxXAqP jWz7yCeUu8Rze3ZD/0xO6LPtR9EGB7wW1kO0csKCgYM3ldkjwbrhLMMwsMT06DtQDLtu kvpw== 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:date:cc:to:from:subject :message-id:dkim-signature; bh=DjMNCHMyDCy1jfhKIOW9ACx2HA3RpxUNEcWNfn6GUuE=; b=xCmOAegF6G+CqA7Hg8H9OPkfTt8JhUKMeKFyIJuzWpyH2EZ/QDmw6k1os50PWInDnL azb2eeXN9zWJKafLxezuEzUIihd9bEDN770nWPwuheEha/eYTWgroTYK0kWlumOpVu4N EyzKhGh6UaC1Msa5vxaOdD52qUXdGo+RsX2FqloGYlyhbXX8WzCzohg4tVzhUkk82Dgn AjlnW+n9NEjBduUgDIHqObTDJUWmwKQpzGbDqt9G7wqyerLrz/E+PuWMn+lcSnMketdl sIl2TYwQua/WDVpheXM08P6nmd8M8HMareDnpHfnHPCigbu0ma8iaFcckhic/DlXOFN6 2V8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DJnjIu79; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u9si3030198edf.583.2021.08.05.10.02.23; Thu, 05 Aug 2021 10:02:48 -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=@redhat.com header.s=mimecast20190719 header.b=DJnjIu79; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241050AbhHEL1B (ORCPT + 99 others); Thu, 5 Aug 2021 07:27:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:32097 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240893AbhHEL1A (ORCPT ); Thu, 5 Aug 2021 07:27:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628162805; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DjMNCHMyDCy1jfhKIOW9ACx2HA3RpxUNEcWNfn6GUuE=; b=DJnjIu79BOe5QPcWlhQ8n/kBSDdgij5TofhD/qjPHuBOZworvCr0GQcbUfo8mc+WeEkhLU DdV1sb4YJ9OquIvLiSOgQgc+xajntw9rXoGM1OytmZwBQ1x5K/sQTCXC9D5s4F+kkOco9r 86dkP1TvbIJW5vThZf5fdFdPPEFgNPw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-365-xGV4-VKZPp-6CLV0ThoiMA-1; Thu, 05 Aug 2021 07:26:44 -0400 X-MC-Unique: xGV4-VKZPp-6CLV0ThoiMA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 664D01084F4C; Thu, 5 Aug 2021 11:26:42 +0000 (UTC) Received: from starship (unknown [10.35.206.50]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0029060BD9; Thu, 5 Aug 2021 11:26:39 +0000 (UTC) Message-ID: <63b556880ceaf9a9455eca9517d47c7b33e7260d.camel@redhat.com> Subject: Re: [PATCH] KVM: x86/mmu: Fix per-cpu counter corruption on 32-bit builds From: Maxim Levitsky To: Sean Christopherson , Paolo Bonzini Cc: Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon Date: Thu, 05 Aug 2021 14:26:38 +0300 In-Reply-To: <20210804214609.1096003-1-seanjc@google.com> References: <20210804214609.1096003-1-seanjc@google.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2021-08-04 at 14:46 -0700, Sean Christopherson wrote: > 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 > --- > arch/x86/kvm/mmu/mmu.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index b4b65c21b2ca..082a0ba79edd 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -1700,7 +1700,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); I am almost sure that I seen this bug as well (I do test 32 bit KVM hosts, even with nested 32 bit guests once in a while), but I didn't dare to investigate it due to the fact the 32 bit KVM host is a very rare thing these days. Reviewed-by: Maxim Levitsky Best regards, Maxim Levitsky