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 6EFABC04A94 for ; Mon, 14 Aug 2023 15:21:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D79476B0074; Mon, 14 Aug 2023 11:21:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D299F8E0002; Mon, 14 Aug 2023 11:21:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C18788E0001; Mon, 14 Aug 2023 11:21:31 -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 B2F866B0074 for ; Mon, 14 Aug 2023 11:21:31 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 69C1F40B91 for ; Mon, 14 Aug 2023 15:21:31 +0000 (UTC) X-FDA: 81123074382.23.70AB07B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 6DE7720022 for ; Mon, 14 Aug 2023 15:21:29 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AXLQNPsD; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of oleg@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=oleg@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692026489; 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=/V5w2sP0PQIzPQLgm9IttuFV+XYMXraW+Ku5dve1XcM=; b=q06PyHMo/b8ggSmSdRpBw+32cf6zmpZ6fO/xtnSyQzXQwFDUFSnNzgFf1WWPrxb6pkTQpg /gYiaesCDvQZSvJRd0XqLFp2MT/esuNakm7Kl2zTN1E872uoDoVqyV8pf6faY9Fz3iXBKE c0c/USzX2leFVQTN/Etn9LbK87sZChQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AXLQNPsD; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of oleg@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=oleg@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692026489; a=rsa-sha256; cv=none; b=vjnIi6HviECsdt/xTAzpW3XvVNshXeNdJ/dnOmhDv8bV6UYPIKchle/rhWSq4wfAhRji+z eg9RlYG+9g+si2Pg5Paw8mTkHOkdyQQJF7keqqvPjs+BAiqyfHH3i09Ja4iHO9AEJ8I22H nFOhJHAcdZY5+Dvu3bGwexqh1wix9tE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692026488; 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=/V5w2sP0PQIzPQLgm9IttuFV+XYMXraW+Ku5dve1XcM=; b=AXLQNPsD510HvDYJPDqc+BtZVloY7CLXBP2RjpmvqKTcCBKkHE2bRVEYEFJmmYIHNHE6Zq /HuFvgj8eVkhCTR6ECkpL9j5BlgZyNEgItielGgkJ6mUNyg9CLfiuO6OBFPpCvEDrS5syI ycjobuhaiwCl2YPQgVgFZLzphbvvew4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-615-24Y3TeWKMRGd1quSiVtWPw-1; Mon, 14 Aug 2023 11:21:25 -0400 X-MC-Unique: 24Y3TeWKMRGd1quSiVtWPw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B15A985CCE7; Mon, 14 Aug 2023 15:21:24 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.27]) by smtp.corp.redhat.com (Postfix) with SMTP id 3E01C492C13; Mon, 14 Aug 2023 15:21:22 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Mon, 14 Aug 2023 17:20:42 +0200 (CEST) Date: Mon, 14 Aug 2023 17:20:38 +0200 From: Oleg Nesterov To: Mateusz Guzik Cc: linux-kernel@vger.kernel.org, torvalds@linux-foundation.org, brauner@kernel.org, ebiederm@xmission.com, david@redhat.com, akpm@linux-foundation.org, linux-mm@kvack.org, koct9i@gmail.com, dave@stgolabs.net Subject: Re: [PATCH] kernel/fork: stop playing lockless games for exe_file replacement Message-ID: <20230814152038.GA2367@redhat.com> References: <20230813123333.1705833-1-mjguzik@gmail.com> <20230814150530.GB17738@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230814150530.GB17738@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Rspamd-Queue-Id: 6DE7720022 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ab9bqqp88ge1sakucsobi5gfkcrj3aeh X-HE-Tag: 1692026489-682787 X-HE-Meta: U2FsdGVkX1/QFVWzCwl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hOE0L0U2EDp6tdJcmzOUtPDDa1b5d1zp9zeHFGTo+w30bqftHjyl5STD/gJxuOc9Z467CKvtDawn0lRVext8YnvjRiw59yY3rjJLlfr8OhIsKBXCv5/PwsRddjdl4Z896UeLYt0TnA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000133, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 08/14, Oleg Nesterov wrote: > > On 08/13, Mateusz Guzik wrote: > > > > fe69d560b5bd ("kernel/fork: always deny write access to current MM > > exe_file") added another lock trip to synchronize the state of exe_file > > against fork, further defeating the point of xchg. > > > > As such I think the atomic here only adds complexity for no benefit. > > > > Just write-lock around the replacement. > > Well, I tend to agree but can't really comment because I forgot everything > about these code paths. > > But I have to admit that I don't understand the code in replace_mm_exe_file() > without this patch... > > old_exe_file = xchg(&mm->exe_file, new_exe_file); > if (old_exe_file) { > /* > * Don't race with dup_mmap() getting the file and disallowing > * write access while someone might open the file writable. > */ > mmap_read_lock(mm); > allow_write_access(old_exe_file); > fput(old_exe_file); > mmap_read_unlock(mm); > } > > Can someone please explain me which exactly race this mmap_read_lock() tries > to avoid and how ? OK, I seem to understand... without mmap_read_lock() it is possible that - dup_mm_exe_file() sees mm->exe_file = old_exe_file - replace_mm_exe_file() does allow_write_access(old_exe_file) - another process does get_write_access(old_exe_file) - dup_mm_exe_file()->deny_write_access() fails Right? Or something else? Well to me Mateusz's patch does make this logic more clear ;) Oleg.