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 X-Spam-Level: X-Spam-Status: No, score=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A5FEC433B4 for ; Wed, 12 May 2021 21:53:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7FC3F61417 for ; Wed, 12 May 2021 21:53:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7FC3F61417 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B9A506B0070; Wed, 12 May 2021 17:53:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B48956B0071; Wed, 12 May 2021 17:53:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E9D66B0072; Wed, 12 May 2021 17:53:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0246.hostedemail.com [216.40.44.246]) by kanga.kvack.org (Postfix) with ESMTP id 6A16D6B0070 for ; Wed, 12 May 2021 17:53:07 -0400 (EDT) Received: from smtpin34.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id F1C52180357CD for ; Wed, 12 May 2021 21:53:06 +0000 (UTC) X-FDA: 78133929972.34.66DD5CE Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf06.hostedemail.com (Postfix) with ESMTP id B4FB3C0007EF for ; Wed, 12 May 2021 21:53:06 +0000 (UTC) Received: by mail-pf1-f174.google.com with SMTP id e19so1509379pfv.3 for ; Wed, 12 May 2021 14:53:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=31Caylk6NgplyOdEHmBs90YF+DyIdObUGOQdNgbTVPU=; b=khNY1wUPCnx6s/b1o0E8dyvbVcj5ruewQNunqDgsQAd+jlQeIkT4piAjt5QNFwxvnp BbIhTImXu1OkMhFGW++qgklR8ei2F4E5faPIekzMSxQx8mU+/ky0EJAuOG1V3NuA06u8 uuAcR5NbTxEzyOzGjgxj8jexzp0Ov6f8HrZwhdVnfu7jFF0TyjF1djzyut3U41mCC1Ny U/ZWYaI2SI/FnalW+MWF1sfWNH1IoJadHWQVWnbkPyDJvQM/UDFxUq74yCoceZKH+zH/ UkomqvAv+2DvOmYSRnD+qysQI1/7KDT+ekUbgYgzyiDmBqUwc9YpbfCkv1wDufeIG19E 07xA== 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=31Caylk6NgplyOdEHmBs90YF+DyIdObUGOQdNgbTVPU=; b=lnuez80D695KqEfNDWNPX+L7KeSOtnDTur/SR3ZxhpQrXwVh+AQw4JJG9+SPAdJvUt aSS8CJ29g0otW1ukVKCM4LEH8/V4HGn9rxa0yUCjKOTGeO256G8hcsBJNshyRRXnml8L HOGl2Rg6KCvSIULcs9ObxE/060DAzdhHG1YcS3Y+BzpTY5/0ybCrP24ylL1L6/Jnr+3j MGqUR4v+aiBZkqdqn597KWBB4Bet5R5gDasy0vvjS2oYwzyJ5TSYLjx4jVvWBJnS8jRV r5UFY7nKndkWLt3fDKok4Kj98F4++iCsOV68YslBcg2poIJL/nLB9BhYOUrKaU8XKbaD 5EsQ== X-Gm-Message-State: AOAM530OlvlWm+k51wDKBfciLRuU22J6NMXZCjaQY5az8a/9ezu8l9Ud WtqeIu0BFXoB7urbIo3HaGL/YiFgv2ktOv3rComKRg== X-Google-Smtp-Source: ABdhPJyYMdgdxgQl4RO7FDyTSI8sljhmYktWge1iSEPO0dv6Q3L3L3hfZpTE078xVY4h180yyGL6FjvVnrXGrRocj4o= X-Received: by 2002:a17:90b:2393:: with SMTP id mr19mr728085pjb.24.1620856385292; Wed, 12 May 2021 14:53:05 -0700 (PDT) MIME-Version: 1.0 References: <20210512065813.89270-1-almasrymina@google.com> <6a4678a2-c6d1-cf27-cd69-1b49349a3271@oracle.com> In-Reply-To: <6a4678a2-c6d1-cf27-cd69-1b49349a3271@oracle.com> From: Mina Almasry Date: Wed, 12 May 2021 14:52:54 -0700 Message-ID: Subject: Re: [PATCH] mm, hugetlb: fix resv_huge_pages underflow on UFFDIO_COPY To: Mike Kravetz Cc: Peter Xu , Andrew Morton , Linux-MM , open list , Axel Rasmussen Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=khNY1wUP; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of almasrymina@google.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=almasrymina@google.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: B4FB3C0007EF X-Stat-Signature: p1qq5koy478nykxzqk8c9hqymrxwdf48 Received-SPF: none (google.com>: No applicable sender policy available) receiver=imf06; identity=mailfrom; envelope-from=""; helo=mail-pf1-f174.google.com; client-ip=209.85.210.174 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620856386-853738 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 Wed, May 12, 2021 at 2:31 PM Mike Kravetz wrote: > > On 5/12/21 1:14 PM, Peter Xu wrote: > > On Wed, May 12, 2021 at 12:42:32PM -0700, Mina Almasry wrote: > >>>>> @@ -4868,30 +4869,39 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, > >>>>> + WARN_ON(*pagep); > >>>> > >>>> I don't think this warning works, because we do set *pagep, in the > >>>> copy_huge_page_from_user failure case. In that case, the following > >>>> happens: > >>>> > >>>> 1. We set *pagep, and return immediately. > >>>> 2. Our caller notices this particular error, drops mmap_lock, and then > >>>> calls us again with *pagep set. > >>>> > >>>> In this path, we're supposed to just re-use this existing *pagep > >>>> instead of allocating a second new page. > >>>> > >>>> I think this also means we need to keep the "else" case where *pagep > >>>> is set below. > >>>> > >>> > >>> +1 to Peter's comment. > >>> > > Apologies to Axel (and Peter) as that comment was from Axel. > > >> > >> Gah, sorry about that. I'll fix in v2. > > > > I have a question regarding v1: how do you guarantee huge_add_to_page_cache() > > won't fail again even if checked before page alloc? Say, what if the page > > cache got inserted after hugetlbfs_pagecache_present() (which is newly added in > > your v1) but before huge_add_to_page_cache()? > > In the caller (__mcopy_atomic_hugetlb) we obtain the hugetlb fault mutex > before calling this routine. This should prevent changes to the cache > while in the routine. > > However, things get complicated in the case where copy_huge_page_from_user > fails. In this case, we will return to the caller which will drop mmap_lock > and the hugetlb fault mutex before doing the copy. After dropping the > mutex, someone could populate the cache. This would result in the same > situation where two reserves are 'temporarily' consumed for the same > mapping offset. By the time we get to the second call to > hugetlb_mcopy_atomic_pte where the previously allocated page is passed > in, it is too late. > Thanks. I tried locally to allocate a page, then add it into the cache, *then* copy its contents (dropping that lock if that fails). That also has the test passing, but I'm not sure if I'm causing a fire somewhere else by having a page in the cache that has uninitialized contents. The only other code that checks the cache seems to be the hugetlb_fault/hugetlb_cow code. I'm reading that code to try to understand if I'm breaking that code doing this. > -- > Mike Kravetz