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 B71FEC433FE for ; Thu, 6 Oct 2022 19:04:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 039756B0072; Thu, 6 Oct 2022 15:04:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F2BC76B0073; Thu, 6 Oct 2022 15:04:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA4068E0001; Thu, 6 Oct 2022 15:04:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C69166B0072 for ; Thu, 6 Oct 2022 15:04:54 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9455E1A117E for ; Thu, 6 Oct 2022 19:04:54 +0000 (UTC) X-FDA: 79991451708.08.7CA0091 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 2389C40031 for ; Thu, 6 Oct 2022 19:04:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665083092; 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: in-reply-to:in-reply-to:references:references; bh=JWiYaOXLcLPovjcCyVZW8Hvnr9LXyElLmC25/eoQjh0=; b=TPWmpRspp54e1WM4Ris1iLQw3RNRFRChliJfsyTTU5hy67G3+N3GEYmbGK54tkPDN9rjgY xcweTQzlZxm7Z2jx9rhTy11GxiYUycaESmDQDc6psg4KDvFmvNok+6sFyaII9D3LZ//ADC KsQbBP+CU+3mhvKdO9CCKnmgQbH5TU8= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-645-PS0_yepcMSyyxLSCn_-CUw-1; Thu, 06 Oct 2022 15:04:51 -0400 X-MC-Unique: PS0_yepcMSyyxLSCn_-CUw-1 Received: by mail-qk1-f197.google.com with SMTP id o13-20020a05620a2a0d00b006cf9085682dso2165600qkp.7 for ; Thu, 06 Oct 2022 12:04:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JWiYaOXLcLPovjcCyVZW8Hvnr9LXyElLmC25/eoQjh0=; b=OJYimhx8rOTsne4lTC+bVPvfaP/Gsnmx9yZx2++n/uiQwTtLcB/Bg8Bok9wnU0Etto S64hHi6AZGtUcaySg8ceQR/vMtrUX9uADppkKzXP7Yh3RfqgucvdqXJoFqf3IRjV9ZpE k7ldOYBwaUgdqgYRBuoU98bg4A/q4zl16cOBuKxOAKP+UL+k427Mdt70v5rPG+KmPEYn GUh2L3UM1PxBZg4Ousd4q5C3TtSW3csPOnHmIDb2ogY7RFCRzuI7LTDYTqtCLg5/mZ/x M5UFZ4EziYNfQzDqm7dX/2sSX0DWg2AoZqkJVjAbZAXZZ+P4avEjFtGVESO+/pgdRXOF OBog== X-Gm-Message-State: ACrzQf07k3S4YEHMwKM1Aj5qdXQBz+8R9Y5SQF4IKpm0wPCkAs01Lfi6 koJiaXW2dmpfvJdmdWmDocRgTuAppyyPsXF7FjMSK3Vgu9PmJyHhIanmmsnUp5WmE8PLZ0p98uT dBstxUhkIUMA= X-Received: by 2002:a05:6214:21ea:b0:4b1:bc8f:b182 with SMTP id p10-20020a05621421ea00b004b1bc8fb182mr1268060qvj.90.1665083090677; Thu, 06 Oct 2022 12:04:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7ktypX+Rn92Vi/FHAUQs+cZWsJiTq70J7qmscJNemTST4c+2LJpEm40kZ7k1cwD99PWk8Gxw== X-Received: by 2002:a05:6214:21ea:b0:4b1:bc8f:b182 with SMTP id p10-20020a05621421ea00b004b1bc8fb182mr1268029qvj.90.1665083090378; Thu, 06 Oct 2022 12:04:50 -0700 (PDT) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id w2-20020a05620a444200b006dfa0891397sm8495177qkp.32.2022.10.06.12.04.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 12:04:49 -0700 (PDT) Date: Thu, 6 Oct 2022 15:04:48 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Andrew Morton , Shuah Khan , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , "Matthew Wilcox (Oracle)" , Jason Gunthorpe , John Hubbard Subject: Re: [PATCH v1 4/7] mm/ksm: fix KSM COW breaking with userfaultfd-wp via FAULT_FLAG_UNSHARE Message-ID: References: <20220930141931.174362-1-david@redhat.com> <20220930141931.174362-5-david@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665083093; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JWiYaOXLcLPovjcCyVZW8Hvnr9LXyElLmC25/eoQjh0=; b=8YuQ4V/Q7MJO8l6/39bI37gyudchNbJIfiRMWwAQ4RJvHTO5IeiSUlXTYBz6FWd5A8OLII IoH1uhBRecddZKSyurX2dMFbeW25nJBa7V5UuWunvtFfYyVG/j67HXMfvo46dPxdQ3o0z7 O0UzV4gqvpll/CZqG6Mu09tMXKLKiJQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TPWmpRsp; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf01.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665083093; a=rsa-sha256; cv=none; b=VHDm4axFZg6712YjT3jSPjfjTUu0GkoKyMMRqnxxILVr8gZ+ZxhuYkJyqAt8TyImi3O1yl zK9Fo5V5vyPX2A6SNA7WIY7LiS9PWPPtMGekI9Hw48LmCOg17LfrviqTAJA0jFCZyRwO/q vpaOBk4eMgmvbdBZqcWnYAh1K9RdeaY= X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2389C40031 X-Rspam-User: Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TPWmpRsp; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf01.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Stat-Signature: nifmt1s9i7ebhm86g656z1ahrbg5pf6j X-HE-Tag: 1665083092-317840 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 Thu, Oct 06, 2022 at 11:29:29AM +0200, David Hildenbrand wrote: > On 05.10.22 22:35, Peter Xu wrote: > > On Fri, Sep 30, 2022 at 04:19:28PM +0200, David Hildenbrand wrote: > > > Let's stop breaking COW via a fake write fault and let's use > > > FAULT_FLAG_UNSHARE instead. This avoids any wrong side effects of the fake > > > write fault, such as mapping the PTE writable and marking the pte > > > dirty/softdirty. > > > > > > Also, this fixes KSM interaction with userfaultfd-wp: when we have a KSM > > > page that's write-protected by userfaultfd, break_ksm()->handle_mm_fault() > > > will fail with VM_FAULT_SIGBUS and will simpy return in break_ksm() with 0. > > > The warning in dmesg indicates this wrong handling: > > > > > > [ 230.096368] FAULT_FLAG_ALLOW_RETRY missing 881 > > > [ 230.100822] CPU: 1 PID: 1643 Comm: ksm-uffd-wp [...] > > > [ 230.110124] Hardware name: [...] > > > [ 230.117775] Call Trace: > > > [ 230.120227] > > > [ 230.122334] dump_stack_lvl+0x44/0x5c > > > [ 230.126010] handle_userfault.cold+0x14/0x19 > > > [ 230.130281] ? tlb_finish_mmu+0x65/0x170 > > > [ 230.134207] ? uffd_wp_range+0x65/0xa0 > > > [ 230.137959] ? _raw_spin_unlock+0x15/0x30 > > > [ 230.141972] ? do_wp_page+0x50/0x590 > > > [ 230.145551] __handle_mm_fault+0x9f5/0xf50 > > > [ 230.149652] ? mmput+0x1f/0x40 > > > [ 230.152712] handle_mm_fault+0xb9/0x2a0 > > > [ 230.156550] break_ksm+0x141/0x180 > > > [ 230.159964] unmerge_ksm_pages+0x60/0x90 > > > [ 230.163890] ksm_madvise+0x3c/0xb0 > > > [ 230.167295] do_madvise.part.0+0x10c/0xeb0 > > > [ 230.171396] ? do_syscall_64+0x67/0x80 > > > [ 230.175157] __x64_sys_madvise+0x5a/0x70 > > > [ 230.179082] do_syscall_64+0x58/0x80 > > > [ 230.182661] ? do_syscall_64+0x67/0x80 > > > [ 230.186413] entry_SYSCALL_64_after_hwframe+0x63/0xcd > > > > Since it's already there, worth adding the test into ksm_test.c? > > Yes, I can give it a try. What I dislike about ksm_test is that it's a > mixture of benchmarks and test cases that have to explicitly triggered by > parameters. It's not a simple "run all available test cases" tests as we > know it. So maybe something separate (or having it as part of the uffd > tests) makes more sense. We can add an entry into run_vmtests.sh. That's also what current ksm_test does. Yes adding into uffd test would work too, but I do have a plan that we should move functional tests out of userfaultfd.c, leaving that with the stress test only. Not really a big deal, though. > > > > > > > > > Consequently, we will no longer trigger a fake write fault and break COW > > > without any such side-effects. > > > > > > This is primarily a fix for KSM+userfaultfd-wp, however, the fake write > > > fault was always questionable. As this fix is not easy to backport and it's > > > not very critical, let's not cc stable. > > > > A patch to cc most of the stable would probably need to still go with the > > old write approach but attaching ALLOW_RETRY. But I agree maybe that may > > not need to bother, or a report should have arrived earlier.. The unshare > > approach looks much cleaner indeed. > > A fix without FAULT_FLAG_UNSHARE is not straight forward. We really don't > want to notify user space about write events here (because there is none). > And there is no way around the uffd handling in WP code without that. > > FAULT_FLAG_ALLOW_RETRY would rely on userfaultfd triggering and having to > resolve the WP event. Right it'll be very much a false positive, but the userspace should be fine with it e.g. for live snapshot we need to copy page earlier; it still won't stop the process from running along the way. But I agree that's not ideal. -- Peter Xu