From patchwork Thu Jan 11 16:17:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 187436 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1559375dyi; Thu, 11 Jan 2024 08:19:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGm04U0ruOCy8sJQvGCmSQJeu4CoRVA83tCAMWCeW7X/d0LRHt1D3zTfLTBUZ7VqLQBBl9f X-Received: by 2002:ac8:5c88:0:b0:429:a053:e0a1 with SMTP id r8-20020ac85c88000000b00429a053e0a1mr1260528qta.16.1704989940139; Thu, 11 Jan 2024 08:19:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704989940; cv=none; d=google.com; s=arc-20160816; b=zk0P/tqcb3L3MW1cH/E8DudmbrnTBgkjPt4E/SbCRdjzy/gs83n8QdM5n6qAeyGANn Z2PvPF+GA6awwIckb9BmKnqpu6H346vxG3ZTuH61aJ4i1vDPEfFE7J/U1TzV6u7rE3SG loHYsXGG9Vzd6+/x+9WSG+hSLpxi4J7ptyGryVBiWSdKoOKeT2RsOi6Z2uD1Baox4xWu UHiad0yH7eCr4y8U3fWQMC2bU7LA4AqPQrLuS3lSdXY/r7VVbURf1VihEZNwxE4a5f0j +UGxoTXXstZdVnMrSLw+PL/aKtghyL86LCcdyQBkYIUPCY8hf/da8NT28ZceZ4LeuhQw MoGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=urOae5rhFO+lmkwawv/vVpYhVX+PQ6+XyZK8aXYOwuU=; fh=Osewoyidmyt6GUNpr1HkxMO6OLm/aX24FkE9kqTXAtA=; b=gBk8lWec+gY/MNoH4xJkowWFpFDLPzSUY01+uB6KHHcKExaghsWy/PUZ7a6ZoKMdGI ejiyGiXp1Ht4l0Rx3o2ySvRjbf2HxUce62VCKbGXo/MewkdGvjVpHfu+RTcSNtJwaQjO 4LKEoCbqgoC6xlsPsVj2MDNZGxj6aEOWiyjKTmDj9MOEuzpwbelXGFQ5fufeQ1lJ/rHS 83qe4unuXWWJ07NeMdLbzUqG6JFUFw673k2djnIXOpVjYkDVBbSiADvpUDkkppgWKApu w6H75PVVfrkrB9vDaa40QZ3AnIfVu7SYAuG+UlQ9xxdWJWCBNM0JH0mph/uOC2ou1a6M DsSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="K0lnm9+/"; spf=pass (google.com: domain of linux-kernel+bounces-23843-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23843-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id s8-20020a05622a178800b004299d7d03adsi1246162qtk.339.2024.01.11.08.19.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 08:19:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23843-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="K0lnm9+/"; spf=pass (google.com: domain of linux-kernel+bounces-23843-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23843-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E895C1C23242 for ; Thu, 11 Jan 2024 16:18:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 668F552F78; Thu, 11 Jan 2024 16:17:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="K0lnm9+/" Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 DBDAF50241 for ; Thu, 11 Jan 2024 16:17:23 +0000 (UTC) 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-wm1-f74.google.com with SMTP id 5b1f17b1804b1-40e4caa37f5so26392185e9.0 for ; Thu, 11 Jan 2024 08:17:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704989842; x=1705594642; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=urOae5rhFO+lmkwawv/vVpYhVX+PQ6+XyZK8aXYOwuU=; b=K0lnm9+/Oin3Bl9s7WIjjMeZoohAvkHXeGcXPjXRRpLtmbSdpKMlCPUIrdLen9q1tx Xok7zLWfWGECmITMmsZlH2RWJU4phaXF/mXq+sni2IqMbNUwpF8y0glmojJ0a39PMyjo X+Zja6GZVrRdxVF2mvB7l1rzdR4ESOjt0+gC4vGq5MeCl6BMTAOEn2nVmOyLQWllmOQ7 /Z+N2snbfFtgqwanuC0akjvAI2Y1kKI7cLAfTXgCWZyuCXTJIQag+XVUHQqqUphjG1Dv msliKn0Zsth/LnMX+yD/AwpocbbOjY98yixE+nVnZy5gkkPEKuMt1fsCaQ/r2jUhpFVx rMeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704989842; x=1705594642; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=urOae5rhFO+lmkwawv/vVpYhVX+PQ6+XyZK8aXYOwuU=; b=xJIUWpRqy6qqf40TM5HFx0/LjFsON7lQB83sPbVKI1zhgj2GClUG+a43O/1sPE+wEe jot2wQvrGhPvP5AgM9ZAPTESvqF+AjIKbzodurc1SN/IetAKJncms7TAad2FmQu5NAUs zP+mMgHhDiihOi4pvr0oQR+kQJ7e1zkXU8s6c88SERq3pwGoKaUZalgklksNoDBJYOem A5RcZCgb77P5gUVg3P1RQy+32tA1Z5uq0zvDfkWxjEbj65sGbmbGfrUPnEro5yu/G/DB 02qRUMbVkE8WUJ0Sfxf/JZS4zY+qoCDoVL5gMUTWCsTE1w1NO03sir68aHDFOmwAt8pZ rwPQ== X-Gm-Message-State: AOJu0Ywj//pLFHj2gOBNO9HdEnWPstPpUl4QX8T+DrbhuO95l/OOqiDX z50VU3WE7lDSIpxoX5Tsi3uuRlxegLSRM/AkiC0BNI8= X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:3b0c:b0:40d:5e86:fe9e with SMTP id m12-20020a05600c3b0c00b0040d5e86fe9emr173wms.5.1704989841944; Thu, 11 Jan 2024 08:17:21 -0800 (PST) Date: Thu, 11 Jan 2024 16:17:08 +0000 In-Reply-To: <20240111161712.1480333-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240111161712.1480333-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240111161712.1480333-2-vdonnefort@google.com> Subject: [PATCH v11 1/5] ring-buffer: Zero ring-buffer sub-buffers 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787811531521434552 X-GMAIL-MSGID: 1787811531521434552 In preparation for the ring-buffer memory mapping where each subbuf will be accessible to user-space, zero all the page allocations. Signed-off-by: Vincent Donnefort Reviewed-by: Masami Hiramatsu (Google) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 173d2595ce2d..db73e326fa04 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1466,7 +1466,8 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, list_add(&bpage->list, pages); - page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), mflags, + page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), + mflags | __GFP_ZERO, cpu_buffer->buffer->subbuf_order); if (!page) goto free_pages; @@ -1551,7 +1552,8 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) cpu_buffer->reader_page = bpage; - page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, cpu_buffer->buffer->subbuf_order); + page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL | __GFP_ZERO, + cpu_buffer->buffer->subbuf_order); if (!page) goto fail_free_reader; bpage->page = page_address(page); @@ -5525,7 +5527,8 @@ ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu) if (bpage->data) goto out; - page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL | __GFP_NORETRY, + page = alloc_pages_node(cpu_to_node(cpu), + GFP_KERNEL | __GFP_NORETRY | __GFP_ZERO, cpu_buffer->buffer->subbuf_order); if (!page) { kfree(bpage); From patchwork Thu Jan 11 16:17:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 187437 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1559706dyi; Thu, 11 Jan 2024 08:19:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGGRgqadvqUpleEYKABPH8NMyFODyrniII3DRiJnEkb4NzCZ7WAKzVPcbfPmDX3dvyjKh+p X-Received: by 2002:a17:902:6545:b0:1d5:8cbb:ad55 with SMTP id d5-20020a170902654500b001d58cbbad55mr1015549pln.134.1704989971908; Thu, 11 Jan 2024 08:19:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704989971; cv=none; d=google.com; s=arc-20160816; b=Kncvv2b5ugDOoU1/yR0DlIhdrwtre8zU+OtoFwhrpOtZBlt57LV9qM5p2FVPu5hJ4A Yq6gVl2MVY3qL50DP5IsamEppS1dOQzJVx+HM2SyuSb4zxaxwZZU3IvqTM9c9eN5lMHF a/fjIqWFEFRVoX6B0LzgIT3suzGKnQR94gRy8kAlSrgzgNhhmIegqKjKgksCFGYawYN1 WIYuIc8joc9CZ4Qx9K7P1lpu57Q5b0yRGaNVtvPBZ2st04espAGrYrml5Z/GVwoDWymk J5dKumSoPdqnt32rOVXsc0eKZBg+PxAQxTNc4dN1ydkh+K+LN7BaKzc8A370vUU0Tfdd fifg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=tDSYk7XYW6OJAyFvY7TTHo+xr/O1Sc5t/M2lwwtKZ4I=; fh=Osewoyidmyt6GUNpr1HkxMO6OLm/aX24FkE9kqTXAtA=; b=gco7DeQHTVo0lJOwpKpgrz75aMu/C0wrDF0I6uhZ7J8aNU3uRkFXicY44x/ws1J2ne Uou9ujZ+T9Mnn09ckSA4VrBCpl4Qk8msmRLu9n0L5tsD//Hpo27H7azfuTXSGfFbtJfT Pp/BbHwxs+zLkqbKqzKgkIksq9MrDtfgZY4JU9ZcTP4qG6MaHP7hbILsJ/w0ax18oq6d GgWXHmcq8TcSgvq63hCTnMXJgV9R2aBscH8IG7S21h6BAtrsh8UnyuYvciEcGIPJL30e V0nMx37d3iPdvTqn60PvKfio/GMziUV95aKzMHbITaZxJgAT/w4YTca+cQuV0mQZQzbB 1zzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=W2MEm62t; spf=pass (google.com: domain of linux-kernel+bounces-23844-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23844-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id m20-20020a170902c45400b001d49621cb08si1330123plm.200.2024.01.11.08.19.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 08:19:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23844-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=W2MEm62t; spf=pass (google.com: domain of linux-kernel+bounces-23844-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23844-ouuuleilei=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A76BB287689 for ; Thu, 11 Jan 2024 16:19:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1F47352F73; Thu, 11 Jan 2024 16:17:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W2MEm62t" Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.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 0E484524CE for ; Thu, 11 Jan 2024 16:17:25 +0000 (UTC) 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-wm1-f73.google.com with SMTP id 5b1f17b1804b1-40e46bceed8so36387275e9.0 for ; Thu, 11 Jan 2024 08:17:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704989844; x=1705594644; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tDSYk7XYW6OJAyFvY7TTHo+xr/O1Sc5t/M2lwwtKZ4I=; b=W2MEm62tY4oCFHtvsWJG2vhUIpagMYTO6SiB7O8eUyS9t079Mb5J1ilhTYQIKzWm8o 3FLNNkLxkLr6iNlLk5dnARceN9ngz+xISe64GAUCcCWtMhz8ZFKT+kZhs1wbEzxchqGZ MfxNdqWckoRsHngZGA4bmU2uXYb9XZ6Ud9wMMo64hT0CjCDYXoUi7Yr0GpGKpPRfl+8g Au+wgSwpRD0I+pqlNyaE/j3mm45PHVM8unB4uFHibxrf/DO1SNgI2SPlt7MiD/iMTtfM Z61Pl6WUwYTElUv8rE28VujfzMAmEHnAnYym/iPwsQlkn89cFKIO3nhg2JtK6qzUI8cO 7YxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704989844; x=1705594644; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tDSYk7XYW6OJAyFvY7TTHo+xr/O1Sc5t/M2lwwtKZ4I=; b=Y9Z8RtsZjZVRfMH6P7gunjOHM3sqZmf9bd465gaDpd0a2E/TDq+qR/7PfHqyzF7xit shAUA0qzCYLkNIay9eABH0rEoNdVydOVNQ33P02+4NFJg/ZAkSCTkdW/FdJS9GNxL1um xNqXQqP+/qITrHAnRjTO5RrjaJww1AeABqwJ1+zZlFoxjlq1qzS3tDocjxx0bVF9PdvL GTeIrk9P701M1rBEP+lbWXeko70q/5iNLTm6kQFHqinL3uoW8WZB+dzAF+xUFACpXVpm XP+s4XvqDT+JL1razAGwPspzVAsUz+UIGmTGbJtGAdrZhPF2pF6XwRNXOM316X4BoqnS Nipw== X-Gm-Message-State: AOJu0Yx23e0IBM/0lgdOfHLUKNB10UaG/z/QGGq5b+aC5PMcUtO7chRo N0qz17si5J23znnpaU0GNLKlDDZc9q1jrzhSG2Lp8ks= X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:1c88:b0:40e:48b2:b337 with SMTP id k8-20020a05600c1c8800b0040e48b2b337mr147wms.5.1704989844417; Thu, 11 Jan 2024 08:17:24 -0800 (PST) Date: Thu, 11 Jan 2024 16:17:09 +0000 In-Reply-To: <20240111161712.1480333-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240111161712.1480333-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240111161712.1480333-3-vdonnefort@google.com> Subject: [PATCH v11 2/5] ring-buffer: Introducing ring-buffer mapping functions 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787811564811962190 X-GMAIL-MSGID: 1787811564811962190 In preparation for allowing the user-space to map a ring-buffer, add a set of mapping functions: ring_buffer_{map,unmap}() ring_buffer_map_fault() And controls on the ring-buffer: ring_buffer_map_get_reader() /* swap reader and head */ Mapping the ring-buffer also involves: A unique ID for each subbuf of the ring-buffer, currently they are only identified through their in-kernel VA. A meta-page, where are stored ring-buffer statistics and a description for the current reader The linear mapping exposes the meta-page, and each subbuf of the ring-buffer, ordered following their unique ID, assigned during the first mapping. Once mapped, no subbuf can get in or out of the ring-buffer: the buffer size will remain unmodified and the splice enabling functions will in reality simply memcpy the data instead of swapping subbufs. Signed-off-by: Vincent Donnefort diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h index fa802db216f9..0841ba8bab14 100644 --- a/include/linux/ring_buffer.h +++ b/include/linux/ring_buffer.h @@ -6,6 +6,8 @@ #include #include +#include + struct trace_buffer; struct ring_buffer_iter; @@ -221,4 +223,9 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node); #define trace_rb_cpu_prepare NULL #endif +int ring_buffer_map(struct trace_buffer *buffer, int cpu); +int ring_buffer_unmap(struct trace_buffer *buffer, int cpu); +struct page *ring_buffer_map_fault(struct trace_buffer *buffer, int cpu, + unsigned long pgoff); +int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu); #endif /* _LINUX_RING_BUFFER_H */ diff --git a/include/uapi/linux/trace_mmap.h b/include/uapi/linux/trace_mmap.h new file mode 100644 index 000000000000..bde39a73ce65 --- /dev/null +++ b/include/uapi/linux/trace_mmap.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _TRACE_MMAP_H_ +#define _TRACE_MMAP_H_ + +#include + +/** + * struct trace_buffer_meta - Ring-buffer Meta-page description + * @entries: Number of entries in the ring-buffer. + * @overrun: Number of entries lost in the ring-buffer. + * @read: Number of entries that have been read. + * @subbufs_touched: Number of subbufs that have been filled. + * @subbufs_lost: Number of subbufs lost to overrun. + * @subbufs_read: Number of subbufs that have been read. + * @reader.lost_events: Number of events lost at the time of the reader swap. + * @reader.id: subbuf ID of the current reader. From 0 to @nr_subbufs - 1 + * @reader.read: Number of bytes read on the reader subbuf. + * @subbuf_size: Size of each subbuf, including the header. + * @nr_subbufs: Number of subbfs in the ring-buffer. + * @meta_page_size: Size of this meta-page. + * @meta_struct_len: Size of this structure. + */ +struct trace_buffer_meta { + unsigned long entries; + unsigned long overrun; + unsigned long read; + + unsigned long subbufs_touched; + unsigned long subbufs_lost; + unsigned long subbufs_read; + + struct { + unsigned long lost_events; + __u32 id; + __u32 read; + } reader; + + __u32 subbuf_size; + __u32 nr_subbufs; + + __u32 meta_page_size; + __u32 meta_struct_len; +}; + +#endif /* _TRACE_MMAP_H_ */ diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index db73e326fa04..e9ff1c95e896 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -338,6 +338,7 @@ struct buffer_page { local_t entries; /* entries on this page */ unsigned long real_end; /* real end of data */ unsigned order; /* order of the page */ + u32 id; /* ID for external mapping */ struct buffer_data_page *page; /* Actual data page */ }; @@ -484,6 +485,12 @@ struct ring_buffer_per_cpu { u64 read_stamp; /* pages removed since last reset */ unsigned long pages_removed; + + int mapped; + struct mutex mapping_lock; + unsigned long *subbuf_ids; /* ID to addr */ + struct trace_buffer_meta *meta_page; + /* ring buffer pages to update, > 0 to add, < 0 to remove */ long nr_pages_to_update; struct list_head new_pages; /* new pages to add */ @@ -1542,6 +1549,7 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters); init_waitqueue_head(&cpu_buffer->irq_work.waiters); init_waitqueue_head(&cpu_buffer->irq_work.full_waiters); + mutex_init(&cpu_buffer->mapping_lock); bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), GFP_KERNEL, cpu_to_node(cpu)); @@ -5160,6 +5168,23 @@ static void rb_clear_buffer_page(struct buffer_page *page) page->read = 0; } +static void rb_update_meta_page(struct ring_buffer_per_cpu *cpu_buffer) +{ + struct trace_buffer_meta *meta = cpu_buffer->meta_page; + + WRITE_ONCE(meta->entries, local_read(&cpu_buffer->entries)); + WRITE_ONCE(meta->overrun, local_read(&cpu_buffer->overrun)); + WRITE_ONCE(meta->read, cpu_buffer->read); + + WRITE_ONCE(meta->subbufs_touched, local_read(&cpu_buffer->pages_touched)); + WRITE_ONCE(meta->subbufs_lost, local_read(&cpu_buffer->pages_lost)); + WRITE_ONCE(meta->subbufs_read, local_read(&cpu_buffer->pages_read)); + + WRITE_ONCE(meta->reader.read, cpu_buffer->reader_page->read); + WRITE_ONCE(meta->reader.id, cpu_buffer->reader_page->id); + WRITE_ONCE(meta->reader.lost_events, cpu_buffer->lost_events); +} + static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) { @@ -5204,6 +5229,9 @@ rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) cpu_buffer->lost_events = 0; cpu_buffer->last_overrun = 0; + if (cpu_buffer->mapped) + rb_update_meta_page(cpu_buffer); + rb_head_page_activate(cpu_buffer); cpu_buffer->pages_removed = 0; } @@ -5418,6 +5446,11 @@ int ring_buffer_swap_cpu(struct trace_buffer *buffer_a, cpu_buffer_a = buffer_a->buffers[cpu]; cpu_buffer_b = buffer_b->buffers[cpu]; + if (READ_ONCE(cpu_buffer_a->mapped) || READ_ONCE(cpu_buffer_b->mapped)) { + ret = -EBUSY; + goto out; + } + /* At least make sure the two buffers are somewhat the same */ if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages) goto out; @@ -5682,7 +5715,8 @@ int ring_buffer_read_page(struct trace_buffer *buffer, * Otherwise, we can simply swap the page with the one passed in. */ if (read || (len < (commit - read)) || - cpu_buffer->reader_page == cpu_buffer->commit_page) { + cpu_buffer->reader_page == cpu_buffer->commit_page || + cpu_buffer->mapped) { struct buffer_data_page *rpage = cpu_buffer->reader_page->page; unsigned int rpos = read; unsigned int pos = 0; @@ -5901,6 +5935,11 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) cpu_buffer = buffer->buffers[cpu]; + if (cpu_buffer->mapped) { + err = -EBUSY; + goto error; + } + /* Update the number of pages to match the new size */ nr_pages = old_size * buffer->buffers[cpu]->nr_pages; nr_pages = DIV_ROUND_UP(nr_pages, buffer->subbuf_size); @@ -6002,6 +6041,295 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) } EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_set); +#define subbuf_page(off, start) \ + virt_to_page((void *)(start + (off << PAGE_SHIFT))) + +#define foreach_subbuf_page(sub_order, start, page) \ + page = subbuf_page(0, (start)); \ + for (int __off = 0; __off < (1 << (sub_order)); \ + __off++, page = subbuf_page(__off, (start))) + +static inline void subbuf_map_prepare(unsigned long subbuf_start, int order) +{ + struct page *page; + + /* + * When allocating order > 0 pages, only the first struct page has a + * refcount > 1. Increasing the refcount here ensures none of the struct + * page composing the sub-buffer is freeed when the mapping is closed. + */ + foreach_subbuf_page(order, subbuf_start, page) + page_ref_inc(page); +} + +static inline void subbuf_unmap(unsigned long subbuf_start, int order) +{ + struct page *page; + + foreach_subbuf_page(order, subbuf_start, page) { + page_ref_dec(page); + page->mapping = NULL; + } +} + +static void rb_free_subbuf_ids(struct ring_buffer_per_cpu *cpu_buffer) +{ + int sub_id; + + for (sub_id = 0; sub_id < cpu_buffer->nr_pages + 1; sub_id++) + subbuf_unmap(cpu_buffer->subbuf_ids[sub_id], + cpu_buffer->buffer->subbuf_order); + + kfree(cpu_buffer->subbuf_ids); + cpu_buffer->subbuf_ids = NULL; +} + +static int rb_alloc_meta_page(struct ring_buffer_per_cpu *cpu_buffer) +{ + if (cpu_buffer->meta_page) + return 0; + + cpu_buffer->meta_page = page_to_virt(alloc_page(GFP_USER | __GFP_ZERO)); + if (!cpu_buffer->meta_page) + return -ENOMEM; + + return 0; +} + +static void rb_free_meta_page(struct ring_buffer_per_cpu *cpu_buffer) +{ + unsigned long addr = (unsigned long)cpu_buffer->meta_page; + + virt_to_page((void *)addr)->mapping = NULL; + free_page(addr); + cpu_buffer->meta_page = NULL; +} + +static void rb_setup_ids_meta_page(struct ring_buffer_per_cpu *cpu_buffer, + unsigned long *subbuf_ids) +{ + struct trace_buffer_meta *meta = cpu_buffer->meta_page; + unsigned int nr_subbufs = cpu_buffer->nr_pages + 1; + struct buffer_page *first_subbuf, *subbuf; + int id = 0; + + subbuf_ids[id] = (unsigned long)cpu_buffer->reader_page->page; + subbuf_map_prepare(subbuf_ids[id], cpu_buffer->buffer->subbuf_order); + cpu_buffer->reader_page->id = id++; + + first_subbuf = subbuf = rb_set_head_page(cpu_buffer); + do { + if (id >= nr_subbufs) { + WARN_ON(1); + break; + } + + subbuf_ids[id] = (unsigned long)subbuf->page; + subbuf->id = id; + subbuf_map_prepare(subbuf_ids[id], cpu_buffer->buffer->subbuf_order); + + rb_inc_page(&subbuf); + id++; + } while (subbuf != first_subbuf); + + /* install subbuf ID to kern VA translation */ + cpu_buffer->subbuf_ids = subbuf_ids; + + meta->meta_page_size = PAGE_SIZE; + meta->meta_struct_len = sizeof(*meta); + meta->nr_subbufs = nr_subbufs; + meta->subbuf_size = cpu_buffer->buffer->subbuf_size + BUF_PAGE_HDR_SIZE; + + rb_update_meta_page(cpu_buffer); +} + +static inline struct ring_buffer_per_cpu * +rb_get_mapped_buffer(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + + if (!cpumask_test_cpu(cpu, buffer->cpumask)) + return ERR_PTR(-EINVAL); + + cpu_buffer = buffer->buffers[cpu]; + + mutex_lock(&cpu_buffer->mapping_lock); + + if (!cpu_buffer->mapped) { + mutex_unlock(&cpu_buffer->mapping_lock); + return ERR_PTR(-ENODEV); + } + + return cpu_buffer; +} + +static inline void rb_put_mapped_buffer(struct ring_buffer_per_cpu *cpu_buffer) +{ + mutex_unlock(&cpu_buffer->mapping_lock); +} + +int ring_buffer_map(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + unsigned long flags, *subbuf_ids; + int err = 0; + + if (!cpumask_test_cpu(cpu, buffer->cpumask)) + return -EINVAL; + + cpu_buffer = buffer->buffers[cpu]; + + mutex_lock(&cpu_buffer->mapping_lock); + + if (cpu_buffer->mapped) { + if (cpu_buffer->mapped == INT_MAX) + err = -EBUSY; + else + WRITE_ONCE(cpu_buffer->mapped, cpu_buffer->mapped + 1); + mutex_unlock(&cpu_buffer->mapping_lock); + return err; + } + + /* prevent another thread from changing buffer sizes */ + mutex_lock(&buffer->mutex); + + err = rb_alloc_meta_page(cpu_buffer); + if (err) + goto unlock; + + /* subbuf_ids include the reader while nr_pages does not */ + subbuf_ids = kzalloc(sizeof(*subbuf_ids) * (cpu_buffer->nr_pages + 1), + GFP_KERNEL); + if (!subbuf_ids) { + rb_free_meta_page(cpu_buffer); + err = -ENOMEM; + goto unlock; + } + + atomic_inc(&cpu_buffer->resize_disabled); + + /* + * Lock all readers to block any subbuf swap until the subbuf IDs are + * assigned. + */ + raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); + + rb_setup_ids_meta_page(cpu_buffer, subbuf_ids); + + WRITE_ONCE(cpu_buffer->mapped, 1); + + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); +unlock: + mutex_unlock(&buffer->mutex); + mutex_unlock(&cpu_buffer->mapping_lock); + + return err; +} + +int ring_buffer_unmap(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + int err = 0; + + if (!cpumask_test_cpu(cpu, buffer->cpumask)) + return -EINVAL; + + cpu_buffer = buffer->buffers[cpu]; + + mutex_lock(&cpu_buffer->mapping_lock); + + if (!cpu_buffer->mapped) { + err = -ENODEV; + goto unlock; + } + + WRITE_ONCE(cpu_buffer->mapped, cpu_buffer->mapped - 1); + if (!cpu_buffer->mapped) { + /* Wait for the writer and readers to observe !mapped */ + synchronize_rcu(); + + rb_free_subbuf_ids(cpu_buffer); + rb_free_meta_page(cpu_buffer); + atomic_dec(&cpu_buffer->resize_disabled); + } +unlock: + mutex_unlock(&cpu_buffer->mapping_lock); + + return err; +} + +/* + * +--------------+ pgoff == 0 + * | meta page | + * +--------------+ pgoff == 1 + * | subbuffer 0 | + * +--------------+ pgoff == 1 + (1 << subbuf_order) + * | subbuffer 1 | + * ... + */ +struct page *ring_buffer_map_fault(struct trace_buffer *buffer, int cpu, + unsigned long pgoff) +{ + struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; + unsigned long subbuf_id, subbuf_offset, addr; + struct page *page; + + if (!pgoff) + return virt_to_page((void *)cpu_buffer->meta_page); + + pgoff--; + + subbuf_id = pgoff >> buffer->subbuf_order; + if (subbuf_id > cpu_buffer->nr_pages) + return NULL; + + subbuf_offset = pgoff & ((1UL << buffer->subbuf_order) - 1); + addr = cpu_buffer->subbuf_ids[subbuf_id] + (subbuf_offset * PAGE_SIZE); + page = virt_to_page((void *)addr); + + return page; +} + +int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + unsigned long reader_size; + unsigned long flags; + + cpu_buffer = rb_get_mapped_buffer(buffer, cpu); + if (IS_ERR(cpu_buffer)) + return (int)PTR_ERR(cpu_buffer); + + raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); +consume: + if (rb_per_cpu_empty(cpu_buffer)) + goto out; + + reader_size = rb_page_size(cpu_buffer->reader_page); + + /* + * There are data to be read on the current reader page, we can + * return to the caller. But before that, we assume the latter will read + * everything. Let's update the kernel reader accordingly. + */ + if (cpu_buffer->reader_page->read < reader_size) { + while (cpu_buffer->reader_page->read < reader_size) + rb_advance_reader(cpu_buffer); + goto out; + } + + if (WARN_ON(!rb_get_reader_page(cpu_buffer))) + goto out; + + goto consume; +out: + rb_update_meta_page(cpu_buffer); + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); + rb_put_mapped_buffer(cpu_buffer); + + return 0; +} + /* * We only allocate new buffers, never free them if the CPU goes down. * If we were to free the buffer, then the user would lose any trace that was in From patchwork Thu Jan 11 16:17:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 187440 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1560681dyi; Thu, 11 Jan 2024 08:21:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IGYjHxQRIYDvTCurbQqVBe3p+Ta4EJJMPzZNrEhO30kLnYWZDxDk3Cm6SeajFxU91zdnGPD X-Received: by 2002:a05:6358:ce25:b0:175:649f:ebc4 with SMTP id gt37-20020a056358ce2500b00175649febc4mr73645rwb.12.1704990069188; Thu, 11 Jan 2024 08:21:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704990069; cv=none; d=google.com; s=arc-20160816; b=Qsh2w6czDxKTCn+G3hjcVf3wrnE6lJedyR6VY5FJs3gBSRJCs6pIo1Vv7MjfLz+x8t ZP/OghU0+ZD75ajFS7CAQIBEBW87kWtmz48fqXSN4AdvHRzgPFdnEC1OVoPnPBBc2zU9 RMT+UAzsTh7r2C/S++sH4xxMJVsDhplWm3YOhgXF6p+B31uj34kx6Ec0Z2/a6U7qoVuT 6oE7vhQyQ7AIrvClDpHY8H1dJNUg0WEQjqBQcEadqV3LVnnXuDznVLLgkR4fwp/xmqVJ s3fwYLFRlunYMs+eEzR+aVCyvXxbJys/YL7s8s5X+87WVtOqcPL/pJ+U9xUldpSxIyTV rUDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=PtyECdimc05A+gLeHojIfO6SoowPh15bzbJQtIDe3O0=; fh=Osewoyidmyt6GUNpr1HkxMO6OLm/aX24FkE9kqTXAtA=; b=qoLNGecT9KqRyGe4TPCNyKbAEQy7T6+vEy8c0WlPBKZCK/x3WdmAdomXFSFgTVJxqO ZGNcoFQJ0AWzN5bjzTpdHGMCPw6b+Td6IsqB72FRtQ4ECMzcGehVe3Lak4/oWwOtufXE edEQHEDOWsdgNDneg3FGC3DiVA6gVm8IZDxD+HMqZGvYdKJmDl6qr9LHzELqdWG/l6bq ZrQ1NdRRrQvoRHYlrKm69RttvBhFxt+SbirCFA+4/nuGG3B9rIyUMtuOqtIZaQjDHqeF Gm3Ajk3gdaIqPdT8ntTBMvCDV7DfZaWvRz/vYEAVeiTaV/hRorPrqNKUaFz3G5Q/W8dk taxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=njdBluqA; spf=pass (google.com: domain of linux-kernel+bounces-23845-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23845-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id bx40-20020a056a02052800b005cdb49a8117si1520462pgb.1.2024.01.11.08.21.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 08:21:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23845-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; dkim=pass header.i=@google.com header.s=20230601 header.b=njdBluqA; spf=pass (google.com: domain of linux-kernel+bounces-23845-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23845-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 629C1B21884 for ; Thu, 11 Jan 2024 16:19:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3FABB53E28; Thu, 11 Jan 2024 16:17:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="njdBluqA" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 AEF6B52F73 for ; Thu, 11 Jan 2024 16:17:27 +0000 (UTC) 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-yw1-f201.google.com with SMTP id 00721157ae682-5efb07ddb0fso80390307b3.0 for ; Thu, 11 Jan 2024 08:17:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704989846; x=1705594646; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PtyECdimc05A+gLeHojIfO6SoowPh15bzbJQtIDe3O0=; b=njdBluqACu/YDYn/Lvqg2LhQH0RnKnFWv7/li4QdW08WvwLEwRB08c4Bwr5hGD922g rZDN7OYVAa8bYLbFVq67KJi1Wo2TGkSqFwHJGFYeQeAuxW/g8vKsWh+1xVZnS6WYPd3B eQ5S1TZECTbv3GrmohCe4oClBGH1UgYpC4fI3pCwJ9LQQ62JhsLodqBMSjcgyBbrsqUy +QFnY2Lhleu8HAL7deRoLkXXSoQkZSCOhdJqvG09km6z5fJy6F6W9Rk8jw6jcY6rZX3N nB4GkuN9rhrv6yLIUrDxt0G42Hhzw9kHO9qundj5vBAjOPsESecfWrGyIpdxj15tG9RW BS6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704989846; x=1705594646; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PtyECdimc05A+gLeHojIfO6SoowPh15bzbJQtIDe3O0=; b=LJJySGu5tHdeMC4D8VZ+hbnQVLY9Tj2qymWocGKmEvLsfKfViHgDKhgh30QNHdILl3 8QAZhLAO7ZvJFFExyNiq79B6r5y5ogR7BspsdYU2b2mLh34d57W/1B5Bg+kzlegW1zDd 0NZ932lP8DBjE9U7cUvtXMiBaOWoZfr/7yVIn/8LvaVxDrycd6FKfvYFrGhS6VRMc2DF AQuNBQvFaKFdFX0uecEeK6wo+L+BHcCqaB4hyUg/LXbCGTP/4O4XEp2gw0xFSGxusF0F yPZr2rWeddYe3v/0+zLPqUdHA36tHL8ZVA7c7d9plq/t6BuqyiI8Slr7t3vI6LNsPMo9 7Shw== X-Gm-Message-State: AOJu0YxF2Ata4SRwoRa+C94czmJq1vJIsCi1jlJaCxnMOM0NVDIG1qY0 7fr5MwLl/+biPCcByNJgZsnaOOqunfkD6763tmYREJ0= X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a81:dd14:0:b0:5fb:7e5b:b87d with SMTP id e20-20020a81dd14000000b005fb7e5bb87dmr1942ywn.10.1704989846711; Thu, 11 Jan 2024 08:17:26 -0800 (PST) Date: Thu, 11 Jan 2024 16:17:10 +0000 In-Reply-To: <20240111161712.1480333-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240111161712.1480333-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240111161712.1480333-4-vdonnefort@google.com> Subject: [PATCH v11 3/5] tracing: Allow user-space mapping of the ring-buffer 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787811666383233251 X-GMAIL-MSGID: 1787811666383233251 Currently, user-space extracts data from the ring-buffer via splice, which is handy for storage or network sharing. However, due to splice limitations, it is imposible to do real-time analysis without a copy. A solution for that problem is to let the user-space map the ring-buffer directly. The mapping is exposed via the per-CPU file trace_pipe_raw. The first element of the mapping is the meta-page. It is followed by each subbuffer constituting the ring-buffer, ordered by their unique page ID: * Meta-page -- include/uapi/linux/trace_mmap.h for a description * Subbuf ID 0 * Subbuf ID 1 ... It is therefore easy to translate a subbuf ID into an offset in the mapping: reader_id = meta->reader->id; reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size; When new data is available, the mapper must call a newly introduced ioctl: TRACE_MMAP_IOCTL_GET_READER. This will update the Meta-page reader ID to point to the next reader containing unread data. Signed-off-by: Vincent Donnefort diff --git a/include/uapi/linux/trace_mmap.h b/include/uapi/linux/trace_mmap.h index bde39a73ce65..a797891e3ba0 100644 --- a/include/uapi/linux/trace_mmap.h +++ b/include/uapi/linux/trace_mmap.h @@ -42,4 +42,6 @@ struct trace_buffer_meta { __u32 meta_struct_len; }; +#define TRACE_MMAP_IOCTL_GET_READER _IO('T', 0x1) + #endif /* _TRACE_MMAP_H_ */ diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 46dbe22121e9..7bf6c2942aea 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -6472,7 +6472,7 @@ static void tracing_set_nop(struct trace_array *tr) { if (tr->current_trace == &nop_trace) return; - + tr->current_trace->enabled--; if (tr->current_trace->reset) @@ -8583,15 +8583,31 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos, return ret; } -/* An ioctl call with cmd 0 to the ring buffer file will wake up all waiters */ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct ftrace_buffer_info *info = file->private_data; struct trace_iterator *iter = &info->iter; + int err; - if (cmd) - return -ENOIOCTLCMD; + if (cmd == TRACE_MMAP_IOCTL_GET_READER) { + if (!(file->f_flags & O_NONBLOCK)) { + err = ring_buffer_wait(iter->array_buffer->buffer, + iter->cpu_file, + iter->tr->buffer_percent); + if (err) + return err; + } + + return ring_buffer_map_get_reader(iter->array_buffer->buffer, + iter->cpu_file); + } else if (cmd) { + return -ENOTTY; + } + /* + * An ioctl call with cmd 0 to the ring buffer file will wake up all + * waiters + */ mutex_lock(&trace_types_lock); iter->wait_index++; @@ -8604,6 +8620,62 @@ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned return 0; } +static vm_fault_t tracing_buffers_mmap_fault(struct vm_fault *vmf) +{ + struct ftrace_buffer_info *info = vmf->vma->vm_file->private_data; + struct trace_iterator *iter = &info->iter; + vm_fault_t ret = VM_FAULT_SIGBUS; + struct page *page; + + page = ring_buffer_map_fault(iter->array_buffer->buffer, iter->cpu_file, + vmf->pgoff); + if (!page) + return ret; + + get_page(page); + vmf->page = page; + vmf->page->mapping = vmf->vma->vm_file->f_mapping; + vmf->page->index = vmf->pgoff; + + return 0; +} + +static void tracing_buffers_mmap_close(struct vm_area_struct *vma) +{ + struct ftrace_buffer_info *info = vma->vm_file->private_data; + struct trace_iterator *iter = &info->iter; + + ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file); +} + +static void tracing_buffers_mmap_open(struct vm_area_struct *vma) +{ + struct ftrace_buffer_info *info = vma->vm_file->private_data; + struct trace_iterator *iter = &info->iter; + + WARN_ON(ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file)); +} + +static const struct vm_operations_struct tracing_buffers_vmops = { + .open = tracing_buffers_mmap_open, + .close = tracing_buffers_mmap_close, + .fault = tracing_buffers_mmap_fault, +}; + +static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma) +{ + struct ftrace_buffer_info *info = filp->private_data; + struct trace_iterator *iter = &info->iter; + + if (vma->vm_flags & VM_WRITE) + return -EPERM; + + vm_flags_mod(vma, VM_DONTCOPY | VM_DONTDUMP, VM_MAYWRITE); + vma->vm_ops = &tracing_buffers_vmops; + + return ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file); +} + static const struct file_operations tracing_buffers_fops = { .open = tracing_buffers_open, .read = tracing_buffers_read, @@ -8612,6 +8684,7 @@ static const struct file_operations tracing_buffers_fops = { .splice_read = tracing_buffers_splice_read, .unlocked_ioctl = tracing_buffers_ioctl, .llseek = no_llseek, + .mmap = tracing_buffers_mmap, }; static ssize_t From patchwork Thu Jan 11 16:17:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 187438 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1559916dyi; Thu, 11 Jan 2024 08:19:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IG/DKKh681JiIBLNXrgYQKUYOCoHAMa2IdfeVOnb732LgKpY2/xFXASmMFeWpsYxRdFmmei X-Received: by 2002:a9d:6a8b:0:b0:6dc:6fa8:fafa with SMTP id l11-20020a9d6a8b000000b006dc6fa8fafamr23049otq.38.1704989991834; Thu, 11 Jan 2024 08:19:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704989991; cv=none; d=google.com; s=arc-20160816; b=jQfr/PJr78tqLCOsRTd3xyBi+afYnmoTRx4+VhY62DFsE8w4egbVOikRjEFOs9HebS d3i5zsrd5EfuFeLDBxUf3W5EcOzuHifyY5KVfK/4G75iHOYomdzg0ER2sVL1qPUDBpGb Pa3zcedL/CxJhqaXDKxjHsPgnmJNeJuGUP9i798cGZuj6SlXQXC42x421ye+sLFD+013 B8WrRA86LqCbibcZjiE1O6tNMXeWDtB0y4U9JWrTCd3lLZBTj86rQh1GJ9X2nV36HcHW mxSDzUb4DoyT9NPqxmu20TTgHGmHXfYI7JVnTrMjlxK/DOMy1WJjnyO3UMT0istu4tfR uO4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=XCuyvwf8Wck/dCBmVNrAbagpZQESmdweCPz4Yt1A3mA=; fh=Osewoyidmyt6GUNpr1HkxMO6OLm/aX24FkE9kqTXAtA=; b=VGnf1BIthchO0hgajwy38ToaM6DUxDLGiaQYbPIXrEwdk0hq3xxAIixwwbwzQt0gTm T8K5KzM0R7iaomux36OqXJkEJSzNzVM2L7PnNRxwXUDSx0xLYIHnG3vVOJPFS1oYckW3 h+rxM+ej1O3C1Pi1dmP86ECoX7SUH3sSOL9KBOnJ5uWSKu3qMwV5AAKpCiuovNf33mdD LYLr6yl8viHl7EDRxIndbZPcsmRnVHPlC2OG/AUJagrwXNZ7bZMSOHuYGdEvOJ72kmKT G63NrNZOJFCB7hQmOKaTUccVeLlAQiuv9wHBbJEKXsRaWzSuGs063WEE+xbSaUX6GvFZ 60Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Yl5sYijT; spf=pass (google.com: domain of linux-kernel+bounces-23846-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23846-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id v17-20020a05622a189100b004299a99c7e4si1232853qtc.757.2024.01.11.08.19.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 08:19:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23846-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Yl5sYijT; spf=pass (google.com: domain of linux-kernel+bounces-23846-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23846-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 825911C23685 for ; Thu, 11 Jan 2024 16:19:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5054F5576B; Thu, 11 Jan 2024 16:17:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Yl5sYijT" 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 89A3A53800 for ; Thu, 11 Jan 2024 16:17:30 +0000 (UTC) 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-33769c5db4aso3309908f8f.0 for ; Thu, 11 Jan 2024 08:17:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704989849; x=1705594649; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=XCuyvwf8Wck/dCBmVNrAbagpZQESmdweCPz4Yt1A3mA=; b=Yl5sYijTJddYPE8A0XbzsIxeUXUxr0R4yCq8ZzoKqOZLWBwT59vScwLfcMHzGaEu6o 6db7yvLqTcwCj3kmHSKnsoA7tsyBhu3XnFD0jJgCUhT17S2t6Xg3M2GPq/JTCg3rSYh1 kgO+QW8caVlNLBRh+oPED2UD5vqxTKGaMYkqUu24QuTZbEEZlf7yclpmBbDHNg63nu7U 21SDh14jgxJFTHdQ4Klav2uQNq3erZlwpA+UGLAxlJ5lm1u+BBJ/kgkV83Y+XT130706 iRF1p64/p6IipwupCJHs34EWLxrJmLjOX2QgM7855UYsqrEVGkXGoPbCUNG1Fp/Cgc0+ Pu2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704989849; x=1705594649; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XCuyvwf8Wck/dCBmVNrAbagpZQESmdweCPz4Yt1A3mA=; b=oO3mqcogYJUj6mhDwo6aIjeLzJIr+pSfw9zi5llD6IocTu9lLGUQ405mI+8a/DspE1 YLKGscHbVr6u8SxCnAlMkTguSE7Osl/PKJnMGs/OoVpMdORK9xxJcPc3fWR0iGy5VBIR wjAD2Z70Kf+1QKogScP3t13k1HT2kQr/Iz0PxN8fTNtDZ+6/vUMiNJhKvGN7h6VqZH99 VrE0wAjLSJFrZjQl9ULLZfM44maZbRF8txmQYVbeFaiRN2P6A2Ewjmpx62DjLigRneNu z+KT7rdSn00ez24Bi2qn65+KrfL6bbi65KGciqqMx16tlFVeL8LJu/i+7L7XaYOO8Oic KhQg== X-Gm-Message-State: AOJu0YzhYMrzSuY05dcKNeGaufPpxuHVTDUoahF9ic3IIjBLPMvB8MsX 3A5KichaKYTrtC0JmrOk5xjyoeV0G1e6ZX2LoWc/D20= X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:6000:618:b0:337:8f60:972e with SMTP id bn24-20020a056000061800b003378f60972emr1263wrb.3.1704989848845; Thu, 11 Jan 2024 08:17:28 -0800 (PST) Date: Thu, 11 Jan 2024 16:17:11 +0000 In-Reply-To: <20240111161712.1480333-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240111161712.1480333-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240111161712.1480333-5-vdonnefort@google.com> Subject: [PATCH v11 4/5] Documentation: tracing: Add ring-buffer mapping 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787811585394758536 X-GMAIL-MSGID: 1787811585394758536 It is now possible to mmap() a ring-buffer to stream its content. Add some documentation and a code example. Signed-off-by: Vincent Donnefort diff --git a/Documentation/trace/index.rst b/Documentation/trace/index.rst index 5092d6c13af5..0b300901fd75 100644 --- a/Documentation/trace/index.rst +++ b/Documentation/trace/index.rst @@ -29,6 +29,7 @@ Linux Tracing Technologies timerlat-tracer intel_th ring-buffer-design + ring-buffer-map stm sys-t coresight/index diff --git a/Documentation/trace/ring-buffer-map.rst b/Documentation/trace/ring-buffer-map.rst new file mode 100644 index 000000000000..2ba7b5339178 --- /dev/null +++ b/Documentation/trace/ring-buffer-map.rst @@ -0,0 +1,105 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================== +Tracefs ring-buffer memory mapping +================================== + +:Author: Vincent Donnefort + +Overview +======== +Tracefs ring-buffer memory map provides an efficient method to stream data +as no memory copy is necessary. The application mapping the ring-buffer becomes +then a consumer for that ring-buffer, in a similar fashion to trace_pipe. + +Memory mapping setup +==================== +The mapping works with a mmap() of the trace_pipe_raw interface. + +The first system page of the mapping contains ring-buffer statistics and +description. It is referred as the meta-page. One of the most important field of +the meta-page is the reader. It contains the subbuf ID which can be safely read +by the mapper (see ring-buffer-design.rst). + +The meta-page is followed by all the subbuf, ordered by ascendant ID. It is +therefore effortless to know where the reader starts in the mapping: + +.. code-block:: c + + reader_id = meta->reader->id; + reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size; + +When the application is done with the current reader, it can get a new one using +the trace_pipe_raw ioctl() TRACE_MMAP_IOCTL_GET_READER. This ioctl also updates +the meta-page fields. + +Limitations +=========== +When a mapping is in place on a Tracefs ring-buffer, it is not possible to +either resize it (either by increasing the entire size of the ring-buffer or +each subbuf). It is also not possible to use snapshot or splice. + +Concurrent readers (either another application mapping that ring-buffer or the +kernel with trace_pipe) are allowed but not recommended. They will compete for +the ring-buffer and the output is unpredictable. + +Example +======= + +.. code-block:: c + + #include + #include + #include + #include + + #include + + #include + #include + + #define TRACE_PIPE_RAW "/sys/kernel/tracing/per_cpu/cpu0/trace_pipe_raw" + + int main(void) + { + int page_size = getpagesize(), fd, reader_id; + unsigned long meta_len, data_len; + struct trace_buffer_meta *meta; + void *map, *reader, *data; + + fd = open(TRACE_PIPE_RAW, O_RDONLY); + if (fd < 0) + exit(EXIT_FAILURE); + + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0); + if (map == MAP_FAILED) + exit(EXIT_FAILURE); + + meta = (struct trace_buffer_meta *)map; + meta_len = meta->meta_page_size; + + printf("entries: %lu\n", meta->entries); + printf("overrun: %lu\n", meta->overrun); + printf("read: %lu\n", meta->read); + printf("subbufs_touched:%lu\n", meta->subbufs_touched); + printf("subbufs_lost: %lu\n", meta->subbufs_lost); + printf("subbufs_read: %lu\n", meta->subbufs_read); + printf("nr_subbufs: %u\n", meta->nr_subbufs); + + data_len = meta->subbuf_size * meta->nr_subbufs; + data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, data_len); + if (data == MAP_FAILED) + exit(EXIT_FAILURE); + + if (ioctl(fd, TRACE_MMAP_IOCTL_GET_READER) < 0) + exit(EXIT_FAILURE); + + reader_id = meta->reader.id; + reader = data + meta->subbuf_size * reader_id; + + munmap(data, data_len); + munmap(meta, meta_len); + close (fd); + + return 0; + } From patchwork Thu Jan 11 16:17:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 187439 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1560015dyi; Thu, 11 Jan 2024 08:20:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IGlFaQQqFnxeb2CjaUKG2cWN2OVofeKFAJlqmFohx0Af/SGBQJFQJcL+zIB6yY08WSXBl0l X-Received: by 2002:a05:6402:1acb:b0:54c:4837:9a99 with SMTP id ba11-20020a0564021acb00b0054c48379a99mr808072edb.64.1704990001956; Thu, 11 Jan 2024 08:20:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704990001; cv=none; d=google.com; s=arc-20160816; b=BqVZBXxTM/UGG3ktJ5e+5AtdTZ5NRJ5NOqQf0HgjyecWxDgKFTsxlFwkOOOhCR67tL qbgyZEfbccsfxfpEceHNgo1tRfhdx9ZkLAJDHVwFZrV/CE8Fn1JuDwaPtVBqp6MXk6Za j0lQ5EJDPBiqlmCzPEGLF9t+fayIt59HtqJ7MtK9sFSbKtLNaQhemmmIxlGkvpGGHIQm bEDQf9iOGihlbjdq1dmfMhTP/hKqQxq66J60E0Un7QBvIwNx+prjZKQhljuGCy3jkQV3 Flf213U6xNk6NYSYN5XCY8oCAR08KF7xgLs/vwyVBacdncrZesE1OvrLCzjYA78Rcn5X bNTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=chPSMnrDRu741hV8mN6BhcsZOY4+3XYSLVWxRQVgXxY=; fh=YQhVWO2XL9584FKmBhrq7yC9i9qkutxA7K29vY+DUJE=; b=bIqdKyx8xCoX3rRYJV7RAzHbRyN624vHcBmGms/K5iFeq3+jhNLn68yh07a5g00Q00 xclv1WjZXnZ1tJ3LwZ4yHz3A764xspwF+lX9sUPYnVhgGLRFatZyr847mUXD4iZmeCEV WdXEQAV6kaCqVeRpZhU3vfu49HwZ4dF31KTEN6wqud+FpyySrM/9Y07CEBW6AxJ2jJNn pJnRJ+kKRGK9ETFjFh2iNk3N9KUZEajigcp0KwO8W/KMlz/AcywgfQlZtonwj7srGnaY q9MlK86zyg0cpuoB//M5iYuzEgPZ85HuAYHwC/VKeLN8FmwyjL6uiRRIvOKzjMHjIwln GlbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=UGvD8Rzf; spf=pass (google.com: domain of linux-kernel+bounces-23847-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23847-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d10-20020a056402400a00b00554ae146096si677926eda.279.2024.01.11.08.20.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 08:20:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23847-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; dkim=pass header.i=@google.com header.s=20230601 header.b=UGvD8Rzf; spf=pass (google.com: domain of linux-kernel+bounces-23847-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23847-ouuuleilei=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 5F6571F25531 for ; Thu, 11 Jan 2024 16:20:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AAFCC55791; Thu, 11 Jan 2024 16:17:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UGvD8Rzf" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 059B253E16 for ; Thu, 11 Jan 2024 16:17:31 +0000 (UTC) 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-yw1-f202.google.com with SMTP id 00721157ae682-5f6a6febfe6so86224037b3.0 for ; Thu, 11 Jan 2024 08:17:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704989851; x=1705594651; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=chPSMnrDRu741hV8mN6BhcsZOY4+3XYSLVWxRQVgXxY=; b=UGvD8RzfpYUIhBQ91b4rrlCN8cdhWQTaenUUoyYPbR2cXdS51zCxvrzeeolJjxgndl jM86vQfrZgnp2duq8HAl58jWzrDs5xPmRuo8txvbB/f2TUHtlcnee22+y6Fw5u3F7Q9X aw7xkOyJt09WNC6VhStqGhoK8uGzw/bID3m3aOR0sLaZqa97gLU1mjGsJbJ5Gh0XYLqI rV2vSkDkZeJY60Ea7mBANwH1WTDOL4nlG5kVd/GtCyWfkkJcGJxBeHe3F8mGwNeu+tHe bQW6kOaQMEBAsREc+OTEiUzGUJ90APx8jWMRW2iAHcK3M0hvXbfjRBc12+o9C1fxhPBU SKcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704989851; x=1705594651; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=chPSMnrDRu741hV8mN6BhcsZOY4+3XYSLVWxRQVgXxY=; b=kKxMs/QDt1qLNm68VUluNsXI55eoZw8cwcIoVTQU85Gy4SHx2jS1lj1Pyf/BxiBt5d l/xNs7R6Wg0tQUGwe4w8SkVA8bDGjrCwUpYKdxOIIXOWTVeqkCqIj4SGjhuVGAlBlxK7 agwveMu7XlVIEpNAudNuGQ4HeT/tf6bW1DWVqNoXPQ2Y3ZofJjh3uiOB8SgL+yFjyCh0 bX9Kdv6bBtCVXjzYvZLhgwK/b3VbWq3eieWVikwf3MNSY3lJUo++fst1LB/Z4jgLSnSv ciIm2hmHziaH4SLwT+b/hmI7VlpZlLGemXgv3VkydxH+2Xol0TuxN9th30LZnK76QYZ0 xYTA== X-Gm-Message-State: AOJu0YzB5Z8L0qZwRvD2DiC6h+fQhaqmtCT0BUjE3c2XkqnqTznK4w1B /bmDXSPoM7qLG8vLoaD+TkXqo4RV2rUWnAIYkRWuG/E= X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:6902:b03:b0:dbf:142d:9dc4 with SMTP id ch3-20020a0569020b0300b00dbf142d9dc4mr59718ybb.3.1704989851099; Thu, 11 Jan 2024 08:17:31 -0800 (PST) Date: Thu, 11 Jan 2024 16:17:12 +0000 In-Reply-To: <20240111161712.1480333-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240111161712.1480333-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240111161712.1480333-6-vdonnefort@google.com> Subject: [PATCH v11 5/5] ring-buffer/selftest: Add ring-buffer mapping test 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 , Shuah Khan , Shuah Khan , linux-kselftest@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787811596123058705 X-GMAIL-MSGID: 1787811596123058705 This test maps a ring-buffer and validate the meta-page after reset and after emitting few events. Cc: Shuah Khan Cc: Shuah Khan Cc: linux-kselftest@vger.kernel.org Signed-off-by: Vincent Donnefort Reviewed-by: Masami Hiramatsu (Google) Tested-by: Masami Hiramatsu (Google) diff --git a/tools/testing/selftests/ring-buffer/Makefile b/tools/testing/selftests/ring-buffer/Makefile new file mode 100644 index 000000000000..627c5fa6d1ab --- /dev/null +++ b/tools/testing/selftests/ring-buffer/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -Wl,-no-as-needed -Wall +CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -D_GNU_SOURCE + +TEST_GEN_PROGS = map_test + +include ../lib.mk diff --git a/tools/testing/selftests/ring-buffer/config b/tools/testing/selftests/ring-buffer/config new file mode 100644 index 000000000000..ef8214661612 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/config @@ -0,0 +1 @@ +CONFIG_FTRACE=y diff --git a/tools/testing/selftests/ring-buffer/map_test.c b/tools/testing/selftests/ring-buffer/map_test.c new file mode 100644 index 000000000000..49107e8da5e9 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/map_test.c @@ -0,0 +1,188 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ring-buffer memory mapping tests + * + * Copyright (c) 2024 Vincent Donnefort + */ +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include "../user_events/user_events_selftests.h" /* share tracefs setup */ +#include "../kselftest_harness.h" + +#define TRACEFS_ROOT "/sys/kernel/tracing" + +static int __tracefs_write(const char *path, const char *value) +{ + FILE *file; + + file = fopen(path, "w"); + if (!file) + return -1; + + fputs(value, file); + fclose(file); + + return 0; +} + +static int __tracefs_write_int(const char *path, int value) +{ + char *str; + int ret; + + if (asprintf(&str, "%d", value) < 0) + return -1; + + ret = __tracefs_write(path, str); + + free(str); + + return ret; +} + +#define tracefs_write_int(path, value) \ + ASSERT_EQ(__tracefs_write_int((path), (value)), 0) + +static int tracefs_reset(void) +{ + if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0)) + return -1; + if (__tracefs_write_int(TRACEFS_ROOT"/trace", 0)) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/set_event", "")) + return -1; + + return 0; +} + +FIXTURE(map) { + struct trace_buffer_meta *meta; + void *data; + int cpu_fd; + bool umount; +}; + +FIXTURE_VARIANT(map) { + int subbuf_size; +}; + +FIXTURE_VARIANT_ADD(map, subbuf_size_4k) { + .subbuf_size = 4, +}; + +FIXTURE_VARIANT_ADD(map, subbuf_size_8k) { + .subbuf_size = 8, +}; + +FIXTURE_SETUP(map) +{ + int cpu = sched_getcpu(), page_size = getpagesize(); + unsigned long meta_len, data_len; + char *cpu_path, *message; + bool fail, umount; + cpu_set_t cpu_mask; + void *map; + + if (!tracefs_enabled(&message, &fail, &umount)) { + if (fail) { + TH_LOG("Tracefs setup failed: %s", message); + ASSERT_FALSE(fail); + } + SKIP(return, "Skipping: %s", message); + } + + self->umount = umount; + + ASSERT_GE(cpu, 0); + + ASSERT_EQ(tracefs_reset(), 0); + + tracefs_write_int(TRACEFS_ROOT"/buffer_subbuf_size_kb", variant->subbuf_size); + + ASSERT_GE(asprintf(&cpu_path, + TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw", + cpu), 0); + + self->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK); + ASSERT_GE(self->cpu_fd, 0); + free(cpu_path); + + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, self->cpu_fd, 0); + ASSERT_NE(map, MAP_FAILED); + self->meta = (struct trace_buffer_meta *)map; + + meta_len = self->meta->meta_page_size; + data_len = self->meta->subbuf_size * self->meta->nr_subbufs; + + map = mmap(NULL, data_len, PROT_READ, MAP_SHARED, self->cpu_fd, meta_len); + ASSERT_NE(map, MAP_FAILED); + self->data = map; + + /* + * Ensure generated events will be found on this very same ring-buffer. + */ + CPU_ZERO(&cpu_mask); + CPU_SET(cpu, &cpu_mask); + ASSERT_EQ(sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask), 0); +} + +FIXTURE_TEARDOWN(map) +{ + tracefs_reset(); + + if (self->umount) + tracefs_unmount(); + + munmap(self->data, self->meta->subbuf_size * self->meta->nr_subbufs); + munmap(self->meta, self->meta->meta_page_size); + close(self->cpu_fd); +} + +TEST_F(map, meta_page_check) +{ + int cnt = 0; + + ASSERT_EQ(self->meta->entries, 0); + ASSERT_EQ(self->meta->overrun, 0); + ASSERT_EQ(self->meta->read, 0); + ASSERT_EQ(self->meta->subbufs_touched, 0); + ASSERT_EQ(self->meta->subbufs_lost, 0); + ASSERT_EQ(self->meta->subbufs_read, 0); + + ASSERT_EQ(self->meta->reader.id, 0); + ASSERT_EQ(self->meta->reader.read, 0); + + ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + ASSERT_EQ(self->meta->reader.id, 0); + + tracefs_write_int(TRACEFS_ROOT"/tracing_on", 1); + for (int i = 0; i < 16; i++) + tracefs_write_int(TRACEFS_ROOT"/trace_marker", i); +again: + ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + + ASSERT_EQ(self->meta->entries, 16); + ASSERT_EQ(self->meta->overrun, 0); + ASSERT_EQ(self->meta->read, 16); + /* subbufs_touched doesn't take into account the commit page */ + ASSERT_EQ(self->meta->subbufs_touched, 0); + ASSERT_EQ(self->meta->subbufs_lost, 0); + ASSERT_EQ(self->meta->subbufs_read, 1); + + ASSERT_EQ(self->meta->reader.id, 1); + + if (!(cnt++)) + goto again; +} + +TEST_HARNESS_MAIN