From patchwork Wed Feb 17 23:35:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12092599 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 BBFA0C433E0 for ; Wed, 17 Feb 2021 23:36:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 421C364D74 for ; Wed, 17 Feb 2021 23:36:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 421C364D74 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 3DB396B006E; Wed, 17 Feb 2021 18:35:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2ECD86B0070; Wed, 17 Feb 2021 18:35:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B8466B0071; Wed, 17 Feb 2021 18:35:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0074.hostedemail.com [216.40.44.74]) by kanga.kvack.org (Postfix) with ESMTP id 031D46B006E for ; Wed, 17 Feb 2021 18:35:57 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id BD61C2478 for ; Wed, 17 Feb 2021 23:35:57 +0000 (UTC) X-FDA: 77829369954.13.B5BB01E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf15.hostedemail.com (Postfix) with ESMTP id BD869A0009D5 for ; Wed, 17 Feb 2021 23:35:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613604956; 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=IJC/VlIq+91W98xndkPTGEyN7MrILcrOIZFc0zZvO1w=; b=VjEOLRaZcjysqMGZau93OoGuMc1tlX/RTNn1wbV5vrMr6H5QdYC1+tnBIvthtzEnBeq56Y 4kB1nUcDqvF7x7Eu2q035OsdE0gsxpYEpihcFk4nlkEtPI3qX74p3Yw6LdyZtIXF1rmUKX SKuxgpWcr7QoWivjpHbCGBtoYP4kfk8= 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-371-0nrDRXYtMAGwip8ASPosTg-1; Wed, 17 Feb 2021 18:35:53 -0500 X-MC-Unique: 0nrDRXYtMAGwip8ASPosTg-1 Received: by mail-qv1-f70.google.com with SMTP id t18so77991qva.6 for ; Wed, 17 Feb 2021 15:35:52 -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=IJC/VlIq+91W98xndkPTGEyN7MrILcrOIZFc0zZvO1w=; b=GiJzMaLRlcUIExEkR1yb3le/QP2SwCEPuZ6YgJ8Fss+cDAT9snbOoKZkClgcM3dwNv X1EPlEwZbBtNYDSgOM8UTsVTZZGsNRH4V1rlKhDN0YPOxyieuN2tO4qwgj7NrnKPbk96 O5Nlc7cHniB0kkgogp1LqTY07Kf8z373xKl5RKXGSfsGD5ksHRMBPdY/TOfCnf9N3i38 vZom9furekLrD6y184hJFbTYwvNdlniNxCTPcF7sGmTH2J4NJGNMRQ7ym4gGBwA/thxv m2/MwsjGZVgOz577siXpR0UtSBk8DNQNuUn4OQInSsh9m+EEcPOVJoJmxUx6dIOI+PSD y2Ag== X-Gm-Message-State: AOAM532OIpMSquhQe5rmnl1SJnJQvpVhJx8YD6rE8ETfHeIqNUAT9m7S j2528AX2bjgKGfw8CiegHpeEUsS2jdar98T9h6fn1pmOT0fyCimZ/8W9VeW16rdcvuFZ/mNWE9f TIziIV5SoLvCsqsfj3ckX8ja4nrjEBlcN/LXp5Dgi8lqWPHtkKoopRtqsoPVr X-Received: by 2002:a37:a452:: with SMTP id n79mr1749732qke.60.1613604951953; Wed, 17 Feb 2021 15:35:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJxYXiab+QHsGr+JgkvRch04EDItcq1JEuMGGAgiNB4vHvF7Kqj8vtVYT6KCzNXV5qB6U0d2ww== X-Received: by 2002:a37:a452:: with SMTP id n79mr1749695qke.60.1613604951694; Wed, 17 Feb 2021 15:35:51 -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 o5sm2739622qkh.59.2021.02.17.15.35.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Feb 2021 15:35:51 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Matthew Wilcox , Wei Zhang , peterx@redhat.com, Mike Kravetz , Mike Rapoport , Christoph Hellwig , Andrew Morton , Linus Torvalds , David Gibson , Jason Gunthorpe , Jann Horn , Kirill Tkhai , Kirill Shutemov , Miaohe Lin , Andrea Arcangeli , Jan Kara Subject: [PATCH v5 1/5] hugetlb: Dedup the code to add a new file_region Date: Wed, 17 Feb 2021 18:35:43 -0500 Message-Id: <20210217233547.93892-2-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210217233547.93892-1-peterx@redhat.com> References: <20210217233547.93892-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: BD869A0009D5 X-Stat-Signature: uonhsawamdqt7dana9xu4ddyyys9g8nz Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf15; 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: 1613604956-413617 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 b6992297aa16..e302ed715839 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 Wed Feb 17 23:35:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12092597 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 93716C433E6 for ; Wed, 17 Feb 2021 23:35:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 38C98600EF for ; Wed, 17 Feb 2021 23:35:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 38C98600EF 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 3DB386B006C; Wed, 17 Feb 2021 18:35:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 313586B006E; Wed, 17 Feb 2021 18:35:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 18D7B6B0070; Wed, 17 Feb 2021 18:35:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0165.hostedemail.com [216.40.44.165]) by kanga.kvack.org (Postfix) with ESMTP id ED0FB6B006C for ; Wed, 17 Feb 2021 18:35:56 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id BBA2E1E1A for ; Wed, 17 Feb 2021 23:35:56 +0000 (UTC) X-FDA: 77829369912.10.CD7EE08 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 177AF407F8DF for ; Wed, 17 Feb 2021 23:35:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613604955; 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=DOLBpO+jIc7m6kTzABHfMZhxUnbe7uQo4h61IVSpjcg=; b=OvL5+oGY2IjeIVaSXbH0Eo5pmJn2SLnvmwynLxYJPGdn/WNibHHi1YQ4IZM7tI+2HYQrss vU8llXlQL1YSK/r7Oawgk4TPukYoD/UtRkgTyeLuYAM5tkgFC0A0b129oAhwfIY9Agmrvg u1Y3vcJzTY7vsnDIsvjLENkl+C5jw+s= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-552-s92N21KJPN2xYncUDaENHw-1; Wed, 17 Feb 2021 18:35:54 -0500 X-MC-Unique: s92N21KJPN2xYncUDaENHw-1 Received: by mail-qk1-f199.google.com with SMTP id d66so129158qke.9 for ; Wed, 17 Feb 2021 15:35:54 -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=DOLBpO+jIc7m6kTzABHfMZhxUnbe7uQo4h61IVSpjcg=; b=PEXgSW/XiK/Sl2SZa4TCxattbWXsXgor0psXD+GFMzjmUp9UzgUAiIsa/x9ChppdPb 85Vic7U0gt/s4gIQL5eXX0cRVf8kE9k4WfWD62guutr+3DFBUZ5bfEX2xVGkck7Nq2yB zP15Ny/76A2vNferx42eYaRzfQU6kRbbhoAfMiaLACdfaCY4PRQpypDzcHvPuaA9OGGE YcuyJ11VsOabcJarRtvyLzmZbPqrTzXMKE3hTFT/yp/5AV6GCtMmG+7SbHnkKQ9nuBLp sousVYJxU/Ocfd+rbL1yrfBdIHSOUSvdIFi6+zYqpRtNRn13g3NN0Bo7nSsLoSIl3pGE Mo0g== X-Gm-Message-State: AOAM533sLD38pha9CiFaDd9yo3JZs7/f6exU9dY96wyJxrAiBb8XSGjb iJn6ve3XM6xtVyfxDmgBaZ5Nkiinebfz8dPks57qxI1PO6SjqGCnjGc6vIpUrdHeyPJTPbs33X4 auMK/Jaht+wbbWrfV0PXOz6SHgx/ymds03iIwW10avOFuoTp6JoDkLhgyJuEQ X-Received: by 2002:ac8:149a:: with SMTP id l26mr1755913qtj.210.1613604953503; Wed, 17 Feb 2021 15:35:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJzkPF9IdEx/0qho199ZELPwh7tKPt6adZyOrtuP5Au+UOQSOmMOTUV01IpIZiH6otG3JNuBHw== X-Received: by 2002:ac8:149a:: with SMTP id l26mr1755887qtj.210.1613604953196; Wed, 17 Feb 2021 15:35:53 -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 o5sm2739622qkh.59.2021.02.17.15.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Feb 2021 15:35:52 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Matthew Wilcox , Wei Zhang , peterx@redhat.com, Mike Kravetz , Mike Rapoport , Christoph Hellwig , Andrew Morton , Linus Torvalds , David Gibson , Jason Gunthorpe , Jann Horn , Kirill Tkhai , Kirill Shutemov , Miaohe Lin , Andrea Arcangeli , Jan Kara Subject: [PATCH v5 2/5] hugetlg: Break earlier in add_reservation_in_range() when we can Date: Wed, 17 Feb 2021 18:35:44 -0500 Message-Id: <20210217233547.93892-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210217233547.93892-1-peterx@redhat.com> References: <20210217233547.93892-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: ogxh15foze8bank6csxxje8j43af1dy1 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 177AF407F8DF Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf17; 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: 1613604954-130166 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 e302ed715839..2ba4ea4ab46e 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 Wed Feb 17 23:35:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12092601 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 DFD27C433DB for ; Wed, 17 Feb 2021 23:36:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 47FB564D74 for ; Wed, 17 Feb 2021 23:36:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 47FB564D74 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 BA1D96B0070; Wed, 17 Feb 2021 18:35:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B2F1D6B0071; Wed, 17 Feb 2021 18:35:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C2118D0001; Wed, 17 Feb 2021 18:35:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0234.hostedemail.com [216.40.44.234]) by kanga.kvack.org (Postfix) with ESMTP id 50A016B0071 for ; Wed, 17 Feb 2021 18:35:58 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 102C28249980 for ; Wed, 17 Feb 2021 23:35:58 +0000 (UTC) X-FDA: 77829369996.12.skin10_0d1345c27650 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id E2BC218004540 for ; Wed, 17 Feb 2021 23:35:57 +0000 (UTC) X-HE-Tag: skin10_0d1345c27650 X-Filterd-Recvd-Size: 8608 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Wed, 17 Feb 2021 23:35:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613604957; 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=STpfyN8Z1UaurR27BXEZzooPdTPi/top9+Ws5AZcNHA=; b=VC6wmoNj7Tz4W7aBNNHHvozkFTiKYuHrtIkzwBmsDP5WZUtywujMWxwI/G4D3zeEtApnSw kgL4tlq2DChyREkDKNrcflnqJHVXpDTdKm2v5uUUzF+Ynh4ZVEqDnzgreS63MvmVRQlyuy BfdGkpsbj0h7NFfuZXX0gfZGjL7ydRo= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-564-06wOYYyONkyVQ6kFnz4Irg-1; Wed, 17 Feb 2021 18:35:55 -0500 X-MC-Unique: 06wOYYyONkyVQ6kFnz4Irg-1 Received: by mail-qt1-f199.google.com with SMTP id e3so105030qtc.1 for ; Wed, 17 Feb 2021 15:35:55 -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=STpfyN8Z1UaurR27BXEZzooPdTPi/top9+Ws5AZcNHA=; b=m2cGp03F5E6L1pflMCRvE5LR1d6W4GGd7dGftwKQcbnRa1cF+F2j2+B6B0H+4IkG8d XVrSgHOKcmsFpi1ShJ1IBILec+c+6bgfQNhAwBfqtQXdA4fyddI1pX49p1gAdD39k2gd qfnUFGqUPMiO1dm9Z074eYhovYo6WvwSbD0kU/Tl/XdnVtDKazCX2RrbQojgB24xB/cj bGznGK6NmK2bKEz2spncKUC6v5MGXqvuRUn1wZ5+LxamMVeDPP/un9jEUvBzMulbv3ey Yjv0r9TOsg8nqsUdpN/yMIS59wNLVqdZoLvHj/VaOnd/MNMXfpnLnb0wSYbW5k8XWqQN YglA== X-Gm-Message-State: AOAM5307QJspyLPGEUid6xR7st/UBvYen2T+3n1XD97ybFxkFv2CJjgy awF9kRQUALO3/QWCWlaOfk1ztw/3aqTlmz0Hi/qHQhx5ZwO/VmaGuh3vN/dWVe0kxFdUgXCpz4J YEIc1oIKOfAP8RPcfVOB0FR3SYA0jZjnOxyqGYtzrZgbH7wkPak2AaaQbDeaj X-Received: by 2002:a05:622a:552:: with SMTP id m18mr1773672qtx.207.1613604955096; Wed, 17 Feb 2021 15:35:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJwTtocSTEfx7O+Eln2AHspJzthhrkUPbsyEtDyDs4UT9lHpY1HhZ6bjOJIXvvJ8W0XO0w+dOw== X-Received: by 2002:a05:622a:552:: with SMTP id m18mr1773637qtx.207.1613604954782; Wed, 17 Feb 2021 15:35:54 -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 o5sm2739622qkh.59.2021.02.17.15.35.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Feb 2021 15:35:54 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Matthew Wilcox , Wei Zhang , peterx@redhat.com, Mike Kravetz , Mike Rapoport , Christoph Hellwig , Andrew Morton , Linus Torvalds , David Gibson , Jason Gunthorpe , Jann Horn , Kirill Tkhai , Kirill Shutemov , Miaohe Lin , Andrea Arcangeli , Jan Kara Subject: [PATCH v5 3/5] mm: Introduce page_needs_cow_for_dma() for deciding whether cow Date: Wed, 17 Feb 2021 18:35:45 -0500 Message-Id: <20210217233547.93892-4-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210217233547.93892-1-peterx@redhat.com> References: <20210217233547.93892-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. Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- 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 77e64e3eac80..64a71bf20536 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1300,6 +1300,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 395c75111d33..da1d63a41aec 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1100,9 +1100,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); @@ -1214,9 +1212,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 9902648f2206..1432feec62df 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 eac765e1c6b9..e50e488b8ea3 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 Wed Feb 17 23:35:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12092603 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 761D6C433E0 for ; Wed, 17 Feb 2021 23:36:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1D90E600EF for ; Wed, 17 Feb 2021 23:36:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1D90E600EF 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 4EBEB6B0071; Wed, 17 Feb 2021 18:36:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DBDC8D0001; Wed, 17 Feb 2021 18:36:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DD1A6B0073; Wed, 17 Feb 2021 18:36:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0219.hostedemail.com [216.40.44.219]) by kanga.kvack.org (Postfix) with ESMTP id 03EA66B0071 for ; Wed, 17 Feb 2021 18:36:00 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id C83718249980 for ; Wed, 17 Feb 2021 23:36:00 +0000 (UTC) X-FDA: 77829370080.12.504B752 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf10.hostedemail.com (Postfix) with ESMTP id 43AF0407F8DB for ; Wed, 17 Feb 2021 23:35:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613604959; 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=bBTLGivaN2/sd96zZsp336IcNgBMJM5xgqwCgvKU+2Q=; b=c5Cr/i0hcbZIfRoEUgCVLusJ0dS6q1CIGoUo3kxcUuifPnwAsQD7Jf65w835/68kZ7YRlE kjTL+FaQbWTyopYWc9YhExEpuB5evQ143WVqVIRkeDKyx8XIoxTlGzHS7hqA41hE9hNFCA TLZwfL7qJD7dJ0S2FgoUGDmHalneLjY= 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-353-7oPZlBCsN_-4EjCuSgOWvg-1; Wed, 17 Feb 2021 18:35:58 -0500 X-MC-Unique: 7oPZlBCsN_-4EjCuSgOWvg-1 Received: by mail-qv1-f69.google.com with SMTP id j6so72232qvo.11 for ; Wed, 17 Feb 2021 15:35:57 -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=bBTLGivaN2/sd96zZsp336IcNgBMJM5xgqwCgvKU+2Q=; b=XY1LCcIQ7EQFTJkwwdX38MRJKcuAYeNtntPtzSXQthx+wAD7ev+MwDGX8gnfmREsA5 04NFT/onV+sz6ANmBjA4YMk/9IIFRJy8X5WE2sEihXwIYO7cM3VrmdVl6hwxqQXNyblK 86oizn/CFGM8/mD77uZX/6Ec+XGAG++rA/K25C0sxc6ZDc8Rxb6fwKf7t0oDvAtQmxhx oD99CyiqWLz+8MqsKswk6NpIRxK1ubb6zANpUz7gKWwIgrhHuIVtYt27Bl29lNTIJSr4 AjcigU2Z9nqehxL1cNACyYFeDlWCVT8OVgQ/svkN/FPdsaWZnW1brXQo75jP6oYmuccg YX1w== X-Gm-Message-State: AOAM530G9OxEkNTjd1iuB3nVyL8xFLq5KXhxlyAmOIeXg8YU95MuscMk aA7scgn5SeLYBLliVNvZ1Q+hkbGDNoq7H/1N77QY3QIX0d9cQkzKTcw83t16dPqARxQLpa8hfxD aE83yuqOY6Q7zcm+BkDmvBYpyJzfPyKpCAss9c8GOXQzSyymtIgHsl/hneOJE X-Received: by 2002:a05:622a:183:: with SMTP id s3mr1751836qtw.223.1613604956727; Wed, 17 Feb 2021 15:35:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJwCH2IIJVdVsOH2LNlUl1Ovh4tC3TAN8kSvtARyw6G8f10CuUr38qe/cCGqM2ng0NiVxAXCwQ== X-Received: by 2002:a05:622a:183:: with SMTP id s3mr1751793qtw.223.1613604956479; Wed, 17 Feb 2021 15:35:56 -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 o5sm2739622qkh.59.2021.02.17.15.35.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Feb 2021 15:35:56 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Matthew Wilcox , Wei Zhang , peterx@redhat.com, Mike Kravetz , Mike Rapoport , Christoph Hellwig , Andrew Morton , Linus Torvalds , David Gibson , Jason Gunthorpe , Jann Horn , Kirill Tkhai , Kirill Shutemov , Miaohe Lin , Andrea Arcangeli , Jan Kara , VMware Graphics , Roland Scheidegger , David Airlie , Daniel Vetter , Alexey Dobriyan Subject: [PATCH v5 4/5] mm: Use is_cow_mapping() across tree where proper Date: Wed, 17 Feb 2021 18:35:46 -0500 Message-Id: <20210217233547.93892-5-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210217233547.93892-1-peterx@redhat.com> References: <20210217233547.93892-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: rmfosu8ooc4i7ezkixj5whj47cmoj4n4 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 43AF0407F8DB Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf10; 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: 1613604957-863849 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 Reviewed-by: Jason Gunthorpe 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 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 3cec6fbef725..e862cab69583 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 2ba4ea4ab46e..8379224e1d43 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3731,15 +3731,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; + bool 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 Wed Feb 17 23:35:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12092605 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 AC79BC433E6 for ; Wed, 17 Feb 2021 23:36:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 48209600EF for ; Wed, 17 Feb 2021 23:36:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 48209600EF 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 7F7608D0002; Wed, 17 Feb 2021 18:36:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 783538D0001; Wed, 17 Feb 2021 18:36:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FFDA8D0002; Wed, 17 Feb 2021 18:36:02 -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 38F158D0001 for ; Wed, 17 Feb 2021 18:36:02 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 0265B180AD802 for ; Wed, 17 Feb 2021 23:36:02 +0000 (UTC) X-FDA: 77829370164.03.teeth99_0f0883227650 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id D2B6628A4EA for ; Wed, 17 Feb 2021 23:36:01 +0000 (UTC) X-HE-Tag: teeth99_0f0883227650 X-Filterd-Recvd-Size: 8733 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf28.hostedemail.com (Postfix) with ESMTP for ; Wed, 17 Feb 2021 23:36:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613604960; 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=WSh1ft3TkUZQw5lT26Ir4z6+mrJgP1tvshvl4KSiDTY=; b=T0K/bEwDMGJRGa+35SEQRfBSwvgdEuZbph4Dj6i/33kAwHrMUNOEFb6gDDP06yVQuSOcRG mHoMHCTurHt2yXe+95rSMMp88AHe98768EGoHEFRFtUzeRJQW02DrYvO7RX8IaZgLBqFAS lBMV+JS+k2rAYoQFhnI5847VuMtY9r8= 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-446-zuvg3Jb9OyCHEmTE1xjz5Q-1; Wed, 17 Feb 2021 18:35:59 -0500 X-MC-Unique: zuvg3Jb9OyCHEmTE1xjz5Q-1 Received: by mail-qv1-f70.google.com with SMTP id u8so81099qvm.5 for ; Wed, 17 Feb 2021 15:35:59 -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=WSh1ft3TkUZQw5lT26Ir4z6+mrJgP1tvshvl4KSiDTY=; b=WpzbqlPdv8JXaN2ie5Lz56cZTRtHZqY91qLcaNNx3d0Ou4zuyZC+Ihxo77rDbyaInG oQ7L2j7sl7HmmEEmrDvDtwhOeDOxxRzadoIxDQjGnlWIrL7mb5E1gmyOH9qJZjIPzr/k OTDSIXxtZcZAaxtL6ws02/1YDrWUVlka25syOPU2ljBAZr4Rn7N5fk13wpdDTxGFon8c c4icS6peBrGyU0MaLOsZUMr9VOpmrl1D4p7NWg3WfR8Vg5XqcGtInVF87LUELklq58lR VfLKSBYavpGTsgseHyTLNo6a/Bu+GR4r2KkzfeVJMUgkFDHw1jSCV22V8AOEArsqcSL6 ojzA== X-Gm-Message-State: AOAM5303mGACDh2TKdKYD/aGwusj57OzYiXnd+ay3/CSB8i/ZkVGX27r Z7Esn2BPUx7/M/f6Yc73m5Dp+fjcsqFhFfQV/F9miKCDrKtnVb7PFKNY6/DE6DYzY4Tld9sab4/ bHOPmwPlYRZj7ZGJYFbHzB6zT9jylF6VRSfqgL9fjk9XgRQYjkgD8Fyb+XMIo X-Received: by 2002:ac8:4c8e:: with SMTP id j14mr1827741qtv.92.1613604958281; Wed, 17 Feb 2021 15:35:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZ/yEV04MLom7yxkWLFWTI5mn1lD6GWwzpCnVnvrYHJZXeaz6IYDZJzbezFjqMM5XCtZhhUA== X-Received: by 2002:ac8:4c8e:: with SMTP id j14mr1827704qtv.92.1613604957958; Wed, 17 Feb 2021 15:35: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 o5sm2739622qkh.59.2021.02.17.15.35.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Feb 2021 15:35:57 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Matthew Wilcox , Wei Zhang , peterx@redhat.com, Mike Kravetz , Mike Rapoport , Christoph Hellwig , Andrew Morton , Linus Torvalds , David Gibson , Jason Gunthorpe , Jann Horn , Kirill Tkhai , Kirill Shutemov , Miaohe Lin , Andrea Arcangeli , Jan Kara Subject: [PATCH v5 5/5] hugetlb: Do early cow when page pinned on src mm Date: Wed, 17 Feb 2021 18:35:47 -0500 Message-Id: <20210217233547.93892-6-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210217233547.93892-1-peterx@redhat.com> References: <20210217233547.93892-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 Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- mm/hugetlb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 8379224e1d43..0b45ff7df708 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3725,6 +3725,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) { @@ -3734,6 +3746,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, bool 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; @@ -3782,6 +3795,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 @@ -3805,6 +3819,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 @@ -3815,12 +3875,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);