From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1B0AC4320A for ; Wed, 4 Aug 2021 21:46:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8817860249 for ; Wed, 4 Aug 2021 21:46:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233796AbhHDVq2 (ORCPT ); Wed, 4 Aug 2021 17:46:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231345AbhHDVq0 (ORCPT ); Wed, 4 Aug 2021 17:46:26 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6C9FC061798 for ; Wed, 4 Aug 2021 14:46:13 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id o32-20020a0c85a30000b0290328f91ede2bso2483985qva.1 for ; Wed, 04 Aug 2021 14:46:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:message-id:mime-version:subject:from:to:cc; bh=C4TQOA8B8J/q4Zfe5kHKEB3lmPCutdPC4QZ7gqnArH8=; b=YNPOfuewQrcWJCQ/5Iz1uNhVfCb1JkA4TKkX/0eh8jFXMExJPlyT8oXMGZuw00qiCd C1/GMBA6RZBqyhevuhfebMNc+N2QsjkyLxFe07Cc+rN13zJCqwy7DrMvbPFF7OQaZO5K bhXLFz6y7Of7H+rEatnX55xtleoN0xU/Aud13OEC0HEB35o5FXSwWY+qLM6V8dOi80aB yLbZWKMiIA2jqt8de0baS96IM6KRrc0+2tfoXthF8vlgqfgtK5Hnfm5/a54wXuChfA35 sTHKSjHy5zp4Sr8X9yl8pPez1sljtm+vUKXWatUrSr4GvZk7U/8pozMdAIwomPipG4CQ OnqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:message-id:mime-version:subject :from:to:cc; bh=C4TQOA8B8J/q4Zfe5kHKEB3lmPCutdPC4QZ7gqnArH8=; b=RPMatwVbtfXt1OozOLuVCIZdxllWaLuv4c+BWH1rHUWz1TXW6D//gGwDl7Ck8Uuood UpTtNrg4xnfDL+F6w5n/Ovp0LIRYr9ltCYEsHubtnfTLX5iTVPv2KUAGx/3kQ25ItEsj etcrPqZXBBMjeCrm00nLHNSThffbKE0YbfPF8fSKbVn82E45BJ2IE3k9ht1ImzLXb97x 2v/IcoLVNaqZ1tnrSLlJAxhZETMEiU2D5FSf3170rGGxHnsIT6Ht8yGeISS463hh+8t5 eWd3xFm92/MeJMoU44e+SB9EEMsORDHjn1aqLxTA7692l0rujWYtw38fTpmKHILtor5g SxxA== X-Gm-Message-State: AOAM5316un5+qIVaO+zcNamlEfJPMJ5iRVYT/00LtdHqbEC5U7ZWAphI g3xwQkwyYeCuJvYyGMRz5Q7FxqBXhao= X-Google-Smtp-Source: ABdhPJzre0zuQgcoCCv3rfHIjO6dR7eyTRXfK2AxxKiu1wox9S+sbtAdIzb3L38QimrvyQF7xC/AQRzu05o= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:e041:28e5:75db:9055]) (user=seanjc job=sendgmr) by 2002:a0c:a321:: with SMTP id u30mr1545572qvu.57.1628113573026; Wed, 04 Aug 2021 14:46:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 4 Aug 2021 14:46:09 -0700 Message-Id: <20210804214609.1096003-1-seanjc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.554.ge1b32706d8-goog Subject: [PATCH] KVM: x86/mmu: Fix per-cpu counter corruption on 32-bit builds From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); -- 2.32.0.554.ge1b32706d8-goog