linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Nadav Amit <nadav.amit@gmail.com>
To: David Hildenbrand <david@redhat.com>
Cc: Li Wang <liwang@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-kselftest@vger.kernel.org,
	"open list:MEMORY MANAGEMENT" <linux-mm@kvack.org>,
	Peter Xu <peterx@redhat.com>,
	Aruna Ramakrishna <aruna.ramakrishna@oracle.com>,
	Bagas Sanjaya <bagasdotme@gmail.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Joey Gouly <joey.gouly@arm.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Keith Lucas <keith.lucas@oracle.com>,
	Ryan Roberts <ryan.roberts@arm.com>,
	Shuah Khan <shuah@kernel.org>
Subject: Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation
Date: Tue, 24 Jun 2025 14:29:27 +0300	[thread overview]
Message-ID: <611F9598-A1A4-47B6-B37E-09BF7B4D17D0@gmail.com> (raw)
In-Reply-To: <4fd18a1c-aba2-468a-881f-0507953f2904@redhat.com>



> On 24 Jun 2025, at 11:22, David Hildenbrand <david@redhat.com> wrote:
> 
> On 24.06.25 10:07, David Hildenbrand wrote:
>>> 
>> Is that actually required?
>> The man page explicitly documents:
>> "       EINVAL A  previous  UFFDIO_API  call already enabled one or more
>> features for this userfaultfd.  Calling UFF‐
>>                DIO_API twice, the first time with no features set, is
>> explicitly allowed as per the two-step  feature
>>                detection handshake.
>> "
>> So if that doesn't work, something might be broken.
> 
> CCing Nadav and Peter:
> 
> Could it be that
> 
> commit 22e5fe2a2a279d9a6fcbdfb4dffe73821bef1c90
> Author: Nadav Amit <nadav.amit@gmail.com>
> Date:   Thu Sep 2 14:58:59 2021 -0700
> 
>    userfaultfd: prevent concurrent API initialization
>        userfaultfd assumes that the enabled features are set once and never
>    changed after UFFDIO_API ioctl succeeded.
>        However, currently, UFFDIO_API can be called concurrently from two
>    different threads, succeed on both threads and leave userfaultfd's
>    features in non-deterministic state.  Theoretically, other uffd operations
>    (ioctl's and page-faults) can be dispatched while adversely affected by
>    such changes of features.
>        Moreover, the writes to ctx->state and ctx->features are not ordered,
>    which can - theoretically, again - let userfaultfd_ioctl() think that
>    userfaultfd API completed, while the features are still not initialized.
>        To avoid races, it is arguably best to get rid of ctx->state.  Since there
>    are only 2 states, record the API initialization in ctx->features as the
>    uppermost bit and remove ctx->state.
> 
> Accidentally broke the documented two-step handshake in the man page where we
> can avoid closing + reopening the fd?

I agree the code is not correct (and my patch didn’t address this issue),
but I don’t see it broke it either.

Unless I’m missing something the code before my patch, when
uffdio_api.features == 0, also set ctx->state to UFFD_STATE_RUNNING, which
meant another invocation would see (ctx->state != UFFD_STATE_WAIT_API) and
fail.

> 
> Without testing, the following might fix it if I am right:
> 
> diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
> index 22f4bf956ba1c..f03e7c980e1c5 100644
> --- a/fs/userfaultfd.c
> +++ b/fs/userfaultfd.c
> @@ -1944,9 +1944,9 @@ static int userfaultfd_move(struct userfaultfd_ctx *ctx,
> static int userfaultfd_api(struct userfaultfd_ctx *ctx,
>                           unsigned long arg)
> {
> +       unsigned int new_features, old_features = 0;
>        struct uffdio_api uffdio_api;
>        void __user *buf = (void __user *)arg;
> -       unsigned int ctx_features;
>        int ret;
>        __u64 features;
> @@ -1990,9 +1990,12 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx,
>                goto out;
>         /* only enable the requested features for this uffd context */
> -       ctx_features = uffd_ctx_features(features);
> +       new_features = uffd_ctx_features(features);
> +       /* allow two-step handshake */
> +       if (userfaultfd_is_initialized(ctx))
> +               old_features = UFFD_FEATURE_INITIALIZED;
>        ret = -EINVAL;
> -       if (cmpxchg(&ctx->features, 0, ctx_features) != 0)
> +       if (cmpxchg(&ctx->features, old_features, new_features) != old_features)
>                goto err_out;
>         ret = 0;

I am not sure it is right since you would return EINVAL in this case.
It also looks a bit overly complicated - are you concerned about a race?
My whole concern about race was that somebody would exploit it to
overcome non-cooperative UFFD (IIRC).

So perhaps just add a check for the case features if 0 and be done with
it? Something like adding:

	ret = 0;
	if (ctx->features == 0 && features == 0)
		goto err_out; 		/* no error but copying of uffdio_api required */

before enabling the requested features for this uffd context.



  reply	other threads:[~2025-06-24 11:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-22  8:10 [PATCH] mm/selftests: improve UFFD-WP feature detection in KSM test Li Wang
2025-06-23  8:33 ` David Hildenbrand
2025-06-24  3:43   ` Li Wang
2025-06-24  4:24 ` [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation Li Wang
2025-06-24  8:07   ` David Hildenbrand
2025-06-24  8:22     ` David Hildenbrand
2025-06-24 11:29       ` Nadav Amit [this message]
2025-06-24 11:39         ` David Hildenbrand
2025-06-24 11:48           ` David Hildenbrand
2025-06-24 15:03             ` Peter Xu
2025-06-24 15:17               ` David Hildenbrand
2025-06-24 15:17   ` David Hildenbrand
2025-06-25  0:34     ` Li Wang

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=611F9598-A1A4-47B6-B37E-09BF7B4D17D0@gmail.com \
    --to=nadav.amit@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=aruna.ramakrishna@oracle.com \
    --cc=bagasdotme@gmail.com \
    --cc=catalin.marinas@arm.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@redhat.com \
    --cc=hannes@cmpxchg.org \
    --cc=joey.gouly@arm.com \
    --cc=keith.lucas@oracle.com \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=liwang@redhat.com \
    --cc=peterx@redhat.com \
    --cc=ryan.roberts@arm.com \
    --cc=shuah@kernel.org \
    /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