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 ACB29C32793 for ; Wed, 18 Jan 2023 15:12:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D4C36B0073; Wed, 18 Jan 2023 10:12:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 484D66B0074; Wed, 18 Jan 2023 10:12:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34C6D6B0075; Wed, 18 Jan 2023 10:12:07 -0500 (EST) 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 230E46B0073 for ; Wed, 18 Jan 2023 10:12:07 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DEA571C63A3 for ; Wed, 18 Jan 2023 15:12:01 +0000 (UTC) X-FDA: 80368260042.30.7C94AC3 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by imf20.hostedemail.com (Postfix) with ESMTP id C42DD1C000A for ; Wed, 18 Jan 2023 15:11:51 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=aF9K1Wli; spf=pass (imf20.hostedemail.com: domain of mhocko@suse.com designates 195.135.220.28 as permitted sender) smtp.mailfrom=mhocko@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674054716; 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=5em31WnKESBz5bbIwil8S7FpkwJxR7V2RuP1871EUeM=; b=oY1YD2/pqsA/R+uyT5jL9ltv+L54MNJTIy5Q34urgVbLfVbmPPnge82QFFKzwIjMvanuCq IKVvMZX7K+okXNnd8wyVUIIvSWs36oZctzdsV34tSfYhnFYrggjhZVKuytmITrF+7ruCpZ 5wHYpe8qSWGyYynmiZV1Xxgz5YttptM= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=aF9K1Wli; spf=pass (imf20.hostedemail.com: domain of mhocko@suse.com designates 195.135.220.28 as permitted sender) smtp.mailfrom=mhocko@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674054716; a=rsa-sha256; cv=none; b=daPCyEm3Gk3iPiGD9Xv3UJRoyfdUdJzsVno0Ry8Izw8JV8rSDJcm4WqJnr7uL5t4Xz+bK4 SNGkqormtmJpWpBAQ5YICNpyO1Yuk09yPHYsHvMNeDgfO9gtQMVCLBo7tBPhcCPoQ3XpnL Vv9sbAeR4gawSztOjzjNsDn2OhOZMao= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 13D313F7EB; Wed, 18 Jan 2023 15:11:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1674054690; h=from:from:reply-to: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=5em31WnKESBz5bbIwil8S7FpkwJxR7V2RuP1871EUeM=; b=aF9K1WliNn4BIZToV16V+ba3KzF5Cqki5ggfos+Ds0GGqWRtWrc2TWJvCr+t4u5QSjF+tT NYSQH8TPifIG5Z/5Vf46G8ULId0cwKhvRvgkHOwRIQap5qPbY6j323xyeLVQw80xnUCL7D OT91RzX1l7C3rZVWK1eDiXtmf09Usz4= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id D9619139D2; Wed, 18 Jan 2023 15:11:29 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id /UgCNSEMyGMufQAAMHmgww (envelope-from ); Wed, 18 Jan 2023 15:11:29 +0000 Date: Wed, 18 Jan 2023 16:11:29 +0100 From: Michal Hocko To: Jann Horn Cc: Suren Baghdasaryan , peterz@infradead.org, Ingo Molnar , Will Deacon , akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, ldufour@linux.ibm.com, laurent.dufour@fr.ibm.com, paulmck@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, hughlynch@google.com, leewalsh@google.com, posk@google.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH 12/41] mm: add per-VMA lock and helper functions to control it Message-ID: References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-13-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: C42DD1C000A X-Stat-Signature: nremf5cjpwq74ypkxat9bd7fu5jrwtra X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674054711-311631 X-HE-Meta: U2FsdGVkX18pj28G9u9aORr7AZ8zCtyxpJGx9XK90s17HqBNO5dZTMt0TnDpQEkLhyQRdcTCKw/RGKJd3/1LQ29MbCLnFqXtbhKRBbH2hMsmxZHKT2DozNNHsMnKpkgC9/mACmmeUq2B2hKKL/7OIqGyoL+V/DTOHpx7lgWgU8dfY0yJb1EL4L2dL4xmpSR7IBlhNv3+JwHyXmgJw6brfJbIhiFEJ1RbcDz31j1oQWybeCV3i5NcqAWExDO+F3g8/VFNRfYJDfxb3dd4Nz6DHi8WPJNR4Zz/dFfpSa1H2ShTNUIJDQvn3yDfccsXaXUmGcoer4babuNCI2s9EizGawJYSK9BO+QSMYd0kJpgfR6j7v7UOBeQtVRwWCR4Q5nKgwAtnFn2QpJblsIH56+jM1JX/aXxtTmvXssZ8cc5si1MDBQDu/h8WbKQLT543tpg54Y1Vmv2NEOclOZT3yODwBVp+wo7KEygqogZ+nttwk1TNr71iBePGTgitbz9SOI4Ns4Op8/VK6vXMFLdl28+kyJyjZM3kEEZCcnxbvDDHbvNWtcNBJwLvvAG68xjc36pNE8lJZ0HSW78yCnQNZUvTmM5s2ckB1HRVihDuSAOeycHw2qR9eExv1BvguJursjhY1jmGEAGzBYI/jDA6m/Gc6EI3RImVLwIqGU+L0l8d6LQOJPFa3i0yHNj9RkftwMX/+wuVePAJGB0isQuQ2vDC0ghJmRTgjZ/BTKA++MLbh9LlKKeeqMw7jFvylOK8Lc1tP6JI/o3yNMv0vfIHoz+gQu3UgcQUbuN8bRBlIgQYiKo58KJ8BfaHraPEA7UzANIkLO23WbFlpi/AI/L49sPxFirb4ym/wJGqkdyX83haDvbq5lpiNQr+h/QcI1Wk3wpdefo7YbmVZ5yvWl0FEtLMh57dNXfYsxe/AWTjrWNmXXewB2+6VXFAXZBXZ5csSYHHBlEUhdRMsT1LGa5M5/ QYdu27+s ZAZbj/iQ7ZlRwznb4yiduFlKoAuFNqKi7vQiixmI08DdZdAua0QqqcEjjsSEXQs9CvXSg 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 18-01-23 14:23:32, Jann Horn wrote: > On Wed, Jan 18, 2023 at 1:28 PM Michal Hocko wrote: > > On Tue 17-01-23 19:02:55, Jann Horn wrote: > > > +locking maintainers > > > > > > On Mon, Jan 9, 2023 at 9:54 PM Suren Baghdasaryan wrote: > > > > Introduce a per-VMA rw_semaphore to be used during page fault handling > > > > instead of mmap_lock. Because there are cases when multiple VMAs need > > > > to be exclusively locked during VMA tree modifications, instead of the > > > > usual lock/unlock patter we mark a VMA as locked by taking per-VMA lock > > > > exclusively and setting vma->lock_seq to the current mm->lock_seq. When > > > > mmap_write_lock holder is done with all modifications and drops mmap_lock, > > > > it will increment mm->lock_seq, effectively unlocking all VMAs marked as > > > > locked. > > > [...] > > > > +static inline void vma_read_unlock(struct vm_area_struct *vma) > > > > +{ > > > > + up_read(&vma->lock); > > > > +} > > > > > > One thing that might be gnarly here is that I think you might not be > > > allowed to use up_read() to fully release ownership of an object - > > > from what I remember, I think that up_read() (unlike something like > > > spin_unlock()) can access the lock object after it's already been > > > acquired by someone else. > > > > Yes, I think you are right. From a look into the code it seems that > > the UAF is quite unlikely as there is a ton of work to be done between > > vma_write_lock used to prepare vma for removal and actual removal. > > That doesn't make it less of a problem though. > > > > > So if you want to protect against concurrent > > > deletion, this might have to be something like: > > > > > > rcu_read_lock(); /* keeps vma alive */ > > > up_read(&vma->lock); > > > rcu_read_unlock(); > > > > > > But I'm not entirely sure about that, the locking folks might know better. > > > > I am not a locking expert but to me it looks like this should work > > because the final cleanup would have to happen rcu_read_unlock. > > > > Thanks, I have completely missed this aspect of the locking when looking > > into the code. > > > > Btw. looking at this again I have fully realized how hard it is actually > > to see that vm_area_free is guaranteed to sync up with ongoing readers. > > vma manipulation functions like __adjust_vma make my head spin. Would it > > make more sense to have a rcu style synchronization point in > > vm_area_free directly before call_rcu? This would add an overhead of > > uncontended down_write of course. > > Something along those lines might be a good idea, but I think that > rather than synchronizing the removal, it should maybe be something > that splats (and bails out?) if it detects pending readers. If we get > to vm_area_free() on a VMA that has pending readers, we might already > be in a lot of trouble because the concurrent readers might have been > traversing page tables while we were tearing them down or fun stuff > like that. > > I think maybe Suren was already talking about something like that in > another part of this patch series but I don't remember... This http://lkml.kernel.org/r/20230109205336.3665937-27-surenb@google.com? -- Michal Hocko SUSE Labs