From patchwork Wed Aug 29 12:17:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Motai.Hirotaka@aj.MitsubishiElectric.co.jp" X-Patchwork-Id: 10580163 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E2B09174A for ; Wed, 29 Aug 2018 12:38:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5B9382AA06 for ; Wed, 29 Aug 2018 12:38:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4FDB12AF34; Wed, 29 Aug 2018 12:38:01 +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=-5.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CCF692AA06 for ; Wed, 29 Aug 2018 12:38:00 +0000 (UTC) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id CDE9AD13; Wed, 29 Aug 2018 12:36:39 +0000 (UTC) X-Original-To: ltsi-dev@lists.linuxfoundation.org Delivered-To: ltsi-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 3DC0FCD3 for ; Wed, 29 Aug 2018 12:36:37 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mx05.melco.co.jp (mx05.melco.co.jp [192.218.140.145]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id E3C544FA for ; Wed, 29 Aug 2018 12:36:35 +0000 (UTC) Received: from mr05.melco.co.jp (mr05 [133.141.98.165]) by mx05.melco.co.jp (Postfix) with ESMTP id B03A53A2839 for ; Wed, 29 Aug 2018 21:17:25 +0900 (JST) Received: from mr05.melco.co.jp (unknown [127.0.0.1]) by mr05.imss (Postfix) with ESMTP id 420l4P50QWzRkCl for ; Wed, 29 Aug 2018 21:17:25 +0900 (JST) Received: from mf04_second.melco.co.jp (unknown [192.168.20.184]) by mr05.melco.co.jp (Postfix) with ESMTP id 420l4P4hb5zRkC3 for ; Wed, 29 Aug 2018 21:17:25 +0900 (JST) Received: from mf04.melco.co.jp (unknown [133.141.98.184]) by mf04_second.melco.co.jp (Postfix) with ESMTP id 420l4P4jktzRkCw for ; Wed, 29 Aug 2018 21:17:25 +0900 (JST) Received: from JPN01-OS2-obe.outbound.protection.outlook.com (unknown [23.103.139.147]) by mf04.melco.co.jp (Postfix) with ESMTP id 420l4P4DmYzRkCn for ; Wed, 29 Aug 2018 21:17:25 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mitsubishielectricgroup.onmicrosoft.com; s=selector1-mitsubishielectricgroup-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7HuiSFDrHX4B+yFvpAnZ1CXlFXSb19uOoBm3pqcCa18=; b=WJ0q/sY7g5YAFEi3rLFdIeaAxnb/jTPt8EuphbhfcHEq1A4BPm3O9h5Dsl8dNLbaRNq8rRE/lZB1IlzCADDU8EjQV7xr42vY+NgVzO6wFupIZQl6MIgiJmJTsfUnk7GD/E1Kyt6+i6U63gAa/NChpghfh0M41K/u0HiMfImGFno= Received: from TY1PR01MB1692.jpnprd01.prod.outlook.com (52.133.160.145) by TY1PR01MB1231.jpnprd01.prod.outlook.com (10.174.226.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1080.17; Wed, 29 Aug 2018 12:17:24 +0000 Received: from TY1PR01MB1692.jpnprd01.prod.outlook.com ([fe80::d04e:e6f6:c782:22fa]) by TY1PR01MB1692.jpnprd01.prod.outlook.com ([fe80::d04e:e6f6:c782:22fa%2]) with mapi id 15.20.1080.015; Wed, 29 Aug 2018 12:17:24 +0000 From: "Motai.Hirotaka@aj.MitsubishiElectric.co.jp" To: "ltsi-dev@lists.linuxfoundation.org" Thread-Topic: [LTSI-dev] [PATCH 09/54] ring-buffer: Rewrite trace_recursive_(un)lock() to be simpler Thread-Index: AdQ/icqNcGDNms7fSmu4I9yWnWSeQg== Date: Wed, 29 Aug 2018 12:17:05 +0000 Deferred-Delivery: Wed, 29 Aug 2018 12:15:00 +0000 Message-ID: Accept-Language: ja-JP, en-US Content-Language: ja-JP X-MS-Has-Attach: X-MS-TNEF-Correlator: x-melpop: 1 authentication-results: spf=none (sender IP is ) smtp.mailfrom=Motai.Hirotaka@aj.MitsubishiElectric.co.jp; x-originating-ip: [153.231.200.201] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; TY1PR01MB1231; 6:0hSGYvfLC4mmtU9tcIo77rGsP59OvgtQu7UXH88SNYKylWGOeHVgpFKoXW4HIVl2qhZte+FAZJUCnpdF78eoeQ+EM6BG40mrjSm1ZOWoFhmU7qma4A8Up2g7qTDdW4yzk+pQWUOLw+myZeYWlZj/jTTDpC6srdYUqNf5k99O2FEE8wewAyOfNI8sfQXnsTbSgcjEtsDssbAPh4KY+ZnFecbnUCc0d0Mt4rrOueuxUt5s+wWjEZS4+0ctol17GIon1vvBUBmYvnwimzosont7RxexbMarkKvwAY2za2YKcujeMxhmtleVTRICvjudzm/TttraJT8CH8GTvATtBiAhvYrN/I/w3n9yX45qA4clRGoT4O0itiIflRtvcOJw5utcJXVxyOer57fpNhO8koxvYKpsKOp6IskSgZCAZaH9CPLIA1x3PLmdEufbrrnxZs1nAWkaFg76QBneL71ruA1qMA==; 5:jWyI3hKc4PjpYdKlGq0Ng9XCyzJMhRMbFWsRsmTp0qN4JEcjsTiUg0aNGxSovkGuzJKMLyjkyLNU8pIfwjkUsFy3E+SdmLgQXRA8WRlz2ETBQrfZyE17xExFhNCFw0EVmErDnRwxuQsERkrYCs73ajVtX3kgxtnzmIdyBWhXWiY=; 7:LPCEhCCCXkSApP4gGpZFWdmhDbL00zyQCKLTjTmbTgIJStTQXBVejDI6oQLSfGulGCe7jpQTbDq0UQYcCQ4ljxbLOa1poBx7PwrooQujP+hGezaU7IM2hT0UqTZQ9soE/RMgMMTzWCgPwSH3vypHipJcPaYuZ9M6rqIxe3ctF94mT8tzfPiO14Sjs+Os4FzbpjWzaubewazdk97aKpeyx2SFjVz23YDoIPYgd1HdURBpMU+m1Cvp/OSursr1WH7v x-ms-office365-filtering-correlation-id: d74af703-6f5e-40d7-0b7f-08d60da96125 x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989137)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(5600074)(711020)(2017052603328)(7153060)(7193020); SRVR:TY1PR01MB1231; x-ms-traffictypediagnostic: TY1PR01MB1231: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040522)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(10201501046)(3231311)(944501410)(52105095)(149027)(150027)(6041310)(20161123558120)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123560045)(201708071742011)(7699016); SRVR:TY1PR01MB1231; BCL:0; PCL:0; RULEID:; SRVR:TY1PR01MB1231; x-forefront-prvs: 077929D941 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(136003)(396003)(366004)(376002)(346002)(39860400002)(189003)(199004)(478600001)(105586002)(2906002)(14454004)(33656002)(66066001)(72206003)(6116002)(3846002)(97736004)(2900100001)(2501003)(5250100002)(74482002)(25786009)(7736002)(9686003)(5660300001)(6506007)(26005)(6666003)(6916009)(2351001)(81156014)(106356001)(86362001)(6436002)(5640700003)(74316002)(8676002)(1857600001)(81166006)(102836004)(53936002)(7696005)(68736007)(55016002)(305945005)(256004)(14444005)(316002)(186003)(8936002)(486006)(99286004)(476003); DIR:OUT; SFP:1102; SCL:1; SRVR:TY1PR01MB1231; H:TY1PR01MB1692.jpnprd01.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:0; received-spf: None (protection.outlook.com: aj.MitsubishiElectric.co.jp does not designate permitted sender hosts) x-microsoft-antispam-message-info: BThq+jF+eUd86/rA9FkEs9f4VPx7ChT0w7sLC4VQZEC0Y+nutxWgDYdKh5UJHeI9/eUkfo/5Sfakj6N0gNLp+gFuS42Rqocm8SM6HHQDCh0Ujccgq5W2mS+G74uvXm0f5zwprbDrSQLT3M8x7WKYT/EjqgQLlFAKYCedQZGQpOrfKeNO8vjxuWwIiQAsVmb8EdjjJPLw2tmpCnHaImOe7DGyxtgQVBfxyT7KietC4/vUANVWim9AAm1q7e8kopb212mcTUFEHABPw4M39xxrMXnL9IqvJnh/24qp6HhDaYKq2uEWrBh9e8cl6i4Zrir6cq68SIahqHfO/foIaXNWU1cYRE7a9vz61xSsyzE/7TU= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: aj.MitsubishiElectric.co.jp X-MS-Exchange-CrossTenant-Network-Message-Id: d74af703-6f5e-40d7-0b7f-08d60da96125 X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Aug 2018 12:17:22.6277 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: c5a75b62-4bff-4c96-a720-6621ce9978e5 X-MS-Exchange-Transport-CrossTenantHeadersStamped: TY1PR01MB1231 Subject: [LTSI-dev] [PATCH 09/54] ring-buffer: Rewrite trace_recursive_(un)lock() to be simpler X-BeenThere: ltsi-dev@lists.linuxfoundation.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: "A list to discuss patches, development, and other things related to the LTSI project" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: ltsi-dev-bounces@lists.linuxfoundation.org Errors-To: ltsi-dev-bounces@lists.linuxfoundation.org X-Virus-Scanned: ClamAV using ClamSMTP The current method to prevent the ring buffer from entering into a recursize loop is to use a bitmask and set the bit that maps to the current context (normal, softirq, irq or NMI), and if that bit was already set, it is considered a recursive loop. New code is being added that may require the ring buffer to be entered a second time in the current context. The recursive locking prevents that from happening. Instead of mapping a bitmask to the current context, just allow 4 levels of nesting in the ring buffer. This matches the 4 context levels that it can already nest. It is highly unlikely to have more than two levels, thus it should be fine when we add the second entry into the ring buffer. If that proves to be a problem, we can always up the number to 8. An added benefit is that reading preempt_count() to get the current level adds a very slight but noticeable overhead. This removes that need. Signed-off-by: Steven Rostedt (VMware) (cherry picked from commit 1a149d7d3f45d311da1f63473736c05f30ae8a75) Signed-off-by: Hirotaka MOTAI --- kernel/trace/ring_buffer.c | 64 ++++++++++---------------------------- 1 file changed, 17 insertions(+), 47 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index fd780900..c3b6b47f 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -2540,79 +2540,49 @@ rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer) irq_work_queue(&cpu_buffer->irq_work.work); } } /* * The lock and unlock are done within a preempt disable section. * The current_context per_cpu variable can only be modified * by the current task between lock and unlock. But it can - * be modified more than once via an interrupt. To pass this - * information from the lock to the unlock without having to - * access the 'in_interrupt()' functions again (which do show - * a bit of overhead in something as critical as function tracing, - * we use a bitmask trick. + * be modified more than once via an interrupt. There are four + * different contexts that we need to consider. * - * bit 0 = NMI context - * bit 1 = IRQ context - * bit 2 = SoftIRQ context - * bit 3 = normal context. + * Normal context. + * SoftIRQ context + * IRQ context + * NMI context * - * This works because this is the order of contexts that can - * preempt other contexts. A SoftIRQ never preempts an IRQ - * context. - * - * When the context is determined, the corresponding bit is - * checked and set (if it was set, then a recursion of that context - * happened). - * - * On unlock, we need to clear this bit. To do so, just subtract - * 1 from the current_context and AND it to itself. - * - * (binary) - * 101 - 1 = 100 - * 101 & 100 = 100 (clearing bit zero) - * - * 1010 - 1 = 1001 - * 1010 & 1001 = 1000 (clearing bit 1) - * - * The least significant bit can be cleared this way, and it - * just so happens that it is the same bit corresponding to - * the current context. + * If for some reason the ring buffer starts to recurse, we + * only allow that to happen at most 4 times (one for each + * context). If it happens 5 times, then we consider this a + * recusive loop and do not let it go further. */ static __always_inline int trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer) { - unsigned int val = cpu_buffer->current_context; - int bit; - - if (in_interrupt()) { - if (in_nmi()) - bit = RB_CTX_NMI; - else if (in_irq()) - bit = RB_CTX_IRQ; - else - bit = RB_CTX_SOFTIRQ; - } else - bit = RB_CTX_NORMAL; - - if (unlikely(val & (1 << bit))) + if (cpu_buffer->current_context >= 4) return 1; - val |= (1 << bit); - cpu_buffer->current_context = val; + cpu_buffer->current_context++; + /* Interrupts must see this update */ + barrier(); return 0; } static __always_inline void trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer) { - cpu_buffer->current_context &= cpu_buffer->current_context - 1; + /* Don't let the dec leak out */ + barrier(); + cpu_buffer->current_context--; } /** * ring_buffer_unlock_commit - commit a reserved * @buffer: The buffer to commit to * @event: The event pointer to commit. * * This commits the data to the ring buffer, and releases any locks held.