Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2291406rdb; Thu, 21 Sep 2023 14:17:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF33UuwbH1TpV3dSm0vbMKXDC4sLLQHXKfr08aUtaK+iP0NW2F7dtU4Gue4C0b2pSfZg8b9 X-Received: by 2002:a05:6358:5284:b0:143:1ef5:5860 with SMTP id g4-20020a056358528400b001431ef55860mr7457498rwa.27.1695331070358; Thu, 21 Sep 2023 14:17:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695331070; cv=none; d=google.com; s=arc-20160816; b=dVKp3fmRMM1YsPm4UP5UcA/6hf+x6vceU2HG/MXrQq5q6/iyLLIEZmeIiOLD5kJw7W WHlZr35NA4avkBOb5+WSMIDzY95y9Qc32x8etLWKIbPrundNHsvRgP80g/oyz9+5A8FW ul3AsV+6tTJWgg9ozHRjjoVx8+2dJRq1h0XlH0NFuknITu0ZepCW6rn7/yAT1vnNcCtT HA9rANzB19n/DxOD0DZ8FhFoOpbxxf6xr+kdudEVF7k1u9ARlXLsF4190kYIIeIFQaEE Tfj5RwQ8XsUUqEcrKBqWZ6Hs6eXqW8eTj77IvioKoZYtACpat5F2im3PwFllWg68I+2l Xbgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qc994C3oy15L7h5supFHMH5UN4Y7uQUATVWHw7sJFgs=; fh=ANS6iRpdj3ntKSmwSzT4LEHUH9ZAudCj9zvP8/EeThQ=; b=IB2LeEy5Z4/7PrJsm93Tp6pHoIWLe8EjQRlH00ERA//HqMrccYfdKOjZ/oTipdlp9Z N15sxVxmZqD77v3/fhx0tUaRjb579sY5zFimsUp0+Pi+MiTRZqSMK2GP7KcXaZyO8sw/ b+DmTZlteiutbHVQJfS0XTQsw/TFGvmfAWiOQWj+sAfnPdDByGLU3M2hQl2M7TGPEPOL 6/OtIfgn8WqqlLWDLOBqNjV99LEejYkU0+7YMUyR8QkDdR21fsd704e9My6IJM1m+Iv6 +srKy6yDNxUYHJbUsCeqBNRwpRapgsX1SVm5W8k317bP996OfYC0kAQUiYM8kR0ulQl4 fMZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Wn1oLNbX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id cn12-20020a056a020a8c00b00564a5b8937asi2174310pgb.341.2023.09.21.14.17.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 14:17:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Wn1oLNbX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 22ED482BB19E; Thu, 21 Sep 2023 13:41:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231954AbjIUUlr (ORCPT + 99 others); Thu, 21 Sep 2023 16:41:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230381AbjIUUl3 (ORCPT ); Thu, 21 Sep 2023 16:41:29 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44045A4B8; Thu, 21 Sep 2023 13:15:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695327317; x=1726863317; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LrS431o0cshlDoRy46oclShFvKK7oFAV7dBwoRHaY3I=; b=Wn1oLNbX95JqgAKw0V6G00XnJSaj6qw5sx/flPjuV+pbwHeluKqKPHbv li4Z/6TEhi+G35SW560WmpcPj9LdeCPy8okmx2ZQ41YvdCUlu45P0WQ03 3Qsch7dsXUVEiy8JcC8+dZ42gCtMZRE6CreRI3snpsdt9QEscRrOIzmsm yo6eAzCZ6fjJSIaXd3iUgvNpP7CjB7k+va6QcXuEt45acwhx47xiOkCtE 17hxKVbGaNKNuztBwpFf0RCtNW9vI/pvSbd8nHqFBHkoxC50hbHHB+yl2 goEfW3JeO7PJ+0vs0Z2Ra0yGis3dBmT6IWWn8l/wF7N8lZrHzvDSIAF0f Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10840"; a="383401642" X-IronPort-AV: E=Sophos;i="6.03,166,1694761200"; d="scan'208";a="383401642" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Sep 2023 13:14:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10840"; a="696897807" X-IronPort-AV: E=Sophos;i="6.03,166,1694761200"; d="scan'208";a="696897807" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Sep 2023 13:14:48 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Michael Roth , Paolo Bonzini , Sean Christopherson , erdemaktas@google.com, Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, linux-coco@lists.linux.dev, Chao Peng , Ackerley Tng , Vishal Annapurve , Yuan Yao , Jarkko Sakkinen , Xu Yilun , Quentin Perret , wei.w.wang@intel.com, Fuad Tabba Subject: [RFC PATCH v2 4/6] KVM: gmem: Add ioctl to inject memory failure on guest memfd Date: Thu, 21 Sep 2023 13:14:37 -0700 Message-Id: <363c4ac28af93aa96a52f897d2fe5c7ec013f746.1695327124.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 21 Sep 2023 13:41:51 -0700 (PDT) From: Isaku Yamahata To test error_remove_page() method of KVM gmem, add a new ioctl to inject memory failure based on offset of guest memfd. Signed-off-by: Isaku Yamahata --- include/uapi/linux/kvm.h | 6 ++++ virt/kvm/guest_mem.c | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 65fc983af840..4160614bcc0f 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2323,4 +2323,10 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_GUEST_MEMORY_FAILURE _IOWR(KVMIO, 0xd5, struct kvm_guest_memory_failure) + +struct kvm_guest_memory_failure { + __u64 offset; +}; + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index 01fb4ca861d0..bc9dae50004b 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -291,10 +291,78 @@ static struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot) return file; } +static int kvm_gmem_inject_failure(struct file *file, + struct kvm_guest_memory_failure *mf) +{ + struct inode *inode = file_inode(file); + struct address_space *mapping = inode->i_mapping; + pgoff_t index = mf->offset >> PAGE_SHIFT; + struct folio *folio; + unsigned long pfn; + int err = 0; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + filemap_invalidate_lock_shared(mapping); + + /* Don't allocate page. */ + folio = filemap_get_folio(mapping, index); + if (!folio) { + err = -ENOENT; + goto out; + } + if (IS_ERR(folio)) { + err = PTR_ERR(folio); + goto out; + } + + pfn = folio_pfn(folio) + (index - folio_index(folio)); + folio_put(folio); + +out: + filemap_invalidate_unlock_shared(mapping); + if (err) + return err; + + /* + * Race with pfn: memory_failure() and unpoison_memory() gain invalidate + * lock as the error recovery logic tries to remove pages from + * mapping. + */ + if (!pfn_valid(pfn)) + return -ENXIO; + return memory_failure(pfn, MF_SW_SIMULATED); +} + +static long kvm_gmem_ioctl(struct file *file, unsigned int ioctl, + unsigned long arg) +{ + void __user *argp = (void __user *)arg; + int r = -EINVAL; + + switch (ioctl) { + case KVM_GUEST_MEMORY_FAILURE: { + struct kvm_guest_memory_failure mf; + + r = -EFAULT; + if (copy_from_user(&mf, argp, sizeof(mf))) + break; + r = kvm_gmem_inject_failure(file, &mf); + break; + } + default: + break; + } + + return r; +} + static const struct file_operations kvm_gmem_fops = { .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate, + .unlocked_ioctl = kvm_gmem_ioctl, }; static int kvm_gmem_migrate_folio(struct address_space *mapping, -- 2.25.1