From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20052C77B7C for ; Wed, 31 May 2023 07:34:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A021B280002; Wed, 31 May 2023 03:34:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B273280001; Wed, 31 May 2023 03:34:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 87A4D280002; Wed, 31 May 2023 03:34:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 783C9280001 for ; Wed, 31 May 2023 03:34:24 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 453E916025D for ; Wed, 31 May 2023 07:34:24 +0000 (UTC) X-FDA: 80849737248.05.5C0E1A4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 0985014000F for ; Wed, 31 May 2023 07:34:21 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TLxBBEJU; spf=pass (imf23.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685518462; h=from:from:sender: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:dkim-signature; bh=ZMG4vUmpGMZQcynLYMZszKgD6M1TLYmEBHSlh9g4e6w=; b=a+FyHLtRbvI9pXiTa6OT4i1CJeEr/nckvPtyq2yIo6FJRfD6SwvocR6dQIOZ8CTUm/1+/3 p3CTt833650q0mEa+66Zb7tzJ4yS+g+ASUkmjqpXKCJo1FgaZq0Cvu0KOF8qlfjGNzqG/Z I7zLvUUvbDk3TqODXu+TIcQNTV4babY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685518462; a=rsa-sha256; cv=none; b=jR56Xm39qvfD2qdSAHyBUwK9CdU2VLSOEuboruOrE4Pld2zu8hLZRAYOjIjeQi0l81Gfea dZYBa+9TA2HCW2X8szi/CQrqW9ZnBrIjQ3kiPPlsuxYE38XgUlGUQhmH/wulPqIDS31wpg 6omrkkHK37yM70eoiZuJZV3gfHPIQoI= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TLxBBEJU; spf=pass (imf23.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685518461; 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=ZMG4vUmpGMZQcynLYMZszKgD6M1TLYmEBHSlh9g4e6w=; b=TLxBBEJUfs7mI3wVCY/yNHUQ+F9/a+hAEsWUC6ReKxc5oJn/fZIPmcN8z+9jz7B3RHi8VG 4njanAZDVOQYgjwevwOsgObw/s9ynpyiIdbB4f0Lhkv01eYv0CcEhfbT0nh+b64D6xg+cX XiHaCXu2nMszQ9MUDuR9W3CyDs8KAyI= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-593-ue9zEx6qMvSK_VrUA6eexQ-1; Wed, 31 May 2023 03:34:20 -0400 X-MC-Unique: ue9zEx6qMvSK_VrUA6eexQ-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3f5df65f9f4so21429535e9.2 for ; Wed, 31 May 2023 00:34:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685518459; x=1688110459; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZMG4vUmpGMZQcynLYMZszKgD6M1TLYmEBHSlh9g4e6w=; b=ETQjAzSnA6Hhu7sgzkri8+HGSTSgamSuqyUsaCXoqhVYCzy6yQ6pBYXHXtIvOnqpx+ /5ZoMmvEIeESZ0gsxlVCSiEUEj797ledideXx320taP4nCelvmejWC+p7VlqNYy3KCo8 nJsj/qG3Vx+wQTfAFcJH98//7ZjaSvI3btyNR5Tuws8RaSrb6NQoq12DH44m8Zs4Oey7 r6yWQXlCESNWHRqpz79Mxu009u/zWHhvCNEWnjYdhmaqUGij89Tg4Ru5BybKImF0abUJ D8my6OSCfeYVLFVdFPJJ5MZ3B8dnRksb2jW3cAusCp+rQQsvKlssS0VExZxeKW000eN5 Glgw== X-Gm-Message-State: AC+VfDxgZr6L8mhxctkyQ9P5GNtKoFqmleH8eu38/DHilSm/zzaY9mlR S3Hh3HUgIFrAW3KY+NlarixneB6/si/D79h06kgAIUwVtIOgixdinKpca2nGG4pORwKX3AqAnG/ kYvHeeGxp7f4= X-Received: by 2002:a5d:644f:0:b0:306:4239:4cd with SMTP id d15-20020a5d644f000000b00306423904cdmr3377838wrw.31.1685518458876; Wed, 31 May 2023 00:34:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6eJ595pUOfxsxQ4hon0QFaAasTBo5N69tj71Iql1/xOKcxhfwzMWOl33hDEBmacxD6kBOTIA== X-Received: by 2002:a5d:644f:0:b0:306:4239:4cd with SMTP id d15-20020a5d644f000000b00306423904cdmr3377817wrw.31.1685518458465; Wed, 31 May 2023 00:34:18 -0700 (PDT) Received: from ?IPV6:2003:cb:c749:cb00:fc9f:d303:d4cc:9f26? (p200300cbc749cb00fc9fd303d4cc9f26.dip0.t-ipconnect.de. [2003:cb:c749:cb00:fc9f:d303:d4cc:9f26]) by smtp.gmail.com with ESMTPSA id l18-20020a5d5272000000b002fb60c7995esm5713370wrc.8.2023.05.31.00.34.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 31 May 2023 00:34:18 -0700 (PDT) Message-ID: Date: Wed, 31 May 2023 09:34:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 To: David Howells , Christoph Hellwig , Lorenzo Stoakes Cc: Jens Axboe , Al Viro , Matthew Wilcox , Jan Kara , Jeff Layton , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , Christian Brauner , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton References: <20230526214142.958751-1-dhowells@redhat.com> <20230526214142.958751-2-dhowells@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v4 1/3] mm: Don't pin ZERO_PAGE in pin_user_pages() In-Reply-To: <20230526214142.958751-2-dhowells@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Stat-Signature: 9b1qqdhs68kbxfd4bfjyxepokbq3qtjn X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 0985014000F X-Rspam-User: X-HE-Tag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Iaq1Xc2dZ2D1xsLyCAuLEAIHy2R7zr1ceWZSkkhpuJdfUcwesqArY7G0dKeNkKUU4EbRdDPM9+UdJcC6Bl0t5DIm0psd5XsDibfxwe7VQC6bS3eluE2ODd3jH7iiiFtwkglK+I3F9ON8qVj0hIK4gclTd8nv0yjWnRd3cpOZVOeAuo+rXTZX+k/IhGihLPyIWxgjhqjhbNjzkm3kHVKYA2qq0oBW0ObjTH0kaGcvHw94Y8qTHQ9xuSPNn7RRTbfwCYz65OZNTvi/APjIcitxMlmMoyxA1JPY0C5gzC8OF/rXLmRJIqKN2FYbuPnGqWDKIt2aOdHRVncKFynQ== 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: On 26.05.23 23:41, David Howells wrote: > Make pin_user_pages*() leave a ZERO_PAGE unpinned if it extracts a pointer > to it from the page tables and make unpin_user_page*() correspondingly > ignore a ZERO_PAGE when unpinning. We don't want to risk overrunning a > zero page's refcount as we're only allowed ~2 million pins on it - > something that userspace can conceivably trigger. 2 millions pins (FOLL_PIN, which increments the refcount by 1024) or 2 million references ? > > Add a pair of functions to test whether a page or a folio is a ZERO_PAGE. > [...] > diff --git a/mm/gup.c b/mm/gup.c > index bbe416236593..ad28261dcafd 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -51,7 +51,8 @@ static inline void sanity_check_pinned_pages(struct page **pages, > struct page *page = *pages; > struct folio *folio = page_folio(page); > > - if (!folio_test_anon(folio)) > + if (is_zero_page(page) || > + !folio_test_anon(folio)) Nit: Fits into a single line (without harming readability IMHO). > continue; > if (!folio_test_large(folio) || folio_test_hugetlb(folio)) > VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page), page); > @@ -131,6 +132,13 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) > else if (flags & FOLL_PIN) { > struct folio *folio; > > + /* > + * Don't take a pin on the zero page - it's not going anywhere > + * and it is used in a *lot* of places. > + */ > + if (is_zero_page(page)) > + return page_folio(page); > + > /* > * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a > * right zone, so fail and let the caller fall back to the slow > @@ -180,6 +188,8 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) > static void gup_put_folio(struct folio *folio, int refs, unsigned int flags) > { > if (flags & FOLL_PIN) { > + if (is_zero_folio(folio)) > + return; > node_stat_mod_folio(folio, NR_FOLL_PIN_RELEASED, refs); > if (folio_test_large(folio)) > atomic_sub(refs, &folio->_pincount); > @@ -224,6 +234,13 @@ int __must_check try_grab_page(struct page *page, unsigned int flags) > if (flags & FOLL_GET) > folio_ref_inc(folio); > else if (flags & FOLL_PIN) { > + /* > + * Don't take a pin on the zero page - it's not going anywhere > + * and it is used in a *lot* of places. > + */ > + if (is_zero_page(page)) > + return 0; > + > /* > * Similar to try_grab_folio(): be sure to *also* > * increment the normal page refcount field at least once, > @@ -3079,6 +3096,9 @@ EXPORT_SYMBOL_GPL(get_user_pages_fast); > * > * FOLL_PIN means that the pages must be released via unpin_user_page(). Please > * see Documentation/core-api/pin_user_pages.rst for further details. > + * > + * Note that if a zero_page is amongst the returned pages, it will not have > + * pins in it and unpin_user_page() will not remove pins from it. > */ "it will not have pins in it" sounds fairly weird to a non-native speaker. "Note that the refcount of any zero_pages returned among the pinned pages will not be incremented, and unpin_user_page() will similarly not decrement it." Acked-by: David Hildenbrand -- Thanks, David / dhildenb