From patchwork Sun Feb 26 17:30:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak R Varma X-Patchwork-Id: 61650 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2037195wrd; Sun, 26 Feb 2023 09:42:36 -0800 (PST) X-Google-Smtp-Source: AK7set9X3/EODecfw5IpRBvnK/STrf/gCWEyJc8LzR49oTmDlTeX1SFndKCV+9EgnOZ0RY7/uunN X-Received: by 2002:a17:906:d4:b0:8f5:14ab:94bc with SMTP id 20-20020a17090600d400b008f514ab94bcmr8866963eji.6.1677433356543; Sun, 26 Feb 2023 09:42:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677433356; cv=none; d=google.com; s=arc-20160816; b=vnorE8plUmMl74FhzN5+BOwEKYlUEv8mEcWXZYxDUSWuwhOj41USUe5M6G6gTJ/78+ BaisVtS7H+aJTuze4kXHoGqrKx0ak5J3zLyoO9WHFD0WwHnolspC6ypD79gisLa2cvOs PQx5dBKRt8ZsmAydawS0q684yjZ6R0e5nZgHqK6ztr6hgiSjxM4yLTr974LjZ1j0jaOu pzHP0lpcnJHvh0BgZHRhZH6kT+mCW5EID8tDJ89cSdT+R198RZYN5w1PtWfK2VQKYxnv O7o+ylQt/Weca3LGLT+H8pYya479FK34Sn67z93QGHFVi1a0ZBJ5Ayd43X3u+efgtxD2 FfyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=g1Y0B/xnEw9eslq3wIO04F8j8SFFZaVJ+wP2Sn8u9TI=; b=Zrq5IlQ4q4KXqVR3B2y4U0Q8pDFfW6ijbP8ZCHiaUY0OaRr3PT2+D/M0J0x//Vj3YD GngOcxl6iRdvB0n6tSrTuQjkXDtht6guBOq5+U40YOodGxeVJ0GJdKAmARim5DXq8MG7 F5isfZDTo9DhOW3XsaLFblwUVTrT8tW3LUq3IAG4kR7hcVKBsbj+WcXecO8lummQHJGs 7kyIi1V9by5K4ncjKTg03+aMYcaGUQgglTgLr8nMk0wou/aP+wQykQp2lz9tmikqdvLB N9uGxk7ACu6oWmHuu6Up3xcLUZgPYKIP3PnDmmheEUA+VT7Ik/xdeP2uX7a0/Uj7vFcY twFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=b9UBNZ3n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mailo.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k21-20020aa7c395000000b004ad79751752si6132815edq.645.2023.02.26.09.42.14; Sun, 26 Feb 2023 09:42:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=b9UBNZ3n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mailo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229781AbjBZRbE (ORCPT + 99 others); Sun, 26 Feb 2023 12:31:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbjBZRbC (ORCPT ); Sun, 26 Feb 2023 12:31:02 -0500 Received: from msg-4.mailo.com (msg-4.mailo.com [213.182.54.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EF339EDA; Sun, 26 Feb 2023 09:31:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1677432612; bh=kbszH9m85sjV1+wRhwLfMuDJ81RvA1n+6wdLTFkDGHo=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:In-Reply-To; b=b9UBNZ3njp8Q3JxreIHTwITHrQVDR5FqkhQQgBQI5MIM+qV1/s6YGDpcEcG1dY4FC +caKAGNEmnVgLOp1dOsLWwolhIIQlWDcmTsRLf5Jsn/amajpVnXTw6Q94/sZlWQ3or jZ4VmXylgjA8dicOsY3IIx4aevc2twnZv+UvBApo= Received: by b-4.in.mailobj.net [192.168.90.14] with ESMTP via ip-206.mailobj.net [213.182.55.206] Sun, 26 Feb 2023 18:30:12 +0100 (CET) X-EA-Auth: XhjWn0297xPJSHS7FHXDD0OmaaUOkOqIx9ectiF4tFqsu0nxWKVrOT/JDOqNFYoDmelxxAbqRdV2V6eLAfm1h82mL63nuXtZ Date: Sun, 26 Feb 2023 23:00:06 +0530 From: Deepak R Varma To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Saurabh Singh Sengar , Praveen Kumar , Deepak R Varma Subject: [PATCH v3 1/3] perf/x86/core: Use sysfs_emit() in show() callback function Message-ID: <4fa04945ae4b6ee35943da25c6b75dbcaeaf6af0.1677431992.git.drv@mailo.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758916359246634924?= X-GMAIL-MSGID: =?utf-8?q?1758916359246634924?= As per C99 standard, snprintf() returns the number of bytes that would be encoded in the destination buffer when it is sufficiently large. This return value may be different from what the caller is expecting and hence may lead to potential errors in the program. Kernel release 2.6.2 introduced scnprintf() & vscnprintf() which precisely return the actual bytes encoded into the destination buffer. For the sysfs attribute show() callback functions, which returns the number of bytes to the user space, a more recent recommendation is to use sysfs_emit() or sysfs_emit_at() instead of sprintf() family of functions. This is recorded in the Documentation/filesystems/sysfs.rst Kernel documentation file. Issue identified using the coccinelle device_attr_show.cocci script. Signed-off-by: Deepak R Varma --- Changes in v3: - Remove incorrect/erroneous change proposed. Feedback provided by Peter Zijlstra Changes in v2: - Revise patch log message to include details on the potential issues with current implementation and how the proposal is a better solution. Feedback provided by Peter Zijlstra arch/x86/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index d096b04bf80e..87a7f0cd77fd 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -2544,7 +2544,7 @@ static ssize_t get_attr_rdpmc(struct device *cdev, struct device_attribute *attr, char *buf) { - return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc); + return sysfs_emit(buf, "%d\n", x86_pmu.attr_rdpmc); } static ssize_t set_attr_rdpmc(struct device *cdev, @@ -2602,7 +2602,7 @@ static ssize_t max_precise_show(struct device *cdev, struct device_attribute *attr, char *buf) { - return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu_max_precise()); + return sysfs_emit(buf, "%d\n", x86_pmu_max_precise()); } static DEVICE_ATTR_RO(max_precise);