Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2186281rda; Tue, 24 Oct 2023 15:28:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGti2+iTGTVLesKis1iWjV+b8i3NfantOGdGqEFCHTCCaKX7RiG9j69kK20bcOVOTfRXSK/ X-Received: by 2002:a05:6a00:2daa:b0:6be:4228:698b with SMTP id fb42-20020a056a002daa00b006be4228698bmr12024433pfb.20.1698186486963; Tue, 24 Oct 2023 15:28:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186486; cv=none; d=google.com; s=arc-20160816; b=MQQDQQLjdh0rxgiPYJoFMoq//GAJKOx1JB+ZdDVoc39ixDI1bkr4uCvU2pByv29A8P xhxfA/KXvlAanThka9cZ/juqwUel0zzWO1Qvtlw2m2T9Ib7x1XG+SPIcykR+5JygpZyb iJk2W7Ctv+PkOcUoAJu8jTlkErhZUiJuxWd6Hflo6IeWlWq+LEO9enSYK4XnDHm57V+W FjZNCufyQmoNuqqMpczT0+ruH8T5JhRzrN4wz+E/AV/H1Soe0wBjZX0vtG0us7yJuH7I gWQXnBfVWaOh7HcKsvlmkMfQSOCbPesm681vXza1ZToYTrcUMpo238zbIvkUJhnf/C8x jFjg== 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=XKH/5K4bvubvIr9pw7ZYc/Z4ET3hwQ4HW4s3RX+AQbw=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=ZF/DhFHp0IrKAxHMRDzYhSv0F1lck9sDoez+G7y0vLiCPg7kpxnbPzwL2vDUur13X2 rzMjpa9TPj5L0VciL2M143rFfFFP+1jduEfVmi3dDXaDSuUtucqf60YmTxBS1WNgALp4 b/JiyX1YvRDzxrSxHyJT06gvbaKye1QQy9p5lqxVR53IUVbyJCklSGK0SH4h3kaEZi9P rvHnybMeKmq+XG//lcVfrOB4FMo2M8EIPlsxVjWWbdz4ZNF7AH7iyE+p59uP/DkL163O UvM+0uX0/tSVrxXBUW3rfc1yt1jMlQnmcC7CvbrJ1nzh3BNaTgqkboiVB6/vFvN1IWVN 0vnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ti0BxZOq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id t12-20020a056a0021cc00b0068fee0e95c5si9025417pfj.89.2023.10.24.15.28.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ti0BxZOq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 21C438025432; Tue, 24 Oct 2023 15:28:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344525AbjJXW1i (ORCPT + 99 others); Tue, 24 Oct 2023 18:27:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344494AbjJXW1Y (ORCPT ); Tue, 24 Oct 2023 18:27:24 -0400 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 1DBFF1FDA for ; Tue, 24 Oct 2023 15:24:52 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a909b4e079so58021657b3.2 for ; Tue, 24 Oct 2023 15:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186291; x=1698791091; 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=XKH/5K4bvubvIr9pw7ZYc/Z4ET3hwQ4HW4s3RX+AQbw=; b=ti0BxZOqkKM1O8RMMAUyNmmZ2o8mbX3dMm8Vjr/jsi/SElnTLRGvvf7tm75ekFnwiP aTja6tBcUnRlTYdAa/U5yaTaroVUtJWEYClh9UtPf3AR59z5jFFreWEiQzusSQnVGRY3 S3OLHaoUGOB4ZHSNhCYtNRDALBRJ6eGzSS4AWWWDf0TvXoUOCWwbmgX7ZsTzq0+S74/B Ahn6YidRiKy/TTgwUS9p/Cnai3YnI5tyiTz7S6UBux/y8JM1d2ch5KkkZPaseNvy7xx0 ZeX0cs0pJ6lhq1L9FybhTefYg7fLVyVZG5gYtjm/N/lm4c306xGJDGGw6kvBGfSvO6uU Edbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186291; x=1698791091; 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=XKH/5K4bvubvIr9pw7ZYc/Z4ET3hwQ4HW4s3RX+AQbw=; b=GsSvgl859Tocmw8iUcGeeb563eKVSNDEryb6aP1gyEE3nLgkVCKuxjNVmxrzQNp6v6 L7QAV3cU0GzHRrxOJ2pHPPe8hJiOUcsrUaaqpr5ZcKQLQSjT2TpwWXyKK6h88jsTq+6y JQxjObktZFdD8YbzqXiZrkKGYRrFXa/o51elgRpolmsoN7u9GcR6tJ6tkRlr7FRTozoc oIy+uCJ+xMU12wVdtAahPm2CxFDx8BevKbbk3k6UTfGhkNOLbB+Ub/WVz+QS+oi1C+Zg J4NfCLv0yHX+sZ53ts0cHj0Z5CIzYZsgY2XGo9faBS4K/rT2GE1MaZgRmq2KNuUi6gSw Us/g== X-Gm-Message-State: AOJu0Yy3FvSPJuxcEktqqBxINR3ofjmaZAYPJImrMd3sCHrytW6VsB7D 6G5xb6JL+xaZ3pU0NkOCh5HflXAIbTZh X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:eb92:0:b0:59b:ca80:919a with SMTP id u140-20020a0deb92000000b0059bca80919amr270216ywe.0.1698186291218; Tue, 24 Oct 2023 15:24:51 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:20 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-18-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 17/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 , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org 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,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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:01 -0700 (PDT) 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 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c index 496812b5f1d2..4c35a689d1fc 100644 --- a/tools/lib/api/fs/fs.c +++ b/tools/lib/api/fs/fs.c @@ -447,15 +447,16 @@ 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; - ret = sysfs__read_str(entry, &buf, &size); - if (ret < 0) - return ret; + io.fd = open(entry, 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 +470,7 @@ int sysfs__read_bool(const char *entry, bool *value) default: ret = -1; } - - free(buf); + close(io.fd); return ret; } -- 2.42.0.758.gaed0368e0e-goog