From patchwork Wed Oct 31 13:26:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Orr X-Patchwork-Id: 10662675 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AE98514DE for ; Wed, 31 Oct 2018 13:26:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9EBC9284BD for ; Wed, 31 Oct 2018 13:26:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9288628AA4; Wed, 31 Oct 2018 13:26:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 20938284BD for ; Wed, 31 Oct 2018 13:26:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BADFE6B0010; Wed, 31 Oct 2018 09:26:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B5C946B026B; Wed, 31 Oct 2018 09:26:44 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A71E26B026C; Wed, 31 Oct 2018 09:26:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-yw1-f70.google.com (mail-yw1-f70.google.com [209.85.161.70]) by kanga.kvack.org (Postfix) with ESMTP id 7609C6B0010 for ; Wed, 31 Oct 2018 09:26:44 -0400 (EDT) Received: by mail-yw1-f70.google.com with SMTP id j69-v6so11380653ywb.19 for ; Wed, 31 Oct 2018 06:26:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=sag5MWmUMVeJlNMpxbgpOCQmQo4Ok3FbocF8rLJTZCg=; b=qan9gnaQ4S0w7OL8dYqsrrzctB8UnucjnyOLdBgVa8HGAlP6n+XtrZclEaGWERkPTF fs7jzmbMXP/TQ6Sn+wm5EMzjcQ2LiH0UcpfwXEdLgZSEi0obnrS+ehJ3koZgYEkGu8jW mPE8aG9tIVxciZkUb0VQ2smz9VQZffV5Q7a9YzQtCC+YCucxD89bn7hNim5/BXjwXT5c isc6jU8lmgTvAR6NeyRjRzsrn9NWTgVkIPmpZs8rUp+BO+N5bcGJuwR6vozJsp06nUCd DMbtenCV2j70RjxcEQK/7Fp11pdi8lZ6ylqm9uJ/Ks2LSVVJ27QHhuZicyJMLmbKVhn2 mZQw== X-Gm-Message-State: AGRZ1gJNKt5FMOKFN+SLJz75AIo6zQP15e3Tn2PXYsQfWl0ilX9gM1Qv l0UafLOd7RstQJbs+X4/Y1FX03ZbHugpYvMhZDueATiRcukinDCRAaIHM8qYW0ICZZ89NFgnIqK 9KV3fkaawkGYPxr6yDRXDXmHw/MBmzJKo47P+sjCuWuljNx5ATGEbg/MqIFVbPhL07JoT0E/DFS 4jZnMQJTpSczWurVaGqZux3ng24GyKCN/PyCXdzxb0c1LjFVPBjzCeFs3LsSsfg2C2Nkisgie2r u6Ha8QR5AF3UgVuB2B43M/In2+X/6AWaS9UoYqHgy7ouzioKjgp+sK0Kw1hKWcGv+BSSOAN9hw2 hUxjkXxIFZtIysQP1u2aMpQbCbWrzg+PKAgfiA2D12p6JmlGHrxzAYp61ku9zg/wyDYIQ7Cm3Io P X-Received: by 2002:a5b:3c2:: with SMTP id t2-v6mr3035259ybp.328.1540992404078; Wed, 31 Oct 2018 06:26:44 -0700 (PDT) X-Received: by 2002:a5b:3c2:: with SMTP id t2-v6mr3035154ybp.328.1540992402491; Wed, 31 Oct 2018 06:26:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540992402; cv=none; d=google.com; s=arc-20160816; b=0veEt0rfnwCfo4b8+DIH2V0ctJexU9dchOupoF7Eyv6UVUUzYpRtUaqzLNjO/VrHed 1Fa7FYnaqDGNcf4KhfIid0bRmKKmGwhahFepHiSLi/MUR5rczcFKVwOGsaKrElQmbCBo q6Dgoh5d64VZbI2hFwTGLpU548Gw8nIlYLve83RXiCioaq5b2aWfsLx66icF+/tJ02jv E5RY9NIhhmEp7Nk7UZ8BAe3R/fisoSL45QMrSSSCRigTE2P/Isfp1nJFKzFKL7OFXZNQ FSCgId3vVrpC+Cnpf/dAlvCWi+c09sT3TISTOQehiCeCMUhzzNWjnjwuokUITOD6xSwv 0vVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:dkim-signature; bh=sag5MWmUMVeJlNMpxbgpOCQmQo4Ok3FbocF8rLJTZCg=; b=Pm3DwC+/IaQuJhZiy2ONqEVp9Jo4Hg+AWz3VRNZeqfiq65JGUN47xkQQEAutZhXUoG wth0w+FirrodgmfLv8mMBk9cu2h5v/7NWzP+ZsDTdMRFIDaks4TZGn++4sEX26Sb2bYq zkPOIgWFZINPU9aeVPPozMFdqRR/KA+HvXbtqH8ocZM8QQQ6M/53oz5eTjjHV7WSihKG C0A/T4WQW3e3l1BPfptiSuhAabpeCYxNTbRqAx6oOmujcTSF/deGEoLrbq/qJ6Fg8Cwh 6oTG3ZJvOLHt6RMweUfsvVhD2Zj67LqtvHMfUsmsrwhCdqHG48kicYaPIy5js9+O1+aC cAeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=klEZe1+Y; spf=pass (google.com: domain of 3kq3zwwckcdkhvmxjmmbjjbgz.xjhgdips-hhfqvxf.jmb@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3kq3ZWwcKCDkhVmXjmmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--marcorr.bounces.google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f73.google.com (mail-sor-f73.google.com. [209.85.220.73]) by mx.google.com with SMTPS id m14-v6sor12427754ybp.84.2018.10.31.06.26.42 for (Google Transport Security); Wed, 31 Oct 2018 06:26:42 -0700 (PDT) Received-SPF: pass (google.com: domain of 3kq3zwwckcdkhvmxjmmbjjbgz.xjhgdips-hhfqvxf.jmb@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) client-ip=209.85.220.73; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=klEZe1+Y; spf=pass (google.com: domain of 3kq3zwwckcdkhvmxjmmbjjbgz.xjhgdips-hhfqvxf.jmb@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3kq3ZWwcKCDkhVmXjmmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--marcorr.bounces.google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=sag5MWmUMVeJlNMpxbgpOCQmQo4Ok3FbocF8rLJTZCg=; b=klEZe1+YLFreakUC1NNWYeyOnlaW0ntB3zzqwzbnKOaNr+HjvwFIoPO+mZH+KAwDlZ j+lseImIqnz9dnhMd0IopzDS5+E5z7GC8b1ifKqMksISoeug5GrN82adGqE2ttXDcJHr 9h9IpYHmtTy6cIvoqClSy2yErvilzoRCGC936R0e+woy9EklHZAlZ2zXeyvO449TV7DP FnECVaZ8rWoA5SGl0kzb/l2Kjgtf4oPjArv7bo8AZ5qFCZymZpQPtMzWm+vq867jTIHJ WYP6G9Dua4VPzkl0oURZB8xSwkhWwSCYXNmVeYkpAakTx5Z/wiJ2oezakszw8tHPOlsU Rxjw== X-Google-Smtp-Source: AJdET5c7s7Wrujes1yN28z27Ablc0GtM2SWbbocQ15S6tHRBU9kdq6AxSSMsZhc+s2/n1eNSXZgn2HWdP18m X-Received: by 2002:a25:cb56:: with SMTP id b83-v6mr1485845ybg.72.1540992402222; Wed, 31 Oct 2018 06:26:42 -0700 (PDT) Date: Wed, 31 Oct 2018 06:26:31 -0700 In-Reply-To: <20181031132634.50440-1-marcorr@google.com> Message-Id: <20181031132634.50440-2-marcorr@google.com> Mime-Version: 1.0 References: <20181031132634.50440-1-marcorr@google.com> X-Mailer: git-send-email 2.19.1.568.g152ad8e336-goog Subject: [kvm PATCH v5 1/4] kvm: x86: Use task structs fpu field for user From: Marc Orr To: kvm@vger.kernel.org, jmattson@google.com, rientjes@google.com, konrad.wilk@oracle.com, linux-mm@kvack.org, akpm@linux-foundation.org, pbonzini@redhat.com, rkrcmar@redhat.com, willy@infradead.org, sean.j.christopherson@intel.com, dave.hansen@linux.intel.com, kernellwp@gmail.com Cc: Marc Orr 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: X-Virus-Scanned: ClamAV using ClamSMTP Previously, x86's instantiation of 'struct kvm_vcpu_arch' added an fpu field to save/restore fpu-related architectural state, which will differ from kvm's fpu state. However, this is redundant to the 'struct fpu' field, called fpu, embedded in the task struct, via the thread field. Thus, this patch removes the user_fpu field from the kvm_vcpu_arch struct and replaces it with the task struct's fpu field. This change is significant because the fpu struct is actually quite large. For example, on the system used to develop this patch, this change reduces the size of the vcpu_vmx struct from 23680 bytes down to 19520 bytes, when building the kernel with kvmconfig. This reduction in the size of the vcpu_vmx struct moves us closer to being able to allocate the struct at order 2, rather than order 3. Suggested-by: Dave Hansen Signed-off-by: Marc Orr --- arch/x86/include/asm/kvm_host.h | 7 +++---- arch/x86/kvm/x86.c | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 55e51ff7e421..ebb1d7a755d4 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -601,16 +601,15 @@ struct kvm_vcpu_arch { /* * QEMU userspace and the guest each have their own FPU state. - * In vcpu_run, we switch between the user and guest FPU contexts. - * While running a VCPU, the VCPU thread will have the guest FPU - * context. + * In vcpu_run, we switch between the user, maintained in the + * task_struct struct, and guest FPU contexts. While running a VCPU, + * the VCPU thread will have the guest FPU context. * * Note that while the PKRU state lives inside the fpu registers, * it is switched out separately at VMENTER and VMEXIT time. The * "guest_fpu" state here contains the guest FPU context, with the * host PRKU bits. */ - struct fpu user_fpu; struct fpu guest_fpu; u64 xcr0; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bdcb5babfb68..ff77514f7367 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7999,7 +7999,7 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu) static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) { preempt_disable(); - copy_fpregs_to_fpstate(&vcpu->arch.user_fpu); + copy_fpregs_to_fpstate(¤t->thread.fpu); /* PKRU is separately restored in kvm_x86_ops->run. */ __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state, ~XFEATURE_MASK_PKRU); @@ -8012,7 +8012,7 @@ static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) { preempt_disable(); copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu); - copy_kernel_to_fpregs(&vcpu->arch.user_fpu.state); + copy_kernel_to_fpregs(¤t->thread.fpu.state); preempt_enable(); ++vcpu->stat.fpu_reload; trace_kvm_fpu(0); From patchwork Wed Oct 31 13:26:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Orr X-Patchwork-Id: 10662681 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 227023CF1 for ; Wed, 31 Oct 2018 13:26:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 10E41284BD for ; Wed, 31 Oct 2018 13:26:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0419628AA4; Wed, 31 Oct 2018 13:26:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A81428935 for ; Wed, 31 Oct 2018 13:26:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D2D06B026B; Wed, 31 Oct 2018 09:26:46 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4A71C6B026D; Wed, 31 Oct 2018 09:26:46 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 321D46B026E; Wed, 31 Oct 2018 09:26:46 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by kanga.kvack.org (Postfix) with ESMTP id 029AE6B026B for ; Wed, 31 Oct 2018 09:26:46 -0400 (EDT) Received: by mail-qt1-f198.google.com with SMTP id u28-v6so16980512qtu.3 for ; Wed, 31 Oct 2018 06:26:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=uuP0VVlUnqXp2dMHDoSAgZr4dF/qtgdpqgl8mRbBnkk=; b=dElOSLlYFcrh4wqeVlvZ9uZC3qcgmhi1XMy1B6XtSGLl6m9cV2oEWj8jMFZoO89UeE R3mwVUMMOeU+Xmzqa2z4VsFMtRei701cGAcisx2dt1yCrxToK5E0eerX5Lqvdgx+lemM WfN6GmpCtQ85Q7QlgVO0WdwlZv3yZJ92OsVUxpOSKFnZMmKmjT0RKud/3dylRPKkMYKL 5mD+hMgVUNxcp2XasHndjSRafhj56yBDeOAl6jZ2EJ3bBJJkiH1xJ1CDG6HpJtifoTFD 1JlW8U8LzT3NwEtOqCPcbIt2zBnLao9cww5Fy4ZLDsugeHZMwG0/YDTbH23umcOi4y8e FsDQ== X-Gm-Message-State: AGRZ1gL8v0T61HHCBWmldxDu6SOn6X2MfPCaylo9XSWfSErRNFAW9x0n 8OpEQxwCkCNtX4C7PvR20jHUSsUxxuToX9nEP7dGqHxsNW3Wm/HonCQdcfiCs/imNucvzr8UZBR lLpRYmt80j36zgDqqYRY2pV6HnHTj2PImZe3nCfa3GBCVwbgfSF2ONcZcoxrLziGZ2UXL47OMjA scwXysKDVsBkj71LVbF4FfvFkX0VIT92K1T3dY3HnepiYnT/uqDfUBOJQGtxIOmnOs0JQvSeueh yKhPLyyrUcbOfEE8uEHbP218o11KLNu/+QPAi1AWhuFvh1T0PSMSHltlQ1piMc8yx41e9AVZCTt HhiLOvy7KbjU2fuptvTLrGGA1uhplG8iS8GhEtCM1yX4D4DoIy8VvASdIcclNIVTkNvhVPXcoUs z X-Received: by 2002:a0c:d1eb:: with SMTP id k40mr2595488qvh.150.1540992405670; Wed, 31 Oct 2018 06:26:45 -0700 (PDT) X-Received: by 2002:a0c:d1eb:: with SMTP id k40mr2595434qvh.150.1540992404587; Wed, 31 Oct 2018 06:26:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540992404; cv=none; d=google.com; s=arc-20160816; b=piymYcE+Y2gk0y/gLBe0jI1G9yqBlkO6CpIXGo8cxIXo6Rzq/4PUHAKm0H6p71whcu ywftryZXkVQ18xybp3Qpg2g8i3bGLoDgWT8xKk3PxBY59xBlCdXMMeuUAFGKaQEW3FAh 8G/LhAT8NNYWlCSmibmebbKVydVTWEP5rqvhlugIrAdiemB3NG3CCX0aXiwErJAr8nk8 K43YljUaJLumFqFX3RoKyRdWRZ1Yg9nuqAOlSA6rEEHruxeS+k2hszyXF4I1HQnS+Wpa sRjb4GHQbzHFKHYDNW+ZZjtG35de1qzPOa1N5EXoxeK9iuL6g9hu/koMrKQ8m/JkHH76 GuSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:dkim-signature; bh=uuP0VVlUnqXp2dMHDoSAgZr4dF/qtgdpqgl8mRbBnkk=; b=z7r7PPjWeuetbbf5S9LYwuqvEeJseiuHRNyyGzqDJA1Bp49/eJ5FyW21gBMR2RBJwy 0pcYDJXdh/HxfD7Yyi8Cjj68Uvf+Zqe7AwTTbwHqDp8Uc0os4yxZ1jYHJ/KoSeMrju7o tstdehYx6WeNKRV5FShrPFdwW5VWTukGQTmLncyNF3VgxX4G9ccXOcjFTdsCK6JoAGSI lupGCCh2fxm/RzeyWlY0WlokynMCguUX4uU4FOxpkmujXUHZmjqNcI0Dm15zD2vkWoHn qu0MZqzj1lwiNZwiEU/lazia3zinrpjXSKw2qrjfC89ziXU3I6u4ChPzulLpQ5dp8DY2 wlgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=F+NB1Rxc; spf=pass (google.com: domain of 3lk3zwwckcdsjxozloodlldib.zljifkru-jjhsxzh.lod@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3lK3ZWwcKCDsjXoZloodlldib.Zljifkru-jjhsXZh.lod@flex--marcorr.bounces.google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f73.google.com (mail-sor-f73.google.com. [209.85.220.73]) by mx.google.com with SMTPS id b127sor3950749qkc.48.2018.10.31.06.26.44 for (Google Transport Security); Wed, 31 Oct 2018 06:26:44 -0700 (PDT) Received-SPF: pass (google.com: domain of 3lk3zwwckcdsjxozloodlldib.zljifkru-jjhsxzh.lod@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) client-ip=209.85.220.73; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=F+NB1Rxc; spf=pass (google.com: domain of 3lk3zwwckcdsjxozloodlldib.zljifkru-jjhsxzh.lod@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3lK3ZWwcKCDsjXoZloodlldib.Zljifkru-jjhsXZh.lod@flex--marcorr.bounces.google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=uuP0VVlUnqXp2dMHDoSAgZr4dF/qtgdpqgl8mRbBnkk=; b=F+NB1RxcQr2bTutSgJwdifRc4zUjLOl3TxTiJHutBqkRq9urjO1g/Ejfoj2m02VRRi mXa/kOSHDf/1S4KttEeQI3hW+yIRaWyYJPtEj/dd2aj7aAHrB26s2t7pkMRg48EnSci0 yqIfk90njcXNVQY1uiKJ68GfDVT5xiWF7pkMfgPTBRoaVTKiWJAhUyl1WlU2wL4toIIn JFjJ/iE4dnPv3Bx0Mt9D7bq70PkLz+bIg3jOY7BsGE/V5dK0Mv+LNlqAJSNn9tKRCUBF HaD5EDUQolCbdGjcBOeO52uO4hdoud3797xftSYdWIOQeDEFhmro/vIcS3OEMiXdi2eP 8F5A== X-Google-Smtp-Source: AJdET5ctl87/dqXf6bdBRw29fQWrPDgKICEXJLDQF68l2QLKEa9Behs/YHIqguLELUiMZXKxyWi7agOifVkj X-Received: by 2002:a37:9a90:: with SMTP id c138mr2150620qke.36.1540992404300; Wed, 31 Oct 2018 06:26:44 -0700 (PDT) Date: Wed, 31 Oct 2018 06:26:32 -0700 In-Reply-To: <20181031132634.50440-1-marcorr@google.com> Message-Id: <20181031132634.50440-3-marcorr@google.com> Mime-Version: 1.0 References: <20181031132634.50440-1-marcorr@google.com> X-Mailer: git-send-email 2.19.1.568.g152ad8e336-goog Subject: [kvm PATCH v5 2/4] kvm: x86: Dynamically allocate guest_fpu From: Marc Orr To: kvm@vger.kernel.org, jmattson@google.com, rientjes@google.com, konrad.wilk@oracle.com, linux-mm@kvack.org, akpm@linux-foundation.org, pbonzini@redhat.com, rkrcmar@redhat.com, willy@infradead.org, sean.j.christopherson@intel.com, dave.hansen@linux.intel.com, kernellwp@gmail.com Cc: Marc Orr 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: X-Virus-Scanned: ClamAV using ClamSMTP Previously, the guest_fpu field was embedded in the kvm_vcpu_arch struct. Unfortunately, the field is quite large, (e.g., 4352 bytes on my current setup). This bloats the kvm_vcpu_arch struct for x86 into an order 3 memory allocation, which can become a problem on overcommitted machines. Thus, this patch moves the fpu state outside of the kvm_vcpu_arch struct. With this patch applied, the kvm_vcpu_arch struct is reduced to 15168 bytes for vmx on my setup when building the kernel with kvmconfig. Suggested-by: Dave Hansen Signed-off-by: Marc Orr --- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/svm.c | 10 ++++++++ arch/x86/kvm/vmx.c | 10 ++++++++ arch/x86/kvm/x86.c | 45 +++++++++++++++++++++++---------- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index ebb1d7a755d4..c8a2a263f91f 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -610,7 +610,7 @@ struct kvm_vcpu_arch { * "guest_fpu" state here contains the guest FPU context, with the * host PRKU bits. */ - struct fpu guest_fpu; + struct fpu *guest_fpu; u64 xcr0; u64 guest_supported_xcr0; @@ -1194,6 +1194,7 @@ struct kvm_arch_async_pf { }; extern struct kvm_x86_ops *kvm_x86_ops; +extern struct kmem_cache *x86_fpu_cache; #define __KVM_HAVE_ARCH_VM_ALLOC static inline struct kvm *kvm_arch_alloc_vm(void) diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index f416f5c7f2ae..ac0c52ca22c6 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -2121,6 +2121,13 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) goto out; } + svm->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, GFP_KERNEL); + if (!svm->vcpu.arch.guest_fpu) { + printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n"); + err = -ENOMEM; + goto free_partial_svm; + } + err = kvm_vcpu_init(&svm->vcpu, kvm, id); if (err) goto free_svm; @@ -2180,6 +2187,8 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) uninit: kvm_vcpu_uninit(&svm->vcpu); free_svm: + kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu); +free_partial_svm: kmem_cache_free(kvm_vcpu_cache, svm); out: return ERR_PTR(err); @@ -2194,6 +2203,7 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu) __free_page(virt_to_page(svm->nested.hsave)); __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); kvm_vcpu_uninit(vcpu); + kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu); kmem_cache_free(kvm_vcpu_cache, svm); /* * The vmcb page can be recycled, causing a false negative in diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index abeeb45d1c33..4078cf15a4b0 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -11476,6 +11476,7 @@ static void vmx_free_vcpu(struct kvm_vcpu *vcpu) free_loaded_vmcs(vmx->loaded_vmcs); kfree(vmx->guest_msrs); kvm_vcpu_uninit(vcpu); + kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu); kmem_cache_free(kvm_vcpu_cache, vmx); } @@ -11489,6 +11490,13 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) if (!vmx) return ERR_PTR(-ENOMEM); + vmx->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, GFP_KERNEL); + if (!vmx->vcpu.arch.guest_fpu) { + printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n"); + err = -ENOMEM; + goto free_partial_vcpu; + } + vmx->vpid = allocate_vpid(); err = kvm_vcpu_init(&vmx->vcpu, kvm, id); @@ -11576,6 +11584,8 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) kvm_vcpu_uninit(&vmx->vcpu); free_vcpu: free_vpid(vmx->vpid); + kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu); +free_partial_vcpu: kmem_cache_free(kvm_vcpu_cache, vmx); return ERR_PTR(err); } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ff77514f7367..420516f0749a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -213,6 +213,9 @@ struct kvm_stats_debugfs_item debugfs_entries[] = { u64 __read_mostly host_xcr0; +struct kmem_cache *x86_fpu_cache; +EXPORT_SYMBOL_GPL(x86_fpu_cache); + static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu) @@ -3635,7 +3638,7 @@ static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu) { - struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave; + struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; u64 xstate_bv = xsave->header.xfeatures; u64 valid; @@ -3677,7 +3680,7 @@ static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu) static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) { - struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave; + struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET); u64 valid; @@ -3725,7 +3728,7 @@ static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, fill_xsave((u8 *) guest_xsave->region, vcpu); } else { memcpy(guest_xsave->region, - &vcpu->arch.guest_fpu.state.fxsave, + &vcpu->arch.guest_fpu->state.fxsave, sizeof(struct fxregs_state)); *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] = XFEATURE_MASK_FPSSE; @@ -3755,7 +3758,7 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, if (xstate_bv & ~XFEATURE_MASK_FPSSE || mxcsr & ~mxcsr_feature_mask) return -EINVAL; - memcpy(&vcpu->arch.guest_fpu.state.fxsave, + memcpy(&vcpu->arch.guest_fpu->state.fxsave, guest_xsave->region, sizeof(struct fxregs_state)); } return 0; @@ -6819,10 +6822,23 @@ int kvm_arch_init(void *opaque) } r = -ENOMEM; + x86_fpu_cache = kmem_cache_create_usercopy( + "x86_fpu", + sizeof(struct fpu), + __alignof__(struct fpu), + SLAB_ACCOUNT, + offsetof(struct fpu, state), + sizeof_field(struct fpu, state), + NULL); + if (!x86_fpu_cache) { + printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n"); + goto out; + } + shared_msrs = alloc_percpu(struct kvm_shared_msrs); if (!shared_msrs) { printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n"); - goto out; + goto out_free_x86_fpu_cache; } r = kvm_mmu_module_init(); @@ -6855,6 +6871,8 @@ int kvm_arch_init(void *opaque) out_free_percpu: free_percpu(shared_msrs); +out_free_x86_fpu_cache: + kmem_cache_destroy(x86_fpu_cache); out: return r; } @@ -6878,6 +6896,7 @@ void kvm_arch_exit(void) kvm_x86_ops = NULL; kvm_mmu_module_exit(); free_percpu(shared_msrs); + kmem_cache_destroy(x86_fpu_cache); } int kvm_vcpu_halt(struct kvm_vcpu *vcpu) @@ -8001,7 +8020,7 @@ static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) preempt_disable(); copy_fpregs_to_fpstate(¤t->thread.fpu); /* PKRU is separately restored in kvm_x86_ops->run. */ - __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state, + __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state, ~XFEATURE_MASK_PKRU); preempt_enable(); trace_kvm_fpu(1); @@ -8011,7 +8030,7 @@ static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) { preempt_disable(); - copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu); + copy_fpregs_to_fpstate(vcpu->arch.guest_fpu); copy_kernel_to_fpregs(¤t->thread.fpu.state); preempt_enable(); ++vcpu->stat.fpu_reload; @@ -8506,7 +8525,7 @@ int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) vcpu_load(vcpu); - fxsave = &vcpu->arch.guest_fpu.state.fxsave; + fxsave = &vcpu->arch.guest_fpu->state.fxsave; memcpy(fpu->fpr, fxsave->st_space, 128); fpu->fcw = fxsave->cwd; fpu->fsw = fxsave->swd; @@ -8526,7 +8545,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) vcpu_load(vcpu); - fxsave = &vcpu->arch.guest_fpu.state.fxsave; + fxsave = &vcpu->arch.guest_fpu->state.fxsave; memcpy(fxsave->st_space, fpu->fpr, 128); fxsave->cwd = fpu->fcw; @@ -8582,9 +8601,9 @@ static int sync_regs(struct kvm_vcpu *vcpu) static void fx_init(struct kvm_vcpu *vcpu) { - fpstate_init(&vcpu->arch.guest_fpu.state); + fpstate_init(&vcpu->arch.guest_fpu->state); if (boot_cpu_has(X86_FEATURE_XSAVES)) - vcpu->arch.guest_fpu.state.xsave.header.xcomp_bv = + vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED; /* @@ -8708,11 +8727,11 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) */ if (init_event) kvm_put_guest_fpu(vcpu); - mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu.state.xsave, + mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, XFEATURE_MASK_BNDREGS); if (mpx_state_buffer) memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state)); - mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu.state.xsave, + mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, XFEATURE_MASK_BNDCSR); if (mpx_state_buffer) memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr)); From patchwork Wed Oct 31 13:26:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Orr X-Patchwork-Id: 10662685 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CA77714DE for ; Wed, 31 Oct 2018 13:26:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB81F284BD for ; Wed, 31 Oct 2018 13:26:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE71A28AA4; Wed, 31 Oct 2018 13:26:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 29E9F284BD for ; Wed, 31 Oct 2018 13:26:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 592BF6B026E; Wed, 31 Oct 2018 09:26:48 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4F4C76B026F; Wed, 31 Oct 2018 09:26:48 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34BC76B0271; Wed, 31 Oct 2018 09:26:48 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) by kanga.kvack.org (Postfix) with ESMTP id 00C366B026E for ; Wed, 31 Oct 2018 09:26:48 -0400 (EDT) Received: by mail-oi1-f200.google.com with SMTP id j192-v6so7601207oih.11 for ; Wed, 31 Oct 2018 06:26:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=+kKh85pxhdnY1kL+7UzqrV5xVyad1ppPxeuCWtpIWyw=; b=bDJ7zIgkI3MpCuqnMNMH/AoKDz3VDoHQTLS7PlroxA/cUCO08tjWeXTr4GXERYX2ug 4EATNC1YorPxciW9FH/3O3NXBTB20+sZN7defrbWg8zGZuwHQ0GrITfztUAEJ5CqENgm 1c0yxYleJfL8T8/H0J4DVUhirS3PcaaiEYL/mem7QBymUC+lJogmPmY74FaXVxTFuA4E uUvc40WAlyBLWO2f1Gf5rPqQnrZz7PlYcH8ejY6/1OvgRwzAXQjEALWxHr8FBcTDLe5b yJ60UokTbzI80zEaUfkX7TAwlN6Is3Rbqdg9u2HlpmrauhJNB+JY7zn9gQnjcKmkf4nt Bojg== X-Gm-Message-State: AGRZ1gLOe3b2mpO0adFYSGAOOTYGPvMy3DCO99m5lKj8jQP3u8eH11l6 5f3hhDeUcgh7ClrkHnRtsq6vxOrYX5j/GXnj3pj0WZpbfD4wWeZylu+/eqA9xPTR3hzQowWU3Nx cKLnKly2rHbFIJ8++3LA53CA/AiBje544Rpbjdly5zN3Eu91pTrTmmUHN8CZb74YyjjQNhu4ABA zcl5cqLpc6dxEOtSrsLQ/+AfMmfSm7kUFazDyiJ7oRAEr3jFjceVvz/HkjcxrOAEIaT8Pv38w46 od8UYvi2jZmBLr9WkkER6dlduVUTk4gjyorS1rITxl+VnjoQKRkwyDRjgXFkutakgP3Wf2wjTme xAJf44jnVFqUYMT6cVKHKgeUpmUlHxR+glk3ACRWb0cSdvUMpPyZkLtS2xMy2MsCr3PvYpH1MHN A X-Received: by 2002:a9d:60cf:: with SMTP id b15mr1801557otk.144.1540992407687; Wed, 31 Oct 2018 06:26:47 -0700 (PDT) X-Received: by 2002:a9d:60cf:: with SMTP id b15mr1801527otk.144.1540992406755; Wed, 31 Oct 2018 06:26:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540992406; cv=none; d=google.com; s=arc-20160816; b=Lc/4y+4JRC1U9b3x1jgu9akTE/ISa2q1tE5uaoghR5FK5JCxS6gn7752zTZLcOQv7J Hrx7flunxvtCpFP1krjabgNY3ERfPo2p5nEcKLiyfBVK6QWtkjOx7hNYwgU+UVsQ5TJV 6rqo16HAn0yfqRX7MOX/baVEZPND7+tqs7NEgcAv6BBi8DUk2cbqFP3TgvIcgwTcn6AN Yf7X272NSXBVmJ9yiC5x94aky0wQeAPRafkdnfx3zPWUy7MDnMccx5z8hylQP3gwSWsH Dv3TkgVUJrNf53D5sT64IhAAOqJbL60OkelSHxPEZtgIxNWHhTnyNT1m1Jh2QYvyNYqJ Tt4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:dkim-signature; bh=+kKh85pxhdnY1kL+7UzqrV5xVyad1ppPxeuCWtpIWyw=; b=ypluvxSAnIbMeVFygUXXCol8jrEMDnJ6khXVh9kux5y0dF8cd3geOOCVlNkjzZiVTo A6V6d4va7pP7EuroSb6SICd9cuDYC7wWivWS3AhYXdutkB5bHHAIBtrax4j70NpEKNM8 vYGzV3hvxo3AQVVG1Cu+3cxxwqXUqYT9Mv+gu91bbfJzKajJhtq9Prf0w/q1eBwCyjN9 bpyk4BJekcCFi34PNqbKnGjH2H+u0oMTkjojNmrFQlKQR3X4Z48M4g2wZSLkVw4G0Fc+ NE9EDMFv9T/n1BBOC5XkpiYXmKfcMNO9kn61BFcyKrHgy2YTC7Un0E4bmxtzibNviJcg 2bZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ct2XtX9b; spf=pass (google.com: domain of 3lq3zwwckcd0lzqbnqqfnnfkd.bnlkhmtw-lljuzbj.nqf@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3lq3ZWwcKCD0lZqbnqqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--marcorr.bounces.google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f73.google.com (mail-sor-f73.google.com. [209.85.220.73]) by mx.google.com with SMTPS id e14sor2651563otk.75.2018.10.31.06.26.46 for (Google Transport Security); Wed, 31 Oct 2018 06:26:46 -0700 (PDT) Received-SPF: pass (google.com: domain of 3lq3zwwckcd0lzqbnqqfnnfkd.bnlkhmtw-lljuzbj.nqf@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) client-ip=209.85.220.73; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ct2XtX9b; spf=pass (google.com: domain of 3lq3zwwckcd0lzqbnqqfnnfkd.bnlkhmtw-lljuzbj.nqf@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3lq3ZWwcKCD0lZqbnqqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--marcorr.bounces.google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=+kKh85pxhdnY1kL+7UzqrV5xVyad1ppPxeuCWtpIWyw=; b=Ct2XtX9b23GZ26vl6VP5NA3NZg7RWq519mYx7oJ8CrLrgjowUDaiZmDRTdQDGls9mT adpzHqPePiiIeDFZMbj92pvz6N2BDauDdNyROzkGus8ImDbwPxtAbVvqr/ycqpMvQJHv S3XKoeOaUs74PlB+MOu/CD58m/hbdCNGs1SmDNmsyMF4afG/1S2Lq1rp4hF0sEa7yUIi 6GBq8IONXg300L89jOOR1PBFinf4RCMuDc1lycFEnQi2oB70ZeZoUOH2J3dIpzYyR3Nw pxXtwnSaNrHPkltm+9T5rfgPTEqid56qv/OqGBClscQUvX8F0cdH55m9IZGHlRVhYn48 MABg== X-Google-Smtp-Source: AJdET5enSEQeBJOcQVgXZpGLBcQtZd0h76K8S3C874gGlFjLhe01hHCqTSH9nWeq2d0OTbNmqW0DBLEbWib0 X-Received: by 2002:a9d:2944:: with SMTP id d62mr1890189otb.9.1540992406394; Wed, 31 Oct 2018 06:26:46 -0700 (PDT) Date: Wed, 31 Oct 2018 06:26:33 -0700 In-Reply-To: <20181031132634.50440-1-marcorr@google.com> Message-Id: <20181031132634.50440-4-marcorr@google.com> Mime-Version: 1.0 References: <20181031132634.50440-1-marcorr@google.com> X-Mailer: git-send-email 2.19.1.568.g152ad8e336-goog Subject: [kvm PATCH v5 3/4] kvm: vmx: refactor vmx_msrs struct for vmalloc From: Marc Orr To: kvm@vger.kernel.org, jmattson@google.com, rientjes@google.com, konrad.wilk@oracle.com, linux-mm@kvack.org, akpm@linux-foundation.org, pbonzini@redhat.com, rkrcmar@redhat.com, willy@infradead.org, sean.j.christopherson@intel.com, dave.hansen@linux.intel.com, kernellwp@gmail.com Cc: Marc Orr 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: X-Virus-Scanned: ClamAV using ClamSMTP Previously, the vmx_msrs struct relied being aligned within a struct that is backed by the direct map (e.g., memory allocated with kalloc()). Specifically, this enabled the virtual addresses associated with the struct to be translated to physical addresses. However, we'd like to refactor the host struct, vcpu_vmx, to be allocated with vmalloc(), so that allocation will succeed when contiguous physical memory is scarce. Thus, this patch refactors how vmx_msrs is declared and allocated, to ensure that it can be mapped to the physical address space, even when vmx_msrs resides within in a vmalloc()'d struct. Signed-off-by: Marc Orr --- arch/x86/kvm/vmx.c | 57 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 4078cf15a4b0..315cf4b5f262 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -970,8 +970,25 @@ static inline int pi_test_sn(struct pi_desc *pi_desc) struct vmx_msrs { unsigned int nr; - struct vmx_msr_entry val[NR_AUTOLOAD_MSRS]; + struct vmx_msr_entry *val; }; +struct kmem_cache *vmx_msr_entry_cache; + +/* + * To prevent vmx_msr_entry array from crossing a page boundary, require: + * sizeof(*vmx_msrs.vmx_msr_entry.val) to be a power of two. This is guaranteed + * through compile-time asserts that: + * - NR_AUTOLOAD_MSRS * sizeof(struct vmx_msr_entry) is a power of two + * - NR_AUTOLOAD_MSRS * sizeof(struct vmx_msr_entry) <= PAGE_SIZE + * - The allocation of vmx_msrs.vmx_msr_entry.val is aligned to its size. + */ +#define CHECK_POWER_OF_TWO(val) \ + BUILD_BUG_ON_MSG(!((val) && !((val) & ((val) - 1))), \ + #val " is not a power of two.") +#define CHECK_INTRA_PAGE(val) do { \ + CHECK_POWER_OF_TWO(val); \ + BUILD_BUG_ON(!(val <= PAGE_SIZE)); \ + } while (0) struct vcpu_vmx { struct kvm_vcpu vcpu; @@ -11497,6 +11514,19 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) goto free_partial_vcpu; } + vmx->msr_autoload.guest.val = + kmem_cache_zalloc(vmx_msr_entry_cache, GFP_KERNEL); + if (!vmx->msr_autoload.guest.val) { + err = -ENOMEM; + goto free_fpu; + } + vmx->msr_autoload.host.val = + kmem_cache_zalloc(vmx_msr_entry_cache, GFP_KERNEL); + if (!vmx->msr_autoload.host.val) { + err = -ENOMEM; + goto free_msr_autoload_guest; + } + vmx->vpid = allocate_vpid(); err = kvm_vcpu_init(&vmx->vcpu, kvm, id); @@ -11584,6 +11614,10 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) kvm_vcpu_uninit(&vmx->vcpu); free_vcpu: free_vpid(vmx->vpid); + kmem_cache_free(vmx_msr_entry_cache, vmx->msr_autoload.host.val); +free_msr_autoload_guest: + kmem_cache_free(vmx_msr_entry_cache, vmx->msr_autoload.guest.val); +free_fpu: kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu); free_partial_vcpu: kmem_cache_free(kvm_vcpu_cache, vmx); @@ -15163,6 +15197,10 @@ module_exit(vmx_exit); static int __init vmx_init(void) { int r; + size_t vmx_msr_entry_size = + sizeof(struct vmx_msr_entry) * NR_AUTOLOAD_MSRS; + + CHECK_INTRA_PAGE(vmx_msr_entry_size); #if IS_ENABLED(CONFIG_HYPERV) /* @@ -15194,9 +15232,21 @@ static int __init vmx_init(void) #endif r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), - __alignof__(struct vcpu_vmx), THIS_MODULE); + __alignof__(struct vcpu_vmx), THIS_MODULE); if (r) return r; + /* + * A vmx_msr_entry array resides exclusively within the kernel. Thus, + * use kmem_cache_create_usercopy(), with the usersize argument set to + * ZERO, to blacklist copying vmx_msr_entry to/from user space. + */ + vmx_msr_entry_cache = + kmem_cache_create_usercopy("vmx_msr_entry", vmx_msr_entry_size, + vmx_msr_entry_size, SLAB_ACCOUNT, 0, 0, NULL); + if (!vmx_msr_entry_cache) { + r = -ENOMEM; + goto out; + } /* * Must be called after kvm_init() so enable_ept is properly set @@ -15220,5 +15270,8 @@ static int __init vmx_init(void) vmx_check_vmcs12_offsets(); return 0; +out: + kvm_exit(); + return r; } module_init(vmx_init); From patchwork Wed Oct 31 13:26:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Orr X-Patchwork-Id: 10662687 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2DAEA14DE for ; Wed, 31 Oct 2018 13:26:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E42128935 for ; Wed, 31 Oct 2018 13:26:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1260628AE7; Wed, 31 Oct 2018 13:26:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 753BC28935 for ; Wed, 31 Oct 2018 13:26:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E4626B0271; Wed, 31 Oct 2018 09:26:50 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 56B916B0273; Wed, 31 Oct 2018 09:26:50 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 395216B0274; Wed, 31 Oct 2018 09:26:50 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by kanga.kvack.org (Postfix) with ESMTP id E48446B0271 for ; Wed, 31 Oct 2018 09:26:49 -0400 (EDT) Received: by mail-pl1-f197.google.com with SMTP id az6-v6so12280249plb.21 for ; Wed, 31 Oct 2018 06:26:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=unPH4bSgTFjVNwKk6oCcUeWwQ3wTeQ6A3j9CRmR6rxE=; b=Lk+9/ehdN/953mHoiFZnC0dRb4VUYxEADv7vS++stVtYEW8YwBQl4cwz2ZBRYju2eR AbQR9YwLm1SwOd88fP+MB80jOvgOfLzhPvCmjk+vE5wixF3X9mTK0pAaUDDDKPLGBMbj aymkke9HHzbYQKusoOO36UYOHwOB4vbKo5lL/1y6aQ3WDoS4RsPw4di/UOue5I9HRKRw 9QyGDbEP5mJvsrW9HF3Mohl2u6N4F7w/stMx46CjuIxY2Sc+RNpdBymA4e4GCFrBeAj/ CckTBKGE4USj26BWucN+FgkO1YpLhkhfxSjnyTShrBO+xBRibPvAX3ZsbBxUqHfuOZcT HK7g== X-Gm-Message-State: AGRZ1gK9Q6AEP1zH/2JJ5BBh+wxxTz2pVe7xJbYjNo6o8D1Uhboz8egx fsSC1PyDaq8R2KKTjVMc6khkwA06yGofD9BBog3S11mKY/A6ZiNI9+6X268ujXWP1Ln91FSxhBI sIEWuM6AIafmMlbmT58KLE+t/Wl4aC6Ayr/Alp1jcinrnWJdlxFEOjULsTbq7Nu0BcD9fjzkoVr 2uF02UWp2dZU70k0AXfM1KykhnF7HhWXt9vXjEiU1k/8jkk/qrFyfGqZrCxPfO4GC0ntMKWsTKP qXBid4f1Ys8S4M9pzYHD3//u94SWjaiu+W/V7q30nutzJY+jJ4YyB6KX4mv3WBiv3hw0t8xKchs xnLPwmkna2eJ9neZqcT0V010iG397ab+KSTT1dAc/1k7y/1Fqj+Ip6zU2WJdEXbF+wH/A3XG911 z X-Received: by 2002:a17:902:9a44:: with SMTP id x4-v6mr3384686plv.121.1540992409567; Wed, 31 Oct 2018 06:26:49 -0700 (PDT) X-Received: by 2002:a17:902:9a44:: with SMTP id x4-v6mr3384629plv.121.1540992408683; Wed, 31 Oct 2018 06:26:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540992408; cv=none; d=google.com; s=arc-20160816; b=l20cb6V1LyfF53fStg/wEPrFToQKuz3EDFUCgYcAsUjzbtZl2YWe5TyEcEQiG+UK3S 2JuaVlQgN+4t2QS/WNLq+7N4+7GvOrg1GhYXfY7CieSdO7GCEhL5gs+zgK+4cCFSbBT6 y7O02RrIVVTt2H+bPj3dqQV6weYt0GN94+g0oKJoOzN02OrW4aSbFpD5OhNIHtkkOjYN nnUBN+rOy99z1TcvLqlkieyF/RLifRezN2pgHQCOn9lRdiE+X8jw/F5uypOoMatgf/oB GXGyFaczU85lDQJWJXZs2Pspt4HYkcecfKWOqCui14JzfulK6bZ2X++VgIURfn7BlaaI 4Z1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:dkim-signature; bh=unPH4bSgTFjVNwKk6oCcUeWwQ3wTeQ6A3j9CRmR6rxE=; b=Oucv00uItMGxNxleGIX5vFu9SSO05IgQYNY3Sr59ioB+cFKD/HHlx2iuGW1ZMnq7c9 SD1wuh5OwIPodQ7dhSetU+QTXcbUxRGBEmk0bVuIkChXgelYMXQ45BBMtgkwusfWVqpb VdsaHmR5UPk2CayipPWQjqKCMiexeZBKmY+WJF2DqaourkE35Llpn4rZBQjAT0nO90W3 hQ3AhGXZL/3Em2YszK7rQYeWwePWRBCyQMYV5s4+0gAY9rxWSfdBOB1+w9SpDqRv+9au A8+RBZA8P3dU3sXU0xZItIPcrg6rAyBUWd0os9S0uAkCIRzCPYfY9/7nS8mvlgVJsTY1 SC7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cTbNiZHL; spf=pass (google.com: domain of 3mk3zwwckcd8nbsdpsshpphmf.dpnmjovy-nnlwbdl.psh@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3mK3ZWwcKCD8nbsdpsshpphmf.dpnmjovy-nnlwbdl.psh@flex--marcorr.bounces.google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f73.google.com (mail-sor-f73.google.com. [209.85.220.73]) by mx.google.com with SMTPS id d2-v6sor3574787pll.50.2018.10.31.06.26.48 for (Google Transport Security); Wed, 31 Oct 2018 06:26:48 -0700 (PDT) Received-SPF: pass (google.com: domain of 3mk3zwwckcd8nbsdpsshpphmf.dpnmjovy-nnlwbdl.psh@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) client-ip=209.85.220.73; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cTbNiZHL; spf=pass (google.com: domain of 3mk3zwwckcd8nbsdpsshpphmf.dpnmjovy-nnlwbdl.psh@flex--marcorr.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3mK3ZWwcKCD8nbsdpsshpphmf.dpnmjovy-nnlwbdl.psh@flex--marcorr.bounces.google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=unPH4bSgTFjVNwKk6oCcUeWwQ3wTeQ6A3j9CRmR6rxE=; b=cTbNiZHLLqiCsYSAZzQUyUDFYhvrkAWgtf3cj0Miwz59hKR9ktOnzwuowCNk10ND2T XeC/gRxIHywBmmJIRBm202aMLKcmxqyqY5Jtg5SqkFm4X2H6ffysMCn7ybtrWYT+PaaG M0udFkpE2MJmeBW3yYirc9TQFPR+lwIX/UJkCkjvvv5cDEEMWsZV5vM7mQLnxMTRsD3j ifkQ6gYckIfuYPQVcq+Uen7rBOmcmYiU5wMG6yroNThexULCmjpdl7EG7m7JYQpb5ct4 65Rcfut8b2u83RbIRPgKl12fBTrwroL2+60iS17EuEts6hEnbbtCZ/zelxNB/5rTu84f W3AA== X-Google-Smtp-Source: AJdET5fwqKY/nofJYGiflINsQ/c34vPpLl2kZUVixIhMBX5Oty0tx4FrTX1q4lr7MlcFLh3fR7e1oRtlW1FC X-Received: by 2002:a17:902:20e8:: with SMTP id v37-v6mr1039308plg.42.1540992408341; Wed, 31 Oct 2018 06:26:48 -0700 (PDT) Date: Wed, 31 Oct 2018 06:26:34 -0700 In-Reply-To: <20181031132634.50440-1-marcorr@google.com> Message-Id: <20181031132634.50440-5-marcorr@google.com> Mime-Version: 1.0 References: <20181031132634.50440-1-marcorr@google.com> X-Mailer: git-send-email 2.19.1.568.g152ad8e336-goog Subject: [kvm PATCH v5 4/4] kvm: vmx: use vmalloc() to allocate vcpus From: Marc Orr To: kvm@vger.kernel.org, jmattson@google.com, rientjes@google.com, konrad.wilk@oracle.com, linux-mm@kvack.org, akpm@linux-foundation.org, pbonzini@redhat.com, rkrcmar@redhat.com, willy@infradead.org, sean.j.christopherson@intel.com, dave.hansen@linux.intel.com, kernellwp@gmail.com Cc: Marc Orr 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: X-Virus-Scanned: ClamAV using ClamSMTP Previously, vcpus were allocated through the kmem_cache_zalloc() API, which requires the underlying physical memory to be contiguous. Because the x86 vcpu struct, struct vcpu_vmx, is relatively large (e.g., currently 47680 bytes on my setup), it can become hard to find contiguous memory. At the same time, the comments in the code indicate that the primary reason for using the kmem_cache_zalloc() API is to align the memory rather than to provide physical contiguity. Thus, this patch updates the vcpu allocation logic for vmx to use the vmalloc() API. Signed-off-by: Marc Orr --- arch/x86/kvm/vmx.c | 37 ++++++++++++++++++++++++++++++------- virt/kvm/kvm_main.c | 28 ++++++++++++++++------------ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 315cf4b5f262..af651540ee45 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -898,7 +898,14 @@ struct nested_vmx { #define POSTED_INTR_ON 0 #define POSTED_INTR_SN 1 -/* Posted-Interrupt Descriptor */ +/* + * Posted-Interrupt Descriptor + * + * Note, the physical address of this structure is used by VMX. Furthermore, the + * translation code assumes that the entire pi_desc struct resides within a + * single page, which will be true because the struct is 64 bytes and 64-byte + * aligned. + */ struct pi_desc { u32 pir[8]; /* Posted interrupt requested */ union { @@ -6633,6 +6640,14 @@ static void vmx_vcpu_setup(struct vcpu_vmx *vmx) } if (kvm_vcpu_apicv_active(&vmx->vcpu)) { + /* + * Note, pi_desc is contained within a single + * page because the struct is 64 bytes and 64-byte aligned. + */ + phys_addr_t pi_desc_phys = + page_to_phys(vmalloc_to_page(&vmx->pi_desc)) + + (u64)&vmx->pi_desc % PAGE_SIZE; + vmcs_write64(EOI_EXIT_BITMAP0, 0); vmcs_write64(EOI_EXIT_BITMAP1, 0); vmcs_write64(EOI_EXIT_BITMAP2, 0); @@ -6641,7 +6656,7 @@ static void vmx_vcpu_setup(struct vcpu_vmx *vmx) vmcs_write16(GUEST_INTR_STATUS, 0); vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); - vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); + vmcs_write64(POSTED_INTR_DESC_ADDR, pi_desc_phys); } if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { @@ -11494,13 +11509,18 @@ static void vmx_free_vcpu(struct kvm_vcpu *vcpu) kfree(vmx->guest_msrs); kvm_vcpu_uninit(vcpu); kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu); - kmem_cache_free(kvm_vcpu_cache, vmx); + kmem_cache_free(vmx_msr_entry_cache, vmx->msr_autoload.guest.val); + kmem_cache_free(vmx_msr_entry_cache, vmx->msr_autoload.host.val); + vfree(vmx); } static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) { int err; - struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); + struct vcpu_vmx *vmx = + __vmalloc(sizeof(struct vcpu_vmx), + GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT, + PAGE_KERNEL); unsigned long *msr_bitmap; int cpu; @@ -11620,7 +11640,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) free_fpu: kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu); free_partial_vcpu: - kmem_cache_free(kvm_vcpu_cache, vmx); + vfree(vmx); return ERR_PTR(err); } @@ -15231,8 +15251,11 @@ static int __init vmx_init(void) } #endif - r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), - __alignof__(struct vcpu_vmx), THIS_MODULE); + /* + * Disable kmem cache; vmalloc will be used instead + * to avoid OOM'ing when memory is available but not contiguous. + */ + r = kvm_init(&vmx_x86_ops, 0, 0, THIS_MODULE); if (r) return r; /* diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 786ade1843a2..8b979e7c3ecd 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4038,18 +4038,22 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, goto out_free_2; register_reboot_notifier(&kvm_reboot_notifier); - /* A kmem cache lets us meet the alignment requirements of fx_save. */ - if (!vcpu_align) - vcpu_align = __alignof__(struct kvm_vcpu); - kvm_vcpu_cache = - kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align, - SLAB_ACCOUNT, - offsetof(struct kvm_vcpu, arch), - sizeof_field(struct kvm_vcpu, arch), - NULL); - if (!kvm_vcpu_cache) { - r = -ENOMEM; - goto out_free_3; + /* + * When vcpu_size is zero, + * architecture-specific code manages its own vcpu allocation. + */ + kvm_vcpu_cache = NULL; + if (vcpu_size) { + if (!vcpu_align) + vcpu_align = __alignof__(struct kvm_vcpu); + kvm_vcpu_cache = kmem_cache_create_usercopy( + "kvm_vcpu", vcpu_size, vcpu_align, SLAB_ACCOUNT, + offsetof(struct kvm_vcpu, arch), + sizeof_field(struct kvm_vcpu, arch), NULL); + if (!kvm_vcpu_cache) { + r = -ENOMEM; + goto out_free_3; + } } r = kvm_async_pf_init();