From patchwork Tue Feb 9 03:02:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12077093 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=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 58BC2C433E0 for ; Tue, 9 Feb 2021 03:02:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D774E64E9D for ; Tue, 9 Feb 2021 03:02:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D774E64E9D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 64E226B006C; Mon, 8 Feb 2021 22:02:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 601208D0003; Mon, 8 Feb 2021 22:02:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 47CE48D0002; Mon, 8 Feb 2021 22:02:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0108.hostedemail.com [216.40.44.108]) by kanga.kvack.org (Postfix) with ESMTP id 297526B006C for ; Mon, 8 Feb 2021 22:02:38 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id ED9BA824805A for ; Tue, 9 Feb 2021 03:02:37 +0000 (UTC) X-FDA: 77797231554.18.ghost59_3c12be727603 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin18.hostedemail.com (Postfix) with ESMTP id D096B100EDBE8 for ; Tue, 9 Feb 2021 03:02:37 +0000 (UTC) X-HE-Tag: ghost59_3c12be727603 X-Filterd-Recvd-Size: 7315 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Tue, 9 Feb 2021 03:02:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612839756; 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=tR/UuXaqBaROltUbV2RDYpoyLXOK1N4qjZ6UzYcMHb0=; b=CHC/1F3HrammfUpdUB7R3DEjtwSO6j1v3IchYVEOZiQuJKHUz03LJqTHDZFIE0AHHrvfZ7 Wk2lkjm5P5ep2IVS8spk96GU7bATWLjgKxnWRRPhHZgQrj4onwF0nxzsIe+Njr0yX2fuRX i9lOFeAlK2emnSMg8n9akE/mlxA1adQ= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-107-REtwhMZuOG6bE5kl8jUlsw-1; Mon, 08 Feb 2021 22:02:35 -0500 X-MC-Unique: REtwhMZuOG6bE5kl8jUlsw-1 Received: by mail-qv1-f72.google.com with SMTP id u8so12225843qvm.5 for ; Mon, 08 Feb 2021 19:02:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tR/UuXaqBaROltUbV2RDYpoyLXOK1N4qjZ6UzYcMHb0=; b=TDFRFMh0yFasGchMevqH3HZtE3zq/ophpXjuGpfvA/wmM5U5CMGwKmijipxc9RLkwg FiqCW3x5xrnv8XcMD9swAbUliEGNYGRYmI08Rbb+C9jQDV0Z5T3K1tB/4GXWoOWZqqX/ 8yIwn/j4Qc7YAPoqBidv/INg49qd66SYjfrLQmvDabzS5BLxMYi/9yCLpq0F9hTsXCA1 Eh1d8B+6JeFH4BfA6EG8u5JDhkosHUJC1ktKojb4DCqPOF7+HFwWDNmrYMmXAbqQM+Ee nDStt6DxtP7IzLpz7VmMduXAxHgUWzwYMCGf+lICk7uZffyleEd1kuG54CheapD9Uyu5 g2Vg== X-Gm-Message-State: AOAM533PGB4FmDx1XY9TJu7JUVHqRvoWC2CoR39ibhkD4ay+w5MULS82 rprxxPLXlGpVMrgxWxmazJ07rxT02qjLXBFofA+EiIfUk5whS9nDVG5AC0sY9GFINOrSaK/xUGk c6ZOageUgmD41gF8DmncC/aUwa0DXer+jhtsS++qBdbrvaMKMVgYY1CX+i97d X-Received: by 2002:a0c:a905:: with SMTP id y5mr19341949qva.55.1612839754354; Mon, 08 Feb 2021 19:02:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJzLHspfWpI6dEM2H1FKYzO8yoaXtFXKAxy9ewDvI5IRLUI759Dc6OnoCgk4yPZj+ynYz1yRpQ== X-Received: by 2002:a0c:a905:: with SMTP id y5mr19341901qva.55.1612839753827; Mon, 08 Feb 2021 19:02:33 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id z20sm17078830qki.93.2021.02.08.19.02.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Feb 2021 19:02:33 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Linus Torvalds , Kirill Tkhai , Mike Rapoport , David Gibson , Kirill Shutemov , Christoph Hellwig , Miaohe Lin , Gal Pressman , Jason Gunthorpe , Jann Horn , peterx@redhat.com, Jan Kara , Wei Zhang , Mike Kravetz , Andrea Arcangeli , Andrew Morton Subject: [PATCH v4 1/5] hugetlb: Dedup the code to add a new file_region Date: Mon, 8 Feb 2021 22:02:25 -0500 Message-Id: <20210209030229.84991-2-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210209030229.84991-1-peterx@redhat.com> References: <20210209030229.84991-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: Introduce hugetlb_resv_map_add() helper to add a new file_region rather than duplication the similar code twice in add_reservation_in_range(). Reviewed-by: Mike Kravetz Reviewed-by: Miaohe Lin Signed-off-by: Peter Xu --- mm/hugetlb.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 6ef278ecf7ff..ec8e29c805fe 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -331,6 +331,24 @@ static void coalesce_file_region(struct resv_map *resv, struct file_region *rg) } } +static inline long +hugetlb_resv_map_add(struct resv_map *map, struct file_region *rg, long from, + long to, struct hstate *h, struct hugetlb_cgroup *cg, + long *regions_needed) +{ + struct file_region *nrg; + + if (!regions_needed) { + nrg = get_file_region_entry_from_cache(map, from, to); + record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg); + list_add(&nrg->link, rg->link.prev); + coalesce_file_region(map, nrg); + } else + *regions_needed += 1; + + return to - from; +} + /* * Must be called with resv->lock held. * @@ -346,7 +364,7 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, long add = 0; struct list_head *head = &resv->regions; long last_accounted_offset = f; - struct file_region *rg = NULL, *trg = NULL, *nrg = NULL; + struct file_region *rg = NULL, *trg = NULL; if (regions_needed) *regions_needed = 0; @@ -375,18 +393,11 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, /* Add an entry for last_accounted_offset -> rg->from, and * update last_accounted_offset. */ - if (rg->from > last_accounted_offset) { - add += rg->from - last_accounted_offset; - if (!regions_needed) { - nrg = get_file_region_entry_from_cache( - resv, last_accounted_offset, rg->from); - record_hugetlb_cgroup_uncharge_info(h_cg, h, - resv, nrg); - list_add(&nrg->link, rg->link.prev); - coalesce_file_region(resv, nrg); - } else - *regions_needed += 1; - } + if (rg->from > last_accounted_offset) + add += hugetlb_resv_map_add(resv, rg, + last_accounted_offset, + rg->from, h, h_cg, + regions_needed); last_accounted_offset = rg->to; } @@ -394,17 +405,9 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, /* Handle the case where our range extends beyond * last_accounted_offset. */ - if (last_accounted_offset < t) { - add += t - last_accounted_offset; - if (!regions_needed) { - nrg = get_file_region_entry_from_cache( - resv, last_accounted_offset, t); - record_hugetlb_cgroup_uncharge_info(h_cg, h, resv, nrg); - list_add(&nrg->link, rg->link.prev); - coalesce_file_region(resv, nrg); - } else - *regions_needed += 1; - } + if (last_accounted_offset < t) + add += hugetlb_resv_map_add(resv, rg, last_accounted_offset, + t, h, h_cg, regions_needed); VM_BUG_ON(add < 0); return add; From patchwork Tue Feb 9 03:02:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12077095 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=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 EA556C433E6 for ; Tue, 9 Feb 2021 03:02:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7580964E9D for ; Tue, 9 Feb 2021 03:02:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7580964E9D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 05C316B006E; Mon, 8 Feb 2021 22:02:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F29336B0070; Mon, 8 Feb 2021 22:02:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CDB918D0002; Mon, 8 Feb 2021 22:02:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0204.hostedemail.com [216.40.44.204]) by kanga.kvack.org (Postfix) with ESMTP id B6ED76B006E for ; Mon, 8 Feb 2021 22:02:39 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 89017A2B0 for ; Tue, 9 Feb 2021 03:02:39 +0000 (UTC) X-FDA: 77797231638.09.sky44_57050a327603 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 72428183A39AE for ; Tue, 9 Feb 2021 03:02:39 +0000 (UTC) X-HE-Tag: sky44_57050a327603 X-Filterd-Recvd-Size: 5417 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf12.hostedemail.com (Postfix) with ESMTP for ; Tue, 9 Feb 2021 03:02:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612839758; 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=Co5gWVPolRGWSV6qsroagZOgnBTQI/bBXcBdWtmnAnQ=; b=BXFI8uI09qMQfZbZQNzIqTNgyvqRdd88EHCM0XI/efwN/ayunaOXXi8XwXP0+ahUlg+Mi/ FPiiaHSxprScpzpdjpIhDWOJLBy1t2nJMa4D0jYP/F0PTD1+siXQOKc4ThjdjZXiXaqzqI kWf1rkU+U1DASCjMRGGXzl113i9bPqA= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-167-NZUXUBaWMtCjTD7mdUd1vA-1; Mon, 08 Feb 2021 22:02:37 -0500 X-MC-Unique: NZUXUBaWMtCjTD7mdUd1vA-1 Received: by mail-qt1-f200.google.com with SMTP id p20so11187457qtn.23 for ; Mon, 08 Feb 2021 19:02:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Co5gWVPolRGWSV6qsroagZOgnBTQI/bBXcBdWtmnAnQ=; b=je1V1LdJsgoj7Tu5KudQ3/wAL0i7YSSSw15w4RMOetvNG/3pWXX1579/5lxPsd/ETK BX8N3LFVUIDJWw8Gw9V1zV0JSUfp3rxpYth6b9xBO/D4Nr1gIv9cmcvBiHalMIGDVcNz 3ZJCB9SY2xqVb85wJ+ubtZeEqAX7DS9qI2D2bsWpmmjnw6unVtV5idgG2BX3/JlwvJ6z N5fkYPIfb2nqNV1i2HkTiy3psD98jRbuKLCDHhLPGFxgHpOTl6oAlQ+SokNfB3xkcBLo btVCULOKZR8oLId5frbgh+1PGTiAoUFHiiiMHFMCdY8Li3tEXP6bca0hPH7Vd8e2Nh57 NPnA== X-Gm-Message-State: AOAM5324uCHdNenZU+pxCXYLaGvh65+i8QcrtWr9tMz7wxwfLY1kpUYQ IRPvL64MAW8nqrx1qi1l9RB2c1tgQTpfa0MqYMLedsShB20LEsx7GZ48C3dNcw/nl+BgNLNaEc7 48Z8rHsP9VhYJG2BKnnrQcHsJ8SOIAwT9ZKuqsO9zPldCwxxoHsDiYxbBYsfe X-Received: by 2002:a37:9bcb:: with SMTP id d194mr20914256qke.217.1612839756229; Mon, 08 Feb 2021 19:02:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJz2pULQFQQt0HU+IE5y/rbbIFYnrd2LL3d0zubsXk3enhPFegynpvAbmbfc+/W5MTm1VEszLA== X-Received: by 2002:a37:9bcb:: with SMTP id d194mr20914224qke.217.1612839755945; Mon, 08 Feb 2021 19:02:35 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id z20sm17078830qki.93.2021.02.08.19.02.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Feb 2021 19:02:35 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Linus Torvalds , Kirill Tkhai , Mike Rapoport , David Gibson , Kirill Shutemov , Christoph Hellwig , Miaohe Lin , Gal Pressman , Jason Gunthorpe , Jann Horn , peterx@redhat.com, Jan Kara , Wei Zhang , Mike Kravetz , Andrea Arcangeli , Andrew Morton Subject: [PATCH v4 2/5] hugetlg: Break earlier in add_reservation_in_range() when we can Date: Mon, 8 Feb 2021 22:02:26 -0500 Message-Id: <20210209030229.84991-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210209030229.84991-1-peterx@redhat.com> References: <20210209030229.84991-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: All the regions maintained in hugetlb reserved map is inclusive on "from" but exclusive on "to". We can break earlier even if rg->from==t because it already means no possible intersection. This does not need a Fixes in all cases because when it happens (rg->from==t) we'll not break out of the loop while we should, however the next thing we'd do is still add the last file_region we'd need and quit the loop in the next round. So this change is not a bugfix (since the old code should still run okay iiuc), but we'd better still touch it up to make it logically sane. Reviewed-by: Mike Kravetz Reviewed-by: Miaohe Lin Signed-off-by: Peter Xu --- mm/hugetlb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ec8e29c805fe..71ccec5c3817 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -387,7 +387,7 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, /* When we find a region that starts beyond our range, we've * finished. */ - if (rg->from > t) + if (rg->from >= t) break; /* Add an entry for last_accounted_offset -> rg->from, and From patchwork Tue Feb 9 03:02:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12077097 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=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 4987AC433E0 for ; Tue, 9 Feb 2021 03:02:43 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B477264EAC for ; Tue, 9 Feb 2021 03:02:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B477264EAC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 478DB6B0070; Mon, 8 Feb 2021 22:02:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4513E8D0003; Mon, 8 Feb 2021 22:02:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A6AD8D0002; Mon, 8 Feb 2021 22:02:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0121.hostedemail.com [216.40.44.121]) by kanga.kvack.org (Postfix) with ESMTP id 04F2B6B0070 for ; Mon, 8 Feb 2021 22:02:42 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id CADE31839B98A for ; Tue, 9 Feb 2021 03:02:41 +0000 (UTC) X-FDA: 77797231722.18.0525F29 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 2F8D2136 for ; Tue, 9 Feb 2021 03:02:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612839760; 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=JHImIlBSKN1nSIl0TDY6RA8xaD02NZcbelnnRmoiTro=; b=eIz9VpIe0oyo4zLbpgBwNBOZGKFjVXbZZWCFZ4aZBQFUWVtZmX6quGrIgJXry0i04UtBpH nxG5XY5+X1N97IyAjrqqpY31SocjdWYtYcJj8f15HaFtlS7SdR5JLcEwklsLh0IqdrmQee D73xNSR28Mj1BCI3nR4LBQXR1WQwsyY= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-220-VWYeOd_LNHesYiDd6PHh5g-1; Mon, 08 Feb 2021 22:02:38 -0500 X-MC-Unique: VWYeOd_LNHesYiDd6PHh5g-1 Received: by mail-qk1-f197.google.com with SMTP id p185so14691933qkc.9 for ; Mon, 08 Feb 2021 19:02:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JHImIlBSKN1nSIl0TDY6RA8xaD02NZcbelnnRmoiTro=; b=RgQmDslW9AxkrW5PIUw2o6IDr3uYErKH2Qa46pqTAJhpQIF0M6OTMFKXwnPlMNecJ0 H5J2hjjwLI4g7m7Rob0fIX/ys/Tj5H8vv1HepgyK4i/CZGxKqKPsPHrKElNkkhKVmOWL POeoa6Uswt8CcjpoxfzbVfePJx0zq6wNfDUBYuZM9kmDOz2IoHj0HVZ6AnqvS/ZWMVCN j9p4pKZPCjGrZcO7DIeQ8RTPUOXasXMs38qyIsSgip4TTQfJsAT+kV7EdGlfuQ+VKhQL RHqb+ENppyguDtfRwYcYtnuVRpBkFsP8w44UKknNIybCMWzQDtOEkFL5rTXuCht79coh KLQw== X-Gm-Message-State: AOAM532RTpE7YRd/x/8/U7bA8TuyWGYnbOTDp3x84vJtv/eeJPiKmEpq 3yM1/70fH8DKZDQj6l3Xc4IO0y49Q57ZbEVvAMF4aLCkSptJuDezkQnUj4YHGWvH7nEtoRIuahL I+YYwV5N+Or4wC8gDtccD3HjtjN9WkS50IEF++25vtnAk2RjvYaIQ1FKt2MUU X-Received: by 2002:aed:33c4:: with SMTP id v62mr16790075qtd.377.1612839758063; Mon, 08 Feb 2021 19:02:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJxni6VP6PqQbjOPxHYa0azVKnNKN6eiP3K3PbRfnHpW8VhUYbjJ54KUIJwwcOlJNl9Vcexj1Q== X-Received: by 2002:aed:33c4:: with SMTP id v62mr16790043qtd.377.1612839757771; Mon, 08 Feb 2021 19:02:37 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id z20sm17078830qki.93.2021.02.08.19.02.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Feb 2021 19:02:37 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Linus Torvalds , Kirill Tkhai , Mike Rapoport , David Gibson , Kirill Shutemov , Christoph Hellwig , Miaohe Lin , Gal Pressman , Jason Gunthorpe , Jann Horn , peterx@redhat.com, Jan Kara , Wei Zhang , Mike Kravetz , Andrea Arcangeli , Andrew Morton Subject: [PATCH v4 3/5] mm: Introduce page_needs_cow_for_dma() for deciding whether cow Date: Mon, 8 Feb 2021 22:02:27 -0500 Message-Id: <20210209030229.84991-4-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210209030229.84991-1-peterx@redhat.com> References: <20210209030229.84991-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: xtrcmft6kmqxy64jbub5c68hpc1bh9ib X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2F8D2136 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf04; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=63.128.21.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1612839760-557658 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: We've got quite a few places (pte, pmd, pud) that explicitly checked against whether we should break the cow right now during fork(). It's easier to provide a helper, especially before we work the same thing on hugetlbfs. Since we'll reference is_cow_mapping() in mm.h, move it there too. Actually it suites mm.h more since internal.h is mm/ only, but mm.h is exported to the whole kernel. With that we should expect another patch to use is_cow_mapping() whenever we can across the kernel since we do use it quite a lot but it's always done with raw code against VM_* flags. Signed-off-by: Peter Xu Reviewed-by: Jason Gunthorpe --- include/linux/mm.h | 21 +++++++++++++++++++++ mm/huge_memory.c | 8 ++------ mm/internal.h | 5 ----- mm/memory.c | 8 +------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 904e2517cd45..2e555d57631f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1314,6 +1314,27 @@ static inline bool page_maybe_dma_pinned(struct page *page) GUP_PIN_COUNTING_BIAS; } +static inline bool is_cow_mapping(vm_flags_t flags) +{ + return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; +} + +/* + * This should most likely only be called during fork() to see whether we + * should break the cow immediately for a page on the src mm. + */ +static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma, + struct page *page) +{ + if (!is_cow_mapping(vma->vm_flags)) + return false; + + if (!atomic_read(&vma->vm_mm->has_pinned)) + return false; + + return page_maybe_dma_pinned(page); +} + #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP) #define SECTION_IN_PAGE_FLAGS #endif diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 987cf5e4cf90..57f5c7d3a328 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1097,9 +1097,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, * best effort that the pinned pages won't be replaced by another * random page during the coming copy-on-write. */ - if (unlikely(is_cow_mapping(vma->vm_flags) && - atomic_read(&src_mm->has_pinned) && - page_maybe_dma_pinned(src_page))) { + if (unlikely(page_needs_cow_for_dma(vma, src_page))) { pte_free(dst_mm, pgtable); spin_unlock(src_ptl); spin_unlock(dst_ptl); @@ -1211,9 +1209,7 @@ int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, } /* Please refer to comments in copy_huge_pmd() */ - if (unlikely(is_cow_mapping(vma->vm_flags) && - atomic_read(&src_mm->has_pinned) && - page_maybe_dma_pinned(pud_page(pud)))) { + if (unlikely(page_needs_cow_for_dma(vma, pud_page(pud)))) { spin_unlock(src_ptl); spin_unlock(dst_ptl); __split_huge_pud(vma, src_pud, addr); diff --git a/mm/internal.h b/mm/internal.h index 8e9c660f33ca..a24847e48081 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -296,11 +296,6 @@ static inline unsigned int buddy_order(struct page *page) */ #define buddy_order_unsafe(page) READ_ONCE(page_private(page)) -static inline bool is_cow_mapping(vm_flags_t flags) -{ - return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; -} - /* * These three helpers classifies VMAs for virtual memory accounting. */ diff --git a/mm/memory.c b/mm/memory.c index 9d68a2340589..a935262bfd3d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -809,12 +809,8 @@ copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, struct page **prealloc, pte_t pte, struct page *page) { - struct mm_struct *src_mm = src_vma->vm_mm; struct page *new_page; - if (!is_cow_mapping(src_vma->vm_flags)) - return 1; - /* * What we want to do is to check whether this page may * have been pinned by the parent process. If so, @@ -828,9 +824,7 @@ copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma * the page count. That might give false positives for * for pinning, but it will work correctly. */ - if (likely(!atomic_read(&src_mm->has_pinned))) - return 1; - if (likely(!page_maybe_dma_pinned(page))) + if (likely(!page_needs_cow_for_dma(src_vma, page))) return 1; new_page = *prealloc; From patchwork Tue Feb 9 03:02:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12077099 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=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 10891C433E6 for ; Tue, 9 Feb 2021 03:02:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8EBC664E9D for ; Tue, 9 Feb 2021 03:02:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8EBC664E9D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DCA216B0071; Mon, 8 Feb 2021 22:02:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D58888D0003; Mon, 8 Feb 2021 22:02:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B0A718D0002; Mon, 8 Feb 2021 22:02:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0131.hostedemail.com [216.40.44.131]) by kanga.kvack.org (Postfix) with ESMTP id 9A4B96B0071 for ; Mon, 8 Feb 2021 22:02:43 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5F7F018391D6B for ; Tue, 9 Feb 2021 03:02:43 +0000 (UTC) X-FDA: 77797231806.14.brake59_220df1827603 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 2F15618150768 for ; Tue, 9 Feb 2021 03:02:43 +0000 (UTC) X-HE-Tag: brake59_220df1827603 X-Filterd-Recvd-Size: 7817 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Tue, 9 Feb 2021 03:02:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612839761; 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=45ThGTqfFb6/yx2k3kPs+btJTG2mPFONj9RwvqJLKYY=; b=BfhE3PHxs8VNKvgC3LBTL5+4Ipev0Pts1mYK4BOIqMSjVzg2cHJvJsgoey5q+rNqloj5Ym I5GzDk6VHYp41LivEFtelF6h3iJz49fVxyB0OHWlAQ+rs2vGhOgYpntUYrZK9JzWY9T9qF OwLzFKBrl/q1T6B5Q5KSzxHZohUzf3M= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-60-gBmExIdwPEymLd-yifzjqw-1; Mon, 08 Feb 2021 22:02:40 -0500 X-MC-Unique: gBmExIdwPEymLd-yifzjqw-1 Received: by mail-qt1-f200.google.com with SMTP id h8so11221449qtj.1 for ; Mon, 08 Feb 2021 19:02:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=45ThGTqfFb6/yx2k3kPs+btJTG2mPFONj9RwvqJLKYY=; b=qEfIffTn+y8OX8Hk1RcL+iFJzCyGT8T3Bk3RBDJsR9mEVzdAHGFZcvsdz6CjAukGh+ +T/hMcZEuQ3V3fjUBQVg1VjoyHdipNbYAoG2wB0hIyyekEYGjcm4GEUKjW+r3IFHUt3R H44WJAvemDuwXiZ/oeYnJ90FrLeC+9x1EkkYRVQnZIxlVDyORB3wkRUXHlPaxtOFlwII KZmboXaG96XE8//ZUhyKZcHGMcZf4s5PEzK0cbs+PDlTpFRy09+7KX+IWV8vQHFPv308 W9001ejcKFGuDn9rzTQkSLleeuGHvSqMrfKLDNuOfSN/dwkasWpeB07UXcul76dUCYsn +nHw== X-Gm-Message-State: AOAM530P9Y+KZ29dXuztBGdogkXEc8R0fnR4X7KKFpd47XOrYwRGXdjQ sZOISB6kfS8HBBVml04OTeKRZ9ct1RFTIllkSkDpqT3uCaR+VUdzN3D3GjmaKMADBSDYLVduruv MgO1cnSAdpuGri3SGFInHclU4QhNvAcRp/aFHFr49AKruf0sLyiW9DaR8zP6/ X-Received: by 2002:ac8:1403:: with SMTP id k3mr18504429qtj.266.1612839760022; Mon, 08 Feb 2021 19:02:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJz2c1gqjFwl2EgCFV1jzhiFbIEsvjBLharoixnAKXp/pgq/v39CQuAjuR9IcJNqX5BFJAMTYA== X-Received: by 2002:ac8:1403:: with SMTP id k3mr18504382qtj.266.1612839759707; Mon, 08 Feb 2021 19:02:39 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id z20sm17078830qki.93.2021.02.08.19.02.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Feb 2021 19:02:39 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Linus Torvalds , Kirill Tkhai , Mike Rapoport , David Gibson , Kirill Shutemov , Christoph Hellwig , Miaohe Lin , Gal Pressman , Jason Gunthorpe , Jann Horn , peterx@redhat.com, Jan Kara , Wei Zhang , Mike Kravetz , Andrea Arcangeli , Andrew Morton , VMware Graphics , Roland Scheidegger , David Airlie , Daniel Vetter , Alexey Dobriyan Subject: [PATCH v4 4/5] mm: Use is_cow_mapping() across tree where proper Date: Mon, 8 Feb 2021 22:02:28 -0500 Message-Id: <20210209030229.84991-5-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210209030229.84991-1-peterx@redhat.com> References: <20210209030229.84991-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: After is_cow_mapping() is exported in mm.h, replace some manual checks elsewhere throughout the tree but start to use the new helper. Cc: VMware Graphics Cc: Roland Scheidegger Cc: David Airlie Cc: Daniel Vetter Cc: Mike Kravetz Cc: Alexey Dobriyan Cc: Andrew Morton Signed-off-by: Peter Xu Reviewed-by: Jason Gunthorpe --- drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c | 4 +--- drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c | 2 +- fs/proc/task_mmu.c | 2 -- mm/hugetlb.c | 4 +--- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c b/drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c index 0a900afc66ff..45c9c6a7f1d6 100644 --- a/drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c @@ -500,8 +500,6 @@ vm_fault_t vmw_bo_vm_huge_fault(struct vm_fault *vmf, vm_fault_t ret; pgoff_t fault_page_size; bool write = vmf->flags & FAULT_FLAG_WRITE; - bool is_cow_mapping = - (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; switch (pe_size) { case PE_SIZE_PMD: @@ -518,7 +516,7 @@ vm_fault_t vmw_bo_vm_huge_fault(struct vm_fault *vmf, } /* Always do write dirty-tracking and COW on PTE level. */ - if (write && (READ_ONCE(vbo->dirty) || is_cow_mapping)) + if (write && (READ_ONCE(vbo->dirty) || is_cow_mapping(vma->vm_flags))) return VM_FAULT_FALLBACK; ret = ttm_bo_vm_reserve(bo, vmf); diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c index 3c03b1746661..cb9975889e2f 100644 --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c @@ -49,7 +49,7 @@ int vmw_mmap(struct file *filp, struct vm_area_struct *vma) vma->vm_ops = &vmw_vm_ops; /* Use VM_PFNMAP rather than VM_MIXEDMAP if not a COW mapping */ - if ((vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) != VM_MAYWRITE) + if (!is_cow_mapping(vma->vm_flags)) vma->vm_flags = (vma->vm_flags & ~VM_MIXEDMAP) | VM_PFNMAP; return 0; diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 602e3a52884d..96c1682025f9 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1036,8 +1036,6 @@ struct clear_refs_private { #ifdef CONFIG_MEM_SOFT_DIRTY -#define is_cow_mapping(flags) (((flags) & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE) - static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte) { struct page *page; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 71ccec5c3817..620700f05ff4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3733,15 +3733,13 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, pte_t *src_pte, *dst_pte, entry, dst_entry; struct page *ptepage; unsigned long addr; - int cow; + int cow = is_cow_mapping(vma->vm_flags); struct hstate *h = hstate_vma(vma); unsigned long sz = huge_page_size(h); struct address_space *mapping = vma->vm_file->f_mapping; struct mmu_notifier_range range; int ret = 0; - cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; - if (cow) { mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, src, vma->vm_start, From patchwork Tue Feb 9 03:02:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12077101 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=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 5C562C433DB for ; Tue, 9 Feb 2021 03:02:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id ED31C64E9D for ; Tue, 9 Feb 2021 03:02:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ED31C64E9D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9625A6B0072; Mon, 8 Feb 2021 22:02:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E78F8D0003; Mon, 8 Feb 2021 22:02:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7AD9B8D0002; Mon, 8 Feb 2021 22:02:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0110.hostedemail.com [216.40.44.110]) by kanga.kvack.org (Postfix) with ESMTP id 615916B0072 for ; Mon, 8 Feb 2021 22:02:45 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 2F4C9B7BD for ; Tue, 9 Feb 2021 03:02:45 +0000 (UTC) X-FDA: 77797231890.16.wrist03_1207dd027603 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin16.hostedemail.com (Postfix) with ESMTP id 0F2A7100E6917 for ; Tue, 9 Feb 2021 03:02:45 +0000 (UTC) X-HE-Tag: wrist03_1207dd027603 X-Filterd-Recvd-Size: 8692 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Tue, 9 Feb 2021 03:02:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612839763; 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=m/Cml9fJfOIKMiH17sYbLZNb5fx7uhgkIw+TMGyZimw=; b=HQAOsGPOGMS+wR5L5qHy1FIkDOSgC+fwGoYyxK/AdBJqyX9lf1sYOcM6sCcddBO4XoBo64 839u/Yhl9ufbsLcWxaDdwu4vA8jnmzBjgGLl1vPxwWHJ8T1eTWagAkEFjHsp5DO6SOs/MK 9F0xkznNqywlx3Hy6vpNAc4pe4Fnptg= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-572-a5lZogprNT-54MGS4B3CDQ-1; Mon, 08 Feb 2021 22:02:42 -0500 X-MC-Unique: a5lZogprNT-54MGS4B3CDQ-1 Received: by mail-qv1-f69.google.com with SMTP id m1so12216489qvp.0 for ; Mon, 08 Feb 2021 19:02:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=m/Cml9fJfOIKMiH17sYbLZNb5fx7uhgkIw+TMGyZimw=; b=onBg/SI4prjJXnCh8mNMnOwhAz5vhLdLcXPxdYF/GV3w1wDeu157CVgx0p8KVqDvyH Lh/ELqPWUiMDfN1Nu+GB/zRXp53jX78tTE10RuwPotvDsQ504KeyEtnDy37V7B6TmQVR +LFecgEjSi6FQznBvbmOQBw+9d2p/BTCTkbYmX+ao/6jQzgqM2G1mj5SCHcb1pxs77XY mBVwNQKXKI0BuTJBy5ciY9wta358XOXZPMsoZ4X7WcQkcwTJdriLQ98TG+WScU48GmsU nfqCGqL7AjMtgB2IagSF2SB86hgitIQN3hxmdV0mklt8bkVLDuqIQNU7CS9+ZZyicbGu UZIA== X-Gm-Message-State: AOAM532j25IgqWhClHBIpXYp6gXj/IvOUicdJ7vSH3RpWTkgT6FFkolQ wn5HMW5a2++becNzc407qyGIRKI4jWn1t3NxkmNIvzNzZYCVmD+hqNxCldPIBk8xx786c4rlS34 PGjvEcZuO9DHS67/R5GpyTaZnStqo1gIBhvGOlBAXiiQhJNl1TVQaRAd/tCyJ X-Received: by 2002:a0c:b7a3:: with SMTP id l35mr5881573qve.46.1612839761841; Mon, 08 Feb 2021 19:02:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxIOEctrkm3l5R6SZkaqR8QWfLpvxLeWV7SKUDmzr17+0bv3pMiSOaRSzsWGdMpQzUXQOlGNA== X-Received: by 2002:a0c:b7a3:: with SMTP id l35mr5881537qve.46.1612839761506; Mon, 08 Feb 2021 19:02:41 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id z20sm17078830qki.93.2021.02.08.19.02.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Feb 2021 19:02:40 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Linus Torvalds , Kirill Tkhai , Mike Rapoport , David Gibson , Kirill Shutemov , Christoph Hellwig , Miaohe Lin , Gal Pressman , Jason Gunthorpe , Jann Horn , peterx@redhat.com, Jan Kara , Wei Zhang , Mike Kravetz , Andrea Arcangeli , Andrew Morton Subject: [PATCH v4 5/5] hugetlb: Do early cow when page pinned on src mm Date: Mon, 8 Feb 2021 22:02:29 -0500 Message-Id: <20210209030229.84991-6-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210209030229.84991-1-peterx@redhat.com> References: <20210209030229.84991-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: This is the last missing piece of the COW-during-fork effort when there're pinned pages found. One can reference 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes", 2020-09-27) for more information, since we do similar things here rather than pte this time, but just for hugetlb. Note that after Jason's recent work on 57efa1fe5957 ("mm/gup: prevent gup_fast from racing with COW during fork", 2020-12-15) which is safer and easier to understand, we're safe now within the whole copy_page_range() against gup-fast, we don't need the wr-protect trick that proposed in 70e806e4e645 anymore. Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu Reviewed-by: Jason Gunthorpe --- mm/hugetlb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 620700f05ff4..7c1a0ecc130e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3727,6 +3727,18 @@ static bool is_hugetlb_entry_hwpoisoned(pte_t pte) return false; } +static void +hugetlb_install_page(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr, + struct page *new_page) +{ + __SetPageUptodate(new_page); + set_huge_pte_at(vma->vm_mm, addr, ptep, make_huge_pte(vma, new_page, 1)); + hugepage_add_new_anon_rmap(new_page, vma, addr); + hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm); + ClearHPageRestoreReserve(new_page); + SetHPageMigratable(new_page); +} + int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma) { @@ -3736,6 +3748,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, int cow = is_cow_mapping(vma->vm_flags); struct hstate *h = hstate_vma(vma); unsigned long sz = huge_page_size(h); + unsigned long npages = pages_per_huge_page(h); struct address_space *mapping = vma->vm_file->f_mapping; struct mmu_notifier_range range; int ret = 0; @@ -3784,6 +3797,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); entry = huge_ptep_get(src_pte); dst_entry = huge_ptep_get(dst_pte); +again: if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) { /* * Skip if src entry none. Also, skip in the @@ -3807,6 +3821,52 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, } set_huge_swap_pte_at(dst, addr, dst_pte, entry, sz); } else { + entry = huge_ptep_get(src_pte); + ptepage = pte_page(entry); + get_page(ptepage); + + /* + * This is a rare case where we see pinned hugetlb + * pages while they're prone to COW. We need to do the + * COW earlier during fork. + * + * When pre-allocating the page or copying data, we + * need to be without the pgtable locks since we could + * sleep during the process. + */ + if (unlikely(page_needs_cow_for_dma(vma, ptepage))) { + pte_t src_pte_old = entry; + struct page *new; + + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + /* Do not use reserve as it's private owned */ + new = alloc_huge_page(vma, addr, 1); + if (IS_ERR(new)) { + put_page(ptepage); + ret = PTR_ERR(new); + break; + } + copy_user_huge_page(new, ptepage, addr, vma, + npages); + put_page(ptepage); + + /* Install the new huge page if src pte stable */ + dst_ptl = huge_pte_lock(h, dst, dst_pte); + src_ptl = huge_pte_lockptr(h, src, src_pte); + spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); + entry = huge_ptep_get(src_pte); + if (!pte_same(src_pte_old, entry)) { + put_page(new); + /* dst_entry won't change as in child */ + goto again; + } + hugetlb_install_page(vma, dst_pte, addr, new); + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + continue; + } + if (cow) { /* * No need to notify as we are downgrading page @@ -3817,12 +3877,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, */ huge_ptep_set_wrprotect(src, addr, src_pte); } - entry = huge_ptep_get(src_pte); - ptepage = pte_page(entry); - get_page(ptepage); + page_dup_rmap(ptepage, true); set_huge_pte_at(dst, addr, dst_pte, entry); - hugetlb_count_add(pages_per_huge_page(h), dst); + hugetlb_count_add(npages, dst); } spin_unlock(src_ptl); spin_unlock(dst_ptl);