Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Reinette Chatre <reinette.chatre@intel.com>
Cc: linux-kselftest@vger.kernel.org, Shuah Khan <shuah@kernel.org>,
	Shaopeng Tan <tan.shaopeng@jp.fujitsu.com>,
	Fenghua Yu <fenghua.yu@intel.com>,
	Babu Moger <babu.moger@amd.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v4 04/19] selftests/resctrl: Close perf value read fd on errors
Date: Mon, 17 Jul 2023 16:05:43 +0300 (EEST)	[thread overview]
Message-ID: <8b904781-c5d-8164-b8dc-903d412330fd@linux.intel.com> (raw)
In-Reply-To: <4531fca1-2f3b-0c08-351b-f8e06c5f9f5c@intel.com>

[-- Attachment #1: Type: text/plain, Size: 4312 bytes --]

On Fri, 14 Jul 2023, Reinette Chatre wrote:
> On 7/14/2023 3:35 AM, Ilpo Järvinen wrote:
> > On Thu, 13 Jul 2023, Reinette Chatre wrote:
> >> On 7/13/2023 6:19 AM, Ilpo Järvinen wrote:
> >>> Perf event fd (fd_lm) is not closed on some error paths.
> >>>
> >>> Always close fd_lm in get_llc_perf() and add close into an error
> >>> handling block in cat_val().
> >>>
> >>> Fixes: 790bf585b0ee ("selftests/resctrl: Add Cache Allocation Technology (CAT) selftest")
> >>> Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> >>> ---
> >>>  tools/testing/selftests/resctrl/cache.c | 10 +++++-----
> >>>  1 file changed, 5 insertions(+), 5 deletions(-)
> >>>
> >>> diff --git a/tools/testing/selftests/resctrl/cache.c b/tools/testing/selftests/resctrl/cache.c
> >>> index 8a4fe8693be6..ced47b445d1e 100644
> >>> --- a/tools/testing/selftests/resctrl/cache.c
> >>> +++ b/tools/testing/selftests/resctrl/cache.c
> >>> @@ -87,21 +87,20 @@ static int reset_enable_llc_perf(pid_t pid, int cpu_no)
> >>>  static int get_llc_perf(unsigned long *llc_perf_miss)
> >>>  {
> >>>  	__u64 total_misses;
> >>> +	int ret;
> >>>  
> >>>  	/* Stop counters after one span to get miss rate */
> >>>  
> >>>  	ioctl(fd_lm, PERF_EVENT_IOC_DISABLE, 0);
> >>>  
> >>> -	if (read(fd_lm, &rf_cqm, sizeof(struct read_format)) == -1) {
> >>> +	ret = read(fd_lm, &rf_cqm, sizeof(struct read_format));
> >>> +	close(fd_lm);
> >>> +	if (ret == -1) {
> >>>  		perror("Could not get llc misses through perf");
> >>> -
> >>>  		return -1;
> >>>  	}
> >>>  
> >>>  	total_misses = rf_cqm.values[0].value;
> >>> -
> >>> -	close(fd_lm);
> >>> -
> >>>  	*llc_perf_miss = total_misses;
> >>>  
> >>>  	return 0;
> >>> @@ -253,6 +252,7 @@ int cat_val(struct resctrl_val_param *param)
> >>>  					 memflush, operation, resctrl_val)) {
> >>>  				fprintf(stderr, "Error-running fill buffer\n");
> >>>  				ret = -1;
> >>> +				close(fd_lm);
> >>>  				break;
> >>>  			}
> >>>  
> >>
> >> Instead of fixing these existing patterns I think it would make the code
> >> easier to understand and maintain if it is made symmetrical.
> >> Having the perf event fd opened in one place but its close()
> >> scattered elsewhere has the potential for confusion and making later
> >> mistakes easy to miss.
> >>
> >> What if perf event fd is closed in a new "disable_llc_perf()" that
> >> is matched with "reset_enable_llc_perf()" and called
> >> from cat_val()?
> >>
> >> I think this raises another issue with the test trickery where
> >> measure_cache_vals() has some assumptions about state based on the
> >> test name.
> > 
> > I very much agree on the principle here, and thus I already have created 
> > patches which will do a major cleanup on this area. The cleaned-up code 
> > has pe_fd local var to cat_val() and handles closing it in cat_val() with 
> > the usual patterns.
> > 
> > However, the patch is currently resides post L3 CAT test rewrite. 
> > Backporting the cleanups/refactors into this series would require 
> > considerable effort due to how convoluted all those n-step cleanup patches 
> > and L3 CAT test rewrite are in this area. There's just very much to 
> > cleanup here and L3 rewrite will touch the same areas so its a net 
> > full of conflicts.
> > 
> > Do you want me to spend the effort to backport them into this series 
> > (I expect will take some time)?
> 
> Considering the "Fixes" tag, having a smaller fix that can easily
> be backported would be ideal so I am ok with deferring a bigger
> rework.
> 
> I do think this fix can be made more robust with a couple of small
> changes that should not introduce significant conflicts:
> * initialize fd_lm to -1 

> * do not close() fd_lm in get_llc_perf() but instead move its
>   close() to at exit of cat_val().

I changed the test to only close the fd in cat_val() which is the 
direction the later refactor/cleanup changes (not in this series) was 
moving anyway.

> * add check in get_llc_perf() that it does not attempt ioctl()
>   on "fd_lm == -1" (later addition would be error checking of
>   the ioctl())

The other two things suggested seem unnecessary and I've not implemented 
them, I don't thinkg fd_lm can be -1 at ioctl(). Given this code is going 
to be replaced soonish, putting any extra "safety" effort into it now 
seems waste of time.

-- 
 i.

  reply	other threads:[~2023-07-17 13:07 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-13 13:19 [PATCH v4 00/19] selftests/resctrl: Fixes and cleanups Ilpo Järvinen
2023-07-13 13:19 ` [PATCH v4 01/19] selftests/resctrl: Add resctrl.h into build deps Ilpo Järvinen
2023-07-13 22:43   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 02/19] selftests/resctrl: Don't leak buffer in fill_cache() Ilpo Järvinen
2023-07-13 22:44   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 03/19] selftests/resctrl: Unmount resctrl FS if child fails to run benchmark Ilpo Järvinen
2023-07-13 22:51   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 04/19] selftests/resctrl: Close perf value read fd on errors Ilpo Järvinen
2023-07-13 22:52   ` Reinette Chatre
2023-07-14 10:35     ` Ilpo Järvinen
2023-07-14 17:36       ` Reinette Chatre
2023-07-17 13:05         ` Ilpo Järvinen [this message]
2023-07-17 16:09           ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 05/19] selftests/resctrl: Unmount resctrl FS before starting the first test Ilpo Järvinen
2023-07-13 22:53   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 06/19] selftests/resctrl: Move resctrl FS mount/umount to higher level Ilpo Järvinen
2023-07-13 22:55   ` Reinette Chatre
2023-07-14 11:31     ` Ilpo Järvinen
2023-07-14 17:36       ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 07/19] selftests/resctrl: Refactor remount_resctrl(bool mum_resctrlfs) to mount_resctrl() Ilpo Järvinen
2023-07-13 22:57   ` Reinette Chatre
2023-07-14 11:03     ` Ilpo Järvinen
2023-07-24  7:12       ` Wieczor-Retman, Maciej
2023-08-07 10:26         ` Ilpo Järvinen
2023-07-13 13:19 ` [PATCH v4 08/19] selftests/resctrl: Remove mum_resctrlfs from struct resctrl_val_param Ilpo Järvinen
2023-07-13 22:59   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 09/19] selftests/resctrl: Convert span to size_t Ilpo Järvinen
2023-07-13 22:59   ` Reinette Chatre
2023-07-14 10:33     ` Ilpo Järvinen
2023-07-13 13:19 ` [PATCH v4 10/19] selftests/resctrl: Express span internally in bytes Ilpo Järvinen
2023-07-13 23:00   ` Reinette Chatre
2023-07-14  6:43     ` Wieczor-Retman, Maciej
2023-07-14 10:22       ` Ilpo Järvinen
2023-07-14 17:38         ` Reinette Chatre
2023-07-17 12:30           ` Ilpo Järvinen
2023-07-17 16:10             ` Reinette Chatre
2023-07-18 10:10               ` Ilpo Järvinen
2023-07-14 17:38       ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 11/19] selftests/resctrl: Remove duplicated preparation for span arg Ilpo Järvinen
2023-07-13 23:01   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 12/19] selftests/resctrl: Remove "malloc_and_init_memory" param from run_fill_buf() Ilpo Järvinen
2023-07-13 23:05   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 13/19] selftests/resctrl: Remove unnecessary startptr global from fill_buf Ilpo Järvinen
2023-07-13 23:06   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 14/19] selftests/resctrl: Improve parameter consistency in fill_buf Ilpo Järvinen
2023-07-13 23:07   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 15/19] selftests/resctrl: Don't pass test name to fill_buf Ilpo Järvinen
2023-07-13 23:10   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 16/19] selftests/resctrl: Don't use variable argument list for ->setup() Ilpo Järvinen
2023-07-13 23:13   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 17/19] selftests/resctrl: Move CAT/CMT test global vars to function they are used in Ilpo Järvinen
2023-07-14  0:04   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 18/19] selftests/resctrl: Pass the real number of tests to show_cache_info() Ilpo Järvinen
2023-07-14  0:05   ` Reinette Chatre
2023-07-13 13:19 ` [PATCH v4 19/19] selftests/resctrl: Remove test type checks from cat_val() Ilpo Järvinen
2023-07-14  0:07   ` Reinette Chatre

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=8b904781-c5d-8164-b8dc-903d412330fd@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=babu.moger@amd.com \
    --cc=fenghua.yu@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=reinette.chatre@intel.com \
    --cc=shuah@kernel.org \
    --cc=tan.shaopeng@jp.fujitsu.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