Re: [PATCH] selftests/livepatch: fix resource leak in test_klp_syscall init error path

"Rui Qi" <[email protected]> Thu, 4 Jun 2026 16:10:53 +0800
Newsgroups org.kernel.vger.live-patching,org.kernel.vger.linux-kernel,org.kernel.vger.linux-kselftest
Message-ID <[email protected]>
On 6/3/26 9:09 PM, Miroslav Benes wrote:
> On Tue, 02 Jun 2026 20:45:09 +0800, Rui Qi <[email protected]> wrote:
>> diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c b/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c
>> index 0630ffd9d9a1..d631acae48b9 100644
>> --- a/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c
>> +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c
>> @@ -109,7 +109,12 @@ static int livepatch_init(void)
>>  	 */
>>  	npids = npids_pending;
>>  
>> -	return klp_enable_patch(&patch);
>> +	ret = klp_enable_patch(&patch);
>> +	if (ret) {
>> +		sysfs_remove_file(klp_kobj, &klp_attr.attr);
>> +		kobject_put(klp_kobj);
>> +	}
>> +	return ret;
> 
> Is sysfs_remove_file() needed? I think that kobject_put() should remove
> it automatically since the object is bound to sysfs.
> 

You are right, the sysfs_remove_file() call is redundant. When
kobject_put() drops the refcount to zero, kobject_cleanup() is
invoked, which calls __kobject_del() -> sysfs_remove_dir(). The
latter removes the entire kobject directory including all files
created under it via sysfs_create_file(). This is also consistent
with the livepatch_exit() cleanup path, which only calls
kobject_put() without an explicit sysfs_remove_file().

I will send v2 with sysfs_remove_file() removed.

Thank you for the review.