public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dirk Gouders <dirk@gouders.net>
To: David Laight <david.laight.linux@gmail.com>
Cc: "Ian Rogers" <irogers@google.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Arnaldo Carvalho de Melo" <acme@kernel.org>,
	"Namhyung Kim" <namhyung@kernel.org>,
	"Mark Rutland" <mark.rutland@arm.com>,
	"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Adrian Hunter" <adrian.hunter@intel.com>,
	"Kan Liang" <kan.liang@linux.intel.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Darren Hart" <dvhart@infradead.org>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"André Almeida" <andrealmeid@igalia.com>,
	"John Garry" <john.g.garry@oracle.com>,
	"Will Deacon" <will@kernel.org>,
	"James Clark" <james.clark@linaro.org>,
	"Mike Leach" <mike.leach@linaro.org>,
	"Leo Yan" <leo.yan@linux.dev>,
	"Yicong Yang" <yangyicong@hisilicon.com>,
	"Jonathan Cameron" <jonathan.cameron@huawei.com>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Bill Wendling" <morbo@google.com>,
	"Justin Stitt" <justinstitt@google.com>,
	"Josh Poimboeuf" <jpoimboe@kernel.org>,
	"Al Viro" <viro@zeniv.linux.org.uk>,
	"Kyle Meyer" <kyle.meyer@hpe.com>,
	"Ben Gainey" <ben.gainey@arm.com>,
	"Athira Rajeev" <atrajeev@linux.vnet.ibm.com>,
	"Kajol Jain" <kjain@linux.ibm.com>,
	"Aditya Gupta" <adityag@linux.ibm.com>,
	"Eder Zulian" <ezulian@redhat.com>,
	"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
	"Kuan-Wei Chiu" <visitorckw@gmail.com>,
	"He Zhe" <zhe.he@windriver.com>,
	"Brian Geffon" <bgeffon@google.com>,
	"Ravi Bangoria" <ravi.bangoria@amd.com>,
	"Howard Chu" <howardchu95@gmail.com>,
	"Charlie Jenkins" <charlie@rivosinc.com>,
	"Colin Ian King" <colin.i.king@gmail.com>,
	"Dominique Martinet" <asmadeus@codewreck.org>,
	"Jann Horn" <jannh@google.com>,
	"Masahiro Yamada" <masahiroy@kernel.org>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Yang Jihong" <yangjihong@bytedance.com>,
	"Dmitry Vyukov" <dvyukov@google.com>,
	"Andi Kleen" <ak@linux.intel.com>,
	"Graham Woodward" <graham.woodward@arm.com>,
	"Ilkka Koskinen" <ilkka@os.amperecomputing.com>,
	"Anshuman Khandual" <anshuman.khandual@arm.com>,
	"Zhongqiu Han" <quic_zhonhan@quicinc.com>,
	"Hao Ge" <gehao@kylinos.cn>,
	"Tengda Wu" <wutengda@huaweicloud.com>,
	"Gabriele Monaco" <gmonaco@redhat.com>,
	"Chun-Tse Shao" <ctshao@google.com>,
	"Casey Chen" <cachen@purestorage.com>,
	"Dr. David Alan Gilbert" <linux@treblig.org>,
	"Li Huafei" <lihuafei1@huawei.com>,
	"Steinar H. Gunderson" <sesse@google.com>,
	"Levi Yun" <yeoreum.yun@arm.com>,
	"Weilin Wang" <weilin.wang@intel.com>,
	"Thomas Falcon" <thomas.falcon@intel.com>,
	"Thomas Richter" <tmricht@linux.ibm.com>,
	"Andrew Kreimer" <algonell@gmail.com>,
	"Krzysztof Łopatowski" <krzysztof.m.lopatowski@gmail.com>,
	"Christophe Leroy" <christophe.leroy@csgroup.eu>,
	"Jean-Philippe Romain" <jean-philippe.romain@foss.st.com>,
	"Junhao He" <hejunhao3@huawei.com>,
	"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
	"Xu Yang" <xu.yang_2@nxp.com>,
	"Steve Clevenger" <scclevenger@os.amperecomputing.com>,
	"Zixian Cai" <fzczx123@gmail.com>,
	"Stephen Brennan" <stephen.s.brennan@oracle.com>,
	"Yujie Liu" <yujie.liu@intel.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org, llvm@lists.linux.dev
