[v11,1/8] KVM: Assert that a page's refcount is elevated when marking accessed/dirty
Message ID | 20240229025759.1187910-2-stevensd@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-86065-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp142698dyb; Wed, 28 Feb 2024 18:58:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUE69hcZZrCO068Wbre2tIi5XmWvhJrtfYggVIkopkmY0Dw0QAlwmBrWMcXDoi6NohIM7Wvef7EQ7YXjJIkh5ArKENC4w== X-Google-Smtp-Source: AGHT+IEVAm5y0i2n6y4bXT6GSAd0MT5qZsYAZ2ETptfcalq2K/pDSKyrn3JM0ZqJxgN0g2KSeE13 X-Received: by 2002:a62:e802:0:b0:6e4:c376:bc11 with SMTP id c2-20020a62e802000000b006e4c376bc11mr935785pfi.6.1709175529925; Wed, 28 Feb 2024 18:58:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709175529; cv=pass; d=google.com; s=arc-20160816; b=xcevZ+ns6CpUszS5drp0JxkwNxyYB1H5MEEKKg0IJjtLxthfuOcH2C8ZPqbPsKK1gb mVvp4/HjrjNP2CTRh+Oqa3OW158aUb4MeoA0rr7xmRU/nUgBvZEzI+Vm5CwoOcdK9hsl he/BVbbZrcVJDYSsKt/bm/rGXNWAH/zwzqES5GvFSUYqyNGoTjQCco1xOvY+td5+RVEX +NFWZVVMOJtfBM3XUcdqO3tu7La1yJ5zYaOua/uA9zgW/I4PxXPQlvgqDhnb8Ur6mxvm L3LwMpwUaGKcfzH99GBm+Xy36+exaz89rdpgNY0JHnY3vd448gr9F7JUiap/U5LkPmyH zPtg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=h8gBHZBcb2YMWItHZ5v52k75LYEpNNA/oZohojyoTqA=; fh=mK71tCayosplD7hKXdVC2WehuaAZDNTKdCAyGa1Fgwk=; b=ojiIFfyKvsRydh9VR2CoSX8XE4Jg2gzKoEXizrExMzNzJC14cL5WnO4Ee1eLQMOAm9 VhbywBR/fQfQFzAeyXpNmXaLEL5/+gdEQjmtdvV/Cy4UzKeGJL58v2dYchKO0Oe95hs8 F8OFiU/gglcx69PAhHfqvOoKKKNIQnYKht4jQKlcMp6pHCKhDMVsS37GP+gEpSjQ8lnd u4+apEN131ParjICpDNg1fYD+d8a03s1HxJmqMaF41L+6DpiyclkLZu6DDF0C3IfLdC6 h2H5prd74B2zvTzzB299xNIjvpYW9UT/NFi7ncTm9d9r/h3i7igB0kayBe4pLHtR7Vm6 m1Uw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="KgjD/AlN"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-86065-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86065-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id bz41-20020a056a02062900b005dca1e01c0asi470561pgb.880.2024.02.28.18.58.49 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 18:58:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86065-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="KgjD/AlN"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-86065-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86065-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BA6092860DD for <ouuuleilei@gmail.com>; Thu, 29 Feb 2024 02:58:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 96134381DD; Thu, 29 Feb 2024 02:58:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="KgjD/AlN" Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5436B374F7 for <linux-kernel@vger.kernel.org>; Thu, 29 Feb 2024 02:58:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709175493; cv=none; b=svC6Cm3PM/+YX9+bqUecOnMZnoivUYRDthMHYt481JOe6Wt5LcED85HW8U6xhWDum9LuO9juhRZbalw3PE7iCk4gZ6sWtDulJrdtcB33d2WuuVjrRaBNxhiBiOv0Skn4oMWoc8jjlwn14YRAbUrSB6qzuffISFX6l/yQbzR/yzw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709175493; c=relaxed/simple; bh=4TOA+r5FgQu9r7XyUG+jnEXfKZGB1J0B4LAWQX2lpPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i6/6zvs4vRX/e5gxIW+Axuq5ogz8zcI3aEmSDSioGGiodfw1gtH8sTGYLzLED6Lb4N7NctUeGyEf2yN1awhhnqc9UosW86z43smOWXC5whOKbYYDQXr0hoXd9tVf/EnR51SFKNO0Rn5OF130NvtSIav5wrZZdki2sAp6NmjNtlw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=KgjD/AlN; arc=none smtp.client-ip=209.85.210.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-6e48153c13aso202777a34.3 for <linux-kernel@vger.kernel.org>; Wed, 28 Feb 2024 18:58:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1709175491; x=1709780291; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=h8gBHZBcb2YMWItHZ5v52k75LYEpNNA/oZohojyoTqA=; b=KgjD/AlNkquVx5HzOsnwQTVby5PEbzF8CT9VtOG6+9VTjGFd1/M9KSvgEKOA9xLvbb BY/KVjfOgJj9vGOhDs1ghheKsREuquRCGywg9BMWydxuhwC64noIs18BAvNfI+ewTVlY z5rKjQEsjgaMHTB+CWbvJ3e3Tnr9onosiTpvs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709175491; x=1709780291; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=h8gBHZBcb2YMWItHZ5v52k75LYEpNNA/oZohojyoTqA=; b=rUJVvPn+Ue1C+Mawin7TN7ag+7kOu9nPMJJ+1bwJXys/s7PnDGCQMt3finHXQXOINY YQNGtMd/OPnwrIVw9Mg2gZmR28tX67lstfS0/g3eKKeDX1/L5PY3dUJwKx7rzqHo47m9 mQ85NBgSKJOW3vE7r/jGO7rQSfRb+zEm/r4gEqaTo0jRe4KD29CuPpZAwgQNb9VMk1fZ Sz7BlGinpXC+1Aphwvbbrn9+PObD5WHthU5Bk5TVdjvEEsd3hfn3/ZJg5S+jR3yD3oO8 98S6pAr0YgOhOAkhgPsuZr6fHUEbVBCGFsLFWkjk95tCKbTdPj2RzYZJzfwCnAiLCYDT TAWQ== X-Forwarded-Encrypted: i=1; AJvYcCVc5EMq1DrLXss/WhWabAJdvbretz9pWzRrTfMpZBer3dWfQ/+QgbeKq+A6uVTk9qxNdjvWJQQmO0vMn+TbLUyQ0XECLC8VGzIhQ+hD X-Gm-Message-State: AOJu0YwXQixYshK8e+zqd4UuCJ7LfCZG2GQU9Sk6cHTrcH5rxF86MxZE gRlpXJIRElSIi4XJpxIK9woh+heJyBUHlZprpZ7OjVEbUi5UVeJXo3N1bZk8mw== X-Received: by 2002:a05:6358:6f12:b0:17b:c624:b0a1 with SMTP id r18-20020a0563586f1200b0017bc624b0a1mr1060447rwn.23.1709175491359; Wed, 28 Feb 2024 18:58:11 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:f51:e79e:9056:77ea]) by smtp.gmail.com with UTF8SMTPSA id r37-20020a632065000000b005dcc075d5edsm190825pgm.60.2024.02.28.18.58.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Feb 2024 18:58:10 -0800 (PST) From: David Stevens <stevensd@chromium.org> X-Google-Original-From: David Stevens <stevensd@google.com> To: Sean Christopherson <seanjc@google.com>, Paolo Bonzini <pbonzini@redhat.com> Cc: Yu Zhang <yu.c.zhang@linux.intel.com>, Isaku Yamahata <isaku.yamahata@gmail.com>, Zhi Wang <zhi.wang.linux@gmail.com>, Maxim Levitsky <mlevitsk@redhat.com>, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v11 1/8] KVM: Assert that a page's refcount is elevated when marking accessed/dirty Date: Thu, 29 Feb 2024 11:57:52 +0900 Message-ID: <20240229025759.1187910-2-stevensd@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog In-Reply-To: <20240229025759.1187910-1-stevensd@google.com> References: <20240229025759.1187910-1-stevensd@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792200440361033191 X-GMAIL-MSGID: 1792200440361033191 |
Series |
KVM: allow mapping non-refcounted pages
|
|
Commit Message
David Stevens
Feb. 29, 2024, 2:57 a.m. UTC
From: Sean Christopherson <seanjc@google.com> Assert that a page's refcount is elevated, i.e. that _something_ holds a reference to the page, when KVM marks a page as accessed and/or dirty. KVM typically doesn't hold a reference to pages that are mapped into the guest, e.g. to allow page migration, compaction, swap, etc., and instead relies on mmu_notifiers to react to changes in the primary MMU. Incorrect handling of mmu_notifier events (or similar mechanisms) can result in KVM keeping a mapping beyond the lifetime of the backing page, i.e. can (and often does) result in use-after-free. Yelling if KVM marks a freed page as accessed/dirty doesn't prevent badness as KVM usually only does A/D updates when unmapping memory from the guest, i.e. the assertion fires well after an underlying bug has occurred, but yelling does help detect, triage, and debug use-after-free bugs. Note, the assertion must use page_count(), NOT page_ref_count()! For hugepages, the returned struct page may be a tailpage and thus not have its own refcount. Signed-off-by: Sean Christopherson <seanjc@google.com> --- virt/kvm/kvm_main.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 10bfc88a69f7..c5e4bf7c48f9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3204,6 +3204,19 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_unmap); static bool kvm_is_ad_tracked_page(struct page *page) { + /* + * Assert that KVM isn't attempting to mark a freed page as Accessed or + * Dirty, i.e. that KVM's MMU doesn't have a use-after-free bug. KVM + * (typically) doesn't pin pages that are mapped in KVM's MMU, and + * instead relies on mmu_notifiers to know when a mapping needs to be + * zapped/invalidated. Unmapping from KVM's MMU must happen _before_ + * KVM returns from its mmu_notifier, i.e. the page should have an + * elevated refcount at this point even though KVM doesn't hold a + * reference of its own. + */ + if (WARN_ON_ONCE(!page_count(page))) + return false; + /* * Per page-flags.h, pages tagged PG_reserved "should in general not be * touched (e.g. set dirty) except by its owner".