Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp826148yba; Thu, 16 May 2019 09:27:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqyp+75N0+atBaJNdKl0c8U/CQgWh9KiSZnW5VwFdGnZZt1PNuttZm6jLPhnwhxQYvug4TwV X-Received: by 2002:a17:902:5066:: with SMTP id f35mr3529887plh.54.1558024058263; Thu, 16 May 2019 09:27:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558024058; cv=none; d=google.com; s=arc-20160816; b=wGN9CUNhK1Psd93nil+iepM36URq1klWnYMBAQYhqfzgK/xQfjUMcUzXqdnXmyrUFT Oll/iazlsYRJBnjrC2ZRZFLW71FypizaRDUJu4ye4nCrioqhcVi8ma8Fjz2LLN3KyN7i SMHC0g29xIYaZOv+YOdJ/soEY5JYEFvWEuyq02pgR3NvDEKoCnbgohWdbkfj2xgUU4t1 dvjO2VMrs+vn2XnZql2DZZADYCBaFmItnE3sRpSxB+/tpkISVtH2pKY4VARxSZrX4CLz JHs5AWBAuZSxslj0cu5jU+vKWPub5MViSQg5H8RsbRliwowZPz7r53qwy9p/YMU/pOjk EpVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=aF7z4HMLUnakhny7k0GasKk7IF4REIlPL6fyWISpTgk=; b=xURSsA0Q0HW/4hGQOxXmiLzjLDV8VqhFYMtnDcRVlFmJ8z4BigekJNDN0nTZeo9fm1 NGd242ziv3t7NTFCE3dzWfb6wO1Rw2rfPENEVk6VX5bjb5GoIRuWqtl2Vbxw4jnl3ESD hwTYeThkqkY81g0WrWBckeWTxcWFYi4la8h1aKSoD3V//DagEHdedugQog0CYkYP5d/3 pTRD80DD76S9isW9ZIFGpTw2VNV3lAOcOLxFip+tUuU/J9F6BUt6go+M713UrKIz7fzw fwirNa0UHPzM5K95uckv6oe9fj8lQpfs1ek3GN3Sof/lINP8B/4d/mYc0W+bTLQd4uAn hzDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="tNqI2/n1"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r25si314033pgv.333.2019.05.16.09.27.22; Thu, 16 May 2019 09:27:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="tNqI2/n1"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726785AbfEPQ0C (ORCPT + 99 others); Thu, 16 May 2019 12:26:02 -0400 Received: from mail-vs1-f65.google.com ([209.85.217.65]:38575 "EHLO mail-vs1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726635AbfEPQ0C (ORCPT ); Thu, 16 May 2019 12:26:02 -0400 Received: by mail-vs1-f65.google.com with SMTP id x184so1688344vsb.5 for ; Thu, 16 May 2019 09:26:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=aF7z4HMLUnakhny7k0GasKk7IF4REIlPL6fyWISpTgk=; b=tNqI2/n1/KRWaJC+/CpSW6dpIqwdsMYCdLezQXGYWXVbirGLs2g6o5eFI8o7yn3+fy zoP1qOF3qa/Ft/+eb6VML5ISPaIJxdHsZahgMeuB3cDrPgk5NlWNVi7VcgXobHzL37vA VYT4v1aRcr5tm9LBwLpSLP/+yE6YIGXDhlwaQiUexHLdFnVcVGcGZ6nZTw/5Mo+yHzcc HPFl2Juc+RnIB0MPzP0JfxkaEtrwWJK/Bnsv08I8r221StpqU1iDcSBXMUP/xw7YAinb T5+IC4xAPzZyHwQmvh11vFQWFg1Tz8fszmCRdto1rJaDjf1gPy1nHNTLHjuSoImf4/si jdng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=aF7z4HMLUnakhny7k0GasKk7IF4REIlPL6fyWISpTgk=; b=G4itPURbCFfoZR1SKfivRh3kfpMztw/q6z2GVaSyIhjbQ7AoXgE2fwHCdJOvJYmUdD 8wKA5UWdbqYpBXCs6vD/OnowBWW/o+LOFmhL0iJfZHMnGUaMBV+UAh1KeN9wq7x2EyAe 7MC8cyE9NNznJAgyKoSNz/AKM7VjMhXb4UkZsECsIdY4DoBq6i7WloSZFpX0zKclgFGx 3vRiXIBUn0WPea945gfZlzISLRYCjfFuaKab4ip7WDE/pwMRCCrf2ybuQ/o1XUs4x3J+ OjgaXgt+3h2cWgBGkRpAAXSYxp0H7RjiX0xF39EMqrkPZFfkLk1z6SrLyNpNedyhi7+5 HQCA== X-Gm-Message-State: APjAAAXoFi8qpIWj5+rL/ug4n1RG3h/U5wpv3KbBBzN+JT/sjbq7+513 +uxS+tPlKTNYqig3/w2fyiFXw0QnY9FkAZiB74o= X-Received: by 2002:a67:f6c4:: with SMTP id v4mr144463vso.182.1558023960664; Thu, 16 May 2019 09:26:00 -0700 (PDT) MIME-Version: 1.0 References: <1557844195-18882-1-git-send-email-rppt@linux.ibm.com> In-Reply-To: <1557844195-18882-1-git-send-email-rppt@linux.ibm.com> From: Andrei Vagin Date: Thu, 16 May 2019 09:25:49 -0700 Message-ID: Subject: Re: [PATCH] mm/gup: continue VM_FAULT_RETRY processing event for pre-faults To: Mike Rapoport Cc: Andrew Morton , Andrea Arcangeli , linux-mm@kvack.org, LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 14, 2019 at 7:32 AM Mike Rapoport wrote: > > When get_user_pages*() is called with pages = NULL, the processing of > VM_FAULT_RETRY terminates early without actually retrying to fault-in all > the pages. > > If the pages in the requested range belong to a VMA that has userfaultfd > registered, handle_userfault() returns VM_FAULT_RETRY *after* user space > has populated the page, but for the gup pre-fault case there's no actual > retry and the caller will get no pages although they are present. > > This issue was uncovered when running post-copy memory restore in CRIU > after commit d9c9ce34ed5c ("x86/fpu: Fault-in user stack if > copy_fpstate_to_sigframe() fails"). > > After this change, the copying of FPU state to the sigframe switched from > copy_to_user() variants which caused a real page fault to get_user_pages() > with pages parameter set to NULL. > > In post-copy mode of CRIU, the destination memory is managed with > userfaultfd and lack of the retry for pre-fault case in get_user_pages() > causes a crash of the restored process. > > Making the pre-fault behavior of get_user_pages() the same as the "normal" > one fixes the issue. > Tested-by: Andrei Vagin https://travis-ci.org/avagin/linux/builds/533184940 > Fixes: d9c9ce34ed5c ("x86/fpu: Fault-in user stack if copy_fpstate_to_sigframe() fails") > Signed-off-by: Mike Rapoport > --- > mm/gup.c | 15 ++++++++------- > 1 file changed, 8 insertions(+), 7 deletions(-) > > diff --git a/mm/gup.c b/mm/gup.c > index 91819b8..c32ae5a 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -936,10 +936,6 @@ static __always_inline long __get_user_pages_locked(struct task_struct *tsk, > BUG_ON(ret >= nr_pages); > } > > - if (!pages) > - /* If it's a prefault don't insist harder */ > - return ret; > - > if (ret > 0) { > nr_pages -= ret; > pages_done += ret; > @@ -955,8 +951,12 @@ static __always_inline long __get_user_pages_locked(struct task_struct *tsk, > pages_done = ret; > break; > } > - /* VM_FAULT_RETRY triggered, so seek to the faulting offset */ > - pages += ret; > + /* > + * VM_FAULT_RETRY triggered, so seek to the faulting offset. > + * For the prefault case (!pages) we only update counts. > + */ > + if (likely(pages)) > + pages += ret; > start += ret << PAGE_SHIFT; > > /* > @@ -979,7 +979,8 @@ static __always_inline long __get_user_pages_locked(struct task_struct *tsk, > pages_done++; > if (!nr_pages) > break; > - pages++; > + if (likely(pages)) > + pages++; > start += PAGE_SIZE; > } > if (lock_dropped && *locked) { > -- > 2.7.4 >