Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3427306rdh; Mon, 27 Nov 2023 14:10:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwh1QEi3oC2Zh5V4qxATy97MNOYERQhIGz5qSpayViA/M9Qz9SpVSHLwhDcKv6g8jthpl8 X-Received: by 2002:a05:6a00:938f:b0:6cb:a18a:22b5 with SMTP id ka15-20020a056a00938f00b006cba18a22b5mr14926336pfb.2.1701123008853; Mon, 27 Nov 2023 14:10:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123008; cv=none; d=google.com; s=arc-20160816; b=DYyOAaT2byDrIEbsylFqcngLq7N920+ayTEaUifvPqJ/AuoUUg9QwL90TePtZ+eedK 5voqv7PRuCy3+UENu/yaDUhaWCvN6CfyJHwFLGESguB3l7pJZ5/mxxZ5pUmVMR8bISXA 9c+1aOk+5hyipcp7Nhn+wT/QrLbQH3AjY5tgCRM4SBpJwjSRLh6Z5WG3No+zLKroU5A8 lMAH4NVh03Zob6kMJzKPBmHcKukTj+83Fw83Wgsa8oX4Ib7/5J2++eby2sSZObSNrmzS I3PXdoHTftmy2x337vaTJ5nZkjA1paVqwH0fqkYY5NH4rxwAdSmPIvYp9VhX+8y8ShVm D+hA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=ujtQ3QhQpwDoNxv/mBiBRV+6eibZuimyXiHKFflebSQ=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=fxF0dBTWG/Tx6Wf9mHQAcmsbctwijAlJm0dsAwejjKRi9D/suD7ZcqxFlOXx4w3y7e CTIZ/lOvyheuL1zHIW2v/TExKRoMRAn7jNs4wENC1eeyscEzaL0sWh7FsgMuSCiOcgY/ +g8mlvOuTUpks2fkEe1BEc5rtVFyRWfgmAUMh1Bl8LCZG+pbDglLRY6yXll0BexeabXn zuEPAxcP+EDmgzhnkOQA5wRDL6OV6ANLcOL8N2zGzNDkbIWbXidkGXBKt1QwWs68pnn4 Z0G6k6zRPa3CqHp5Dp9Z9LtGdJW3+CTGu8FllCQ8wp89kjK0QvhQ9a3a2DH+bkELhcrB TbTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=COmBgx4L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id t7-20020a056a0021c700b006cb70cd9e4dsi10909396pfj.143.2023.11.27.14.10.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:10:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=COmBgx4L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 2754D80FFDB3; Mon, 27 Nov 2023 14:10:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233677AbjK0WJh (ORCPT + 99 others); Mon, 27 Nov 2023 17:09:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233570AbjK0WJ0 (ORCPT ); Mon, 27 Nov 2023 17:09:26 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F09FDD5A for ; Mon, 27 Nov 2023 14:09:32 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5c994880223so71177537b3.0 for ; Mon, 27 Nov 2023 14:09:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701122972; x=1701727772; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ujtQ3QhQpwDoNxv/mBiBRV+6eibZuimyXiHKFflebSQ=; b=COmBgx4L1//l84yqThO705JA0rnfhKDL7zGmyRiscQFiJ8m1D2SSwPk4sAaUUMvmTg 80yYZQ8UEmRQjs7qeURMKM5phtmMkOF8smhRPOaKSdtwjrmHXNuvWNyg1A/JcrpQUkHz BZZydtTZ+jmUEHm2/dNE3v2ovnfCD972cm2w+yQrrTM45qO1DlKSF5R8K/vUn7g094r0 5ZrgHetW1dEjIHlPjT9TxpKQPx65u1ebrAUpzsszB0gxzVdP2nA3Lva4sRZyd1stXWv9 UajzHr0cAB1lMbHqK63cs6NeCyjOJbryjQ35cioSjPuwu6tc1BBTzXOOC1H4051lteww mapQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701122972; x=1701727772; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ujtQ3QhQpwDoNxv/mBiBRV+6eibZuimyXiHKFflebSQ=; b=J0he0skL6xrhckmnpndv10p6luf+7adSKSIJTFJpRbxUJySjXARWT4+DkjizUuxdxo ag6WWPOktgtcJ8WBdXU4/b72xfLoDGzIhyWoOBn52PlJ+0C1qu8Uli3sB30EO1tU/GKI zFnqC4hWAd8UhNYVnmrBjlRTecFSwFRjXVkpsWUp6o+rA4n1tvBzyUbiqVYvZ7yt18fE p5reV0XK7ZV4uFLS03o80qSfsRq9qzlqYk2tICc+G/+WM2XZxFLpO1KZQiKOJOIlL1QX Egz/xu50Vv3ldbsJdmjlEicMPJyFtd/BmeFCSWS/4eMb2u6v0BrYr8frogJdCTjfJf+o bhZg== X-Gm-Message-State: AOJu0Yw4ts0TLYxXYgmTfilLRVND/Qu8TMQ630UfGuBRFp/MhCSqwM/W SUxNejY/htMlGbhWnKq1zFFtGngN2nMt X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a05:690c:3105:b0:5cc:4276:1c6b with SMTP id fb5-20020a05690c310500b005cc42761c6bmr362765ywb.4.1701122972182; Mon, 27 Nov 2023 14:09:32 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:17 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-6-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 05/50] tools api fs: Avoid reading whole file for a 1 byte bool From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:10:06 -0800 (PST) sysfs__read_bool used the first byte from a fully read file into a string. It then looked at the first byte's value. Avoid doing this and just read the first byte. Signed-off-by: Ian Rogers --- tools/lib/api/fs/fs.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c index 004f2af5504b..337fde770e45 100644 --- a/tools/lib/api/fs/fs.c +++ b/tools/lib/api/fs/fs.c @@ -447,15 +447,22 @@ int sysfs__read_str(const char *entry, char **buf, size_t *sizep) int sysfs__read_bool(const char *entry, bool *value) { - char *buf; - size_t size; - int ret; + struct io io; + char bf[16]; + int ret = 0; + char path[PATH_MAX]; + const char *sysfs = sysfs__mountpoint(); + + if (!sysfs) + return -1; - ret = sysfs__read_str(entry, &buf, &size); - if (ret < 0) - return ret; + snprintf(path, sizeof(path), "%s/%s", sysfs, entry); + io.fd = open(path, O_RDONLY); + if (io.fd < 0) + return -errno; - switch (buf[0]) { + io__init(&io, io.fd, bf, sizeof(bf)); + switch (io__get_char(&io)) { case '1': case 'y': case 'Y': @@ -469,8 +476,7 @@ int sysfs__read_bool(const char *entry, bool *value) default: ret = -1; } - - free(buf); + close(io.fd); return ret; } -- 2.43.0.rc1.413.gea7ed67945-goog