From patchwork Wed Feb 3 21:08: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: 12065373 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 94721C433DB for ; Wed, 3 Feb 2021 21:08:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EDFD261477 for ; Wed, 3 Feb 2021 21:08:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EDFD261477 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 30AC26B0005; Wed, 3 Feb 2021 16:08:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B9796B006C; Wed, 3 Feb 2021 16:08:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1836F6B006E; Wed, 3 Feb 2021 16:08:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0092.hostedemail.com [216.40.44.92]) by kanga.kvack.org (Postfix) with ESMTP id F348C6B0005 for ; Wed, 3 Feb 2021 16:08:40 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id B13DA181AEF15 for ; Wed, 3 Feb 2021 21:08:40 +0000 (UTC) X-FDA: 77778195600.20.turn20_5a0a8e9275d6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 93144180C07A3 for ; Wed, 3 Feb 2021 21:08:40 +0000 (UTC) X-HE-Tag: turn20_5a0a8e9275d6 X-Filterd-Recvd-Size: 7136 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Wed, 3 Feb 2021 21:08:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612386519; 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=hi+7C1TTEkkl4WRac4WQ2x2QuL/BvKdq/6D+mj3cbKc=; b=alhk85diwXDtlJMGANqqSLWSjY4tquHv0OdbASmXjGraN6dCH+0tD8TYxTRpOOi2n1UZtU jPsyK/QVp3mhpyOXAbJteppFmSaknuNi8vO8xLysvEMwr+m6VqIsVAyAQyVnOHiNsExOTp NSimawLczuVjZWYCLDJkfxIJt/rvuCM= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-570-ZbThGZZ8MbqVLrvin3xlrg-1; Wed, 03 Feb 2021 16:08:38 -0500 X-MC-Unique: ZbThGZZ8MbqVLrvin3xlrg-1 Received: by mail-qt1-f197.google.com with SMTP id w3so896885qti.17 for ; Wed, 03 Feb 2021 13:08: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=hi+7C1TTEkkl4WRac4WQ2x2QuL/BvKdq/6D+mj3cbKc=; b=DHpRynwxMbyqWqP8DMfIqC1n49v39D96VuwPgHUJKklWypaLhbGZuO71jI9P+1zh8E eyM4kcM5+Q/jmVcngTSONfqXq/LeSB2utcyEuX4GlLhBtJK53iGA2thGuWVcRTuoxZ6h PuJIQr46yAPxTpogecdhXfI0oSyQuETNL1oVaksnKhEAW/K8CkYiHSTLIVy+VAgWpCSH uHG2/19tR7wrznNC39+VKGvQkmSu0PRl+Ua2m8fG00a1cnPsiLGFdAZfKvmiYNApqv/s /r3F3cwpzKP0VYj/0E0uvYNO/TfLZCyNGL3l+uUriNbwACBHCRjfS30TwceOeeOuF7PE +KDw== X-Gm-Message-State: AOAM5317AIzk9gSxLiv2oilbiGxMYruaG8R0DgImYODNCdceyszC1cGK 3seozNOxJ2VmSwufTUOFAvkHuwaYv6gczR7P1SNr+zTpG06z2EGcL2ZoX2lAmPo6QMwcs25yEv4 JYCaIqQwa+Lo= X-Received: by 2002:a05:622a:109:: with SMTP id u9mr4287325qtw.116.1612386517535; Wed, 03 Feb 2021 13:08:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJwsK56DJ4Ctms//4+fxvCDxExkPh+ZfRXVZ5RIfd/7b2Jecyo9d2wOQoUVa7DDZ2m6IK7Mk1Q== X-Received: by 2002:a05:622a:109:: with SMTP id u9mr4287296qtw.116.1612386517306; Wed, 03 Feb 2021 13:08: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 t6sm2507659qti.2.2021.02.03.13.08.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 13:08:36 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Wei Zhang , Matthew Wilcox , Linus Torvalds , Jason Gunthorpe , Gal Pressman , peterx@redhat.com, Christoph Hellwig , Andrea Arcangeli , Jan Kara , Kirill Shutemov , David Gibson , Mike Rapoport , Mike Kravetz , Kirill Tkhai , Jann Horn , Andrew Morton Subject: [PATCH 1/4] hugetlb: Dedup the code to add a new file_region Date: Wed, 3 Feb 2021 16:08:29 -0500 Message-Id: <20210203210832.113685-2-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210203210832.113685-1-peterx@redhat.com> References: <20210203210832.113685-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(). Signed-off-by: Peter Xu Reviewed-by: Mike Kravetz Reviewed-by: Miaohe Lin --- 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 Wed Feb 3 21:08: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: 12065379 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 BE8F0C433DB for ; Wed, 3 Feb 2021 21:08:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6499A64F5F for ; Wed, 3 Feb 2021 21:08:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6499A64F5F 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 446666B0070; Wed, 3 Feb 2021 16:08:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D0196B0071; Wed, 3 Feb 2021 16:08:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1FE446B0072; Wed, 3 Feb 2021 16:08:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0119.hostedemail.com [216.40.44.119]) by kanga.kvack.org (Postfix) with ESMTP id DA0416B0071 for ; Wed, 3 Feb 2021 16:08:44 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 96D55181AEF15 for ; Wed, 3 Feb 2021 21:08:44 +0000 (UTC) X-FDA: 77778195768.18.feet99_4508bbc275d6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin18.hostedemail.com (Postfix) with ESMTP id 7781A100ED0D0 for ; Wed, 3 Feb 2021 21:08:44 +0000 (UTC) X-HE-Tag: feet99_4508bbc275d6 X-Filterd-Recvd-Size: 5219 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Wed, 3 Feb 2021 21:08:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612386523; 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=anQ4NxVMrC5prvoBF/9r6AhuqrJJE/i32bJ7Mv18iJk=; b=TQ48rzCefnyrVgoooN9lZm3MOkrMZx76CfkiPpkk9zDmjtV03izRojpKt9R7heM75Z5Yzx XenDCEslRRYU0QubkQSrQnikLTVb0X7uR2HS2QWgoaQctx7XdBkiny21RS/3vPJ5LQbr9S u+tpnYJcdhu/aHO66b9KE8uGsN4t7rE= 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-297-er7gTkjdOwCEzW0wP4WAag-1; Wed, 03 Feb 2021 16:08:39 -0500 X-MC-Unique: er7gTkjdOwCEzW0wP4WAag-1 Received: by mail-qv1-f71.google.com with SMTP id d26so492473qve.7 for ; Wed, 03 Feb 2021 13:08:39 -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=anQ4NxVMrC5prvoBF/9r6AhuqrJJE/i32bJ7Mv18iJk=; b=XT/et+93NS1Wu/eq9DPppa6ewcxA7hwAR0WW8FQfHBWUwlUZ9TqN/C2+YFyJBDLUEu wU26rUzDWKTuW9YJ01zoRQd2WzH0sYmwswHILmRjuV6OgXOl7WVzH2WYAKnbYS0SxceC XW9EdpF4OXSes09caCM9OmQx7kskVlC+jdgZi0cBF1cvKO2qnrhOOIutwmeVmK6FW+7T eQUgmfBrroT44DBCswZ2aWJcjQAQPspwy8O5Qrr/9h1ZM5IBw28P2ATAMejBhehpJXAF 6j3qMk29WE2J73qfB6PNLh4O10KXpkE2QX65R0g676M7GA0URrz9pmuPmq+w+hg2DC7m xj/Q== X-Gm-Message-State: AOAM533f30m+V/57ebBE5tMFSL5e4o7WqsRXn51wLfGsKPsnHjRFZZkU ako8gyK3wmGHN637QjUzpHksdW5bNCUow2MyC7JFYoyo9sSkHoBSZsWpM7P2kiUCiqy0MJ/SCih ErvBL8tgX6Uo= X-Received: by 2002:a37:2784:: with SMTP id n126mr4598375qkn.328.1612386519200; Wed, 03 Feb 2021 13:08:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9Tlp/gYAwyauborxxPzXq74J5tG7yADTxCpGSxaHSHAC9pDiZhS+5nbNdTuqIMFlTfXHzVg== X-Received: by 2002:a37:2784:: with SMTP id n126mr4598351qkn.328.1612386518990; Wed, 03 Feb 2021 13:08:38 -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 t6sm2507659qti.2.2021.02.03.13.08.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 13:08:38 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Wei Zhang , Matthew Wilcox , Linus Torvalds , Jason Gunthorpe , Gal Pressman , peterx@redhat.com, Christoph Hellwig , Andrea Arcangeli , Jan Kara , Kirill Shutemov , David Gibson , Mike Rapoport , Mike Kravetz , Kirill Tkhai , Jann Horn , Andrew Morton Subject: [PATCH 2/4] hugetlg: Break earlier in add_reservation_in_range() when we can Date: Wed, 3 Feb 2021 16:08:30 -0500 Message-Id: <20210203210832.113685-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210203210832.113685-1-peterx@redhat.com> References: <20210203210832.113685-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. Signed-off-by: Peter Xu Reviewed-by: Mike Kravetz Reviewed-by: Miaohe Lin --- 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 Wed Feb 3 21:08: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: 12065377 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 CEC77C433E9 for ; Wed, 3 Feb 2021 21:08:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5B42E64F92 for ; Wed, 3 Feb 2021 21:08:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5B42E64F92 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 D210E6B006E; Wed, 3 Feb 2021 16:08:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CCAF36B0070; Wed, 3 Feb 2021 16:08:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B23446B0071; Wed, 3 Feb 2021 16:08:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0103.hostedemail.com [216.40.44.103]) by kanga.kvack.org (Postfix) with ESMTP id 947236B006E for ; Wed, 3 Feb 2021 16:08:44 -0500 (EST) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 5D8598249980 for ; Wed, 3 Feb 2021 21:08:44 +0000 (UTC) X-FDA: 77778195768.01.verse12_190f8ea275d6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin01.hostedemail.com (Postfix) with ESMTP id 36D6610046469 for ; Wed, 3 Feb 2021 21:08:44 +0000 (UTC) X-HE-Tag: verse12_190f8ea275d6 X-Filterd-Recvd-Size: 8338 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Wed, 3 Feb 2021 21:08:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612386522; 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=Tq4TIV/RMG0tEsNDmt/BFFNFbqf+0B+944kupEaRaResYHYrONNMkpVheP2uA+I+YuVpNJ cpyAaoxVE+vGrrNRr5wPJDw9oSC8soejvJrfaUbX9mrnAH498OgCzKnYMMQLqJwHSuJokk eyH0xqzqQ9atwAW+4cj/xrhyo3Q14Nc= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-597-CWz156oqOQSprPYrzayvYQ-1; Wed, 03 Feb 2021 16:08:41 -0500 X-MC-Unique: CWz156oqOQSprPYrzayvYQ-1 Received: by mail-qk1-f200.google.com with SMTP id i11so654488qkn.21 for ; Wed, 03 Feb 2021 13:08:41 -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=N2JmpRHXVVHoLUMHaSLoQE3M9XD7uAhQ81KrMXT1yBHiQDumIkxH38tpFtORkRaymT XqoXzZAip0Oj0CO2K8yhVGhT9mREQmLlcgsIRr/7n4/6+cvyuoTESq8tqD/8kQIakvOc NCTscUvVquoCDtZfYdIHxw5hmsdqsgksZ3U17xpYSqAxzKbuwQn7kdZSBMgOfjhnPMTd sKjT/cJYt7c3N29FgM5+T1u3xpeJTRJdGyXFbhed4IpanocRqgkEVZpa3PIgQOGyzJLM J0/IC5qphLkV1QKKZ8HeISL3t7vytlGjMwjz273mXh1PqHsmNS21ix0Gsrhr0wtQo1f2 OJ9w== X-Gm-Message-State: AOAM532+aiQmldXc8wUZxQ1uAZsBa4OFpzkSQX4C7aLIwfm9tF71tW90 DjcXzo6cDCkFlCXBE30Infv32U9CyXxxf+IVtkE43nYiiX+6jhaqrGwTNRDiryEFByeHKvjCsDM 2lYG78Bv38kY= X-Received: by 2002:ac8:7757:: with SMTP id g23mr4249253qtu.305.1612386520835; Wed, 03 Feb 2021 13:08:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJw38komgAUUDsxRCb/zg9p3zM2x1SPpALss4dFzMM+hvkykqlrxEZLE1vHGsdJt5hpRlnezlw== X-Received: by 2002:ac8:7757:: with SMTP id g23mr4249234qtu.305.1612386520623; Wed, 03 Feb 2021 13:08: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 t6sm2507659qti.2.2021.02.03.13.08.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 13:08:39 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Wei Zhang , Matthew Wilcox , Linus Torvalds , Jason Gunthorpe , Gal Pressman , peterx@redhat.com, Christoph Hellwig , Andrea Arcangeli , Jan Kara , Kirill Shutemov , David Gibson , Mike Rapoport , Mike Kravetz , Kirill Tkhai , Jann Horn , Andrew Morton Subject: [PATCH 3/4] mm: Introduce page_needs_cow_for_dma() for deciding whether cow Date: Wed, 3 Feb 2021 16:08:31 -0500 Message-Id: <20210203210832.113685-4-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210203210832.113685-1-peterx@redhat.com> References: <20210203210832.113685-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 Wed Feb 3 21:08: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: 12065381 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 EE64AC433DB for ; Wed, 3 Feb 2021 21:08:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9ACD864F60 for ; Wed, 3 Feb 2021 21:08:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9ACD864F60 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 A85666B0071; Wed, 3 Feb 2021 16:08:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E8756B0072; Wed, 3 Feb 2021 16:08:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 813936B0073; Wed, 3 Feb 2021 16:08:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0226.hostedemail.com [216.40.44.226]) by kanga.kvack.org (Postfix) with ESMTP id 5D8876B0071 for ; Wed, 3 Feb 2021 16:08:46 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 1B88E1EF1 for ; Wed, 3 Feb 2021 21:08:46 +0000 (UTC) X-FDA: 77778195852.11.water87_3200f37275d6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id E07D1180F8B81 for ; Wed, 3 Feb 2021 21:08:45 +0000 (UTC) X-HE-Tag: water87_3200f37275d6 X-Filterd-Recvd-Size: 8306 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Wed, 3 Feb 2021 21:08:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612386524; 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=CAR5UYzoAPy14j76vk233Tw9s0t/A46HICdSqrKjEFQ=; b=Oxqf5uGskzjt3YIm3gS2xwucn05MJPScPWJI844xlTIodeBE/VT09EWnyvy2RZijylBPoC 7LsWW/TQ4PWZaVYnMBxFObe1mSeOZ2ItBueL4STmqLo3qi9UuttPBwmORVyVa4EE5KutEZ SvIgwha4pBNY2GhP2nAReRdnVdmALew= 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-545-oyn7w1bPOPGWnHTnWVXx7w-1; Wed, 03 Feb 2021 16:08:43 -0500 X-MC-Unique: oyn7w1bPOPGWnHTnWVXx7w-1 Received: by mail-qv1-f69.google.com with SMTP id h13so468784qvo.18 for ; Wed, 03 Feb 2021 13:08: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=CAR5UYzoAPy14j76vk233Tw9s0t/A46HICdSqrKjEFQ=; b=QwBoXZA0hbl3HxqOQ8rBBnFbpCQMx6DSMJAop1nJ1VkI5JzoMBab0JAFU/mgTlCF4f GxOHT4ATghq4OsMoIedKIQvFXE/5cMoQUZ4ygWacK76uwICJbJqsvXj4O3Jt8uRB3UcO EmPligxNL1JRvMaC14Lp28FsAFTV51ebTFMb9KNxmTc5Sl1vp7pLrcEj9Zjo6YVIMrQN rcvwt0ObKkTiL8w6ImhqFJ8urAFgEmQzNRPUseFS+ZsCaHfuifAc6LOUXFG6XEaXzNDm 1Dh9CXg7Tw6WmJQSHvclGdZP/dwm36iXGYjTiwDg1+WE9O2WlLFvrImoSA4MYjn7FJTu mh+g== X-Gm-Message-State: AOAM533xnR+ByG9dfV2wtvjEqJusTl/ZMkc5nKdFrg6AaT2zbHaEwplp GfE4glnGrCQ7m9ya54LteZ05aCouLPgDBzg9Sj2atg5K5cKApa1DloL4zJp9pLSymSM5zNUIitK mmK+duF0ZdMc= X-Received: by 2002:ac8:82b:: with SMTP id u40mr4391918qth.332.1612386522676; Wed, 03 Feb 2021 13:08:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJwVVt5kjHSDTzY5qynUdx3KoddfTDU7c0XF5xIku0zmBdUNWjFgD59RCmSGKUsLIoLJfGy42A== X-Received: by 2002:ac8:82b:: with SMTP id u40mr4391895qth.332.1612386522444; Wed, 03 Feb 2021 13:08: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 t6sm2507659qti.2.2021.02.03.13.08.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 13:08:41 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Wei Zhang , Matthew Wilcox , Linus Torvalds , Jason Gunthorpe , Gal Pressman , peterx@redhat.com, Christoph Hellwig , Andrea Arcangeli , Jan Kara , Kirill Shutemov , David Gibson , Mike Rapoport , Mike Kravetz , Kirill Tkhai , Jann Horn , Andrew Morton Subject: [PATCH 4/4] hugetlb: Do early cow when page pinned on src mm Date: Wed, 3 Feb 2021 16:08:32 -0500 Message-Id: <20210203210832.113685-5-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210203210832.113685-1-peterx@redhat.com> References: <20210203210832.113685-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 | 76 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9e6ea96bf33b..931bf1a81c16 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,54 @@ 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); + + if (unlikely(page_needs_cow_for_dma(vma, ptepage))) { + /* This is very possibly a pinned huge page */ + if (!prealloc) { + /* + * Preallocate the huge page without + * tons of locks since we could sleep. + * Note: we can't use any reservation + * because the page will be exclusively + * owned by the child later. + */ + put_page(ptepage); + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + prealloc = alloc_huge_page(vma, addr, 0); + if (!prealloc) { + /* + * hugetlb_cow() seems to be + * more careful here than us. + * However for fork() we could + * be strict not only because + * no one should be referencing + * the child mm yet, but also + * if resources are rare we'd + * better simply fail the + * fork() even earlier. + */ + ret = -ENOMEM; + break; + } + goto again; + } + /* + * We have page preallocated so that we can do + * the copy right now. + */ + 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 +3890,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 +3904,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; }