From patchwork Sat Dec 16 04:22:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179805 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30005dyi; Fri, 15 Dec 2023 20:22:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IHGjolT4C9XE+ssehn0N1HiroN7KzgVO+jq89F7LA2/l1NEbIf3Y0V3dsAwe+2U8OqTM9t7 X-Received: by 2002:a17:907:86a4:b0:a19:a1ba:da56 with SMTP id qa36-20020a17090786a400b00a19a1bada56mr8273544ejc.125.1702700569404; Fri, 15 Dec 2023 20:22:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700569; cv=none; d=google.com; s=arc-20160816; b=yBsctLSdq7uu1+YWFbEH5FZHgGziiJNpK5Cwq60OF/cisiuVxXOdMr7ERwO3YO/eZ6 7yZi0tc+P51neUdipvw5tNbskCqlVOTe0JGZI4cEp/aqJl/b5Jf/+BaeHxqIwKO26AeW eeDTVs4QmGN86Gkmas4ZnW4Rq2kNZ2xtyp0rhPpEYcm1wD0SZ5hRQHPzDvJ4TBJxXxhG hyVO3Jxs0IrFs/Cw0yuXUM7psODPc8JTD3KztxlEP3u0Leyy2uB3leHUpRLMfMtVXbJI a6fchrBsMlhPvUk+vmn9rQqKePra/qNGfr/dXxn0pHBrWCQnXXcaFLibnftOY/qyfSyU 1fZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=HT4wNZr9czdlr7TI02hEBQv8zl2xOnGyKnRbzLu7erQ=; fh=hu/11vjhAVGN2soHkDrejjtKeeIeHno3fFH7CvZIC0Y=; b=rS5944ld/c40QLaXsz/EdhG7XqJUDIKzMPlMDcyYpAyX6u2kelHyktHWtUgeIk041P 3yYatKAfaJibL/k7nkjoQ+DExAJuzcaDXmAwyVxQSWQKfFmZpmyhDaLKa3e5a8UrvhgK slDuzPHi9lr6wBBw3kCJJJDZ3H/y15Iwu8I1hnBOEYEI2ic6vcqyN0B+GTL3e5pHcLqW y6CgN78Gt32gkiIT+EOwxXOGO0dVhKhMLrAnWZ1nyiTwQjYH+SKmaVfwvq4bF0iyXCP0 wjvhgGIzK3mD7ZX725vRK2QYg58Zo6p3I2+62tgns0sdUWyQ7EhRWc9ROh86qMZy/Agm Lpag== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2044-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2044-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id h4-20020a1709060f4400b009fc22483156si7971617ejj.457.2023.12.15.20.22.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:22:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2044-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2044-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2044-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0AE901F257D6 for ; Sat, 16 Dec 2023 04:22:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2813E14AAD; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F33610A06; Sat, 16 Dec 2023 04:21:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29489C433CA; Sat, 16 Dec 2023 04:21:51 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCI-00000002yAm-0etS; Fri, 15 Dec 2023 23:22:42 -0500 Message-ID: <20231216042241.937275443@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:15 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org Subject: [for-linus][PATCH 01/15] ring-buffer: Fix writing to the buffer with max_data_size References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785410952439982056 X-GMAIL-MSGID: 1785410952439982056 From: "Steven Rostedt (Google)" The maximum ring buffer data size is the maximum size of data that can be recorded on the ring buffer. Events must be smaller than the sub buffer data size minus any meta data. This size is checked before trying to allocate from the ring buffer because the allocation assumes that the size will fit on the sub buffer. The maximum size was calculated as the size of a sub buffer page (which is currently PAGE_SIZE minus the sub buffer header) minus the size of the meta data of an individual event. But it missed the possible adding of a time stamp for events that are added long enough apart that the event meta data can't hold the time delta. When an event is added that is greater than the current BUF_MAX_DATA_SIZE minus the size of a time stamp, but still less than or equal to BUF_MAX_DATA_SIZE, the ring buffer would go into an infinite loop, looking for a page that can hold the event. Luckily, there's a check for this loop and after 1000 iterations and a warning is emitted and the ring buffer is disabled. But this should never happen. This can happen when a large event is added first, or after a long period where an absolute timestamp is prefixed to the event, increasing its size by 8 bytes. This passes the check and then goes into the algorithm that causes the infinite loop. For events that are the first event on the sub-buffer, it does not need to add a timestamp, because the sub-buffer itself contains an absolute timestamp, and adding one is redundant. The fix is to check if the event is to be the first event on the sub-buffer, and if it is, then do not add a timestamp. This also fixes 32 bit adding a timestamp when a read of before_stamp or write_stamp is interrupted. There's still no need to add that timestamp if the event is going to be the first event on the sub buffer. Also, if the buffer has "time_stamp_abs" set, then also check if the length plus the timestamp is greater than the BUF_MAX_DATA_SIZE. Link: https://lore.kernel.org/all/20231212104549.58863438@gandalf.local.home/ Link: https://lore.kernel.org/linux-trace-kernel/20231212071837.5fdd6c13@gandalf.local.home Link: https://lore.kernel.org/linux-trace-kernel/20231212111617.39e02849@gandalf.local.home Cc: stable@vger.kernel.org Cc: Mark Rutland Cc: Mathieu Desnoyers Fixes: a4543a2fa9ef3 ("ring-buffer: Get timestamp after event is allocated") Fixes: 58fbc3c63275c ("ring-buffer: Consolidate add_timestamp to remove some branches") Reported-by: Kent Overstreet # (on IRC) Acked-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 8d2a4f00eca9..b8986f82eccf 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -3579,7 +3579,10 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, * absolute timestamp. * Don't bother if this is the start of a new page (w == 0). */ - if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) { + if (!w) { + /* Use the sub-buffer timestamp */ + info->delta = 0; + } else if (unlikely(!a_ok || !b_ok || info->before != info->after)) { info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND; info->length += RB_LEN_TIME_EXTEND; } else { @@ -3737,6 +3740,8 @@ rb_reserve_next_event(struct trace_buffer *buffer, if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) { add_ts_default = RB_ADD_STAMP_ABSOLUTE; info.length += RB_LEN_TIME_EXTEND; + if (info.length > BUF_MAX_DATA_SIZE) + goto out_fail; } else { add_ts_default = RB_ADD_STAMP_NONE; } From patchwork Sat Dec 16 04:22:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179806 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30067dyi; Fri, 15 Dec 2023 20:23:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IERsr2y1mYEjnkfvfumEqomi154rghn3eWs8BAk7nh9HnrSwnZySgKjJlEphN593+9NW+Lp X-Received: by 2002:a0c:d788:0:b0:67a:9cd5:1178 with SMTP id z8-20020a0cd788000000b0067a9cd51178mr11691654qvi.14.1702700581157; Fri, 15 Dec 2023 20:23:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700581; cv=none; d=google.com; s=arc-20160816; b=p4y/VeUuqFFBfm6q5ECK+Ydy4ZfDZHHEE8iXKPu51LcroN/b7mjdniNic568x+p/zi 6pJtmtjns6PjoMxLZrbWUIWf3rWSwuTsaN9ln5M8AWyaqGrnoYnw5f1e2IO+EqWVKjkP FOkoEyggnAKcJTxPyVePhX1PssB9QVpCilEmMnRd1qJvQXDKZjS9Xu+Hs2hkWqPz+mWo DxH75uAgRX6LxaWgp+IDZXL37t9Z3cOCq6qSaQiK+9h7c6U/wet8Nkjb3xZW0eOOSbWN lWzzREZtFaX3x3NlmPVElnCJnUnFADCCdKfswUZ8/GwbxcysMpQjSTN5l44BjrS1C1LC 3jHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=39ZeLZel1/Hwh9780/8ttdp7FL587hxok/hPGlKdclI=; fh=mTYg3JJ8i2GBNLFt8qb4bgYwOyk0kmrOzICLbi6qJ1c=; b=mLKpjULmgsuxctB7eIt3QHWnEjI3CuhWikBBaN5i7/hk2QBBVTu0Q1Y2Su2lZEBeHA JCUkX0IiYqvkrRb6N8WmMgcVdU1UkzSaFbXTshtt0tyfd52PCaXCMm4RyQ2qAkLkc5NM H05d1rowqB/zuaNLbk44FlAenjmHss0oVOveucB5DY2Qa5NrjjQ564Do8cCRs1KOPn4P cveZ8DQoTdZfQyQ3ZRbkb3rDMOuUSi0wrcffjtqvWv0/vzRL2RpTIx2IB0NZYEvsg7Qq ILZm+cY+mOqB+X2GlpQPl+pWscGRKZrH7vj62/gPO6ay98coG0cKrKv5bU+s54JwBPE1 5jQg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2045-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2045-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id q10-20020a0c8b0a000000b0067f0a7488basi5065913qva.290.2023.12.15.20.23.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:23:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2045-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2045-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2045-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E9C3D1C212C0 for ; Sat, 16 Dec 2023 04:23:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B52D16409; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D424A10A31 for ; Sat, 16 Dec 2023 04:21:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 632C3C433C8; Sat, 16 Dec 2023 04:21:51 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCI-00000002yBH-1rhM; Fri, 15 Dec 2023 23:22:42 -0500 Message-ID: <20231216042242.232509644@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:16 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [for-linus][PATCH 02/15] tracing: Have large events show up as [LINE TOO BIG] instead of nothing References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785410964501839489 X-GMAIL-MSGID: 1785410964501839489 From: "Steven Rostedt (Google)" If a large event was added to the ring buffer that is larger than what the trace_seq can handle, it just drops the output: ~# cat /sys/kernel/tracing/trace # tracer: nop # # entries-in-buffer/entries-written: 2/2 #P:8 # # _-----=> irqs-off/BH-disabled # / _----=> need-resched # | / _---=> hardirq/softirq # || / _--=> preempt-depth # ||| / _-=> migrate-disable # |||| / delay # TASK-PID CPU# ||||| TIMESTAMP FUNCTION # | | | ||||| | | <...>-859 [001] ..... 141.118951: tracing_mark_write <...>-859 [001] ..... 141.148201: tracing_mark_write: 78901234 Instead, catch this case and add some context: ~# cat /sys/kernel/tracing/trace # tracer: nop # # entries-in-buffer/entries-written: 2/2 #P:8 # # _-----=> irqs-off/BH-disabled # / _----=> need-resched # | / _---=> hardirq/softirq # || / _--=> preempt-depth # ||| / _-=> migrate-disable # |||| / delay # TASK-PID CPU# ||||| TIMESTAMP FUNCTION # | | | ||||| | | <...>-852 [001] ..... 121.550551: tracing_mark_write[LINE TOO BIG] <...>-852 [001] ..... 121.550581: tracing_mark_write: 78901234 This now emulates the same output as trace_pipe. Link: https://lore.kernel.org/linux-trace-kernel/20231209171058.78c1a026@gandalf.local.home Cc: Mark Rutland Cc: Mathieu Desnoyers Reviewed-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index fbcd3bafb93e..aa8f99f3e5de 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -4722,7 +4722,11 @@ static int s_show(struct seq_file *m, void *v) iter->leftover = ret; } else { - print_trace_line(iter); + ret = print_trace_line(iter); + if (ret == TRACE_TYPE_PARTIAL_LINE) { + iter->seq.full = 0; + trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n"); + } ret = trace_print_seq(m, &iter->seq); /* * If we overflow the seq_file buffer, then it will From patchwork Sat Dec 16 04:22:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179807 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30107dyi; Fri, 15 Dec 2023 20:23:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHwzapbOT+Rb88TUI3N2uIlWQbkiYaL3HgVtkxN06PIlNGadgwX3vxAKJbDWnmYPd6jvfQP X-Received: by 2002:a05:651c:11d0:b0:2cc:2f77:a5fb with SMTP id z16-20020a05651c11d000b002cc2f77a5fbmr3063945ljo.31.1702700591318; Fri, 15 Dec 2023 20:23:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700591; cv=none; d=google.com; s=arc-20160816; b=QuS/h7EYjveo4KusRY/8UBpsLbN9ACCUAfZOs9xJwrbb8YztHeMTR02+5WEkiYwqIg IY1jAnsEu15nmYd0KNw6icknrYWrFpTC01uLymrklVd9nr0Pt1AM8AAvvX/e2COMhDik 9FT6xnQl77QzdlVEh1kdqDg6Q2uH3Wo1z1gxr7SizQnT9cZDjcXdJt+1M5SgI9WHCewA ZB1CiIaEGbvAPBwyQPoEA0mVPZKWJTaoy/1PRO9vxf7ollU0DZ2ZnPqSmv/3h/fUITvC CiZuebeSiIpPJShw4Btr6X3fOb3gPEQyTqv2AKqGjWZVWJxBLC4MjKofpTkvwNr2SsMV JXBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=c0KZf5RT80r58fVl6ue2FjRINMhaweT4HjI0Qw8IK6M=; fh=sbDUv6PuKPAkpuv6zbp1fPCjJZUEufc8KwKtgDDsdwc=; b=Cbgqvr7Czn5D3tp7byTrM8KUSLlEuPUXNn5ZLyiExxoVR/R9peultZ11WVraYosNyg NQLb6Lpe0Oh9hPhPClT4JBoERhM30qj2EMNlnFuD3+tGYd0Uwu8bpMGRxDAbf1HAjJNK 3/QyRbuxJW2dC1S/0Qhi5SBKJNOx/+1ONS+X99EfXoWENFfOfhHnc3M4tiR9UXWOblCG a0XuMRy5vy0KH70dCJQhiW+QWNQsAU6YkNImqoKX3RWUul/BJNUOHQ0VSQjISr9eAMHi Da2Gcy9h3Vc9kZr7BMRsl+cNw2GlNBO21ldYIJa7n7QUnpWR205wXsdNJ2ClBrhBdXyg ZvAQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2046-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2046-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id b8-20020a056402350800b0054cae753509si7948902edd.412.2023.12.15.20.23.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:23:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2046-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2046-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2046-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DB8871F25A4E for ; Sat, 16 Dec 2023 04:23:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A06DA18042; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18CB51119E for ; Sat, 16 Dec 2023 04:21:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4E80C433C7; Sat, 16 Dec 2023 04:21:51 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCI-00000002yBl-336s; Fri, 15 Dec 2023 23:22:42 -0500 Message-ID: <20231216042242.519691764@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:17 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Beau Belgrave Subject: [for-linus][PATCH 03/15] eventfs: Fix events beyond NAME_MAX blocking tasks References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785410975498526704 X-GMAIL-MSGID: 1785410975498526704 From: Beau Belgrave Eventfs uses simple_lookup(), however, it will fail if the name of the entry is beyond NAME_MAX length. When this error is encountered, eventfs still tries to create dentries instead of skipping the dentry creation. When the dentry is attempted to be created in this state d_wait_lookup() will loop forever, waiting for the lookup to be removed. Fix eventfs to return the error in simple_lookup() back to the caller instead of continuing to try to create the dentry. Link: https://lore.kernel.org/linux-trace-kernel/20231210213534.497-1-beaub@linux.microsoft.com Fixes: 63940449555e ("eventfs: Implement eventfs lookup, read, open functions") Link: https://lore.kernel.org/linux-trace-kernel/20231208183601.GA46-beaub@linux.microsoft.com/ Signed-off-by: Beau Belgrave Signed-off-by: Steven Rostedt (Google) --- fs/tracefs/event_inode.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 0b90869fd805..43e237864a42 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -546,6 +546,8 @@ static struct dentry *eventfs_root_lookup(struct inode *dir, if (strcmp(ei_child->name, name) != 0) continue; ret = simple_lookup(dir, dentry, flags); + if (IS_ERR(ret)) + goto out; create_dir_dentry(ei, ei_child, ei_dentry, true); created = true; break; @@ -568,6 +570,8 @@ static struct dentry *eventfs_root_lookup(struct inode *dir, if (r <= 0) continue; ret = simple_lookup(dir, dentry, flags); + if (IS_ERR(ret)) + goto out; create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops, true); break; From patchwork Sat Dec 16 04:22:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179808 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30141dyi; Fri, 15 Dec 2023 20:23:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IFhiPWzBjzcTVP88n5BSmBgW4kKObJ3lUJSbmAblZoflRoFu01XpgZAHuO62QN2Eep8JOxV X-Received: by 2002:a2e:3312:0:b0:2cc:5778:eb25 with SMTP id d18-20020a2e3312000000b002cc5778eb25mr463734ljc.15.1702700599074; Fri, 15 Dec 2023 20:23:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700599; cv=none; d=google.com; s=arc-20160816; b=P4fMNPmQUvrlb3S6SsxR8VnKRLeYtezc9oP/tOvnlpNNnNmxWR2Kljm6ruN+JL7a2x mCBigBrU/taWMMH4fXpyJi1CVG7F7KuwaozHagpSuZW+bIO6Uw22oPNA+ErQJuzQK/ry Kn1y2W12f34G+rfGG/7PwrOuVDX5TL418Po0hXSzYeXKBNFwt4CSPhLiz7VBjaVYO/6U jDYmSYIE8wHK0F0a9w+7QZFxp9PQCkD+CpqAkf04w/khJkVszt9ekLlkpLOEkgXDtcmO TAdqcWr8TPxFUeHCcpaEw45mhh5PPRIRhcu08j9mi3GTt35SFzDe0wmur9cxpJw+70h/ u7nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=nTJbPWI5VtfGPBUG6MmVAfxANzE+myniOlxvPctGyUE=; fh=hu/11vjhAVGN2soHkDrejjtKeeIeHno3fFH7CvZIC0Y=; b=SGa4kVHRqBn55lJzOhIyNi8ZlabuIC9ppO+LH/0/Yonlz/1UXO2p5lrDWyiQOk3efW SkF1kLHl7sa+S8PFiGTY+YsR7RRb5bPDe+QuTWlg0vpGZEg5cAgKdAgEf/v9+lRYcb1C sV6fv8QEWhrD65hEmqZ85qhV/Obu9IVjJZDNrmI0YQ7w1U5/bg/oc9Xsj4MPB8geI8ul d3ii3DJY5iWYYiJ1l6pWGgUB48ymDaZWI1BmT8bj//XrAUSzX2ak15cVufDayKmmEOko i8vSLcksqWuxLdsxRl8yo2T/ORxLxw8iZ/kd959FeWGuuGDfgnPu39EDAFQUbotGB9n+ wPSA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2047-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2047-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d2-20020a50cd42000000b0054c6f3c1f00si8451846edj.435.2023.12.15.20.23.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:23:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2047-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2047-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2047-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id AB1291F25A6E for ; Sat, 16 Dec 2023 04:23:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C38E218C08; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 303681171E; Sat, 16 Dec 2023 04:21:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05736C43397; Sat, 16 Dec 2023 04:21:52 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCJ-00000002yCF-03UD; Fri, 15 Dec 2023 23:22:43 -0500 Message-ID: <20231216042242.803616937@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:18 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org Subject: [for-linus][PATCH 04/15] ring-buffer: Fix memory leak of free page References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785410983190327260 X-GMAIL-MSGID: 1785410983190327260 From: "Steven Rostedt (Google)" Reading the ring buffer does a swap of a sub-buffer within the ring buffer with a empty sub-buffer. This allows the reader to have full access to the content of the sub-buffer that was swapped out without having to worry about contention with the writer. The readers call ring_buffer_alloc_read_page() to allocate a page that will be used to swap with the ring buffer. When the code is finished with the reader page, it calls ring_buffer_free_read_page(). Instead of freeing the page, it stores it as a spare. Then next call to ring_buffer_alloc_read_page() will return this spare instead of calling into the memory management system to allocate a new page. Unfortunately, on freeing of the ring buffer, this spare page is not freed, and causes a memory leak. Link: https://lore.kernel.org/linux-trace-kernel/20231210221250.7b9cc83c@rorschach.local.home Cc: stable@vger.kernel.org Cc: Mark Rutland Cc: Mathieu Desnoyers Fixes: 73a757e63114d ("ring-buffer: Return reader page back into existing ring buffer") Acked-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index b8986f82eccf..dcd47895b424 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1787,6 +1787,8 @@ static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) free_buffer_page(bpage); } + free_page((unsigned long)cpu_buffer->free_page); + kfree(cpu_buffer); } From patchwork Sat Dec 16 04:22:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179820 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp32149dyi; Fri, 15 Dec 2023 20:31:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXP4XJbZpZQ9PTjA05hMD1Wg8HZt+NZlB0wWlXYRidDaCe3wt72h2iHQHgeMwr+2pnmyts X-Received: by 2002:a05:6a00:1ad1:b0:6cd:d6c0:d8d0 with SMTP id f17-20020a056a001ad100b006cdd6c0d8d0mr16817039pfv.24.1702701092495; Fri, 15 Dec 2023 20:31:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702701092; cv=none; d=google.com; s=arc-20160816; b=NTjr2P83QcvYxvkIOTtQRfXWvVMH6tGHiPmva3W8c/bhtn3KsjdtYCMtJROsKfgDTA yuLWaz5gqmANxoGdpeIqrJhat8Mkv5F+b1Cs6Iv86NQJcjr0ySxYaiMnJKOQnViQNczW zLDPuYA5dl1eZaqT726WaLLa8az28zaToAOSWoPkgpFXTXuEUBcL8pw2cMvVHZdsN9mD IltIto/+xGQQd9tw8uvDZiOs9lvbNbw69L3t+Beb5841L1SxFYvABgnpWIY5wW15kYCX qwenN3wkcDjv/GRnE0qcsrzTJirT/DKkgvfQI+3p7uOhNfzAZB5X05RD+FEbTvh33a06 tuig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=f3BEfCuOMe1kaaRkUcacI2EbEdvdHGrFXGWyde/tM7g=; fh=hu/11vjhAVGN2soHkDrejjtKeeIeHno3fFH7CvZIC0Y=; b=HqPpmPuEEkci6mn+LByNqotCMPEoFQKjPywvRRAAUaRVUR30IQ6nKV4dI6COqVSVua 6M7LLh5vqxQqRBOnU/eQq9Lyp9Kcl78GQeirVRcMsx5hgwVeRH18gDrC3ERYgM+XlWKQ RV2oP07Xu3Km46UO+fyK7veqSpGVwN22o5Ub/G2n9d1mtNlJPMWW26SdckS3B1oH/DXM UCAIHWphartv0EKW6dzOL6RcjFi6X71HEklODd2kMnX4Q+QUt4krl1De1RRKcg/6foc5 k/j4FNIxpS9Oy8Mm2xJgRAwoDsjLmmfxr33hH3AaEuZJ51ry9Xau+C/zsM/OI2XaTF1t DMhg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2048-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2048-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id b9-20020a056a000cc900b0069100706960si13969623pfv.116.2023.12.15.20.31.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:31:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2048-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2048-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2048-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BEA10283E49 for ; Sat, 16 Dec 2023 04:31:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 036481F610; Sat, 16 Dec 2023 04:21:55 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D117313FEA; Sat, 16 Dec 2023 04:21:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5B4F6C433CD; Sat, 16 Dec 2023 04:21:52 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCJ-00000002yCk-1JRZ; Fri, 15 Dec 2023 23:22:43 -0500 Message-ID: <20231216042243.088375583@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:19 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org Subject: [for-linus][PATCH 05/15] tracing: Update snapshot buffer on resize if it is allocated References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411500808808840 X-GMAIL-MSGID: 1785411500808808840 From: "Steven Rostedt (Google)" The snapshot buffer is to mimic the main buffer so that when a snapshot is needed, the snapshot and main buffer are swapped. When the snapshot buffer is allocated, it is set to the minimal size that the ring buffer may be at and still functional. When it is allocated it becomes the same size as the main ring buffer, and when the main ring buffer changes in size, it should do. Currently, the resize only updates the snapshot buffer if it's used by the current tracer (ie. the preemptirqsoff tracer). But it needs to be updated anytime it is allocated. When changing the size of the main buffer, instead of looking to see if the current tracer is utilizing the snapshot buffer, just check if it is allocated to know if it should be updated or not. Also fix typo in comment just above the code change. Link: https://lore.kernel.org/linux-trace-kernel/20231210225447.48476a6a@rorschach.local.home Cc: stable@vger.kernel.org Cc: Mark Rutland Cc: Mathieu Desnoyers Fixes: ad909e21bbe69 ("tracing: Add internal tracing_snapshot() functions") Reviewed-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index aa8f99f3e5de..6c79548f9574 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -6348,7 +6348,7 @@ static int __tracing_resize_ring_buffer(struct trace_array *tr, if (!tr->array_buffer.buffer) return 0; - /* Do not allow tracing while resizng ring buffer */ + /* Do not allow tracing while resizing ring buffer */ tracing_stop_tr(tr); ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu); @@ -6356,7 +6356,7 @@ static int __tracing_resize_ring_buffer(struct trace_array *tr, goto out_start; #ifdef CONFIG_TRACER_MAX_TRACE - if (!tr->current_trace->use_max_tr) + if (!tr->allocated_snapshot) goto out; ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu); From patchwork Sat Dec 16 04:22:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179809 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30394dyi; Fri, 15 Dec 2023 20:24:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IEFXt5BVrZtA80eg+ieePaDpZshc8bIX6AAk9WXQzc1h2HmZxnNXTiwbehMpIFAookv6Uae X-Received: by 2002:a17:902:704c:b0:1d0:b196:7193 with SMTP id h12-20020a170902704c00b001d0b1967193mr12795900plt.126.1702700678236; Fri, 15 Dec 2023 20:24:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700678; cv=none; d=google.com; s=arc-20160816; b=JFleyFTYnMBviocziYnzmuPw5LWHNfvhQfxIJYILOKnbfmEjXzf+mAn4urbfw5ZlpS reHmXF5MUtb5H/WIi5FSc/7CH7akKGsCNT89gPMoHznEQGJzAdYrDH5z1JUlgfMZD+S4 od01MBG9Aww4KQVtNW7sPmKO2uwMUja9yOy+CixgcIrmp/hSNvvjXvVzOc/86/DSVkuG ewKgEKpWucUqcfT0bobSfUH9iugTPYh3lzsEBlk92blfxAhjVej5W7gfmTGwggooPq4z X9GnYfz/Fz7mm75hvn3wGy2kESu7cjaUpcshbigHnRRpKYSlqnIb9KszviHpuIumqJi4 nxBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=tC9ONrzicci7ob7zZjpSqYN0/ONPK4+bOsNFq3cbzQo=; fh=hu/11vjhAVGN2soHkDrejjtKeeIeHno3fFH7CvZIC0Y=; b=V/KZxnLEKynjaaWnOPOAIFhdisRw3XKd2rafjEBtj+l1plSw+i4hgjLoERiWW/yhUQ 8wJSeDxOpvOb/Q8rF+atiKAhAW7sBdPVPS9tMKB2svNWrv0FSiBCKG6dGTCmmmMGmrgY PWCfwajjFYh7sDS7Qt3iNQzXt1V5qxHY0X9KUY5igRMPdv0r1K4Pom4/2xa44/+qjp7h tztIaChdPnOBeYitCjbPvOBGkPHURM1z6R2QuMp9ngc6hV0EsBMzW3vBINwkqrRP5sUZ oV+LkRMpak6zyh3lXIvpD60GOYJvfaAYBPC3jOEo5ZN2iOa7uxdAZ9zD1xd31mlLJ5iw kMPA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2050-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2050-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id c17-20020a170902d49100b001cfc2da926bsi14634335plg.378.2023.12.15.20.24.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:24:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2050-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2050-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2050-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 9DE8A281DC3 for ; Sat, 16 Dec 2023 04:24:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3499328E14; Sat, 16 Dec 2023 04:21:55 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DEAC14A8A; Sat, 16 Dec 2023 04:21:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A620AC4339A; Sat, 16 Dec 2023 04:21:52 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCJ-00000002yDG-2W0E; Fri, 15 Dec 2023 23:22:43 -0500 Message-ID: <20231216042243.383892237@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:20 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org Subject: [for-linus][PATCH 06/15] ring-buffer: Do not update before stamp when switching sub-buffers References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411066580579803 X-GMAIL-MSGID: 1785411066580579803 From: "Steven Rostedt (Google)" The ring buffer timestamps are synchronized by two timestamp placeholders. One is the "before_stamp" and the other is the "write_stamp" (sometimes referred to as the "after stamp" but only in the comments. These two stamps are key to knowing how to handle nested events coming in with a lockless system. When moving across sub-buffers, the before stamp is updated but the write stamp is not. There's an effort to put back the before stamp to something that seems logical in case there's nested events. But as the current event is about to cross sub-buffers, and so will any new nested event that happens, updating the before stamp is useless, and could even introduce new race conditions. The first event on a sub-buffer simply uses the sub-buffer's timestamp and keeps a "delta" of zero. The "before_stamp" and "write_stamp" are not used in the algorithm in this case. There's no reason to try to fix the before_stamp when this happens. As a bonus, it removes a cmpxchg() when crossing sub-buffers! Link: https://lore.kernel.org/linux-trace-kernel/20231211114420.36dde01b@gandalf.local.home Cc: stable@vger.kernel.org Cc: Mark Rutland Cc: Mathieu Desnoyers Fixes: a389d86f7fd09 ("ring-buffer: Have nested events still record running time stamp") Reviewed-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index dcd47895b424..c7abcc215fe2 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -3607,14 +3607,7 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, /* See if we shot pass the end of this buffer page */ if (unlikely(write > BUF_PAGE_SIZE)) { - /* before and after may now different, fix it up*/ - b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before); - a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after); - if (a_ok && b_ok && info->before != info->after) - (void)rb_time_cmpxchg(&cpu_buffer->before_stamp, - info->before, info->after); - if (a_ok && b_ok) - check_buffer(cpu_buffer, info, CHECK_FULL_PAGE); + check_buffer(cpu_buffer, info, CHECK_FULL_PAGE); return rb_move_tail(cpu_buffer, tail, info); } From patchwork Sat Dec 16 04:22:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179812 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30516dyi; Fri, 15 Dec 2023 20:25:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IHrFKkg0CZjIHIdljLvod+29f4jW5G43r+tAYYPiaAGJX9PQ4FjRXMwuypRssvfv0dmpVOc X-Received: by 2002:a05:6e02:1a0f:b0:35f:6a71:9315 with SMTP id s15-20020a056e021a0f00b0035f6a719315mr10086773ild.23.1702700715094; Fri, 15 Dec 2023 20:25:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700715; cv=none; d=google.com; s=arc-20160816; b=a9KesRg5ogXnIeGfeC1GgOqiL2gwWHMEDxm5VpLW6IAVg8obYubx1/3N0hXp50B/m+ w/NUSiNU0QKf23dZlQyH7KwL2Yy/eculE4BFfsnh50JZ1NecqOEHfjLH9kvwxVr5TUn6 bhsa+TqgzF1kCcMrMUqLKu50I/yBoGaRxfs0lTlvnZiflc+xOWUOsCalDlBPBFydozJU W5a2oSMGnWfDNf1FFp3b87bRMWpslfTJlpkVx6BLwlJVot4tb5ky4mm43+8jVz9fVsNZ ycpTe56SEDENbz9pN46BSQCQ/V34HSsaPm5k7NfJ3h0RxcaHZi+KvUBh8Aylib3vZYfn WgoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=b+WT8rA1IwhmX9oI2JVmEp9pF15eMxqdJmEL2ym8NzY=; fh=hu/11vjhAVGN2soHkDrejjtKeeIeHno3fFH7CvZIC0Y=; b=tErwBg4O9gwmSr2s7wLBYVN/X0M3EqL/10pbjgqW3oy+s68kq1ZKrSQuYzTgL6vJK1 v+9+jqZqn6Uej5HgRlTgm0z4JAiAE/oUeCJ1jWHXQ8vz78uhiKVOVEAbbpdJXIz/MpSi kzeDTV8I8rhu2Eoh0jH9XdyomX8Bs7i56DSFAFsDmhhO4QlDU+SUPter85pjHvbYebjP YiOlT6JfDllrO5nmM+ZyYi7JAnU/wV/R04azt06CMkYs+KrrYMElNMSLttpHHHBwOHbF VVJBwolo824f3VyTVkf6P8FC4mm/RztOLhkAhYreZlZu546IK+ai1+b2QEHwmG/Zb70G lQTg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2049-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2049-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id q24-20020a170902bd9800b001d04e2f99b1si14058431pls.513.2023.12.15.20.25.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:25:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2049-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2049-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2049-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 62069B230F8 for ; Sat, 16 Dec 2023 04:24:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F3B3B1EA80; Sat, 16 Dec 2023 04:21:54 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 008111427B; Sat, 16 Dec 2023 04:21:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6927C433BC; Sat, 16 Dec 2023 04:21:52 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCJ-00000002yDl-3hwE; Fri, 15 Dec 2023 23:22:43 -0500 Message-ID: <20231216042243.671261141@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:21 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org Subject: [for-linus][PATCH 07/15] ring-buffer: Have saved event hold the entire event References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411105217109871 X-GMAIL-MSGID: 1785411105217109871 From: "Steven Rostedt (Google)" For the ring buffer iterator (non-consuming read), the event needs to be copied into the iterator buffer to make sure that a writer does not overwrite it while the user is reading it. If a write happens during the copy, the buffer is simply discarded. But the temp buffer itself was not big enough. The allocation of the buffer was only BUF_MAX_DATA_SIZE, which is the maximum data size that can be passed into the ring buffer and saved. But the temp buffer needs to hold the meta data as well. That would be BUF_PAGE_SIZE and not BUF_MAX_DATA_SIZE. Link: https://lore.kernel.org/linux-trace-kernel/20231212072558.61f76493@gandalf.local.home Cc: stable@vger.kernel.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Fixes: 785888c544e04 ("ring-buffer: Have rb_iter_head_event() handle concurrent writer") Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index c7abcc215fe2..1d9caee7f542 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -2409,7 +2409,7 @@ rb_iter_head_event(struct ring_buffer_iter *iter) */ barrier(); - if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE) + if ((iter->head + length) > commit || length > BUF_PAGE_SIZE) /* Writer corrupted the read? */ goto reset; @@ -5118,7 +5118,8 @@ ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags) if (!iter) return NULL; - iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags); + /* Holds the entire event: data and meta data */ + iter->event = kmalloc(BUF_PAGE_SIZE, flags); if (!iter->event) { kfree(iter); return NULL; From patchwork Sat Dec 16 04:22:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179810 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30403dyi; Fri, 15 Dec 2023 20:24:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHp8gV3gEAqJUwtb68sF0EFF4Uwtm021u+FHd9u563Cgl0bJGUUUeh3OB5vZ9eXGcKO1Yyw X-Received: by 2002:a50:9546:0:b0:552:84bb:c124 with SMTP id v6-20020a509546000000b0055284bbc124mr1115238eda.86.1702700681333; Fri, 15 Dec 2023 20:24:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700681; cv=none; d=google.com; s=arc-20160816; b=hJhCGEddCdw0xhMafTbA7YwuznOg3ZQPd4d59gColo6tPnX2cDxMupjUslYaMT3adW fQ3775ni/kBVBaQAK3JENEnji8RDIj/iwNh+PQAHzI1fYIDAPy1/nhHEFqbO7Hvoh+Ev rql5/KfwAVYZMV9b/nNFIW6X6dUywWABMBkKfxIo3jZi6Q1lPAaz5uYq1BuR2k+W+jSg DOaD1Ujjdo5o3AcAQYy3/bQnAOGjkqr3v0t90cGtB+MHzw53kA2BLs8H5Qv5RYyz7SZA 7CMAfM1bzKb8Dm+FVURLUhq7gk+bmrPKSqsHo0UjOofaQUVTsf4wVqOhL0oJxCHt6fXE 8bUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=LGkRuWwUcy7mq59kxOpccGWtI4Hkn0qX9qadRpeJrko=; fh=mTYg3JJ8i2GBNLFt8qb4bgYwOyk0kmrOzICLbi6qJ1c=; b=WMnFEO7Pcb8g+/8dc67+PbWHhsyraSG/uS+KzKtHEUE7Xm6YyLqx5HvPvar0soAjBE NgAkt75g4mJkCxyPVk7ROpnpg+oAV1jkbfOJO3DSDiAE6/uTJd34oBNDoeorsTzrBzo7 /0bgL5sX3LKWvJvTuKlL5zDNBtGrnZ7xepd4k7GO7fo8o8uYnS6dQpH9T+Nk+jzegzhd 2NwY2T0498n49p1P7b59Bvi5f9w3aT9hV4Z3CXGsxmmFzeg00zTgOvWUFmt2P4zxg/IO 88hIlDk2X9Ip4L0ED/SwN9qOE2aFy9h1g6WgizNCgb8pyZBtXjoPcI0W3eo7Mukq2wZ+ xieQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2051-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2051-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d2-20020a50cd42000000b0054c6f3c1f00si8451846edj.435.2023.12.15.20.24.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:24:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2051-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2051-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2051-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id EFACC1F25CEE for ; Sat, 16 Dec 2023 04:24:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3ACF82C6B5; Sat, 16 Dec 2023 04:21:55 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82D38171DA for ; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 424E9C43215; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCK-00000002yEF-0iz1; Fri, 15 Dec 2023 23:22:44 -0500 Message-ID: <20231216042243.959035900@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:22 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [for-linus][PATCH 08/15] tracing: Add size check when printing trace_marker output References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411069691583589 X-GMAIL-MSGID: 1785411069691583589 From: "Steven Rostedt (Google)" If for some reason the trace_marker write does not have a nul byte for the string, it will overflow the print: trace_seq_printf(s, ": %s", field->buf); The field->buf could be missing the nul byte. To prevent overflow, add the max size that the buf can be by using the event size and the field location. int max = iter->ent_size - offsetof(struct print_entry, buf); trace_seq_printf(s, ": %*.s", max, field->buf); Link: https://lore.kernel.org/linux-trace-kernel/20231212084444.4619b8ce@gandalf.local.home Cc: Mark Rutland Cc: Mathieu Desnoyers Reviewed-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace_output.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c index d8b302d01083..3e7fa44dc2b2 100644 --- a/kernel/trace/trace_output.c +++ b/kernel/trace/trace_output.c @@ -1587,11 +1587,12 @@ static enum print_line_t trace_print_print(struct trace_iterator *iter, { struct print_entry *field; struct trace_seq *s = &iter->seq; + int max = iter->ent_size - offsetof(struct print_entry, buf); trace_assign_type(field, iter->ent); seq_print_ip_sym(s, field->ip, flags); - trace_seq_printf(s, ": %s", field->buf); + trace_seq_printf(s, ": %.*s", max, field->buf); return trace_handle_return(s); } @@ -1600,10 +1601,11 @@ static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags, struct trace_event *event) { struct print_entry *field; + int max = iter->ent_size - offsetof(struct print_entry, buf); trace_assign_type(field, iter->ent); - trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf); + trace_seq_printf(&iter->seq, "# %lx %.*s", field->ip, max, field->buf); return trace_handle_return(&iter->seq); } From patchwork Sat Dec 16 04:22:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179811 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30450dyi; Fri, 15 Dec 2023 20:24:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IEHQf1pLhS3rZbpLlOStd11tEt1I/nFfzsD1bpYe/07nDWXZF4bsYiFnNcD+N854Vu4XC+d X-Received: by 2002:a0c:c48d:0:b0:67c:cd09:9b93 with SMTP id u13-20020a0cc48d000000b0067ccd099b93mr13008275qvi.7.1702700697385; Fri, 15 Dec 2023 20:24:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700697; cv=none; d=google.com; s=arc-20160816; b=ZtRTmeebyzdAreInZCf6OgyfA7tneQtyw3+OUH8hhd2kKx8l4alqXiq+BjJZiaa0ov ZV/6uzok+T0M8vg7DNAq3Xra7SfAAOTNJ5C+51WyD0K26I2eqrJXu9jlzPo5hNi0H0OM mmXEv3ak85ikwtnTy4xs//BYdz+PQ/O8KfzpufFUBlBaOyoqe6LTCdMvi724JkIENg/O Cw0oPi/n6aL/xpSw0doA14se+Pv8qLUfB1glmOTfkuyLVyKfEb7BraR2LqmKv33kyEkc marseJTnv+L/F04RFQ/zQrjV5ifH3jzNATii7PQQIp4jo7EDuYrVYhnuzaq6SxIxwkwp WneA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=mgQhSU72e8k67FFoV2h1ZMRWzagtrHyrSsENd15IhaA=; fh=MK0E06hcwmV5W4NpFpMEjwzVRJBfLkCOrdZVKDznkQE=; b=pbj91fpKKFlGXEG7Ai0cNt3caM49qPcEGWJKJade/fbyUrrWb7hEa/bNCuRY67Y+sk 6TXnFtGITsUGyJxys+7Lsm4TZ0wORO82rCSd1o1/lXxEiZyg7WmGbxDCyJaii7jZZnCM nWDYXhGVQlEl6VzW7NocRcYlZvFmAkKQ+pDbKRk9SvbhT9J/HcCZtCdCEfRFz26PXILZ dlcMrG/ds/KiSVuzeQv+ubL83Xhp6SK4VwqIdrPliMva9eggs3psG8seXeG3t467VUjX ozU5+0UP1eOmYdvZZYiHWpPk60RF2S40dVcvPjQDFvJxSRWTyJcOHPftfqlLMHPwub92 lSzw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2052-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2052-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t14-20020a0ce58e000000b0067ed2267b72si13547724qvm.275.2023.12.15.20.24.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:24:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2052-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2052-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2052-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 30E781C2428C for ; Sat, 16 Dec 2023 04:24:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7AF3131A7F; Sat, 16 Dec 2023 04:21:55 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97843179B4 for ; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77E9CC433D9; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCK-00000002yEk-1wmG; Fri, 15 Dec 2023 23:22:44 -0500 Message-ID: <20231216042244.247503479@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:23 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Zheng Yejian Subject: [for-linus][PATCH 09/15] tracing: Fix uaf issue when open the hist or hist_debug file References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411086514186576 X-GMAIL-MSGID: 1785411086514186576 From: Zheng Yejian KASAN report following issue. The root cause is when opening 'hist' file of an instance and accessing 'trace_event_file' in hist_show(), but 'trace_event_file' has been freed due to the instance being removed. 'hist_debug' file has the same problem. To fix it, call tracing_{open,release}_file_tr() in file_operations callback to have the ref count and avoid 'trace_event_file' being freed. BUG: KASAN: slab-use-after-free in hist_show+0x11e0/0x1278 Read of size 8 at addr ffff242541e336b8 by task head/190 CPU: 4 PID: 190 Comm: head Not tainted 6.7.0-rc5-g26aff849438c #133 Hardware name: linux,dummy-virt (DT) Call trace: dump_backtrace+0x98/0xf8 show_stack+0x1c/0x30 dump_stack_lvl+0x44/0x58 print_report+0xf0/0x5a0 kasan_report+0x80/0xc0 __asan_report_load8_noabort+0x1c/0x28 hist_show+0x11e0/0x1278 seq_read_iter+0x344/0xd78 seq_read+0x128/0x1c0 vfs_read+0x198/0x6c8 ksys_read+0xf4/0x1e0 __arm64_sys_read+0x70/0xa8 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Allocated by task 188: kasan_save_stack+0x28/0x50 kasan_set_track+0x28/0x38 kasan_save_alloc_info+0x20/0x30 __kasan_slab_alloc+0x6c/0x80 kmem_cache_alloc+0x15c/0x4a8 trace_create_new_event+0x84/0x348 __trace_add_new_event+0x18/0x88 event_trace_add_tracer+0xc4/0x1a0 trace_array_create_dir+0x6c/0x100 trace_array_create+0x2e8/0x568 instance_mkdir+0x48/0x80 tracefs_syscall_mkdir+0x90/0xe8 vfs_mkdir+0x3c4/0x610 do_mkdirat+0x144/0x200 __arm64_sys_mkdirat+0x8c/0xc0 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Freed by task 191: kasan_save_stack+0x28/0x50 kasan_set_track+0x28/0x38 kasan_save_free_info+0x34/0x58 __kasan_slab_free+0xe4/0x158 kmem_cache_free+0x19c/0x508 event_file_put+0xa0/0x120 remove_event_file_dir+0x180/0x320 event_trace_del_tracer+0xb0/0x180 __remove_instance+0x224/0x508 instance_rmdir+0x44/0x78 tracefs_syscall_rmdir+0xbc/0x140 vfs_rmdir+0x1cc/0x4c8 do_rmdir+0x220/0x2b8 __arm64_sys_unlinkat+0xc0/0x100 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Link: https://lore.kernel.org/linux-trace-kernel/20231214012153.676155-1-zhengyejian1@huawei.com Suggested-by: Steven Rostedt Signed-off-by: Zheng Yejian Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 6 ++++++ kernel/trace/trace.h | 1 + kernel/trace/trace_events_hist.c | 12 ++++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 6c79548f9574..199df497db07 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -4968,6 +4968,12 @@ int tracing_release_file_tr(struct inode *inode, struct file *filp) return 0; } +int tracing_single_release_file_tr(struct inode *inode, struct file *filp) +{ + tracing_release_file_tr(inode, filp); + return single_release(inode, filp); +} + static int tracing_mark_open(struct inode *inode, struct file *filp) { stream_open(inode, filp); diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index b7f4ea25a194..0489e72c8169 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -617,6 +617,7 @@ int tracing_open_generic(struct inode *inode, struct file *filp); int tracing_open_generic_tr(struct inode *inode, struct file *filp); int tracing_open_file_tr(struct inode *inode, struct file *filp); int tracing_release_file_tr(struct inode *inode, struct file *filp); +int tracing_single_release_file_tr(struct inode *inode, struct file *filp); bool tracing_is_disabled(void); bool tracer_tracing_is_on(struct trace_array *tr); void tracer_tracing_on(struct trace_array *tr); diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 1abc07fba1b9..5ecf3c8bde20 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -5623,10 +5623,12 @@ static int event_hist_open(struct inode *inode, struct file *file) { int ret; - ret = security_locked_down(LOCKDOWN_TRACEFS); + ret = tracing_open_file_tr(inode, file); if (ret) return ret; + /* Clear private_data to avoid warning in single_open() */ + file->private_data = NULL; return single_open(file, hist_show, file); } @@ -5634,7 +5636,7 @@ const struct file_operations event_hist_fops = { .open = event_hist_open, .read = seq_read, .llseek = seq_lseek, - .release = single_release, + .release = tracing_single_release_file_tr, }; #ifdef CONFIG_HIST_TRIGGERS_DEBUG @@ -5900,10 +5902,12 @@ static int event_hist_debug_open(struct inode *inode, struct file *file) { int ret; - ret = security_locked_down(LOCKDOWN_TRACEFS); + ret = tracing_open_file_tr(inode, file); if (ret) return ret; + /* Clear private_data to avoid warning in single_open() */ + file->private_data = NULL; return single_open(file, hist_debug_show, file); } @@ -5911,7 +5915,7 @@ const struct file_operations event_hist_debug_fops = { .open = event_hist_debug_open, .read = seq_read, .llseek = seq_lseek, - .release = single_release, + .release = tracing_single_release_file_tr, }; #endif From patchwork Sat Dec 16 04:22:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179813 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30535dyi; Fri, 15 Dec 2023 20:25:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IEAJxfxD5dUpiE4GCepx2dVh/77MhAbtze9VOBbpspQvYL2JgCqL5BA1+4z1XDrGqqG65BG X-Received: by 2002:a05:6808:124a:b0:3b8:b063:8261 with SMTP id o10-20020a056808124a00b003b8b0638261mr15124045oiv.99.1702700718925; Fri, 15 Dec 2023 20:25:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700718; cv=none; d=google.com; s=arc-20160816; b=EDFlSaY1bt+EY2keZvu4+sk2ZCi9vAg+ksD7vW0KX3zhNPBH1lEyYDA29gfRT+JRSN AvOfcNCBqHzJCQRgrXO/Lh8xTSsSDpDpiA65v28yZeBT2BwdIqx4PJDKrKjMWGKPCM+T VdNjtHBBnwgZBDxRUeUCgD+sEWIEKZq6sGpZ9XCdlr3dRsaFEz3fpL5/P6PltsTnv0fQ pLQnkyjlMvQyq+6WaYgInW0Z/tD0GREdAL8CBOi3trbbErW7xPAR9AODlMEpSPnMuLaR wjOfelcvWNmjxNvvYIohUauUqSDv59eaRoIgt7WBS00dO82eh88R6SRPlcsqqmbVPCR2 CoCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=oTbzVo2gUqjoybDDrE8akHhAZYfzGsu+J6VYZ8GFSeE=; fh=2pwdZbs71J8w5U0NvpP4HP8qFQa6exIsPiz6XoqdMn4=; b=DjyJhwLKYU6w9LqdgMcHfWHSZ47VyJGd5p5iFzOYF7XrqRe9/+GZ8Y0BqCauJL4Q2J kLIYYuoR8O5X5x5DR499/sLQy7KKP4N7rsIzS/mNTMdlHU+l63JLWDzXg3Sc94XnvvQk qVFkTDTJbt0RaRjtAn4S1U09bpj6muV8Xm4EMFst/mE0x1GZOrp0tFNIiD42gNvqHjFG DvpYKbtQvivmlTcHYsQE8UKNe5rImA+MAsAq0a6sg1Cj8C0y/jiVXhZtYWf9V3zr9gs1 kL9OZ883LrAbykB5pYmEJS04hIMzTEUHHpf8jIXt2eTPSgWV8WwAQoBcMPzLsZudg4yD KRGw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2053-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2053-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id a64-20020a639043000000b005cd7caf17fbsi457104pge.408.2023.12.15.20.25.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:25:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2053-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2053-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2053-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 9FD06284D30 for ; Sat, 16 Dec 2023 04:25:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DF95432C83; Sat, 16 Dec 2023 04:21:55 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4099519BA5; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5E6EC433CB; Sat, 16 Dec 2023 04:21:53 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCK-00000002yFF-3BcO; Fri, 15 Dec 2023 23:22:44 -0500 Message-ID: <20231216042244.539165490@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:24 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org, Joel Fernandes , Vincent Donnefort Subject: [for-linus][PATCH 10/15] ring-buffer: Do not try to put back write_stamp References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411108967968104 X-GMAIL-MSGID: 1785411108967968104 From: "Steven Rostedt (Google)" If an update to an event is interrupted by another event between the time the initial event allocated its buffer and where it wrote to the write_stamp, the code try to reset the write stamp back to the what it had just overwritten. It knows that it was overwritten via checking the before_stamp, and if it didn't match what it wrote to the before_stamp before it allocated its space, it knows it was overwritten. To put back the write_stamp, it uses the before_stamp it read. The problem here is that by writing the before_stamp to the write_stamp it makes the two equal again, which means that the write_stamp can be considered valid as the last timestamp written to the ring buffer. But this is not necessarily true. The event that interrupted the event could have been interrupted in a way that it was interrupted as well, and can end up leaving with an invalid write_stamp. But if this happens and returns to this context that uses the before_stamp to update the write_stamp again, it can possibly incorrectly make it valid, causing later events to have in correct time stamps. As it is OK to leave this function with an invalid write_stamp (one that doesn't match the before_stamp), there's no reason to try to make it valid again in this case. If this race happens, then just leave with the invalid write_stamp and the next event to come along will just add a absolute timestamp and validate everything again. Bonus points: This gets rid of another cmpxchg64! Link: https://lore.kernel.org/linux-trace-kernel/20231214222921.193037a7@gandalf.local.home Cc: stable@vger.kernel.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Joel Fernandes Cc: Vincent Donnefort Fixes: a389d86f7fd09 ("ring-buffer: Have nested events still record running time stamp") Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 1d9caee7f542..2668dde23343 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -3612,14 +3612,14 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, } if (likely(tail == w)) { - u64 save_before; - bool s_ok; - /* Nothing interrupted us between A and C */ /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts); - barrier(); - /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before); - RB_WARN_ON(cpu_buffer, !s_ok); + /* + * If something came in between C and D, the write stamp + * may now not be in sync. But that's fine as the before_stamp + * will be different and then next event will just be forced + * to use an absolute timestamp. + */ if (likely(!(info->add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)))) /* This did not interrupt any time update */ @@ -3627,24 +3627,7 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, else /* Just use full timestamp for interrupting event */ info->delta = info->ts; - barrier(); check_buffer(cpu_buffer, info, tail); - if (unlikely(info->ts != save_before)) { - /* SLOW PATH - Interrupted between C and E */ - - a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after); - RB_WARN_ON(cpu_buffer, !a_ok); - - /* Write stamp must only go forward */ - if (save_before > info->after) { - /* - * We do not care about the result, only that - * it gets updated atomically. - */ - (void)rb_time_cmpxchg(&cpu_buffer->write_stamp, - info->after, save_before); - } - } } else { u64 ts; /* SLOW PATH - Interrupted between A and C */ From patchwork Sat Dec 16 04:22:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179814 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30574dyi; Fri, 15 Dec 2023 20:25:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IENRYqsgRzk/HlQin3dcvsFMBZuyeu5L8AAceL6Mz7e17/CeDMXycr8MU6eHLShIBHJ9wVB X-Received: by 2002:a50:8701:0:b0:552:fc86:d417 with SMTP id i1-20020a508701000000b00552fc86d417mr148385edb.15.1702700725368; Fri, 15 Dec 2023 20:25:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700725; cv=none; d=google.com; s=arc-20160816; b=yqrlkXGLm0tEawlSwiW7FDkdQ/EwWfr8nmnCqE7i+/0haMvVEGfj1WrgaWPH9T+mr3 iQGD74Kpjmb1A0mpY9/0QDQl6infqT6bVnKYLmw5hsVg1v3kkJj3vJTfstQjoJ+JNHe/ OnK+Q5XNndLvnDpcvTkPj3WR/RoRyyGPl7Bi2tbSko7ltwIj8sNQzqXInbDKgJZXXEFL lHM0hLa1TyeVWQ/3te51zFTNbKNaeBsuz9eVGOcxUULYtM55A53PyrybgUuOX6//i+5+ GDcMgvzxER+ntoswc9SAPcIPSv2hSUvk0AVbjspWztqi4PMnlLvOAsdA8eOZxFm5RdsX 3j5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=eRXNISRySPQ9ooYh5iHpeBHMLEX0jQCUguba0gV5XLQ=; fh=dSbO5nrudX1o1/5h1aiDXadU1yIdyz7bM/pnn0K5lFA=; b=BANCy1MHqBmCiL+GJA6KpwAt+1uE99Dfqa2wQNvsdxsg941PPq1+d7VZoMlE2YsIeF WnbLBH24JUDaAfTJ8ciE+LR++OXMcxpR0G6+hbd5h20jUYpiVAaSF60T3oJ0yV8FZlM5 770JmRwlCErhv92tzdeDTr1DXEE5sjcYUooAJuBUuuUqU8Gj6m3yjXvNRiwle/uCd3vb odTgwU6nz1HCgrvraT/7ff4kYZhLGV0+LzO7b7GjNQR+7ewIb5kDC7d9D02hgSGFWSQZ aOM4L0GxY04Yab8d/ayK9xd91xfE9ITP3ThmwI6jbtQdyQi/ofXYNvqKJe0vRDfcMVPM HmlA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2054-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2054-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d25-20020a50cd59000000b0054cb7887a54si8355477edj.119.2023.12.15.20.25.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:25:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2054-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2054-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2054-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id BF7401F25CF8 for ; Sat, 16 Dec 2023 04:25:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 06B2033094; Sat, 16 Dec 2023 04:21:56 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C1F01A28A for ; Sat, 16 Dec 2023 04:21:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C31EC116B4; Sat, 16 Dec 2023 04:21:54 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCL-00000002yFl-0EfZ; Fri, 15 Dec 2023 23:22:45 -0500 Message-ID: <20231216042244.832048983@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:25 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Joel Fernandes , Vincent Donnefort Subject: [for-linus][PATCH 11/15] ring-buffer: Remove useless update to write_stamp in rb_try_to_discard() References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411115727326820 X-GMAIL-MSGID: 1785411115727326820 From: "Steven Rostedt (Google)" When filtering is enabled, a temporary buffer is created to place the content of the trace event output so that the filter logic can decide from the trace event output if the trace event should be filtered out or not. If it is to be filtered out, the content in the temporary buffer is simply discarded, otherwise it is written into the trace buffer. But if an interrupt were to come in while a previous event was using that temporary buffer, the event written by the interrupt would actually go into the ring buffer itself to prevent corrupting the data on the temporary buffer. If the event is to be filtered out, the event in the ring buffer is discarded, or if it fails to discard because another event were to have already come in, it is turned into padding. The update to the write_stamp in the rb_try_to_discard() happens after a fix was made to force the next event after the discard to use an absolute timestamp by setting the before_stamp to zero so it does not match the write_stamp (which causes an event to use the absolute timestamp). But there's an effort in rb_try_to_discard() to put back the write_stamp to what it was before the event was added. But this is useless and wasteful because nothing is going to be using that write_stamp for calculations as it still will not match the before_stamp. Remove this useless update, and in doing so, we remove another cmpxchg64()! Also update the comments to reflect this change as well as remove some extra white space in another comment. Link: https://lore.kernel.org/linux-trace-kernel/20231215081810.1f4f38fe@rorschach.local.home Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Joel Fernandes Cc: Vincent Donnefort Fixes: b2dd797543cf ("ring-buffer: Force absolute timestamp on discard of event") Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 47 +++++++++----------------------------- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 2668dde23343..ad4af0cba159 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -2983,25 +2983,6 @@ static unsigned rb_calculate_event_length(unsigned length) return length; } -static u64 rb_time_delta(struct ring_buffer_event *event) -{ - switch (event->type_len) { - case RINGBUF_TYPE_PADDING: - return 0; - - case RINGBUF_TYPE_TIME_EXTEND: - return rb_event_time_stamp(event); - - case RINGBUF_TYPE_TIME_STAMP: - return 0; - - case RINGBUF_TYPE_DATA: - return event->time_delta; - default: - return 0; - } -} - static inline bool rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, struct ring_buffer_event *event) @@ -3009,8 +2990,6 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, unsigned long new_index, old_index; struct buffer_page *bpage; unsigned long addr; - u64 write_stamp; - u64 delta; new_index = rb_event_index(event); old_index = new_index + rb_event_ts_length(event); @@ -3019,14 +2998,10 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, bpage = READ_ONCE(cpu_buffer->tail_page); - delta = rb_time_delta(event); - - if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp)) - return false; - - /* Make sure the write stamp is read before testing the location */ - barrier(); - + /* + * Make sure the tail_page is still the same and + * the next write location is the end of this event + */ if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) { unsigned long write_mask = local_read(&bpage->write) & ~RB_WRITE_MASK; @@ -3037,20 +3012,20 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, * to make sure that the next event adds an absolute * value and does not rely on the saved write stamp, which * is now going to be bogus. + * + * By setting the before_stamp to zero, the next event + * is not going to use the write_stamp and will instead + * create an absolute timestamp. This means there's no + * reason to update the wirte_stamp! */ rb_time_set(&cpu_buffer->before_stamp, 0); - /* Something came in, can't discard */ - if (!rb_time_cmpxchg(&cpu_buffer->write_stamp, - write_stamp, write_stamp - delta)) - return false; - /* * If an event were to come in now, it would see that the * write_stamp and the before_stamp are different, and assume * that this event just added itself before updating * the write stamp. The interrupting event will fix the - * write stamp for us, and use the before stamp as its delta. + * write stamp for us, and use an absolute timestamp. */ /* @@ -3487,7 +3462,7 @@ static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer, return; /* - * If this interrupted another event, + * If this interrupted another event, */ if (atomic_inc_return(this_cpu_ptr(&checking)) != 1) goto out; From patchwork Sat Dec 16 04:22:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179818 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30730dyi; Fri, 15 Dec 2023 20:26:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNRkWx6pmXt8FNo8n949YfBkIu5/91WWth/yJX9vJVb4lV4eR+9vP9NnHy61RzW/DYxYjV X-Received: by 2002:a05:6870:c1cf:b0:203:959f:4dc3 with SMTP id i15-20020a056870c1cf00b00203959f4dc3mr15262oad.88.1702700763303; Fri, 15 Dec 2023 20:26:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700763; cv=none; d=google.com; s=arc-20160816; b=N7ACzgTTPHHdG3qkgxXXvLMkLY8ZUJgo0UMYHBoy6EbX2UfqovQ40yOhjzPwrJXm4E EOVj9KubxHyz0pl0AQbwUHOwgW45mZYH8zpOPEj/B2RbuVM8eqoxKpvpJ0vznFzwEQNI BnVCT9/RGijKfT4oiMKIMoalsgmwyP3liZp2UzikfjZvoBVWro+H623MuSz00xM1xX3T jAIfwnO4gA2zH8aC8u33z1T/ziwqWXjUd+buF2rAfLtOSxsbrvA+/eUicIxsnVzGV1dr IXe+xS5U1Bqm1wJslVcpgTBQmmYUsPXm/ZIhP6EKAJE+ETXixHvAhYdv4teRymIffJz6 Eg3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=3BKF9/e6chZUxlDvrK4itmX1cfIFCVVwB4aQlZdttvw=; fh=hu/11vjhAVGN2soHkDrejjtKeeIeHno3fFH7CvZIC0Y=; b=Gaoq/xmEvel2BMXP6og5Ac5/8DQzIOLnl2dXwpUFRISqugTicZkunECJHpZF3PzPqj snPWQi/bX68RzMEnB4dhUgq/yp8BmYIGK0nEAwWHrogpw/aFrwoDzzob/ZCXQgwRRPVb FulJg5xvxY/Y/7wMJ14QGN0X1FBIh8RcP7WIiIsYvnEZnztBb4bHmSKKEtSkx9h7uVVS bRcj2/AabsuayUDI1Zu0krmCenlcUVkX9DiIyNcVOzEey2Rgz14NeKcDnlbic+9KuAEN s6a6U0uIt9oFqlQrR5N86L0eRSuzmUaYl5GxT0lF4r73ff74Rwo8GZIQb8iSn+ctRBR1 +/Gw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2055-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id o9-20020a655209000000b005b8ee1c0c67si13802800pgp.594.2023.12.15.20.26.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:26:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2055-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 5143BB238F7 for ; Sat, 16 Dec 2023 04:25:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1255533CD9; Sat, 16 Dec 2023 04:21:56 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FDB11A28E; Sat, 16 Dec 2023 04:21:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A30FC433B6; Sat, 16 Dec 2023 04:21:54 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCL-00000002yGF-1QzQ; Fri, 15 Dec 2023 23:22:45 -0500 Message-ID: <20231216042245.133638178@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:26 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org Subject: [for-linus][PATCH 12/15] ring-buffer: Fix a race in rb_time_cmpxchg() for 32 bit archs References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411155808350206 X-GMAIL-MSGID: 1785411155808350206 From: "Steven Rostedt (Google)" Mathieu Desnoyers pointed out an issue in the rb_time_cmpxchg() for 32 bit architectures. That is: static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) { unsigned long cnt, top, bottom, msb; unsigned long cnt2, top2, bottom2, msb2; u64 val; /* The cmpxchg always fails if it interrupted an update */ if (!__rb_time_read(t, &val, &cnt2)) return false; if (val != expect) return false; <<<< interrupted here! cnt = local_read(&t->cnt); The problem is that the synchronization counter in the rb_time_t is read *after* the value of the timestamp is read. That means if an interrupt were to come in between the value being read and the counter being read, it can change the value and the counter and the interrupted process would be clueless about it! The counter needs to be read first and then the value. That way it is easy to tell if the value is stale or not. If the counter hasn't been updated, then the value is still good. Link: https://lore.kernel.org/linux-trace-kernel/20231211201324.652870-1-mathieu.desnoyers@efficios.com/ Link: https://lore.kernel.org/linux-trace-kernel/20231212115301.7a9c9a64@gandalf.local.home Cc: stable@vger.kernel.org Cc: Masami Hiramatsu Cc: Mark Rutland Fixes: 10464b4aa605e ("ring-buffer: Add rb_time_t 64 bit operations for speeding up 32 bit") Reported-by: Mathieu Desnoyers Reviewed-by: Mathieu Desnoyers Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index ad4af0cba159..b8ab0557bd1b 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -706,6 +706,9 @@ static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) unsigned long cnt2, top2, bottom2, msb2; u64 val; + /* Any interruptions in this function should cause a failure */ + cnt = local_read(&t->cnt); + /* The cmpxchg always fails if it interrupted an update */ if (!__rb_time_read(t, &val, &cnt2)) return false; @@ -713,7 +716,6 @@ static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) if (val != expect) return false; - cnt = local_read(&t->cnt); if ((cnt & 3) != cnt2) return false; From patchwork Sat Dec 16 04:22:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179815 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30625dyi; Fri, 15 Dec 2023 20:25:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IEV9K+6rfxAYPonGR4aYfcrNlDcDbdi+Hbcdhhdu48VA7O4oPIsHf90gw4FiUuGv5cyfR/d X-Received: by 2002:a05:620a:22c1:b0:77f:26:18fd with SMTP id o1-20020a05620a22c100b0077f002618fdmr13112526qki.6.1702700738790; Fri, 15 Dec 2023 20:25:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700738; cv=none; d=google.com; s=arc-20160816; b=DBGJnGdowRCl7ez5GBDygZr+6lvF7DJYppdLJ1QvLjnsdmM/sh6/J3eGa2o9gSGHGz 5huQ0VKaRBvRbKu3KVqxA9n4NFh0TchBbjzxEeJchtHhGFOA/CcKR3GxTEyBsrF6XJaZ xzypJI87VrAtvWYpVMq9TbP5BW2Pnz3/OtIPkP0WEBuXg2wXaXS8rIAIVQ6NHhTvZ+9o 2GtY7fa97h8CBpt7QGF2FxAtW0RPAgA8e817Yf8rDTR7vKzXicVfzrdKBlC0BRx+Ll3Z GV7qDXUtSJ1LJwH+9MnW/GwFSpacK9QvCEzeqgNiQCaCq45E2XCL+X45M+D+/LfDJjLO mGxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=5Bo0lovkUOr3TbYahxU5Ri+BxAPVcaqm6Z1QP6CmUno=; fh=mTYg3JJ8i2GBNLFt8qb4bgYwOyk0kmrOzICLbi6qJ1c=; b=y3D/PfIVNkQATcV5X+Pnd7xZr2spdg7Rpt5PPmFrwQp56qqFoVmhgawlzBzUb8EwXc MDosb8A6j+jKz1zzRYQ4UenrvoV3RyeVD5MbOuZqis4yZzNzMy5EBTivuuGKC+/PxPVf iFrGmLikvD9xJrGdH8PrnV6a2jmlscPB+ohlwCscyI258W7Xy9qY4kgXZW5qJqPQTHpY +JMG+EkaDrAPdrWz1kC+bIVZT3YzXAeu3wDOPHIUWsaJ0rvKIPKeQY1dEilG8Tw8nXDk 9MFfT9xgLkES5HDnMIPH/ChRg4vUcNefKri+OKVOYgUL7TxRJehcxZjnlbcgR7q0dG/a nP+A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2056-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2056-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id bn34-20020a05620a2ae200b0077f4e80e131si18172981qkb.692.2023.12.15.20.25.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:25:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2056-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2056-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2056-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8EBA81C24618 for ; Sat, 16 Dec 2023 04:25:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 568D8341BD; Sat, 16 Dec 2023 04:21:56 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AED5B1E493 for ; Sat, 16 Dec 2023 04:21:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D64DC433CA; Sat, 16 Dec 2023 04:21:54 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCL-00000002yGk-2eXm; Fri, 15 Dec 2023 23:22:45 -0500 Message-ID: <20231216042245.415755764@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:27 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [for-linus][PATCH 13/15] ring-buffer: Fix 32-bit rb_time_read() race with rb_time_cmpxchg() References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411129822974756 X-GMAIL-MSGID: 1785411129822974756 From: Mathieu Desnoyers The following race can cause rb_time_read() to observe a corrupted time stamp: rb_time_cmpxchg() [...] if (!rb_time_read_cmpxchg(&t->msb, msb, msb2)) return false; if (!rb_time_read_cmpxchg(&t->top, top, top2)) return false; __rb_time_read() [...] do { c = local_read(&t->cnt); top = local_read(&t->top); bottom = local_read(&t->bottom); msb = local_read(&t->msb); } while (c != local_read(&t->cnt)); *cnt = rb_time_cnt(top); /* If top and msb counts don't match, this interrupted a write */ if (*cnt != rb_time_cnt(msb)) return false; ^ this check fails to catch that "bottom" is still not updated. So the old "bottom" value is returned, which is wrong. Fix this by checking that all three of msb, top, and bottom 2-bit cnt values match. The reason to favor checking all three fields over requiring a specific update order for both rb_time_set() and rb_time_cmpxchg() is because checking all three fields is more robust to handle partial failures of rb_time_cmpxchg() when interrupted by nested rb_time_set(). Link: https://lore.kernel.org/lkml/20231211201324.652870-1-mathieu.desnoyers@efficios.com/ Link: https://lore.kernel.org/linux-trace-kernel/20231212193049.680122-1-mathieu.desnoyers@efficios.com Fixes: f458a1453424e ("ring-buffer: Test last update in 32bit version of __rb_time_read()") Signed-off-by: Mathieu Desnoyers Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index b8ab0557bd1b..f22a849da179 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -644,8 +644,8 @@ static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt) *cnt = rb_time_cnt(top); - /* If top and msb counts don't match, this interrupted a write */ - if (*cnt != rb_time_cnt(msb)) + /* If top, msb or bottom counts don't match, this interrupted a write */ + if (*cnt != rb_time_cnt(msb) || *cnt != rb_time_cnt(bottom)) return false; /* The shift to msb will lose its cnt bits */ From patchwork Sat Dec 16 04:22:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179816 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30653dyi; Fri, 15 Dec 2023 20:25:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFrmbUIkNb0VaNn2BjKFEUKlVU3nSRVSSAUkcdiTS2/EMst34aWIrHcKqCwQBtLDqOBcT3S X-Received: by 2002:a05:6e02:1a21:b0:35f:83e3:e4e7 with SMTP id g1-20020a056e021a2100b0035f83e3e4e7mr4341865ile.51.1702700746007; Fri, 15 Dec 2023 20:25:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700745; cv=none; d=google.com; s=arc-20160816; b=RgGU3TP5ukGqt69zDGVJ1retj8PY1iHFeKCxBeAhJaDxi4k9ye46A5kqoX0AUBj7SN nm7YzbMHMdxTIQzU0wdnIY+bg7bE81PRhGE5OOkSVjMR2lGULQVn0edlC8WNV3pMJVIo bywP7gjsfXLKlRTadOSAn9tNiC/Q/BlOxPSOJrhyAkf6kUMuZsQkvFE6CVNBzmdzTCrQ 9mQTzWGBvJKgbbAWX9Cjm8fK5f22ZSlSwjp4BXLhBvRyOFyB/ndK7Ya1bhHPkXnlj1TF WDwmVs9AqkJFVknT+nAYjTxnTHUIpVmEN089BIIArnZV0ww5d4s69pBRbVxMTjfJb/j1 WbXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=qI+7acxIb25WQNUFqz8nIUtNa/LHotVSPXsC8knILBw=; fh=hu/11vjhAVGN2soHkDrejjtKeeIeHno3fFH7CvZIC0Y=; b=a6KkzxKI448OPRrmI/94VAmGAC7t8HzVeLZ6G4/WyPz3r4UsbKGMFp2aWo1wPmQ1Ci ymCsDcFIXEcR3QIiQFPshsxdfY5UB6c63RjI1bLpdkVy+CbQj8y1CPZpiWo/nHSi21eq pyY0gc84R9vyqQm2CXZLTJuyxTozgWZDfpj/xxa2M1JtebHWzMDc9C8hyjOTDCbBFapj fa1bfbXYY+m6fn/c6JLIw2IEjF3uED0PoII9DxzZN1LbTPLUyLRU6eE0TmNHo4pAP//O Dvw2Bxa9Yg4LlM1WXy66DMBExidrjbgbyKWx2XyEO+Uy1YUiP84TANBmv18PNDSzBO0A YwZw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2057-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2057-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i23-20020a635417000000b005ca5619a761si4677037pgb.397.2023.12.15.20.25.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:25:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2057-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2057-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2057-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C40CD28227D for ; Sat, 16 Dec 2023 04:25:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9E68534CE4; Sat, 16 Dec 2023 04:21:56 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 098FC208D7; Sat, 16 Dec 2023 04:21:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB796C433CA; Sat, 16 Dec 2023 04:21:54 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCL-00000002yHF-3tRk; Fri, 15 Dec 2023 23:22:45 -0500 Message-ID: <20231216042245.708750639@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:28 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org Subject: [for-linus][PATCH 14/15] ring-buffer: Have rb_time_cmpxchg() set the msb counter too References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411137220578899 X-GMAIL-MSGID: 1785411137220578899 From: "Steven Rostedt (Google)" The rb_time_cmpxchg() on 32-bit architectures requires setting three 32-bit words to represent the 64-bit timestamp, with some salt for synchronization. Those are: msb, top, and bottom The issue is, the rb_time_cmpxchg() did not properly salt the msb portion, and the msb that was written was stale. Link: https://lore.kernel.org/linux-trace-kernel/20231215084114.20899342@rorschach.local.home Cc: stable@vger.kernel.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Fixes: f03f2abce4f39 ("ring-buffer: Have 32 bit time stamps use all 64 bits") Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index f22a849da179..f4679013289b 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -722,10 +722,12 @@ static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) cnt2 = cnt + 1; rb_time_split(val, &top, &bottom, &msb); + msb = rb_time_val_cnt(msb, cnt); top = rb_time_val_cnt(top, cnt); bottom = rb_time_val_cnt(bottom, cnt); rb_time_split(set, &top2, &bottom2, &msb2); + msb2 = rb_time_val_cnt(msb2, cnt); top2 = rb_time_val_cnt(top2, cnt2); bottom2 = rb_time_val_cnt(bottom2, cnt2); From patchwork Sat Dec 16 04:22:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179817 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp30680dyi; Fri, 15 Dec 2023 20:25:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IFs3goIn9i0Aj4Tnj/mfrqjcWldlGwkdNpCkyV1qrpB7G2+oICMCgITUxKzhp9DgEL0P9HF X-Received: by 2002:a05:6214:1106:b0:67a:cbf8:b162 with SMTP id e6-20020a056214110600b0067acbf8b162mr11470609qvs.87.1702700752624; Fri, 15 Dec 2023 20:25:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702700752; cv=none; d=google.com; s=arc-20160816; b=0V9e6hWOuM1C3iI57plEYH/a3EE0YEHoEUp2JSB1nm+5GJkFGzH11lkrYrmpvcuzZ4 zvJjNdGWaWFr/+CFKfiFdzfTlXb2g6i3oc6xBpXjPTMY9udname8HJH6Scgjny8VTWy2 q1aVVOwIrILbyx9k7fSD5y20M0GeU2zaqxhVndUJcDEMtxVBAz9HTbSmqLd+4+01kW4p ju5VFrw4Vt/Hr5fA9nggTD4SgYQpRBaCacpN6XFVUzUEevU5Yh2GULREPrsdUHWoXNcb /WKHCumOVcBNxFjZwG2onEKhTYhVvvqHGSmtb+jmXZB2hFrpcm8RmHaldduZCxkTpQz3 CouA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=7yWmTZz6xBQfotj9D8RbHPBcVW8g+FDm4fTllJi7Ds4=; fh=mTYg3JJ8i2GBNLFt8qb4bgYwOyk0kmrOzICLbi6qJ1c=; b=WIwKRT9pkuh1YBvoZgZxzxU7YlPnM1hgAc0sKO/bigNhQUU5t0PYCNVkQ4VeCPqHu9 6E0EWDHEOwFp9B638unEL+8wtg8CMSNBSMvISNhGbZ1o1FC8jeG/mccV4urH+K8ObXgT Tp3lDjHJfIX9XtKM8HIBxEQBlkz46Y34tKmhfdXpEXy4CSYQTVNcYuJ/NdlvBgbQM3Gu P/K+o2Am8DgUiunu0QG+oFhmBJ4OPeXZ4+HBIbBE8gSC/20HiAMe/FOkdvOcolLPHyEm L9E6+3pb5koWJuwkt/aG2yU4BQhL2maElp17CjUEac4n9JVoibq9Wyu4SM3a1WOcna0J Wykg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2058-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2058-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t16-20020a0ce2d0000000b0067ef911bb5asi1174135qvl.342.2023.12.15.20.25.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 20:25:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2058-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-2058-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2058-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 661091C2433C for ; Sat, 16 Dec 2023 04:25:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 285ED3526F; Sat, 16 Dec 2023 04:21:57 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 936FE328B6 for ; Sat, 16 Dec 2023 04:21:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 27FF5C433C7; Sat, 16 Dec 2023 04:21:55 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rEMCM-00000002yHl-0uWe; Fri, 15 Dec 2023 23:22:46 -0500 Message-ID: <20231216042245.999106507@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 15 Dec 2023 23:22:29 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [for-linus][PATCH 15/15] ring-buffer: Do not record in NMI if the arch does not support cmpxchg in NMI References: <20231216042214.905262999@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785411144568802495 X-GMAIL-MSGID: 1785411144568802495 From: "Steven Rostedt (Google)" As the ring buffer recording requires cmpxchg() to work, if the architecture does not support cmpxchg in NMI, then do not do any recording within an NMI. Link: https://lore.kernel.org/linux-trace-kernel/20231213175403.6fc18540@gandalf.local.home Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index f4679013289b..5a114e752f11 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -3674,6 +3674,12 @@ rb_reserve_next_event(struct trace_buffer *buffer, int nr_loops = 0; int add_ts_default; + /* ring buffer does cmpxchg, make sure it is safe in NMI context */ + if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && + (unlikely(in_nmi()))) { + return NULL; + } + rb_start_commit(cpu_buffer); /* The commit page can not change after this */