From patchwork Thu Feb 4 14:50:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12067469 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.5 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 006FCC433DB for ; Thu, 4 Feb 2021 14:50:43 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 615C264F5E for ; Thu, 4 Feb 2021 14:50:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 615C264F5E 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 BD2626B0006; Thu, 4 Feb 2021 09:50:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BAAFB6B006C; Thu, 4 Feb 2021 09:50:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC0056B0070; Thu, 4 Feb 2021 09:50:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0129.hostedemail.com [216.40.44.129]) by kanga.kvack.org (Postfix) with ESMTP id 9709A6B0006 for ; Thu, 4 Feb 2021 09:50:41 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 46186180AD820 for ; Thu, 4 Feb 2021 14:50:41 +0000 (UTC) X-FDA: 77780871882.23.spot49_140abd5275dd Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id F339F37606 for ; Thu, 4 Feb 2021 14:50:40 +0000 (UTC) X-HE-Tag: spot49_140abd5275dd X-Filterd-Recvd-Size: 7315 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Thu, 4 Feb 2021 14:50:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612450239; 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=w7jRvMA4HBy1aAj/yC80X2PETreSK76wGUdXXk+MGi4=; b=Wd08ZBICHukiZuUqZm00PZ3FAO0zDQ16KRG6tD71/tCtr2FhCbTOnK38cJwlEzGSY0NmfU ZoCVsEPJ1L/jvUz8FKKGAXZj9vZUBfsREA54NeIusGDHlKURvbs7zB7PIBFj/XfwR2C8u9 g8+SGAx3AGe6bJ5i8Y+SJTXkooLxUlY= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-242-iNsKpJguPv23JDqdwtHfJw-1; Thu, 04 Feb 2021 09:50:38 -0500 X-MC-Unique: iNsKpJguPv23JDqdwtHfJw-1 Received: by mail-qv1-f70.google.com with SMTP id v1so2376404qvb.2 for ; Thu, 04 Feb 2021 06:50: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=w7jRvMA4HBy1aAj/yC80X2PETreSK76wGUdXXk+MGi4=; b=oB5t+667fqiGhBtugjOhAB1n36+I5BD7dKMnnkgxIZgIBiyAimRn3opL3SpryFnxb9 BYMrktPK1GMBeAMpfpLjn7TVHp08+y+YFbOV31tgJo/ptqGMflGjsdfX2A6O1cSAKDCB cElcEdTnIuGMcDr3MtCaje/GO4VBA+hF6NyDFyL9YvQE7higjmTw0BEYU7ELer270WQ7 A6mQI42KYiFdt6yLwU1FYd43tYvfPygZtxwMHLAnClpy6y0pQHTC2LwV+/etmzglVt1G 0KJwkvdkS1VLS2jbQfyebaGWeePv+7WuQj4/lMpYXO9AJE1SfsNQKailRmShtroN1ocD ocpw== X-Gm-Message-State: AOAM531xgPdtQfNBdUfPDMJSsLvFaJNBzgjarHo87ZRuUXJ/fqz74bwz TYZXniYeOvoZz9t6H63hsndx6qrZD1g8c8VvH4b0VyKjyXiuPJzvikWzQD9xX7Q7Sn357mqtKdZ CTmusarsGLFPvtbrllnOwkyBy0y7DQnD4jX/GE6IM+Pxf2enI16S8nWEcKHAg X-Received: by 2002:ac8:6d2:: with SMTP id j18mr2156539qth.214.1612450237718; Thu, 04 Feb 2021 06:50:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJzER/i1DvUdHrf1GPiOThk9/cqRKFwCL/S/o8WFC1+MtPN7xx83suFHqdyMHA+4tPyflgLFFg== X-Received: by 2002:ac8:6d2:: with SMTP id j18mr2156499qth.214.1612450237441; Thu, 04 Feb 2021 06:50: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 e14sm4697589qte.18.2021.02.04.06.50.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 06:50:36 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Kirill Shutemov , Wei Zhang , Mike Rapoport , Matthew Wilcox , Miaohe Lin , Andrea Arcangeli , peterx@redhat.com, Gal Pressman , Jan Kara , Jann Horn , Andrew Morton , Kirill Tkhai , Linus Torvalds , Mike Kravetz , Jason Gunthorpe , David Gibson , Christoph Hellwig Subject: [PATCH v2 1/4] hugetlb: Dedup the code to add a new file_region Date: Thu, 4 Feb 2021 09:50:30 -0500 Message-Id: <20210204145033.136755-2-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210204145033.136755-1-peterx@redhat.com> References: <20210204145033.136755-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 18f6ee317900..d2859c2aecc9 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -321,6 +321,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. * @@ -336,7 +354,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; @@ -365,18 +383,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; } @@ -384,17 +395,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 Thu Feb 4 14:50:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12067471 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.5 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 54068C433E0 for ; Thu, 4 Feb 2021 14:50:44 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D300164F58 for ; Thu, 4 Feb 2021 14:50:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D300164F58 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 67B646B0070; Thu, 4 Feb 2021 09:50:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 659E46B006C; Thu, 4 Feb 2021 09:50:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 458B36B0072; Thu, 4 Feb 2021 09:50:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0191.hostedemail.com [216.40.44.191]) by kanga.kvack.org (Postfix) with ESMTP id 26B486B006C for ; Thu, 4 Feb 2021 09:50:43 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id E136D181AC9CC for ; Thu, 4 Feb 2021 14:50:42 +0000 (UTC) X-FDA: 77780871924.24.E1090AC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 7E07640001DB for ; Thu, 4 Feb 2021 14:50:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612450241; 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=BFhiSabHbTK6awvWbCXdRee7Kg5oltAX0WknJbnHBPY=; b=OLTp2Jtb3Fh1niGKi1ZpnyVYHpypXoj4uXwpuBptZgfnEafl9GeS56remMy4PAWf0xXLe9 d/I+CzIuid+Ng9rJXFlHTRcLlwMRuaAgaCcWAt1LlLA9YsZbcjmecvBlYw4MQ9dOYAwvuS TqUr0H6MZwq7h3HQgsCa3kz7INnP+gc= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-517-P5nW2M21M4KSRbnVoPwYAw-1; Thu, 04 Feb 2021 09:50:40 -0500 X-MC-Unique: P5nW2M21M4KSRbnVoPwYAw-1 Received: by mail-qt1-f198.google.com with SMTP id z19so2709365qtv.20 for ; Thu, 04 Feb 2021 06:50: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=BFhiSabHbTK6awvWbCXdRee7Kg5oltAX0WknJbnHBPY=; b=OmxqzXIHvu5cRGLRewVLAfx2lmbmzQawCx5FywKivj2uOAyJAWgVtt0FnriKgue7vE rPX/5RMJDXtOs5kkXgNOTsyUBjOczTLm2/3IyOIHuNIui8dLeDQ4YFzy1eIUmM+2Qus5 +mL+WalvPDzq9A04p5m0ow0vRKJzjhcAvrWugCZCu2TdaxvR6RLqQnm/XtP6lY9WC4nv 12nI1eeplQC8t1YskdMzdMKJPFy6SAO6qTl8W5Syj4IDWcGU/pq1cuPzAuPGV9WUEVtF tU7q3u2eKeYbjuNf3mtQzXWXOHXSUdcGbLe+3h6h060eq14BFu3yWlnZMAnOltQxdrL7 WnQA== X-Gm-Message-State: AOAM533cgM/+mx6r0ea+GvnPtwjqJ5NW0bEXKWkOU7wTbGrLEMyD3YW9 m9H+47yDDMms2tp1gYWqZNgbu2hB7FjU5lQWwfe5VNthurN3Lhe4/Hj5ZrI6zyd5hJNdSybwvQO YBfFaHLWLauxjSQM1AfVO3YKUYZv15/dfwno/0xOp9P9lb02L77cnjWmFXhhh X-Received: by 2002:a0c:ea22:: with SMTP id t2mr7988258qvp.49.1612450239469; Thu, 04 Feb 2021 06:50:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJyPUkNyHWb+UWJMNEc3cGhB+rAC8gjZRsIwVoa3hAW/lFIuyqXBQa1vuvjUS9s3w6r1pDZpIg== X-Received: by 2002:a0c:ea22:: with SMTP id t2mr7988208qvp.49.1612450239176; Thu, 04 Feb 2021 06:50: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 e14sm4697589qte.18.2021.02.04.06.50.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 06:50:38 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Kirill Shutemov , Wei Zhang , Mike Rapoport , Matthew Wilcox , Miaohe Lin , Andrea Arcangeli , peterx@redhat.com, Gal Pressman , Jan Kara , Jann Horn , Andrew Morton , Kirill Tkhai , Linus Torvalds , Mike Kravetz , Jason Gunthorpe , David Gibson , Christoph Hellwig Subject: [PATCH v2 2/4] hugetlg: Break earlier in add_reservation_in_range() when we can Date: Thu, 4 Feb 2021 09:50:31 -0500 Message-Id: <20210204145033.136755-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210204145033.136755-1-peterx@redhat.com> References: <20210204145033.136755-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-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 7E07640001DB X-Stat-Signature: sqh595tt58od4ce63tfkheknzz6seiii Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf02; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1612450241-507847 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 d2859c2aecc9..9e6ea96bf33b 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -377,7 +377,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 Thu Feb 4 14:50:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12067475 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 649E2C433E0 for ; Thu, 4 Feb 2021 14:50:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id ED00A64F58 for ; Thu, 4 Feb 2021 14:50:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ED00A64F58 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 1F6766B0071; Thu, 4 Feb 2021 09:50:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E46966B0072; Thu, 4 Feb 2021 09:50:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5B756B0073; Thu, 4 Feb 2021 09:50:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0206.hostedemail.com [216.40.44.206]) by kanga.kvack.org (Postfix) with ESMTP id BDC8B6B0071 for ; Thu, 4 Feb 2021 09:50:45 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 85E3D181AC9CC for ; Thu, 4 Feb 2021 14:50:45 +0000 (UTC) X-FDA: 77780872050.22.form94_200569e275dd Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 6665C18038E60 for ; Thu, 4 Feb 2021 14:50:45 +0000 (UTC) X-HE-Tag: form94_200569e275dd X-Filterd-Recvd-Size: 8433 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Thu, 4 Feb 2021 14:50:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612450244; 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=Re19mNdJTnr2eyYv/JALCHR5raIIkk7skM0STXXGd4k=; b=Dy3nSL44fcyr2gytiSCm1lHacbQG/NdsCeOD5VpWHQlxTCA8KaFgsI6MNE+cswBUTxnnfA dW7HcAqUOP+BopvRmvfWtLiKAnFhDjuPblO953j93dwJNEk97mDZEnMOVCEmOkqKBZTAyE t13iBvWjxRbfaSZDrHNl7v8fBB6e63w= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-197-iMcHBuo8NR6AbQFr0z3fjg-1; Thu, 04 Feb 2021 09:50:43 -0500 X-MC-Unique: iMcHBuo8NR6AbQFr0z3fjg-1 Received: by mail-qv1-f71.google.com with SMTP id v13so2353191qvm.10 for ; Thu, 04 Feb 2021 06:50: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=Re19mNdJTnr2eyYv/JALCHR5raIIkk7skM0STXXGd4k=; b=NmpOR3tSBPFMUangvqi3pLSr6QkpWuCtCp7PADfa9UJ2AhDmKw/77Oc4QksXPy43G4 TlKj5A7klzelOE7EWMgD96xPwj/ppMJntt2hI+UJTFJCYmsAYbdbTUn7K/sEhBBSDtzu boXKXHJFKFeJ2cYbLP3rxf1ZoI3BN5eZ9BItRJPkkrBo7fiKVswc7MYi+hUP8A441Vuo xUUKS71USo/qBtcEAKaTLS3CMvVu0kokEHtoooXEHdyMmg5rJEKXvTP5QU7eFFvzpr4h q6nyq4XFLvXi8JttzkPSIPv8pMhrg0qiv0wFJWgMY8rQYM8ZT6A374xn9QoxxNQ9HT8/ rXWg== X-Gm-Message-State: AOAM531Uk69OrMXS6OfeQGz8gRAnnV1uUfMLWLpMzDSs7nFlYPIOowJ2 zBii1RsiFqHLokzJQC1ssZdv0tjq16qsaHThEGM/TS35JNz+yX1RYOoe+UDSi2qHQqLXZdOH7If PMnExa3Eny2G93A3JrYVkT9chaTWNwVRcT1WdIydO7sHTC3LpC8I+iqdsvwy4 X-Received: by 2002:a05:620a:13e2:: with SMTP id h2mr7907036qkl.495.1612450241237; Thu, 04 Feb 2021 06:50:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxamB5FIYlcmCuyZ5FNYhQLAdqB1vVcgA8Ln8HyoBPTizJOqj6aW9ViJwY3wZQogZA8YxY8ag== X-Received: by 2002:a05:620a:13e2:: with SMTP id h2mr7907001qkl.495.1612450240944; Thu, 04 Feb 2021 06:50:40 -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 e14sm4697589qte.18.2021.02.04.06.50.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 06:50:40 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Kirill Shutemov , Wei Zhang , Mike Rapoport , Matthew Wilcox , Miaohe Lin , Andrea Arcangeli , peterx@redhat.com, Gal Pressman , Jan Kara , Jann Horn , Andrew Morton , Kirill Tkhai , Linus Torvalds , Mike Kravetz , Jason Gunthorpe , David Gibson , Christoph Hellwig Subject: [PATCH v2 3/4] mm: Introduce page_needs_cow_for_dma() for deciding whether cow Date: Thu, 4 Feb 2021 09:50:32 -0500 Message-Id: <20210204145033.136755-4-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210204145033.136755-1-peterx@redhat.com> References: <20210204145033.136755-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: 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 --- include/linux/mm.h | 21 +++++++++++++++++++++ mm/huge_memory.c | 8 ++------ mm/internal.h | 5 ----- mm/memory.c | 7 +------ 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ecdf8a8cd6ae..6ea20721d349 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1291,6 +1291,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 9237976abe72..dbff6c7eda67 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1095,9 +1095,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); @@ -1209,9 +1207,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 25d2b2439f19..24eec93d0dac 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 feff48e1465a..b2849e1d4aab 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -800,9 +800,6 @@ copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma 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, @@ -816,9 +813,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 Thu Feb 4 14:50:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12067477 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 410BDC433E0 for ; Thu, 4 Feb 2021 14:50:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B5C5F64F4E for ; Thu, 4 Feb 2021 14:50:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B5C5F64F4E 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 7DBFC6B0072; Thu, 4 Feb 2021 09:50:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7663C6B0073; Thu, 4 Feb 2021 09:50:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6549A6B0074; Thu, 4 Feb 2021 09:50:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0192.hostedemail.com [216.40.44.192]) by kanga.kvack.org (Postfix) with ESMTP id 4A2276B0072 for ; Thu, 4 Feb 2021 09:50:47 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 01AD51EE6 for ; Thu, 4 Feb 2021 14:50:47 +0000 (UTC) X-FDA: 77780872134.08.error77_4f0d4ca275dd Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id D217E1819E76F for ; Thu, 4 Feb 2021 14:50:46 +0000 (UTC) X-HE-Tag: error77_4f0d4ca275dd X-Filterd-Recvd-Size: 7942 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf30.hostedemail.com (Postfix) with ESMTP for ; Thu, 4 Feb 2021 14:50:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612450245; 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=J1qA8YDBc2tuVyycbGtMSDtlYrQVjFJWlhZm/wJFwx8=; b=iyBJSfKRLWDMV/mpLRi4H6W5gqO8Gb4V7LpUgrSyb1GUqNMpx0HJTnYe2J3TKTgmneyR0g dI8OmkrvjXWvwZstgao5Cd5dvhqH6j7D83KCuiWu45sduNUeLAAlMMuJIEazEYl2lsFovR SK7j5nhiKcgQPjKW6e98XvjaH1Nz0nU= 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-196-za7kma6cPK-S_go3zXdj-A-1; Thu, 04 Feb 2021 09:50:43 -0500 X-MC-Unique: za7kma6cPK-S_go3zXdj-A-1 Received: by mail-qt1-f200.google.com with SMTP id v13so2717553qtq.18 for ; Thu, 04 Feb 2021 06:50:43 -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=J1qA8YDBc2tuVyycbGtMSDtlYrQVjFJWlhZm/wJFwx8=; b=Q4N2rqe+yBgYSvl7vwSGx0kzbzotS03R59Pnv24e8gkxlbKtCggtXBeIWc4W//nSXh 7+NRXQg5chBbkKRUgHUmsUX3UYX/VJvVWaLvWHskyU0pBevKbBr30kRi+vEyj9KR47RX bXstTFs3wmdW5Cn5s4YC48UDEBwDTErL5e02r4FbX1OCUIsKPRPQXf2jimkrYG7ePQ/5 62vS57CWynsZLL/NHqcjN68Fst7Hv6cUEx71lBedgPd9ieaynqWTFjFU3vNmdZzxJWUF K898vEV6sDsfSBIqzKTfW/6wbpMNd5yvD4w1gzWqIYSHUw5CX/WjGIcrz3+mMxRSMEhM 2GuA== X-Gm-Message-State: AOAM530vz0+V0Rp9sCTRImQy7RZxCdu2QYd/vL+levBjY0gITjzj1lR0 X/NtLEXusT3bRkVArUG7NRqUDfr7pXZT4wnhosRh9ALwYpWsUIK64YQQArDRAhKG2677s6r9IAd n1tvYj4RAQ/s1nZFgKcQCTvpzdXpJMZjaj/bav1T200V2jABnOxtiLeIxAk1l X-Received: by 2002:ac8:6712:: with SMTP id e18mr7358806qtp.15.1612450243138; Thu, 04 Feb 2021 06:50:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJxGPdLTx9+uGrpdLPJ2GgjaQeEj+B8me3HP+fj+LrnvfPOpIo+YyzJ1xNFxxr0eOPO00kg2rw== X-Received: by 2002:ac8:6712:: with SMTP id e18mr7358766qtp.15.1612450242858; Thu, 04 Feb 2021 06:50:42 -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 e14sm4697589qte.18.2021.02.04.06.50.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 06:50:42 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Kirill Shutemov , Wei Zhang , Mike Rapoport , Matthew Wilcox , Miaohe Lin , Andrea Arcangeli , peterx@redhat.com, Gal Pressman , Jan Kara , Jann Horn , Andrew Morton , Kirill Tkhai , Linus Torvalds , Mike Kravetz , Jason Gunthorpe , David Gibson , Christoph Hellwig Subject: [PATCH v2 4/4] hugetlb: Do early cow when page pinned on src mm Date: Thu, 4 Feb 2021 09:50:33 -0500 Message-Id: <20210204145033.136755-5-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210204145033.136755-1-peterx@redhat.com> References: <20210204145033.136755-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. Signed-off-by: Peter Xu --- mm/hugetlb.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9e6ea96bf33b..5793936e00ef 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3734,11 +3734,27 @@ static bool is_hugetlb_entry_hwpoisoned(pte_t pte) return false; } +static void +hugetlb_copy_page(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr, + struct page *old_page, struct page *new_page) +{ + struct hstate *h = hstate_vma(vma); + unsigned int psize = pages_per_huge_page(h); + + copy_user_huge_page(new_page, old_page, addr, vma, psize); + __SetPageUptodate(new_page); + ClearPagePrivate(new_page); + set_page_huge_active(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(psize, vma->vm_mm); +} + int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma) { pte_t *src_pte, *dst_pte, entry, dst_entry; - struct page *ptepage; + struct page *ptepage, *prealloc = NULL; unsigned long addr; int cow; struct hstate *h = hstate_vma(vma); @@ -3787,7 +3803,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, dst_entry = huge_ptep_get(dst_pte); if ((dst_pte == src_pte) || !huge_pte_none(dst_entry)) continue; - +again: 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); @@ -3816,6 +3832,39 @@ 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 we need to be without + * all the locks since we could sleep when allocate. + */ + if (unlikely(page_needs_cow_for_dma(vma, ptepage))) { + if (!prealloc) { + put_page(ptepage); + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + prealloc = alloc_huge_page(vma, addr, 1); + if (!prealloc) { + ret = -ENOMEM; + break; + } + goto again; + } + hugetlb_copy_page(vma, dst_pte, addr, ptepage, + prealloc); + put_page(ptepage); + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + prealloc = NULL; + continue; + } + if (cow) { /* * No need to notify as we are downgrading page @@ -3826,9 +3875,7 @@ 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); @@ -3842,6 +3889,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, else i_mmap_unlock_read(mapping); + /* Free the preallocated page if not used at last */ + if (prealloc) + put_page(prealloc); + return ret; } From patchwork Thu Feb 4 20:20:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12068601 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.5 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 B5246C433DB for ; Thu, 4 Feb 2021 20:21:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4662764F3F for ; Thu, 4 Feb 2021 20:21:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4662764F3F 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 A944D6B0006; Thu, 4 Feb 2021 15:21:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A6B756B006E; Thu, 4 Feb 2021 15:21:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EA396B0070; Thu, 4 Feb 2021 15:21:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0180.hostedemail.com [216.40.44.180]) by kanga.kvack.org (Postfix) with ESMTP id 76E1D6B0006 for ; Thu, 4 Feb 2021 15:21:01 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2C28D180AD81D for ; Thu, 4 Feb 2021 20:21:01 +0000 (UTC) X-FDA: 77781704322.14.brain94_0c0f058275df Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 0CC9E1822987A for ; Thu, 4 Feb 2021 20:21:01 +0000 (UTC) X-HE-Tag: brain94_0c0f058275df X-Filterd-Recvd-Size: 7837 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf28.hostedemail.com (Postfix) with ESMTP for ; Thu, 4 Feb 2021 20:21:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612470060; 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=8RUhZ+EXApqGSb7+Tmq9sGFuKWAYlgVlaiJ/4jTiTkk=; b=BzsVl3ojZ0y0UeAhlRPXkbJlDE07gqnYRJ8mZw3On/zqTT0YzGi12wpsQwT8XqAQVfpqEt x9xvyyvgKUDt065VJgsJvjvXsUEG425Z3ey20gCnk2dLQTXFr8CtUvBY4UeNsJ6WhVrDhX NUPa1SZI7FxLRJJcgwH7GNKqKFb++jg= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-559-f84aR7OKPXyAwI1FLR5nLg-1; Thu, 04 Feb 2021 15:20:58 -0500 X-MC-Unique: f84aR7OKPXyAwI1FLR5nLg-1 Received: by mail-qk1-f198.google.com with SMTP id 70so3743639qkh.4 for ; Thu, 04 Feb 2021 12:20:58 -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=8RUhZ+EXApqGSb7+Tmq9sGFuKWAYlgVlaiJ/4jTiTkk=; b=jHhcwcr9X8phI23SDqMdkMgTcGkEE1x2v2EsLVAt7DwFK2yDzR3uFoZ7Ij9WVrhUqO 8o3yTYx2o7gu1N9ui/c0o8L3YTY84gQcfxtBe+BBwqvBzms2ldM7iL+MLC5Ra49JJTzX Bqxp8HOQWBFHIF0gXWdoqtHsnrjXHRubYPf3V8R6gAiKtP+tqmGzfM0gc7J/x0nhSQZ+ Z4CQbKfqd4Q/gmLG+g+TbXCD33b9ugle96V27LM8nehrTEeIn4zbF0vxrzgP5lYZ0Tat 8KpsCM1sEHTBYErtKwdocE/+lZcsquAB+x9k3CQ0sIhV9NHyOrCFVUVv2hRaKOJgFQhd wbag== X-Gm-Message-State: AOAM5334DsOUmfA/zwxdpxGjKL8E3J+UhoYOro+Dqx/1NYc4VU7ydPV8 TmkGGMWdEA65KVJFP12WlgQuylotOKqxPs+w4UaqRem6Si/KwcAs3o2dUjfhmGIvI5lsSqHN/Z0 5wqlwxqtMl3DguOOAhh1SfHmULqwoEwID+aU77d3aRmwDHtwZAtOwEBc+cGjw X-Received: by 2002:ae9:ef55:: with SMTP id d82mr991098qkg.243.1612470058182; Thu, 04 Feb 2021 12:20:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJwMrOpedoRrsIqyFMCtBNBcL9gayR8imVmM/6UmrovjTLeTt3NSZKJNQgK1fpK21QJITN0cvA== X-Received: by 2002:ae9:ef55:: with SMTP id d82mr991055qkg.243.1612470057866; Thu, 04 Feb 2021 12:20:57 -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 p18sm5990601qkj.130.2021.02.04.12.20.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 12:20:57 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Mike Kravetz , Jan Kara , Mike Rapoport , Andrew Morton , Christoph Hellwig , Kirill Tkhai , David Gibson , Jason Gunthorpe , Andrea Arcangeli , Miaohe Lin , Gal Pressman , Wei Zhang , Matthew Wilcox , Jann Horn , Linus Torvalds , peterx@redhat.com, Kirill Shutemov , VMware Graphics , Roland Scheidegger , David Airlie , Daniel Vetter , Alexey Dobriyan Subject: [PATCH v2 5/4] mm: Use is_cow_mapping() across tree where proper Date: Thu, 4 Feb 2021 15:20:55 -0500 Message-Id: <20210204202055.192260-1-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210204145033.136755-1-peterx@redhat.com> References: <20210204145033.136755-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 --- 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 0b76b3d17d4c..22c4a0e9d250 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 5793936e00ef..684d91b68b60 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3756,15 +3756,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, *prealloc = NULL; 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,