Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2459823pxb; Fri, 5 Feb 2021 20:04:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJz4eR38Gty366rZ8wXjsy7tP76Hgb0aSfMlQeIf/hDLWPoO4VehxTtZXRWoOI+4z7oCV1Md X-Received: by 2002:a17:906:1613:: with SMTP id m19mr7162627ejd.344.1612584245569; Fri, 05 Feb 2021 20:04:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612584245; cv=none; d=google.com; s=arc-20160816; b=Q6PDnaxA6mH762lkQklpHbiQfUNiORaVoOq15x9FuvEnTN534ZdapWFxLP6Vqbc/PN 5SrxkAsZRkDAjSABL435AKmcUWXCi/P1T6RHuCgYzNTrC1uQEuYGbtUBUiWfEuq5IE4P i9wvc1VhuM8oV340w6MAvs1n970NkVOMyvx75GltWdXnENkj2QzH+xNkJy6P7Wz/jBU6 gP7nAGgRURBnZqIzZJ/TKKzf5ClMftu14zXaucQu177W0lBO464VSOAcHxr2qyqw509A ONU7vnQswGQ/nAR7uKXiE6NsCGarGunyfP88qwZyR70Xe+scWrQEKlGNMISsHmcQ7mfs K7/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:message-id :mime-version:in-reply-to:references:cc:to:subject:from:date :dkim-signature; bh=P8M5D06zGz+PP013NXW7T63SHqKN6IjGe2PJ8tjT/2M=; b=nvCXbSm5kyMKykpaTZz0377VFq/nq6cDdO7NEUC0kKSYXzE1fJmJb+LrJUDVF83TY0 GQXuxI7bDQwQUxErhxxJAn9ZtvoGD/djQLO9UJikGebv25nlmlG1SnBT6bXQ2w4oMdxH KLBIPYNvF13JX4Krj+fTmvZnO6pmH/jsYGTpCxuC9PBp4xypTaoIsif/uUJToaMSTi00 pHXMlKkD1V4cHuT4B3Is7krWxYfzL/6HkhKOTRO8Y74+LL+n9Bwuz749vbPZHI4/wh8G V0t5Fz+O28lwRq7jGb7JX0k8puHIt4qVI57hxX1ITqT6m92OCvYa9hjWKayUJizkhldA mxTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XHE7VzBe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a12si7101930edn.568.2021.02.05.20.03.40; Fri, 05 Feb 2021 20:04:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XHE7VzBe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232137AbhBFEBD (ORCPT + 99 others); Fri, 5 Feb 2021 23:01:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232053AbhBFDD4 (ORCPT ); Fri, 5 Feb 2021 22:03:56 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6182C061788; Fri, 5 Feb 2021 19:03:15 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id lw17so7404027pjb.0; Fri, 05 Feb 2021 19:03:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:subject:to:cc:references:in-reply-to:mime-version :message-id:content-transfer-encoding; bh=P8M5D06zGz+PP013NXW7T63SHqKN6IjGe2PJ8tjT/2M=; b=XHE7VzBeJ2VyZCPQMDaisRZpClsmtX217Q8Cgdwx9ko69Fi5QbynKNqxgLWUpM2bky WPB7UTigD2ob9uxRdLQykZwyfhwJgAjCoCHwLgn8UvkvjpcrEipfcVa7z+7g5jwlz6eP UFyncS8wiBBC6pYf+M7/FizQjhrofvnnvaUHHS/y7wuK99w9cc6VpTw4tvXDCQNjlGW1 vn8FTnp7Wd790YaVImg3dSIDExL/78UXbmBlYk5KNeuvkd8B0stdiMpxneFbaqkr1xsi F/vxjHdZHXsnp2p7RpsaObZBaLeu1mbnXDqcFR2XGqCNXDMpcZoy5rhqQvUW2qy7mmuc eW8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:subject:to:cc:references:in-reply-to :mime-version:message-id:content-transfer-encoding; bh=P8M5D06zGz+PP013NXW7T63SHqKN6IjGe2PJ8tjT/2M=; b=YQ3eihPsCKKGFXgwxsIzhPkK4HXwhK81LiTup034omVWkCGxNRWZaEP9U3A/DP+TKM LwK2R1qEU77oPJ9kPVacGTPvkhRGnor5M4/VFQbLnXzqZmLv1lP+DLGzqNXBQusn+nEv ETOMIJqIg5IRkZIjk81iU2KKIOZE1yvjFLBpCmCryQmsFY6YyQkYNhsl97Fz1PffVo47 33oObJchVt7/5biIA98ORvFAlnpNfyFs4DIrS7PlQpAcF7WzwdOFaFheOvtvdPz/UkOb D/sSwDRLhTHDuZGsveQJvAHs1P2gBMT2yPb1ToTsh9cqQ6p7SemKoTysxnwtJmkH1mTI bo1g== X-Gm-Message-State: AOAM531KHzKYaMs1rlBCpigJvSINJ1+Ej/6UOgLmbxNLuWwnmX5ay5bm w87nZsJaaQw+1nGMMuNNVp4= X-Received: by 2002:a17:90a:4a84:: with SMTP id f4mr7094787pjh.231.1612580595381; Fri, 05 Feb 2021 19:03:15 -0800 (PST) Received: from localhost (60-242-11-44.static.tpgi.com.au. [60.242.11.44]) by smtp.gmail.com with ESMTPSA id x4sm3135012pfm.64.2021.02.05.19.03.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 19:03:14 -0800 (PST) Date: Sat, 06 Feb 2021 13:03:09 +1000 From: Nicholas Piggin Subject: Re: [PATCH v2 1/1] powerpc/kvm: Save Timebase Offset to fix sched_clock() while running guest code. To: "Aneesh Kumar K.V" , Athira Rajeev , Benjamin Herrenschmidt , Christophe Leroy , Frederic Weisbecker , Geert Uytterhoeven , Jordan Niethe , Leonardo Bras , Michael Ellerman , Paul Mackerras , Thomas Gleixner Cc: kvm-ppc@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org References: <20210205060643.233481-1-leobras.c@gmail.com> <1612506268.6rrvx34gzu.astroid@bobo.none> <7e231b91e41c3f3586ba2fd604c40f1716db228d.camel@gmail.com> In-Reply-To: <7e231b91e41c3f3586ba2fd604c40f1716db228d.camel@gmail.com> MIME-Version: 1.0 Message-Id: <1612579579.ztbklit4un.astroid@bobo.none> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Excerpts from Leonardo Bras's message of February 5, 2021 5:01 pm: > Hey Nick, thanks for reviewing :) >=20 > On Fri, 2021-02-05 at 16:28 +1000, Nicholas Piggin wrote: >> Excerpts from Leonardo Bras's message of February 5, 2021 4:06 pm: >> > Before guest entry, TBU40 register is changed to reflect guest timebas= e. >> > After exitting guest, the register is reverted to it's original value. >> >=20 >> > If one tries to get the timestamp from host between those changes, it >> > will present an incorrect value. >> >=20 >> > An example would be trying to add a tracepoint in >> > kvmppc_guest_entry_inject_int(), which depending on last tracepoint >> > acquired could actually cause the host to crash. >> >=20 >> > Save the Timebase Offset to PACA and use it on sched_clock() to always >> > get the correct timestamp. >>=20 >> Ouch. Not sure how reasonable it is to half switch into guest registers=20 >> and expect to call into the wider kernel, fixing things up as we go.=20 >> What if mftb is used in other places? >=20 > IIUC, the CPU is not supposed to call anything as host between guest > entry and guest exit, except guest-related cases, like When I say "call", I'm including tracing in that. If a function is not=20 marked as no trace, then it will call into the tracing subsystem. > kvmppc_guest_entry_inject_int(), but anyway, if something calls mftb it > will still get the same value as before. Right, so it'll be out of whack again. > This is only supposed to change stuff that depends on sched_clock, like > Tracepoints, that can happen in those exceptions. If they depend on sched_clock that's one thing. Do they definitely have=20 no dependencies on mftb from other calls? >> Especially as it doesn't seem like there is a reason that function _has_ >> to be called after the timebase is switched to guest, that's just how=20 >> the code is structured. >=20 > Correct, but if called, like in rb routines, used by tracepoints, the > difference between last tb and current (lower) tb may cause the CPU to > trap PROGRAM exception, crashing host.=20 Yes, so I agree with Michael any function that is involved when we begin=20 to switch into guest context (or have not completed switching back to=20 host going the other way) should be marked as no trace (noinstr even,=20 perhaps). >> As a local hack to work out a bug okay. If you really need it upstream=20 >> could you put it under a debug config option? >=20 > You mean something that is automatically selected whenever those > configs are enabled?=20 >=20 > CONFIG_TRACEPOINT && CONFIG_KVM_BOOK3S_HANDLER && CONFIG_PPC_BOOK3S_64 >=20 > Or something the user need to select himself in menuconfig? Yeah I meant a default n thing under powerpc kernel debugging somewhere. Thanks, Nick