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 65003C4708D for ; Wed, 4 Jan 2023 20:03:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D25FC8E0002; Wed, 4 Jan 2023 15:03:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CD5D68E0001; Wed, 4 Jan 2023 15:03:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B76FB8E0002; Wed, 4 Jan 2023 15:03:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A8A268E0001 for ; Wed, 4 Jan 2023 15:03:53 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6A7C3140979 for ; Wed, 4 Jan 2023 20:03:53 +0000 (UTC) X-FDA: 80318192346.21.D422515 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 7E4261C0017 for ; Wed, 4 Jan 2023 20:03:51 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FDQI0syL; spf=pass (imf18.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@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=1672862631; 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=VCJoB+mKqkSz09nDtW8cNX32+uHYvkoQDhWihAl/a04=; b=NzX+SjuPFtnKltf3Jhg2Q3inrgmZSK/1RdN0n+A8228IxighgTdiszs2CvlJUDBIJyEiAK bp159lp9TR1/ydboeLJw0aUX2xPeR0fkwmlUpskL++/5v1jGi2xEAsW3lgBkhcIn0ReWrT 1RHFi4SuuRCFuPrFIhoPpbwEivOJZZs= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FDQI0syL; spf=pass (imf18.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672862631; a=rsa-sha256; cv=none; b=kla7fK3iy7FbKX16QKr4auZcy6d6r6ChA8QMy3hdeqf062gFROg3JyN2C+/hcFpeygmyuu 8JcNvM79dWZ+oJwXJxcqiLY/1CHgqrHlw5hWMUq0nkJkosAcvvSKs3cxUWxx5n3dAlJasN MsKNPm2I9AtXWM3bmvzNkBME18XwcQk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672862630; 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=VCJoB+mKqkSz09nDtW8cNX32+uHYvkoQDhWihAl/a04=; b=FDQI0syLd9/I+UxactmcbeIHysifDNVePIaum+Ab4EVd6j3v/R6RlQA5RHr6HGtu78ba+l BqCEjym2FHpTZyXv4MKn3uv7ZCLzdGBvN7xVqSp1WbcVImFmWxXZOUWAshSvy3j4qyEizS 9VqM2lJ/w61aPHkdwCuGcMqWYFP1hpg= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-279-AC-uyN5mN4CGKgyHNPRh2g-1; Wed, 04 Jan 2023 15:03:49 -0500 X-MC-Unique: AC-uyN5mN4CGKgyHNPRh2g-1 Received: by mail-qv1-f72.google.com with SMTP id 3-20020a05621420e300b00531b6f7e4bdso6618101qvk.1 for ; Wed, 04 Jan 2023 12:03:49 -0800 (PST) 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=VCJoB+mKqkSz09nDtW8cNX32+uHYvkoQDhWihAl/a04=; b=EspyBXZbFMFklqsx5TLgy5MBRbDkZ3VIDNoOUe8dBrcXkJ5J25tIpa/S+Rzzzz3j85 KD5UNL0eSZrgR4d4vgCXxQPUpK7IGAzLOgNMLwV/1liTG/iw3pUDhZw8LOdk1MmqR987 RW/2GD9Dcit6rmCyGD/DoteNFYhZh/HR0LgqkJVErlKsiEd7JOSFeukEDP+8FrilC3Ry vbdyqML7A83On8xctckIsS5FvYlobQVOt8lGdIEFwuWasKl5Czqfznd3kPSBXbHusupt xEwhl+c77TyJCtJ4trLBiYAvhdlwvVXqvM7HOCZZFp264R3m00Q46WiV01xi8hCE24aO BAAw== X-Gm-Message-State: AFqh2koFDFXbr4a29bq6zLqxpRPyHEq9DwYlQeNNVlW7dktgMbcBfNM+ ICU/1ujx3WE0mjqIUwIl0q7fVajrZV3W8FVRoBB5GkQ1m7bd1xkbM8jGhdgqbCM/6VBPM3NdR2D jxuf73LGhtwQ= X-Received: by 2002:ac8:4808:0:b0:3a8:16c0:d9c9 with SMTP id g8-20020ac84808000000b003a816c0d9c9mr68523835qtq.7.1672862628995; Wed, 04 Jan 2023 12:03:48 -0800 (PST) X-Google-Smtp-Source: AMrXdXsOBvwpHMgJ01kJRNO6mhggyjm6TVgBXS7eNzC/N5GqdjhLWSKBpavEwqMsvePbQso0RnMI/g== X-Received: by 2002:ac8:4808:0:b0:3a8:16c0:d9c9 with SMTP id g8-20020ac84808000000b003a816c0d9c9mr68523815qtq.7.1672862628616; Wed, 04 Jan 2023 12:03:48 -0800 (PST) Received: from x1n (bras-base-aurron9127w-grc-39-70-52-228-144.dsl.bell.ca. [70.52.228.144]) by smtp.gmail.com with ESMTPSA id r14-20020ac87eee000000b003a6a92a202esm20665683qtc.83.2023.01.04.12.03.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 12:03:48 -0800 (PST) Date: Wed, 4 Jan 2023 15:03:47 -0500 From: Peter Xu To: James Houghton Cc: Mike Kravetz , Muchun Song , Axel Rasmussen , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] hugetlb: unshare some PMDs when splitting VMAs Message-ID: References: <20230101230042.244286-1-jthoughton@google.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 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 7E4261C0017 X-Stat-Signature: snkbpqxggte9phmpwp7bw1ay673chzuk X-Rspam-User: X-HE-Tag: 1672862631-454948 X-HE-Meta: U2FsdGVkX1+xiKIIhrjw9PwjXCa4A2bYdamYetPHGYH05jBZBJS7AZX+szIcdWEYZSOw3+GmaA6JiEEzCF8a1XXM058yUJRJtAzbJ9RPKWYjCpe3IHHEFgraWg9UPgIoQUP2cfQUP2xXgNK9E6RznHMSPSfL2sPIm5eUwkFiTfh174RfWtdlsGdf6PMA1h042e8bT7jCTudIwyfKGc2lj4ywHQ2jta+7EXWMtzlS0oUHaqoBy4OyDLpiVRSAyVEvxeury+q7oh+FmiZZX3Vb+JCTP1AWssFA6TAZvv6/a8vqdXRhrdPweu70/kJXyqt+d8JTjwFqDOoJFGsfW2VIRvMTjw5ryy1A/Cqi1Se87/iU8o5SU3ttoxgxyY8XHu1KdYDt7Ep5JYP9h+T0lHw0GiPsT/Jh1da4c0Yj+GM6f9P/lW9JSizp70amkp04arjCWf9wdJUtLi/xKTzp6f9c8fHdt1bmF6b/rMcbeL83IpvKrdjXllbZ1OytTmkuSx6VR5vtV2KHiRwUdDsvRLk0sjXqXRkIKPKWDfe6pp959TojniupaDymY4h9oaVYcyCLx3UQJv/6VbvBPEd4BqGpDJYCBeHHaT24XgN9RbR7/lsZIpTnbQssdYSYELG7mFdxKF8sLoxs9n3OF31f+fUogu8T+bBLcWi0fvAAavXUyR2KLkdyeF3kbxrtJDAzI8i2DnnAs3KhEL92Nsou1i+265nUVLAszCx+M9E/4D4XgqKrItV2w1okYA8jCT1rtyI8xOKLCoY1UCF0t+lN169BZAMTgLjdKfABpkB6ROoENMGjta5nd4hiWCsDpyGbBlovvqRVR+W9GWaETjnqjSxoUOL8eAvOPFis67nQnuQox0TcbY7tn6N4fjgnWR00GjZSIQBTfreJUXYUO08zk5NGmXMDUCkPQGQE2mH8ZkagQBtNiSsZHTcu80cxTN86MQEiMsTDNBvgNR+sjUVDZiY +QMHCudU YP6WphCbh9BKLMJgdTHaLLvELOo0fsvVBn7GSbZaRmqB3XmWUmF/FqMIzdUjgv9XUKZaPQJJTWoBCiKDkvpWZdR9Y6M19UxN/iuYRJ77w63j0jcg= 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 Wed, Jan 04, 2023 at 07:10:11PM +0000, James Houghton wrote: > > > I'll see if I can confirm that this is indeed possible and send a > > > repro if it is. > > > > I think your analysis above is correct. The key being the failure to unshare > > in the non-PUD_SIZE vma after the split. > > I do indeed hit the WARN_ON_ONCE (repro attached), and the MADV wasn't > even needed (the UFFDIO_REGISTER does the VMA split before "unsharing > all PMDs"). With the fix, we avoid the WARN_ON_ONCE, but the behavior > is still incorrect: I expect the address range to be write-protected, > but it isn't. > > The reason why is that hugetlb_change_protection uses huge_pte_offset, > even if it's being called for a UFFDIO_WRITEPROTECT with > UFFDIO_WRITEPROTECT_MODE_WP. In that particular case, I'm pretty sure > we should be using huge_pte_alloc, but even so, it's not trivial to > get an allocation failure back up to userspace. The non-hugetlb > implementation of UFFDIO_WRITEPROTECT seems to also have this problem. > > Peter, what do you think? Indeed. Thanks for spotting that, James. Non-hugetlb should be fine with having empty pgtable entries. Anon doesn't need to care about no-pgtable-populated ranges so far. Shmem does it with a few change_prepare() calls to populate the entries so the markers can be installed later on. However I think the fault handling is still not well handled as you pointed out even for shmem: that's the path I probably never triggered myself yet before and the code stayed there since a very early version: #define change_pmd_prepare(vma, pmd, cp_flags) \ do { \ if (unlikely(uffd_wp_protect_file(vma, cp_flags))) { \ if (WARN_ON_ONCE(pte_alloc(vma->vm_mm, pmd))) \ break; \ } \ } while (0) I think a better thing we can do here (instead of warning and stop the UFFDIO_WRITEPROTECT at the current stage) is returning with -ENOMEM properly so the user can know the error. We'll need to touch the stacks up to uffd_wp_range() as it's the only one that can trigger the -ENOMEM so far, so as to not ignore retval from change_protection(). Meanwhile, I'd also wonder whether we should call pagefault_out_of_memory() because it should be the same as when pgtable allocation failure happens in page faults, we may want to OOM already. I can take care of hugetlb part too along the way. Man page of UFFDIO_WRITEPROTECT may need a fixup too to introduce -ENOMEM. I can quickly prepare some patches for this, and hopefully it doesn't need to block the current fix on split. Any thoughts? > > > > > To me, the fact it was somewhat difficult to come up with this scenario is an > > argument what we should just unshare at split time as you propose. Who > > knows what other issues may exist. > > > > > 60dfaad65a ("mm/hugetlb: allow uffd wr-protect none ptes") is the > > > commit that introduced the WARN_ON_ONCE; perhaps it's a good choice > > > for a Fixes: tag (if above is indeed true). > > > > If the key issue in your above scenario is indeed the failure of > > hugetlb_unshare_all_pmds in the non-PUD_SIZE vma, then perhaps we tag? > > > > 6dfeaff93be1 ("hugetlb/userfaultfd: unshare all pmds for hugetlbfs when > > register wp") > > SGTM. Thanks Mike. Looks good here too. Thanks, -- Peter Xu