Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp713936pxj; Fri, 14 May 2021 13:57:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDRLGqMTZfjcSXjqTNmpZn7HMGdSytxin2JQMi1Q+kHTXV8mHbFo38dMcuRVCeQWB0CKdU X-Received: by 2002:a05:6e02:1ca1:: with SMTP id x1mr40594471ill.86.1621025822561; Fri, 14 May 2021 13:57:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621025822; cv=none; d=google.com; s=arc-20160816; b=BEwz0WM0TkYiO67K4kWseiNv79qpbRrmIzOdYwwxA4bDd+ZBhnGztMRDx2IPluctKW jOVh9X2VKGDWqCdMfaRHvmmUrK8EWYu9tCJRW2TILAS20lgqoDMF5Q5NKDqq3XR611In AqyiGwAl/DiiAd2Ug02yNaUKWDSSUCddQxkYSlbrY7wpUOYMTIzdzJhCHmhOzlem2M+o jbC39HoXzKsNzte0FYosT90xZe6+XwoROxXTcjHqAjq5dcTZ8RjHpYde/w0HVoDEJqsQ 9hDrFn2F7ta3JPI4lwoOzxEhTE8UwKcChZJfm5S7fZrdrSNaMsuh+2omWhUSC6Rb5BVl RJOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sF8dRlNIQCEusEjjgo7vMe+xZ31mX4hk0pzL4HeXzns=; b=E9cJJXu3M86F2SJ/ubJteHZq99WY4/8tn6yYPGvd2uL45MYsMFJukRyxdwbiVL9qT7 W0PRwa6iF7d1/hxAMESCL9vUsab+sKTK606+YTFT7+/WGJ6dNpTT9MNK0YUg6fmJGXwV MSjzI1k2bNfTKj7qrdu6NALPqlTvE1lov5YTwl3CmQfVPcAbVNcz1tYRhLCF3mCw5DJL tdiHtcPlK4PDws/iutvcZLtwjIOomVm/8iWFoafu7qbuCMdKK1IgSo0TwdS9xZ6ziiVX dS7Uxt7SHEsoJEWTIebS8TzHn/0iQr5Ne2fcJQeVYKxBs0wCXrGFPeSqsNEQzVfO3CHm 3GMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="bkpth4/V"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i17si1338011jav.59.2021.05.14.13.56.49; Fri, 14 May 2021 13:57:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="bkpth4/V"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232298AbhENUxU (ORCPT + 99 others); Fri, 14 May 2021 16:53:20 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:59217 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232190AbhENUxS (ORCPT ); Fri, 14 May 2021 16:53:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621025526; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sF8dRlNIQCEusEjjgo7vMe+xZ31mX4hk0pzL4HeXzns=; b=bkpth4/VGIhjjs4COw0Rryg5DyHKYLCelsfYSPOuls/FL9zpgU4aD1DKtuJZfDu32yhr63 5ZFbVOde1CfKskaqE2AyezgqZ99T0XsSuP8GUfzdCuKEMDkSh135KDM+9/VEWBewjV0bVm GVg/wBMuo0iOL7yKD+Bqa3vK5GR3gvU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-418-AgsDE_ayMWWkQ7GgdjxHOQ-1; Fri, 14 May 2021 16:52:04 -0400 X-MC-Unique: AgsDE_ayMWWkQ7GgdjxHOQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2E3A81007476; Fri, 14 May 2021 20:52:03 +0000 (UTC) Received: from x1.bristot.me.homenet.telecomitalia.it (ovpn-113-210.rdu2.redhat.com [10.10.113.210]) by smtp.corp.redhat.com (Postfix) with ESMTP id A54A217A63; Fri, 14 May 2021 20:51:59 +0000 (UTC) From: Daniel Bristot de Oliveira To: linux-kernel@vger.kernel.org Cc: Phil Auld , Sebastian Andrzej Siewior , Kate Carcia , Daniel Bristot de Oliveira , Jonathan Corbet , Steven Rostedt , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexandre Chartre , Clark Willaims , John Kacur , Juri Lelli , linux-doc@vger.kernel.org Subject: [PATCH V3 5/9] tracing/trace: Add a generic function to read/write u64 values from tracefs Date: Fri, 14 May 2021 22:51:14 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Provides a generic read and write implementation to save/read u64 values from a file on tracefs. The trace_ull_config structure defines where to read/write the value, the min and the max acceptable values, and a lock to protect the write. Cc: Jonathan Corbet Cc: Steven Rostedt Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Alexandre Chartre Cc: Clark Willaims Cc: John Kacur Cc: Juri Lelli Cc: linux-doc@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Daniel Bristot de Oliveira --- kernel/trace/trace.c | 87 ++++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace.h | 19 ++++++++++ 2 files changed, 106 insertions(+) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 560e4c8d3825..b4cd89010813 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -7516,6 +7516,93 @@ static const struct file_operations snapshot_raw_fops = { #endif /* CONFIG_TRACER_SNAPSHOT */ +/* + * trace_ull_config_write - Generic write function to save u64 value + * @filp: The active open file structure + * @ubuf: The userspace provided buffer to read value into + * @cnt: The maximum number of bytes to read + * @ppos: The current "file" position + * + * This function provides a generic write implementation to save u64 values + * from a file on tracefs. The filp->private_data must point to a + * trace_ull_config structure that defines where to write the value, the + * min and the max acceptable values, and a lock to protect the write. + */ +static ssize_t +trace_ull_config_write(struct file *filp, const char __user *ubuf, size_t cnt, + loff_t *ppos) +{ + struct trace_ull_config *config = filp->private_data; + u64 val; + int err; + + if (!config) + return -EFAULT; + + err = kstrtoull_from_user(ubuf, cnt, 10, &val); + if (err) + return err; + + if (config->lock) + mutex_lock(config->lock); + + if (config->min && val < *config->min) + err = -EINVAL; + + if (config->max && val > *config->max) + err = -EINVAL; + + if (!err) + *config->val = val; + + if (config->lock) + mutex_unlock(config->lock); + + if (err) + return err; + + return cnt; +} + +/* + * trace_ull_config_read - Generic write function to read u64 value via tracefs + * @filp: The active open file structure + * @ubuf: The userspace provided buffer to read value into + * @cnt: The maximum number of bytes to read + * @ppos: The current "file" position + * + * This function provides a generic read implementation to read a u64 value + * from a file on tracefs. The filp->private_data must point to a + * trace_ull_config structure with valid data. + */ +static ssize_t +trace_ull_config_read(struct file *filp, char __user *ubuf, size_t cnt, + loff_t *ppos) +{ + struct trace_ull_config *config = filp->private_data; + char buf[ULL_STR_SIZE]; + u64 val; + int len; + + if (!config) + return -EFAULT; + + val = *config->val; + + if (cnt > sizeof(buf)) + cnt = sizeof(buf); + + len = snprintf(buf, sizeof(buf), "%llu\n", val); + + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); +} + +const struct file_operations trace_ull_config_fops = { + .open = tracing_open_generic, + .read = trace_ull_config_read, + .write = trace_ull_config_write, +}; + #define TRACING_LOG_ERRS_MAX 8 #define TRACING_LOG_LOC_MAX 128 diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index cd80d046c7a5..44fa25c1264a 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -1952,4 +1952,23 @@ static inline bool is_good_name(const char *name) return true; } +/* + * This is a generic way to read and write a u64 config value from a file + * in tracefs. + * + * The value is stored on the variable pointed by *val. The value needs + * to be at least *min and at most *max. The write is protected by an + * existing *lock. + */ +struct trace_ull_config { + struct mutex *lock; + u64 *val; + u64 *min; + u64 *max; +}; + +#define ULL_STR_SIZE 22 /* 20 digits max */ + +extern const struct file_operations trace_ull_config_fops; + #endif /* _LINUX_KERNEL_TRACE_H */ -- 2.26.3