From patchwork Tue Sep 10 23:44:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13799510 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 DA24EEE01F2 for ; Tue, 10 Sep 2024 23:46:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1FEAF8D00F0; Tue, 10 Sep 2024 19:45:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1878B8D00E2; Tue, 10 Sep 2024 19:45:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EF2C48D00F0; Tue, 10 Sep 2024 19:45:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CAEEC8D00E2 for ; Tue, 10 Sep 2024 19:45:40 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8FD491607E9 for ; Tue, 10 Sep 2024 23:45:40 +0000 (UTC) X-FDA: 82550463240.10.30C576D Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf22.hostedemail.com (Postfix) with ESMTP id C477DC0009 for ; Tue, 10 Sep 2024 23:45:38 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KKb6Fajk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3IdrgZgsKCJY02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3IdrgZgsKCJY02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726011854; a=rsa-sha256; cv=none; b=PS3BbuLwVIPpcU9kiaQBXQV9gpkcR7yMtqamNbvpXcUxlVkZQraIlLIDHXvPCLiXxf/vjN 7LiQwRy6MNZIigp3HFT+bpv5V5qWANMQr4G7WLZrtswUQvMUUf9t5poBSanDpMoBbzgbOk VkjdKKN2O6mgA0uA2J6wTJMWI/ihW4g= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KKb6Fajk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3IdrgZgsKCJY02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3IdrgZgsKCJY02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726011854; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mkb+NAnOFL/DwPaS2OVTOWWsNRjgIx4s2eg9q8AJrXM=; b=eh71leQeuUXC45dmooycz9tu9IR+9i5n5GDBhPWHohb23uw2p6Aw+nC4/41q58QSOfAQbb PRMo2421FBg3kv4o+1m2VcCroQFTHDlCFY3Gf3mbIR35T9FoQEQwwCQOu63+VP0daDQs4W v4n/EVfeNFQXHYMibT9NAlJbvGjNZqo= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2058456b864so23030565ad.3 for ; Tue, 10 Sep 2024 16:45:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726011937; x=1726616737; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mkb+NAnOFL/DwPaS2OVTOWWsNRjgIx4s2eg9q8AJrXM=; b=KKb6FajkFeOq6G4dk53GjwOcrp+y/VsOtemrKjibDdvUDuGbuZwwCLlLvuwSMwsehw 6XcTkAeHz2BnEyid0LeyBh9fYkSKZZa8Juwwgk6l1VUoWtqYts+WWAicdCWo5GWGSq9U jswyUO1M01JM42hBCkoccjiGIEkUqYsS7lJKNJroyGoCXW7jhj+5pToSTgQdFCfaJ+M+ 0DsC4SftrGcljDpgpxb7eLmu8oQnkU4+d0eTjCi+/bQMgbroVF2wnWIMoNZx7ScuAEpd cgMKZArU9twMH0FRPiNEoMabB70Puo6Q5pho7i4LB7RqqyAHQaS5WtK6GOv2tVPLj4pf BlDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726011937; x=1726616737; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mkb+NAnOFL/DwPaS2OVTOWWsNRjgIx4s2eg9q8AJrXM=; b=JF9JDOvTIzPLCSKWMVVZlDY37C5E7I5UKY6K12/mnI+xWVtH7NheDLjTCpcz818k45 qc0Ut8IZAx32IEZedjQ/EfyG5kI0gc7TRR/2RfvhHPuGCRwxC6F0PjQsC0BmMGqh+rOg ohoe9UyeCgKPIMVZzzIEDUNbOntkxKg6gYrFmXZ7Bejk9P3+w2cku/9vkr/6THVIbHTD w0WE/CcTtZ9ERZpJ03zhj5HPIhBPm2i+XsT/d/OtXQ8P/0rPgZyXzDbhN63e25JlOTaq DCdAYUL72/HzDFe5xEWbaE3zKFhn2EneNLF0l2Phl07ZCohI/uIlAP11kt3VS/T3r1FT y+5Q== X-Forwarded-Encrypted: i=1; AJvYcCVgILAaKLXTzzqdOs3T8pGWIkXVabUge7Lh0a0qx5sFwWQGkfmUk8/+6lVDR38/3T7CNq90MuQPtg==@kvack.org X-Gm-Message-State: AOJu0YwVmpGz9cfgXqRdZkPiUfAGAb0JDRRYonCoFmzpMrQcM2iT7OE5 bfHMNh/aPCTo/wMieQYFdwZqt1GqpVvWm1ePzg3Em0GiWU0hPasoTAzld/2VeJ+gobf0DbUOU3H yKN/Kz0I7xarvOZheJ0sZBA== X-Google-Smtp-Source: AGHT+IEIVAikHARJb/9AtMZHbBPnKppzatT8jO6P6FcNUKI8bwXbpm7jyhYnZiKRc4hR4u8fz8EVE4930Msvwij+9Q== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a17:903:1ca:b0:1fb:8620:e113 with SMTP id d9443c01a7336-20752183657mr936435ad.3.1726011937387; Tue, 10 Sep 2024 16:45:37 -0700 (PDT) Date: Tue, 10 Sep 2024 23:44:08 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: Subject: [RFC PATCH 37/39] KVM: selftests: Add helper to perform madvise by memslots From: Ackerley Tng To: tabba@google.com, quic_eberman@quicinc.com, roypat@amazon.co.uk, jgg@nvidia.com, peterx@redhat.com, david@redhat.com, rientjes@google.com, fvdl@google.com, jthoughton@google.com, seanjc@google.com, pbonzini@redhat.com, zhiquan1.li@intel.com, fan.du@intel.com, jun.miao@intel.com, isaku.yamahata@intel.com, muchun.song@linux.dev, mike.kravetz@oracle.com Cc: erdemaktas@google.com, vannapurve@google.com, ackerleytng@google.com, qperret@google.com, jhubbard@nvidia.com, willy@infradead.org, shuah@kernel.org, brauner@kernel.org, bfoster@redhat.com, kent.overstreet@linux.dev, pvorel@suse.cz, rppt@kernel.org, richard.weiyang@gmail.com, anup@brainfault.org, haibo1.xu@intel.com, ajones@ventanamicro.com, vkuznets@redhat.com, maciej.wieczor-retman@intel.com, pgonda@google.com, oliver.upton@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-fsdevel@kvack.org X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C477DC0009 X-Stat-Signature: 84so3myc7gcegntmb8oxdyu8pc76cenj X-Rspam-User: X-HE-Tag: 1726011938-699073 X-HE-Meta: U2FsdGVkX1+8+VTjCsj90E1zoSqaSND567nO6kp3qXfSZO3XgP2jSgwx/vorVCGLMBJeZK3DteEQNl/9MbM726NmT7fWIz6wVP2aU7KEbXMitWf+6hv0YI1BA7M5iK4Vosr6wkVJ7H1z7E2ZuANzZ9DR9ht97puWci3Ap3eqcMuklDjYv2yArF7JagXfO452ulOASgTmgWpOg+0PzJWCPoYeBOlxtwyxJAd/iEaJggu3r6VUxYcc0b9o96Z9TBcW1v93kKT2UJ7Ou+Gv0c+/Xkay8KqNqE5uDnc4APT1Ok9Z9ASNPyhAGgfQjjIXkbmJy+DXpClwg3VmNAhtZO6WWPxhjpW4WkPpnPvUumA+nl7h5A5Ay9DeKTMK/OV9QmKUVQi/RNW+w2g9FS0FioxTvC8d5f9+8OtnTTnFlORSh6FjKQ7/92ggkGybA8g8wEZVsgOjHToCWk/urV4ehQgaIqNI0cltkmn4j4LATc+VDm/c/PaQrLZi8WcTXBHVWu21qdbK9GZlhGj2X7pxA6ROGLZrIJMgIFG5kmWgwLmkekQYgZ/xhGY2mwRcyKGvqYTrKWuftZif1EerAHKfxTArBvUK4rB2xTkXkj8zY6C3YqDjclvgnqsSCv7RzeN7TgoJsckSE/1nrQLya6k+5Ep5jtGDvlfdyB1u8+M0QzfWL3OuvLQbGnD34qPbNJpXQPFYOV1oQKq8H+fTL0Gj8VhRuLTt3YKLdIz7YIIZLrA7zwwcYiGseXhdArEZ/zqwPTREbbFk5yEtlDdyux0UV7rgWPU6evocxWRzv5hZ+4n0TdzwjJr2Wetre0ndfmuGa9IJbD8aKx/konE1hSkt+TAqvUP20GZVjy5PkKMU43G6nox69UDY65xQHHGTxXA1sAvMgwDzlv379lZuHRNhULMW2IdsMQ6mOAs+xApzyoD5Yh3fxdH0m+Jv7NewtqKZbzT0MRbb6K0xN1mVThX7y1v fCnimKYE cgZD6lTpl7wxwRCD9eE90DS2QZbfpCe4m+3Ax+BlHJA3qtBzYb19c7sjveB/8znvI0otKiCuKrMngjd296jj48yaxTRH7HTZ3ba5VS7YqOU7mgoD/qjIvlQ4bYAFe37fJP2dr9ALHap20ocSAH1kz0mJp7zmr2b5W9ryqALWLy5O4pvlR8zJvNzwknuImFGshDYTy8Z7xoFqZvWS+pdowpLBZpnYVVu04Po0pMVji0jkmwKLULRtYqZDjRX2muqBhHfjZxIQGhhx8WYrLBeqOiXuO01WPfXGjsS+q6vOLOFNGGu+gtpbVRxqFHNW23Z5bOMEw3mHXYgLA39iVUxak4hdCo9zsOL3RdD0RXVOnU/pUqo7Y6VD714VY/1lLKi/C1uVhVSSV15a+sCT70VCHktaK0FK4AOBytRp+iqvk3xkwjb7/NggOJMF1/Xmn86H8rxGRnTZv+KubwRnGSUX6f9QH14FjT/V8URm5visMEe2uW2TJKpwA+h/IxWIbsPjJ+Z4j5KqL1cTmD9axrSfoBb8A9W3q8X+U/uwbn8C5oCCHxwHa4bGg1UJlyPg5okb+7vEfwytrCBXpNi7bvBzjwf9LpU1rzkThpptSadIDGQk8/cocwVItyU51/9I/LRDGbFFvGrINs1b+NEadB8wzmkZPRA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.013762, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: A contiguous GPA range may not be contiguous in HVA. This helper performs madvise, given a GPA range, by madvising in blocks according to memslot configuration. Signed-off-by: Ackerley Tng --- tools/include/linux/kernel.h | 4 +-- .../testing/selftests/kvm/include/kvm_util.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 30 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/tools/include/linux/kernel.h b/tools/include/linux/kernel.h index 07cfad817d53..5454cd3272ed 100644 --- a/tools/include/linux/kernel.h +++ b/tools/include/linux/kernel.h @@ -54,8 +54,8 @@ _min1 < _min2 ? _min1 : _min2; }) #endif -#define max_t(type, x, y) max((type)x, (type)y) -#define min_t(type, x, y) min((type)x, (type)y) +#define max_t(type, x, y) max((type)(x), (type)(y)) +#define min_t(type, x, y) min((type)(x), (type)(y)) #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) #ifndef BUG_ON diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 1576e7e4aefe..58b516c23574 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -433,6 +433,8 @@ static inline void vm_mem_set_shared(struct kvm_vm *vm, uint64_t gpa, void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t gpa, uint64_t size, bool punch_hole); +void vm_guest_mem_madvise(struct kvm_vm *vm, vm_paddr_t gpa_start, uint64_t size, + int advice); static inline void vm_guest_mem_punch_hole(struct kvm_vm *vm, uint64_t gpa, uint64_t size) { diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 9bdd03a5da90..21ea6616124c 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1416,6 +1416,36 @@ void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t size, } } +void vm_guest_mem_madvise(struct kvm_vm *vm, vm_paddr_t gpa_start, uint64_t size, + int advice) +{ + size_t madvise_len; + vm_paddr_t gpa_end; + vm_paddr_t gpa; + + gpa_end = gpa_start + size; + for (gpa = gpa_start; gpa < gpa_end; gpa += madvise_len) { + struct userspace_mem_region *region; + void *hva_start; + uint64_t memslot_end; + int ret; + + region = userspace_mem_region_find(vm, gpa, gpa); + TEST_ASSERT(region, "Memory region not found for GPA 0x%lx", gpa); + + hva_start = addr_gpa2hva(vm, gpa); + memslot_end = region->region.userspace_addr + + region->region.memory_size; + madvise_len = min_t(size_t, memslot_end - (uint64_t)hva_start, + gpa_end - gpa); + + ret = madvise(hva_start, madvise_len, advice); + TEST_ASSERT(!ret, "madvise(addr=%p, len=%lx, advice=%x) failed\n", + hva_start, madvise_len, advice); + } +} + + /* Returns the size of a vCPU's kvm_run structure. */ static int vcpu_mmap_sz(void) {