From patchwork Fri Feb 5 16:54:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12070471 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 0305AC433DB for ; Fri, 5 Feb 2021 16:57:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6C7E264F08 for ; Fri, 5 Feb 2021 16:56:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6C7E264F08 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 E60676B0074; Fri, 5 Feb 2021 11:56:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E10E96B0075; Fri, 5 Feb 2021 11:56:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D28826B0078; Fri, 5 Feb 2021 11:56:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0167.hostedemail.com [216.40.44.167]) by kanga.kvack.org (Postfix) with ESMTP id BCA516B0074 for ; Fri, 5 Feb 2021 11:56:58 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 81334180AD822 for ; Fri, 5 Feb 2021 16:56:58 +0000 (UTC) X-FDA: 77784818916.11.AEDF2E6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 86244C00CEF9 for ; Fri, 5 Feb 2021 16:53:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612544014; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tR/UuXaqBaROltUbV2RDYpoyLXOK1N4qjZ6UzYcMHb0=; b=f2TfroY1gue1vWX7Scmh/i7tKLZcF3l/eAqxWfHCWUwiFXiAEF2Mc6dSYmCzSZEiuWjYYr qhFU4wM+YOMB6Q0y5+YexeffIajdzEnwGJZ7K4GXGyqAjq0+XmzCGaAFQzThfl8rkC8RxB 15B3J767qZgrXQBbAJiKQDYFnybkaF4= 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-539-DrOsQDanOtmwbY-l5Ru3Mg-1; Fri, 05 Feb 2021 11:53:33 -0500 X-MC-Unique: DrOsQDanOtmwbY-l5Ru3Mg-1 Received: by mail-qv1-f71.google.com with SMTP id j4so5453692qvi.8 for ; Fri, 05 Feb 2021 08:53:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tR/UuXaqBaROltUbV2RDYpoyLXOK1N4qjZ6UzYcMHb0=; b=UQWKck1dTjXf8abLEvHUiFqcU8NPAf1yrIogpVNDQ99blhiDW1E1+m4UKFKW0ztM6j scSLcXyehDKHuTSMpd48TIpATSW0Da3uGZBek+HC9/BkjhvNbwgxO3cSnk/bArSW3UGG JamLsJRKKgMtL4NYXvqV4VN0QALL96SKazG0oZNtC/lQCYGX4eiz8NXoQ15tU4GbLhuw l0oSkblW0SMiZsl1O1EbwA3Naw8WYYXel85es4zMhXQxOPgA6q7pHiUiEtCi1X4u7suP xSHMa4kBOtgSjt5ZUeFfwQjiLFe37TrWLLkQvcB/rEIBx4Ptju577zZwfyq1sZnlSDg+ TmfQ== X-Gm-Message-State: AOAM5321DKILm3BPaD5LdIRo25YfaZe4raDNnMtyGdmm2pLQ4jG9lkLR 2N81HsievNyfU5L8r8gVaaXKuL4mVCCqNIR1UI/gPLjlGa3co+3+KuJD+fwi6865OuQHUktdaCL s0IZO7Wl0dn0q/zf/J5fg2W3bppRs7bSWwCxtc0cpQ8sD7SI7mOWI7WjRt6js X-Received: by 2002:ac8:23f0:: with SMTP id r45mr4834217qtr.345.1612544012782; Fri, 05 Feb 2021 08:53:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJyJLoIssHDvkJfBmCjyZhFqfntCVz8qYhtn6INdIk/dYPa81yrmZmAl6ZslVudgD8VCc3c9gQ== X-Received: by 2002:ac8:23f0:: with SMTP id r45mr4834171qtr.345.1612544012473; Fri, 05 Feb 2021 08:53:32 -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 12sm9618893qkg.39.2021.02.05.08.53.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 08:53:31 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Andrea Arcangeli , Christoph Hellwig , Miaohe Lin , Kirill Shutemov , Jann Horn , Matthew Wilcox , Jan Kara , Jason Gunthorpe , Linus Torvalds , Mike Rapoport , David Gibson , Mike Kravetz , peterx@redhat.com, Kirill Tkhai , Wei Zhang , Andrew Morton Subject: [PATCH v3 1/5] hugetlb: Dedup the code to add a new file_region Date: Fri, 5 Feb 2021 11:54:02 -0500 Message-Id: <20210205165406.4655-2-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210205165406.4655-1-peterx@redhat.com> References: <20210205165406.4655-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: 69ax4thww3nzfk6cqrmtccair7ketabp X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 86244C00CEF9 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf06; 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: 1612544019-96447 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce hugetlb_resv_map_add() helper to add a new file_region rather than duplication the similar code twice in add_reservation_in_range(). Reviewed-by: Mike Kravetz Reviewed-by: Miaohe Lin Signed-off-by: Peter Xu --- mm/hugetlb.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 6ef278ecf7ff..ec8e29c805fe 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -331,6 +331,24 @@ static void coalesce_file_region(struct resv_map *resv, struct file_region *rg) } } +static inline long +hugetlb_resv_map_add(struct resv_map *map, struct file_region *rg, long from, + long to, struct hstate *h, struct hugetlb_cgroup *cg, + long *regions_needed) +{ + struct file_region *nrg; + + if (!regions_needed) { + nrg = get_file_region_entry_from_cache(map, from, to); + record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg); + list_add(&nrg->link, rg->link.prev); + coalesce_file_region(map, nrg); + } else + *regions_needed += 1; + + return to - from; +} + /* * Must be called with resv->lock held. * @@ -346,7 +364,7 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, long add = 0; struct list_head *head = &resv->regions; long last_accounted_offset = f; - struct file_region *rg = NULL, *trg = NULL, *nrg = NULL; + struct file_region *rg = NULL, *trg = NULL; if (regions_needed) *regions_needed = 0; @@ -375,18 +393,11 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, /* Add an entry for last_accounted_offset -> rg->from, and * update last_accounted_offset. */ - if (rg->from > last_accounted_offset) { - add += rg->from - last_accounted_offset; - if (!regions_needed) { - nrg = get_file_region_entry_from_cache( - resv, last_accounted_offset, rg->from); - record_hugetlb_cgroup_uncharge_info(h_cg, h, - resv, nrg); - list_add(&nrg->link, rg->link.prev); - coalesce_file_region(resv, nrg); - } else - *regions_needed += 1; - } + if (rg->from > last_accounted_offset) + add += hugetlb_resv_map_add(resv, rg, + last_accounted_offset, + rg->from, h, h_cg, + regions_needed); last_accounted_offset = rg->to; } @@ -394,17 +405,9 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, /* Handle the case where our range extends beyond * last_accounted_offset. */ - if (last_accounted_offset < t) { - add += t - last_accounted_offset; - if (!regions_needed) { - nrg = get_file_region_entry_from_cache( - resv, last_accounted_offset, t); - record_hugetlb_cgroup_uncharge_info(h_cg, h, resv, nrg); - list_add(&nrg->link, rg->link.prev); - coalesce_file_region(resv, nrg); - } else - *regions_needed += 1; - } + if (last_accounted_offset < t) + add += hugetlb_resv_map_add(resv, rg, last_accounted_offset, + t, h, h_cg, regions_needed); VM_BUG_ON(add < 0); return add; From patchwork Fri Feb 5 16:54:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12070461 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 98D53C433DB for ; Fri, 5 Feb 2021 16:53:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0ED7B64EE0 for ; Fri, 5 Feb 2021 16:53:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0ED7B64EE0 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 9B0916B0073; Fri, 5 Feb 2021 11:53:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 95F716B0074; Fri, 5 Feb 2021 11:53:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78CBC6B0075; Fri, 5 Feb 2021 11:53:39 -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 4FB156B0073 for ; Fri, 5 Feb 2021 11:53:39 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 7CEE43626 for ; Fri, 5 Feb 2021 16:53:38 +0000 (UTC) X-FDA: 77784810516.19.moon67_320d083275e6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin19.hostedemail.com (Postfix) with ESMTP id 442ED1AD1B3 for ; Fri, 5 Feb 2021 16:53:38 +0000 (UTC) X-HE-Tag: moon67_320d083275e6 X-Filterd-Recvd-Size: 5403 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Fri, 5 Feb 2021 16:53:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612544016; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Co5gWVPolRGWSV6qsroagZOgnBTQI/bBXcBdWtmnAnQ=; b=VIRV7vaZHlv32/h4swlP5iUUoCtIJ8y9H7vxK/nFD/n68Hls9dJzR4Luh3HVdyE+BFlb86 PrchROI6OPIAW5pt7OMTSSFgoGsDE6HUPpoFjIKDrtm9o3ORgyYJAeTfABJSzD7Eggo5Pb WdgF3JjrTdllMs7g017AIqqdvceHX0o= 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-490-ZRSwxjlMMnGh9qUNsWIFHQ-1; Fri, 05 Feb 2021 11:53:35 -0500 X-MC-Unique: ZRSwxjlMMnGh9qUNsWIFHQ-1 Received: by mail-qt1-f199.google.com with SMTP id c19so5696864qtp.2 for ; Fri, 05 Feb 2021 08:53:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Co5gWVPolRGWSV6qsroagZOgnBTQI/bBXcBdWtmnAnQ=; b=msQpR/S1sQ9EGtnYqNFdgioVW/MonzfE6JyD+3Ryll/EZtVyNFL5Im9e3Vq65Dxh8Q UxL97JmZxHtBjm7Hd2rvlrzLLoS3MxTluUSg/pAvkdiwoE5O2WtzpX+bj5cto7ifo1Xj zGcFQrG74rzHYJ6mtZlzPj6/vPdG7Y/dV9AktBsmhMRcT5z7A/IWdBIcmCqjbJXjPfg8 dXTnukoWhWgrJCqVj2I8bOQjBmCCXyLm71lVJOScI1XPJzKzS94RX7xzO80aVshdp7NX upsq6mck2MCXKcqxf1GauyM4KovWMRRYd4E1ymgvzVo0WuOnY727qxEnU+EOIe7zmykv QWBg== X-Gm-Message-State: AOAM530O7IiiPQw6fPQ9g2YpZ6BXrksUBf6K2keQlAy4dGjE1bbBzK5L fq3CqgRx+APfxvlmpbO2Dhi8DuMuPDQGhwWvJ80KFi99V4oU3cyn3ViMb8ql6TS4zr+10EcFZaa jDAY5st/57DGpZwThu4stgkcnz/ZF1o3opKKOPqTtmGC63OS5aIblNtU9WNDR X-Received: by 2002:aed:3aa2:: with SMTP id o31mr822984qte.353.1612544014574; Fri, 05 Feb 2021 08:53:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJyKc6PYm/orcl/kVkLHYNyCRK5wd2QouIxAqDztwVwMw7bYTeEXrpzk1qdQmoRBmevHeSICGw== X-Received: by 2002:aed:3aa2:: with SMTP id o31mr822948qte.353.1612544014300; Fri, 05 Feb 2021 08:53:34 -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 12sm9618893qkg.39.2021.02.05.08.53.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 08:53:33 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Andrea Arcangeli , Christoph Hellwig , Miaohe Lin , Kirill Shutemov , Jann Horn , Matthew Wilcox , Jan Kara , Jason Gunthorpe , Linus Torvalds , Mike Rapoport , David Gibson , Mike Kravetz , peterx@redhat.com, Kirill Tkhai , Wei Zhang , Andrew Morton Subject: [PATCH v3 2/5] hugetlg: Break earlier in add_reservation_in_range() when we can Date: Fri, 5 Feb 2021 11:54:03 -0500 Message-Id: <20210205165406.4655-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210205165406.4655-1-peterx@redhat.com> References: <20210205165406.4655-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: All the regions maintained in hugetlb reserved map is inclusive on "from" but exclusive on "to". We can break earlier even if rg->from==t because it already means no possible intersection. This does not need a Fixes in all cases because when it happens (rg->from==t) we'll not break out of the loop while we should, however the next thing we'd do is still add the last file_region we'd need and quit the loop in the next round. So this change is not a bugfix (since the old code should still run okay iiuc), but we'd better still touch it up to make it logically sane. Reviewed-by: Mike Kravetz Reviewed-by: Miaohe Lin Signed-off-by: Peter Xu --- mm/hugetlb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ec8e29c805fe..71ccec5c3817 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -387,7 +387,7 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, /* When we find a region that starts beyond our range, we've * finished. */ - if (rg->from > t) + if (rg->from >= t) break; /* Add an entry for last_accounted_offset -> rg->from, and From patchwork Fri Feb 5 16:54:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12070463 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 C6EB2C433E0 for ; Fri, 5 Feb 2021 16:53:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3E68564F06 for ; Fri, 5 Feb 2021 16:53:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3E68564F06 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 431C06B0074; Fri, 5 Feb 2021 11:53:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 370936B0075; Fri, 5 Feb 2021 11:53:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D8126B0078; Fri, 5 Feb 2021 11:53:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0020.hostedemail.com [216.40.44.20]) by kanga.kvack.org (Postfix) with ESMTP id E96466B0074 for ; Fri, 5 Feb 2021 11:53:39 -0500 (EST) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id ADE55181AEF15 for ; Fri, 5 Feb 2021 16:53:39 +0000 (UTC) X-FDA: 77784810558.02.trade43_3814410275e6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id 8568C10097AA1 for ; Fri, 5 Feb 2021 16:53:39 +0000 (UTC) X-HE-Tag: trade43_3814410275e6 X-Filterd-Recvd-Size: 8418 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Fri, 5 Feb 2021 16:53:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612544018; 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=TwxcdMSyfJ8S06/346B51A7/s/B5gXuJmv9CWCzGamY=; b=cLPwP+6bkpoVQeKhQ+V9FtPcpkHa1QkRVj+e9nuImC/Sa/4hJ0fhUn0W3t+Bh2g41NpQ1o kBdTogCscdekCXZZcAspqy2XkBWcTg8uTtE+T+IQ9gH6EfDqPWwzRAuKSIAAJfm1X5M0HG L7nAhdzsERpwEjmadFjYNahtDsPJVbY= 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-72-cBiHQOtmPzuK5ORFFfmcmw-1; Fri, 05 Feb 2021 11:53:37 -0500 X-MC-Unique: cBiHQOtmPzuK5ORFFfmcmw-1 Received: by mail-qk1-f200.google.com with SMTP id o16so6341014qkj.15 for ; Fri, 05 Feb 2021 08:53:37 -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=TwxcdMSyfJ8S06/346B51A7/s/B5gXuJmv9CWCzGamY=; b=FSnCstyTj4oJPxAB+yshF9HklswISpowPrH94b1Xo85PPwQvI4I7rxF83BGBPM1Knr l7gFHFj9IZZGBZM+SiFv1aUmAsR14kwckVibF6CaMxV31MDmpyT4rolH8RXadPlkgspN NnEdY/fGZGuFXb8++t6tTZM5Zp7cjrCPNZUkP2hG5rM3nV6je/k82JMK88O2dypSr6lg /f4yxasyt49vo/GQRxdFXmjPEiy1jP6Q13TMI/ZuW7jeHrX5tq2uxL11QsjOwxeL7vEE UmarZeri+MMYag6RjdZrGw1PUA0+lPydywadmAqsD+C9AUBRGJP91pWL0Gw+sX8gV0fb B/8w== X-Gm-Message-State: AOAM532bLnOatY9+Hv636O2g/DbDjw5cEBexP/9mYobT/ShvO3jJZpSb IBQ9NU2tF2sgl6eEn4Gl1+T8tRx9T8Rz+hbrSd2MnZ+/oeL++DVUj7gBhAMUQC7sCTEvyWGjCg3 pzS6jotlZLL2xwe7zlQW0sWyHdsu7O+8tJF1Q7j7iGdi92/2WOd9FjUYi1O57 X-Received: by 2002:ac8:7b45:: with SMTP id m5mr4945687qtu.323.1612544016389; Fri, 05 Feb 2021 08:53:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJzVjBeNTfdHJRDZsNfbVbE79qOyKTx11cyhQle8vLNZybbu3lrutJz+pRVO0m7DKhLfr4sYKg== X-Received: by 2002:ac8:7b45:: with SMTP id m5mr4945654qtu.323.1612544016071; Fri, 05 Feb 2021 08:53:36 -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 12sm9618893qkg.39.2021.02.05.08.53.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 08:53:35 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Andrea Arcangeli , Christoph Hellwig , Miaohe Lin , Kirill Shutemov , Jann Horn , Matthew Wilcox , Jan Kara , Jason Gunthorpe , Linus Torvalds , Mike Rapoport , David Gibson , Mike Kravetz , peterx@redhat.com, Kirill Tkhai , Wei Zhang , Andrew Morton Subject: [PATCH v3 3/5] mm: Introduce page_needs_cow_for_dma() for deciding whether cow Date: Fri, 5 Feb 2021 11:54:04 -0500 Message-Id: <20210205165406.4655-4-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210205165406.4655-1-peterx@redhat.com> References: <20210205165406.4655-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 904e2517cd45..2e555d57631f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1314,6 +1314,27 @@ static inline bool page_maybe_dma_pinned(struct page *page) GUP_PIN_COUNTING_BIAS; } +static inline bool is_cow_mapping(vm_flags_t flags) +{ + return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; +} + +/* + * This should most likely only be called during fork() to see whether we + * should break the cow immediately for a page on the src mm. + */ +static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma, + struct page *page) +{ + if (!is_cow_mapping(vma->vm_flags)) + return false; + + if (!atomic_read(&vma->vm_mm->has_pinned)) + return false; + + return page_maybe_dma_pinned(page); +} + #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP) #define SECTION_IN_PAGE_FLAGS #endif diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 987cf5e4cf90..57f5c7d3a328 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1097,9 +1097,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, * best effort that the pinned pages won't be replaced by another * random page during the coming copy-on-write. */ - if (unlikely(is_cow_mapping(vma->vm_flags) && - atomic_read(&src_mm->has_pinned) && - page_maybe_dma_pinned(src_page))) { + if (unlikely(page_needs_cow_for_dma(vma, src_page))) { pte_free(dst_mm, pgtable); spin_unlock(src_ptl); spin_unlock(dst_ptl); @@ -1211,9 +1209,7 @@ int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, } /* Please refer to comments in copy_huge_pmd() */ - if (unlikely(is_cow_mapping(vma->vm_flags) && - atomic_read(&src_mm->has_pinned) && - page_maybe_dma_pinned(pud_page(pud)))) { + if (unlikely(page_needs_cow_for_dma(vma, pud_page(pud)))) { spin_unlock(src_ptl); spin_unlock(dst_ptl); __split_huge_pud(vma, src_pud, addr); diff --git a/mm/internal.h b/mm/internal.h index 8e9c660f33ca..a24847e48081 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -296,11 +296,6 @@ static inline unsigned int buddy_order(struct page *page) */ #define buddy_order_unsafe(page) READ_ONCE(page_private(page)) -static inline bool is_cow_mapping(vm_flags_t flags) -{ - return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; -} - /* * These three helpers classifies VMAs for virtual memory accounting. */ diff --git a/mm/memory.c b/mm/memory.c index 9d68a2340589..cd28871be559 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -812,9 +812,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, @@ -828,9 +825,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 Fri Feb 5 16:54:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12070465 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 106F3C433E0 for ; Fri, 5 Feb 2021 16:53:44 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7DAB264EE0 for ; Fri, 5 Feb 2021 16:53:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7DAB264EE0 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 2B0DE6B0075; Fri, 5 Feb 2021 11:53:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C1766B007B; Fri, 5 Feb 2021 11:53:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 023D76B0075; Fri, 5 Feb 2021 11:53:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0233.hostedemail.com [216.40.44.233]) by kanga.kvack.org (Postfix) with ESMTP id DBCAC6B0075 for ; Fri, 5 Feb 2021 11:53:41 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 99DB83626 for ; Fri, 5 Feb 2021 16:53:41 +0000 (UTC) X-FDA: 77784810642.03.duck19_100624c275e6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id 74C8728A4E9 for ; Fri, 5 Feb 2021 16:53:41 +0000 (UTC) X-HE-Tag: duck19_100624c275e6 X-Filterd-Recvd-Size: 7807 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf19.hostedemail.com (Postfix) with ESMTP for ; Fri, 5 Feb 2021 16:53:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612544020; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=45ThGTqfFb6/yx2k3kPs+btJTG2mPFONj9RwvqJLKYY=; b=O4JFbhd6PRJ25jNdPFlITDabnD1o+fb6w0t/Bab1z6MM99BOUim8Crya7GUNJqGBG9N0a8 rtl2QLhpIZEuf4LSaovOs/3PL2fDFlz7KQ+vnSnVs8xCAbh33y3NqmMUN0a0rxSPgv9p/C x2Juk+jCeEFAykVEnnhfUyAVFoa7mFo= 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-334-C5mE1GZ-OPK_ctvhgZMQTA-1; Fri, 05 Feb 2021 11:53:39 -0500 X-MC-Unique: C5mE1GZ-OPK_ctvhgZMQTA-1 Received: by mail-qt1-f199.google.com with SMTP id w3so5659165qti.17 for ; Fri, 05 Feb 2021 08:53: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=45ThGTqfFb6/yx2k3kPs+btJTG2mPFONj9RwvqJLKYY=; b=E7P3r++BPOoNi51SNH6eKb3yxX3s7GFZR3/Gjys/l1vtgCQNSTA2Oj/dgpbSZnMcAi cHKwq8j1DMXdaBqjQA4fYFuLO02dK1RGiZVwl4HzMy1GfDVKlEupHD+EjO7IVnx9Ezfk awHFzVtPUC0R/Qc2OlDIysL0GvJNZ76NF1M2PjkeD6qtpMQL2Qt4Ya5e2muorjkR8Z/G +iFLXxuHLNMtpq0OoQpHxZEn3CPK/6nDBeOOYzzEEPMRjVKogoMTU3F/PM3PCjsS7hYJ y+N107bC+Jjv5oKQOsEluGQtF9QnMdUaEwGnu+f9ji4zKNF0liSUMwenYC+ec78O+Fob 5zmw== X-Gm-Message-State: AOAM531fyMBi4p0U+UXfLOf3fE/gTBj0lkz2nyJmXmk6hcTOqJEE/tdb qqugPdWA/MVHwCLO+VU5APyb9iNhydEsn1AuhB8VB6JEJr/QBHWJ+e2XqKNbmpwCAUaov18Y3Zj HQsPTNo7i9dTtOygULoSXVXwowZ8n4h5mOH1ubSjAKoMyUlVJDR7stjoaUVqs X-Received: by 2002:a37:d01:: with SMTP id 1mr5220152qkn.247.1612544018364; Fri, 05 Feb 2021 08:53:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJxioHYSeFu7FIORv42JUdT5KiGh790cz4iF+AF9w2tOueheZlZAI5UINMF2g+tHX8/8AUMikA== X-Received: by 2002:a37:d01:: with SMTP id 1mr5220104qkn.247.1612544018061; Fri, 05 Feb 2021 08:53: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 12sm9618893qkg.39.2021.02.05.08.53.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 08:53:37 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Andrea Arcangeli , Christoph Hellwig , Miaohe Lin , Kirill Shutemov , Jann Horn , Matthew Wilcox , Jan Kara , Jason Gunthorpe , Linus Torvalds , Mike Rapoport , David Gibson , Mike Kravetz , peterx@redhat.com, Kirill Tkhai , Wei Zhang , Andrew Morton , VMware Graphics , Roland Scheidegger , David Airlie , Daniel Vetter , Alexey Dobriyan Subject: [PATCH v3 4/5] mm: Use is_cow_mapping() across tree where proper Date: Fri, 5 Feb 2021 11:54:05 -0500 Message-Id: <20210205165406.4655-5-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210205165406.4655-1-peterx@redhat.com> References: <20210205165406.4655-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 0a900afc66ff..45c9c6a7f1d6 100644 --- a/drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c @@ -500,8 +500,6 @@ vm_fault_t vmw_bo_vm_huge_fault(struct vm_fault *vmf, vm_fault_t ret; pgoff_t fault_page_size; bool write = vmf->flags & FAULT_FLAG_WRITE; - bool is_cow_mapping = - (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; switch (pe_size) { case PE_SIZE_PMD: @@ -518,7 +516,7 @@ vm_fault_t vmw_bo_vm_huge_fault(struct vm_fault *vmf, } /* Always do write dirty-tracking and COW on PTE level. */ - if (write && (READ_ONCE(vbo->dirty) || is_cow_mapping)) + if (write && (READ_ONCE(vbo->dirty) || is_cow_mapping(vma->vm_flags))) return VM_FAULT_FALLBACK; ret = ttm_bo_vm_reserve(bo, vmf); diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c index 3c03b1746661..cb9975889e2f 100644 --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c @@ -49,7 +49,7 @@ int vmw_mmap(struct file *filp, struct vm_area_struct *vma) vma->vm_ops = &vmw_vm_ops; /* Use VM_PFNMAP rather than VM_MIXEDMAP if not a COW mapping */ - if ((vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) != VM_MAYWRITE) + if (!is_cow_mapping(vma->vm_flags)) vma->vm_flags = (vma->vm_flags & ~VM_MIXEDMAP) | VM_PFNMAP; return 0; diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 602e3a52884d..96c1682025f9 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1036,8 +1036,6 @@ struct clear_refs_private { #ifdef CONFIG_MEM_SOFT_DIRTY -#define is_cow_mapping(flags) (((flags) & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE) - static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte) { struct page *page; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 71ccec5c3817..620700f05ff4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3733,15 +3733,13 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, pte_t *src_pte, *dst_pte, entry, dst_entry; struct page *ptepage; unsigned long addr; - int cow; + int cow = is_cow_mapping(vma->vm_flags); struct hstate *h = hstate_vma(vma); unsigned long sz = huge_page_size(h); struct address_space *mapping = vma->vm_file->f_mapping; struct mmu_notifier_range range; int ret = 0; - cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; - if (cow) { mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, src, vma->vm_start, From patchwork Fri Feb 5 16:54:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12070467 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 40012C433DB for ; Fri, 5 Feb 2021 16:53:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BE70264F06 for ; Fri, 5 Feb 2021 16:53:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BE70264F06 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 DCFE26B0078; Fri, 5 Feb 2021 11:53:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D381B6B007B; Fri, 5 Feb 2021 11:53:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B62636B007D; Fri, 5 Feb 2021 11:53:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0252.hostedemail.com [216.40.44.252]) by kanga.kvack.org (Postfix) with ESMTP id 9617B6B0078 for ; Fri, 5 Feb 2021 11:53:43 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 54ED2180AD822 for ; Fri, 5 Feb 2021 16:53:43 +0000 (UTC) X-FDA: 77784810726.09.laugh02_2d0892a275e6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 3A25A180AD80F for ; Fri, 5 Feb 2021 16:53:43 +0000 (UTC) X-HE-Tag: laugh02_2d0892a275e6 X-Filterd-Recvd-Size: 8320 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf20.hostedemail.com (Postfix) with ESMTP for ; Fri, 5 Feb 2021 16:53:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612544022; 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=NBcsouZewuvk8KZODy/tJjYpoCFexLf8th1hdmd2fdU=; b=YAZJwhm0fXlDQSOhj2imMPl9RHKpUzrFTCgvxFi9/UTuFrwWGMm8LMhKwTnwN0xP5phoA2 dzMmuLFU/H/WBj6h+2YJxM8ZkCinQqTeZBe1N1wT+YBEaxImsOZUrHf83D+HvmnGJP/yIo zw9R7hhrwrEFwmAq9RdxMI2VjTQjhe4= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-584-sab53n-CO0qxaxo-tAEOMg-1; Fri, 05 Feb 2021 11:53:40 -0500 X-MC-Unique: sab53n-CO0qxaxo-tAEOMg-1 Received: by mail-qv1-f72.google.com with SMTP id j4so5453931qvi.8 for ; Fri, 05 Feb 2021 08:53: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=NBcsouZewuvk8KZODy/tJjYpoCFexLf8th1hdmd2fdU=; b=hjfhnoNgv+mY7wEXZs+6kL7adPb8tNjMRVLjFoaIEh8uPo6IrftTr5WFnNyPhvSA6B 2O90GE9iYXRB1OP7qBjVoa/Wy6fZ2awzwtck4z3wSfw2uTxZ276RjPZ/uCdXstHt6xN8 Zm3kLBlIEBZZDncdLMmkPUNs5y7PVvXYEur69qDHculZdpWIxIhKMolgPWLCv3weQZNU TpY0lxt9vWCPNMnWs17u7rk7c0fPGjWQxA4alroM5NBGXaz+BVQGs6zhIJ3zmNPDytWf /72tYm8V0xyq1eztLaxYYU3nsnpvnsv8M0zyQl135ySaIJZkN+vMwr5KNwFhI1ybXHI4 VJJA== X-Gm-Message-State: AOAM533VmO1VzYtk+pwu4j8o06x1D00I7TyPF9HM8UZrIeMWo+HVimY1 M43fvfsSkjQngMRe3kLb3yldSj4JbGqCN9gGf0az7TtYlC304++xjWFvNJlF1ZVksCR+oWQK5Pi W5+I1nap1FmfhV9WLaepGj1YhVg/PJtYxuK4S/czipbVO+OMuRqOpQpwO8w6i X-Received: by 2002:ac8:5c0a:: with SMTP id i10mr4900107qti.239.1612544019970; Fri, 05 Feb 2021 08:53:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFpCiS7vlmmziN/8ahxv+juOeWp1Z1zPJrlPr5nMSaR9n55md55zNusgN1cZRy2ABzzKQMqw== X-Received: by 2002:ac8:5c0a:: with SMTP id i10mr4900071qti.239.1612544019658; Fri, 05 Feb 2021 08:53: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 12sm9618893qkg.39.2021.02.05.08.53.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 08:53:39 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Andrea Arcangeli , Christoph Hellwig , Miaohe Lin , Kirill Shutemov , Jann Horn , Matthew Wilcox , Jan Kara , Jason Gunthorpe , Linus Torvalds , Mike Rapoport , David Gibson , Mike Kravetz , peterx@redhat.com, Kirill Tkhai , Wei Zhang , Andrew Morton Subject: [PATCH v3 5/5] hugetlb: Do early cow when page pinned on src mm Date: Fri, 5 Feb 2021 11:54:06 -0500 Message-Id: <20210205165406.4655-6-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210205165406.4655-1-peterx@redhat.com> References: <20210205165406.4655-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 Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 620700f05ff4..7c1a0ecc130e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3727,6 +3727,18 @@ static bool is_hugetlb_entry_hwpoisoned(pte_t pte) return false; } +static void +hugetlb_install_page(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr, + struct page *new_page) +{ + __SetPageUptodate(new_page); + set_huge_pte_at(vma->vm_mm, addr, ptep, make_huge_pte(vma, new_page, 1)); + hugepage_add_new_anon_rmap(new_page, vma, addr); + hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm); + ClearHPageRestoreReserve(new_page); + SetHPageMigratable(new_page); +} + int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma) { @@ -3736,6 +3748,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, int cow = is_cow_mapping(vma->vm_flags); struct hstate *h = hstate_vma(vma); unsigned long sz = huge_page_size(h); + unsigned long npages = pages_per_huge_page(h); struct address_space *mapping = vma->vm_file->f_mapping; struct mmu_notifier_range range; int ret = 0; @@ -3784,6 +3797,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); entry = huge_ptep_get(src_pte); dst_entry = huge_ptep_get(dst_pte); +again: if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) { /* * Skip if src entry none. Also, skip in the @@ -3807,6 +3821,52 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, } set_huge_swap_pte_at(dst, addr, dst_pte, entry, sz); } else { + entry = huge_ptep_get(src_pte); + ptepage = pte_page(entry); + get_page(ptepage); + + /* + * This is a rare case where we see pinned hugetlb + * pages while they're prone to COW. We need to do the + * COW earlier during fork. + * + * When pre-allocating the page or copying data, we + * need to be without the pgtable locks since we could + * sleep during the process. + */ + if (unlikely(page_needs_cow_for_dma(vma, ptepage))) { + pte_t src_pte_old = entry; + struct page *new; + + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + /* Do not use reserve as it's private owned */ + new = alloc_huge_page(vma, addr, 1); + if (IS_ERR(new)) { + put_page(ptepage); + ret = PTR_ERR(new); + break; + } + copy_user_huge_page(new, ptepage, addr, vma, + npages); + put_page(ptepage); + + /* Install the new huge page if src pte stable */ + dst_ptl = huge_pte_lock(h, dst, dst_pte); + src_ptl = huge_pte_lockptr(h, src, src_pte); + spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); + entry = huge_ptep_get(src_pte); + if (!pte_same(src_pte_old, entry)) { + put_page(new); + /* dst_entry won't change as in child */ + goto again; + } + hugetlb_install_page(vma, dst_pte, addr, new); + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + continue; + } + if (cow) { /* * No need to notify as we are downgrading page @@ -3817,12 +3877,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, */ huge_ptep_set_wrprotect(src, addr, src_pte); } - entry = huge_ptep_get(src_pte); - ptepage = pte_page(entry); - get_page(ptepage); + page_dup_rmap(ptepage, true); set_huge_pte_at(dst, addr, dst_pte, entry); - hugetlb_count_add(pages_per_huge_page(h), dst); + hugetlb_count_add(npages, dst); } spin_unlock(src_ptl); spin_unlock(dst_ptl);