From patchwork Tue Sep 10 16:30:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13798885 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54D31ECE58A for ; Tue, 10 Sep 2024 16:31:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA5DF8D0090; Tue, 10 Sep 2024 12:31:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C55EA8D0002; Tue, 10 Sep 2024 12:31:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD1058D0090; Tue, 10 Sep 2024 12:31:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 8A9D08D0002 for ; Tue, 10 Sep 2024 12:31:32 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 31507806AA for ; Tue, 10 Sep 2024 16:31:32 +0000 (UTC) X-FDA: 82549369224.21.EBD9DF0 Received: from smtp-fw-52003.amazon.com (smtp-fw-52003.amazon.com [52.119.213.152]) by imf04.hostedemail.com (Postfix) with ESMTP id 434CA40013 for ; Tue, 10 Sep 2024 16:31:30 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=L6P1N3zg; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf04.hostedemail.com: domain of "prvs=976277991=roypat@amazon.co.uk" designates 52.119.213.152 as permitted sender) smtp.mailfrom="prvs=976277991=roypat@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725985886; a=rsa-sha256; cv=none; b=6olfXbMuvPtBfs8amz4hpNFHOQLpA/P6OJat7U4T7go6C4wAWeMNZV82MYey4ePUKppKBf XH+9UR+W/B5JdglbpjW6tBB2OUuOAQiSyt4wRSSDR6xF9LNZZimku93y9vG7+Ry6iWEXFt 2zhTJ5fyfsvOeYLX5e4Dwgzy1188dBM= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=L6P1N3zg; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf04.hostedemail.com: domain of "prvs=976277991=roypat@amazon.co.uk" designates 52.119.213.152 as permitted sender) smtp.mailfrom="prvs=976277991=roypat@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725985886; h=from:from:sender: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:dkim-signature; bh=O39OHNSY2jvqQ+6nIWGz3rExj3YlDzBo0CAgdRJUjCQ=; b=Vt06wukDXYNq5iVpOU6f/UlpucBOHRxK1Fni7WATD4JUmfCS7qRfWJWqSNoGM/TVsRCkuV 3A043ukJhDcaWxn0kOyYQfrLZ0NepUmdR43PQnzaxIqt0SgirF22+gSCT2kEA7Y5kfphqS qqc0SS+NK6k02840jH51L07CIo+94rQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1725985891; x=1757521891; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O39OHNSY2jvqQ+6nIWGz3rExj3YlDzBo0CAgdRJUjCQ=; b=L6P1N3zgMWEd/s2Vu6hq9O4Ld34W5/HIf2n0JjEnLv5o1x/hrnr7awt5 1MKTVsaRVUE2XEYbbdpM2+vqCXbI4Jd2u7fmI9qn/S8gAnrNRjmmjZJMY bq1nMej7iESpy2ksQGwl1Nb4t1/4IGIzlk4rudmdyYc335eTX/z595tEX A=; X-IronPort-AV: E=Sophos;i="6.10,217,1719878400"; d="scan'208";a="24649840" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52003.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Sep 2024 16:31:26 +0000 Received: from EX19MTAUEC002.ant.amazon.com [10.0.0.204:15768] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.46.235:2525] with esmtp (Farcaster) id a66d20fe-467b-4f84-9176-15708e5e7cff; Tue, 10 Sep 2024 16:31:25 +0000 (UTC) X-Farcaster-Flow-ID: a66d20fe-467b-4f84-9176-15708e5e7cff Received: from EX19D008UEA004.ant.amazon.com (10.252.134.191) by EX19MTAUEC002.ant.amazon.com (10.252.135.253) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 10 Sep 2024 16:31:20 +0000 Received: from EX19MTAUWB001.ant.amazon.com (10.250.64.248) by EX19D008UEA004.ant.amazon.com (10.252.134.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 10 Sep 2024 16:31:19 +0000 Received: from ua2d7e1a6107c5b.home (172.19.88.180) by mail-relay.amazon.com (10.250.64.254) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Tue, 10 Sep 2024 16:31:15 +0000 From: Patrick Roy To: , , , , , , , , , , , , , , , , , , , , CC: Patrick Roy , , , , , Subject: [RFC PATCH v2 06/10] kvm: gmem: add tracepoints for gmem share/unshare Date: Tue, 10 Sep 2024 17:30:32 +0100 Message-ID: <20240910163038.1298452-7-roypat@amazon.co.uk> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240910163038.1298452-1-roypat@amazon.co.uk> References: <20240910163038.1298452-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: g95665rp77kycagcxty73n4yxy4i9fzm X-Rspamd-Queue-Id: 434CA40013 X-Rspamd-Server: rspam02 X-HE-Tag: 1725985890-287179 X-HE-Meta: U2FsdGVkX1+JNl0JifgcSWD1XIuCCOPWwSLC8xNt8KghKGeukjGolYC79DaQ4UTUvxSrXDB9vMd8bAmv57hVICFbJsLQu4BVvmQ1bORcFcq3gEtmombGfda49E8+DFib+AJxLdiQTd8Yc8zTjMW0N/3//8/acDuhcx13I/uzAl4oupTpjmUhbdoxf2pgxYr65nzbIF71uMN3P7JIz784tc7kJSIr6CEXgaNykN+ZNrf6NI1B52eFUoUnEnJlbImZcopUtvCxpSNCRFO7c0xEFw7Z9Y5OLY8gbeCMA0zes0r0QhPIfG8XCLg5hKSaCV3Yhtv1KsEx14lp9aiT/ebGc7kod1jlYBV03AOO9fgcQ0JM0UH/RrJARtirkXK6+MtJVnd8wVGCpxUCxIRUqkU8vxeq6LUBjWTxdV3yFHwaV14v92wveh+XivZ+BXOcgoucQFKF3lMdJGqyU7yQWpl/1evCoflExSRO4vp8/U1a6pH028JuBvBRGGRpflVIwd3++gjq0cVM7dIq4GOk7ajU7KiaHqnqs7gDmfvfEfsxKBAOUj8/E7YRUTbFh9mtT9xCwn2fYztjNwqurKul2HKJBbaLbTMi5R85/R4J31Rr7O430zGjT6oHWK48VUTInVk1aCe6LwgaUR5zK7nqRaqMLtvixTqDc6g1LlKFCm5/+ioD5vgkwT8naEOR2zXitfug+7t9rgYcamskyGvgxCUhI4o1+5poPNBl8wndIB+ECJPI0R8a9RhVZRAY/y6y20TZUVpXFwtsbjPvgxcQyQsiagoU30llu21zznlnFU6iCQSDyVu0Xmf09N1Ahr0IZFf0ekIdYixJmwvcCpvkXl3Q6PfY9jUxwW52SMMdKlZE9txL2mDqiEXMvNceLQZoO0fmsJM4gKJmCUDFI6FN8vVErSocmiJEXUCaUqvYLkvRQqgSq8tdP+LEFdbmH370i+rHDBYiF7G/+SJN++bpPpt 23uuNg26 N9MuLpMlWCAOaoib0Hp6+PTHRujYW/oQyPYHqGmya5xo1GrcYNdeMYAaO/RK5Y/PUJqM9BJDz4H4uzW/4crgCj/iaq04rboy48hxC74tgRIr7D447ltqCFRgubD33mm7ISYJKyo6eNNPo2b+tZxP7+kh0TGXaVoABC/fYHkmq6LjBBT2uMiog+UjfJa9C8iG9D0kqJASBqE3jjMMlxObMlo9O3c59WZWiF9Qtd6npdfbwuXYshTa7DkcckOTig8yMNBYRMsRPyhvlcuwRU2nBnQco0el3w/XpPb7YphLU1zZJozbRl6MS5m0my2drxqMW4tnE2iyYUY6Qw4wmEKKkLkcHyYbgsXJhr9wjFHWvurs4FamVTOdJhhMRGLW6/lOUEt6ObIKs5QqVQ5Y= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add tracepoints for calls to kvm_gmem_get_folio that cause the returned folio to be considered "shared" (e.g. accessible by host KVM), and tracepoint for when KVM is done accessing a gmem pfn (kvm_gmem_put_shared_pfn). The above operations can cause folios to be insert/removed into/from the direct map. We want to be able to make sure that only those gmem folios that we expect KVM to access are ever reinserted into the direct map, and that all folios that are temporarily reinserted are also removed again at a later point. Processing ftrace output is one way to verify this. Signed-off-by: Patrick Roy --- include/trace/events/kvm.h | 43 ++++++++++++++++++++++++++++++++++++++ virt/kvm/guest_memfd.c | 7 ++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h index 74e40d5d4af42..4a40fd4c22f91 100644 --- a/include/trace/events/kvm.h +++ b/include/trace/events/kvm.h @@ -489,6 +489,49 @@ TRACE_EVENT(kvm_test_age_hva, TP_printk("mmu notifier test age hva: %#016lx", __entry->hva) ); +#ifdef CONFIG_KVM_PRIVATE_MEM +TRACE_EVENT(kvm_gmem_share, + TP_PROTO(struct folio *folio, pgoff_t index), + TP_ARGS(folio, index), + + TP_STRUCT__entry( + __field(unsigned int, sharing_count) + __field(kvm_pfn_t, pfn) + __field(pgoff_t, index) + __field(unsigned long, npages) + ), + + TP_fast_assign( + __entry->sharing_count = refcount_read(folio_get_private(folio)); + __entry->pfn = folio_pfn(folio); + __entry->index = index; + __entry->npages = folio_nr_pages(folio); + ), + + TP_printk("pfn=0x%llx index=%lu pages=%lu (refcount now %d)", + __entry->pfn, __entry->index, __entry->npages, __entry->sharing_count - 1) +); + +TRACE_EVENT(kvm_gmem_unshare, + TP_PROTO(kvm_pfn_t pfn), + TP_ARGS(pfn), + + TP_STRUCT__entry( + __field(unsigned int, sharing_count) + __field(kvm_pfn_t, pfn) + ), + + TP_fast_assign( + __entry->sharing_count = refcount_read(folio_get_private(pfn_folio(pfn))); + __entry->pfn = pfn; + ), + + TP_printk("pfn=0x%llx (refcount now %d)", + __entry->pfn, __entry->sharing_count - 1) +) + +#endif + #endif /* _TRACE_KVM_MAIN_H */ /* This part must be outside protection */ diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 6772253497e4d..742eba36d2371 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -7,6 +7,7 @@ #include #include "kvm_mm.h" +#include "trace/events/kvm.h" struct kvm_gmem { struct kvm *kvm; @@ -204,8 +205,10 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, unsi if (r) goto out_err; - if (share) + if (share) { refcount_inc(folio_get_private(folio)); + trace_kvm_gmem_share(folio, index); + } out: /* @@ -759,6 +762,8 @@ int kvm_gmem_put_shared_pfn(kvm_pfn_t pfn) { if (refcount_read(sharing_count) == 1) r = kvm_gmem_folio_set_private(folio); + trace_kvm_gmem_unshare(pfn); + return r; } EXPORT_SYMBOL_GPL(kvm_gmem_put_shared_pfn);