From patchwork Tue Apr 11 14:25:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13207672 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 07B62C77B73 for ; Tue, 11 Apr 2023 14:25:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C9BB8280001; Tue, 11 Apr 2023 10:25:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE05128000D; Tue, 11 Apr 2023 10:25:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E43328000B; Tue, 11 Apr 2023 10:25:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 7B84928000B for ; Tue, 11 Apr 2023 10:25:29 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 16539C0DD7 for ; Tue, 11 Apr 2023 14:25:29 +0000 (UTC) X-FDA: 80669333178.20.CFC5136 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 2FB39A0003 for ; Tue, 11 Apr 2023 14:25:25 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="c8u7i5D/"; spf=pass (imf25.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=1681223126; 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=2z0ZWpIzSXqyEF2KVa3LzGMQGesZiayZZfA3zEW/CRA=; b=NSF3EGAz+gSOHf7CsMV9OV2hO9NWNT81LuMDGjL+GrWMXT6JqiaBu3YCY+/zlPMTwFbS83 0cquO/kTRU+jsWObBSC/3TyqyF9H+eUx5So+0IjrhkD9qmS6oGC1pVJR2rAKotsMJGtkrQ JRNZd/8B7SfQ1qcyKvstyipMRIZz6nc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="c8u7i5D/"; spf=pass (imf25.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681223126; a=rsa-sha256; cv=none; b=3ohUeRofuRQLxjoVZjPJHUzX6daE//+K30gPblqEZ0TocSFsYeUHcR3zF03i+1IHEYjCul C/4LW3UEBEEmBHGdyEMpdSIx2vTnTsgc7tCBC02SvC9sqbV32FVtZRZIKtIhhUxXBf5eX3 yXVtV0ICPz52dxbZdaI7gBK7tncKJh0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681223125; 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=2z0ZWpIzSXqyEF2KVa3LzGMQGesZiayZZfA3zEW/CRA=; b=c8u7i5D/yvp/QzA0u0Doz8GeKSjf/9kebw8PmGtWqsc9AazyxW6+/VX5lc44RDQxHDkKkU cgZg1zCUA8pouWoTnYOjA0tEu5L677kWx3qe0h9kIgjQ/WpOl80CKHoOQE5wtgEOL+SE7f RCxe+yAn2l/lbZMylYcy2BIYReUOGaE= 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-577-jmHLwFX-O2O_HjNQNHWeyw-1; Tue, 11 Apr 2023 10:25:22 -0400 X-MC-Unique: jmHLwFX-O2O_HjNQNHWeyw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 94B9B3C0F19B; Tue, 11 Apr 2023 14:25:18 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8217D1417202; Tue, 11 Apr 2023 14:25:16 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, sparclinux@vger.kernel.org, David Hildenbrand , Andrew Morton , "David S. Miller" , Peter Xu , Hugh Dickins , Shuah Khan , Sam Ravnborg , Yu Zhao , Anshuman Khandual Subject: [PATCH v1 RESEND 1/6] selftests/mm: reuse read_pmd_pagesize() in COW selftest Date: Tue, 11 Apr 2023 16:25:07 +0200 Message-Id: <20230411142512.438404-2-david@redhat.com> In-Reply-To: <20230411142512.438404-1-david@redhat.com> References: <20230411142512.438404-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Stat-Signature: fu9m6izbaga7oxj5bokmua66su4cid7e X-Rspam-User: X-Rspamd-Queue-Id: 2FB39A0003 X-Rspamd-Server: rspam06 X-HE-Tag: 1681223125-366941 X-HE-Meta: U2FsdGVkX1/Mg7U10VCbJdaHWUga9CsQy1LyYVC5ERuyCdfGhfBnaxBOooQZv5Gs+Wsn69isNpI02XTrGW7bLmJ6AxLy7MFRkLSPM2WW3yXIkYHVH3YYlgvbnCKISPyqNklf/MBCZlbErl4FT968WX8ReAQCZab0MKYfsKxe41CKIqugcHfIh2zZdaqC43d6ZUz2sopi3bdJsN68cy92celPN+nU/qkRFHz9m/JmNE/5LnSEZHAy++TiaxbaP0WTb0nN9UotwAFAx9xhSC9wMPnFnNkK8GNMS23Nkyu3tFMOySMnyJwG9k4ySlESRxXspvLtt+rDJkRU/lN3sL4jcGu17AZosk3nCpZSDsZehZfk/F/Q/EkG5W/sfEqocnRp8+4M3dkCm3MdBVtgTnUUR3YVUl4e+Ohb3pHq+BYKwEgPQMJhe08MJ+YihV9scTBDc/py8dkM9YEQNE4JZICGOoqZFHnEdLWagss8pZEqlPge3EoeDaNZZcTnVH64k/x+dQn6ITCHX7ilHefFGJtSKnHmNiFSw7bjhenybXn2OplUc54C94zndMv+ld69Yqb9KXHre+gVfiB5fmeQmj5scsqv/CUtaHzay7ZV6HdlF3rBiLtyxvOVwikKXm3hzTEfpBxSgHz4NowvP1qSuxU0Irw73Hl+mOXmwqoPUDPKriN1YuXphkO8OdHc9w2Y+CY0f4V78SJmVCq0SQFy/ubVdFRBEcJLKeJuA9055DBZu/uFt6Bxn7liomF6M6oVVAjt6NtTi1+zGPEqpsKhMgL+pNuinVVMAtcjxf3EagQp95k7k7zGWViYBvzirJNWpBZT682lzaxKBWqCCzxzG+nCTTXc0qGf/aV2r/vbvQOnbhGQ/iejfFmHzQIeqQJ4/pwJ0QKsoQ9ahkim/bUVZ/82yU1aiqePgSDmm2jcVkxHfBx1e9JqOPMu961a+bYfAlRx7VBvcALbECyeOkUOHgd 4SueqU7j DFOoWtODynZ6Cz0WBElNVvKM26jr7g3S8OnBoUHColQjf+VV7i+JMTkaUYfk8nBfhQQN905gmoZZNXOSg4xGpDC/w8+umRQP88iZ7OBU14rMnbEhW0NpIOtF8sQcYgfJSbS31eJNJgY9gqJMyQOqdtw1G1wANaf0ipmhTyNDfc4JRYYrljZJszk+0fmp+H/rWYf0jRPirufX9AxdUGg8t1qQM6mgxVMyK01yr51Fpamzc2A0= 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: The COW selftest can deal with THP not being configured. So move error handling of read_pmd_pagesize() into the callers such that we can reuse it in the COW selftest. Signed-off-by: David Hildenbrand --- tools/testing/selftests/mm/cow.c | 33 +++---------------- tools/testing/selftests/mm/khugepaged.c | 4 +++ tools/testing/selftests/mm/soft-dirty.c | 3 ++ .../selftests/mm/split_huge_page_test.c | 4 +++ tools/testing/selftests/mm/vm_util.c | 4 +-- 5 files changed, 17 insertions(+), 31 deletions(-) diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c index 0eb2e8180aa5..dc9d6fe86028 100644 --- a/tools/testing/selftests/mm/cow.c +++ b/tools/testing/selftests/mm/cow.c @@ -45,34 +45,6 @@ static size_t hugetlbsizes[10]; static int gup_fd; static bool has_huge_zeropage; -static void detect_thpsize(void) -{ - int fd = open("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", - O_RDONLY); - size_t size = 0; - char buf[15]; - int ret; - - if (fd < 0) - return; - - ret = pread(fd, buf, sizeof(buf), 0); - if (ret > 0 && ret < sizeof(buf)) { - buf[ret] = 0; - - size = strtoul(buf, NULL, 10); - if (size < pagesize) - size = 0; - if (size > 0) { - thpsize = size; - ksft_print_msg("[INFO] detected THP size: %zu KiB\n", - thpsize / 1024); - } - } - - close(fd); -} - static void detect_huge_zeropage(void) { int fd = open("/sys/kernel/mm/transparent_hugepage/use_zero_page", @@ -1741,7 +1713,10 @@ int main(int argc, char **argv) int err; pagesize = getpagesize(); - detect_thpsize(); + thpsize = read_pmd_pagesize(); + if (thpsize) + ksft_print_msg("[INFO] detected THP size: %zu KiB\n", + thpsize / 1024); detect_hugetlbsizes(); detect_huge_zeropage(); diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c index 64126c8cd561..97adc0f34f9c 100644 --- a/tools/testing/selftests/mm/khugepaged.c +++ b/tools/testing/selftests/mm/khugepaged.c @@ -1476,6 +1476,10 @@ int main(int argc, const char **argv) page_size = getpagesize(); hpage_pmd_size = read_pmd_pagesize(); + if (!hpage_pmd_size) { + printf("Reading PMD pagesize failed"); + exit(EXIT_FAILURE); + } hpage_pmd_nr = hpage_pmd_size / page_size; default_settings.khugepaged.max_ptes_none = hpage_pmd_nr - 1; diff --git a/tools/testing/selftests/mm/soft-dirty.c b/tools/testing/selftests/mm/soft-dirty.c index 21d8830c5f24..cc5f144430d4 100644 --- a/tools/testing/selftests/mm/soft-dirty.c +++ b/tools/testing/selftests/mm/soft-dirty.c @@ -80,6 +80,9 @@ static void test_hugepage(int pagemap_fd, int pagesize) int i, ret; size_t hpage_len = read_pmd_pagesize(); + if (!hpage_len) + ksft_exit_fail_msg("Reading PMD pagesize failed"); + map = memalign(hpage_len, hpage_len); if (!map) ksft_exit_fail_msg("memalign failed\n"); diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c index 76e1c36dd9e5..1dc5804b8b2b 100644 --- a/tools/testing/selftests/mm/split_huge_page_test.c +++ b/tools/testing/selftests/mm/split_huge_page_test.c @@ -300,6 +300,10 @@ int main(int argc, char **argv) pagesize = getpagesize(); pageshift = ffs(pagesize) - 1; pmd_pagesize = read_pmd_pagesize(); + if (!pmd_pagesize) { + printf("Reading PMD pagesize failed\n"); + exit(EXIT_FAILURE); + } split_pmd_thp(); split_pte_mapped_thp(); diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 40e795624ff3..8dc74dd022c2 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -84,12 +84,12 @@ uint64_t read_pmd_pagesize(void) fd = open(PMD_SIZE_FILE_PATH, O_RDONLY); if (fd == -1) - ksft_exit_fail_msg("Open hpage_pmd_size failed\n"); + return 0; num_read = read(fd, buf, 19); if (num_read < 1) { close(fd); - ksft_exit_fail_msg("Read hpage_pmd_size failed\n"); + return 0; } buf[num_read] = '\0'; close(fd); From patchwork Tue Apr 11 14:25:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13207673 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 7AFE5C77B70 for ; Tue, 11 Apr 2023 14:25:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1835828000D; Tue, 11 Apr 2023 10:25:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E61628000B; Tue, 11 Apr 2023 10:25:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D910E28000D; Tue, 11 Apr 2023 10:25:30 -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 BBF6828000B for ; Tue, 11 Apr 2023 10:25:30 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8B5B91A0D34 for ; Tue, 11 Apr 2023 14:25:30 +0000 (UTC) X-FDA: 80669333220.02.85808F4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id C7C514001F for ; Tue, 11 Apr 2023 14:25:28 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ilyLH2DO; spf=pass (imf11.hostedemail.com: domain of david@redhat.com designates 170.10.133.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=1681223128; 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=kiblhRIXN5QVGSvHoajKRlNDk3I/DcpYGGNog0nXOOg=; b=7oStF75/q4TU1CPODrV8j1EubnvAQ7O1f7270ygdOjoHKdf1Es1mb+4RLwFRTwtSznSq0f qCcd8LRO9jBLNABqktuE/W84sQL7XuTnHjqZweg2atmq/uLx274BWyf2JSLfEFGFdvrGu2 4HeqUr4zId07VjQvhswFgrMbgtIsaz0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ilyLH2DO; spf=pass (imf11.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681223128; a=rsa-sha256; cv=none; b=ccghPIJxvwtzCljFY7H6W6wQYPEsVCd2IYOAGIAnSomuCyBggTIY9ljWKz8UkcBIKQySXr z1gr5yCijhFyDgXo5/n+jBOye9+kf3xMymsIOquFIOe17I6yS1Rpv9cwD5ckVJx+ARSwno miN1R0ehomA2t4O9Hvx3ubMlb33/OJY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681223128; 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=kiblhRIXN5QVGSvHoajKRlNDk3I/DcpYGGNog0nXOOg=; b=ilyLH2DOPEyhKJ7/JRU83kFCjBAnos3ynRJ9FrlNz1HCMlvqQAhuZwtjIA5lEs4Gc6xACx 3gBjU/qeK7adVfloQcu6Nffgz6oC+HWsfqhF457v0MSuyzyftnBK91xrxmbVssEESPiSja zKN1WEnHOQi9UIbbBUtUbfxtWr35N+g= 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-638-WwIwvrdTOo2r1pNFp4lo0Q-1; Tue, 11 Apr 2023 10:25:22 -0400 X-MC-Unique: WwIwvrdTOo2r1pNFp4lo0Q-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 57E2328237CB; Tue, 11 Apr 2023 14:25:21 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id F133214171D9; Tue, 11 Apr 2023 14:25:18 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, sparclinux@vger.kernel.org, David Hildenbrand , Andrew Morton , "David S. Miller" , Peter Xu , Hugh Dickins , Shuah Khan , Sam Ravnborg , Yu Zhao , Anshuman Khandual Subject: [PATCH v1 RESEND 2/6] selftests/mm: mkdirty: test behavior of (pte|pmd)_mkdirty on VMAs without write permissions Date: Tue, 11 Apr 2023 16:25:08 +0200 Message-Id: <20230411142512.438404-3-david@redhat.com> In-Reply-To: <20230411142512.438404-1-david@redhat.com> References: <20230411142512.438404-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: bgx5gj6r79qjn73arhft4nryrkic1zxr X-Rspamd-Queue-Id: C7C514001F X-HE-Tag: 1681223128-895098 X-HE-Meta: U2FsdGVkX192YMPKKU1MZJJQgex88yNB9rNwVwGVxgYPTJCFXuoZuOampWwT/RCslUfMEiQaCKYoxPUD8HDwLOxPEterGg3JwkAQhmCfLYph/3cSKpTpnDCUg3tyIH0eST8a19MbGzUNocvDtIQ1J3UItHIihoq3YioIa2+aOHTQEsfoFQbmCPqIhu+rKXMDCBqmV/bEMyQVWUc0SibZ/HIjv2UhjJZDW4HWstA8/SrVWPhv1zyWjzDVCfYpundIhPwXC4gDAIC0500UvLGcyQqxX75vkCCZEZHjtOfrBBfgQIu8SPqI/5ZRl8/by3lRTsXZKEvNSGCLzecHINxZg1W37IacL0y3x8MKl6G/DuPt1mqQMbIlb2Yj3JoQQcZAa4x0URKtZS9oH4I7uzOmbyu81YfIR/FhmUPv8Rp0QRqKHIRsqdsIQBstUAtuJLzwafV7w1Qj+MIKDFNkM3jWCF2aVT4NMc9VZFMJ+L61yFUF62J9Gzugg2QwTeyRmYblDMxoLTiCggS13IBmY/4IqDXes36iCHF8qYdZfl3yOUJ5WhFYSB99DaBnISeNjnyQwD5YYEgVIIikGxuTdofSlcF0dzxElfJoHnu9K8QnLdmxLAwB037hqQ5DXp5mOjNn5QzRzKJxGggObiQzmZKWxnGM+R8phf48bkj/30z3r9X1SSrKoVUPU4dGCK//ZpOLjIA1+caFBovZMHCSke56d8rtqPU73ij4r554ArTps0C/S3oFcMfgScIQKnf56OiGHNvHHQT3WJqkPYix/s7qBBdUeoEyMTYvDthKbfBJtt+wr9DVHOFFw73QH8rBP8bjCaGx4hroPK40xNWWeemVfFCeDO/P+3KvRo3C9QuJAPN5zY7bCzAC6B1G7CWJ1DjGYfQm7ffCouumwAyzM84WHptInaxsnK50NZ2u2gWwBTIByxSwJiAbnzuEpzx4gdgpeeyDd9cPyvjs1LJgIzh eLUlJA8F 7HYQgdxXpjAzDDgF/kiIp5Jxex8h5kNzhS0K6EyvsC3Kq0crSVfJUnJ57lyGuc3v6M9VCXaz9QnHemoaD1R99bJ4kaPNjQuwLkiyB/hCcn3I0bYNGMJ+efTZOvHsIIIJGu2UYEtis9w0TJWHTTwY7qTt6d91pgqx6yB1FdcrIGTF0+1y7DQRrQnXCZMsV8mtrgFnycLskOJsNpu8HnFYEE1WjbtSyuupnGmXJOgBzoenqhIc= 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: Let's add some tests that trigger (pte|pmd)_mkdirty on VMAs without write permissions. If an architecture implementation is wrong, we might accidentally set the PTE/PMD writable and allow for write access in a VMA without write permissions. The tests include reproducers for the two issues recently discovered and worked-around in core-MM for now: (1) commit 624a2c94f5b7 ("Partly revert "mm/thp: carry over dirty bit when thp splits on pmd"") (2) commit 96a9c287e25d ("mm/migrate: fix wrongly apply write bit after mkdirty on sparc64") In addition, some other tests that reveal further issues. All tests pass under x86_64: ./mkdirty # [INFO] detected THP size: 2048 KiB TAP version 13 1..6 # [INFO] PTRACE write access ok 1 SIGSEGV generated, page not modified # [INFO] PTRACE write access to THP ok 2 SIGSEGV generated, page not modified # [INFO] Page migration ok 3 SIGSEGV generated, page not modified # [INFO] Page migration of THP ok 4 SIGSEGV generated, page not modified # [INFO] PTE-mapping a THP ok 5 SIGSEGV generated, page not modified # [INFO] UFFDIO_COPY ok 6 SIGSEGV generated, page not modified # Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0 But some fail on sparc64: ./mkdirty # [INFO] detected THP size: 8192 KiB TAP version 13 1..6 # [INFO] PTRACE write access not ok 1 SIGSEGV generated, page not modified # [INFO] PTRACE write access to THP not ok 2 SIGSEGV generated, page not modified # [INFO] Page migration ok 3 SIGSEGV generated, page not modified # [INFO] Page migration of THP ok 4 SIGSEGV generated, page not modified # [INFO] PTE-mapping a THP ok 5 SIGSEGV generated, page not modified # [INFO] UFFDIO_COPY not ok 6 SIGSEGV generated, page not modified Bail out! 3 out of 6 tests failed # Totals: pass:3 fail:3 xfail:0 xpass:0 skip:0 error:0 Reverting both above commits makes all tests fail on sparc64: ./mkdirty # [INFO] detected THP size: 8192 KiB TAP version 13 1..6 # [INFO] PTRACE write access not ok 1 SIGSEGV generated, page not modified # [INFO] PTRACE write access to THP not ok 2 SIGSEGV generated, page not modified # [INFO] Page migration not ok 3 SIGSEGV generated, page not modified # [INFO] Page migration of THP not ok 4 SIGSEGV generated, page not modified # [INFO] PTE-mapping a THP not ok 5 SIGSEGV generated, page not modified # [INFO] UFFDIO_COPY not ok 6 SIGSEGV generated, page not modified Bail out! 6 out of 6 tests failed # Totals: pass:0 fail:6 xfail:0 xpass:0 skip:0 error:0 The tests are useful to detect other problematic archs, to verify new arch fixes, and to stop such issues from reappearing in the future. For now, we don't add any hugetlb tests. Signed-off-by: David Hildenbrand --- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/mkdirty.c | 379 +++++++++++++++++++++++++++ 2 files changed, 381 insertions(+) create mode 100644 tools/testing/selftests/mm/mkdirty.c diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index c31d952cff68..8235dddbbbc6 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -47,6 +47,7 @@ TEST_GEN_FILES += map_hugetlb TEST_GEN_FILES += map_populate TEST_GEN_FILES += memfd_secret TEST_GEN_FILES += migration +TEST_GEN_PROGS += mkdirty TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += mlock2-tests TEST_GEN_FILES += mrelease_test @@ -108,6 +109,7 @@ $(OUTPUT)/cow: vm_util.c $(OUTPUT)/khugepaged: vm_util.c $(OUTPUT)/ksm_functional_tests: vm_util.c $(OUTPUT)/madv_populate: vm_util.c +$(OUTPUT)/mkdirty: vm_util.c $(OUTPUT)/soft-dirty: vm_util.c $(OUTPUT)/split_huge_page_test: vm_util.c $(OUTPUT)/userfaultfd: vm_util.c diff --git a/tools/testing/selftests/mm/mkdirty.c b/tools/testing/selftests/mm/mkdirty.c new file mode 100644 index 000000000000..6d71d972997b --- /dev/null +++ b/tools/testing/selftests/mm/mkdirty.c @@ -0,0 +1,379 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Test handling of code that might set PTE/PMD dirty in read-only VMAs. + * Setting a PTE/PMD dirty must not accidentally set the PTE/PMD writable. + * + * Copyright 2023, Red Hat, Inc. + * + * Author(s): David Hildenbrand + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" +#include "vm_util.h" + +static size_t pagesize; +static size_t thpsize; +static int mem_fd; +static int pagemap_fd; +static sigjmp_buf env; + +static void signal_handler(int sig) +{ + if (sig == SIGSEGV) + siglongjmp(env, 1); + siglongjmp(env, 2); +} + +static void do_test_write_sigsegv(char *mem) +{ + char orig = *mem; + int ret; + + if (signal(SIGSEGV, signal_handler) == SIG_ERR) { + ksft_test_result_fail("signal() failed\n"); + return; + } + + ret = sigsetjmp(env, 1); + if (!ret) + *mem = orig + 1; + + if (signal(SIGSEGV, SIG_DFL) == SIG_ERR) + ksft_test_result_fail("signal() failed\n"); + + ksft_test_result(ret == 1 && *mem == orig, + "SIGSEGV generated, page not modified\n"); +} + +static char *mmap_thp_range(int prot, char **_mmap_mem, size_t *_mmap_size) +{ + const size_t mmap_size = 2 * thpsize; + char *mem, *mmap_mem; + + mmap_mem = mmap(NULL, mmap_size, prot, MAP_PRIVATE|MAP_ANON, + -1, 0); + if (mmap_mem == MAP_FAILED) { + ksft_test_result_fail("mmap() failed\n"); + return MAP_FAILED; + } + mem = (char *)(((uintptr_t)mmap_mem + thpsize) & ~(thpsize - 1)); + + if (madvise(mem, thpsize, MADV_HUGEPAGE)) { + ksft_test_result_skip("MADV_HUGEPAGE failed\n"); + munmap(mmap_mem, mmap_size); + return MAP_FAILED; + } + + *_mmap_mem = mmap_mem; + *_mmap_size = mmap_size; + return mem; +} + +static void test_ptrace_write(void) +{ + char data = 1; + char *mem; + int ret; + + ksft_print_msg("[INFO] PTRACE write access\n"); + + mem = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE|MAP_ANON, -1, 0); + if (mem == MAP_FAILED) { + ksft_test_result_fail("mmap() failed\n"); + return; + } + + /* Fault in the shared zeropage. */ + if (*mem != 0) { + ksft_test_result_fail("Memory not zero\n"); + goto munmap; + } + + /* + * Unshare the page (populating a fresh anon page that might be set + * dirty in the PTE) in the read-only VMA using ptrace (FOLL_FORCE). + */ + lseek(mem_fd, (uintptr_t) mem, SEEK_SET); + ret = write(mem_fd, &data, 1); + if (ret != 1 || *mem != data) { + ksft_test_result_fail("write() failed\n"); + goto munmap; + } + + do_test_write_sigsegv(mem); +munmap: + munmap(mem, pagesize); +} + +static void test_ptrace_write_thp(void) +{ + char *mem, *mmap_mem; + size_t mmap_size; + char data = 1; + int ret; + + ksft_print_msg("[INFO] PTRACE write access to THP\n"); + + mem = mmap_thp_range(PROT_READ, &mmap_mem, &mmap_size); + if (mem == MAP_FAILED) + return; + + /* + * Write to the first subpage in the read-only VMA using + * ptrace(FOLL_FORCE), eventually placing a fresh THP that is marked + * dirty in the PMD. + */ + lseek(mem_fd, (uintptr_t) mem, SEEK_SET); + ret = write(mem_fd, &data, 1); + if (ret != 1 || *mem != data) { + ksft_test_result_fail("write() failed\n"); + goto munmap; + } + + /* MM populated a THP if we got the last subpage populated as well. */ + if (!pagemap_is_populated(pagemap_fd, mem + thpsize - pagesize)) { + ksft_test_result_skip("Did not get a THP populated\n"); + goto munmap; + } + + do_test_write_sigsegv(mem); +munmap: + munmap(mmap_mem, mmap_size); +} + +static void test_page_migration(void) +{ + char *mem; + + ksft_print_msg("[INFO] Page migration\n"); + + mem = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, + -1, 0); + if (mem == MAP_FAILED) { + ksft_test_result_fail("mmap() failed\n"); + return; + } + + /* Populate a fresh page and dirty it. */ + memset(mem, 1, pagesize); + if (mprotect(mem, pagesize, PROT_READ)) { + ksft_test_result_fail("mprotect() failed\n"); + goto munmap; + } + + /* Trigger page migration. Might not be available or fail. */ + if (syscall(__NR_mbind, mem, pagesize, MPOL_LOCAL, NULL, 0x7fful, + MPOL_MF_MOVE)) { + ksft_test_result_skip("mbind() failed\n"); + goto munmap; + } + + do_test_write_sigsegv(mem); +munmap: + munmap(mem, pagesize); +} + +static void test_page_migration_thp(void) +{ + char *mem, *mmap_mem; + size_t mmap_size; + + ksft_print_msg("[INFO] Page migration of THP\n"); + + mem = mmap_thp_range(PROT_READ|PROT_WRITE, &mmap_mem, &mmap_size); + if (mem == MAP_FAILED) + return; + + /* + * Write to the first page, which might populate a fresh anon THP + * and dirty it. + */ + memset(mem, 1, pagesize); + if (mprotect(mem, thpsize, PROT_READ)) { + ksft_test_result_fail("mprotect() failed\n"); + goto munmap; + } + + /* MM populated a THP if we got the last subpage populated as well. */ + if (!pagemap_is_populated(pagemap_fd, mem + thpsize - pagesize)) { + ksft_test_result_skip("Did not get a THP populated\n"); + goto munmap; + } + + /* Trigger page migration. Might not be available or fail. */ + if (syscall(__NR_mbind, mem, thpsize, MPOL_LOCAL, NULL, 0x7fful, + MPOL_MF_MOVE)) { + ksft_test_result_skip("mbind() failed\n"); + goto munmap; + } + + do_test_write_sigsegv(mem); +munmap: + munmap(mmap_mem, mmap_size); +} + +static void test_pte_mapped_thp(void) +{ + char *mem, *mmap_mem; + size_t mmap_size; + + ksft_print_msg("[INFO] PTE-mapping a THP\n"); + + mem = mmap_thp_range(PROT_READ|PROT_WRITE, &mmap_mem, &mmap_size); + if (mem == MAP_FAILED) + return; + + /* + * Write to the first page, which might populate a fresh anon THP + * and dirty it. + */ + memset(mem, 1, pagesize); + if (mprotect(mem, thpsize, PROT_READ)) { + ksft_test_result_fail("mprotect() failed\n"); + goto munmap; + } + + /* MM populated a THP if we got the last subpage populated as well. */ + if (!pagemap_is_populated(pagemap_fd, mem + thpsize - pagesize)) { + ksft_test_result_skip("Did not get a THP populated\n"); + goto munmap; + } + + /* Trigger PTE-mapping the THP by mprotect'ing the last subpage. */ + if (mprotect(mem + thpsize - pagesize, pagesize, + PROT_READ|PROT_WRITE)) { + ksft_test_result_fail("mprotect() failed\n"); + goto munmap; + } + + do_test_write_sigsegv(mem); +munmap: + munmap(mmap_mem, mmap_size); +} + +#ifdef __NR_userfaultfd +static void test_uffdio_copy(void) +{ + struct uffdio_register uffdio_register; + struct uffdio_copy uffdio_copy; + struct uffdio_api uffdio_api; + char *dst, *src; + int uffd; + + ksft_print_msg("[INFO] UFFDIO_COPY\n"); + + src = malloc(pagesize); + memset(src, 1, pagesize); + dst = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE|MAP_ANON, -1, 0); + if (dst == MAP_FAILED) { + ksft_test_result_fail("mmap() failed\n"); + return; + } + + uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); + if (uffd < 0) { + ksft_test_result_skip("__NR_userfaultfd failed\n"); + goto munmap; + } + + uffdio_api.api = UFFD_API; + uffdio_api.features = 0; + if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) { + ksft_test_result_fail("UFFDIO_API failed\n"); + goto close_uffd; + } + + uffdio_register.range.start = (unsigned long) dst; + uffdio_register.range.len = pagesize; + uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; + if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) { + ksft_test_result_fail("UFFDIO_REGISTER failed\n"); + goto close_uffd; + } + + /* Place a page in a read-only VMA, which might set the PTE dirty. */ + uffdio_copy.dst = (unsigned long) dst; + uffdio_copy.src = (unsigned long) src; + uffdio_copy.len = pagesize; + uffdio_copy.mode = 0; + if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy)) { + ksft_test_result_fail("UFFDIO_COPY failed\n"); + goto close_uffd; + } + + do_test_write_sigsegv(dst); +close_uffd: + close(uffd); +munmap: + munmap(dst, pagesize); + free(src); +#endif /* __NR_userfaultfd */ +} + +int main(void) +{ + int err, tests = 2; + + pagesize = getpagesize(); + thpsize = read_pmd_pagesize(); + if (thpsize) { + ksft_print_msg("[INFO] detected THP size: %zu KiB\n", + thpsize / 1024); + tests += 3; + } +#ifdef __NR_userfaultfd + tests += 1; +#endif /* __NR_userfaultfd */ + + ksft_print_header(); + ksft_set_plan(tests); + + mem_fd = open("/proc/self/mem", O_RDWR); + if (mem_fd < 0) + ksft_exit_fail_msg("opening /proc/self/mem failed\n"); + pagemap_fd = open("/proc/self/pagemap", O_RDONLY); + if (pagemap_fd < 0) + ksft_exit_fail_msg("opening /proc/self/pagemap failed\n"); + + /* + * On some ptrace(FOLL_FORCE) write access via /proc/self/mem in + * read-only VMAs, the kernel may set the PTE/PMD dirty. + */ + test_ptrace_write(); + if (thpsize) + test_ptrace_write_thp(); + /* + * On page migration, the kernel may set the PTE/PMD dirty when + * remapping the page. + */ + test_page_migration(); + if (thpsize) + test_page_migration_thp(); + /* PTE-mapping a THP might propagate the dirty PMD bit to the PTEs. */ + if (thpsize) + test_pte_mapped_thp(); + /* Placing a fresh page via userfaultfd may set the PTE dirty. */ +#ifdef __NR_userfaultfd + test_uffdio_copy(); +#endif /* __NR_userfaultfd */ + + err = ksft_get_fail_cnt(); + if (err) + ksft_exit_fail_msg("%d out of %d tests failed\n", + err, ksft_test_num()); + return ksft_exit_pass(); +} From patchwork Tue Apr 11 14:25:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13207671 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 178F0C76196 for ; Tue, 11 Apr 2023 14:25:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F44E28000C; Tue, 11 Apr 2023 10:25:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 96F02280001; Tue, 11 Apr 2023 10:25:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 85EC128000C; Tue, 11 Apr 2023 10:25:29 -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 6D3FA280001 for ; Tue, 11 Apr 2023 10:25:29 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F11D0C0DCE for ; Tue, 11 Apr 2023 14:25:28 +0000 (UTC) X-FDA: 80669333136.22.7E9A8B9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 1357540026 for ; Tue, 11 Apr 2023 14:25:26 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cq+SORSc; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.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=1681223127; 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=VwTQ8oLmv6mJPSwow3w38v2InKFos9H0Yp4Ad7AU1D8=; b=cGzP5U7Eq37xnQ4GQ7utP0v++Q13mioANegHUkKQbSzMc0v3q5yBN0pp5btK/psgjFIZ3l Rd7ari4LNEUvKIu3zFvtOT3gcjwNeyL6mtrvZnYRzoMPQjRG/FL7R2oqxpx+hfpIvvqsz9 WPOqQ4eQ17HEFSqFQN5KqJT0awEMyZk= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cq+SORSc; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681223127; a=rsa-sha256; cv=none; b=Z7E79toFRXtONgFv8ND0efyMXh2YnV6D+ys8ndgRn1Onm8Mh2Y0mPU32iIkLCmDn0iweLX G64UF6Uh7jPLlwz/MiBdwhOzQ09Br6CqoSdu98GK4G0h0r+YwC4Md8v4cKwnhdX5fOjAIH 1B2C4TD/UN7uZEkcXRBh9CuMEZPNrC4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681223126; 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=VwTQ8oLmv6mJPSwow3w38v2InKFos9H0Yp4Ad7AU1D8=; b=cq+SORSc5ieYbQz25AEBjFw/hU590HorQePiIXwSz4rf0pfGkI4xy2MLW7TgODVriadiRM 6fN/tGjOFDR0FTo74afyspp1iiDQ+nunHdvFo+zh1onZut8G3HJ17GBIZF7bb+hkh18uHO 4sAq9u85+Un/wBz+HvIKQkjbR+kZOLM= 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-534-G3xKFkO9Mheb-VbM75NmYQ-1; Tue, 11 Apr 2023 10:25:25 -0400 X-MC-Unique: G3xKFkO9Mheb-VbM75NmYQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 069A43C0F423; Tue, 11 Apr 2023 14:25:24 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9221A14171D5; Tue, 11 Apr 2023 14:25:21 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, sparclinux@vger.kernel.org, David Hildenbrand , Andrew Morton , "David S. Miller" , Peter Xu , Hugh Dickins , Shuah Khan , Sam Ravnborg , Yu Zhao , Anshuman Khandual Subject: [PATCH v1 RESEND 3/6] sparc/mm: don't unconditionally set HW writable bit when setting PTE dirty on 64bit Date: Tue, 11 Apr 2023 16:25:09 +0200 Message-Id: <20230411142512.438404-4-david@redhat.com> In-Reply-To: <20230411142512.438404-1-david@redhat.com> References: <20230411142512.438404-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1357540026 X-Stat-Signature: dwha44rtm9zm9crwf7om9maycg44c9pq X-HE-Tag: 1681223126-206541 X-HE-Meta: U2FsdGVkX19w4Nlz0BFvcWLPHqltH7ly1wRf6qo9fPZwA4Uw7U+5xRrADZKC3EG+CA53ykrwS5N9ZRiuVk40kue8rS4Am/qrSnYydJYptM/AJRpNfdWMJMfbh3D3V2TjQzUkL8WUFa7u0Fa0v0Glbd47OzyghIWhmccnAko16b/JaZAWqC4N3DQ08XWi+86GWDMoZ/qMbHHhnKXrssCJfB5o14k5etos/Xez8s66AeJI2uoWEgJw9oUDPR/efmy5HrDRDLz7PGxmmxQ0cYPeEPwqXJdxxOKtezLM2IT3/S0cTpT9w/Sjk8xOMb9gzMTIHGgHmxiLgvAN7mHKNRo6Z8IijuqAjmzYEHqFHk1zGmXztVXJ/Uisrv1dfX6sBP1mywVcqpFFc8HnqUoHBOY1KWHHyvC7r9XS/f35FN8n8PbPCL/i23Ir2fKk2cQWkOfCqUdVgAhkOePOMOBNbI7oMRtzmgzDdwVLxFMVKqbQCBXj+kBuk6u2RxBor1UC6yeKPyg0q9+qUrU76CKxOMIDCKddCzo6jit9In1CGHvmGFLkd86BS44gwpmD00BruQJ+0hVJ642GQknRF/n21d/b59M4kjKcmpJ8PxhCXPu4PGXjLGXQNricpmyADdF4Za5WnuPYt/2cMQtmom+xbFfrLtkI89GCClRcT2W5F6BXtID5UQakZoC2LfP0XW6QwcIlhp2iYcw5OJvH45u8CGARE3h0qhbZXDL4ZOTzjGK0Urxf0bV9C+B4KU7xw6OsonVf8nBV3ou+yt+ntP1WIXpm/VvzvDE8ECdMdcKXHSMeJ5HNHfDRgsQoOIYTjtlwnBsrMLA52MAODLAuc5Cp/bY6kMkdPKRXQx1sDGxpqhqaTAjV6kyEigtp9nft3LhEmWgw1nsVL5dyOGW8MVO03LvcGf7eFZ2K4MnDFO8pGv8kbdNSN7J0ynFqlVDOThJ0f5fLkwijs671JTpSt6nN8Tf k75rGFg6 emenTo9wP0qIK5iKl0xmLmvWxBLb75cNsp1orlYnbF0noA40A4+hALQMpN6oDGxoz5Y8b/m2MWfLzJtRneYQyCwJPgd0KOFKZn7xCS9q0OOGXwucGCaOL7v0n42aKv/pQOSU6lVQ6nDy3MeZONEeAWVQ6pVFrVPM94KwDoivTn/Y1yUiOf4HcFhp7T5pTO+BG/kCWuHXlYcwi36O/KgAj3ru6ouSoNDaSdDitKHUr57IuEcdBGl7HHUA3S6qyEBcxovwW 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 sparc64, there is no HW modified bit, therefore, SW tracks via a SW bit if the PTE is dirty via pte_mkdirty(). However, pte_mkdirty() currently also unconditionally sets the HW writable bit, which is wrong. pte_mkdirty() is not supposed to make a PTE actually writable, unless the SW writable bit -- pte_write() -- indicates that the PTE is not write-protected. Fortunately, sparc64 also defines a SW writable bit. For example, this already turned into a problem in the context of THP splitting as documented in commit 624a2c94f5b7 ("Partly revert "mm/thp: carry over dirty bit when thp splits on pmd""), and for page migration, as documented in commit 96a9c287e25d ("mm/migrate: fix wrongly apply write bit after mkdirty on sparc64"). Also, we might want to use the dirty PTE bit in the context of KSM with shared zeropage [1], whereby setting the page writable would be problematic. But more general, any code that might end up setting a PTE/PMD dirty inside a VM without write permissions is possibly broken, Before this commit (sun4u in QEMU): root@debian:~/linux/tools/testing/selftests/mm# ./mkdirty # [INFO] detected THP size: 8192 KiB TAP version 13 1..6 # [INFO] PTRACE write access not ok 1 SIGSEGV generated, page not modified # [INFO] PTRACE write access to THP not ok 2 SIGSEGV generated, page not modified # [INFO] Page migration ok 3 SIGSEGV generated, page not modified # [INFO] Page migration of THP ok 4 SIGSEGV generated, page not modified # [INFO] PTE-mapping a THP ok 5 SIGSEGV generated, page not modified # [INFO] UFFDIO_COPY not ok 6 SIGSEGV generated, page not modified Bail out! 3 out of 6 tests failed # Totals: pass:3 fail:3 xfail:0 xpass:0 skip:0 error:0 Test #3,#4,#5 pass ever since we added some MM workarounds, the underlying issue remains. Let's fix the remaining issues and prepare for reverting the workarounds by setting the HW writable bit only if both, the SW dirty bit and the SW writable bit are set. We have to move pte_dirty() and pte_dirty() up. The code patching mechanism and handling constants > 22bit is a bit special on sparc64. The ASM logic in pte_mkdirty() and pte_mkwrite() match the logic in pte_mkold() to create the mask depending on the machine type. The ASM logic in __pte_mkhwwrite() matches the logic in pte_present(), just using an "or" instead of an "and" instruction. With this commit (sun4u in QEMU): root@debian:~/linux/tools/testing/selftests/mm# ./mkdirty # [INFO] detected THP size: 8192 KiB TAP version 13 1..6 # [INFO] PTRACE write access ok 1 SIGSEGV generated, page not modified # [INFO] PTRACE write access to THP ok 2 SIGSEGV generated, page not modified # [INFO] Page migration ok 3 SIGSEGV generated, page not modified # [INFO] Page migration of THP ok 4 SIGSEGV generated, page not modified # [INFO] PTE-mapping a THP ok 5 SIGSEGV generated, page not modified # [INFO] UFFDIO_COPY ok 6 SIGSEGV generated, page not modified # Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0 This handling seems to have been in place forever. [1] https://lkml.kernel.org/r/533a7c3d-3a48-b16b-b421-6e8386e0b142@redhat.com Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: David Hildenbrand Acked-by: Sam Ravnborg --- arch/sparc/include/asm/pgtable_64.h | 116 ++++++++++++++++------------ 1 file changed, 66 insertions(+), 50 deletions(-) diff --git a/arch/sparc/include/asm/pgtable_64.h b/arch/sparc/include/asm/pgtable_64.h index 2dc8d4641734..5563efa1a19f 100644 --- a/arch/sparc/include/asm/pgtable_64.h +++ b/arch/sparc/include/asm/pgtable_64.h @@ -357,6 +357,42 @@ static inline pgprot_t pgprot_noncached(pgprot_t prot) */ #define pgprot_noncached pgprot_noncached +static inline unsigned long pte_dirty(pte_t pte) +{ + unsigned long mask; + + __asm__ __volatile__( + "\n661: mov %1, %0\n" + " nop\n" + " .section .sun4v_2insn_patch, \"ax\"\n" + " .word 661b\n" + " sethi %%uhi(%2), %0\n" + " sllx %0, 32, %0\n" + " .previous\n" + : "=r" (mask) + : "i" (_PAGE_MODIFIED_4U), "i" (_PAGE_MODIFIED_4V)); + + return (pte_val(pte) & mask); +} + +static inline unsigned long pte_write(pte_t pte) +{ + unsigned long mask; + + __asm__ __volatile__( + "\n661: mov %1, %0\n" + " nop\n" + " .section .sun4v_2insn_patch, \"ax\"\n" + " .word 661b\n" + " sethi %%uhi(%2), %0\n" + " sllx %0, 32, %0\n" + " .previous\n" + : "=r" (mask) + : "i" (_PAGE_WRITE_4U), "i" (_PAGE_WRITE_4V)); + + return (pte_val(pte) & mask); +} + #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE) pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags); #define arch_make_huge_pte arch_make_huge_pte @@ -418,28 +454,43 @@ static inline bool is_hugetlb_pte(pte_t pte) } #endif +static inline pte_t __pte_mkhwwrite(pte_t pte) +{ + unsigned long val = pte_val(pte); + + /* + * Note: we only want to set the HW writable bit if the SW writable bit + * and the SW dirty bit are set. + */ + __asm__ __volatile__( + "\n661: or %0, %2, %0\n" + " .section .sun4v_1insn_patch, \"ax\"\n" + " .word 661b\n" + " or %0, %3, %0\n" + " .previous\n" + : "=r" (val) + : "0" (val), "i" (_PAGE_W_4U), "i" (_PAGE_W_4V)); + + return __pte(val); +} + static inline pte_t pte_mkdirty(pte_t pte) { - unsigned long val = pte_val(pte), tmp; + unsigned long val = pte_val(pte), mask; __asm__ __volatile__( - "\n661: or %0, %3, %0\n" - " nop\n" - "\n662: nop\n" + "\n661: mov %1, %0\n" " nop\n" " .section .sun4v_2insn_patch, \"ax\"\n" " .word 661b\n" - " sethi %%uhi(%4), %1\n" - " sllx %1, 32, %1\n" - " .word 662b\n" - " or %1, %%lo(%4), %1\n" - " or %0, %1, %0\n" + " sethi %%uhi(%2), %0\n" + " sllx %0, 32, %0\n" " .previous\n" - : "=r" (val), "=r" (tmp) - : "0" (val), "i" (_PAGE_MODIFIED_4U | _PAGE_W_4U), - "i" (_PAGE_MODIFIED_4V | _PAGE_W_4V)); + : "=r" (mask) + : "i" (_PAGE_MODIFIED_4U), "i" (_PAGE_MODIFIED_4V)); - return __pte(val); + pte = __pte(val | mask); + return pte_write(pte) ? __pte_mkhwwrite(pte) : pte; } static inline pte_t pte_mkclean(pte_t pte) @@ -481,7 +532,8 @@ static inline pte_t pte_mkwrite(pte_t pte) : "=r" (mask) : "i" (_PAGE_WRITE_4U), "i" (_PAGE_WRITE_4V)); - return __pte(val | mask); + pte = __pte(val | mask); + return pte_dirty(pte) ? __pte_mkhwwrite(pte) : pte; } static inline pte_t pte_wrprotect(pte_t pte) @@ -584,42 +636,6 @@ static inline unsigned long pte_young(pte_t pte) return (pte_val(pte) & mask); } -static inline unsigned long pte_dirty(pte_t pte) -{ - unsigned long mask; - - __asm__ __volatile__( - "\n661: mov %1, %0\n" - " nop\n" - " .section .sun4v_2insn_patch, \"ax\"\n" - " .word 661b\n" - " sethi %%uhi(%2), %0\n" - " sllx %0, 32, %0\n" - " .previous\n" - : "=r" (mask) - : "i" (_PAGE_MODIFIED_4U), "i" (_PAGE_MODIFIED_4V)); - - return (pte_val(pte) & mask); -} - -static inline unsigned long pte_write(pte_t pte) -{ - unsigned long mask; - - __asm__ __volatile__( - "\n661: mov %1, %0\n" - " nop\n" - " .section .sun4v_2insn_patch, \"ax\"\n" - " .word 661b\n" - " sethi %%uhi(%2), %0\n" - " sllx %0, 32, %0\n" - " .previous\n" - : "=r" (mask) - : "i" (_PAGE_WRITE_4U), "i" (_PAGE_WRITE_4V)); - - return (pte_val(pte) & mask); -} - static inline unsigned long pte_exec(pte_t pte) { unsigned long mask; From patchwork Tue Apr 11 14:25:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13207674 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 7C9F2C77B6F for ; Tue, 11 Apr 2023 14:25:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B3B928000E; Tue, 11 Apr 2023 10:25:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 810B428000B; Tue, 11 Apr 2023 10:25:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B38328000E; Tue, 11 Apr 2023 10:25:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 521BC28000B for ; Tue, 11 Apr 2023 10:25:32 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 29A7A140DAF for ; Tue, 11 Apr 2023 14:25:32 +0000 (UTC) X-FDA: 80669333304.18.CB1354C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 5C55B1C000C for ; Tue, 11 Apr 2023 14:25:30 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bM1ebTLB; spf=pass (imf18.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=1681223130; 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=TCmOrD6a+wU+gQBMdrpGdoWY5w1T2pXs/1sAiD9BNP4=; b=ujWq6+IWu5gPjDdLhuScitlEX5eWPmXxQ30i6NpmuyCowle4s6t0gyIgVE6rl5AOyWePhK oa2clgB3OUOYHvxLRR0gEZRGgxXni63fqjwAerCDZCGyq6GZqHLm0xrScxzAW/CWighS73 /+M/6MxtcQyk51LGCcnENj7cMk6xiA8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bM1ebTLB; spf=pass (imf18.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681223130; a=rsa-sha256; cv=none; b=V1j/GhMrxG608tL7zIyWmwitLVYDxo5aRRWhs8MMiWfrPlYACHHBpJBjjL2pKkMF/itSrg p6grnV7X1w9ou0uaShAXi9mCZV8wa/rkkzpBRNbl7EGU6/Z9hWzM5Hgit2EeKjhV67zoHf S28UfvuBEUgclHSthFPsDLq0QSj+/2E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681223129; 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=TCmOrD6a+wU+gQBMdrpGdoWY5w1T2pXs/1sAiD9BNP4=; b=bM1ebTLBUtS6qEv0Xt9LiWk4zZAMCUxEmZIWMsXZKbpIdr3nNfrEGx5+vFE1j4RHOJAYN3 kj/32XAKwRtU9++hQ/eSDwTDpIU5OUwoKL3HE055DUGqrqITCH0jl1jKq7Ql/qZXOBp6Ng 17KTgNc68N99aEJLaoC/NPeeTvhAWmI= 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-201-jqtwgpbOM-qgH4SCN4auyA-1; Tue, 11 Apr 2023 10:25:27 -0400 X-MC-Unique: jqtwgpbOM-qgH4SCN4auyA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8E5D128237C8; Tue, 11 Apr 2023 14:25:26 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 64E9714171D7; Tue, 11 Apr 2023 14:25:24 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, sparclinux@vger.kernel.org, David Hildenbrand , Andrew Morton , "David S. Miller" , Peter Xu , Hugh Dickins , Shuah Khan , Sam Ravnborg , Yu Zhao , Anshuman Khandual Subject: [PATCH v1 RESEND 4/6] mm/migrate: revert "mm/migrate: fix wrongly apply write bit after mkdirty on sparc64" Date: Tue, 11 Apr 2023 16:25:10 +0200 Message-Id: <20230411142512.438404-5-david@redhat.com> In-Reply-To: <20230411142512.438404-1-david@redhat.com> References: <20230411142512.438404-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: fceh5wazh7t936x4e4c8q5t5nn6z6xxm X-Rspamd-Queue-Id: 5C55B1C000C X-HE-Tag: 1681223130-813083 X-HE-Meta: U2FsdGVkX1/Q7HHAzAtj1npQzZI3GT7v98R3v9oGZN04rsSoetz21IiM/Xmp7xEND3IudcqPYSivmsWMRMZilAIp7ayO5qLl8mR/llGMmSw5UMbzlpj9c8IdbWsvqMCFiEigeX6aDQTtMwbSo7foZZptepOURFZf0hq+QxB+lHXMHfHaKdzWANGEklQctCUz34Yxvi0n3st1kuyYU6f6VItZs0VLWgRkekvdnOpCWZ2Yd4O3ZuvZia+0i+ndnqhLygKVqmEpVZhNZ6BZWIAYRQ6WJfWdmyyDGyUjSOKSt1Q9ZxM4jY6MJB64CaQmq3GA76Igj+5NYfl1wnHkQDYUmPr2yOxwFeVsaxLuT1lagqq5dBpGdjhXoQpapd7Jqx+m47oKn/JST5y43OXQb7ON6uAnp5lpd0Rsfb9a2iNWAvAkKKy/4StkW/7KNF+3WvPe02Ol2qHjytNhqLu7ijZ5FV7QoHeWV1hDERjRGU2HpWnf2AeY/eYKoHn+C0fji0LrFhKpjel03VPK/OOq3r5P8E8/EyHLl6/RV1AlP1RusYreqJwidH3EjLgWSlJyLg7WKBn2ENj936NZZM7VfRjFtBnS+PiUytEwYgdjNpoAtOs/bEPkk0+85rawtzs74mADZhlCZCY5+LwE0fQdlc801Cwc8FECGZixm7OQ9G/F7XidrkRUTBt54c2GHO2ADWEn10Uz2qalpaMB5nZESgV4cvdwzn0kiOdsFY/mPEhQNPWlh/BX+X49dXW+FNszrJ1r5cl6KgQX3hBSDB2TfYERzVgXelDtGMeMLEp8QV+OUTSGB+Gv07Km/6zkT3nMHbdN78wgSfUcBD6mk9XEpLjORYjAxN8yIzttR37tAYmVwH8MOg/iZMRCdmwGNPqNtd0l5DhdAxseGLfz14LbspIWpXP7iWihyioCw7IE0Q0Gz7VVeFAVOwJCgowP+QEqbBsRbVzLag6wuyHqpaD2Wro IykdAUlL pya//9e6qJL9EZiQfnXJKy6WwkEGRr8BOTNHKlwkxjGdcbpUatlbcYV5OB0hWExZ0iQHbCERhhlS4L2CDgy7OuIdNBssrIHeX6t5jcQTAPtMphqKkt/MqgiNY8o/a3dV1A0fZ++a7laDFh3onmokq5iKWK/A+oT4oDaDFbZ/dWi9FK4uCk7bR+LNO9OelwAjSVOVb 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 reverts commit 96a9c287e25d ("mm/migrate: fix wrongly apply write bit after mkdirty on sparc64"). Now that sparc64 mkdirty handling is fixed and no longer sets a PTE/PMD writable that shouldn't be writable, let's revert the temporary fix. The mkdirty mm selftest still passes with this change on sparc64. Note that loongarch handling was fixed in commit bf2f34a506e6 ("LoongArch: Set _PAGE_DIRTY only if _PAGE_WRITE is set in {pmd,pte}_mkdirty()"). Signed-off-by: David Hildenbrand --- mm/huge_memory.c | 6 ++---- mm/migrate.c | 2 -- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 032fb0ef9cd1..ec86bf1d4e81 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3276,6 +3276,8 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) pmde = mk_huge_pmd(new, READ_ONCE(vma->vm_page_prot)); if (pmd_swp_soft_dirty(*pvmw->pmd)) pmde = pmd_mksoft_dirty(pmde); + if (is_writable_migration_entry(entry)) + pmde = maybe_pmd_mkwrite(pmde, vma); if (pmd_swp_uffd_wp(*pvmw->pmd)) pmde = pmd_mkuffd_wp(pmde); if (!is_migration_entry_young(entry)) @@ -3283,10 +3285,6 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) /* NOTE: this may contain setting soft-dirty on some archs */ if (PageDirty(new) && is_migration_entry_dirty(entry)) pmde = pmd_mkdirty(pmde); - if (is_writable_migration_entry(entry)) - pmde = maybe_pmd_mkwrite(pmde, vma); - else - pmde = pmd_wrprotect(pmde); if (PageAnon(new)) { rmap_t rmap_flags = RMAP_COMPOUND; diff --git a/mm/migrate.c b/mm/migrate.c index db3f154446af..3b21c44e2176 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -225,8 +225,6 @@ static bool remove_migration_pte(struct folio *folio, pte = maybe_mkwrite(pte, vma); else if (pte_swp_uffd_wp(*pvmw.pte)) pte = pte_mkuffd_wp(pte); - else - pte = pte_wrprotect(pte); if (folio_test_anon(folio) && !is_readable_migration_entry(entry)) rmap_flags |= RMAP_EXCLUSIVE; From patchwork Tue Apr 11 14:25:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13207675 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 63C6AC76196 for ; Tue, 11 Apr 2023 14:25:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F406728000B; Tue, 11 Apr 2023 10:25:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC77A28000A; Tue, 11 Apr 2023 10:25:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D424428000B; Tue, 11 Apr 2023 10:25:37 -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 BCADD28000A for ; Tue, 11 Apr 2023 10:25:37 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 76918140C98 for ; Tue, 11 Apr 2023 14:25:37 +0000 (UTC) X-FDA: 80669333514.21.8192081 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf20.hostedemail.com (Postfix) with ESMTP id 840961C001B for ; Tue, 11 Apr 2023 14:25:35 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FzOvU2t+; spf=pass (imf20.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=1681223135; 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=kMuXjs2koZ/Flt7PJQx7eiW1x5ktANLpUol8YMKeKVs=; b=xWTICjqghE4b7uZIpacrX+d2AJdzrFxRePD3hcmoSP3RGjckBt7VqMEJPRXhy5Qbyxx+Cw AdncHdlMdoIfxpjsjZCNy8Zlt9qKypW0xBYgcMWcyUpFYBWk8ekR4iVv4Ct0+YS7DrvRbD UTmR4fTyxcP7/9tNuBkZ++RUoDPST0A= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FzOvU2t+; spf=pass (imf20.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681223135; a=rsa-sha256; cv=none; b=V1gXCLp5fLv/QjVy3Is+AT736Cy4PO8c/2MOZERy+oPZqNjLno7zxZBXuh8COic6VWMduj iqS+gQVUr6QVRFGsxE/BqdibxIMs4F/v7FTM74GB8HEyuiJczVD6p1NiHvW8qhikL1Ra7z 2fP7apjTUnTzU+CAXsH+VTeykQ14C4o= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681223134; 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=kMuXjs2koZ/Flt7PJQx7eiW1x5ktANLpUol8YMKeKVs=; b=FzOvU2t+fu03w6WvzaWLkqWWsXJvuQ6x3tBeMgm1gevZorETxejKDU4fJZHzYkbe3IlOv5 Qe6gZvwM5EZOcUtyeaqHNmY5Ve+sZ76VehgplNWWut4p0i4kDyldC5QbrLopagn3oXybZa o4J4DWxl28lJ7Uf0YnpgxHDfKNvlxXY= 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-497-HXPMVzgwN7ms9In_1b7_gA-1; Tue, 11 Apr 2023 10:25:29 -0400 X-MC-Unique: HXPMVzgwN7ms9In_1b7_gA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EF19628237D4; Tue, 11 Apr 2023 14:25:28 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA53914171D7; Tue, 11 Apr 2023 14:25:26 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, sparclinux@vger.kernel.org, David Hildenbrand , Andrew Morton , "David S. Miller" , Peter Xu , Hugh Dickins , Shuah Khan , Sam Ravnborg , Yu Zhao , Anshuman Khandual Subject: [PATCH v1 RESEND 5/6] mm/huge_memory: revert "Partly revert "mm/thp: carry over dirty bit when thp splits on pmd"" Date: Tue, 11 Apr 2023 16:25:11 +0200 Message-Id: <20230411142512.438404-6-david@redhat.com> In-Reply-To: <20230411142512.438404-1-david@redhat.com> References: <20230411142512.438404-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 840961C001B X-Stat-Signature: mxp1mjks585d181fgqfdnirfiu845skz X-Rspam-User: X-HE-Tag: 1681223135-571639 X-HE-Meta: U2FsdGVkX18onhZvQobnHCy805HfuWQROqEW2OSAoNicq6RQ9yce9gjnnGWDwonp08BmLdF9N7uuvGd5GdQwoBwVoHMG4hEo7Nut38A9pryLFJQBUa6aQnPtUd3mzZGmL38CqST+srd9aSyhhOeTqvKgG5rsnptDu+/bc53ei4zffdh4T3duVXd9PMBBkOBLFAMase3ipPyELq1For5G/IwEF1B4zp94T7QZUPwPZetClYqmOHyPyB5pteeXP8ylD6jWie/MLx4Di8p84ZarVOYhRxF9gmRyTLkGR9B11DQ035D3xU45ZlMYcBV3VCTmQJqayty20GqPKgrADgIsK7ap5+dhSthK0l1g4mSQ6is/TSDSjOHhtEJHCETwDgEJqDSmDjCKsbEHxiSgL9jx77ZmZTWuNFPCPI55nP1Z1hKPq3O3dnQp38VGGD3K6QTeXphJMEtM7YgBpwqWnvc3IQpDa9EsTXDQ6yf3lo479bEJDJqjoITIbQhNei4iNPPcB5ArqHSjKWVLCoOjQr1UODs6QoxemDU8LLLB/5TlUMuzQ/XRkvFVll6SDO6cB/XmWX3zNgvY/dt2JSskyS++0lh5q4jvhnKiuYwgqHJHSC95EB9kHrVjjby2aGW3Qhnx9N1+3gnPYtOMTSoN5w8JIN+jzclA2syU/G7SQmLE5XKA3b+gDX/2zusk1wbxoxRTtlVcyTGfzccYVFZ6KVFyjQ1d1BJtm+vkSzKaHwbD8fCbhIvvquHzOOcLeq8Ye/cLtSxzuCbL0d34hYVRPscir2KrrRSl7fHAbRZRQMwVDHh3pT0bv8Y20/C/NFBmaB+yNiVO6hSUEd0T8g3yy1x0fmgIiIJnUNXGbcY2XSIwOKG82G/N0Fnv4W91OclUrh2UHmw4wxRaf9iLbcrx8oU/3QDVyQqP1jVT3PcIDp5BY0db5oUtJMJGCR7QUW93WakpzhXpQxV2wVjLuOPmAvK HickW9wc /7yOfXU+H168KFMb4JQxpgmuhz3JNrX7h4ejEWW0zj1sfFMyBizDMC6Ewxva14BBSSY6hvWmOEdJk/YRQdpYbvsyiCWPYMKmER9DJ9FxMKqi8lfqBL58E7gm2HrCRII4XuPiMQvHxtgpr496EWZIipmRK9i4GMGBGgffm8PeDWBstx6yYaU/J07xU4o154ncyTQo+sQCVIjkgQzuxG2MsflejWoNUB/PEIuDS4ee5AgooGlM= 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 reverts commit 624a2c94f5b7 ("Partly revert "mm/thp: carry over dirty bit when thp splits on pmd"") and the fixup in commit e833bc503405 ("mm/thp: re-apply mkdirty for small pages after split"). Now that sparc64 mkdirty handling is fixed and no longer sets a PTE/PMD writable that shouldn't be writable, let's revert the temporary fix and remove the stale comment. The mkdirty mm selftest still passes with this change on sparc64. Note that loongarch handling was fixed in commit bf2f34a506e6 ("LoongArch: Set _PAGE_DIRTY only if _PAGE_WRITE is set in {pmd,pte}_mkdirty()") Signed-off-by: David Hildenbrand --- mm/huge_memory.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ec86bf1d4e81..6f3af65435c8 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2238,18 +2238,13 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, entry = maybe_mkwrite(entry, vma); if (anon_exclusive) SetPageAnonExclusive(page + i); + if (!write) + entry = pte_wrprotect(entry); if (!young) entry = pte_mkold(entry); /* NOTE: this may set soft-dirty too on some archs */ if (dirty) entry = pte_mkdirty(entry); - /* - * NOTE: this needs to happen after pte_mkdirty, - * because some archs (sparc64, loongarch) could - * set hw write bit when mkdirty. - */ - if (!write) - entry = pte_wrprotect(entry); if (soft_dirty) entry = pte_mksoft_dirty(entry); if (uffd_wp) From patchwork Tue Apr 11 14:25:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13207676 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 8B153C77B70 for ; Tue, 11 Apr 2023 14:25:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 860C528000F; Tue, 11 Apr 2023 10:25:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79A2F28000A; Tue, 11 Apr 2023 10:25:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 614A928000F; Tue, 11 Apr 2023 10:25:38 -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 4CD3028000A for ; Tue, 11 Apr 2023 10:25:38 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1E259160E0C for ; Tue, 11 Apr 2023 14:25:38 +0000 (UTC) X-FDA: 80669333556.14.864BB7B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 3A548C000E for ; Tue, 11 Apr 2023 14:25:36 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EKmfHWR9; spf=pass (imf28.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=1681223136; 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=qzK4ogixuaREht4MpkiqqhFPvzGg6ALQmBRRBO9bEhw=; b=70IDSlGde4joheSaFvk6yGAtm4dj3CDFYKE2hbURH3df0z9JMEKVrND2VG31Mw67pU5Qak OdcY+AftS0nXhavK+ert0JlayKYbhJHFP3SQZXragHPWdrBXYDzv4Gu6SwusH/ZY/ROL7t PO6w7jYC5M0EcAZOv10Zj/h9xW6Gtug= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EKmfHWR9; spf=pass (imf28.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681223136; a=rsa-sha256; cv=none; b=Ng5x202dgV41480s5ah5b7wdBF4iXmLY7jkwHdnnOsb6r7poz3nKzIabgVlYFB/C9p9scB odOPbqQaevUlfaW5gEVFo1Ty/fkTjaXPWDu1A/W1nquk1hZa7lOzQD0DRA8VeXghDqPwnK eMCZtKMMHGk56zSh2YvjTvMWzxO+JP0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681223135; 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=qzK4ogixuaREht4MpkiqqhFPvzGg6ALQmBRRBO9bEhw=; b=EKmfHWR9HxArZ7X2PHCZeF6T/VlOEWW6Fblifxeh5r3JRgGJvWYaTvlJvmw/nGl+mFuJRu QPsXeOyXiELrTHHqg3qTVYrxJrl5uLUr2xtIabKyAMsUhP4BQRstgupcYcN7Pr7r7kmEEk XPEsrEfXxDf5WDNxbTh48B3kyx+2HcI= 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-333-CZ7gWNr3MiGiLm_dWDQrxw-1; Tue, 11 Apr 2023 10:25:32 -0400 X-MC-Unique: CZ7gWNr3MiGiLm_dWDQrxw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DCE451C27D9C; Tue, 11 Apr 2023 14:25:31 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 826DB14171D6; Tue, 11 Apr 2023 14:25:29 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, sparclinux@vger.kernel.org, David Hildenbrand , Andrew Morton , "David S. Miller" , Peter Xu , Hugh Dickins , Shuah Khan , Sam Ravnborg , Yu Zhao , Anshuman Khandual Subject: [PATCH v1 RESEND 6/6] mm/huge_memory: conditionally call maybe_mkwrite() and drop pte_wrprotect() in __split_huge_pmd_locked() Date: Tue, 11 Apr 2023 16:25:12 +0200 Message-Id: <20230411142512.438404-7-david@redhat.com> In-Reply-To: <20230411142512.438404-1-david@redhat.com> References: <20230411142512.438404-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3A548C000E X-Stat-Signature: c831ezww6pmsjgtcjxaxzz84nbtwitkg X-HE-Tag: 1681223136-453165 X-HE-Meta: U2FsdGVkX19s1ah5qjfHliPAiXeVyMET/ecZZNXuk/892dCWH6Df9nphHxaeM2jCUChCD4Dyf2ppwQJ+vgyO3YzomOyGDNLrnUDatFnfspGtYd88O482SMKkBRTLhSRll3gckzh4c8Cv/GhGIpe9SQ5nUBLjpeVDYIyFP2VGsvwCBGv1UpsQIZBDUFM7ec7lnE1cJ+7MlYAyqk1PIebumOdQNOUz7TDzxjhWfZA37YriPjH/vpLtEKFIyMQ69Z84DO5g8n2IcuSN1/CQ+KwwuWszCg78WEx+BVCY7XK7WiNXZzUG8wpwpWymnFtSJwysF/hxs2U/ls4KxuxBtcWowPrijeRZZLHJ7aoLKiXNQC53kr8njubfsOwPo8nOkSSvJ09ApNInOw19Z+e2dI90XOvnE7jZFasz21xbeGiIg6a3RYZLT6h5WoL9whbGbyWQ82y7WjDYbJZrQAVrK5R9m2A+l6WBxkBXQEKZr9Qi1Z0/dM+/2uhBMwso3Kv4/1nI1Yem+7wSSJKTaZXxDsFXoAuTMGTgDBkaoOV7ejLciqnDJC32CY3uF1yJ6joGhgQ2LolRpQVoNQNTf+JLMqkUdTIOr3Z1TWENyeiGXPDpASFOWLUaTtfQVymbApyTHZoVWwm2iIHKhbxmBUdk5spy7bt1uX/EW617SwxAooa93Icx787YFCZboqndqi0VahSzfrTWldnQU8+xoUr4cmFAVZo8LuujkL6G1yls004K5DHkcMOYZ1DFtoAavF/SsJdZ34MfyZtOUt3xLndcS/uKh3tftTY4E/Y/RheTn4bk3qEJH2xAVVzGZMimHnTu34LtduMoAwF0GEZ4c+FcVWnchsD/qbGctzBMVecauc1+eorISNbbG205gj7cQwr7zWepK7N7ujrb7NpG0iJlD2Lakp5774aW8E2/Vss1jqelB8H+TOU73YpXasQV7tjipT/zBJMK2iLgXft4psaejyS 3qJh0+2B zjNxvx/3sIy9tPd4MQPpqMunWrF2rI89W3q+xURmPHaobUTw3tvvFCa5H9qFVf3RRPM6OG1XhcLdIF1m2yUCxD58Dc0f/x6z+3Nu/fDG96FGuK7xZT43hvqSrjObsO1rEG6vuxqqjSaDLR8H5KHPt8lXowjusY6jY85rk8X9PeDeSAsL0g3x6NvtoyvcWMf8nZFEwIecJZFM4RVTNfPqV4iE9cav2IFxeVWZKv4/5o6finhQ= 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: No need to call maybe_mkwrite() to then wrprotect if the source PMD was not writable. It's worth nothing that this now allows for PTEs to be writable even if the source PMD was not writable: if vma->vm_page_prot includes write permissions. As documented in commit 931298e103c2 ("mm/userfaultfd: rely on vma->vm_page_prot in uffd_wp_range()"), any mechanism that intends to have pages wrprotected (COW, writenotify, mprotect, uffd-wp, softdirty, ...) has to properly adjust vma->vm_page_prot upfront, to not include write permissions. If vma->vm_page_prot includes write permissions, the PTE/PMD can be writable as default. This now mimics the handling in mm/migrate.c:remove_migration_pte() and in mm/huge_memory.c:remove_migration_pmd(), which has been in place for a long time (except that 96a9c287e25d ("mm/migrate: fix wrongly apply write bit after mkdirty on sparc64") temporarily changed it). Signed-off-by: David Hildenbrand --- mm/huge_memory.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 6f3af65435c8..8332e16ac97b 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2235,11 +2235,10 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, entry = pte_swp_mkuffd_wp(entry); } else { entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot)); - entry = maybe_mkwrite(entry, vma); + if (write) + entry = maybe_mkwrite(entry, vma); if (anon_exclusive) SetPageAnonExclusive(page + i); - if (!write) - entry = pte_wrprotect(entry); if (!young) entry = pte_mkold(entry); /* NOTE: this may set soft-dirty too on some archs */