From owner-svn-src-all@FreeBSD.ORG Mon May 18 18:17:01 2015 Return-Path: Delivered-To: svn-src-all@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 4D810DB8; Mon, 18 May 2015 18:17:01 +0000 (UTC) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id CB9E61E47; Mon, 18 May 2015 18:17:00 +0000 (UTC) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.14.9/8.14.9) with ESMTP id t4IIGtms046693 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Mon, 18 May 2015 21:16:55 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.9.2 kib.kiev.ua t4IIGtms046693 Received: (from kostik@localhost) by tom.home (8.14.9/8.14.9/Submit) id t4IIGtkJ046692; Mon, 18 May 2015 21:16:55 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Mon, 18 May 2015 21:16:55 +0300 From: Konstantin Belousov To: Andriy Gapon Cc: src-committers@FreeBSD.org, svn-src-all@FreeBSD.org, svn-src-head@FreeBSD.org Subject: Re: svn commit: r282678 - in head: share/man/man4 sys/amd64/acpica sys/amd64/include sys/dev/acpica sys/i386/acpica sys/i386/include sys/x86/include sys/x86/x86 Message-ID: <20150518181655.GF2499@kib.kiev.ua> References: <201505091228.t49CSmVv062442@svn.freebsd.org> <5559CD29.8020106@FreeBSD.org> <5559CFC8.3090001@FreeBSD.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5559CFC8.3090001@FreeBSD.org> User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 May 2015 18:17:01 -0000 On Mon, May 18, 2015 at 02:40:56PM +0300, Andriy Gapon wrote: > On 18/05/2015 14:29, Andriy Gapon wrote: > > On 09/05/2015 15:28, Konstantin Belousov wrote: > >> +void > >> +acpi_cpu_idle_mwait(uint32_t mwait_hint) > >> +{ > >> + int *state; > >> + > >> + state = (int *)PCPU_PTR(monitorbuf); > >> + /* > >> + * XXXKIB. Software coordination mode should be supported, > >> + * but all Intel CPUs provide hardware coordination. > >> + */ > >> + cpu_monitor(state, 0, 0); > >> + cpu_mwait(MWAIT_INTRBREAK, mwait_hint); > >> +} > >> + > > > > Kostik, > > > > it's been a while since I studied this code, so please pardon me if I am asking > > something obvious or silly. > > > > I wonder why this function does not set 'state' before monitor + mwait. > > As far as I can see, all other idling functions do that. And cpu_idle_wakeup() > > compares the state to STATE_MWAIT before changing it. > > > > So, I am concerned that if the state happens to be anything other than > > STATE_MWAIT when acpi_cpu_idle_mwait() is called, then cpu_idle_wakeup() won't > > wake up the idled CPU. It seems that if the state is not STATE_SLEEPING then an > > IPI won't be sent either. Actually, that leaves STATE_RUNNING is the only > > problematic case, but that's probably the state that the CPU would have before > > idling. > > > > After having written the above I realized what I overlooked: > acpi_cpu_idle_mwait() is called from the ACPI idle method, so the state must > already be set to STATE_SLEEPING. > So, looks like the wake-up would always be done by an IPI... > > Just in case, here's what I had in my old local code: > void > acpi_cpu_mwait_cx(u_int hints) > { > int *state; > > state = (int *)PCPU_PTR(monitorbuf); > KASSERT(*state == STATE_SLEEPING, > ("cpu_mwait_cx: wrong monitorbuf state")); > *state = STATE_MWAIT; > cpu_monitor(state, 0, 0); > if (*state == STATE_MWAIT) > cpu_mwait(MWAIT_INTR_BRK, hints); > > /* > * We should exit on any event that interrupts mwait, > * because that event might be a wanted interrupt. > */ > *state = STATE_RUNNING; > } > > This code also accounted for a time window between the CPU wanting to go idle > and it calling mwait. During that window other CPU could want to wake up the > first CPU. Right. And there is very similar, but having some small important difference, function cpu_idle_mwait(). My plan was to give the commit some time for wider testing, then axe cpu_idle_mwait and corresponding machdep.idle selector, with the move of the fast wakeup code under acpi_cpu_idle_mwait(). Additional reason to wait was the use of cpu_idle_mwait() as the 'fast' idle method for busy CPU, whatever this means. I think I would commit the patch below shortly, with 'Submitted by: avg'. diff --git a/sys/x86/x86/cpu_machdep.c b/sys/x86/x86/cpu_machdep.c index 7925713..f07b97e 100644 --- a/sys/x86/x86/cpu_machdep.c +++ b/sys/x86/x86/cpu_machdep.c @@ -101,6 +101,10 @@ __FBSDID("$FreeBSD$"); #include #include +#define STATE_RUNNING 0x0 +#define STATE_MWAIT 0x1 +#define STATE_SLEEPING 0x2 + /* * Machine dependent boot() routine * @@ -134,13 +138,24 @@ acpi_cpu_idle_mwait(uint32_t mwait_hint) { int *state; - state = (int *)PCPU_PTR(monitorbuf); /* * XXXKIB. Software coordination mode should be supported, * but all Intel CPUs provide hardware coordination. */ + + state = (int *)PCPU_PTR(monitorbuf); + KASSERT(*state == STATE_SLEEPING, + ("cpu_mwait_cx: wrong monitorbuf state")); + *state = STATE_MWAIT; cpu_monitor(state, 0, 0); - cpu_mwait(MWAIT_INTRBREAK, mwait_hint); + if (*state == STATE_MWAIT) + cpu_mwait(MWAIT_INTRBREAK, mwait_hint); + + /* + * We should exit on any event that interrupts mwait, because + * that event might be a wanted interrupt. + */ + *state = STATE_RUNNING; } /* Get current clock frequency for the given cpu id. */ @@ -231,10 +246,6 @@ static int idle_mwait = 1; /* Use MONITOR/MWAIT for short idle. */ SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RWTUN, &idle_mwait, 0, "Use MONITOR/MWAIT for short idle"); -#define STATE_RUNNING 0x0 -#define STATE_MWAIT 0x1 -#define STATE_SLEEPING 0x2 - #ifndef PC98 static void cpu_idle_acpi(sbintime_t sbt)