Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2939184ybb; Sun, 5 Apr 2020 21:28:25 -0700 (PDT) X-Google-Smtp-Source: APiQypJkTdEMdN+YvP9i4gl27HsMBlH48l4+sOUV7bFbFDBOYoTRdswPC6Rs7SJMdQ5f/s2ks/CC X-Received: by 2002:a9d:7488:: with SMTP id t8mr15670741otk.219.1586147305263; Sun, 05 Apr 2020 21:28:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586147305; cv=none; d=google.com; s=arc-20160816; b=ANwb7xm2aDJHVX2YWAS+tPFpDi9P3X0F9tZvSxTLz1Vnya73mgLqL8tJxyA8jBb8Rz m2j7Z1a8sv+gdIvKJRpxey5OUrWpNHEnsj6uUOqBzd0yMxnthASa092eIzVDmQvNU/8K delKRE3BlXck9rYVzVhU8C7nj/Qx9oT5GaMmfxk7znT2GWxgVN0PxdmmkzpHSdFNVnG8 gHaAEAXyAZlww7RzkugU6TGzhU8GOk4WXEdcFrPVLf44g+hoDqXYWPDrT1rAK1QPdC4E PZuKkSXwTOLbLOSrz7DA9xgvfErLyV4qoWxfI8b68M11kG53O0Jcmj1u/nGLknrHad2E ZLGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=a04yZNaFZZKzr12GmZavPAqQee5gfGxzl24VWbE7fno=; b=IqlP3JUpCvIubu6mucgvJ0wz1hezPueoX4WpXtb4lY+bqzhQyjQ3UiqUsRzD68RiWo Tv6t+yLeRj1fhRgKTTVz9til7Xk3cdL0aUqA73WhmMwuVEAKP6WxR35nB9Wn+O9nb5KO pbl2qy9T9NpzAnZKsD7G9UYx2/RgniDKsTVHSLN1rcAAjKdaeYikbVxUK/dJMWb0g6V4 ULPzmz0Ge4Ckq3NTwKq2V8jSPDWMrP5NsjaLg9Up5yN8hp8R71e1VYnqtOe408urlrQa wkd8/R8/C1xi/qtoTucynLpgo6YhkmebsHPRnY6KNizk9f48HPCjWXgr4Er/rE+qYYAp 4ryg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=cjwiK+WI; 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=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k19si7190861otb.279.2020.04.05.21.28.13; Sun, 05 Apr 2020 21:28:25 -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; dkim=pass header.i=@posteo.net header.s=2017 header.b=cjwiK+WI; 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=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726504AbgDFE1Z (ORCPT + 99 others); Mon, 6 Apr 2020 00:27:25 -0400 Received: from mout02.posteo.de ([185.67.36.66]:46245 "EHLO mout02.posteo.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725856AbgDFE1Z (ORCPT ); Mon, 6 Apr 2020 00:27:25 -0400 Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id 3E1952400FD for ; Mon, 6 Apr 2020 06:27:22 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1586147242; bh=70a++uSvv2qdkM5jAGcOFLiP/g5Cl2dfP85Qllm6F34=; h=Date:From:To:Cc:Subject:From; b=cjwiK+WItkR04byOQUNyd7wqZowBinT5qssQZXriTReqtQEsBM7hsP7gJGWIAm8kQ vngdpkbEcPaIoX6gF2fpkNd1Esm1SuvTRspv5xY1h/8e7hEcya9JGbYA5Mzi6Tw4+H iHvobzW/lzwrcJArEWIipG8Yb+7ohdlyrmrDw84lUNwIetkR4KEnRKzkD/wnBcnWow EPJIKbgy/f1O7ueEa7UxPM2ZPHIJVNdNTd009GYOGuXwnD8jYA8C4/8tak3aphDOEW 7DpKMkJbZEVj+5yxHSRvJNihbzMPa6BpmfUP4DbK8cqPWrjvcS7jJYgGEhsO9d2mM9 kX/DN7XJ+JKrg== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 48wcvN4HXhz9rxN; Mon, 6 Apr 2020 06:27:12 +0200 (CEST) Date: Mon, 6 Apr 2020 00:27:09 -0400 From: Kevyn-Alexandre =?utf-8?B?UGFyw6k=?= To: Alex Belits Cc: "frederic@kernel.org" , "rostedt@goodmis.org" , "mingo@kernel.org" , "peterz@infradead.org" , "linux-kernel@vger.kernel.org" , Prasun Kapoor , "tglx@linutronix.de" , "linux-api@vger.kernel.org" , "catalin.marinas@arm.com" , "linux-arm-kernel@lists.infradead.org" , "netdev@vger.kernel.org" , "davem@davemloft.net" , "linux-arch@vger.kernel.org" , "will@kernel.org" Subject: Re: [PATCH v2 10/12] task_isolation: ringbuffer: don't interrupt CPUs running isolated tasks on buffer resize Message-ID: <20200406042709.e4isjrvrrwsusjc4@x1> References: <4473787e1b6bc3cc226067e8d122092a678b63de.camel@marvell.com> <5add46d3bfbdac3fb42dcef6b6e4ea0e39abe11f.camel@marvell.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <5add46d3bfbdac3fb42dcef6b6e4ea0e39abe11f.camel@marvell.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Mar 08, 2020 at 03:55:24AM +0000, Alex Belits wrote: > From: Yuri Norov > > CPUs running isolated tasks are in userspace, so they don't have to > perform ring buffer updates immediately. If ring_buffer_resize() > schedules the update on those CPUs, isolation is broken. To prevent > that, updates for CPUs running isolated tasks are performed locally, > like for offline CPUs. > > A race condition between this update and isolation breaking is avoided > at the cost of disabling per_cpu buffer writing for the time of update > when it coincides with isolation breaking. > > Signed-off-by: Yuri Norov > [abelits@marvell.com: updated to prevent race with isolation breaking] > Signed-off-by: Alex Belits > --- > kernel/trace/ring_buffer.c | 62 ++++++++++++++++++++++++++++++++++---- > 1 file changed, 56 insertions(+), 6 deletions(-) > > diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c > index 61f0e92ace99..593effe40183 100644 > --- a/kernel/trace/ring_buffer.c > +++ b/kernel/trace/ring_buffer.c > @@ -21,6 +21,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -1701,6 +1702,37 @@ static void update_pages_handler(struct work_struct *work) > complete(&cpu_buffer->update_done); > } > > +static bool update_if_isolated(struct ring_buffer_per_cpu *cpu_buffer, > + int cpu) > +{ > + bool rv = false; > + > + if (task_isolation_on_cpu(cpu)) { > + /* > + * CPU is running isolated task. Since it may lose > + * isolation and re-enter kernel simultaneously with > + * this update, disable recording until it's done. > + */ > + atomic_inc(&cpu_buffer->record_disabled); > + /* Make sure, update is done, and isolation state is current */ > + smp_mb(); > + if (task_isolation_on_cpu(cpu)) { > + /* > + * If CPU is still running isolated task, we > + * can be sure that breaking isolation will > + * happen while recording is disabled, and CPU > + * will not touch this buffer until the update > + * is done. > + */ > + rb_update_pages(cpu_buffer); > + cpu_buffer->nr_pages_to_update = 0; > + rv = true; > + } > + atomic_dec(&cpu_buffer->record_disabled); > + } > + return rv; > +} > + > /** > * ring_buffer_resize - resize the ring buffer > * @buffer: the buffer to resize. > @@ -1784,13 +1816,22 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, > if (!cpu_buffer->nr_pages_to_update) > continue; > > - /* Can't run something on an offline CPU. */ > + /* > + * Can't run something on an offline CPU. > + * > + * CPUs running isolated tasks don't have to > + * update ring buffers until they exit > + * isolation because they are in > + * userspace. Use the procedure that prevents > + * race condition with isolation breaking. > + */ > if (!cpu_online(cpu)) { > rb_update_pages(cpu_buffer); > cpu_buffer->nr_pages_to_update = 0; > } else { > - schedule_work_on(cpu, > - &cpu_buffer->update_pages_work); > + if (!update_if_isolated(cpu_buffer, cpu)) > + schedule_work_on(cpu, > + &cpu_buffer->update_pages_work); > } > } > > @@ -1829,13 +1870,22 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, > > get_online_cpus(); > > - /* Can't run something on an offline CPU. */ > + /* > + * Can't run something on an offline CPU. > + * > + * CPUs running isolated tasks don't have to update > + * ring buffers until they exit isolation because they > + * are in userspace. Use the procedure that prevents > + * race condition with isolation breaking. > + */ > if (!cpu_online(cpu_id)) > rb_update_pages(cpu_buffer); > else { > - schedule_work_on(cpu_id, > + if (!update_if_isolated(cpu_buffer, cpu_id)) > + schedule_work_on(cpu_id, > &cpu_buffer->update_pages_work); > - wait_for_completion(&cpu_buffer->update_done); > + wait_for_completion(&cpu_buffer->update_done); > + } > } > > cpu_buffer->nr_pages_to_update = 0; gcc output: kernel/trace/ring_buffer.c: In function 'ring_buffer_resize': kernel/trace/ring_buffer.c:1884:4: warning: this 'if' clause does not guard... [-Wmisleading-indentation] if (!update_if_isolated(cpu_buffer, cpu_id)) ^~ kernel/trace/ring_buffer.c:1887:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if' wait_for_completion(&cpu_buffer->update_done); ^~~~~~~~~~~~~~~~~~~ kernel/trace/ring_buffer.c:1858:4: error: label 'out' used but not defined goto out; ^~~~ kernel/trace/ring_buffer.c:1868:4: error: label 'out_err' used but not defined goto out_err; ^~~~ My fix: diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 593effe40183..8b458400ac31 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1881,9 +1881,8 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, if (!cpu_online(cpu_id)) rb_update_pages(cpu_buffer); else { - if (!update_if_isolated(cpu_buffer, cpu_id)) - schedule_work_on(cpu_id, - &cpu_buffer->update_pages_work); + if (!update_if_isolated(cpu_buffer, cpu_id)) { + schedule_work_on(cpu_id, &cpu_buffer->update_pages_work); wait_for_completion(&cpu_buffer->update_done); } } thx, -- Kevyn-Alexandre Par?