From patchwork Tue Sep 10 23:43:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13799489 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 94EDBEE01F1 for ; Tue, 10 Sep 2024 23:45:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 541C88D00DD; Tue, 10 Sep 2024 19:45:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 496148D00DB; Tue, 10 Sep 2024 19:45:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E70E8D00DC; Tue, 10 Sep 2024 19:45:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EF7BA8D0002 for ; Tue, 10 Sep 2024 19:45:05 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E12AC160E79 for ; Tue, 10 Sep 2024 23:45:02 +0000 (UTC) X-FDA: 82550461644.29.56822C9 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf19.hostedemail.com (Postfix) with ESMTP id 1400D1A0007 for ; Tue, 10 Sep 2024 23:45:00 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=l3d5oY1y; spf=pass (imf19.hostedemail.com: domain of 3-9ngZgsKCHAOQYSfZSmhbUUccUZS.QcaZWbil-aaYjOQY.cfU@flex--ackerleytng.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3-9ngZgsKCHAOQYSfZSmhbUUccUZS.QcaZWbil-aaYjOQY.cfU@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726011797; 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=j5QWCSGDqbjsS0n7qJVo2UbLIELWUXKqatSc9NHlgvU=; b=030vOcNBkGL+SgVrRtE++mPVJeOdWPUeRxL5c0+cQoWJaDhNvTlWZkV28EwnXTDBMBFhm6 pu3Y/eahoASSf8y5i8MsT2r0QNiP8L7lSRfcMzk13HPohrFk+Dfli51rqrpg5fva9qhp4i yLnnjhtGVLRgIZy8MK3SOvkaX7uxCI4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726011797; a=rsa-sha256; cv=none; b=1CG5GFG1bICvEyUZ0GByNOe7OsEKAVNhTgPrWlq8ef7rmslGT8pmr0bdi0BdZYkCkqBKuC TdP52TkHFzklcxZOf+CN4gnGXapt7YYDWiNCIBcwA3RShuIh+re0VcJKhFLE8YyrUiFEco WNNqgdEBr9W9+IKsYegn/ygIZI3kBqs= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=l3d5oY1y; spf=pass (imf19.hostedemail.com: domain of 3-9ngZgsKCHAOQYSfZSmhbUUccUZS.QcaZWbil-aaYjOQY.cfU@flex--ackerleytng.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3-9ngZgsKCHAOQYSfZSmhbUUccUZS.QcaZWbil-aaYjOQY.cfU@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-70ac9630e3aso1171527a12.1 for ; Tue, 10 Sep 2024 16:45:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726011900; x=1726616700; 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=j5QWCSGDqbjsS0n7qJVo2UbLIELWUXKqatSc9NHlgvU=; b=l3d5oY1yBoKxSLAo6LN7+qEvnjYtoyPY9dCwodYMvKkXO9/hykh+nWERO7fuQwf3GI /Ns9k5FU5mJ8WNEV9F8OSz9DuuIdqpplJNxOoHxTJbtmrZ1fUekwJ8D5imgCJJguifNq 8oMO0j1MQ+XRzD8hS1AYpCeRyucKkB40x0AwTKgtwA4QkpZCl0fue++0F3SVbPmnA6le 2s2LIQnvDTzzC8IS2bDFYewM11jMA5CkCVT9JvWnZVnUuWsA7b2wAtALqJpTB1IYi4wu qafk8v4KNExjTGMOcoF4TtWZd4scJSNcguh9TZIrH7eVb7ewSpcPwR35CH8WV5sRUY5i 4OSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726011900; x=1726616700; 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=j5QWCSGDqbjsS0n7qJVo2UbLIELWUXKqatSc9NHlgvU=; b=IhZdi/kaCFNpFt1lFuZRL3LEko+hJLC9u6bSoqcMB7+oo2I4i63u+zSWsYIBDE9QD9 1Vclgq7zE2AFSVITZUhWsW+KQdlTHJFg76c3zRPLq97PA1QTJqgUH2j4KeP6Vrzcw3Wg pxeVsFwKSiZz7S7K/yePTPG3goT/6tIKOph95JApqVghPEkFdeqatNKHaL0blPTxJC6r RCH0PPcLjoauZ4hxnpdFwYiQ/GMUYwJyN8+7zaHhOVIeipId2O1aJCug3v0APN7DCsS0 o9AZOlq29TnMy0+sW91u1jGEM7fpUH99ceTZ14cWpiHoECnyH2a0Rsqws1Y0e18HiZgG tEwg== X-Forwarded-Encrypted: i=1; AJvYcCWjX/Eri727VuX3y8iSai5nNJSgfFf1FtP8D5J2o17IqKiJflWExypx4gfhqgsufcuUwA0ekmqyeQ==@kvack.org X-Gm-Message-State: AOJu0Yx6fAefFqKUlYXc+Ej3NxCLiBpxRrfpXIUKkhj1Jr/QowUKY7Ao yhRBqWJyJd2JWSBiZU8Jzk/NZZHsRRmsKyrTJtWRp3ovWCZeFfeDP2C9nrm/vFF5/YKIVekDOdN lfWJ/9oo/af5YpWns0OaLtw== X-Google-Smtp-Source: AGHT+IHo9nrZMACTW1GYMiSHsHPpyJhtvpckq5UoyzEbjp2HiFKw7SxN6vYrJsaFr5HrF/PjOMK6efHCzGq+Tv7awg== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a63:3dc6:0:b0:6e9:8a61:b8aa with SMTP id 41be03b00d2f7-7db0bb80899mr2016a12.0.1726011899568; Tue, 10 Sep 2024 16:44:59 -0700 (PDT) Date: Tue, 10 Sep 2024 23:43:47 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: <6f6b891d693ea0733f4b2737858af914bd70a8b6.1726009989.git.ackerleytng@google.com> Subject: [RFC PATCH 16/39] KVM: guest_memfd: Add page alignment check for hugetlb guest_memfd 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1400D1A0007 X-Stat-Signature: wnzzemf5fmy7u49ka6eqnn81b1mu46nc X-HE-Tag: 1726011900-777258 X-HE-Meta: U2FsdGVkX18e2VL8LKwFAjCBRFmhsat8/Jvmmf4hZFiqnH8rYtz1q9ujePLUprH/fHB8vt/edLIl1X2NNAjelz0oTRGzxVD7kj5wg0mF7loiB4vUEI8nMBZNRCAZMOJKzFUAIsHkmt1Zdhfh327MDPCzxMTia+ayV48S1P7iaXLd/65Q/X+ljCjeJji//bIle03u/mRbcuwabq2fx/hVBr3lpysg1vy23Fn3uvqjiPlbJLJdhBFqeERDC8Jz6dsIdeTXWkDEmswRP/ohA42LP1jkNpsazSfKay4sbJZNo40rDc6Nr24bGbiSV/iJQ/SPep2fjGP74oyEbnt122kcA2NZ8butCwcbGnWcA5BsWodpDfHjN8o46P8VTTdoVsMAY2JCNZHzC21hVV40lavFaKKgSrNwuPvN51lh90Gp0IWKzrHGLv+oDOJrXyo/KlHcLkpIfSJ2BSiekzFiPbU+AnE73zRW5BLJZFJngTJiFiIWv4HtgO+fn+DIuKS3TiUH4RfeVcqBsPtOyUKDPY5qupbnggzZRWt53pp741DH5pvDSzFNwBtn6WPDLJpAy3Nc6cgSnZgQsAegVfzQ3xuTdXaJoiIuByBRaoVDpR+KYeU5qE/xbVIVqo9aUX0oLaUXYmbG5x3PjJx/8ecDAFtVdtFRdlv3xrHFAf0s1bqLMuqpJnh9bo9QQdrgz9Jro+hoD+kWFOusLo84nuUbgA34HmcuCVBlE6dmbeEpeN7vGhgudl0Y+uZawwT1tUQL5zKH4v1sqtLQBrCB2Rvjix6l9AF15cTFZJn6Spj7gdjWxyKCEGcICW0pJdmxXhEmc8dsPke2+RW4gQtyNFAQbpFLWBkn0SGKRshriDOh3Y+qP10dBlZ6EHjmNLXVJ44XXohpt8rjIvvXhDmvgkV5xpVbfid+qgANK8y2uDZM1GpRpt5lEowcrUiUnvz+QEOm6MJ4yaRaEjyyncaiNf7XATg cBZq8KX+ TuQD/BLQ5sQ+en7B0bQpgVVmkANqxfExPnDx1P/8m2J/Zlos0HCocXZN7Hooo8TQajy2E8GeHulhpI5ZlyVMC2e0GLyZBCfp8Xkjt0VcSRGrJsVmA3qN0yujF4ppnXP4tvzrrOHt+A11U0Lan0Uelj49omeUwl2nDVX2CkZYyR+Ldd88mTA32kaF9WC5NyD1yAaIv/NCzYOH/FOK6cIK0FMi7qehYeauvgz5Z1Z1it/+NtDl2d/GZ5Sj7imSimI7F3KQrydUFeOWCq/6ak+8+FCrxdJlwiuMCD7kVKAX8naH/OIzz07Bqq5qu30nUmGoq1+glmb9F/wiWp+kz5NKJ4jnV6eXGqEPvYjXLDVKk3wPLAR0fiHz2QzF4n/JEjE9IO3dGLPrgdwS0MrzUnzbfUluTJFr8t0wng7Y75h2dO9WYI7CxbIMM2PNu8YUqdeV0Y6r2XsnX6rMFAbWSuUguGJscCL70+zscYfeuvGlru2o1iHfu+f1m7Me8xTfKgyzFuOoD0eBIxIOg4WsQ3dTXGWKeu7RwKGP0/OpcqJA098S6r7UpavPxRGysQUsDk6fQGv7SXAy/ELGOUvbmz0rIfbybkpW5yR4MpGX9dx78VVMJt6CFUhYw8uq7+jLqISpgi+O2tKZd/JUxSjoLiSKIQ/5MRgVt8wJwy/FfSK5n3Tf1iFn7Ffsj5g4sJeu5Ff4EAGOYPm/dNNxLepw= 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: When a hugetlb guest_memfd is requested, the requested size should be aligned to the size of the hugetlb page requested. Signed-off-by: Ackerley Tng --- virt/kvm/guest_memfd.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 2e6f12e2bac8..eacbfdb950d1 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -909,6 +909,13 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) return err; } +static inline bool kvm_gmem_hugetlb_page_aligned(u32 flags, u64 value) +{ + int page_size_log = (flags >> KVM_GUEST_MEMFD_HUGE_SHIFT) & KVM_GUEST_MEMFD_HUGE_MASK; + u64 page_size = 1ULL << page_size_log; + return IS_ALIGNED(value, page_size); +} + #define KVM_GUEST_MEMFD_ALL_FLAGS KVM_GUEST_MEMFD_HUGETLB int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) @@ -921,12 +928,18 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) if (flags & ~(KVM_GUEST_MEMFD_ALL_FLAGS | (KVM_GUEST_MEMFD_HUGE_MASK << KVM_GUEST_MEMFD_HUGE_SHIFT))) return -EINVAL; + + if (!kvm_gmem_hugetlb_page_aligned(flags, size)) + return -EINVAL; } else { if (flags & ~KVM_GUEST_MEMFD_ALL_FLAGS) return -EINVAL; + + if (!PAGE_ALIGNED(size)) + return -EINVAL; } - if (size <= 0 || !PAGE_ALIGNED(size)) + if (size <= 0) return -EINVAL; return __kvm_gmem_create(kvm, size, flags);