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=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,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 E7725C433B4 for ; Wed, 12 May 2021 07:45:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 740CB61432 for ; Wed, 12 May 2021 07:45:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 740CB61432 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 A979E6B0036; Wed, 12 May 2021 03:45:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A1FE76B006E; Wed, 12 May 2021 03:45:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 872906B0070; Wed, 12 May 2021 03:45:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0207.hostedemail.com [216.40.44.207]) by kanga.kvack.org (Postfix) with ESMTP id 6861D6B0036 for ; Wed, 12 May 2021 03:45:10 -0400 (EDT) Received: from smtpin32.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 117048249980 for ; Wed, 12 May 2021 07:45:10 +0000 (UTC) X-FDA: 78131793180.32.FD0FAC7 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf29.hostedemail.com (Postfix) with ESMTP id F2C283C5 for ; Wed, 12 May 2021 07:45:02 +0000 (UTC) Received: by mail-pl1-f181.google.com with SMTP id t21so12101763plo.2 for ; Wed, 12 May 2021 00:45:09 -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=MtvdMrkUbTnK66R9+ui61gQOXaiSAm2MDifZQfuXMEA=; b=Ghui20jCJRybOfpM0mXz6U9UgcW2lkC5vDJKETWh2HZuuJ7qnjwtoV9TV73YbkBzHf lL0KQIkqYfgwdNF+XfP6DHeXf7C9NjzUcLPOAV0sIwnBBLUHLZ+WoKyqzCmcX77l/N7i XFRp6KDhcBmEMSdx8EWwYs9GV3ppz7b+JGC5kVKIzpr9vXEc2RaJB4Vo2qNktxMlb8Q6 FWWCdNWFjO3WCqjqRWYokNE1WfgtomeeBDbvR2BCHmTHQKlZB68tW+FSO/q02A+IWREu DgoauqqLm1LM3sEsMpDIaOCP51HlInYgLy8tVZscOvSBZ3uMbj9hwG41HC8Bqn+ONkJD uErQ== 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=MtvdMrkUbTnK66R9+ui61gQOXaiSAm2MDifZQfuXMEA=; b=a3PHjfc0QdAIgGXGZskuFPuvue9GONVRodT0f8ynDCQHnA6IyeuXgPNw1Pw2qaZIS7 3pGNcIl9hGkx3B60tV5JdtNhYqpqVI9LzR0qRXQFMUwdN81NdyVwNL6uZVt6SNChyf4k gp3suXpEjv2VxxCjKRbn/hnT0NPC7y8an+d6+bqZW6RDH/jchqyBNQpJYuZpwSU57rtP Vp3bwFyiL9JTmstdT0ejKvdX9xrAjy3yg4qYVVJjm3IlNKbpdpZ3um8eXAyDPm9oJrbU k1E6yIcaLGKgg+WzvR5K/iwlUrGdHwMSupszqO5QfqRkAJ3XTlSRqYlLhAOHZ/gq4WMy inYQ== X-Gm-Message-State: AOAM530ywZOfRCui59H+zGUTE1eeyYi5uKI30Ara0i0cqhu56vTZml/B Cur8cZ4NQv18uU86BWUklaIMnK4UE/fQ5itw30IhTg== X-Google-Smtp-Source: ABdhPJzIRuma7toXotpPxHLNyDtJU0KEDLYsuq3GkR3RGgIYm+jdCZky96AE9F2jO3wRD2MHIEYkGmHQjYf7ydtxoag= X-Received: by 2002:a17:902:7c85:b029:ed:893d:ec7c with SMTP id y5-20020a1709027c85b02900ed893dec7cmr33237150pll.82.1620805508451; Wed, 12 May 2021 00:45:08 -0700 (PDT) MIME-Version: 1.0 References: <20210512065813.89270-1-almasrymina@google.com> In-Reply-To: <20210512065813.89270-1-almasrymina@google.com> From: Mina Almasry Date: Wed, 12 May 2021 00:44:57 -0700 Message-ID: Subject: Re: [PATCH] mm, hugetlb: fix resv_huge_pages underflow on UFFDIO_COPY To: Andrew Morton , Linux-MM , open list Cc: Mike Kravetz , Axel Rasmussen , Peter Xu Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: F2C283C5 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=Ghui20jC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of almasrymina@google.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=almasrymina@google.com X-Rspamd-Server: rspam03 X-Stat-Signature: imdm6ygpd7tx8dojcdgqxhx1td793nz7 Received-SPF: none (google.com>: No applicable sender policy available) receiver=imf29; identity=mailfrom; envelope-from=""; helo=mail-pl1-f181.google.com; client-ip=209.85.214.181 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620805502-477700 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, May 11, 2021 at 11:58 PM Mina Almasry wrote: > > When hugetlb_mcopy_atomic_pte() is called with: > - mode==MCOPY_ATOMIC_NORMAL and, > - we already have a page in the page cache corresponding to the > associated address, > > We will allocate a huge page from the reserves, and then fail to insert it > into the cache and return -EEXIST. > > In this case, we need to return -EEXIST without allocating a new page as > the page already exists in the cache. Allocating the extra page causes > the resv_huge_pages to underflow temporarily until the extra page is > freed. > > Also, add the warning so that future similar instances of resv_huge_pages > underflowing will be caught. > > Also, minor drive-by cleanups to this code path: > - pagep is an out param never set by calling code, so delete code > assuming there could be a valid page in there. > - use hugetlbfs_pagecache_page() instead of repeating its > implementation. > > Tested using: > ./tools/testing/selftests/vm/userfaultfd hugetlb_shared 1024 200 \ > /mnt/huge > > Test passes, and dmesg shows no underflow warnings. > > Signed-off-by: Mina Almasry > Cc: Mike Kravetz > Cc: Axel Rasmussen > Cc: Peter Xu > > --- > mm/hugetlb.c | 33 ++++++++++++++++++++------------- > 1 file changed, 20 insertions(+), 13 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 629aa4c2259c..40f4ad1bca29 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -1165,6 +1165,7 @@ static struct page *dequeue_huge_page_vma(struct hstate *h, > page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask); > if (page && !avoid_reserve && vma_has_reserves(vma, chg)) { > SetHPageRestoreReserve(page); > + WARN_ON_ONCE(!h->resv_huge_pages); > h->resv_huge_pages--; > } > > @@ -4868,30 +4869,39 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, > struct page **pagep) > { > bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE); > - struct address_space *mapping; > - pgoff_t idx; > + struct hstate *h = hstate_vma(dst_vma); > + struct address_space *mapping = dst_vma->vm_file->f_mapping; > + pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr); > unsigned long size; > int vm_shared = dst_vma->vm_flags & VM_SHARED; > - struct hstate *h = hstate_vma(dst_vma); > pte_t _dst_pte; > spinlock_t *ptl; > - int ret; > + int ret = -ENOMEM; > struct page *page; > int writable; > > - mapping = dst_vma->vm_file->f_mapping; > - idx = vma_hugecache_offset(h, dst_vma, dst_addr); > + /* Out parameter. */ > + WARN_ON(*pagep); > > if (is_continue) { > ret = -EFAULT; > - page = find_lock_page(mapping, idx); > + page = hugetlbfs_pagecache_page(h, dst_vma, dst_addr); > if (!page) > goto out; > - } else if (!*pagep) { > - ret = -ENOMEM; > + } else { > + /* If a page already exists, then it's UFFDIO_COPY for > + * a non-missing case. Return -EEXIST. > + */ > + if (hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { > + ret = -EEXIST; > + goto out; > + } > + > page = alloc_huge_page(dst_vma, dst_addr, 0); > - if (IS_ERR(page)) > + if (IS_ERR(page)) { > + ret = -ENOMEM; > goto out; > + } > > ret = copy_huge_page_from_user(page, > (const void __user *) src_addr, > @@ -4904,9 +4914,6 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, > /* don't free the page */ > goto out; > } > - } else { > - page = *pagep; > - *pagep = NULL; > } > > /* > -- > 2.31.1.607.g51e8a6a459-goog I just realized I missed CCing Andrew and the mailing lists to this patch's review. I'll collect review comments from folks and send a v2 to the correct folks and mailing lists.