Received: by 2002:ab2:4a89:0:b0:1f4:a8b6:6e69 with SMTP id w9csp314206lqj; Wed, 10 Apr 2024 11:09:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX2FDf7gfGG2o4wFDYg0mVe/6Z2rPPNi0xC2BS0SXNu57aF9hW8X4iupKuTyM2/vOMid30Q9g3XsDwu30oepZaiT5/ANRy4+jRTcnMlkA== X-Google-Smtp-Source: AGHT+IFUNMH5YtKnF3+ZHe++aka9bHOl1SI2PmxGjtzuWD9dPqIqGhrRqe44dD88VZrzBGAMw60a X-Received: by 2002:a05:6a00:1255:b0:6ed:416d:f7c with SMTP id u21-20020a056a00125500b006ed416d0f7cmr3938334pfi.6.1712772559895; Wed, 10 Apr 2024 11:09:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712772559; cv=pass; d=google.com; s=arc-20160816; b=cx825LNeyc7+SN06eBLpwVluCoPEozUmbXXycnrmp2fhRXvdg9CLGoEjuWKF52c6qp 6ruzJsioMF2dMsLiZLXOELQlhV7r4cF50L4Ts9V491AgxpgWHVj9+9KYr5d0KGDkc757 LZvYienXRaLv524w6/e315WYsZTRBT2iocqau0zV6bIRFM7mMt9pF/lmTiNzJcQKFzgs AJMFncZnV1cqDtGDwYamaEZZfsZkgaxN8pD3ngDflX1j4+wlEhX73OLkeMxRhw99dfA4 LitDv12CUJII3mYt4+x3KvZ/LegagKXyc8+UBMtej+HXHWbl5hsuxryopcLPkl8DbE2A kvvA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date; bh=u24wFyszMudyHp173GWtMa6jcpx2WZ4jk1e5aHCk0Jk=; fh=BENtMTih4A/Zav9J9Gr1Vz2W5Cv2lQsHZ1afEPeEncI=; b=rn2MjCTcc+5sWUHZQLIcURm9UvMKF3rF8BypCuZUD5EBZnsYcmHqqViHRhAAnlI2ut 7kD0kpKiUmsSyw0kxPx6yXRVQhVxXAZg94zR/gLl3Ajtq1lYRdYB8wZ5J7wcXLBvuQhK 2+BBXs7mPpKO8aZab3wuUvyAy/CnVhER1XmDKmQ9eOPoeRmIUmddMXMSIo8eYNY/prbD HKI3LCIbIZzjHoUx4xXic16N9bP0pG9qxpj+r27cgMCj3XUxrgF5PxvyUyg9owslqgGj 7Nuk78MxckldqiqJv2M04MoFcxZcJt0ecIInpubk45GqZqAzTt6nwp1NUX/CNJkE+Zgv enYQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-139116-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139116-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id gu14-20020a056a004e4e00b006ecf3964f66si11028016pfb.219.2024.04.10.11.09.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 11:09:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-139116-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-139116-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139116-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 83065B2265E for ; Wed, 10 Apr 2024 17:41:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7570D17F36E; Wed, 10 Apr 2024 17:41:17 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF62817994F; Wed, 10 Apr 2024 17:41:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712770877; cv=none; b=LOQvebVVnbeD8ZpHiTs0qzDnGfuI6QmuJm1Y2nK7Bz1eGQqYNb0Zcv2l34Jokrpn2p8u5ctqXfXMVHySaKEsk7VGylFd8w6tFaP+oBqgleFDslna3fGWeRXbHi6Jet9caM0KKDdNISeVMFpM7S3gx8EVnEsZ29kl2FLQh79y7mE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712770877; c=relaxed/simple; bh=0aKNApkH5cxJA6D2c8OOUEoVR19/Bs8/LdW506TRGlM=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s3MBThtk9OJdI9XDAPtA1+vmPDeEjjFT/QLJ88VCQKGdnvtaLCz5mdMhbYPhH5PNzWAUW2RchUZt1Q3R1ZVLr74nb1AemWdyi9XYgwUls/ow+wluUwCbgx/zLdaky45dv52vxy/MRmn73I8U28e7/UXWDh6Gzrp7LDscFLXr6mM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCB65C433F1; Wed, 10 Apr 2024 17:41:15 +0000 (UTC) Date: Wed, 10 Apr 2024 13:43:52 -0400 From: Steven Rostedt To: Vincent Donnefort Cc: mhiramat@kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mathieu.desnoyers@efficios.com, kernel-team@android.com, rdunlap@infradead.org, linux-mm@kvack.org Subject: Re: [PATCH v20 2/5] ring-buffer: Introducing ring-buffer mapping functions Message-ID: <20240410134352.66d35fbc@gandalf.local.home> In-Reply-To: <20240406173649.3210836-3-vdonnefort@google.com> References: <20240406173649.3210836-1-vdonnefort@google.com> <20240406173649.3210836-3-vdonnefort@google.com> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sat, 6 Apr 2024 18:36:46 +0100 Vincent Donnefort wrote: > +int ring_buffer_map(struct trace_buffer *buffer, int cpu, > + struct vm_area_struct *vma) > +{ > + 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) { > + err = __rb_map_vma(cpu_buffer, vma); > + if (!err) > + err = __rb_inc_dec_mapped(cpu_buffer, true); > + 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 = kcalloc(cpu_buffer->nr_pages + 1, sizeof(*subbuf_ids), 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); > + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); > + > + err = __rb_map_vma(cpu_buffer, vma); > + if (!err) { > + raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); > + cpu_buffer->mapped = 1; > + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); > + } else { > + kfree(cpu_buffer->subbuf_ids); > + cpu_buffer->subbuf_ids = NULL; > + rb_free_meta_page(cpu_buffer); > + } > +unlock: Nit: For all labels, please add a space before them. Otherwise, diffs will show "unlock" as the function and not "ring_buffer_map", making it harder to find where the change is. Same for the labels below. -- Steve > + 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) { > + __rb_inc_dec_mapped(cpu_buffer, false); > + 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); > + > + kfree(cpu_buffer->subbuf_ids); > + cpu_buffer->subbuf_ids = NULL; > + 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; > +} > + > +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: > + /* Some archs do not have data cache coherency between kernel and user-space */ > + flush_dcache_folio(virt_to_folio(cpu_buffer->reader_page->page)); > + > + rb_update_meta_page(cpu_buffer); > + > + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); > + rb_put_mapped_buffer(cpu_buffer); > + > + return 0; > +} > + > /* > * We only allocate new buffers, never free them if the CPU goes down. > * If we were to free the buffer, then the user would lose any trace that was in