From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9376EC6FD18 for ; Tue, 28 Mar 2023 19:45:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1080F6B0071; Tue, 28 Mar 2023 15:45:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B9196B0072; Tue, 28 Mar 2023 15:45:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9AF46B0074; Tue, 28 Mar 2023 15:45:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D085F6B0071 for ; Tue, 28 Mar 2023 15:45:17 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A1514120485 for ; Tue, 28 Mar 2023 19:45:17 +0000 (UTC) X-FDA: 80619335874.13.7ED7370 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 65E398000B for ; Tue, 28 Mar 2023 19:45:15 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=A68zQewc; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680032715; a=rsa-sha256; cv=none; b=DJWJz03dXJOke3/m8KMXEAda1M7RnpkDQStLc9p2YDIu4LfXtDwubkAdsiJi+KOdvfMm+a yZFOd+IWwXELKTcm7Ipgc2vS3E8O2O2pXJ85VTs4UsKZqaRvE5A/DPJCMSuPW3jg8HMkmn N1/VltRArQv9ig2SUgUYtGIyb2y+ODo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=A68zQewc; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680032715; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=x/M8VEGYnR5Pgi89UAPg3ENEgkiPUXtwVF7r6qIaHxg=; b=xyWE4My0c7PXcYoT4yx1Yolb2oPjco5Y3xG77sSfyFR9Dipw65lhComvWm5u/HX7kr/tgR MW/F7hvYuR1HF1jGfreajHYsxm0o071kk2aFCP9oqn1b4fPy17BMIQvNucf8/MoqWIfOKq MA9tajrVoiKoENfK696htnQGdppHEZY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680032714; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x/M8VEGYnR5Pgi89UAPg3ENEgkiPUXtwVF7r6qIaHxg=; b=A68zQewc5sjNWxBovs4jvQ5vFNL8jxEvU9zc6ltDcAgkPFr3sbHiH2ZNhjybKFRllnp6xR YeDbPBHCYypfKlKUWcU16ymucXqbW/qmr3Qg8tojF9HzTqoOeZwvLIZSgDvnsqX4VpiCyr 63y+IXDQ2RXTvv6nV3J9y1edPdwjIg4= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-436-P9mJPrp8Puq6nB_CJaGETQ-1; Tue, 28 Mar 2023 15:45:13 -0400 X-MC-Unique: P9mJPrp8Puq6nB_CJaGETQ-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-57c67ea348eso8814726d6.1 for ; Tue, 28 Mar 2023 12:45:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680032713; h=in-reply-to:content-transfer-encoding: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=x/M8VEGYnR5Pgi89UAPg3ENEgkiPUXtwVF7r6qIaHxg=; b=QJFP5Q4Z6mwP0aA/FGSzU5GgtMQW9aRyrw3BZpDdtqDjIsle2fAmMOYWgC3rfVs2ZE ufWyABRWmMoE/0Ws4pYCcFyakqUjpGbl34iwCJE8pCHIunj1ylkFmX8+IfwTVxjENZ69 KX7AG2jlKuuBRXG3YlmZT3jXO0oZjd51SVd6KGqc6i2+rijxOWPBNr8uyLBBJPS3RaB2 m5rmT6GnUtPJJfCuCbaCuSZGl1sDF9AsJCweO4IWV+hWijbqO8blWyOzikc3RHMPkK1t yBuFTXuCLCz5ePukwnh4FD3wuGqDvRqojMkqr+ZoySVDEAyWqeEFoYA+t21g3o9DDJwV ZX3g== X-Gm-Message-State: AAQBX9fXnTnCH2qopDx5U+dzcChymGeHVVYYSRS91bzY+ZXTTaGtsgXg Ms/M3UFVfljpcoZ9icOhcvTvdRebq1+nvr/XRfbbAeIGSSCDajxVsGbyY6SP+LrK0xWEpAuY4ys CqvSQP2/qeEk= X-Received: by 2002:a05:6214:5084:b0:57d:747b:1f7 with SMTP id kk4-20020a056214508400b0057d747b01f7mr24823245qvb.1.1680032712929; Tue, 28 Mar 2023 12:45:12 -0700 (PDT) X-Google-Smtp-Source: AKy350b1OWPbZJ0hJOYx4hhf7hI7qh8x9QzpB/N6/g/Ylx7piog3xM++xdIbSSpwnoO35Yfc5FwUIg== X-Received: by 2002:a05:6214:5084:b0:57d:747b:1f7 with SMTP id kk4-20020a056214508400b0057d747b01f7mr24823210qvb.1.1680032712621; Tue, 28 Mar 2023 12:45:12 -0700 (PDT) Received: from x1n (bras-base-aurron9127w-grc-40-70-52-229-124.dsl.bell.ca. [70.52.229.124]) by smtp.gmail.com with ESMTPSA id x16-20020ad440d0000000b005dd8b9345b2sm4043309qvp.74.2023.03.28.12.45.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 12:45:11 -0700 (PDT) Date: Tue, 28 Mar 2023 15:45:10 -0400 From: Peter Xu To: Axel Rasmussen Cc: Alexander Viro , Andrew Morton , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] userfaultfd: don't fail on unrecognized features Message-ID: References: <20220722201513.1624158-1-axelrasmussen@google.com> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: 65E398000B X-Rspamd-Server: rspam01 X-Stat-Signature: 5nbt6x6yci1eisztwngc4ojguyjzdqa7 X-HE-Tag: 1680032715-641426 X-HE-Meta: U2FsdGVkX19pb+h0vyME5mRQ8hL5QsXWURItzwxJEpOxXdtNNx+RafvRdZXMIP9u6FF3EHACmg6doCOropcK3eHhclPk/QupiJKplcMKdxpoQ1P7e08izp+xN92M20rgdGqUiIMRrlxtLVeuymiy0P+NAN325hwzfrJA1PXzjiGkh+tp7Q7fwEZYsd8/TbpEmdR79ez0dL7vF7/cGog6arLdk1bMmvW7wLbqR/WLNqeih8VBFDMNoRol33otPzMI6Uu35vD0KW21nzGP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czKMx14dgsR2YQU7CnoA6zrtyXUs5IoxrK9bLH7daKvDDW4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Mar 28, 2023 at 12:28:59PM -0700, Axel Rasmussen wrote: > On Mon, Mar 27, 2023 at 2:01 PM Peter Xu wrote: > > > > I think I overlooked this patch.. > > > > Axel, could you explain why this patch is correct? Comments inline. > > > > On Fri, Jul 22, 2022 at 01:15:13PM -0700, Axel Rasmussen wrote: > > > The basic interaction for setting up a userfaultfd is, userspace issues > > > a UFFDIO_API ioctl, and passes in a set of zero or more feature flags, > > > indicating the features they would prefer to use. > > > > > > Of course, different kernels may support different sets of features > > > (depending on kernel version, kconfig options, architecture, etc). > > > Userspace's expectations may also not match: perhaps it was built > > > against newer kernel headers, which defined some features the kernel > > > it's running on doesn't support. > > > > > > Currently, if userspace passes in a flag we don't recognize, the > > > initialization fails and we return -EINVAL. This isn't great, though. > > > > Why? IIUC that's the major way for user app to detect any misconfig of > > feature list so it can bail out early. > > > > Quoting from man page (ioctl_userfaultfd(2)): > > > > UFFDIO_API > > (Since Linux 4.3.) Enable operation of the userfaultfd and perform API handshake. > > > > ... > > > > struct uffdio_api { > > __u64 api; /* Requested API version (input) */ > > __u64 features; /* Requested features (input/output) */ > > __u64 ioctls; /* Available ioctl() operations (output) */ > > }; > > > > ... > > > > For Linux kernel versions before 4.11, the features field must be > > initialized to zero before the call to UFFDIO_API, and zero (i.e., > > no feature bits) is placed in the features field by the kernel upon > > return from ioctl(2). > > > > ... > > > > To enable userfaultfd features the application should set a bit > > corresponding to each feature it wants to enable in the features > > field. If the kernel supports all the requested features it will > > enable them. Otherwise it will zero out the returned uffdio_api > > structure and return EINVAL. > > > > IIUC the right way to use this API is first probe with features==0, then > > the kernel will return all the supported features, then the user app should > > enable only a subset (or all, but not a superset) of supported ones in the > > next UFFDIO_API with a new uffd. > > Hmm, I think doing a two-step handshake just overcomplicates things. > > Isn't it simpler to just have userspace ask for the features it wants > up front, and then the kernel responds with the subset of features it > actually supports? In the common case (all features were supported), > there is nothing more to do. Userspace is free to detect the uncommon > case where some features it asked for are missing, and handle that > however it likes. > > I think this patch is backwards compatible with the two-step approach, too. > > I do agree the man page could use some work. I don't think it > describes the two-step handshake process correctly, either. It just > says, "ask for the features you want, and the kernel will either give > them to you or fail". If we really did want to keep the two-step > process, it should describe it (set features == 0 first, then ask only > for the ones you want which are supported), and the example program > should demonstrate it. > > But, I think it's simpler to just have the kernel do what the man page > describes. Userspace asks for the features up front, kernel responds > with the subset that are actually supported. No need to return EINVAL > if unsupported features were requested. The uffdio_api.features passed into the ioctl(UFFDIO_API) should be such request to enable features specified in the kernel. If the kernel doesn't support any of the features in the list, IMHO it's very natural to fail it as described in the man page. That's also most of the kernel apis do afaik, by failing any enablement of features if not supported. > > > > > > Userspace doesn't have an obvious way to react to this; sure, one of the > > > features I asked for was unavailable, but which one? The only option it > > > has is to turn off things "at random" and hope something works. > > > > > > Instead, modify UFFDIO_API to just ignore any unrecognized feature > > > flags. The interaction is now that the initialization will succeed, and > > > as always we return the *subset* of feature flags that can actually be > > > used back to userspace. > > > > > > Now userspace has an obvious way to react: it checks if any flags it > > > asked for are missing. If so, it can conclude this kernel doesn't > > > support those, and it can either resign itself to not using them, or > > > fail with an error on its own, or whatever else. > > > > > > Signed-off-by: Axel Rasmussen > > > --- > > > fs/userfaultfd.c | 6 ++---- > > > 1 file changed, 2 insertions(+), 4 deletions(-) > > > > > > diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c > > > index e943370107d0..4974da1f620c 100644 > > > --- a/fs/userfaultfd.c > > > +++ b/fs/userfaultfd.c > > > @@ -1923,10 +1923,8 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, > > > ret = -EFAULT; > > > if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api))) > > > goto out; > > > - features = uffdio_api.features; > > > - ret = -EINVAL; > > > - if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) > > > - goto err_out; > > > > What's worse is that I think you removed the only UFFD_API check. Although > > I'm not sure whether it'll be extended in the future or not at all (very > > possible we keep using 0xaa forever..), but removing this means we won't be > > able to extend it to a new api version in the future, and misconfig of > > uffdio_api will wrongly succeed I think: > > > > /* Test wrong UFFD_API */ > > uffdio_api.api = 0xab; > > uffdio_api.features = 0; > > if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) > > err("UFFDIO_API should fail but didn't"); > > Agreed, we should add back the UFFD_API check - I am happy to send a > patch for this. Do you plan to just revert the patch? If so, please go ahead. IMHO we should just follow the man page. What I agree here is the api isn't that perfect, in that we need to create a separate userfault file descriptor just to probe. Currently the features will be returned in the initial test with features=0 passed in, but it also initializes the uffd handle even if it'll never be used but for probe only. However since that existed in the 1st day I guess we'd better keep it as-is. And it's not so bad either: user app does open/close one more time, but only once for each app's lifecycle. Thanks, -- Peter Xu