From patchwork Fri Feb 17 16:42:57 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: 58711 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp992805wrn; Fri, 17 Feb 2023 08:50:31 -0800 (PST) X-Google-Smtp-Source: AK7set9N6wxwfz5f4nAEJEAQ4IOXcx+UoteCwjbUIv0sV+Cbzz5Otek3BZl5qI02rt37cwtCTMZF X-Received: by 2002:a17:902:b610:b0:19a:b302:5176 with SMTP id b16-20020a170902b61000b0019ab3025176mr1491411pls.46.1676652631218; Fri, 17 Feb 2023 08:50:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676652631; cv=none; d=google.com; s=arc-20160816; b=B9tGi3Ju643MFU2cMaNJ3sdzF/Qhr9KWFtG/yquVyapnSWB5N+KDk4iZ9gcRtGpe6w 4/g7V0zg4Sl2mUlg3iBaEN8rujUOc8iF6icJHANv+TYxv+8LHopHO8Xnd7NyeIqiaG1Z IdNpKksASaok6iUsxBkWDhFGYAvfBmZKZhXgbamLBIWKJR0aarjdx46/I8ujVBAtvB0c XJPFuLuwYaNDMOplxPP2P9ucjnaGPZHrONJnwL2oFBV5YL5hS1yZJgGSm3ubYOpiFM2y JQmHAH/mLU/3GNBamIBCla8Dz4iXMU2WgqXxseeD9y/qlXouh+WY5+QeG5IFDl4qiOxW Qogg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=mhxwIafYBAkWRwUQXnopvsazox+e+UnmAiLL2GU6Nx0=; b=dQ0L9l30J1bMR2BsV8GW7AejWhRSyAsTh49h6Iuh/emAxgN3R8PbFNZvkoM4T3/eOO bbszAzkV4+a0R5Pnz3hh5K2ob/KU4x4dyELb7JliVSyRbneDYBCooHF6FI+v6KKUqf53 dpeRF4uh2ABMFZiziPZnh3mQpsv4Bzo0LFRa2sLC1MlcV7r6l6OtpNoZFh5Q65AN379e 2Os7/nrscMQa3N/uruMN0Q7ojnspN4uFeQzfkEuFQOTcnUhmpz88GMb6u8iaqMnCrpQy 7k63QfIjd6t6n0DlVkJQOzOVFD5orETgb5zVmO/4+OHo5ZpncX2TXHvx+MNn/0nUqL17 17og== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=Bkg38ebM; 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 e4-20020a17090301c400b0019abec982d2si294010plh.22.2023.02.17.08.50.18; Fri, 17 Feb 2023 08:50:31 -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=Bkg38ebM; 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 S229787AbjBQQnz (ORCPT + 99 others); Fri, 17 Feb 2023 11:43:55 -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-Disposition: inline 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?1758097709476986032?= X-GMAIL-MSGID: =?utf-8?q?1758097709476986032?= 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);