Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp394186rdb; Tue, 23 Jan 2024 03:08:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IEX5oOJZhHFxEfsYBKE6tAB6LXepW5kTjPlFoeI3k9EKgtlIxnBnQPMOph/KCBcb64dwgfA X-Received: by 2002:a05:6512:6c2:b0:50e:c642:3aed with SMTP id u2-20020a05651206c200b0050ec6423aedmr3056606lff.43.1706008092522; Tue, 23 Jan 2024 03:08:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706008092; cv=pass; d=google.com; s=arc-20160816; b=HDoiAWsVzM1qteB3knnaSf83gZu777VpAYsqUW1bN7u4IXvSv7ir+RAqBd/zhlyiOM TIaIDBwwCIWvDYnd06yHXhURojSr+JFxaxiJVNVHwOjjgDVyGYUfn5ATK8QuRM4GkNsH hbHHYuy5+bUFQ6J++OLp28pBO2PPrhm6hmtYUD/spPYVSLNPntV/Ktmxna3NfVYiUxo2 I2z2rejqyDNqdfHsz0S4pXP6Wtp4FwDEAHgZVjpbXDuGXXkjW7AHZDFdn4YT65QqqI3u zZvn2E2DEyfqq8EYFxzT+Ayqlf//VCQ5980zD5GHPXYc0DEaY6lpNIwLuntywCTBZqLZ uSBg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=7bCkUi25orQO9x3R58BSRYv7KUbFdewUElN+OYKzwq0=; fh=Osewoyidmyt6GUNpr1HkxMO6OLm/aX24FkE9kqTXAtA=; b=0wmVkk0lmeCXVm7/3uX9hVluVisgHoEwjkB9NFKLfARhwJ+MypQCLIFs0Rwk27Kr1u MZaXc3ETCbPxug8vxt8whN9pusI915vULL2pwgMgqv2C1yl5zyppLpZ0LNDGqRXad2g2 InagBGMe2RGGQj2dqLdF1IjzxUkdM4WaUINPyMLxSXhrtZQR3qV5XR87svngUffNenhh KgvqXcLh4Il888MAyDg8It7skh0jDxUGMLgD9gn+8RefmfW2m7HgdgpBWR3j11896yWj ktN6UC1z3w1nfwcS0Rm5GF/J3DUaAp4ZjS+1FPT4lQDkA4XFf1fNSwv5tvEUSUqNaBmG VUFw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pbb2toz5; arc=pass (i=1 spf=pass spfdomain=flex--vdonnefort.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-35157-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35157-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id gu21-20020a170906f29500b00a3076949d48si1447159ejb.312.2024.01.23.03.08.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 03:08:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35157-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pbb2toz5; arc=pass (i=1 spf=pass spfdomain=flex--vdonnefort.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-35157-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35157-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 EFDF81F22894 for ; Tue, 23 Jan 2024 11:08:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7052E5D725; Tue, 23 Jan 2024 11:08:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pbb2toz5" Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D10B85C90B for ; Tue, 23 Jan 2024 11:08:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008085; cv=none; b=B1dsKxYQKvNf2G49JvtILD/9m/rU90SRQ37v2EEOs6ckisfapFDndvF/rMYE6BwC3sReqv94RjYKY+9dK1ohwZ5OoOHsJS3ZpoeMmkgu4I0rzTRVFIq6SeG76shonANaDBdgeHNi1vHsw7loqMzXtM34MSQrZjm7RYc546yzyOU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008085; c=relaxed/simple; bh=/WqZhdO9B3n6i+QEfAJZMfeGldCMX4PS9Sj3wBElUGg=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=MqcnixptnJb6lRWx2bFhWClyugvvfo7qtMC2DG71UwPbUPumHXjuxzUnjqw81LItxm6DzFGHrTseIfS8mV21nT2v8ht4ZvXMb/4xZayfPR3zMLm8/ZqaFwA+t5M8pfrHOI3qn7mUkhD1cMCDpksY/D/Ci2ZorZZ5xnYeqcgF4dA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pbb2toz5; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-337d70f889cso2701144f8f.2 for ; Tue, 23 Jan 2024 03:08:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706008081; x=1706612881; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=7bCkUi25orQO9x3R58BSRYv7KUbFdewUElN+OYKzwq0=; b=pbb2toz5XRzGEg8YWrsMqnk4b+UNwTNwNihXJ4R8i1PsaRB361vdWh9acd3LLNNi/H XXjyMpAvUGfUw0yYk1yvHmfg6d+UwDdmjq4l+8pTpCKCk6a9yqvxkqTfJDzX+rGEFoJS JeJPdf4iKQU/QZBJopwNPm9gPwBJtjOcHdMVtuC3w2lAQCczft4Zn4ArzexPIxNVXr6C Y2dqaNHA6mWoyBJOwmgUCmDmgr73AJH7xmicCzBcuku+/YVKSkyash+LyRiv/s8pj7V6 oaiUJ7NFI7dg7J8Hbd4slyKLX60d5S8n7L/yU9OKk338LgxWQImElBBOTqb5cflfKDyw wvcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706008081; x=1706612881; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=7bCkUi25orQO9x3R58BSRYv7KUbFdewUElN+OYKzwq0=; b=BJkkD2wo5jYABcD1hVZj5S19O+OhbWib7Qhz7c7iKNUz3YbgoRt54mBBTsW4u4T5ag 85xOFojoF4+LM/5KSOm+kYraJStVpOAxNm1PDUMn8lW2cHBVzc4wFbIRNZbJ3zKrpaZJ GWiRBiVefZfdrc4MRTD8uWmErFhB2pklFtx7LwUmuiQ49GQJmi3OEx2O8ZfPf74IJ/P7 mbfyRLOeHLOwQZV+zs3zzLCPa2tx74mtjwfAbDQZRDYGTFzrooFTtiHqLlWIg3R39uXB S8Y56uDVx2iz+bKlEt9BfHkVnV7CK1sKWZedF9upGr7P2Bqw+luxZSDnnUMqogMyvLdZ 5HCQ== X-Gm-Message-State: AOJu0YyyRqxUjCydF+5oZr7d5spBXqRpLBjXm4W8xRKzIE4mZfucmjV0 7KCuhwV31Ct6u7qeqDnGb1mR0zErio/2NZSWOG5fIEJqXzSGtNAQQPZsVKwh++fGjdZDAm049RV qcHJLUXlYbSXVQJ9Phg== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a5d:4449:0:b0:337:97ee:f408 with SMTP id x9-20020a5d4449000000b0033797eef408mr15661wrr.12.1706008081071; Tue, 23 Jan 2024 03:08:01 -0800 (PST) Date: Tue, 23 Jan 2024 11:07:51 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123110757.3657908-1-vdonnefort@google.com> Subject: [PATCH v12 0/6] Introducing trace buffer mapping by user-space From: Vincent Donnefort To: rostedt@goodmis.org, mhiramat@kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: mathieu.desnoyers@efficios.com, kernel-team@android.com, Vincent Donnefort Content-Type: text/plain; charset="UTF-8" The tracing ring-buffers can be stored on disk or sent to network without any copy via splice. However the later doesn't allow real time processing of the traces. A solution is to give userspace direct access to the ring-buffer pages via a mapping. An application can now become a consumer of the ring-buffer, in a similar fashion to what trace_pipe offers. Support for this new feature can already be found in libtracefs from version 1.8, when built with EXTRA_CFLAGS=-DFORCE_MMAP_ENABLE. Vincent v11 -> v12: * Fix code sample mmap bug. * Add logging in sample code. * Reset tracer in selftest. * Add a refcount for the snapshot users. * Prevent mapping when there are snapshot users and vice versa. * Refine the meta-page. * Fix types in the meta-page. * Collect Reviewed-by. v10 -> v11: * Add Documentation and code sample. * Add a selftest. * Move all the update to the meta-page into a single rb_update_meta_page(). * rb_update_meta_page() is now called from ring_buffer_map_get_reader() to fix NOBLOCK callers. * kerneldoc for struct trace_meta_page. * Add a patch to zero all the ring-buffer allocations. v9 -> v10: * Refactor rb_update_meta_page() * In-loop declaration for foreach_subbuf_page() * Check for cpu_buffer->mapped overflow v8 -> v9: * Fix the unlock path in ring_buffer_map() * Fix cpu_buffer cast with rb_work_rq->is_cpu_buffer * Rebase on linux-trace/for-next (3cb3091138ca0921c4569bcf7ffa062519639b6a) v7 -> v8: * Drop the subbufs renaming into bpages * Use subbuf as a name when relevant v6 -> v7: * Rebase onto lore.kernel.org/lkml/20231215175502.106587604@goodmis.org/ * Support for subbufs * Rename subbufs into bpages v5 -> v6: * Rebase on next-20230802. * (unsigned long) -> (void *) cast for virt_to_page(). * Add a wait for the GET_READER_PAGE ioctl. * Move writer fields update (overrun/pages_lost/entries/pages_touched) in the irq_work. * Rearrange id in struct buffer_page. * Rearrange the meta-page. * ring_buffer_meta_page -> trace_buffer_meta_page. * Add meta_struct_len into the meta-page. v4 -> v5: * Trivial rebase onto 6.5-rc3 (previously 6.4-rc3) v3 -> v4: * Add to the meta-page: - pages_lost / pages_read (allow to compute how full is the ring-buffer) - read (allow to compute how many entries can be read) - A reader_page struct. * Rename ring_buffer_meta_header -> ring_buffer_meta * Rename ring_buffer_get_reader_page -> ring_buffer_map_get_reader_page * Properly consume events on ring_buffer_map_get_reader_page() with rb_advance_reader(). v2 -> v3: * Remove data page list (for non-consuming read) ** Implies removing order > 0 meta-page * Add a new meta page field ->read * Rename ring_buffer_meta_page_header into ring_buffer_meta_header v1 -> v2: * Hide data_pages from the userspace struct * Fix META_PAGE_MAX_PAGES * Support for order > 0 meta-page * Add missing page->mapping. Vincent Donnefort (6): ring-buffer: Zero ring-buffer sub-buffers ring-buffer: Introducing ring-buffer mapping functions tracing: Add snapshot refcount tracing: Allow user-space mapping of the ring-buffer Documentation: tracing: Add ring-buffer mapping ring-buffer/selftest: Add ring-buffer mapping test Documentation/trace/index.rst | 1 + Documentation/trace/ring-buffer-map.rst | 106 ++++++ include/linux/ring_buffer.h | 7 + include/uapi/linux/trace_mmap.h | 46 +++ kernel/trace/ring_buffer.c | 338 +++++++++++++++++- kernel/trace/trace.c | 208 ++++++++++- kernel/trace/trace.h | 6 + kernel/trace/trace_events_trigger.c | 57 ++- tools/testing/selftests/ring-buffer/Makefile | 8 + tools/testing/selftests/ring-buffer/config | 1 + .../testing/selftests/ring-buffer/map_test.c | 188 ++++++++++ 11 files changed, 925 insertions(+), 41 deletions(-) create mode 100644 Documentation/trace/ring-buffer-map.rst create mode 100644 include/uapi/linux/trace_mmap.h create mode 100644 tools/testing/selftests/ring-buffer/Makefile create mode 100644 tools/testing/selftests/ring-buffer/config create mode 100644 tools/testing/selftests/ring-buffer/map_test.c base-commit: 4f1991a92cfe89096b2d1f5583a2e093bdd55c37 -- 2.43.0.429.g432eaa2c6b-goog