From patchwork Wed Nov 2 19:12:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13028906 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 70853C4332F for ; Wed, 2 Nov 2022 19:12:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EB598E0001; Wed, 2 Nov 2022 15:12:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0998080007; Wed, 2 Nov 2022 15:12:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E55E68E0005; Wed, 2 Nov 2022 15:12:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D74EC8E0001 for ; Wed, 2 Nov 2022 15:12:29 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A9836160605 for ; Wed, 2 Nov 2022 19:12:29 +0000 (UTC) X-FDA: 80089448418.27.08139B0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 506021A0004 for ; Wed, 2 Nov 2022 19:12:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667416348; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BQ7PDKfmtoPRA+bF4Km9GFm2Sx9d/OhosQnplgpAu2o=; b=QaCHCyeBh9g0jUJvbFnoEr/rXlb3vUr5Yz9Vnedmo5TUMNAYExWSkixeq3HXlOuHPsKtEu ZM15c0xtPFIr6Ve5CfX3D5aLv2Ap0QihJ3wPKkM9fNqNb2/4ifA+S4CKnaTw9S+f0jdn6I SZKQ/efmNVCYIJyHMCpjGUq3aweOsBU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-324-w_zaXqGEP3CwXYU0lh2CSw-1; Wed, 02 Nov 2022 15:12:23 -0400 X-MC-Unique: w_zaXqGEP3CwXYU0lh2CSw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BF8E72A5954A; Wed, 2 Nov 2022 19:12:22 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.192.243]) by smtp.corp.redhat.com (Postfix) with ESMTP id 904AF49BB60; Wed, 2 Nov 2022 19:12:19 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, David Hildenbrand , Linus Torvalds , Andrew Morton , Mel Gorman , Dave Chinner , Nadav Amit , Peter Xu , Andrea Arcangeli , Hugh Dickins , Vlastimil Babka , Michael Ellerman , Nicholas Piggin , Mike Rapoport , Anshuman Khandual Subject: [PATCH v1 2/6] mm/mprotect: minor can_change_pte_writable() cleanups Date: Wed, 2 Nov 2022 20:12:05 +0100 Message-Id: <20221102191209.289237-3-david@redhat.com> In-Reply-To: <20221102191209.289237-1-david@redhat.com> References: <20221102191209.289237-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667416349; a=rsa-sha256; cv=none; b=XqmO0KWsDf/Yv1nHuHTRvrYQlmN7K76aFGXIvX8iNE68EVRoPtg+FxYVuRfCBRmhCZ2+bl r7+Inl9U2VF7U2U/WFXHoI2JKTLV+5GQo3/0pxCMLHwBrixIJuzRhocOO0KFL1TxIO7tLm TWfCdyjzjPbqDY0iTEN2YySfgKSGjUo= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QaCHCyeB; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667416349; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BQ7PDKfmtoPRA+bF4Km9GFm2Sx9d/OhosQnplgpAu2o=; b=HNldTJSut4T8cM3/cB7Rptj3FVx6nDksIugfR8QkWWBhbsHuxx6xabgihflsF6Lwky2vWQ FXVpHtihGUGnGItAYmHButpw+G0dRGXlvrIAEHrc45PzUvFw8/lgi6YP+QMQ/5u02miwrR ZmN/he04f6u/M+w1xtv/MDiuDiwoVds= X-Stat-Signature: fe68ebwwmww8kf6sj34twoe6c14medyk X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 506021A0004 X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QaCHCyeB; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com X-HE-Tag: 1667416349-846972 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 want to replicate this code for handling PMDs soon. (1) No need to crash the kernel, warning and rejecting is good enough. As this will no longer get optimized out, drop the pte_write() check: no harm would be done. (2) Add a comment why PROT_NONE mapped pages are excluded. (3) Add a comment regarding MAP_SHARED handling and why we rely on the dirty bit in the PTE. Signed-off-by: David Hildenbrand --- mm/mprotect.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 86a28c0e190f..72aabffb7871 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -44,8 +44,10 @@ static inline bool can_change_pte_writable(struct vm_area_struct *vma, { struct page *page; - VM_BUG_ON(!(vma->vm_flags & VM_WRITE) || pte_write(pte)); + if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE))) + return false; + /* Don't touch entries that are not even readable. */ if (pte_protnone(pte)) return false; @@ -59,15 +61,22 @@ static inline bool can_change_pte_writable(struct vm_area_struct *vma, if (!(vma->vm_flags & VM_SHARED)) { /* - * We can only special-case on exclusive anonymous pages, - * because we know that our write-fault handler similarly would - * map them writable without any additional checks while holding - * the PT lock. + * Writable MAP_PRIVATE mapping: We can only special-case on + * exclusive anonymous pages, because we know that our + * write-fault handler similarly would map them writable without + * any additional checks while holding the PT lock. */ page = vm_normal_page(vma, addr, pte); return page && PageAnon(page) && PageAnonExclusive(page); } + /* + * Writable MAP_SHARED mapping: "clean" might indicate that the FS still + * needs a real write-fault for writenotify + * (see vma_wants_writenotify()). If "dirty", the assumption is that the + * FS was already notified and we can simply mark the PTE writable + * just like the write-fault handler would do. + */ return pte_dirty(pte); }