lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250728165549.62546-1-sj@kernel.org>
Date: Mon, 28 Jul 2025 09:55:49 -0700
From: SeongJae Park <sj@...nel.org>
To: Usama Arif <usamaarif642@...il.com>
Cc: SeongJae Park <sj@...nel.org>,
	Andrew Morton <akpm@...ux-foundation.org>,
	david@...hat.com,
	linux-mm@...ck.org,
	linux-fsdevel@...r.kernel.org,
	corbet@....net,
	rppt@...nel.org,
	surenb@...gle.com,
	mhocko@...e.com,
	hannes@...xchg.org,
	baohua@...nel.org,
	shakeel.butt@...ux.dev,
	riel@...riel.com,
	ziy@...dia.com,
	laoar.shao@...il.com,
	dev.jain@....com,
	baolin.wang@...ux.alibaba.com,
	npache@...hat.com,
	lorenzo.stoakes@...cle.com,
	Liam.Howlett@...cle.com,
	ryan.roberts@....com,
	vbabka@...e.cz,
	jannh@...gle.com,
	Arnd Bergmann <arnd@...db.de>,
	linux-kernel@...r.kernel.org,
	linux-doc@...r.kernel.org,
	kernel-team@...a.com
Subject: Re: [PATCH 5/5] selftests: prctl: introduce tests for disabling THPs except for madvise

On Fri, 25 Jul 2025 17:22:44 +0100 Usama Arif <usamaarif642@...il.com> wrote:

> The test will set the global system THP setting to always and
> the 2M setting to inherit before it starts (and reset to original
> at teardown)
> 
> This tests if the process can:
> - successfully set and get the policy to disable THPs expect for madvise.

s/expect/except/

> - get hugepages only on MADV_HUGE and MADV_COLLAPSE after policy is set.
> - successfully reset the policy of the process.
> - get hugepages always after reset.
> - repeat the above tests in a forked process to make sure  the policy is
>   carried across forks.
> 
> Signed-off-by: Usama Arif <usamaarif642@...il.com>
> ---
>  .../testing/selftests/mm/prctl_thp_disable.c  | 95 +++++++++++++++++++
>  1 file changed, 95 insertions(+)
> 
> diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
> index 52f7e6659b1f..288d5ad6ffbb 100644
> --- a/tools/testing/selftests/mm/prctl_thp_disable.c
> +++ b/tools/testing/selftests/mm/prctl_thp_disable.c
> @@ -65,6 +65,101 @@ static int test_mmap_thp(enum madvise_buffer madvise_buf, size_t pmdsize)
>  	munmap(buffer, buf_size);
>  	return ret;
>  }
> +
> +FIXTURE(prctl_thp_disable_except_madvise)
> +{
> +	struct thp_settings settings;
> +	size_t pmdsize;
> +};
> +
> +FIXTURE_SETUP(prctl_thp_disable_except_madvise)
> +{
> +	if (!thp_is_enabled())
> +		SKIP(return, "Transparent Hugepages not available\n");

As David also pointed out on the other patch, the message and the function name
would better to be consistent.

> +
> +	self->pmdsize = read_pmd_pagesize();
> +	if (!self->pmdsize)
> +		SKIP(return, "Unable to read PMD size\n");
> +
> +	thp_read_settings(&self->settings);
> +	self->settings.thp_enabled = THP_ALWAYS;
> +	self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
> +	thp_save_settings();
> +	thp_push_settings(&self->settings);
> +

Unnecessary empty line?

> +}
> +
> +FIXTURE_TEARDOWN(prctl_thp_disable_except_madvise)
> +{
> +	thp_restore_settings();
> +}
> +
> +/* prctl_thp_disable_except_madvise fixture sets system THP setting to always */
> +static void prctl_thp_disable_except_madvise(struct __test_metadata *const _metadata,
> +					     size_t pmdsize)
> +{
> +	int res = 0;
> +
> +	res = prctl(PR_GET_THP_DISABLE, NULL, NULL, NULL, NULL);
> +	ASSERT_EQ(res, 3);
> +
> +	/* global = always, process = madvise, we shouldn't get HPs without madvise */
> +	res = test_mmap_thp(NONE, pmdsize);
> +	ASSERT_EQ(res, 0);
> +
> +	res = test_mmap_thp(HUGE, pmdsize);
> +	ASSERT_EQ(res, 1);
> +
> +	res = test_mmap_thp(COLLAPSE, pmdsize);
> +	ASSERT_EQ(res, 1);
> +
> +	/* Reset to system policy */
> +	res =  prctl(PR_SET_THP_DISABLE, 0, NULL, NULL, NULL);
> +	ASSERT_EQ(res, 0);
> +
> +	/* global = always, hence we should get HPs without madvise */
> +	res = test_mmap_thp(NONE, pmdsize);
> +	ASSERT_EQ(res, 1);
> +
> +	res = test_mmap_thp(HUGE, pmdsize);
> +	ASSERT_EQ(res, 1);
> +
> +	res = test_mmap_thp(COLLAPSE, pmdsize);
> +	ASSERT_EQ(res, 1);

Seems res is not being used other than saving the return value for assertions.
Why don't you do the assertion at once, e.g., ASSERT_EQ(test_mmap_thp(...), 1)?
No strong opinion, but I think that could make code shorter and easier to read.

> +}
> +
> +TEST_F(prctl_thp_disable_except_madvise, nofork)
> +{
> +	int res = 0;
> +
> +	res = prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL);
> +	ASSERT_EQ(res, 0);

Again, I think 'res' can be removed.

> +	prctl_thp_disable_except_madvise(_metadata, self->pmdsize);
> +}
> +
> +TEST_F(prctl_thp_disable_except_madvise, fork)
> +{
> +	int res = 0, ret = 0;
> +	pid_t pid;
> +
> +	res = prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL);
> +	ASSERT_EQ(res, 0);

Ditto.

> +
> +	/* Make sure prctl changes are carried across fork */
> +	pid = fork();
> +	ASSERT_GE(pid, 0);
> +
> +	if (!pid)
> +		prctl_thp_disable_except_madvise(_metadata, self->pmdsize);
> +
> +	wait(&ret);
> +	if (WIFEXITED(ret))
> +		ret = WEXITSTATUS(ret);
> +	else
> +		ret = -EINVAL;
> +	ASSERT_EQ(ret, 0);
> +}
> +
>  FIXTURE(prctl_thp_disable_completely)
>  {
>  	struct thp_settings settings;
> -- 
> 2.47.3


Thanks,
SJ

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