From patchwork Wed Aug 16 16:46:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Faggioli X-Patchwork-Id: 9904155 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3FBEF600CA for ; Wed, 16 Aug 2017 16:48:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 30DD728A1B for ; Wed, 16 Aug 2017 16:48:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2557828A4C; Wed, 16 Aug 2017 16:48:22 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_SPAM,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 65C9128A1B for ; Wed, 16 Aug 2017 16:48:21 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1di1SL-00028r-FB; Wed, 16 Aug 2017 16:46:09 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1di1SK-00028Y-K2 for xen-devel@lists.xenproject.org; Wed, 16 Aug 2017 16:46:08 +0000 Received: from [85.158.139.211] by server-10.bemta-5.messagelabs.com id E4/3B-01732-FC674995; Wed, 16 Aug 2017 16:46:07 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrBIsWRWlGSWpSXmKPExsVyMbThsO75sim RBrO2sVh83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBk/f5YU/LOrOHbBroFxuXEXIxeHkMAMRoln c64xgTgsAmtYJVbMfgTmSAhcYpXY9XgNcxcjJ5ATJ/F99j52CLtK4tfjy6wgtpCAisTN7auYI Eb9YJS42bmYCSQhLKAnceToD3YIO0Sis/0XWAObgIHEmx17wWwRASWJe6smgzUzC0xilph+4y kjSIJFQFXi+O5pYJt5BXwkNv47xAZicwr4Six8cJkFYrOPxMGevWD1ogJyEisvt7BC1AtKnJz 5BKiGA2iopsT6XfogYWYBeYntb+cwT2AUmYWkahZC1SwkVQsYmVcxahSnFpWlFukaWeolFWWm Z5TkJmbm6BoamOrlphYXJ6an5iQmFesl5+duYgSGfz0DA+MOxstb/A4xSnIwKYnyeuVPiRTiS 8pPqcxILM6ILyrNSS0+xCjDwaEkwStTCpQTLEpNT61Iy8wBRiJMWoKDR0mEd34JUJq3uCAxtz gzHSJ1itGY48qVdV+YOKYc2P6FSYglLz8vVUqcVwdkkgBIaUZpHtwgWIK4xCgrJczLyMDAIMR TkFqUm1mCKv+KUZyDUUmYVxZkCk9mXgncvldApzABnXKlfRLIKSWJCCmpBsZVDcoifr021TNU tj+86VsZV5vuPZtdVWD37s3nJnXuZU+/+tt9y9oIboHGJy0b+Rudpr6fKSh8vL/k5vq3nnJrj GadbW/kqFrr/nXiRBtb64tuCjs06g1c/nWaVrovS/o8x3TDc+tFnMqa/nts10ZMWOKV9n6XpI zOzEObclIl3qs687d8FVJiKc5INNRiLipOBABMgIKyCwMAAA== X-Env-Sender: raistlin.df@gmail.com X-Msg-Ref: server-15.tower-206.messagelabs.com!1502901966!93543177!1 X-Originating-IP: [209.85.128.195] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 23270 invoked from network); 16 Aug 2017 16:46:07 -0000 Received: from mail-wr0-f195.google.com (HELO mail-wr0-f195.google.com) (209.85.128.195) by server-15.tower-206.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 16 Aug 2017 16:46:07 -0000 Received: by mail-wr0-f195.google.com with SMTP id n88so4011153wrb.0 for ; Wed, 16 Aug 2017 09:46:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=OL4jw3rbzX4S4/Xd7BxHeFDaodP3SFYhBeuyJpWI8j0=; b=fcfX4le+iNFZFbRWpTgJBUfItNMN3srOZMrDy18GkAbqLssgGjvqfNkubtZCvM68f1 osvNUbFAaSSFEZPQs998c419SkyRYFmSDuCo0VsD8UTHQxsO/nsvs8CiGw57uxupaFHE Hk2ZXn2Rso/Esxd5Fu+zVblOck1iF+dBi8UfYyL5mGE4nX9EiRhQS7t2m8UkLY3m6PMf E0Is8SKjFH7vIh+NhbS/aevGd+uw19xynmgEqniQB/fYIYHo0lY40ckmqPepw9ofqoEM owx/oXYtkkLJ1JkkrhxYLyJuiUf1FPEVTNdy6uFkrUrK+uTOkVvBr0ADkYwqW7vUr7VY gErA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:from:to:cc:date:message-id :in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=OL4jw3rbzX4S4/Xd7BxHeFDaodP3SFYhBeuyJpWI8j0=; b=lxtITcKOv/ahEndvp7OC7e1kuxqh3oDJsGEirwqKhCRNtadqZXHlFVm0OCqXmrZcag PTGpR37HcMNiBykcyrc5rsRSbFh/EqE+Aye1h/Dvvx68km0pQJKKKJV2L7O7gyVNthlP ugLB6sXjf1kB/UWiTjTdWN/30TrtUVWOtu7Qq7elgStT67ciqnmOVngFHZ6F42C9lldx qi4tiLfNuxC3hU6GmD3dGyv4jIDX8dycO6TfszouC1OaGRlpXVrJm7P8QxZoCDPrlPXE ikvssdM4LoWf3aj0zKFMtwAl0H9sy+YdmelgYYOBkuj2Rku8Y1AFZOT7VN+2oweQNGzH C+xg== X-Gm-Message-State: AHYfb5iHySZg6jfZNOJFoD+IIOMOOqTvENL7Lat34I7wET+FAxvwMlAh 7nWzxWB/1o6EtQ== X-Received: by 10.28.109.218 with SMTP id b87mr1563547wmi.114.1502901966649; Wed, 16 Aug 2017 09:46:06 -0700 (PDT) Received: from Solace.fritz.box ([80.66.223.3]) by smtp.gmail.com with ESMTPSA id f80sm1215658wmh.16.2017.08.16.09.46.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Aug 2017 09:46:06 -0700 (PDT) From: Dario Faggioli To: xen-devel@lists.xenproject.org Date: Wed, 16 Aug 2017 18:46:04 +0200 Message-ID: <150290196442.24854.11981777254473091320.stgit@Solace.fritz.box> In-Reply-To: <150290125292.24854.17418548557562763544.stgit@Solace.fritz.box> References: <150290125292.24854.17418548557562763544.stgit@Solace.fritz.box> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Julien Grall , Jan Beulich Subject: [Xen-devel] [PATCH v2 5/6] xen: RCU: avoid busy waiting until the end of grace period. X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP On the CPU where a callback is queued, cpu_is_haltable() returns false (due to rcu_needs_cpu() being itself false). That means the CPU would spin inside idle_loop(), continuously calling do_softirq(), and, in there, continuously checking rcu_pending(), in a tight loop. Let's instead allow the CPU to really go idle, but make sure, by arming a timer, that we periodically check whether the grace period has come to an ended. As the period of the timer, we pick a value that makes thing look like what happens in Linux, with the periodic tick (as this code comes from there). Note that the timer will *only* be armed on CPUs that are going idle while having queued RCU callbacks. On CPUs that don't, there won't be any timer, and their sleep won't be interrupted (and even for CPUs with callbacks, we only expect an handful of wakeups at most, but that depends on the system load, as much as from other things). Signed-off-by: Dario Faggioli --- Cc: Jan Beulich Cc: Andrew Cooper Cc: George Dunlap Cc: Ian Jackson Cc: Konrad Rzeszutek Wilk Cc: Stefano Stabellini Cc: Tim Deegan Cc: Wei Liu Cc: George Dunlap Cc: Julien Grall --- Changes from v1: * clarified changelog; * fix style/indentation issues; * deal with RCU idle timer in tick suspension logic; * as a consequence of the point above, the timer now fires, so kill the ASSERT_UNREACHABLE, and put a perfcounter there (to count the times it triggers); * add a comment about the value chosen for programming the idle timer; * avoid pointless/bogus '!!' and void* casts; * rearrange the rcu_needs_cpu() return condition; * add a comment to clarify why we don't want to check rcu_pending() in rcu_idle_timer_start(). --- xen/arch/x86/cpu/mwait-idle.c | 3 +- xen/common/rcupdate.c | 72 ++++++++++++++++++++++++++++++++++++++++- xen/common/schedule.c | 2 + xen/include/xen/perfc_defn.h | 2 + xen/include/xen/rcupdate.h | 3 ++ 5 files changed, 79 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/cpu/mwait-idle.c b/xen/arch/x86/cpu/mwait-idle.c index 762dff1..b6770ea 100644 --- a/xen/arch/x86/cpu/mwait-idle.c +++ b/xen/arch/x86/cpu/mwait-idle.c @@ -741,9 +741,8 @@ static void mwait_idle(void) } cpufreq_dbs_timer_suspend(); - sched_tick_suspend(); - /* sched_tick_suspend() can raise TIMER_SOFTIRQ. Process it now. */ + /* Timer related operations can raise TIMER_SOFTIRQ. Process it now. */ process_pending_softirqs(); /* Interrupts must be disabled for C2 and higher transitions. */ diff --git a/xen/common/rcupdate.c b/xen/common/rcupdate.c index 9f7d41d..e27bfed 100644 --- a/xen/common/rcupdate.c +++ b/xen/common/rcupdate.c @@ -84,8 +84,37 @@ struct rcu_data { int cpu; struct rcu_head barrier; long last_rs_qlen; /* qlen during the last resched */ + + /* 3) idle CPUs handling */ + struct timer idle_timer; + bool idle_timer_active; }; +/* + * If a CPU with RCU callbacks queued goes idle, when the grace period is + * not finished yet, how can we make sure that the callbacks will eventually + * be executed? In Linux (2.6.21, the first "tickless idle" Linux kernel), + * the periodic timer tick would not be stopped for such CPU. Here in Xen, + * we (may) don't even have a periodic timer tick, so we need to use a + * special purpose timer. + * + * Such timer: + * 1) is armed only when a CPU with an RCU callback(s) queued goes idle + * before the end of the current grace period (_not_ for any CPUs that + * go idle!); + * 2) when it fires, it is only re-armed if the grace period is still + * running; + * 3) it is stopped immediately, if the CPU wakes up from idle and + * resumes 'normal' execution. + * + * About how far in the future the timer should be programmed each time, + * it's hard to tell (guess!!). Since this mimics Linux's periodic timer + * tick, take values used there as an indication. In Linux 2.6.21, tick + * period can be 10ms, 4ms, 3.33ms or 1ms. Let's use 10ms, to enable + * at least some power saving on the CPU that is going idle. + */ +#define RCU_IDLE_TIMER_PERIOD MILLISECS(10) + static DEFINE_PER_CPU(struct rcu_data, rcu_data); static int blimit = 10; @@ -404,7 +433,45 @@ int rcu_needs_cpu(int cpu) { struct rcu_data *rdp = &per_cpu(rcu_data, cpu); - return (!!rdp->curlist || rcu_pending(cpu)); + return (rdp->curlist && !rdp->idle_timer_active) || rcu_pending(cpu); +} + +/* + * Timer for making sure the CPU where a callback is queued does + * periodically poke rcu_pedning(), so that it will invoke the callback + * not too late after the end of the grace period. + */ +void rcu_idle_timer_start() +{ + struct rcu_data *rdp = &this_cpu(rcu_data); + + /* + * Note that we don't check rcu_pending() here. In fact, we don't want + * the timer armed on CPUs that are in the process of quiescing while + * going idle, unless they really are the ones with a queued callback. + */ + if (likely(!rdp->curlist)) + return; + + set_timer(&rdp->idle_timer, NOW() + RCU_IDLE_TIMER_PERIOD); + rdp->idle_timer_active = true; +} + +void rcu_idle_timer_stop() +{ + struct rcu_data *rdp = &this_cpu(rcu_data); + + if (likely(!rdp->idle_timer_active)) + return; + + rdp->idle_timer_active = false; + stop_timer(&rdp->idle_timer); +} + +static void rcu_idle_timer_handler(void* data) +{ + /* Nothing, really... Just count the number of times we fire */ + perfc_incr(rcu_idle_timer); } void rcu_check_callbacks(int cpu) @@ -425,6 +492,8 @@ static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list, static void rcu_offline_cpu(struct rcu_data *this_rdp, struct rcu_ctrlblk *rcp, struct rcu_data *rdp) { + kill_timer(&rdp->idle_timer); + /* If the cpu going offline owns the grace period we can block * indefinitely waiting for it, so flush it here. */ @@ -453,6 +522,7 @@ static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp, rdp->qs_pending = 0; rdp->cpu = cpu; rdp->blimit = blimit; + init_timer(&rdp->idle_timer, rcu_idle_timer_handler, rdp, cpu); } static int cpu_callback( diff --git a/xen/common/schedule.c b/xen/common/schedule.c index c6f4817..8827921 100644 --- a/xen/common/schedule.c +++ b/xen/common/schedule.c @@ -1904,6 +1904,7 @@ void sched_tick_suspend(void) sched = per_cpu(scheduler, cpu); SCHED_OP(sched, tick_suspend, cpu); rcu_idle_enter(cpu); + rcu_idle_timer_start(); } void sched_tick_resume(void) @@ -1911,6 +1912,7 @@ void sched_tick_resume(void) struct scheduler *sched; unsigned int cpu = smp_processor_id(); + rcu_idle_timer_stop(); rcu_idle_exit(cpu); sched = per_cpu(scheduler, cpu); SCHED_OP(sched, tick_resume, cpu); diff --git a/xen/include/xen/perfc_defn.h b/xen/include/xen/perfc_defn.h index 53849af..ca446e5 100644 --- a/xen/include/xen/perfc_defn.h +++ b/xen/include/xen/perfc_defn.h @@ -12,6 +12,8 @@ PERFCOUNTER(calls_from_multicall, "calls from multicall") PERFCOUNTER(irqs, "#interrupts") PERFCOUNTER(ipis, "#IPIs") +PERFCOUNTER(rcu_idle_timer, "RCU: idle_timer") + /* Generic scheduler counters (applicable to all schedulers) */ PERFCOUNTER(sched_irq, "sched: timer") PERFCOUNTER(sched_run, "sched: runs through scheduler") diff --git a/xen/include/xen/rcupdate.h b/xen/include/xen/rcupdate.h index 561ac43..3402eb5 100644 --- a/xen/include/xen/rcupdate.h +++ b/xen/include/xen/rcupdate.h @@ -149,4 +149,7 @@ int rcu_barrier(void); void rcu_idle_enter(unsigned int cpu); void rcu_idle_exit(unsigned int cpu); +void rcu_idle_timer_start(void); +void rcu_idle_timer_stop(void); + #endif /* __XEN_RCUPDATE_H */