Subject: Re: [PATCH v2 04/47] perf bench: Silence -Wshorten-64-to-32 warnings
Date: Sat, 03 May 2025 14:22:45 +0200	[thread overview]
Message-ID: <gh8qndc296.fsf@gouders.net> (raw)
In-Reply-To: <20250503110538.0d264e4a@pumpkin> (David Laight's message of "Sat, 3 May 2025 11:05:38 +0100")

David Laight <david.laight.linux@gmail.com> writes:

> On Fri, 02 May 2025 16:12:17 +0200
> Dirk Gouders <dirk@gouders.net> wrote:
>
>> David Laight <david.laight.linux@gmail.com> writes:
>> 
>> > On Thu, 01 May 2025 01:11:16 +0200
>> > Dirk Gouders <dirk@gouders.net> wrote:
>> >  
>> >> Ian Rogers <irogers@google.com> writes:
>> >>   
>> >> > On Wed, Apr 30, 2025 at 3:19 PM Dirk Gouders <dirk@gouders.net> wrote:    
>> >> >>
>> >> >> Ian Rogers <irogers@google.com> writes:
>> >> >>    
>> >> >> > On Wed, Apr 30, 2025 at 1:23 PM Dirk Gouders <dirk@gouders.net> wrote:    
>> >> >> >>
>> >> >> >> Hi Ian,
>> >> >> >>
>> >> >> >> considering so many eyes looking at this, I am probably wrong.
>> >> >> >>
>> >> >> >> So, this is only a "gauge reply" to see if it's worth I really read
>> >> >> >> through all the commits ;-)
>> >> >> >>
>> >> >> >> Ian Rogers <irogers@google.com> writes:
>> >> >> >>
>> >> >> >> [SNIP]
>> >> >> >>    
>> >> >> >> > diff --git a/tools/perf/bench/sched-pipe.c b/tools/perf/bench/sched-pipe.c
>> >> >> >> > index 70139036d68f..b847213fd616 100644
>> >> >> >> > --- a/tools/perf/bench/sched-pipe.c
>> >> >> >> > +++ b/tools/perf/bench/sched-pipe.c
>> >> >> >> > @@ -102,7 +102,8 @@ static const char * const bench_sched_pipe_usage[] = {
>> >> >> >> >  static int enter_cgroup(int nr)
>> >> >> >> >  {
>> >> >> >> >       char buf[32];
>> >> >> >> > -     int fd, len, ret;
>> >> >> >> > +     int fd;
>> >> >> >> > +     ssize_t ret, len;
>> >> >> >> >       int saved_errno;
>> >> >> >> >       struct cgroup *cgrp;
>> >> >> >> >       pid_t pid;
>> >> >> >> > @@ -118,7 +119,7 @@ static int enter_cgroup(int nr)
>> >> >> >> >       cgrp = cgrps[nr];
>> >> >> >> >
>> >> >> >> >       if (threaded)
>> >> >> >> > -             pid = syscall(__NR_gettid);
>> >> >> >> > +             pid = (pid_t)syscall(__NR_gettid);
>> >> >> >> >       else
>> >> >> >> >               pid = getpid();
>> >> >> >> >
>> >> >> >> > @@ -172,23 +173,25 @@ static void exit_cgroup(int nr)
>> >> >> >> >
>> >> >> >> >  static inline int read_pipe(struct thread_data *td)
>> >> >> >> >  {
>> >> >> >> > -     int ret, m;
>> >> >> >> > +     ssize_t ret;
>> >> >> >> > +     int m;
>> >> >> >> >  retry:
>> >> >> >> >       if (nonblocking) {
>> >> >> >> >               ret = epoll_wait(td->epoll_fd, &td->epoll_ev, 1, -1);    
>> >> >> >>
>> >> >> >> The epoll_wait(), I know of, returns an int and not ssize_t.
>> >> >> >>
>> >> >> >> That shouldn't show up, because it doesn't cause real problems...    
>> >> >> >
>> >> >> > So the function is read_pipe so it should probably return a ssize_t. I
>> >> >> > stopped short of that but made ret a ssize_t to silence the truncation
>> >> >> > warning on the read call. Assigning smaller to bigger is of course not
>> >> >> > an issue for epoll_wait.    
>> >> >>
>> >> >> Oh yes, I missed that ret is also used for the result of read().
>> >> >>
>> >> >> Some lines down there is also a combination of
>> >> >>
>> >> >> ret = enter_cgroup() (which is int)
>> >> >>
>> >> >> and
>> >> >>
>> >> >> ret = write()
>> >> >>
>> >> >>
>> >> >> Just confusing but yes, because ret is also used for read() and write()
>> >> >> in those cases it should be ssize_t.
>> >> >>
>> >> >> I'm sorry for the noise.    
>> >> >
>> >> > No worries, I'm appreciative of the eyes. I suspect we'll only pick up
>> >> > the first patches in this series to fix what is a bug on ARM. I think
>> >> > I'm responsible for too much noise here ;-)    
>> >> 
>> >> A final thought (in case this patch will also be picked):
>> >> 
>> >> Why not, in case of read_pipe() and worker_thread() just cast
>> >> read() and write() to int?  Both get counts of sizeof(int) and
>> >> it would clearly show: we know the result fits into an int.  
>> >
>> > This is an obvious case of the entire insanity of these changes.  
>> 
>> You mean, because there is still the -1 case where the sign-lost can
>> happen?
>> 
>> I guess your reply is in combination with your replies to another thread
>> to this subject.  As far as I understood, Ian also has problems with
>> full understanding and I wonder if it helps to talk about a real
>> example.  As far as I understood you say that code like this
>> (from tools/perf/bench/sched-pipe.c) is simply wrong:
>> 
>> static inline int read_pipe(struct thread_data *td)
>> {
>> 	int ret, m;
>> retry:
>> 	if (nonblocking) {
>> 		ret = epoll_wait(td->epoll_fd, &td->epoll_ev, 1, -1);
>> 		if (ret < 0)
>> 			return ret;
>> 	}
>> 	ret = read(td->pipe_read, &m, sizeof(int));
>> 	if (nonblocking && ret < 0 && errno == EWOULDBLOCK)
>> 		goto retry;
>> 	return ret;
>> }
>> 
>> And from your reply I understand that casting the read() explicitely to
>> int is insane.  And now, I wonder what you would suggest -- honestly, I
>> am expecting to learn something, here.

First, thank you for elaborating on this.  As I expected, I indeed learn
more than one thing.

> If you look through pretty much all 'posix' userspace code the return
> value from 'read' is assigned to an 'int' variable.

I looked at some read()s in util-linux and all those that I looked at
use ssize_t.  Two reads, I found in bash use int.  In mpich, both
versions are used...  I didn't see a single cast, though ;-)

> If the compiler is going to complain that the return value doesn't fit
> into a 32bit int, it better have a pretty good idea the return value
> might exceed 2^^32.
> That requires knowledge of what 'read' does and analysis of the domain
> (not just type) of the length passed to read.
> Now if you add an (int) cast, you won't get an error (on 32bit) if
> the value is a pointer - and that is an error you always want.

You mean something like:

char *ptr = (int)read(fd, buf, sizeof(buf));

Here in my environment, I'd get an error:

error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]

(I have no 32bit system to test it but from my memory, I'd say I know
 such error from times when 64bit wasn't available...)

> I'm pretty sure that it is also true the linux limits read (and write)
> to INT_MAX - so, for linux, the return value from read() always fits
> int 'int'.

Yes, didn't know that.  From read(2):

------------------------------------------------------------------------
NOTES
       On Linux, read() (and similar system calls) will transfer at most
       0x7ffff000 (2,147,479,552) bytes, returning the number of bytes
       actually transferred.  (This is true on both 32-bit and 64-bit systems.)
------------------------------------------------------------------------

Oh well, I myself took `ssize_t read()' always so serious that I gave my
best to always try to match that type...

> The underlying problem is that if you start adding unnecessary casts for
> integer type conversions you end up with so many casts that it is far too
> easy for a 'broken' one to slip into the code.

OK, in the other thread, you also said that, in your opinion, (just
integer?) casts should be kept to an absolute minimum and I wonder, what
would be an example for such (mandatory) cases.  Just the ones where the
compiler would complain (except for -Wshorten-64-to-32)?

> If you scan the kernel for min_t() there are plenty of very dubious ones.
> They've been added to 'fix' a compile time warning, but there are plenty
> that cast to u8, u16 or long (where there are u64 lurking).
> One of the u16 ones I found was a real bug and found/fixed separately
> from my scans of all the min_t().

Sorry for me still failing to fully understand: do your concerns then
mean you'd vote for not enabling -Wshorten-64-to-32 and live with the
perhaps rare cases of problems like the one in

https://lore.kernel.org/lkml/20250331172759.115604-1-leo.yan@arm.com/

or identify them by other means?

Best regards,

Dirk

  reply	other threads:[~2025-05-03 12:25 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-30 17:49 [PATCH v2 00/47] Perf build support for -Wshorten-64-to-32 Ian Rogers
2025-04-30 17:49 ` [PATCH v2 01/47] perf build: Avoid building libbpf/bpftool with LIBBPF_DYNAMIC Ian Rogers
2025-04-30 17:49 ` [PATCH v2 02/47] perf tests: Silence -Wshorten-64-to-32 warnings Ian Rogers
2025-04-30 17:49 ` [PATCH v2 03/47] perf ui: " Ian Rogers
2025-04-30 17:49 ` [PATCH v2 04/47] perf bench: " Ian Rogers
2025-04-30 20:23   ` Dirk Gouders
2025-04-30 21:04     ` Ian Rogers
2025-04-30 22:18       ` Dirk Gouders
2025-04-30 22:22         ` Ian Rogers
2025-04-30 23:11           ` Dirk Gouders
2025-05-02 12:06             ` David Laight
2025-05-02 14:12               ` Dirk Gouders
2025-05-03 10:05                 ` David Laight
2025-05-03 12:22                   ` Dirk Gouders [this message]
2025-04-30 17:49 ` [PATCH v2 05/47] arm64: cputype: " Ian Rogers
2025-05-09 12:56   ` Mark Rutland
2025-04-30 17:49 ` [PATCH v2 06/47] x86/insn: " Ian Rogers
2025-04-30 17:49 ` [PATCH v2 07/47] tools lib: " Ian Rogers
2025-04-30 17:49 ` [PATCH v2 08/47] libperf: " Ian Rogers
2025-04-30 17:49 ` [PATCH v2 09/47] tools subcmd: " Ian Rogers
2025-04-30 17:49 ` [PATCH v2 10/47] perf arch x86: " Ian Rogers
2025-04-30 17:49 ` [PATCH v2 11/47] perf arm-spe: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 12/47] perf trace: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 13/47] perf trace-event: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 14/47] perf jvmti: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 15/47] perf pmu: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 16/47] perf annotate powerpc: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 17/47] perf s390: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 18/47] perf cs-etm: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 19/47] perf stat: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 20/47] perf dlfilter: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 21/47] perf demangle: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 22/47] perf annotate: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 23/47] perf report: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 24/47] perf help: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 25/47] perf hisi-ptt: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 26/47] perf probe: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 27/47] perf kwork: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 28/47] perf buildid: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 29/47] perf lock: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 30/47] perf mem: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 31/47] perf script: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 32/47] perf evlist: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 33/47] perf bpf_counter: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 34/47] perf ftrace: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 35/47] perf record: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 36/47] perf inject: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 37/47] perf sched: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 38/47] perf timechart: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 39/47] perf list: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 40/47] perf kvm: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 41/47] perf diff: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 42/47] perf daemon: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 43/47] perf zlib: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 44/47] perf symbol: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 45/47] perf util: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 46/47] perf hashmap: " Ian Rogers
2025-04-30 17:50 ` [PATCH v2 47/47] perf: " Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=gh8qndc296.fsf@gouders.net \
    --to=dirk@gouders.net \
    --cc=acme@kernel.org \
    --cc=adityag@linux.ibm.com \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=algonell@gmail.com \
    --cc=andrealmeid@igalia.com \
    --cc=anshuman.khandual@arm.com \
    --cc=arnd@arndb.de \
    --cc=asmadeus@codewreck.org \
    --cc=atrajeev@linux.vnet.ibm.com \
    --cc=ben.gainey@arm.com \
    --cc=bgeffon@google.com \
    --cc=cachen@purestorage.com \
    --cc=charlie@rivosinc.com \
    --cc=christophe.leroy@csgroup.eu \
    --cc=colin.i.king@gmail.com \
    --cc=ctshao@google.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=dave@stgolabs.net \
    --cc=david.laight.linux@gmail.com \
    --cc=dvhart@infradead.org \
    --cc=dvyukov@google.com \
    --cc=ezulian@redhat.com \
    --cc=fzczx123@gmail.com \
    --cc=gehao@kylinos.cn \
    --cc=gmonaco@redhat.com \
    --cc=graham.woodward@arm.com \
    --cc=hejunhao3@huawei.com \
    --cc=howardchu95@gmail.com \
    --cc=ilkka@os.amperecomputing.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jannh@google.com \
    --cc=jean-philippe.romain@foss.st.com \
    --cc=john.g.garry@oracle.com \
    --cc=jolsa@kernel.org \
    --cc=jonathan.cameron@huawei.com \
    --cc=jpoimboe@kernel.org \
    --cc=justinstitt@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=kjain@linux.ibm.com \
    --cc=krzysztof.m.lopatowski@gmail.com \
    --cc=kyle.meyer@hpe.com \
    --cc=leo.yan@linux.dev \
    --cc=lihuafei1@huawei.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=linux@treblig.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=masahiroy@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mike.leach@linaro.org \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=peterz@infradead.org \
    --cc=quic_zhonhan@quicinc.com \
    --cc=ravi.bangoria@amd.com \
    --cc=scclevenger@os.amperecomputing.com \
    --cc=sesse@google.com \
    --cc=stephen.s.brennan@oracle.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.falcon@intel.com \
    --cc=tmricht@linux.ibm.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=visitorckw@gmail.com \
    --cc=weilin.wang@intel.com \
    --cc=will@kernel.org \
    --cc=wutengda@huaweicloud.com \
    --cc=xu.yang_2@nxp.com \
    --cc=yangjihong@bytedance.com \
    --cc=yangyicong@hisilicon.com \
    --cc=yeoreum.yun@arm.com \
    --cc=yujie.liu@intel.com \
    --cc=yury.norov@gmail.com \
    --cc=zhe.he@windriver.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox