From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6EE251D63FC for ; Wed, 15 Jan 2025 18:27:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736965676; cv=none; b=hjSb3OpRsCTeIIsKrMjyhS8nTZCHgp/LK70Mb5wOFnul9EKn3GAOzSoOdb0i0NC9m3/bX2lqqyCDn0LJbSzCB/G/ar/281h7LxsC+DES77otCrp6EYpKt3+F/l1AQIdSkzNyEAKuZUz3ZWUGXI4/rtdRpkQydJwKbF0R9ehNmNE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736965676; c=relaxed/simple; bh=b81TDoYer9uSPGtGgKgaQXhHGD9mvax9zMxqg4mP1tM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Z2j46TfQc8hoOZzWn/iZlin4S5YPwYxEvmJBbOWalWxi6GwmenHcv7CKtrI/pDgq9ZcZkFrEYcewRZeXNIqCv3O5Sd9xEIlBURnne/xrly+AmCEhbypE+VTAU/zL+Ob+kBOMwHKNA3msSt8wynRcG1yZaB8bax4733qPgtTQ5js= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=eGuBx32L; arc=none smtp.client-ip=209.85.208.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="eGuBx32L" Received: by mail-ed1-f46.google.com with SMTP id 4fb4d7f45d1cf-5d3d14336f0so61998a12.3 for ; Wed, 15 Jan 2025 10:27:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1736965673; x=1737570473; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=7oLEDLp95AHbo5oQtn8gEnk71S0ZQtoEW5mdd9dPbF4=; b=eGuBx32Lr3x5zUDmQD8HOo/smxMVBZ0ykoCl1OFuKhFGMruiBqcKCcquUFcb5GmCo1 lTXYIpFmRCul8Q+Wrfi8p3EbFUyw11C42dVi3dn5ARdCrKWYnKFPwpUnvllAipS8P4Y6 2olX8wA2pFjwIEQj2paeHAjKQVHpJIFIdbkAtTbz56ELpHDhDUHr6x/s6dcL7JZko383 8zi6IM/nTngNJZrIurLSu5uXxmZxSi576b6DQQ3xRzIpKSjqD5YIeyFoAzoW+Hu1Xj5I 60jBMmne8HFvuE1H8FGbi7cZT7SVsYi47QWFkGAuUQQR6+pvbwFlJ5DxX8yRynJvQn/A 66Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965673; x=1737570473; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=7oLEDLp95AHbo5oQtn8gEnk71S0ZQtoEW5mdd9dPbF4=; b=dPxnhjQJZCmcYF1HYbJDBG2pDGhhxcYTSIWa0EForjVwMw4jfaD7xBZShlWG81wZXx 5Nv6l+b/K+XM7oTgdx1OMHmIs9HrIUgBclG83q3Skl9PlwJrgICGC2gqkGe98sYlXuDn p7EwjAILNVGjivz2SCXWZBI4m0uliXchScxD6afyg2bmaI+hjOaBeOBJ61lZV3W154jC Uusd+tzuUt++gZJ7fBk6BfeSrpJnaVqQB/Lowy/w8RjAjTUhYUC2J0A1PxRYw3Oc5NmI 25Og1dLg9U2ntGNRK23kf85j8oKIK1DoOjq+ZxtQTu90Rczy9nWY3iKKw2PuVXaPdVQX 3N7g== X-Forwarded-Encrypted: i=1; AJvYcCUn/qIjeqIJRopDgvIsBXnPl2krVzN5PV4QcrHgUzw1M5kMMEFd/UCJJvzNOrHKGdL24nTvip4fHZ67FS4=@vger.kernel.org X-Gm-Message-State: AOJu0YwWuOHO/ibbIPRmOa8wyFS4CuYb3kqIh6y15ygNaMOeEypUpqz0 3xw5DyimIHt9LmlCryRb0B1aF50VG0gto2TSpmgQQ9ORQDcbZESbW4WC7Czo460= X-Gm-Gg: ASbGncsm+/ohOBoHO8sppa72zlioBoMA95QDFaqb+NXMLXeOrgVaBG49t2xH5CYidwb vqi/itg3H2+GFsOmq+j2iBhGK5+ox+SjtAP4ZXa+Nye58mPjZgBPfVu+L7Wpwp7VVFLy2b9T1+I zrXjjbPHuOKG4k2bXk2gDCfwa0ZzqX+LmHGLtCmhidUJFk9n82lyWvLzaBLhASWAt16hYErM7sG W4dxCAR2mHpi8By8Yp2ETf6Erm86n61by5cdfcajK6cvO0MGqZeWXimAA8WaQ== X-Google-Smtp-Source: AGHT+IFnh/mf8QpCO5KOVM66CBu2aIULtQ0OwPffDYH7pNUvhK6RjWRKYXvrK24PestFeF88220XYA== X-Received: by 2002:a05:6402:84f:b0:5d0:d330:c965 with SMTP id 4fb4d7f45d1cf-5d972d28da9mr28448865a12.0.1736965672674; Wed, 15 Jan 2025 10:27:52 -0800 (PST) Received: from localhost ([196.207.164.177]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5d99008c2d7sm7804452a12.3.2025.01.15.10.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:27:52 -0800 (PST) Date: Wed, 15 Jan 2025 21:27:48 +0300 From: Dan Carpenter To: Costa Shulyupin Cc: Steven Rostedt , Daniel Bristot de Oliveira , John Kacur , "Luis Claudio R. Goncalves" , Eder Zulian , Tomas Glozar , Gabriele Monaco , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] Add osnoise_trace_is_off() Message-ID: <142694b4-85de-412b-9016-4ea3c66e726b@stanley.mountain> References: <20250115171359.1954533-1-costa.shul@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250115171359.1954533-1-costa.shul@redhat.com> On Wed, Jan 15, 2025 at 07:11:59PM +0200, Costa Shulyupin wrote: > The usage of trace_is_off() confusing, which requires a detailed > explanation. > > Let's modify the source code by moving the first > member, `trace`, of the `osnoise_tool` structure to the second position: > > struct osnoise_tool { > - struct trace_instance trace; > struct osnoise_context *context; > + struct trace_instance trace; > > A correct program would work properly after this change, > but this one does not. > > Then, run the program under gdb to observe the behavior. > > gdb -q --args ./rtla osnoise -D -d 2 -T 10000 -q > > Program received signal SIGSEGV, Segmentation fault. > 0x000000000040298f in trace_is_off (tool=tool@entry=0x418458, trace=trace@entry=0x8) at src/trace.c:538 > 538 if (trace && !tracefs_trace_is_on(trace->inst)) > ... > > The program checks if trace, which has a value of 8, is not null, > and then crashes when it attempts to dereference trace->inst. > > It happens because trace_is_off() is called as: > trace_is_off(&tool->trace, &record->trace) > > Where `record` is NULL. Expression `&record->trace` returns offset of > member `trace`, which is 8. The original code accidentally works because > offset of `record->trace` is zero. > > Expanded wrong instructions are: > record = NULL; > if (&record->trace && !tracefs_trace_is_on(record->trace.inst)) > return 1; > > The correct instructions are: > record = NULL; > if (record && !tracefs_trace_is_on(record->trace.inst)) > return 1; > > Refactor `trace_is_off` to `osnoise_trace_is_off` and move it to > osnoise.c because it instead of `struct trace_instance` accesses `struct > osnoise_tool`, which is out of the scope of trace.c. > This commit message is so confusing. It takes five paragraphs to explain how NULL + sizeof(void *) equals 8. Everyone knows that already, but it's irrelevant because the actual math in the code is "NULL plus zero". And then there is the crash dump which can't happen unless you change the math from zero to an eight. It would be easy get the impression that this change is some kind of complicated fix to a crashing bug. Earlier I provided you with a good commit message which you could just copy and paste but here is an updated version: This patch is just a clean up and does not affect the behavior. Consider this code: if (trace_is_off(&tool->trace, &record->trace)) The "record" pointer can be NULL in this code. If you're new to the code then it looks like a potential NULL dereference. It turns out it's fine because ->trace is the first element of the record and the trace_is_off() function has a NULL check. So it's fine. But it does look sketchy when you're not super familiar with the code. Refactor the trace_is_off() function to take a record pointer instead of the trace pointer and rename it to osnoise_trace_is_off(). You need to add a subsystem prefix to the subject as well. regards, dan carpenter