From patchwork Fri Feb 9 10:21:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198813 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp758495dyd; Fri, 9 Feb 2024 02:33:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXnSudEPjg4aU9rG3pVrFuaBMExpHHL1Q+JQYx/dSca5kPlFLYNAYAqR8TwzftknNX/T9ojipq00t2asouyvM4u0LdqAg== X-Google-Smtp-Source: AGHT+IE2TiDQKg5NfdAe0q6gsbIjcq/tp5BasQzI8sxtGl8YMSn4VcNQgpIsGIFh/Eb+0pQq7+GF X-Received: by 2002:a05:6a20:d387:b0:19e:9a7b:230f with SMTP id iq7-20020a056a20d38700b0019e9a7b230fmr1738740pzb.9.1707474823289; Fri, 09 Feb 2024 02:33:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707474823; cv=pass; d=google.com; s=arc-20160816; b=aA4BqGYmTFTIFlk/ZsB+CEMceEwAmZis2DHy2rICJLI7LDq4t4OphoX1t4f+JtbnLA hwrzEGz+cdSGQxvaR4TkmdshXTNSsm8Cyk0pVMd+v9tdCsch4q6XyjIZlHGn/yTjsFCo eWbWiGDQPutCwM43JUkGQsPUi53czXFcpmW+Hdlgyr2Z02rlEe8q8QoIsPJaSJSn2AsJ +AMvamOSxSoR86mpETxS4xduaRotgpPukZNDugeHTZcJemMx44fS3XQDEN04rtsG3luW OlkamRRjDmWcrBCI+mLr7fBOp01UcGUas7lUsMQGdW2e8yFW4q9OhN1FDjV0ItoPFYD1 25qg== 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=nia6ZifGSjn3uOiG+MNctK0SdOPrlD8+zjkFBYN+B80=; b=apwU0+rdwu5/phuaxObuLq/A2VsAdM4WU2e8Pqdf/dfWaoQmpQ2ILdTjLMIzCLdw8M Gki6c4G7eMpU5F09RUPAOFGHq1qeqYqFBeELXeAcDmEri/wapreE4d9Zlgq74RiwhNCB R6VEFJH4s57YD/PsIRRkUKrfLNeBFkw2vIyI+EkILP4qHuMJGn4stDrfm9zUgvuKhMGV FrMLVjkNKUGJ+SRYLLa5xsXRuoaHbjABDRYE/xUX4L9Vaue1TgVZbl8FseJgi0oCcHt/ KgM3h8WqTTbktzFYhnq/ASEvcsI1nM7IdNmFPx5cRO/YknNEuDjJIqlhFZZ1A6AGgMOZ /Riw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kLlCa8RU; 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-59211-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59211-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCW1m+4/IH3SPIj72pzI7MlHq0wkaidim/5EImzIXX5iQcZnHg/to/EInD2nppivX6Uyp2u/qpC0q70nJ92/g0nHMDB/CA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id x69-20020a638648000000b005dc41faf522si1472815pgd.790.2024.02.09.02.33.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 02:33:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59211-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=kLlCa8RU; 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-59211-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59211-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 2594EB2823C for ; Fri, 9 Feb 2024 10:23:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B4FEF69941; Fri, 9 Feb 2024 10:22:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kLlCa8RU" 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 5D77A6997E for ; Fri, 9 Feb 2024 10:22:15 +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=1707474137; cv=none; b=mNRf8eyA9DZoxZPw12UuMtBtHZc/MEBKoagUo1ICHiDuG0+zkw3MSZwzB9YLNIvX263ntNoGGsf4qC2YDSZuCFH0dQYFOMBDFPo0iULGZnYAt7DwpW8aP5tJlZ83Rzq3rBkhZP8WXfD1j75Q1NrzYVJuPIetC0e7TLQYfXNzjkM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707474137; c=relaxed/simple; bh=tpt51FeO44HeEXQqmygp1fYLZoXSRZvsHL381MrJybw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cWPCaQ9J5Nb2YBrZyayuWJSmdj8/YtVfnWV5F6A6m8ug29V2PzxK6eiMeU0Rr/PcbGdDWERHhanWbEafwbjmiqAuDXVy+aSzk91Gv3ruHm8hYgOlwyBEfo7151P4mF6qot8xJsuOHsttlU2tHs9Nd47N22xjhXIStNIz+H3RdKU= 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=kLlCa8RU; 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-41024549a9fso4812355e9.0 for ; Fri, 09 Feb 2024 02:22:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707474133; x=1708078933; 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=kLlCa8RUO+aJbOAGzlI200T2102ksm40Zm/LCL3Wby7z/nlFYP3NWZ/657VnDhipGZ FYbd1gq6jfLt0HHKJ7lbZ5w7bBu9SjZWzHe4s+G725HkOHfSw/31yYguJAQEd725cBw3 aa0KRf9tOKsBIIbeqLORDDuduYZVgAAzfXvLQSqeWPXm5tRFhFjCfM2YjZ8XwzIHMvXi /I2U4BZCcfkPAX+/c6H8VAehUQkk+fRYDNE+x/fMLJdO1v4OBv4vpTIaeBDHX/+IN0xy 7GPqjB21aWGMr1lqUuJm389D9yAuj70YSucfg8OvhlMcAh5m+0MnattXBWOznX/2n/EH lifw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707474133; x=1708078933; 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=ciip3GTCfhD28l+3OvRSKGv12ktnlhUbzhFzihstjrHwzTgGG4Q5SPI/wqgIPozlCh EoQvj18m04GpNcgPsq72nYxIKN9VvRewlsoJeu8C3Y9FO33cmVNK3tIP8q7RZxoxuKEC IkqSwX1pEksagF4jEgWEq4O8QSgmTZcsZK+aV6hIDwfFTKw3dewM5eGkATmhxCo5jsn7 E4ciP7yukCSCDRCP/Q+Psg0rjZWFGeNMaUYC+T3ssAsTwNtx3/hZ3kZ2ZNnRPIaM4o8p NIVLTj+EHQntcn+jEjPZeVRW2no2N1hpc/6Nw5zeCXEiXjzWYuuL7DR6grxeOrKe3lHe ImlA== X-Forwarded-Encrypted: i=1; AJvYcCUgXvtRqASxBOttQ8LtJhjL5jdRNBmq9dNkM/kA0HAl2DrpWvVXzT/tP2snAiQllSZOcNus1pmRk7GSoHab/0P1ZVUZ8/dzGxY/ZZEY X-Gm-Message-State: AOJu0YxU5fNV0NjoNs3uu1lmYixkfsZ1jX0iPxuIg/Y7HoSJ52J7x07e IikgepZJRSSafALXNMnm88tEoBEqQNLKOVcS32g05dHA/hZ5xsWxGrqHiru3HrE80NZokQNjehZ 3nU26pxI+DjB7lKGiHw== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:54c9:b0:410:6ab1:4964 with SMTP id iw9-20020a05600c54c900b004106ab14964mr18382wmb.1.1707474133639; Fri, 09 Feb 2024 02:22:13 -0800 (PST) Date: Fri, 9 Feb 2024 10:21:57 +0000 In-Reply-To: <20240209102202.649049-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: <20240209102202.649049-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209102202.649049-2-vdonnefort@google.com> Subject: [PATCH v15 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: 1790417120545721863 X-GMAIL-MSGID: 1790417120545721863 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 10:21:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198804 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp754213dyd; Fri, 9 Feb 2024 02:24:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IH110dGKOFLw9AX7f6evOLwAxY5kFZZi/y+EtFiXoVXi5hImt1v8YC1LZc2BHMlcxF37kWf X-Received: by 2002:a05:6808:5d2:b0:3bf:f233:fb36 with SMTP id d18-20020a05680805d200b003bff233fb36mr1132203oij.24.1707474270280; Fri, 09 Feb 2024 02:24:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707474270; cv=pass; d=google.com; s=arc-20160816; b=EeMrI3hfEPDy7i2bpzBh7gLlfHMWcAxSrzTA0JcI5atAqgS5zzSuy/GC9xpBi4fvZn wJKsXQpxe6cv+aID6sVmjQ4x2mFrnYLcP11Ef7N4dGvk8LZjsbab03rJCEbSaxezFLH5 7U38kPHxpoknThLQEG7U91xJICDWuSdQVMQuISpuk7XzGrlB/TC299HWmPjt5ESvc6lx LsZnKStTz0fc+wqFbYETimZPekaTVi2XhAmmjUyNYRv7mNQu6ElfWSFEHxYhD0Lqfrub 7kithEcOPhZ148MpjSHs5MV8QE0eNnuM776Lw4XWltmvJq30pUTjb0Tlm+Evzr9bvCCA OgbA== 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=x3dqWplP9xgSYek3YNICRH7gRHy0MXezLSqPhhfeORI=; fh=Zz/2JqsnLW9ys0rd8UD1gMimtA48RTLZ0dGxJPmcF/s=; b=pnxAKRNcyyKDD0Q8tIjYS0Si/sMKq5Y2bxvFxeWwao3pI22svdqLqEGAk7MTHYT4cu tQWRF+hAtagauyVHCr2/I5i1QUBdplDOmyCIXdHVMNrojlPuaVwUeIRYisQV3BP7mIi7 2Nrxdc9JXkR/9KHo2T42M0L6ZHRbc1XKBtGf+++NFY1MUokt2Ut0TBRWp2uvDZylp21H cVtJiXAsDkUBuTILQA3xYHIMGOa6A1N76/hUUvSmDI7U2+yiZUVbL4JkmGGSHDDJKPep kI5Z/VavaUv0d24Rga7rhPVgj8unf4ZZb/Lrmg96/59RVP2ZfHCihP8I8I9GfaZTlbgH iamg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="kZJvol/a"; 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-59212-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59212-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCWOElF30ziACboNnbsMUpMaR+0kCIiG1g0BotphVo+q2/WCacBAyjhIuXQa1rKUL2AgnJt6zoPvJLI+8GoPthf1UQje3Q== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id m11-20020a05622a054b00b0042c2fd47326si1493515qtx.26.2024.02.09.02.24.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 02:24:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59212-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="kZJvol/a"; 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-59212-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59212-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 016C91C21B5F for ; Fri, 9 Feb 2024 10:24:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 722BE69956; Fri, 9 Feb 2024 10:22:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kZJvol/a" 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 A68EF69D1F for ; Fri, 9 Feb 2024 10:22:17 +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=1707474139; cv=none; b=WGHEkM1/PFXefHpqV/iQvYlXfHO8kmAWFVrqKmirQcMajIY/4wzthb+xeyejNbWCUXqz8SZUI0ie/Z9Ca45G0j7VzIPSKCTQ8nwtDg24piVb75Sva90i7bCkC1O3L77w4/KolrSP+/kJ7vVmlW3hGkMnYz1y4dfbucNUs36NA9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707474139; c=relaxed/simple; bh=BCzbpsY0bJKZAiKvZ9RPTY3p73GPsjLE7KCeqazSElk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=V75LZku3aVb7RJ3hul+HWyPT5mZjWHimvtCYu9M4TLg5Ka4cEh0I2wdFZCkfIRbhFUS7HVLxHuyk8fgPKSKLqt9MBfS9vYS78Qw+1iCN+CEKufxWBrFaHs73m+FnsMSHJIiBrhoCER/ig2uyYix9cosC4p+1yC+1SehY26qoHU8= 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=kZJvol/a; 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-4103c3c4c23so4529775e9.2 for ; Fri, 09 Feb 2024 02:22:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707474136; x=1708078936; 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=x3dqWplP9xgSYek3YNICRH7gRHy0MXezLSqPhhfeORI=; b=kZJvol/a/mvrP8hY8aIZD3yc8Zh/WqNpysMtYMWQ9kgHWGIeDiMYI+HOl1e8KnSEWE q/Ibja29g7LDlDq47gmDoydkMw/yCjm7c9DWJq2/FzWSkvcfzg05MVKYeJmYl7F2OVbM oIb8ElFIPxvqJGN8CM+CIdmO/G2IzR4rpJASARiR+vQMW8oXppqn40Yp1qFHCwvglMNj dy7qwQDlBvwBG5JvRUNWBjE6YNgCTsAYvw7nbe51IZZtusUE6lc8FmEoNRspSjgTj79m OgzVZfx9f5mWQjsNh631DgOaGi0Huib+ghIjNk5Oy0cNiZfqw8QOVmAMLMyrLqJxdWH/ 7dNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707474136; x=1708078936; 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=x3dqWplP9xgSYek3YNICRH7gRHy0MXezLSqPhhfeORI=; b=kM1hJJHlUsomVg5XLWH8R4wu0dNf6gsy82JpPgdVA1axMb9r2y8CqgtPsqeRehhV+T mlRjEhrfdESm3aiZ+kFF0gJaXnkPw3JmQ63Wrnslq9eEBpV7UOFPqr2T0q+m03frPYNQ 5zDhTpJlUbxKFa8Ap4nUqH3/kWAudoOroGhA1pv27HthSUR44TR3F08iHTkI2Vxc1DMh RHapLgIi1OyXTH92Ltyy0nLVN0sBmjOXdXjN/HVZxKDFa7iT/2gsOhomvxWObzwy5Ppc lHofZqVztK6KBckIj6FZ+RqyO8o8UEJkMtgEVi3yHGqcI77B4h7TAiP95u3E+kGGLNfP Qt/g== X-Gm-Message-State: AOJu0YwbmFHmaS0jHSbVq5scJsuMgipd31mR7Rug1KFqQ8l9UBGqB23n qwq1H5T1z32RqaR9sj4qHd0IopfmpSBhxOQXdKmPZwixoR1AJ6iD37h4khcDkPiT6Sorc7KLr2l GOFjMSL+3rtp1QagmvA== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:adf:fe08:0:b0:33b:51ae:1892 with SMTP id n8-20020adffe08000000b0033b51ae1892mr1610wrr.7.1707474135930; Fri, 09 Feb 2024 02:22:15 -0800 (PST) Date: Fri, 9 Feb 2024 10:21:58 +0000 In-Reply-To: <20240209102202.649049-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: <20240209102202.649049-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209102202.649049-3-vdonnefort@google.com> Subject: [PATCH v15 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: 1790416540542010219 X-GMAIL-MSGID: 1790416540542010219 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..bd23e78e8ec2 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,21 @@ static void rb_clear_buffer_page(struct buffer_page *page) page->read = 0; } +static void rb_update_meta_page(struct ring_buffer_per_cpu *cpu_buffer) +{ + struct trace_buffer_meta *meta = cpu_buffer->meta_page; + + WRITE_ONCE(meta->reader.read, cpu_buffer->reader_page->read); + WRITE_ONCE(meta->reader.id, cpu_buffer->reader_page->id); + WRITE_ONCE(meta->reader.lost_events, cpu_buffer->lost_events); + + WRITE_ONCE(meta->entries, local_read(&cpu_buffer->entries)); + WRITE_ONCE(meta->overrun, local_read(&cpu_buffer->overrun)); + WRITE_ONCE(meta->read, cpu_buffer->read); + + flush_dcache_folio(virt_to_folio(cpu_buffer->meta_page)); +} + static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) { @@ -5204,6 +5226,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 +5443,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 +5713,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 +5933,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 +6039,303 @@ 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); + + 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 10:21:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198815 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp760908dyd; Fri, 9 Feb 2024 02:39:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXKY6DnltRHOoAKAzS1P32blD1GqQeBlSD0pcjIKt61ABSWZ2i+8uc889IY6+57RK2w+qgiigXL8Ds+sH5SKtNADw7QMA== X-Google-Smtp-Source: AGHT+IG7hwux/JKDUKn9HTUS2Dv09Ic8N90QcvxcPHHrWNdIl4rcE1E5pR3RNj2lBhKAmSNILb57 X-Received: by 2002:a62:ea1a:0:b0:6e0:72a9:cf11 with SMTP id t26-20020a62ea1a000000b006e072a9cf11mr1290327pfh.18.1707475166267; Fri, 09 Feb 2024 02:39:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707475166; cv=pass; d=google.com; s=arc-20160816; b=I+JoJgwHHHcv+gMuVJZ1E6v/0xD2wfHeLwbsiRoLdUNm+0saEL9t2jY3ng/aLwdQ4V yBAvzwq78taZ8RwBHAy9fwtdxIwh+L9vK5o8ruFwVz/Xmlcatd7tvbFTEaJ2wZOMaj3L 9+tobd665pChkI5jQh1P1URDwhPNGJcjT6TnPm3+ifDwABxYEbGtK+y1/n5eyfUVl1lp amIn2ZmomzchCjArFCEw38Brc9dB2+S+qq6qwaNPz4b144F71rJNeyFSitSdA+qotueA nJQ4U4NIS7eFRjqgG5s6BtKWyVRMIwy0RI5X2jsNW/cy3VxyXsr7nvFy5E8ujB9WtzOE Z7hw== 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=HfNwPVyte3505NfbJDqS1AyUJKoH86NRIgvml8E5IfE=; b=a32TI2nEL7ox1pKeRJMvUx7DO1lrClFwrVyTR19BERT+iZXmVWWNJ4H9ydRFdwG4pr Tzsdpn6tK06YwdtLPhXsnlrCEf5y5+PihEND28GkojRRhIW4vUINO6WfY2DjJdhtsnoi AozbJZjw9WNzqMyrNmKIGrZWY5vtvt7m9ODeEYiFRqU//LHDLzdKBekvMp/thsvfeRx+ nMyJbRRI1fF0TkTzHF1uN80kBwXzsA80lkxV4ArlPGKR+1iuQPg6ZXpQUUHwguThoEHH 99skBc/JCVVQONO/QwHEfWT7aj/h/0fU6w8NJOw5tzHqm5jvZoSAwL1njXmD9X1oXFNH SX0g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qlVrbZaN; 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-59216-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59216-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCWmYNhPw4YlrhoydrCrVksB+bbFGM2pOjjSSSMh29MA15Z64ZtZfSjAA5XfnHvgVGUVutUq64xhuHCcX+RNk0NtwhORpQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id bq15-20020a056a02044f00b005cd88585706si1618050pgb.828.2024.02.09.02.39.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 02:39:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59216-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=qlVrbZaN; 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-59216-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59216-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 8E66CB23C9F for ; Fri, 9 Feb 2024 10:24:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 886156D1CB; Fri, 9 Feb 2024 10:22:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qlVrbZaN" Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E0AD69D37 for ; Fri, 9 Feb 2024 10:22:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707474142; cv=none; b=O3AuB/EaAEdDcLflP7/HhSzM9JX34NSnrpzAyyE5bU4j1V3ANGIIUUmeRY9lRj8zZbS+tSq0RFZIZEMe8W6MlhSJS2mO/nt12jKNfWphNMOlw8kRus3FwYj1OqQfRJPtvlVSYDjYbO+oKUQ78pX+zlUkJY0DvTOx/AVfNCHl/ok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707474142; c=relaxed/simple; bh=MApb/KU3APzxlFgZL+OJ5De3oK1aRzd7YLFeINSgGUA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GzjTLSCDhUIHEQ7xiiJBE48crOJZ9dSfcrYIh/CcUMeCbORIbMMuG24HCiaTzVtu7jPRD2iSjG7QJ62tVZAiKu4w8CboP7e40mLZXBFGadYIZ65jL7mXtLHA2nDNmQoNrBP1R0X3IAZlzpZQQD5+9NL248AZRQTqxMLWrM77nDg= 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=qlVrbZaN; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-33b26d4294dso267618f8f.0 for ; Fri, 09 Feb 2024 02:22:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707474138; x=1708078938; 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=qlVrbZaNchJC2mo5MdWmi/0qD09DVebP+OWYP9yT5z+OVkE+FFmpILE8fjnZhPWT8N UTLvwCNvuOF67sfMj58M1xjUn7sX3/HPuwyNsZsL1xgepuy8/0/vVUL0pXh2hECs6+ea MeLfhj2If8QZoQRs/DO22wZYFzwF3p6a1mrPvlLos4hMpLmr5Q7lNEBkpWvWWJMPWS4x uhSDN6hIp20ctqcTzJ7wX67rDlMAIwU8nG2iyjrm6+c/D3PhQ4uP82+IIN6/UaHJuxHu psJ+HTrTwiKH+9FZd6i080V80TSZHRPEsu0pKP6v4sU5jjEqntCd0NGPC6Qwx9rEYQi1 dSzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707474138; x=1708078938; 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=QCrps9vhz9+QuHJ/Bn6xie7fRvmrO5gLroRq2pYN4rYQZhH/ML9V9KyrmM9CLzFkbq e0AaI1/SujOoyIB9XEkYOvJawadRPjy4y9prlXYlmxhrcw5G6KDY8F1Ow+IydsATTewr pR4u50ZyBSN11W7pZuRpWxz12KK1hbsj2DebEqce8AdlacPidrMFyLZE+J20KdHw5/17 M3rom4CREHlXrl8rnsa14Kls0VD7L07DqQPUHdbpLAbGgYhrKWiogEOcZGCmcnwrnJNg 4qATSA5++K1IwAiLHyxCOQdk/cmtwR1OBUPaC6eeKyOAzhKJlY9KCrqD9Mh+7T3mQjdj g0FQ== X-Forwarded-Encrypted: i=1; AJvYcCVXm3/wgyzBIWV+hiou+59JDae/fW2vs7D5/nadxBxj2vsJNIK1NoOEdSvQt4qrikaJ2C8jW3YgJxt4WqG61oNn00bsvzd0gRbxbvve X-Gm-Message-State: AOJu0Yyyq/JQIDo7W7LF7nPuAXsI4hE8WtzC8NPnMdeCdJTQ4MGzw2BH +qhHbIcAYL5CND9G4VQlGVAPn4A1xc7RYNhqjus3KrSz914H/RgaWhW3aLe/15lMlRj1I4lW2X8 Ve6U9f0uHVkSFXQa7ng== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:6000:686:b0:33b:316a:10fa with SMTP id bo6-20020a056000068600b0033b316a10famr1719wrb.13.1707474138483; Fri, 09 Feb 2024 02:22:18 -0800 (PST) Date: Fri, 9 Feb 2024 10:21:59 +0000 In-Reply-To: <20240209102202.649049-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: <20240209102202.649049-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209102202.649049-4-vdonnefort@google.com> Subject: [PATCH v15 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: 1790417480338053492 X-GMAIL-MSGID: 1790417480338053492 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 10:22:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198805 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp754359dyd; Fri, 9 Feb 2024 02:24:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnDRgC8wraE+39eYxkuiL4BYKtceva5yaWyDHsKlVGFHBQbt43w7bgLXVsFlTmGAmXhF5G X-Received: by 2002:aa7:d350:0:b0:55e:aca4:aab2 with SMTP id m16-20020aa7d350000000b0055eaca4aab2mr955656edr.19.1707474290069; Fri, 09 Feb 2024 02:24:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707474290; cv=pass; d=google.com; s=arc-20160816; b=gxe/OVRZ0QDHr6PfXj6wv8bXDsfa5wDzThrIMsJMESaaE5/KKUyc1us2AT7wMyo72R qcZVm1ybxO6++fjBJUDPojZVaEOpx/ygQeliwdsJThR7jaP5U4p10P1IK06cWlpaEQA0 9cjmt8bf0MegNU7BIErOABW0Cva8SHHzgzJSbQIkCVHgZHBptg8j1Panmv6GyHgdqUvY ZzxN6pY9qhQkaGDDwwLZ9wssmHCSsB4Ofn5JWkW2KNousc7S3zVlS5smyEPFfnIlpc04 gsnxAx6fuaoWAsEdONXaspj2G0CP2pf2Ex7i+/JYUVfvq5xlLHZ//iHr6gVeGHR+xHqy taDg== 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=Xml0dOB0hcW/obr/TKIXnYC0UxjNQ6bf7aQdIxU3CxQ=; b=R/C2KdtusXsA9DdllHtV15s7pp/jLf057Ghx1Sw8ESzOel3Xjc0bo25LGW2I4CAct+ rPQpLqvWYg4lQjc1Q4Zu8k+aAzW4FNu77FW5jahTtjEmshKsPxgr1uMXfheOES/vQIqV oyRCtPsNDWqig4aL5GFd6WGAq+yDLPwiLWEJosKK/q0n2PoQ9RECBx/qxOAlgQ/CE8dY Fed6uBHBTP2IKerDTt8LJZ6iYBmmpI1Pgq7eefEz9LWK5snytDpLnKcV+D8oL+m1noQY ToAG0QlzGQyLWZSbz2qKdBDb5WXd2Vk8O8j72c7dqcDl1CYAbsI21mEVqE/BKJoHBQuE YWGQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="sZ/cbE+I"; 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-59217-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59217-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVtxe07Hmoc5b2nO9HojecZ0craJ0Jz/d4Nx1yPGRCtyY37atMC4NCBPeVkP7JsaIaCbjkqz9TxoAQtAHVJgZNz/vUPbA== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g26-20020a50ee1a000000b00560a8381394si731334eds.324.2024.02.09.02.24.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 02:24:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59217-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="sZ/cbE+I"; 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-59217-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59217-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 7F1DB1F22873 for ; Fri, 9 Feb 2024 10:24:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F44F6995C; Fri, 9 Feb 2024 10:22:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sZ/cbE+I" 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 095F76A017 for ; Fri, 9 Feb 2024 10:22:21 +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=1707474143; cv=none; b=ipj58UhoPfKe5ORcytbONXM2xC3fPGeOJIW4IMUMu8EPO1iai6VYvkgLayXg2P9423pyOCrv17/EM0B4TJLn5bLhF8ELbhegW1TXY89s5HpIIwH2IFOE1Rj2H0WRqSSSNEV2NK94CKLooXB8C3YJyVUwhuvnhPPRZWlH1gKweKk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707474143; c=relaxed/simple; bh=POwIaSakA3P7VN3DoSdTZ1HOoCQpnAoqCCz15gHTKd8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JoFislNaHQpSoSLHDBUSwJP0miPGyYMAYkUknIKBjVATrR0YO6JDchLs3IpyxoS35dNjTkE6VonVDXhW3eiLsaKOEJWMOgcWu6jw+ZX7Oerl8IsSE1nsThwNpuG58Uc59C5f5CATyoRxqLVrfrnVFmtsIlElv5y6509k+CT5ykE= 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=sZ/cbE+I; 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-60492362802so15361857b3.2 for ; Fri, 09 Feb 2024 02:22:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707474141; x=1708078941; 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=sZ/cbE+InTmDo/r0pfPufq96F7krecFo6JiAexu7KQA8Sh+2p6irCHw3lfk/RIp4We SHHcEU/ed5yI6bgRNfCG9Yl1HzIYwpaqXQyU/ZkzvgYmHstkGNbNHAT8/2tLMIF9L6L/ 1+BQHh1UgIL6GE6weicHflPebNfZHVFzAVnQSaowTIfiThX/RoDOrztXL9QQAsTP/Pwl 4nd5hqQz2kPayo3lJHkbaGscwtQDINCFQQSw+IwzleNVpfurGtyK8Wh1Q6gFoIb/mfdA b9preLA1CpiemB37dMBiF05ZQrvbyrlu/u5hkbSBoqoaXlO0/Ma6Ia3/FDEMnJxTE7HL ILig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707474141; x=1708078941; 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=cck3vOBfuvhzMc+YwKEdLEnPueAJX8HMuE/OoYPI3t4G6lsjG2fypnWFgnl1yqzSg8 i6DFyiajllqtoaPT/5OQSBo0RhyrLTpjEK1/+n9nWHIWCiVa0kJzyM0oIcSAVWUd/hJg eAcFeufB9zq8+RKjKQexpgowUs2mvkoaebyM3jLAKBRol2+J00Hhx9DVz427vO1VskXN U8zDlMNI9Hrz2nWYsOG3iFXNGgZeu6vZcwhkvNl1hOmUJFBq2O//8TDnRGD+uK2LqGzJ jU0OE28pmoLL2V2+fIHdZFtro+UPs9VMUF7suh6pPfe4YqawmyAXciDd9gzVpNlmDXv1 W94Q== X-Gm-Message-State: AOJu0Yy2GMG8xu/NffKDuKa/oX5+p22BDKxtVmblbLcMzIiNffxjc2fJ CoZvmBUamNyWiPtaB28G/1k1enH3aA4Tp4rNR+gdmkOXxhoUrrjLWhFMS5S1uk7R2pT+rJNAXZ4 p2o6iOJSP8DmdT/Y+tg== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a81:9ad8:0:b0:5f9:990d:78c0 with SMTP id r207-20020a819ad8000000b005f9990d78c0mr247901ywg.4.1707474141044; Fri, 09 Feb 2024 02:22:21 -0800 (PST) Date: Fri, 9 Feb 2024 10:22:00 +0000 In-Reply-To: <20240209102202.649049-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: <20240209102202.649049-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209102202.649049-5-vdonnefort@google.com> Subject: [PATCH v15 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: 1790416560988843050 X-GMAIL-MSGID: 1790416560988843050 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 10:22:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198806 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp754476dyd; Fri, 9 Feb 2024 02:25:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IGk9DqGv6SndGNLoORNzjEhVHZpS+jPqUlk5IAJQsUlRcalqI9AZblIWg67laROzDgXh3Xm X-Received: by 2002:a17:90a:bc92:b0:297:de5:75fd with SMTP id x18-20020a17090abc9200b002970de575fdmr571252pjr.20.1707474305978; Fri, 09 Feb 2024 02:25:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707474305; cv=pass; d=google.com; s=arc-20160816; b=geC3fDgImuRx9foRWhKFEmDuVG2UKudXTP5YVLoqRrS1q8LirV//7LOu5hRMbjjOLX lvbtdhhvzgCyh8dFQ1v1GVAUYEo14SyE1vypE9QPUeiA7zz3Q+6r9Ap5eHZZaBs/z6Nb fodPSTQBsuLcF6z12lZXrjck7psmcZBWawrGjEAoh4GaUfFLnc+OKrqJAL/jw1NAxioj uv32B8d7OAJJc4FL04w6EKeC/n8JEe5hi3ApAYRi0Nlqnl30RhC7K4dDRSwNU7lIHSoU K5UpZyE6IuRm2WhoVDf0mYwey6c/mBhwFYzUEKUuM6uGfmlUOj9JnDojjMoB5hWT81pj 0qSg== 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=pp2FaCem+sPL4PNlZf52/PQJvLGbMJdFMNkM1+5VUyY=; b=lO8PsvHeYMqcOBm8q8Gr+3oNscWnWBx9JE4JnU6TrVLCZR5g31fnqXoDb4qfuoWyrR YqeEuPchhjeZjvyFRxpc6c9t5Sv2w0s2nRjlMQGiKNSF5l3fnHyhEg4Tn7Lkl3OW3doz yxKc1dpNXEYt7li+OWMoUqTSM1mBD+Yfm8Htb0go2z455Q+EFAnrlkIMfEHIa8ZxErNX ml+eJqHYHiYofJWv/+Z0/VY45ekwVTT5O9sLpcrdIpX3ZBTVJvfs1PBk2HJbA5OM9Zii +yPVdPEAMnMoprYnVeZOnNqMru2eD626l1/NmxfzvOUCR8ScmqVqxfNsbpwSb8oZpEj6 c5WQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=tBWat87h; 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-59218-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59218-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVMwBTG81CIksPYA2myQf0Mjf05Y+jSlp7UkQHjS+v6aFaC+9yuwDuwv3hQH20jgdmQxYieBse/5P0Lldumh/Mh9GJRvg== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e13-20020a17090a9a8d00b0029704ea3479si1376047pjp.77.2024.02.09.02.25.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 02:25:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59218-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=tBWat87h; 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-59218-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59218-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 AB906281C2A for ; Fri, 9 Feb 2024 10:25:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 251806EB66; Fri, 9 Feb 2024 10:22:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tBWat87h" 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 53BF96BB4D for ; Fri, 9 Feb 2024 10:22:24 +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=1707474146; cv=none; b=KFZiDJUzoR3VaqmymVJv5hFO+8UIDh6rYg7VprcFC7SUF1eiP+tRztrik7Y5voTS85gGLLJkBS1N2S9dewWg3h7leYGroZ+O3j3dn4jSGi8cNz5cBpgHx29aj7ld6NJ1/BCzZeKBM+/knbFWkk2rmanAyColViUKGqFGlPj7sgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707474146; c=relaxed/simple; bh=iPiTM2h7tTVjI8Bap8UHkeVYN1GEEKgHI982louZIPs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eMXm0J2xMrGi4F16K/362HW85+p19/eEM9gHjC2t60bBP563dKpzLaQGhRNmC8Owvkm2G1YHRWuYrAP220Ay3dKdHso9VkoLAJADb1PHnbXiG4hm6REt3bnEWscmHDw47HDE/kyJloLvm0alXDb8zbv6oNzy8xNyf+QqXHpkYtM= 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=tBWat87h; 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-6047a047f4cso22451957b3.3 for ; Fri, 09 Feb 2024 02:22:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707474143; x=1708078943; 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=tBWat87hkNnH5xslKZXl0zV+dOOXPYB3JLZcZlutDQ2hCgQWBAjt5k6a8IvVL/PNmv 1tF5owEVNy0DVEVUFM9prOp5ZPWC2CYMdydy04bKfdg/rGSJEbnuaE+zXYAL/FkUjdSl C3LSNIFP9I+idO6O8bDG02JunEtfLCTtNKlE7bWS8aFgTMd45RjgWN4TLNHVoQ0j4QI4 fXAp9g75pCI4vgEH+9k6RF+zsPdZ0wEaqD4ZivrcaW9LqAMZvKw6eJUDgJd9Bwl0oQXg 5ii4feaV8B7gZ8VyrGJ2UqsNtJdAhuM9WBAB9KhzPnCPGGH6MlCcXqH2Pr0PX3HvHXZP iFag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707474143; x=1708078943; 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=CzZ5dKlGiFsCfQOpLKOPRWpQIY6OqO4Al41wO8/bXoQGe2ao5CQXJLyoJFL/aQQk2B mzWCKVQpNcA9Ll7UshhCdYfUyhbUQJVa4IX+g3UKlBrl3RP2m6s7DZInV+Ataj722Lo9 UPSME0HcZTqRZaGf4ySWSM7DBsLf8vpiiKMQOCwoO7qZ3G/AnrHadko9sPNy803U79ts Z+AOQYJ/gfZr1ZvXYwLaaON0U3iy/3FDbO+7md48Y7Bt0LZVBTWEouh2AqLSFdmQpQ8u ZOH9bcOjgNoCq1PoS2a5SO5o210dKnqZvJYcfOBpzZ8hJVR5kX9UxRjzjKTvSS5SMZYl g+aQ== X-Gm-Message-State: AOJu0YyrPA5v3tXunPJAtdYgPS1zqgLKPDcWzmx+bQLNQaf+UylgHv31 BlGczlbuL1JOXOHebzYwVFhy6EDdYbe8sYyYVsI2ykX0MhpA0Jtop3p2/uDiPMTVdiSQ/Z6+95h yaf5aIsMf4cSnMDmX7A== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:690c:3687:b0:604:9cad:8c36 with SMTP id fu7-20020a05690c368700b006049cad8c36mr275510ywb.4.1707474143306; Fri, 09 Feb 2024 02:22:23 -0800 (PST) Date: Fri, 9 Feb 2024 10:22:01 +0000 In-Reply-To: <20240209102202.649049-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: <20240209102202.649049-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209102202.649049-6-vdonnefort@google.com> Subject: [PATCH v15 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: 1790416577495262984 X-GMAIL-MSGID: 1790416577495262984 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 10:22:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 198807 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp754759dyd; Fri, 9 Feb 2024 02:25:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IHeDAz2fAgoMhnq6QQOgoXyn9/Ay2lAH9JCqnnqQWhMKle2tccFIejHl8shTjRgEGLa5Cke X-Received: by 2002:a05:620a:564f:b0:785:b99f:f2ad with SMTP id vw15-20020a05620a564f00b00785b99ff2admr551699qkn.73.1707474346203; Fri, 09 Feb 2024 02:25:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707474346; cv=pass; d=google.com; s=arc-20160816; b=SVeIH2JvmuN+teoLuuvjvM+GxIErcXprTQ0LKWlaBk8pWIzF6U57hpUZIxcWATsRHS yreAZp+qbKm/q/2KmbYrxNmbxnrbmToC8cgDCb1JcFmaW52cWqFfBdGeAmsxKgVy3jIW 7mufobY3Y6IcuGfIcntE5bEVowFpPjz58w0tBBLtDE+Pspyx4nMzT6tU9y0jTG4iesVE aZv1g3F/8qtYNlHV4Qzk7BmJke/mVmRP90dqQrYQ6q1bx6Yb9wkxX8lSWIsXEoJYNBtx 4Eeag/c4IFj7VrccOOX+6aDFQ/fGtXU+bwfUwkrfFPLKWfoRjcm51Ep07yrKlSunvt0K 9wRA== 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=xgzVow06w7KaUqYFsX3DaUGWdWZ8twIIl6BCnWQkk7k=; b=e/ICfREJfWExjXbMm1VCDZYgqqh7tfOT3mEbNsoU+fs8BsYDcMx1skOjgJzzt0tGnV vQM9B8GzQzs1pAwGH433XBLaTJn27bZNmmpOM4GCtii3txwL/pdmhfFyD3gbvE8Acgzm DmO6/vYmARjgi4e6DHE+CmD20PKfpgL053Kx8885D1ikwUc40G+A3zTQQ7e12Ji2b3bm 57bMsGpe9IYDtIHk9ivfZ3jUtFRfZRnOnm7aBblGykftcjmJbDLO8c6DP2vKMAALrzX8 v9FlIWoqUbmJDyFEDNmgoE5LwuDL1xsBg/znKAXjlicsQipMW83fpq2rZvFAHG+Bmy7P cMpw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zVHPn20F; 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-59219-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59219-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUhYTRvIUbZcSsNu6CNqky7GctAAAHLJFqwHqQufsEebojnRphcckj6pDjkVVdV38qRHfq7q992KlZc9E+f2EKi2rOpYg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id w25-20020ae9e519000000b00785b0808762si1673007qkf.678.2024.02.09.02.25.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 02:25:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59219-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zVHPn20F; 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-59219-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59219-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8A5C91C2405B for ; Fri, 9 Feb 2024 10:25:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD7426F094; Fri, 9 Feb 2024 10:22:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zVHPn20F" 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 3FCB06DD06 for ; Fri, 9 Feb 2024 10:22:27 +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=1707474149; cv=none; b=ACThv6JunKFJvFwcmBtH7ZsRTvELlZZ8vAIM3uT8UF1os69nNRmXAO5kueprQQfaK/O97UohFr/RHHlqQla+jgkuhA+Kykow1pOqHNp12uSLC8kcCORegfB2TJwoApYz7iUk62iSzMmL29BBSRD3e3PpUSA7d3SfLowSsNfM+xU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707474149; c=relaxed/simple; bh=yF4snXCigsdD2V4iu9JXl93VeJuVdieGXDlgLcIwa4g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=io9JXW3UN9mK+NTLBAfAodPlQQ59ADsKuV7eJAWBd4VVc9GteWTldEzw08m9KGxUJkkLtWWqDb1vTDFR3nTWGO8CQFIdZsXFmTFjvTmMGb+RWpd+RzQYOKmYGWPrDwHlyQ2NGxObQbua4kzDt12hRsQkGX4GTDl+6ofakKf5xAw= 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=zVHPn20F; 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-40fb505c97aso5263345e9.3 for ; Fri, 09 Feb 2024 02:22:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707474145; x=1708078945; 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=zVHPn20FoRetrupRW2+jFQlur/YtkrOh4Wrzc18viizOXoERBa1Ht5xcPe5u5WSqAo RT1EuLt1QfnR2RXyVDrJcQaPP8UYzXDphNQ45ziJnw5XexzX9xjh1mYK0l2lmLZiu0B+ F0mgQ00OJKr99t+GMHSDEczbat/PG2dPayZXfpu3kjRGMI2DR18Nj8eh15Wg9PetitlQ dMJk2qBSN3Sl00DLJOwf0vRQrG4dDdKayQjiWjD/DP6znOgM9lI69uJkhhJmIZc5DDE4 35Mh2hfsxLV5D3oNOWSBde5mbFezAxN+qScjR2SZ905irY11sCh9T7zJawBVCwHOBRFY dXyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707474145; x=1708078945; 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=o1968lqWeR2+vD7TdX2X9k/WHBODSHejnUppZ50KyWJr3qRSvIAK6nolApjkjQE18/ 9QAe9SFxZCDHwg55Lhzy3Ol4i1O1xWmQMiKHpcNSsXpz10ElSuiI/HL7AEmiOY/dRYie yuJnTU9RQZxQ8Ju9OB3hW/Lebul3g/Q1oQhcAGG8dhg6EgdLXU3XhSzwlSNSQ1zmvQah PX695T2IsDV3twaMnrTmpy3uI/a5a6fvvuMZ9WkBDgbXPKp7ZFZ7PrFck6o18el4C8I9 m9Ssp+0l495Qle+nxND/Yp/EVeQ3SCMpwWhTwMMmRld+2qeOSxb5cqq4d8avTw/OPa5r RIGg== X-Gm-Message-State: AOJu0YwedMjzmpFGlLkot0fzLtN/GatOaMeRYP0Is0VjIM+l+qTtCI+p gAtbqZ+qYXcfRrP9DGFCaHCJ16k5U5AvAl6r75dtcJjtvMvs9q3Wp1uvBbplf6Qs696UkE2WWmn cGDR5Z9YTM450diK9cQ== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a5d:522d:0:b0:33b:10a9:cfe3 with SMTP id i13-20020a5d522d000000b0033b10a9cfe3mr1978wra.11.1707474145648; Fri, 09 Feb 2024 02:22:25 -0800 (PST) Date: Fri, 9 Feb 2024 10:22:02 +0000 In-Reply-To: <20240209102202.649049-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: <20240209102202.649049-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240209102202.649049-7-vdonnefort@google.com> Subject: [PATCH v15 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: 1790416620312553310 X-GMAIL-MSGID: 1790416620312553310 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