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 B04C8C00140 for ; Mon, 8 Aug 2022 16:25:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DCAE6B0071; Mon, 8 Aug 2022 12:25:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 28C518E0001; Mon, 8 Aug 2022 12:25:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12CB96B0073; Mon, 8 Aug 2022 12:25:28 -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 06EE96B0071 for ; Mon, 8 Aug 2022 12:25:28 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CBCC5AB45A for ; Mon, 8 Aug 2022 16:25:27 +0000 (UTC) X-FDA: 79776950694.28.CDC0E0E 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 67D1D1C002E for ; Mon, 8 Aug 2022 16:25:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659975926; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GBJ8/l6jgpOV8t0mdJlCv5sc7ShJ57oNHXIQuHMcQt4=; b=DZgzFKP6/lIN6eTBF/rqjnyfa81Zjlp/FMPwMRXn/JnpTXE84NLT47TBDOp88F8qx+UKeN byx0eO4HvC+r4JYRQjO5hMoWGWKV5z7nzN2TykgUugeAynWhKDDm+HbdvgqD/cb7cX5tlw La9RTJxNr6XMegBRV4a2mcLsjoPJQLk= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-83-3dWdE6BDP2uXgQFwZElOCQ-1; Mon, 08 Aug 2022 12:25:23 -0400 X-MC-Unique: 3dWdE6BDP2uXgQFwZElOCQ-1 Received: by mail-wm1-f72.google.com with SMTP id 9-20020a1c0209000000b003a53ae8015bso1509206wmc.1 for ; Mon, 08 Aug 2022 09:25:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc; bh=GBJ8/l6jgpOV8t0mdJlCv5sc7ShJ57oNHXIQuHMcQt4=; b=nprxt+Olssi03sdnrfE+pEWrLijRAjR/yDpFUi8jnxIkGWoejRFXy7HMjVims/Ar2K 1yEV8YkVfcFac1koMglmfRPX1AkXkfIE/t/m7M3CohBIBqHCytOsQhq1YnhcwHNlqCLn 3FDw4EI8y+JAE3e51Re3564e/uoziziqPyMMCpOvOhme/JFlo4RdNiYvLf2A7bnO+sQq YVNuBsCDkPVqBUk25m2L0sjksRL/zsRuw2hP5Qy6FMmV4t3s7QhXb3NpvNN3xek6xMXO 0xgVhVOWcjbberHNNc4NA1PyVYFeugS0K3f5HRw7o2KXHE1OF5NgzrrX8sQ/DvK5rYDN uzuw== X-Gm-Message-State: ACgBeo3akswOvnbll5su3F4D43w86Tvux9I/EGHr1CI0K/EsJWEGuJSM JTIqYu65rxqY3c0uzi6B85X9uI2MTmAq1d9SBh8YF/aNpTLqNmHVSI9ffOBWlpbTcPsjfObkTug oeF1QOBTGgl0= X-Received: by 2002:a05:6000:1a41:b0:20e:687f:1c3 with SMTP id t1-20020a0560001a4100b0020e687f01c3mr11602886wry.415.1659975922584; Mon, 08 Aug 2022 09:25:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR5BAckX9l8cHaxk/zqkDO693mHCwu095nPZhvHuFjfESQPSw4kK4Ra96V+4C7Y5PJPf+bRrUQ== X-Received: by 2002:a05:6000:1a41:b0:20e:687f:1c3 with SMTP id t1-20020a0560001a4100b0020e687f01c3mr11602869wry.415.1659975922271; Mon, 08 Aug 2022 09:25:22 -0700 (PDT) Received: from ?IPV6:2003:d8:2f15:c300:d2ce:1fb5:2460:179a? (p200300d82f15c300d2ce1fb52460179a.dip0.t-ipconnect.de. [2003:d8:2f15:c300:d2ce:1fb5:2460:179a]) by smtp.gmail.com with ESMTPSA id e27-20020adfa45b000000b0021e519eba9bsm11790660wra.42.2022.08.08.09.25.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 08 Aug 2022 09:25:21 -0700 (PDT) Message-ID: <8b317ac7-f80e-4aab-4ad1-4e19a1a0740f@redhat.com> Date: Mon, 8 Aug 2022 18:25:21 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 To: Peter Xu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Mike Kravetz , Muchun Song , Peter Feiner , "Kirill A . Shutemov" References: <20220805110329.80540-1-david@redhat.com> <20220805110329.80540-3-david@redhat.com> <4f644ac5-c40b-32d4-3234-c1dac3d09f83@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v1 2/2] mm/hugetlb: support write-faults in shared mappings In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659975927; a=rsa-sha256; cv=none; b=0TJejX48lkXz6bGZ+gZoQinjMV2EWx4jFiaffyc7sJX5+Q8txcH7Hn5BafZ6ObvjFktrot ge8YDh0VKca6LSX3Ar6cNZzJLFlpZJMVXg4jxZmDf0jarWUMuZ1qOYrQNgC4jp49ypuCnt PQ5hLu60svMmhmhGyLCOXE1PdXXe0MY= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DZgzFKP6; 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=1659975927; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GBJ8/l6jgpOV8t0mdJlCv5sc7ShJ57oNHXIQuHMcQt4=; b=eFz/qdmCz2LrhpAYZmj7CuMtEHlOF+VXOYXtmH1JH+2EN6K8LdlyUIEqAv+3ns31D17Zjp K0cvNRKssemq6JB2IGljXaNj9xg619Wr0SVlKXo1EboTn9UcRZYXI71r/fvSex2EHFJ2kH s3Jg+1G1oUrltG3ycWIEkag/snz6XAI= X-Rspamd-Server: rspam06 X-Rspam-User: X-Stat-Signature: mr3we7k8gb9wwy64q6nwgekemfmintji X-Rspamd-Queue-Id: 67D1D1C002E Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DZgzFKP6; 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 X-HE-Tag: 1659975927-440621 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: >> Relying on VM_SHARED to detect MAP_PRIVATE vs. MAP_SHARED is >> unfortunately wrong. >> >> If you're curious, take a look at f83a275dbc5c ("mm: account for >> MAP_SHARED mappings using VM_MAYSHARE and not VM_SHARED in hugetlbfs") >> and mmap() code. >> >> Long story short: if the file is read-only, we only have VM_MAYSHARE but >> not VM_SHARED (and consequently also not VM_MAYWRITE). > > To ask in another way: if file is RO but mapped RW (mmap() will have > VM_SHARED cleared but VM_MAYSHARE set), then if we check VM_MAYSHARE here > won't we grant write bit errornously while we shouldn't? As the user > doesn't really have write permission to the file. Thus the VM_WRITE check. :) I wonder if we should just do it cleanly and introduce the maybe_mkwrite semantics here as well. Then there is no need for additional VM_WRITE checks and hugetlb will work just like !hugetlb. Thoughts? > >> >>> >>>> + if (unshare) >>>> + return 0; >>> >>> Curious when will this happen especially if we switch to VM_SHARED above. >>> Shouldn't "unshare" not happen at all on a shared region? >> >> FAULT_FLAG_UNSHARE is documented to behave like: >> >> "FAULT_FLAG_UNSHARE is ignored and treated like an ordinary read fault >> when no existing R/O-mapped anonymous page is encountered." >> >> It should currently not happen. Focus on should ;) > > OK. :) > > Then does it also mean that it should be better to turn into > WARN_ON_ONCE()? It's just that it looks like a valid path if without it. Well, it should work (and we handle the !hugetlb path) like that as well. So I'd want to avoid WARN_ON_ONCE() at least for that check. > >> >>> >>>> + if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE))) >>>> + return VM_FAULT_SIGSEGV; >>> >>> I had a feeling that you just want to double check we have write >>> permission, but IIUC this should be checked far earlier or we'll have >>> problem. No strong opinion if so, but I'd suggest dropping this one, >>> otherwise we could add tons of WARN_ON_ONCE() in anywhere in the page fault >>> stack and they mostly won't trigger at all. >> >> Not quite. We usually (!hugetlb) have maybe_mkwrite() all over the >> place. This is just an indication that we don't have maybe semantics >> here. But as we also don't have it for hugetlb anon code below, maybe I >> can just drop it. (or check it for both call paths) > > Hmm, this reminded me to wonder how hugetlb handles FOLL_FORCE|FOLL_WRITE > on RO regions. > > Maybe that check is needed, but however instead of warning and sigbus, we > need to handle it? We don't support FOLL_FORCE|FOLL_WRITE for hugetlb, but if we would, we'd need the maybe_mkwrite semantics. Fortunately I detest private hugetlb mappings / anon hugetlb pages and couldn't care less about debug access until it's actually a problem for someone :) -- Thanks, David / dhildenb