From patchwork Fri Feb 9 16:34:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198973 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp974956dyd; Fri, 9 Feb 2024 08:35:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXYgjRIVkaGzc5ErY2pjbTPQ006QuRtQ1+9N6BTr/HwXdSGDCLvNxKloWpoiKZmolT88V9+6qke/mxpDlxgEM3bvm5+Gg== X-Google-Smtp-Source: AGHT+IHtVDygHd27WYjonE++bAjHuJws7K8ITtps+mIJpp/JdBrLq8a6gqoJ4WrAN0rvBaNsETEB X-Received: by 2002:a05:6402:1812:b0:560:10c:3cc with SMTP id g18-20020a056402181200b00560010c03ccmr1723038edy.21.1707496537694; Fri, 09 Feb 2024 08:35:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707496537; cv=pass; d=google.com; s=arc-20160816; b=KESHTZKvbHIQW380Gk7AXccJEhvjbSnUda1HqbyZ2agP9EoWN4PMtb8VwvGyoIRT6V COzgBCtNLUDID82xTwU/O4iRoo+AZ1rUm1IJfN6ajKPIipvSwmY6+tcR0PLByM4VoCiA 2DRfs9T72Jurrgbekiq4VzNSLQQbn23U4fvCnAxGlDsqLFIKaTFASphfenZQoK3qsFJ/ 452lJF3hox77ClKmDdVHq11G1jI6W4rylHARfIXT3/tjhS3x0OiFC1s8zdX94SuzpYxb UC59nk9tpjwAkH1GB9eNQdP27h+k0JDxeCZ+6X+QxHCwiMKrGm5mpU9JeJyLXnXCiveO PRVg== ARC-Message-Signature: i=2; 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=SqiLbHub5ruL/UvpG5IMav/eAn4s+2aSSt9gMa9+oOE=; fh=dVhXJgYP2M+zeQSodGQZ8YLT5l+BPfdQsBD9GOM9leY=; b=XyUU/ob78LVVBlcNKifZxkJcLxyn/6GsMx1JFkL0TEqAz2n/ZqeElKecbr2zUDFyt0 uonm7TgHN29PB1hA2Sn5Bje3zVhdoFrcwOADZ9NqOwSKJR9FjJ6hKZscM1OPj/IoG470 KAjt/sJojnfz7LwEnSKiz+aLYP/fhjq9jwBXgyzEyWErk/mMc6b9SBncpgPsTKVmEmie 1a+zrKZgt0sno6BxLgkAJX2/InG7MBhIn6X3pZsx+Iedc9LIMwg6kMu+0jgOh7Z9dGna DmYo/jxLrFq05xf+6AJeWs2on7uNT75RwUIVTZooGNZXBeclF2HE6Kn/LpGG77TsHaU9 1XHQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=phyjwkxs; 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-59610-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59610-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUoyLclb/W33a7kohjy1YwlMi5OiVHPBixVopjNsBcR09BF/LJBFCvo3qLJdUMN7/ZFCGtQR/gBl0jp7rY0b76onICdjQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z13-20020a05640240cd00b0056061ad85a6si1007679edb.657.2024.02.09.08.35.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:35:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59610-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=phyjwkxs; 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-59610-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59610-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 44C111F25E9C for ; Fri, 9 Feb 2024 16:35:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 825BF7C0AB; Fri, 9 Feb 2024 16:35:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="phyjwkxs" 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 404F279DB1 for ; Fri, 9 Feb 2024 16:34:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496500; cv=none; b=VSah0SCgRtWOeTcctDAHCsvWKQaYlI2t77P6JAVmbQazMtpFZLrq8VcszPhs0hBs4MML+ztX/KI1jFHjg1BB8XRmsYKpu42TJs2DXeMsLpqi9+Xkkb4p9ePyVE6zdwMIMODZKX9ABU3cJIhg9wSJQ7Gdh+RU1mhrrOtJ+jTYNTc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496500; c=relaxed/simple; bh=tpt51FeO44HeEXQqmygp1fYLZoXSRZvsHL381MrJybw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DniC/RN8lo3GNVdrxEMVMQpO6zO5hzq89hdLOSffLYeR9WJrCy3ifDjhZBcWBLFxRVZJEXH8nGzun/akGZNJnRQvou5hL1YNkRh4s3WXRFmRX53ElGXDjxQ/bL2oYos5ovLJYAlJqllNc7GBu1sqQbkmznIud+gRTVSJpz8aM7I= 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=phyjwkxs; arc=none smtp.client-ip=209.85.128.201 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-604a4923adaso21423927b3.0 for ; Fri, 09 Feb 2024 08:34:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707496498; x=1708101298; 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=SqiLbHub5ruL/UvpG5IMav/eAn4s+2aSSt9gMa9+oOE=; b=phyjwkxs2hfxij1srxXmONjYuSe3PReyQ9CMi2LTWTwvM6LOwGS8xkhOVjrzRwlOLj 8KtLdsEZziMaYLZLJOnskI2WZfRc8Ykh86PieGQBCGFnwDRMlH96lvbroXdwaC/s/cvZ sgN5bUfR0fo+Fb9JEynvKkdBCzwgeCVO/l5Z5tSZImqdaAIcGeOMdUDVFcL4KET75M7f 2Hn/qrsiDNEBZm69o9wQXRjamdGJCvihcMoNvext8Q+PFFfLrNNh4MkVtHhYmeiH5khN pQqBHWoGSrrm1JZw3I+AG2vxNnMYgx1TMVIm2zGPFHhPf4EwQXCUqkzb52BkA6s8DEi+ DncA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707496498; x=1708101298; 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=SqiLbHub5ruL/UvpG5IMav/eAn4s+2aSSt9gMa9+oOE=; b=AYRT/+g2H7xDap/2HzmiIYMTFdn4ATbDYrToo/jdRpbf7IXU4VDaz0ScYFYGmOCM7o BYEHXrqIQj8kqi2nNPqEd7PkUloiUdwZDcYRjMWRXd9iicYHGeQlqBmzYyY66CGfxCDu MrSEfSIVZifNWuFO0+o7l35D7Yrc/n8ARFE5JxZI8suQXJOpEzotsQIEIuy3Z8bBQDhp b4mKgP+EMo/rum9ChtJYVnZQEFx9SRDH6BL+MCK7eLBq23Cut+FxGWQ2ediZyWoqSZAX WwATFsVTzy6a7cKhTZSj7AkqzbpFqCob26T54SAVPexpkFFbcd4eESvopFyCXVR1OnXD fO0g== X-Forwarded-Encrypted: i=1; AJvYcCXQlkW88MYy2LUJlIYaxcW7rNsuNdqxNLexOi8wLVeyJBvYucSeq1FTixYKqlLfFc4HyP1bHSkFNUmaKjE+fHH+GPNxEXfORr6Pvlwf X-Gm-Message-State: AOJu0Yy6fasTohLUhCTJN2x9jDDB3sUjqK3V8f0b48C7IzQx9qy6BgD1 hGD3PTymOs/x7wNwpz3ieqgUbcwozkbg0KamuvcNlN2tBzvRbWPgg+g9yzTsybrcvRd7Szm6dx4 7MX4XLZAX664scQt7vQ== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a81:8491:0:b0:604:9b73:ee86 with SMTP id u139-20020a818491000000b006049b73ee86mr275232ywf.4.1707496498236; Fri, 09 Feb 2024 08:34:58 -0800 (PST) Date: Fri, 9 Feb 2024 16:34:43 +0000 In-Reply-To: <20240209163448.944970-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: <20240209163448.944970-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209163448.944970-2-vdonnefort@google.com> Subject: [PATCH v16 1/6] 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: 1790439889631274751 X-GMAIL-MSGID: 1790439889631274751 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 fd4bfe3ecf01..ca796675c0a1 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1472,7 +1472,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; @@ -1557,7 +1558,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 Fri Feb 9 16:34:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198987 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp981748dyd; Fri, 9 Feb 2024 08:47:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUl7+WAnnfL3kZjMY0W1/NRLWB8sjiZ+bhfPXS1+qc3gD2zIvlicHCXuYUaTcFjNN7UB/x5t6QMYOmVos1mh86pqioliw== X-Google-Smtp-Source: AGHT+IFPsp+LqpvhEEGzmqdhJsNtJAGyWVrQFmFUzn1jghjdl//Vg7ye7gI+INnGfeQoMTcXOtYG X-Received: by 2002:a17:903:2291:b0:1d8:d73b:794f with SMTP id b17-20020a170903229100b001d8d73b794fmr2501838plh.56.1707497272055; Fri, 09 Feb 2024 08:47:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707497272; cv=pass; d=google.com; s=arc-20160816; b=mU+WRtOV/i7hwZeSo+o8lx6v9Uxn2ruHwNc0qlAiSqWq7vmk9K+9tzhG/mwKGMZ2q6 UQCuJqEzePU4kRSCOZne8VSIjS0+bWLhXU8FbyGTCMJ/hUB0n5sigMcLd0G3tGlL3VSm I6nZy+S2U2zOeBwFIiGZ4sa5CU3nWpmkTNKGnT5Yg50kXlWFqNX3egzjU1LH7VH4u+p6 ATE9g6/FY+HacL+9h5PAhVCAbm7/jaCxTwb8VTx9JBvk+CGZmlgk0mgSh3sGX6J5d7/j U+Skj2/HwdWNXsV9waaxw1nndzhu4QYIzMOQT9LWgQYcNGx51sKBY3mitlvE19pnxogX rPUw== ARC-Message-Signature: i=2; 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=HTnxtc9p0IItOgKmfqUIkRc38PfFpHz4MZ4pysBfFx0=; fh=ZDK6N68sYMT4naxEIYTMJiKxwmMzZrJFFtvR/pcIw4I=; b=qDGUPww2z2+tf9kNfP4lq1ork6CLv38xiHPIclWpxdDrCZ95zDBR4w/9/R+k1wRrUH SIXrpBKAoAGYmD/ADxhKDyS3Xed+ga0USSsxzujmRP3QW9X9b+XtZj89aDiETYy6s70S Nqzc78yiTZzCp/ZyPB1hpaFF078W+MDzvXTtkZaBkdP3MRQIUZYgUt9LlueYUoxovRKF mm6mSXicNB3FFARZKpBvPZFwcQtCd78Q8M5FmzsKz1+2QZieWbyJpQCEgpxvOgOGskfg w9XvoEGMWiXF27NDv1Nf3NwFUQyVuvItBLojtiCKNJ6Y8Ssbm/paCkTzIMFeML/xIG3J zF3w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=m6tKnzDq; 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-59612-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59612-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVGfca3jBmcnx3/5NzEYxi8mse0U13OC+aBBwXT+OZj5eggeoud09IQ3jkh6zGXMPDCvLtn0GjXPF+YGMMqzYluxJEl8g== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id b13-20020a170902650d00b001d8a5c7a37fsi1855499plk.240.2024.02.09.08.47.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:47:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59612-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=m6tKnzDq; 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-59612-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59612-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 B6C87B275D4 for ; Fri, 9 Feb 2024 16:36:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B17407F489; Fri, 9 Feb 2024 16:35:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="m6tKnzDq" 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 29FBA7BB01 for ; Fri, 9 Feb 2024 16:35:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496504; cv=none; b=WjUzgSUYq1t9qYevicr53OJLMVDvnHIYWrLu5n+X5RxqAXw8mJk4aZc75kf40oKXh4MpLUC5mikdMb96zvhKKDOggsH1hsRxgDCZFbOEZKwAW5TvwY749wIBYkkbbr5SR/B4alpUYuTIKai9744s7kIjo+e04qlWZM1lmO8OjeA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496504; c=relaxed/simple; bh=wKgr0+a74ZwaQJ8pVB9FGQ/maIiV3snxA+rLekKNdjc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=t9dz9twnrPdnHmkGxRbu3SG0XGYO122+I+pQ6oyRDEPeIczVmbrYARPVresl9K0odKuXYZdfhTGbSpfL4rKcdW6oCpaNDRStEKglMj2P/BHfW5ZAV3tIwW4bn+/v27GjQf3yWl2H8DxGb9tyZyrLfpchQm+4OcqsebNlPzDj2mU= 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=m6tKnzDq; arc=none smtp.client-ip=209.85.128.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-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4106ab13a7bso4853185e9.2 for ; Fri, 09 Feb 2024 08:35:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707496500; x=1708101300; 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=HTnxtc9p0IItOgKmfqUIkRc38PfFpHz4MZ4pysBfFx0=; b=m6tKnzDqn83Z1tw3vfPdisQYamzvFzHdm8WuA/mx4CWMoV41XlHBhD8Z9+z25diAjf XFjLtm6Y8pyw0nM2227nuLhAJArp8h05T64LoAfZPOYPvywbG851NiXmWDERNgfgwW0x Uu01DV41xvOjXsP+k1rBmo+1HDKvwqsXmNLtQqOgfWhQd+Ra9u4T4y3SZPSLw/oAB91c nooZimoGqoBvlB7wvqW4VLYPJkjq8ZbqC11ZT7f+IEMce+fJtKqSE5m9B5f/3pYEtx6v jFKcL00wKXmdSpsw+ciRBMG5U+i1F6gXBseAFE04/2yIccTeJcOWE2y7PBub8BXYSbNa 9bBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707496500; x=1708101300; 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=HTnxtc9p0IItOgKmfqUIkRc38PfFpHz4MZ4pysBfFx0=; b=l97plx3RvkckowW/vFCNNLGRb9GngMkkhjhkmVz8p411Dmp56TJck7d2qtujjTCHrF WoRLjZ127MXVXqACMKVxDaJwfX0x7A272DSMhTuRpApH7C22pHra/zckIyVJZBI2z1n0 512+GfNZBbZlupUBuLRGDnb2gZsdSxeJ712cGQxNFKbkOWdwQc+uVLnVHhFPxil1r/ux jLI16wCAT5MRTwunrLemcD/JZEIyGlO9PuBuZQE35WzxzVSB2Vn3TV2BbOUAKFsAdXVF oYsVobjMb3ycMSdot3kHiv075sSlQY5m1h5wI3brY2S+QU73EGHqF52gf+gAndhp8KmJ nI0Q== X-Forwarded-Encrypted: i=1; AJvYcCVaq4FyNLRtjsTr95wXs2bc/g4mFAB0em5ZI9rLyHsYLcefFjGVYFqNTwdxtOs6j+QQpUEJgzPzw7hw/4T7Vdo7z5RIDAkGpK+jCWKd X-Gm-Message-State: AOJu0YxApzUz0OZhtVVPjv2Rcf9K8GwcHoruaBzlGr/S4LmGTsLjatnQ 9PUc1gGerobzUFpmFgFeYoG7HtdDNr2rTpS2KuiW2NuGzewgJzKoKuCYAfR1ZxJF9wSQMEnvL4k ZdU5Ig/JicsmhTCOE1A== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:1e29:b0:410:84b9:b9f7 with SMTP id ay41-20020a05600c1e2900b0041084b9b9f7mr5625wmb.3.1707496500600; Fri, 09 Feb 2024 08:35:00 -0800 (PST) Date: Fri, 9 Feb 2024 16:34:44 +0000 In-Reply-To: <20240209163448.944970-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: <20240209163448.944970-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209163448.944970-3-vdonnefort@google.com> Subject: [PATCH v16 2/6] 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: 1790440659456670428 X-GMAIL-MSGID: 1790440659456670428 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..182e05a3004a --- /dev/null +++ b/include/uapi/linux/trace_mmap.h @@ -0,0 +1,46 @@ +/* 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 + * @meta_page_size: Size of this meta-page. + * @meta_struct_len: Size of this structure. + * @subbuf_size: Size of each sub-buffer. + * @nr_subbufs: Number of subbfs in the ring-buffer. + * @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. + * @flags: Placeholder for now, no defined values. + * @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. + * @Reserved1: Reserved for future use. + * @Reserved2: Reserved for future use. + */ +struct trace_buffer_meta { + __u32 meta_page_size; + __u32 meta_struct_len; + + __u32 subbuf_size; + __u32 nr_subbufs; + + struct { + __u64 lost_events; + __u32 id; + __u32 read; + } reader; + + __u64 flags; + + __u64 entries; + __u64 overrun; + __u64 read; + + __u64 Reserved1; + __u64 Reserved2; +}; + +#endif /* _TRACE_MMAP_H_ */ diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index ca796675c0a1..4543fc51567d 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -338,6 +339,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 +486,12 @@ struct ring_buffer_per_cpu { u64 read_stamp; /* pages removed since last reset */ unsigned long pages_removed; + + unsigned 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 */ @@ -1548,6 +1556,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)); @@ -1738,8 +1747,6 @@ bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer) return buffer->time_stamp_abs; } -static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); - static inline unsigned long rb_page_entries(struct buffer_page *bpage) { return local_read(&bpage->entries) & RB_WRITE_MASK; @@ -5160,6 +5167,22 @@ 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; + + meta->reader.read = cpu_buffer->reader_page->read; + meta->reader.id = cpu_buffer->reader_page->id; + meta->reader.lost_events = cpu_buffer->lost_events; + + meta->entries = local_read(&cpu_buffer->entries); + meta->overrun = local_read(&cpu_buffer->overrun); + meta->read = cpu_buffer->read; + + /* Some archs do not have data cache coherency between kernel and user-space */ + flush_dcache_folio(virt_to_folio(cpu_buffer->meta_page)); +} + static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) { @@ -5204,6 +5227,9 @@ rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) cpu_buffer->lost_events = 0; cpu_buffer->last_overrun = 0; + if (READ_ONCE(cpu_buffer->mapped)) + rb_update_meta_page(cpu_buffer); + rb_head_page_activate(cpu_buffer); cpu_buffer->pages_removed = 0; } @@ -5418,6 +5444,12 @@ int ring_buffer_swap_cpu(struct trace_buffer *buffer_a, cpu_buffer_a = buffer_a->buffers[cpu]; cpu_buffer_b = buffer_b->buffers[cpu]; + /* It's up to the callers to not try to swap mapped buffers */ + if (WARN_ON_ONCE(cpu_buffer_a->mapped || 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 +5714,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 || + READ_ONCE(cpu_buffer->mapped)) { struct buffer_data_page *rpage = cpu_buffer->reader_page->page; unsigned int rpos = read; unsigned int pos = 0; @@ -5901,6 +5934,11 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) cpu_buffer = buffer->buffers[cpu]; + if (READ_ONCE(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 +6040,304 @@ 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 == UINT_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/sub-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); + 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; + unsigned long flags; + 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 out; + } else if (cpu_buffer->mapped > 1) { + WRITE_ONCE(cpu_buffer->mapped, cpu_buffer->mapped - 1); + goto out; + } + + mutex_lock(&buffer->mutex); + raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); + + cpu_buffer->mapped = 0; + + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); + + rb_free_subbuf_ids(cpu_buffer); + rb_free_meta_page(cpu_buffer); + atomic_dec(&cpu_buffer->resize_disabled); + + mutex_unlock(&buffer->mutex); +out: + 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); + + /* Some archs do not have data cache coherency between kernel and user-space */ + flush_dcache_folio(virt_to_folio(cpu_buffer->reader_page->page)); + + 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 Fri Feb 9 16:34:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198988 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp981995dyd; Fri, 9 Feb 2024 08:48:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IFKBZKEfkU620Wtu0j4fPa6PqancvDCVQhHyf7DQ+aV5vt+pN1vlD25QLoUcZ7RCA5tqpy8 X-Received: by 2002:a17:903:244d:b0:1d9:4643:65b6 with SMTP id l13-20020a170903244d00b001d9464365b6mr2750669pls.1.1707497301122; Fri, 09 Feb 2024 08:48:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707497301; cv=pass; d=google.com; s=arc-20160816; b=P0RrHZ/aMPEI64t1XjUaNYljlLUK7oyLDKX50JdaMlPlXGbOkcYejvHh/dkWNOnbdZ +QJb9ygLxKKRT2puguiLDf8lQJ/uGI4MwitCPke+JXg66NfuOva6PcdPEjlCn/vDjJ0Y zeEHyUxzKnuPhDSw/YxQjyRB0/XpqKT3pY7AxHCwj6wIMukEgqymqNVYtkdCZk7VQoMq d46w4L9FIOnao13wZV4Mxk0OpmZyg+qzNi7i1m+a6olsG9akSG5yXZOtcjrutjtfGYE3 gXpolOhPGhIAyGMGjkWt0ZVSOwsiVhGKXf6vLnMrYHSdQtcuMBQKNcYfcfXG0A+jgAJB 5yxg== ARC-Message-Signature: i=2; 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=iC3ltUXDHiI03h8nicKfbM5lCCaHJc2Ye8xyZlGM6ac=; fh=AeESuQrrrqqdhupFqm2UWYeNBiJyzjW4VJqJx8ppxsI=; b=SanarFzcDJus7oE2BmjNyBedsccGuW0ecntdfYZrM+bcFhxXQ8sBi8gQDRfkB9Lbom Xmu304De/EQjYlYSOMUDcyym5KDQsW2Cv8YanxagGd1H7WM4ZqnBhCPiv0d+fNwINTui S4GqEV/I7J3H3xZXb0O9Y0TSSK+fhDdgTMPz10UsOM1O4/6+2/FMeAYPhOa+NX8gJLAc O5nSp9FB7Cp5e2A22pxJv200/POGRHZ2SREEqAvc3hUmgOFXGAiETcmllg6dKtNDbebY 2MEnEw3jSeNOt4vdBpghHtvlyLaPDdKBrdTlLjTUFvxu6gzMITN9QG6rqb3iNbs1oEBd Gy9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Y+tehD/D"; 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-59613-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59613-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCX41c/Kar21V04Rs6X6qk9iyej4fUQL3NyFxdGrE3PrfBgzKk2TsXJ4tXO+KtmTQgJnpjqvgbVrgJ7WA7YpTcgozFMN5g== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id c16-20020a6566d0000000b005cdf8b03384si1957753pgw.805.2024.02.09.08.48.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:48:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59613-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="Y+tehD/D"; 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-59613-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59613-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 E2D43B27D06 for ; Fri, 9 Feb 2024 16:36:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AAFB180BFD; Fri, 9 Feb 2024 16:35:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Y+tehD/D" 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 190727D40A for ; Fri, 9 Feb 2024 16:35:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496507; cv=none; b=XdpkhSR3VBLCPkzN2pne2ihME3WvXgdz3L/xCDIWiHhXTK2kWO/FfE9oHatIIS6CPQ6Oe7z14th/E5lQYD2J9Yeqs8gQEyGKK6zkyxcAAiBnSiMwZHKLhL5L/txhEx0poNAMYjqb7rkROdWqhTbYyWcHzGkHl2QSvxjpq5BO82k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496507; c=relaxed/simple; bh=MApb/KU3APzxlFgZL+OJ5De3oK1aRzd7YLFeINSgGUA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IIs8yODD0NUfyWX7wRsHn/rWoL/BHEoqMp3HQJ1CNLTCzFUOCp+srwVJaYbVP9NzJGWCsr9h0kzZDaoEr9xVrnpfd47lEQafIsIXEydvbckeMcN6SLJ6l0vuuO1CkHz529TbaIopkwTBIpPTd1fuxZrn73jmb97qCsw9HXLvwMI= 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=Y+tehD/D; arc=none smtp.client-ip=209.85.128.202 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-60492d6bfc0so19849077b3.2 for ; Fri, 09 Feb 2024 08:35:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707496503; x=1708101303; 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=iC3ltUXDHiI03h8nicKfbM5lCCaHJc2Ye8xyZlGM6ac=; b=Y+tehD/DtsGFKZ0/8qbql8pkssj1vFdJrZbKPcG+DCdgC7jjZwVycCk3LgiNgzawJL Ho/6H2tNraHIwbQj7wP1MMpRx7VvRR26gsXYgaQNkC1PBka2FhGXgym8KXnZaiPH0mis QihQyO2JsNgUZuVy4qeuD86xhPFqqeYlxp2aw+XcwLiGAsGqBmFqLzUlGoLsasYdQG5Y NsYPGCpe65bZV7h8vh7Z1KKM5F4otubEtPvqTAevn/7Hgbcsq+rxIjvJejbGDYbbpqVd ScdR7fZZ7oVak9IyYYI1BaCbsihTvuqJ3hu7VwmNh7aXGz3TH9TihRoIAwmWd0S4s5Xx gEuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707496503; x=1708101303; 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=iC3ltUXDHiI03h8nicKfbM5lCCaHJc2Ye8xyZlGM6ac=; b=iXt6/G5BlqgvUxmiXcCqSkR3mwCyXMU220cTmQ4tY25jj21g6O8KUNgPxCX/GTjX7I kpjLlugI3mRNmVOzPTHvP1ZLHdGms65wW3ZdfeXgXyePYW6e5GB+W3o9JqGzKtQkF4iL RFBd5GasfkHHDkT1IoVAi9eCDaqi2sgnLU/BqHg/36snRyNx7x/dLDIea4AC7bCCyoRw s7MF5eHVVZwpx025nOKqzJKJJPoK2utaeXwRCsrDRcZMevZUnTBEYqCxzRfJYTXtBBz5 P/6U70yFES6j+sf6H1q/bSkgZ+91br0JtcKBdJOnLrOU6Ql2BMxncAreQcPJ+EEqJ2uz jKyQ== X-Gm-Message-State: AOJu0YzdDGcBTHb0ns+2g30rgMLusrPCGuBDxHnSq8BtctJq8V2gl/dE prppUrSPp/A5rYG9gpSPMaTbHqTOUJuHzMM1bxehV73swA5v8pdTDJc7uLUk1GnTXMroSRrA0xT roiWitsmtbK+Jseae4A== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:6902:1004:b0:dc7:3189:4e75 with SMTP id w4-20020a056902100400b00dc731894e75mr47336ybt.3.1707496503105; Fri, 09 Feb 2024 08:35:03 -0800 (PST) Date: Fri, 9 Feb 2024 16:34:45 +0000 In-Reply-To: <20240209163448.944970-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: <20240209163448.944970-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209163448.944970-4-vdonnefort@google.com> Subject: [PATCH v16 3/6] tracing: Add snapshot refcount 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: 1790440690381680079 X-GMAIL-MSGID: 1790440690381680079 When a ring-buffer is memory mapped by user-space, no trace or ring-buffer swap is possible. This means the snapshot feature is mutually exclusive with the memory mapping. Having a refcount on snapshot users will help to know if a mapping is possible or not. Instead of relying on the global trace_types_lock, a new spinlock is introduced to serialize accesses to trace_array->snapshot. This intends to allow access to that variable in a context where the mmap lock is already held. Signed-off-by: Vincent Donnefort diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 2a7c6fd934e9..4ebf4d0bd14c 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1300,6 +1300,50 @@ static void free_snapshot(struct trace_array *tr) tr->allocated_snapshot = false; } +static int tracing_arm_snapshot_locked(struct trace_array *tr) +{ + int ret; + + lockdep_assert_held(&trace_types_lock); + + spin_lock(&tr->snapshot_trigger_lock); + if (tr->snapshot == UINT_MAX) { + spin_unlock(&tr->snapshot_trigger_lock); + return -EBUSY; + } + + tr->snapshot++; + spin_unlock(&tr->snapshot_trigger_lock); + + ret = tracing_alloc_snapshot_instance(tr); + if (ret) { + spin_lock(&tr->snapshot_trigger_lock); + tr->snapshot--; + spin_unlock(&tr->snapshot_trigger_lock); + } + + return ret; +} + +int tracing_arm_snapshot(struct trace_array *tr) +{ + int ret; + + mutex_lock(&trace_types_lock); + ret = tracing_arm_snapshot_locked(tr); + mutex_unlock(&trace_types_lock); + + return ret; +} + +void tracing_disarm_snapshot(struct trace_array *tr) +{ + spin_lock(&tr->snapshot_trigger_lock); + if (!WARN_ON(!tr->snapshot)) + tr->snapshot--; + spin_unlock(&tr->snapshot_trigger_lock); +} + /** * tracing_alloc_snapshot - allocate snapshot buffer. * @@ -1373,10 +1417,6 @@ int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, mutex_lock(&trace_types_lock); - ret = tracing_alloc_snapshot_instance(tr); - if (ret) - goto fail_unlock; - if (tr->current_trace->use_max_tr) { ret = -EBUSY; goto fail_unlock; @@ -1395,6 +1435,10 @@ int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, goto fail_unlock; } + ret = tracing_arm_snapshot_locked(tr); + if (ret) + goto fail_unlock; + local_irq_disable(); arch_spin_lock(&tr->max_lock); tr->cond_snapshot = cond_snapshot; @@ -1439,6 +1483,8 @@ int tracing_snapshot_cond_disable(struct trace_array *tr) arch_spin_unlock(&tr->max_lock); local_irq_enable(); + tracing_disarm_snapshot(tr); + return ret; } EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); @@ -1481,6 +1527,7 @@ int tracing_snapshot_cond_disable(struct trace_array *tr) } EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); #define free_snapshot(tr) do { } while (0) +#define tracing_arm_snapshot_locked(tr) ({ -EBUSY; }) #endif /* CONFIG_TRACER_SNAPSHOT */ void tracer_tracing_off(struct trace_array *tr) @@ -6593,11 +6640,12 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf) */ synchronize_rcu(); free_snapshot(tr); + tracing_disarm_snapshot(tr); } - if (t->use_max_tr && !tr->allocated_snapshot) { - ret = tracing_alloc_snapshot_instance(tr); - if (ret < 0) + if (t->use_max_tr) { + ret = tracing_arm_snapshot_locked(tr); + if (ret) goto out; } #else @@ -6606,8 +6654,13 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf) if (t->init) { ret = tracer_init(t, tr); - if (ret) + if (ret) { +#ifdef CONFIG_TRACER_MAX_TRACE + if (t->use_max_tr) + tracing_disarm_snapshot(tr); +#endif goto out; + } } tr->current_trace = t; @@ -7709,10 +7762,11 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, if (tr->allocated_snapshot) ret = resize_buffer_duplicate_size(&tr->max_buffer, &tr->array_buffer, iter->cpu_file); - else - ret = tracing_alloc_snapshot_instance(tr); - if (ret < 0) + + ret = tracing_arm_snapshot_locked(tr); + if (ret) break; + /* Now, we're going to swap */ if (iter->cpu_file == RING_BUFFER_ALL_CPUS) { local_irq_disable(); @@ -7722,6 +7776,7 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer, (void *)tr, 1); } + tracing_disarm_snapshot(tr); break; default: if (tr->allocated_snapshot) { @@ -8846,8 +8901,13 @@ ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash, ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops; - if (glob[0] == '!') - return unregister_ftrace_function_probe_func(glob+1, tr, ops); + if (glob[0] == '!') { + ret = unregister_ftrace_function_probe_func(glob+1, tr, ops); + if (!ret) + tracing_disarm_snapshot(tr); + + return ret; + } if (!param) goto out_reg; @@ -8866,12 +8926,13 @@ ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash, return ret; out_reg: - ret = tracing_alloc_snapshot_instance(tr); + ret = tracing_arm_snapshot(tr); if (ret < 0) goto out; ret = register_ftrace_function_probe(glob, tr, ops, count); - + if (ret < 0) + tracing_disarm_snapshot(tr); out: return ret < 0 ? ret : 0; } @@ -9678,7 +9739,9 @@ trace_array_create_systems(const char *name, const char *systems) raw_spin_lock_init(&tr->start_lock); tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; - +#ifdef CONFIG_TRCER_MAX_TRACE + spinlock_init(&tr->snapshot_trigger_lock); +#endif tr->current_trace = &nop_trace; INIT_LIST_HEAD(&tr->systems); @@ -10648,7 +10711,9 @@ __init static int tracer_alloc_buffers(void) global_trace.current_trace = &nop_trace; global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; - +#ifdef CONFIG_TRACER_MAX_TRACE + spin_lock_init(&global_trace.snapshot_trigger_lock); +#endif ftrace_init_global_array_ops(&global_trace); init_trace_flags_index(&global_trace); diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 00f873910c5d..bd312e9afe25 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -334,8 +334,8 @@ struct trace_array { */ struct array_buffer max_buffer; bool allocated_snapshot; -#endif -#ifdef CONFIG_TRACER_MAX_TRACE + spinlock_t snapshot_trigger_lock; + unsigned int snapshot; unsigned long max_latency; #ifdef CONFIG_FSNOTIFY struct dentry *d_max_latency; @@ -1973,12 +1973,16 @@ static inline void trace_event_eval_update(struct trace_eval_map **map, int len) #ifdef CONFIG_TRACER_SNAPSHOT void tracing_snapshot_instance(struct trace_array *tr); int tracing_alloc_snapshot_instance(struct trace_array *tr); +int tracing_arm_snapshot(struct trace_array *tr); +void tracing_disarm_snapshot(struct trace_array *tr); #else static inline void tracing_snapshot_instance(struct trace_array *tr) { } static inline int tracing_alloc_snapshot_instance(struct trace_array *tr) { return 0; } +static inline int tracing_arm_snapshot(struct trace_array *tr) { return 0; } +static inline void tracing_disarm_snapshot(struct trace_array *tr) { } #endif #ifdef CONFIG_PREEMPT_TRACER diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_events_trigger.c index b33c3861fbbb..62e4f58b8671 100644 --- a/kernel/trace/trace_events_trigger.c +++ b/kernel/trace/trace_events_trigger.c @@ -597,20 +597,12 @@ static int register_trigger(char *glob, return ret; } -/** - * unregister_trigger - Generic event_command @unreg implementation - * @glob: The raw string used to register the trigger - * @test: Trigger-specific data used to find the trigger to remove - * @file: The trace_event_file associated with the event - * - * Common implementation for event trigger unregistration. - * - * Usually used directly as the @unreg method in event command - * implementations. +/* + * True if the trigger was found and unregistered, else false. */ -static void unregister_trigger(char *glob, - struct event_trigger_data *test, - struct trace_event_file *file) +static bool try_unregister_trigger(char *glob, + struct event_trigger_data *test, + struct trace_event_file *file) { struct event_trigger_data *data = NULL, *iter; @@ -626,8 +618,32 @@ static void unregister_trigger(char *glob, } } - if (data && data->ops->free) - data->ops->free(data); + if (data) { + if (data->ops->free) + data->ops->free(data); + + return true; + } + + return false; +} + +/** + * unregister_trigger - Generic event_command @unreg implementation + * @glob: The raw string used to register the trigger + * @test: Trigger-specific data used to find the trigger to remove + * @file: The trace_event_file associated with the event + * + * Common implementation for event trigger unregistration. + * + * Usually used directly as the @unreg method in event command + * implementations. + */ +static void unregister_trigger(char *glob, + struct event_trigger_data *test, + struct trace_event_file *file) +{ + try_unregister_trigger(glob, test, file); } /* @@ -1470,7 +1486,7 @@ register_snapshot_trigger(char *glob, struct event_trigger_data *data, struct trace_event_file *file) { - int ret = tracing_alloc_snapshot_instance(file->tr); + int ret = tracing_arm_snapshot(file->tr); if (ret < 0) return ret; @@ -1478,6 +1494,14 @@ register_snapshot_trigger(char *glob, return register_trigger(glob, data, file); } +static void unregister_snapshot_trigger(char *glob, + struct event_trigger_data *data, + struct trace_event_file *file) +{ + if (try_unregister_trigger(glob, data, file)) + tracing_disarm_snapshot(file->tr); +} + static int snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data) { @@ -1510,7 +1534,7 @@ static struct event_command trigger_snapshot_cmd = { .trigger_type = ETT_SNAPSHOT, .parse = event_trigger_parse, .reg = register_snapshot_trigger, - .unreg = unregister_trigger, + .unreg = unregister_snapshot_trigger, .get_trigger_ops = snapshot_get_trigger_ops, .set_filter = set_trigger_filter, }; From patchwork Fri Feb 9 16:34:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198975 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp975711dyd; Fri, 9 Feb 2024 08:36:53 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX/tzono383I9f3I9wdwDELLaBdmkJjNrrYVah3Zvs66TkyCNORkMBW+0MdKnYQSz6luhqsP1Beh5D3vsTovzxtzgYDew== X-Google-Smtp-Source: AGHT+IHefOXEy4VuSl9Duvt4MEY81INERSotACwTVQ+WdZb7+g5K+ObTqx3gZpY8kyW7JIEW2NKB X-Received: by 2002:aa7:cd67:0:b0:561:2fd1:99e5 with SMTP id ca7-20020aa7cd67000000b005612fd199e5mr1636684edb.27.1707496613087; Fri, 09 Feb 2024 08:36:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707496613; cv=pass; d=google.com; s=arc-20160816; b=fj56W9QAE52G92v55Ic/M5LACR3dVKGg0ZqheuxpP3h1LHUOJwSJbtbZwUCi9CGFsJ us6e/837EGjeZhZnaEzc5uKhnzPnTzUYKqHdEyy83sEMBdJpNAIdCWNfOTLGBHNxUsaa HX2GKrNc//Fxuri33nXRqREX2ujtO3sd15SuGTucKrm6O1kX1cWA9zz2CI572il7RCPw zXfCUiam20i6hvziF5aYue44Mec8/ehnCcoQsPj3X3ZNs5o0Pov22u1wzAQmwpBMDQjZ iDGPGh8aQtiZVpUuZTL/WH8pDpgltr30DHYkVERx4whfqRAa2ow6UbtdmNGQRa9VR4P3 T6Wg== ARC-Message-Signature: i=2; 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=8eG/PuLIoWB8LKFjP4cPEQOG7vgr5hu+n2RMYn2R0is=; fh=1RHc9XO61VFkbQau64YpFCN0R/LNtY0aW5UXCD6KarI=; b=wmoSzR3xAH5m7SeXKpxjhijYOkgnyZXOKpuVujh53vqra5xmN6GoftHo/O+guGxqYx fWOxxG4jSok4BjKoMJ5aKnkKzP+DkGUFn6Sc2pW4EjhbTYkrbt8N8OXHYwkebSQCPqKx AkT31zC/ZzDWzDBJj9ialYzNWvsV9m47LOVj07sVko7CJKvX5mHFdiG5aFqCysHA8uKR n8cSk5+6cA7EziN+dPi0qGafwOIiDjSdXupWPId4DITCBIzqX7wwqloXhQLX7ICMHOnD QSSc4+PQ7hVZa3UxmDjPL8YvIXSTVteInl5vCvJFoA2sMG2+cBO3GECOAJWDWcZesaJ+ zPeQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Cq2V1HLt; 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-59614-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59614-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCWMtXPP5Iz9C8iig+J1dEZA2MU1n77ZHHcC775a2DzfOcRsZHCsYqXcq3aFKvTh2jgN/hjn3ljC7AKdISsQXz0iDtiq6g== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id bo1-20020a0564020b2100b00561559fd691si160081edb.5.2024.02.09.08.36.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:36:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59614-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=Cq2V1HLt; 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-59614-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59614-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 6C52A1F267EB for ; Fri, 9 Feb 2024 16:36:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 026667D40A; Fri, 9 Feb 2024 16:35:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Cq2V1HLt" 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 4A5FB7EF03 for ; Fri, 9 Feb 2024 16:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496509; cv=none; b=EjnrzNMcK4vmxwaF7SbnK4BgdyO5CbqqBz518ouuwyp5xZPXbJp8pJTmHdLrajPyiQgvu00uZcAuRjj8K7eRgmJjrnI73mdzBJQEECFihcEEiVDBDPzp771MWoXwYAGcX2SnTU0LbjeV0ILJROWC6dznWzD6c52TY+yv1hFDFHo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496509; c=relaxed/simple; bh=POwIaSakA3P7VN3DoSdTZ1HOoCQpnAoqCCz15gHTKd8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=otf9cPBH6IRjCk46VDGBQYtu8F84pUN/ThuJs1QwARiRr98Bm2kleOQwU1RcRjQqwh1KIiiAxzhwtU+1HlU1hEhYkeeg/bWc0xDvxZ7NNA0r+uef7kjUJyDSecwX60VmqPvKtvhKgEBE4BsZuRjAAOHBe8H/X+tzVerMCKydpq4= 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=Cq2V1HLt; arc=none smtp.client-ip=209.85.128.74 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-40fd2fe490bso9987835e9.0 for ; Fri, 09 Feb 2024 08:35:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707496505; x=1708101305; 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=8eG/PuLIoWB8LKFjP4cPEQOG7vgr5hu+n2RMYn2R0is=; b=Cq2V1HLt/WvU3mF0qGLfWgGkkkxwuiAoivJ7JGt4Wp/MqdG+9t66o8iQ01WS1Jw02K um4k4coU3tbWhpp/nlxkzo77sWSmkLrdrLpjbRkngNU8fkh6RIIe2ZrOq/BmnOqwlSaL 57UfyW3wifEFCJ9/83B79DQAA+TpYG6SxfZx/VobsaCDuzgAckMUlUll92wWzeYkjZiq 4bdIXQiXbr3Vk427p7Z35T1fo/zGqAyMfjRDG+JqIj9grworK/UQ+olDiOE72y0EBfeO oJo+XwGtvyMN47JRQIcXLnvI7OG5NDRKb2oeGwSxWv+Z/AiH/GLXwEY1yZuL7DGy274y RqUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707496505; x=1708101305; 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=8eG/PuLIoWB8LKFjP4cPEQOG7vgr5hu+n2RMYn2R0is=; b=Hle+80zSm6eqECFVbc4DDlMVBHyP7Pz2QD+O5N1sZWAD6R5tCUKOUZ8RBydpBkJH1T awQKUf9k07PjmkMwSQFLtLazB2EIcEWL+qFGV0E+iQ7qp7kCNvFVsVgxrFVtt+IXaiiU E20VJiiCrN1d75o5wuz7rORr6BPBCovd85FvzrGrXafU1Pc3HyEz92IHhsuqwHSOGxSk dHA9fIAAqyxcmmNd1MD7YvuNyOZMkN7x9+rBxEPSEmTDfgbSoXE2bHcsVmWQatk8JWpX rT86wrwlSgQyV70YVo4Jd/PwIkNTE1o6REJyjG1wk9hJJjut8LFrONH3S/aIQNSqEq8C 5Dmg== X-Forwarded-Encrypted: i=1; AJvYcCWq9pDGQIMH5sWrKugU66ILuPH+5H0cCVVZzoXpZYWQSJhvmPqEzBOeVkexCXlISp9BvasqdwRKB2RRH7qSdbv5yDrqZWSHsCbuIpTS X-Gm-Message-State: AOJu0YxH5HkPijz6FQWXaS+KMFjfd5vHKMx10vrKaeY4r394Xhs+l8pm psHfe7Yd8ihvYKxgTF9mFkehXxDVAdGwqr1TYoAiqTwrF4RLT+UKi/nDsd2wFw75z/hEsv0CQmG XesveB4JCdepnPZWIig== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:1c01:b0:40f:dd0e:b43a with SMTP id j1-20020a05600c1c0100b0040fdd0eb43amr23755wms.4.1707496505594; Fri, 09 Feb 2024 08:35:05 -0800 (PST) Date: Fri, 9 Feb 2024 16:34:46 +0000 In-Reply-To: <20240209163448.944970-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: <20240209163448.944970-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209163448.944970-5-vdonnefort@google.com> Subject: [PATCH v16 4/6] 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: 1790439968364547992 X-GMAIL-MSGID: 1790439968364547992 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. Mapping will prevent snapshot and buffer size modifications. Signed-off-by: Vincent Donnefort diff --git a/include/uapi/linux/trace_mmap.h b/include/uapi/linux/trace_mmap.h index 182e05a3004a..7330249257e7 100644 --- a/include/uapi/linux/trace_mmap.h +++ b/include/uapi/linux/trace_mmap.h @@ -43,4 +43,6 @@ struct trace_buffer_meta { __u64 Reserved2; }; +#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 4ebf4d0bd14c..2c0deeedf619 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1175,6 +1175,12 @@ static void tracing_snapshot_instance_cond(struct trace_array *tr, return; } + if (tr->mapped) { + trace_array_puts(tr, "*** BUFFER MEMORY MAPPED ***\n"); + trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n"); + return; + } + local_irq_save(flags); update_max_tr(tr, current, smp_processor_id(), cond_data); local_irq_restore(flags); @@ -1307,7 +1313,7 @@ static int tracing_arm_snapshot_locked(struct trace_array *tr) lockdep_assert_held(&trace_types_lock); spin_lock(&tr->snapshot_trigger_lock); - if (tr->snapshot == UINT_MAX) { + if (tr->snapshot == UINT_MAX || tr->mapped) { spin_unlock(&tr->snapshot_trigger_lock); return -EBUSY; } @@ -6533,7 +6539,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) @@ -8652,15 +8658,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 == 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; + } - if (cmd) - return -ENOIOCTLCMD; + 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++; @@ -8673,6 +8695,97 @@ 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; + struct trace_array __maybe_unused *tr = iter->tr; + + ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file); + +#ifdef CONFIG_TRACER_MAX_TRACE + spin_lock(&tr->snapshot_trigger_lock); + if (!WARN_ON(!tr->mapped)) + tr->mapped--; + spin_unlock(&tr->snapshot_trigger_lock); +#endif +} + +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; + struct trace_array __maybe_unused *tr = iter->tr; + int ret = 0; + + if (vma->vm_flags & VM_WRITE || vma->vm_flags & VM_EXEC) + return -EPERM; + + vm_flags_mod(vma, VM_DONTCOPY | VM_DONTDUMP, VM_MAYWRITE); + vma->vm_ops = &tracing_buffers_vmops; + +#ifdef CONFIG_TRACER_MAX_TRACE + /* + * We hold mmap_lock here. lockdep would be unhappy if we would now take + * trace_types_lock. Instead use the specific snapshot_trigger_lock. + */ + spin_lock(&tr->snapshot_trigger_lock); + if (tr->snapshot || tr->mapped == UINT_MAX) { + spin_unlock(&tr->snapshot_trigger_lock); + return -EBUSY; + } + tr->mapped++; + spin_unlock(&tr->snapshot_trigger_lock); + + /* Wait for update_max_tr() to observe iter->tr->mapped */ + if (tr->mapped == 1) + synchronize_rcu(); +#endif + ret = ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file); +#ifdef CONFIG_TRACER_MAX_TRACE + if (ret) { + spin_lock(&tr->snapshot_trigger_lock); + iter->tr->mapped--; + spin_unlock(&tr->snapshot_trigger_lock); + } +#endif + return ret; +} + static const struct file_operations tracing_buffers_fops = { .open = tracing_buffers_open, .read = tracing_buffers_read, @@ -8681,6 +8794,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 diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index bd312e9afe25..8a96e7a89e6b 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -336,6 +336,7 @@ struct trace_array { bool allocated_snapshot; spinlock_t snapshot_trigger_lock; unsigned int snapshot; + unsigned int mapped; unsigned long max_latency; #ifdef CONFIG_FSNOTIFY struct dentry *d_max_latency; From patchwork Fri Feb 9 16:34:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198977 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp976175dyd; Fri, 9 Feb 2024 08:37:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUzP7M+Gngxk8KPeT82MIZ1G5SxxtuOUlLQfgS9RJinfXyTrJfN/hfzliN6b4gAG+YUnuMAnGH3qG8M/Q9W5cUDfQzQjg== X-Google-Smtp-Source: AGHT+IEEn8o6ipkxaUICDKd6um4tf49nol3nsHGDC3DVvY/7mEfMlFitOGy1PivheIcW5xQ4yMEW X-Received: by 2002:a17:90b:154b:b0:296:1a66:bc64 with SMTP id ig11-20020a17090b154b00b002961a66bc64mr2008847pjb.40.1707496662431; Fri, 09 Feb 2024 08:37:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707496662; cv=pass; d=google.com; s=arc-20160816; b=tkHi6Y+W0ILcVLFuFLteGLxVWAQ0sXgkKHJ3F7yPCX1PNv8z14VGt/i7t8aCWOeCva 94QA+wB5qHBtB6jgLVBJ1HVAMUoDe8Fyl/2p/GjJSfy/lUlDtW1s/m5ke8L9sufKWAcm mK/fhnkxovLFgN2q6KdokW7GO2gRcVuotjYII0xssS4tBgUPcl0MHk+fVtVzxi0FC2UX VAYcNpVMRSAYX1y3LppVVoaColg2p/tnZvOcfd3Y2T4Sbv92niYlwcqZ1jzeg620sZnn ElpOWKxoMdoc6hLRZ8coYpTpMJruErjagvQA7astG5XGrP7toiPM2TJouzPcVHk2PKwj 0k6w== ARC-Message-Signature: i=2; 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=QgkRDZd+PHk3bo7bl2fMfaI4+BE0z0Hk4GDGmCozprQ=; fh=bH8h3fyYA8QJocfX0gbVf1tl7sHaf1SVao4MAmY7KSc=; b=KDyBCdrbFmroLqFLY376+6tOJDRfuOwiW3Qd9mcNPqn+JT/kS/eaib+t5Ufaj48lyO uQ1ihS5c+GjLlRGiYJF9obKh6bWQZ/tSaA3efaot6TncZ4HILXsKMxBi8kTkFEVvJGFR 6mqWmpBxapUS4RPLRzvKByqRFJvPS57qeKEFbZQ5rsg86yOtU7WflGwZVeqz1rrcLQWf jF+ACZ0Etg3aTysJHF1/psFBgUxU/FeulJCBgnqxTxiGBwP2B0nb5dxSmcRdjv1DbC3a tgeST6IX3zWLKTQLGLmNCe6i53MX/59hL/4cxQxS4EaLX99V9Ft13nODN5XTzmtRqiVX xbvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Fvxi2BUn; 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-59615-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59615-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUdz7mQdgdW5X4vOCxBGlFFQEI6Hi3zmYupG+l0LDdfzU1ThyAi7xVSqa6SmN/L+6QcwOCYiCMJVIbMLmttlwwvHfsZrQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id v23-20020a17090ae99700b0029654218b03si1816952pjy.46.2024.02.09.08.37.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:37:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59615-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=Fvxi2BUn; 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-59615-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59615-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 45F63287571 for ; Fri, 9 Feb 2024 16:36:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4B6C082870; Fri, 9 Feb 2024 16:35:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Fvxi2BUn" 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 19E837F46B for ; Fri, 9 Feb 2024 16:35:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496510; cv=none; b=t39zaZIhzD4HpjIDWaX8fpTmGOpDHswIf8l7NOk3PBk0VYFGKAcJb1kTCv1cPUgpBVX0BeeQUD/gP6oGJeMaYu4G/sQUZBLWZ/mZLn3SfEthySf8PcPOSPUlwNaWUjMflVBiMU5rnsshxTMAvQTRBTs9rAlZfZE5S33hbe1Yzyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496510; c=relaxed/simple; bh=iPiTM2h7tTVjI8Bap8UHkeVYN1GEEKgHI982louZIPs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lZGsXoEkihzx/5fuEhWGqpGyEAWmm/QDiNpgohPHpLCgdPKMTKjKSIRscpY9z8PSKPYPPdXmnVqeLnpk5fR8VfQfvFZLbeK9eBCBSgro/9vUFDIh0PmTaxy6+7R3AokvANQEhnIPe+ES5ugn0+y9jqbFpfU//mJ2swDVwji2Co8= 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=Fvxi2BUn; arc=none smtp.client-ip=209.85.128.201 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-5f874219ff9so16087807b3.0 for ; Fri, 09 Feb 2024 08:35:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707496508; x=1708101308; 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=QgkRDZd+PHk3bo7bl2fMfaI4+BE0z0Hk4GDGmCozprQ=; b=Fvxi2BUn/41IPoDQi79WUtZgyE6IEsQA6uqdyHjMPCP4friN3y5j3DBdPrC1wqIPOB ziq/LMNneg2GmsSCZgPovjCTcQZqMYIQMbOYjUHhD5X7VIWoXDAQo1r0CFRuomaMtN/D vZQfMRf3qIUteCRcVciY6AYDnKpWalk/yrYjzmosW8wA++0yt/pDDPspq350FdgqtlRe MtOAccZ+bHYqhg7tvu8hk4pQlduBYtVXDZ999jmNnW5PmOm3ZdPw4XgkR5sh3/Vcih+T YNM9ydsPudrrL5Zru6rs+z79m7AWWQxZR4ZpSyuszTx51xLJVKALGevvgn2M7nBolim8 XWKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707496508; x=1708101308; 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=QgkRDZd+PHk3bo7bl2fMfaI4+BE0z0Hk4GDGmCozprQ=; b=lM1IGMCDoXoEuOxpmR7u0cal1rJJ58gqbjZ1/vv9sH027V0Obb7of9iYoYOr7u9dUg AbdsdcE7pbaCBNY5jKpD7KCdOpU0pJ3sfIE5UuPYuwFLvUdqNl6Hp5Rom2fjMmljXiA0 2ftnpJd0aYXVeVk6+zz8z7cX2K49rdqCpa+oQzuOb3o/T8cwphogFwoUPGwnwG6BSIX9 ByX8GupTpOQRN0s8kMHNMrAbxF+XMqvHZovU9R49jqjQ4RydK6r5sFhUdKxHd2pl6PtH F+bFrdS+0aYeLTkcV9OuXnskTi4YqCtj7b+eiAXBAo/DOS3YTdrQmfStrOuaVTZeDHy8 qiEA== X-Forwarded-Encrypted: i=1; AJvYcCWqmysh97uppXqmuMSqGIhenNZ1gcVqV9uELqx7LAU2dUkXIxUFMfqQKz1qeUWfV0G2bLHu1LrpDTtFjjH4LQGXzG7vlAj0ZdnSYLLj X-Gm-Message-State: AOJu0YzlbQ4Z1K504al1AAWpXXBLim/55VCMNO/KnLoB208nW1qiko/q 79P06JsUnsIqydoHHqSqncGJgB/vqiHpvfrGCOfvZMqMh0IzJao7v7ebClCuRQb+FSdFQ1kvfkO 9e//q4/5HwpSU7iCT8A== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:690c:4284:b0:604:982c:3c26 with SMTP id gj4-20020a05690c428400b00604982c3c26mr276117ywb.3.1707496508221; Fri, 09 Feb 2024 08:35:08 -0800 (PST) Date: Fri, 9 Feb 2024 16:34:47 +0000 In-Reply-To: <20240209163448.944970-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: <20240209163448.944970-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209163448.944970-6-vdonnefort@google.com> Subject: [PATCH v16 5/6] 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: 1790440020151890289 X-GMAIL-MSGID: 1790440020151890289 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..628254e63830 --- /dev/null +++ b/Documentation/trace/ring-buffer-map.rst @@ -0,0 +1,104 @@ +.. 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 sub-buffer ID which can be safely +read by the mapper (see ring-buffer-design.rst). + +The meta-page is followed by all the sub-buffers, 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 | O_NONBLOCK); + 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: %llu\n", meta->entries); + printf("overrun: %llu\n", meta->overrun); + printf("read: %llu\n", meta->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, meta_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; + + printf("Current reader address: %p\n", reader); + + munmap(data, data_len); + munmap(meta, meta_len); + close (fd); + + return 0; + } From patchwork Fri Feb 9 16:34:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198976 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp975852dyd; Fri, 9 Feb 2024 08:37:11 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV02Tf4AaA9VC/4bfoQusXIAcISNLM9I3v7y6e26luCdYOCwNnvuLxWk82ahjYCjNed9/FVLwlU88W7Xd7TMFW7rMYFnw== X-Google-Smtp-Source: AGHT+IGff8uYe1mCRZkaY8ZFxpvlJZV9JjEFUrHApRZPt8E78VkRkOB60alZhPcCVE+eBGdaKl/x X-Received: by 2002:a17:906:454:b0:a3b:d25d:ebb7 with SMTP id e20-20020a170906045400b00a3bd25debb7mr1573228eja.74.1707496631390; Fri, 09 Feb 2024 08:37:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707496631; cv=pass; d=google.com; s=arc-20160816; b=yToFDgwXcwQdi71zcDh3QHnETgy9eaTwhKd2MZSpua39PfXZ3N2de7M1uZ6HvW3+Jj 9Kv0a1VStpyZN5qsxhXx1x32i9YGoJOGvLXFps056fkgtkyRm0qXL2DdGtG+XDZ0paaq iuoOm5a0PVYPMOOUYpFh4fSEb3yOSPNkYxp+VxoJjxKlw7115KWqxMQRr81zfNSXEzwT 9s9Pnp4alUiGL63LW/wD6sGHJEvLdWViTLfMHVFjbpqGUJwHvxryB4iudJN+JDA6mD3j Tq/3H5hsLJQZVNwUndynNQz2idxfa3IU26PMjQlBlOMVzJoij9iMVhj4vuUQZlcjKLwL N3vw== ARC-Message-Signature: i=2; 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=gMbQC9Qrfc/CHbT/uyUQA9ufo0litTV7XPwdOdKcRAI=; fh=cSWwmbsthwTKHfuJGT+gbSRQJfoKJrcDPsaV7NhyUPQ=; b=b10u3ucCHovPiRxk8+qxNqXRvsIXVQUo7+Gi2P7KoqAp9Hs8rZL5Y9KvRXhYf2T+SU NnmFQxJgVCyf2//4Hj7zaERafgugHiGPNroxj53R4yJbL/NWf3vmeWSlVUwNxhK9BKnE cmrty46bbJEp48uY9UXfjuy7GUotvRE/tkFpsK4U5f/tayh+w6bo1lUvUGGqXo7vLukz WJqdO4Y9Wk4sN+yNSnzrz7Xznpwe3WXm+jx/cAfio9JyDbhlO21e0y2O0N3ZLV1U+vE+ YqAtFRPMWyCeYES5CVc/ssvnoXnkFXeHr6IJfukrxuMdfvcnAcXIhZE5+EJtVT/mOKrv iahA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pTKvZpHs; 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-59616-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59616-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCULbUFHJPhtNmAy4zh9RfnUFBmIAJS1xFd++DPIcw7dcAGkoiJl6HJI5f/YVdDohHvpcWIN/jo91ABFlrmSyGRBFJP6tg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g21-20020a170906395500b00a388f94bbcfsi944548eje.657.2024.02.09.08.37.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:37:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59616-ouuuleilei=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=pTKvZpHs; 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-59616-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59616-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 D57AB1F22892 for ; Fri, 9 Feb 2024 16:37:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F86982888; Fri, 9 Feb 2024 16:35:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pTKvZpHs" 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 B285B80C02 for ; Fri, 9 Feb 2024 16:35:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496513; cv=none; b=P9oz+BuxkEQzMwsKpcGPAJ6PMFdSocIbbjJzC7bjZPEguWEwDTQGbQCI/4/goZe30qfCjr4E9RZKe5IiOw8PaaNhu/zuixzsoQ0uKm69wOSVEmsMoCdwGuNu97fMuVjY6eiJ4dBpPqPrRobMWq9RMKhyPinDVhCYiWZpv0F+qaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707496513; c=relaxed/simple; bh=yF4snXCigsdD2V4iu9JXl93VeJuVdieGXDlgLcIwa4g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iNrjaosERN4z1EefiZ+7bwmjIG//4EOZbDqwUibjxGeHX5UCIqUOnpZJnEg+Qhi2+PlgLpj8iGkefp936hvBjo2uqcfyv7hmPJUSZbfyuXMOdS7fDth1jkGvg1fZQnbyZlFhdALKNpIKmNiyNhZGikBpIamgSahQB0TJPC0NfUc= 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=pTKvZpHs; arc=none smtp.client-ip=209.85.128.201 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-5ee22efe5eeso22946207b3.3 for ; Fri, 09 Feb 2024 08:35:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707496510; x=1708101310; 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=gMbQC9Qrfc/CHbT/uyUQA9ufo0litTV7XPwdOdKcRAI=; b=pTKvZpHsEcx0X1kiTj54vR/o94Zgp1Q3WwdrXEqK6CYRAJnMpchoHmvAnoFfpgdio/ tTDIQtHG+X87bYpgh7wsJ/Zvdkzsd4KtO+WmxZSUO+HN5vyyvUL0bmVkCbhRGUvhVvIR fhRc95FpS2tNjm1fKhHvg11XAQUR/ONPJJuDUc7WEBayCAyfWyJX57B8Eng8PTX9HFK3 Ffgs9YFJ9788SrmEX08YdnN0ibsofACvNzSUxYI66AWwAJjXRkfvd2Fmyi9aRVg1NUUj 3f9OXPcpPbehl/PmLPBkobHqT5fksOHGa0pq2V9sk9J2VNJwk1uVIlSuejW+WZ6LtPFm h7EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707496510; x=1708101310; 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=gMbQC9Qrfc/CHbT/uyUQA9ufo0litTV7XPwdOdKcRAI=; b=sCmHNvAT497PSWwPVYjyHB7bhRFsiMz+olqXss0lrwx0PR1F8UsgSxTW1nTwOTTVPG ZVXejk2WSWpLCutsj0ueP20J0qkQMcRoU1b2rynmMkj+t5NpsDwWuxlF4g8KIHLKzgL8 Y0OETQrX/o1Te/1WTFqYrC4aM4aDbcB5C+Ig8jJW/VQxo412JldM3dkWyAm+8p3K2OUF dRlLJHcrUARjpKP5ERbe0A9U0g/q5v9DXTzBsOtHsV77jypqUgUYsqwb9WJFY4ZrJEgE 67Ya8AP7upbWCNO2e2f8Aytq1Uu3ppUNcck1FypKc5mBfsJHAYqvQIwPromsEQRgNDLo 6jZQ== X-Forwarded-Encrypted: i=1; AJvYcCX4p7xUCQkGXiryQJ4E8tZzNNpUzpfGk+wUOZYUzUdafHplxxTLbd1vC1C8ZRlOl1H72qlewhHeJMXcLvT5WHMlCCWZiRSun6a1cOqW X-Gm-Message-State: AOJu0Ywd5iWh3ZoCPzyQi/SU+b4CT1Yp7fJjeHjx4BWQJE1hJ5bvp7Z2 OwMZo6uhrXU1llDRWUbRoe83u/g5YnyqxmQRRMdWO/ekUWkAfPCSH2Q5Ne8tDF7g59o7QHhb57f 00XL/jcSv/lZYkCs1kA== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a25:c586:0:b0:dc6:a6e3:ca93 with SMTP id v128-20020a25c586000000b00dc6a6e3ca93mr46762ybe.10.1707496510734; Fri, 09 Feb 2024 08:35:10 -0800 (PST) Date: Fri, 9 Feb 2024 16:34:48 +0000 In-Reply-To: <20240209163448.944970-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: <20240209163448.944970-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209163448.944970-7-vdonnefort@google.com> Subject: [PATCH v16 6/6] 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: 1790439987733530528 X-GMAIL-MSGID: 1790439987733530528 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 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..d936f8f00e78 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/config @@ -0,0 +1,2 @@ +CONFIG_FTRACE=y +CONFIG_TRACER_SNAPSHOT=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..56c44b29d998 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/map_test.c @@ -0,0 +1,273 @@ +// 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) +{ + int fd, ret; + + fd = open(path, O_WRONLY | O_TRUNC); + if (fd < 0) + return fd; + + ret = write(fd, value, strlen(value)); + + close(fd); + + return ret == -1 ? -errno : 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) + +#define tracefs_write(path, value) \ + ASSERT_EQ(__tracefs_write((path), (value)), 0) + +static int tracefs_reset(void) +{ + if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0)) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/trace", "")) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/set_event", "")) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/current_tracer", "nop")) + return -1; + + return 0; +} + +struct tracefs_cpu_map_desc { + struct trace_buffer_meta *meta; + void *data; + int cpu_fd; +}; + +int tracefs_cpu_map(struct tracefs_cpu_map_desc *desc, int cpu) +{ + unsigned long meta_len, data_len; + int page_size = getpagesize(); + char *cpu_path; + void *map; + + if (asprintf(&cpu_path, + TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw", + cpu) < 0) + return -ENOMEM; + + desc->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK); + free(cpu_path); + if (desc->cpu_fd < 0) + return -ENODEV; + + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, desc->cpu_fd, 0); + if (map == MAP_FAILED) + return -errno; + + desc->meta = (struct trace_buffer_meta *)map; + + meta_len = desc->meta->meta_page_size; + data_len = desc->meta->subbuf_size * desc->meta->nr_subbufs; + + map = mmap(NULL, data_len, PROT_READ, MAP_SHARED, desc->cpu_fd, meta_len); + if (map == MAP_FAILED) { + munmap(desc->meta, desc->meta->meta_page_size); + return -EINVAL; + } + + desc->data = map; + + return 0; +} + +void tracefs_cpu_unmap(struct tracefs_cpu_map_desc *desc) +{ + munmap(desc->data, desc->meta->subbuf_size * desc->meta->nr_subbufs); + munmap(desc->meta, desc->meta->meta_page_size); + close(desc->cpu_fd); +} + +FIXTURE(map) { + struct tracefs_cpu_map_desc map_desc; + 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(); + cpu_set_t cpu_mask; + bool fail, umount; + char *message; + + 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_EQ(tracefs_cpu_map(&self->map_desc, cpu), 0); + + /* + * 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(); + + tracefs_cpu_unmap(&self->map_desc); +} + +TEST_F(map, meta_page_check) +{ + struct tracefs_cpu_map_desc *desc = &self->map_desc; + int cnt = 0; + + ASSERT_EQ(desc->meta->entries, 0); + ASSERT_EQ(desc->meta->overrun, 0); + ASSERT_EQ(desc->meta->read, 0); + + ASSERT_EQ(desc->meta->reader.id, 0); + ASSERT_EQ(desc->meta->reader.read, 0); + + ASSERT_EQ(ioctl(desc->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + ASSERT_EQ(desc->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(desc->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + + ASSERT_EQ(desc->meta->entries, 16); + ASSERT_EQ(desc->meta->overrun, 0); + ASSERT_EQ(desc->meta->read, 16); + + ASSERT_EQ(desc->meta->reader.id, 1); + + if (!(cnt++)) + goto again; +} + +FIXTURE(snapshot) { + bool umount; +}; + +FIXTURE_SETUP(snapshot) +{ + bool fail, umount; + struct stat sb; + char *message; + + if (stat(TRACEFS_ROOT"/snapshot", &sb)) + SKIP(return, "Skipping: %s", "snapshot not available"); + + 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; +} + +FIXTURE_TEARDOWN(snapshot) +{ + __tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger", + "!snapshot"); + tracefs_reset(); + + if (self->umount) + tracefs_unmount(); +} + +TEST_F(snapshot, excludes_map) +{ + struct tracefs_cpu_map_desc map_desc; + int cpu = sched_getcpu(); + + ASSERT_GE(cpu, 0); + tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger", + "snapshot"); + ASSERT_EQ(tracefs_cpu_map(&map_desc, cpu), -EBUSY); +} + +TEST_F(snapshot, excluded_by_map) +{ + struct tracefs_cpu_map_desc map_desc; + int cpu = sched_getcpu(); + + ASSERT_EQ(tracefs_cpu_map(&map_desc, cpu), 0); + + ASSERT_EQ(__tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger", + "snapshot"), -EBUSY); + ASSERT_EQ(__tracefs_write(TRACEFS_ROOT"/snapshot", + "1"), -EBUSY); +} + +TEST_HARNESS_MAIN