Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp995715ybi; Fri, 2 Aug 2019 07:40:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqwtWlRm+SS+rAI/IIfYjHdEwQf+IDVObuUQesnW9l7CRuDvesgrDU6h3s5/bHpOB5JcxqX0 X-Received: by 2002:a17:90a:21cc:: with SMTP id q70mr4854572pjc.56.1564756840253; Fri, 02 Aug 2019 07:40:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564756840; cv=none; d=google.com; s=arc-20160816; b=daeUvcwcyuJma8PP1DhZOlSTxeUkaHHI97b3mjxBcPKs+GWs8e//23hctr+lgG96b2 +hFhZZAsYlVCr9UgZ9ouwK9lbtFlOKBCZuMtfh6/9ps/qTFppezsrjg76tOLkNIV3ZYa IXVz8Lh0Wjwa4anoAE+XCXHq/cZF6HNSJqUbRKOkF6aliVz0NraezlDVv7THd6xKBNWX qSurEUiemsU8KYkXGS3u/N8QXRlNEGzKeKsG4b14XGDibgJCbKpJGJC5T6lR/6ZgoXmK Ffre9pD5FwvQQ1VOq5Z8qqUnerb4NBl7PbEvfRmZVz3LmTrJPS4W071ubHlrGyIPGbxX UaTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:openpgp:from:references:cc:to:subject; bh=JgbbmoU8mRwQDt1oX8O7FGI/YFBQIrkxghclZVXxh9E=; b=FCTKVzF1vqvTZ9rUyK8tkn77veza9zBnfEt8WECyiZpf53vY4Ji+IUWDDnAj7/fwJV 7jJPtDHf2ujLBRZgF9fIo2pRQc7sgTrIPKub1wquaGd+Od762/5u9E14CGP+wX4c82fz Z2Z0sLB1O6Z8bVCYyeXX+4Nl1tZOdiOx5B/KkCDRzWJa7aqOx7p1QDLNpWigavA1dRok HwJA64qpY0sEi94JsX9lG0TUVNEGJ/VmU/SEoQCXKSeInvOC4OScyeMBHFwGgmjQiAUY H5AnxUpDgtEBnCty+FDS06GaJjNndYw4Ap6zVj424AhEKd3V2Qrh6GNU/qogbT51Ehig kNKg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b5si37421303pfd.273.2019.08.02.07.40.25; Fri, 02 Aug 2019 07:40:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732193AbfHBI7j (ORCPT + 99 others); Fri, 2 Aug 2019 04:59:39 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:39975 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728232AbfHBI7j (ORCPT ); Fri, 2 Aug 2019 04:59:39 -0400 Received: by mail-wm1-f65.google.com with SMTP id v19so65612605wmj.5 for ; Fri, 02 Aug 2019 01:59:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:message-id :date:user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=JgbbmoU8mRwQDt1oX8O7FGI/YFBQIrkxghclZVXxh9E=; b=MsRNW8goS0rD0UMxxxHoJAeLTzPo2ZgY6+yCKnkI+80WuU2yMmhCnrkofcLMqY4Eew qV63A3oPxc0DGYvhqbhVnlz07gRi/dg981aaHd6xmDeFwtIvusGQcDoRVmhXs6UzlHB4 DdUJV8wFsnJwyL+1msFASYVjcMqhTqRNd0J2jSYStR+Pmj2MmXdQNaI2XMbr25Pi+Ob6 kpXIcLHpXoL0HnaJ9JtmaEO9hOGe4RsB9++53rdBQeIyYAbAElDAPJFRN+ucXO4y8Azu rqcHBk475UdV9zDXy7m3nann/1TgIGFr37OAZrLxb0ocxmVVqsg3DGTS1eCnjQa7Rw1T ipMg== X-Gm-Message-State: APjAAAU6QJgrGLe79gCeFTATezMFcjwL1mJ4Q5pn/jS93CKRrgux4Ohp AUdHFv62CVOLsIxsNNtVaMilhlDVnJM= X-Received: by 2002:a1c:4184:: with SMTP id o126mr3410562wma.68.1564736377101; Fri, 02 Aug 2019 01:59:37 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:4013:e920:9388:c3ff? ([2001:b07:6468:f312:4013:e920:9388:c3ff]) by smtp.gmail.com with ESMTPSA id f1sm50649557wml.28.2019.08.02.01.59.36 (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Fri, 02 Aug 2019 01:59:36 -0700 (PDT) Subject: Re: [RFC PATCH v2 08/19] RISC-V: KVM: Implement VCPU world-switch To: Anup Patel Cc: Anup Patel , Palmer Dabbelt , Paul Walmsley , Radim K , Daniel Lezcano , Thomas Gleixner , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , "kvm@vger.kernel.org" , "linux-riscv@lists.infradead.org" , "linux-kernel@vger.kernel.org" References: <20190802074620.115029-1-anup.patel@wdc.com> <20190802074620.115029-9-anup.patel@wdc.com> <72d8efbf-ec62-ab1e-68bf-e0c5f0bc256e@redhat.com> From: Paolo Bonzini Openpgp: preference=signencrypt Message-ID: Date: Fri, 2 Aug 2019 10:59:36 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/08/19 10:43, Anup Patel wrote: >> A possible optimization: if these cannot change while Linux runs (I am >> thinking especially of STVEC and HSTATUS, but perhaps SSCRATCH can be >> saved on kvm_arch_vcpu_load too) you can avoid the csrr and store. > Actual exception vector of Host Linux is different so we switch STVEC > every time. > > HSTATUS.SPV is set whenever we come back from Guest world so > while we are in in-kernel run loop with interrupts enabled we can get > external interrupt and HSTATUS.SPV bit can affect SRET of interrupt > handler. To handle this we switch HSTATUS every time. > > The world switch code uses SSCRATCH to save vcpu->arch pointer > which is later used on return path. Now, I did not want to restrict Host > Linux from using SSCRATCH for some other purpose hence we > switch SSCRATCH every time. Right, I'm not saying not to save these registers. I'm saying not to read the host value on every world switch, instead load it in hardware_enable (if it's the same for all physical CPUs) or kvm_arch_vcpu_load (if it's different for every physical CPU). IIUC Linux does not use SSCRATCH while in the kernel (it must be zero while handling an exception, but handle_exception takes care of that). I think it's okay if you make this assumption, but if you don't want to make it, you can still save it in kvm_arch_vcpu_load rather than here since you "own" the thread while in KVM_RUN. Paolo