From patchwork Mon Jul 18 12:02:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Amit X-Patchwork-Id: 12921683 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 1170FC433EF for ; Mon, 18 Jul 2022 19:37:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 980856B0075; Mon, 18 Jul 2022 15:37:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 909216B0078; Mon, 18 Jul 2022 15:37:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7804F8E0001; Mon, 18 Jul 2022 15:37:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6719B6B0075 for ; Mon, 18 Jul 2022 15:37:11 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3099020C44 for ; Mon, 18 Jul 2022 19:37:11 +0000 (UTC) X-FDA: 79701229062.02.4AA2D9E Received: from relay3.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by imf09.hostedemail.com (Postfix) with ESMTP id C825B140004 for ; Mon, 18 Jul 2022 19:37:10 +0000 (UTC) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7A5E734D9B for ; Mon, 18 Jul 2022 19:37:10 +0000 (UTC) X-FDA: 79701229020.25.ACDC87B Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf03.hostedemail.com (Postfix) with ESMTP id EBF0920051 for ; Mon, 18 Jul 2022 19:37:09 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id p8so191830plq.13 for ; Mon, 18 Jul 2022 12:37:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZFfNYrVG625GOSA+id1AogN2HiBF2G/dud76HkUGe+4=; b=DuOhKFmRzGfZbq1nE2NE6Kc5a1/zJpTjh2RvqjQrCWls/7Qhm59F3Q5qnZAqPiZzON HTPLLExPZor3+WUD0GCeFJpaZ9HYJzT2HyRhOUdXKPSxPoB6NcCCp6BaC0PUnMozc1Yt 7yR/B8zLzmfdp4wBCIwoubwwq+IB152Z6aXNEBRtKOMqAeRb0Yg89EaxtZKRLj96Zhk2 zoX+JoTOX8z/qePogfLcdbultYnUpTJzA0NIOrLiUF3n+b/B8dbqeFNxtjocuKqlEg9I gVwTOuaGbUDx39ekLwLxOUStg09K/QpEk4bRVNpBu1NY3eLXRtkHF8N04DDoJYpcUW4c qdtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZFfNYrVG625GOSA+id1AogN2HiBF2G/dud76HkUGe+4=; b=BFOQgqURyXt7NH63vtV6Chm2k0I5LsNjpaoY3MgZyQ8XGAive2xtwvFdN5s77lUxYm eX3Qiv6CI8iMIesK2o85AAjlZTyEemkelBEMwpzXuKTA74RcRZGbbNf2gYGRVzhTprea fFBcszt3fzz1NFjlFDjtFahC1Qmy2Q8sY5N+nWWFoD69xsmllOtcQGtPNFnvyfKWr90/ ux4wR/ZjQG2bEkuPRoDs/a39ooDfAfVFriIZeWNEtgDk/i4+0NPKdzutFDD/YBUKJ19+ /d26QirToQy1jSZKxC4wcDSyeZHiFIQ/Yjieut2/NBzaO664MzvcErAm8fifGqOhHAEN zQpQ== X-Gm-Message-State: AJIora/eArfS9GaltOQKgsBSXHdKGoD5KGpeZr1MF8efZtZJYIXVfvES 1Pm3Tibhuhs5nuA92OOSk73j9h/zgRq9Og== X-Google-Smtp-Source: AGRyM1vEHCEomLrXWGc5eN5sitXXmIDRxEROcYfVCHE3CQXw0PbDpM+yh+ClwMd2ENoAaXz/XYUQHw== X-Received: by 2002:a17:90a:9f8d:b0:1f0:253e:3ecf with SMTP id o13-20020a17090a9f8d00b001f0253e3ecfmr41557002pjp.33.1658173028683; Mon, 18 Jul 2022 12:37:08 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id q6-20020a170902a3c600b0016bc4a6ce28sm9907887plb.98.2022.07.18.12.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 12:37:08 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Mike Rapoport , Axel Rasmussen , Nadav Amit , Andrea Arcangeli , Andrew Cooper , Andy Lutomirski , Dave Hansen , David Hildenbrand , Peter Xu , Peter Zijlstra , Thomas Gleixner , Will Deacon , Yu Zhao , Nick Piggin Subject: [RFC PATCH 04/14] mm/mprotect: preserve write with MM_CP_TRY_CHANGE_WRITABLE Date: Mon, 18 Jul 2022 05:02:02 -0700 Message-Id: <20220718120212.3180-5-namit@vmware.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220718120212.3180-1-namit@vmware.com> References: <20220718120212.3180-1-namit@vmware.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658173030; 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=ZFfNYrVG625GOSA+id1AogN2HiBF2G/dud76HkUGe+4=; b=R7MJfMAovCUv4S/toifyPKIPEXU6ET7ft+a6ck04eSGQDVg7zvCg3wSTCFVOxm7KnEoTuw UAy8yVdl2+B5LTjbWUIPB7OX6pjOvdulp5NkbjQzmpk1oM54RNPQUdEFbZM7v12KFlhFmM UvTXqrXRIf4rFjHwUe4wKOlgBVo7x2A= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=DuOhKFmR; spf=none (imf09.hostedemail.com: domain of MAILER-DAEMON@hostedemail.com has no SPF policy when checking 216.40.44.11) smtp.mailfrom=MAILER-DAEMON@hostedemail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658173030; a=rsa-sha256; cv=none; b=beFqH6zN2xQT/Y1eiwDBvwAlAVAgV+bWzJNGJ3NzrVGZyXDjw/6QiDaFWaEYwVtcMTu2aN aUl0gzTCU/5bGUw8PK99YJFSTLHvvEay1jAeXvEB2QXS7ICcs1ChD6P8XsuCp2AOFZ/KYc IxuoumwzGl4N/Lh2Hu4a0fBYrvrogr0= X-HE-Tag-Orig: 1658173029-138361 Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=DuOhKFmR; spf=none (imf09.hostedemail.com: domain of MAILER-DAEMON@hostedemail.com has no SPF policy when checking 216.40.44.11) smtp.mailfrom=MAILER-DAEMON@hostedemail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: C825B140004 X-Stat-Signature: y9txmwpa73d7kpk8nzjo4n8kzwtkrfpy X-Rspam-User: X-HE-Tag: 1658173030-704531 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: From: Nadav Amit When MM_CP_TRY_CHANGE_WRITABLE is used, change_pte_range() tries to set PTEs as writable. Yet, writable PTEs might still become read-only, due to various limitations of the logic that determines whether a PTE can become writable (see can_change_pte_writable()). Anyhow, it is much easier to keep the writable bit set when MM_CP_TRY_CHANGE_WRITABLE is used than to first clear it and then figure out whether it can be set again. Preserve the write-bit when MM_CP_TRY_CHANGE_WRITABLE is used, similarly to the way it is done with NUMA. Cc: Andrea Arcangeli Cc: Andrew Cooper Cc: Andrew Morton Cc: Andy Lutomirski Cc: Dave Hansen Cc: David Hildenbrand Cc: Peter Xu Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Will Deacon Cc: Yu Zhao Cc: Nick Piggin Signed-off-by: Nadav Amit --- mm/mprotect.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index da5b9bf8204f..92bfb17dcb8a 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -84,6 +84,7 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, bool uffd_wp = cp_flags & MM_CP_UFFD_WP; bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; bool will_need = cp_flags & MM_CP_WILL_NEED; + bool try_change_writable = cp_flags & MM_CP_TRY_CHANGE_WRITABLE; tlb_change_page_size(tlb, PAGE_SIZE); @@ -114,7 +115,8 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, oldpte = *pte; if (pte_present(oldpte)) { pte_t ptent; - bool preserve_write = prot_numa && pte_write(oldpte); + bool preserve_write = (prot_numa || try_change_writable) && + pte_write(oldpte); /* * Avoid trapping faults against the zero or KSM @@ -190,8 +192,7 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, * example, if a PTE is already dirty and no other * COW or special handling is required. */ - if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && - !pte_write(ptent) && + if (try_change_writable && !pte_write(ptent) && can_change_pte_writable(vma, addr, ptent)) { ptent = pte_mkwrite(ptent); if (will_need)