Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50570C64ED6 for ; Fri, 17 Feb 2023 16:43:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229714AbjBQQn4 (ORCPT ); Fri, 17 Feb 2023 11:43:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229508AbjBQQny (ORCPT ); Fri, 17 Feb 2023 11:43:54 -0500 Received: from msg-2.mailo.com (msg-2.mailo.com [213.182.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58A832680; Fri, 17 Feb 2023 08:43:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1676652183; bh=8n0Ms1VnNTk56gud+VEJPT9CfkFf8kiqgsa6gw0+rtE=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type; b=Bkg38ebMftcm82eR9eZeBe6BhxhLijv/1ZIr7t4Pqsoiv8MNV8sodCN0hOVeV2H7J uwGd76HIv49bv5OqeaeaWVxz9TwcPoq1pD1y0pE8HRQcbaE6BgBec7fbBibJhy2mJc onZPbecOW2EmpEyamP6yINnS3LvfT+axKdr8HMuo= Received: by b-4.in.mailobj.net [192.168.90.14] with ESMTP via ip-206.mailobj.net [213.182.55.206] Fri, 17 Feb 2023 17:43:03 +0100 (CET) X-EA-Auth: ZhcQgAhY3MDn0FswqxGe2X004sgsQugLQCWPlfZj4bf0PNQk7V3TyrkGBQyeqIBHOFoV8JxZOC9Qj49IoQuHdB+af3aU/MUX Date: Fri, 17 Feb 2023 22:12:57 +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] perf/x86/amd/core: Use sysfs_emit() in show() callback function Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Using sprintf/snprintf functions are error prone and suggested to be replaced by scnprintf/vscnrptintf as outlined in this [1] LWN article. A more recent recommendation is to use sysfs_emit() or sysfs_emit_at() as per Documentation/filesystems/sysfs.rst in show() callback function when formatting values to be returned to user-space. These helper functions are PAGE_SIZE aware and wrap a safer call to vscnprintf(). [1] https://lwn.net/Articles/69419/ Issue identified using the coccinelle device_attr_show.cocci script. Signed-off-by: Deepak R Varma --- arch/x86/events/amd/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c index 4386b10682ce..47a3a841332a 100644 --- a/arch/x86/events/amd/core.c +++ b/arch/x86/events/amd/core.c @@ -1272,7 +1272,7 @@ static ssize_t branches_show(struct device *cdev, struct device_attribute *attr, char *buf) { - return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu.lbr_nr); + return sysfs_emit(buf, "%d\n", x86_pmu.lbr_nr); } static DEVICE_ATTR_RO(branches); -- 2.34.1