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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02457C433F5 for ; Wed, 17 Nov 2021 09:28:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7ED7E63222 for ; Wed, 17 Nov 2021 09:28:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7ED7E63222 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B5A4C6B006C; Wed, 17 Nov 2021 04:28:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B09386B0073; Wed, 17 Nov 2021 04:28:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D0746B0074; Wed, 17 Nov 2021 04:28:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0001.hostedemail.com [216.40.44.1]) by kanga.kvack.org (Postfix) with ESMTP id 8A5366B006C for ; Wed, 17 Nov 2021 04:28:15 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 486FF80875 for ; Wed, 17 Nov 2021 09:28:05 +0000 (UTC) X-FDA: 78817895730.06.9CC17DC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf31.hostedemail.com (Postfix) with ESMTP id 703461052981 for ; Wed, 17 Nov 2021 09:28:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1637141284; 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: in-reply-to:in-reply-to:references:references; bh=GW74lTKeqcDtN2h/mSP5w/i30xPY127lVyVN1Tm0gaY=; b=Ata4857YmDi2Gi3GwHXEcG79s6zEOL5GBkdICMaZoKo/RoMeNsX+rd8bB1ElWnt5bNyJ7E gHA8udtyyVuCyLRTYUlkTxyPRMxWpZtioUmGwtIRDE/lKsm5AbdmtQVCaDCZ69h0c1grqJ JJ3XyO/TxVEOzYrkRgUCknPvQ5HHP30= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-323-4xqti1GJNe2eFCGM6hwweA-1; Wed, 17 Nov 2021 04:28:02 -0500 X-MC-Unique: 4xqti1GJNe2eFCGM6hwweA-1 Received: by mail-wm1-f69.google.com with SMTP id 201-20020a1c04d2000000b003335bf8075fso825579wme.0 for ; Wed, 17 Nov 2021 01:28:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=GW74lTKeqcDtN2h/mSP5w/i30xPY127lVyVN1Tm0gaY=; b=o+DZRWH2+g6JRqIePk2e0nGWECQX2gYjiOhJpzGAKpwe0oz5UjpMQU4eC2RZXnQqB2 K6llRFPTyNNb49P0WYijUG71TVNQvz4I1QlM0xXubjS3TUNkTs4wM/bTbxsKC5aPEEfD 33ZVklMesF4824V6iDeV30Q47fWQ9Xf/v1i3PrAPggB4P3tdAoJspVO2fgBpv+9SQxUs +F+/CuPvoGgJW+FbJZyQpX+Q/2rR0CQbzRKZ1D2ByEWmOMu8J9114OSfNYkSuvYFjyfv z9xeCrAIJaI0AcRnsRNRRjQIBkuRoni2RxjwxbPH2e0TGMMEwwthTefDI3jcRZkHljza jbpA== X-Gm-Message-State: AOAM531WFDXS7igwKNU/qpG9eNecF5NzFiGP4pE5pr0p2IQ3vr3QCI6f wG9/H73TKMhKKRm8zhG9iSEdITzlYgWoecpz0NllCw/mbhfnEXDVV8v6jFXLsIGZnEQYskm11wR C4GNZHbKHm9mk0wXReje87KnKh74= X-Received: by 2002:a05:600c:1f19:: with SMTP id bd25mr15829323wmb.75.1637141281306; Wed, 17 Nov 2021 01:28:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJxaDTV+6aHsMLuzIDY2bbJIxOX2vm9dyzDJhrihA4osfWwyGWumuLEHjZwhC1n8gEki1v7Dn61uRsHRD9iWZm8= X-Received: by 2002:a05:600c:1f19:: with SMTP id bd25mr15829291wmb.75.1637141281059; Wed, 17 Nov 2021 01:28:01 -0800 (PST) MIME-Version: 1.0 References: <720dcf79314acca1a78fae56d478cc851952149d.1637084492.git.christophe.leroy@csgroup.eu> In-Reply-To: <720dcf79314acca1a78fae56d478cc851952149d.1637084492.git.christophe.leroy@csgroup.eu> From: Andreas Gruenbacher Date: Wed, 17 Nov 2021 10:27:49 +0100 Message-ID: Subject: Re: [PATCH] gup: Avoid multiple user access locking/unlocking in fault_in_{read/write}able To: Christophe Leroy Cc: Andrew Morton , LKML , Linux-MM X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 703461052981 X-Stat-Signature: w3xodswz9squhd9t57k8kspwmqogfwor Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ata4857Y; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf31.hostedemail.com: domain of agruenba@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=agruenba@redhat.com X-HE-Tag: 1637141284-252057 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, Nov 16, 2021 at 6:50 PM Christophe Leroy wrote: > fault_in_readable() and fault_in_writeable() perform __get_user() > and __put_user() in a loop, implying multiple user access > locking/unlocking. > > To avoid that, use user access blocks. > > Cc: Andreas Gruenbacher > Signed-off-by: Christophe Leroy Reviewed-by: Andreas Gruenbacher > --- > mm/gup.c | 18 ++++++++++-------- > 1 file changed, 10 insertions(+), 8 deletions(-) > > diff --git a/mm/gup.c b/mm/gup.c > index 2c51e9748a6a..be2a41feec7d 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -1672,21 +1672,22 @@ size_t fault_in_writeable(char __user *uaddr, size_t size) > > if (unlikely(size == 0)) > return 0; > + if (!user_write_access_begin(uaddr, size)) > + return size; > if (!PAGE_ALIGNED(uaddr)) { > - if (unlikely(__put_user(0, uaddr) != 0)) > - return size; > + unsafe_put_user(0, uaddr, out); > uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr); > } > end = (char __user *)PAGE_ALIGN((unsigned long)start + size); > if (unlikely(end < start)) > end = NULL; > while (uaddr != end) { > - if (unlikely(__put_user(0, uaddr) != 0)) > - goto out; > + unsafe_put_user(0, uaddr, out); > uaddr += PAGE_SIZE; > } > > out: > + user_write_access_end(); > if (size > uaddr - start) > return size - (uaddr - start); > return 0; > @@ -1771,21 +1772,22 @@ size_t fault_in_readable(const char __user *uaddr, size_t size) > > if (unlikely(size == 0)) > return 0; > + if (!user_read_access_begin(uaddr, size)) > + return size; > if (!PAGE_ALIGNED(uaddr)) { > - if (unlikely(__get_user(c, uaddr) != 0)) > - return size; > + unsafe_get_user(c, uaddr, out); > uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr); > } > end = (const char __user *)PAGE_ALIGN((unsigned long)start + size); > if (unlikely(end < start)) > end = NULL; > while (uaddr != end) { > - if (unlikely(__get_user(c, uaddr) != 0)) > - goto out; > + unsafe_get_user(c, uaddr, out); > uaddr += PAGE_SIZE; > } > > out: > + user_read_access_end(); > (void)c; > if (size > uaddr - start) > return size - (uaddr - start); > -- > 2.31.1 >