From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9B2F60A2610 for ; Tue, 27 Oct 2026 08:46:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=mrMDyfwo; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MIHfOMOJ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=x/YAccCPR+QDBod9TtDEo8/GkjTX2DKJZMvCayiMInU=; b=mrMDyfwoBKpbc6RRVLEQcix1zg 2TkNeMPVUTfdt1R2+Ox177Bw26xc+OwJj7dKMQ5HcHgb7rhmUmrL8y+T90q9Bv1mAxnn78weRArcl 3BPI27+BXr83iegMHzWdZTh9DuHvJZ6GYEi8z/g8+ffw884KsfVFETdLTihuCh64XJTZmca8MDq0g LnGaO09R/iZlkJ2LZPjJtijxi8Q3AIWSnxp5726BV7AZBuWY7P24BxcbsxLa/2IupvMoZBwbyUR1B rdEVVYCgHzZ6C9oXPVMD+iTvKaY16bZQfKZBtuDR+8g348rhF6JpbYTNWMxAZlNxR6C3WSTLiFyip AexW+0gw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xLcp5-000000045R0-1PER; Tue, 27 Oct 2026 08:46:23 +0000 Received: from tor.source.kernel.org ([2600:3c04:e001:324:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xLcp3-000000045P2-3gyO for kexec@lists.infradead.org; Tue, 27 Oct 2026 08:46:21 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 3EF5A60210; Tue, 27 Oct 2026 08:46:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3898B1F01825; Tue, 27 Oct 2026 08:46:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793090780; bh=x/YAccCPR+QDBod9TtDEo8/GkjTX2DKJZMvCayiMInU=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=MIHfOMOJNGtvK/bOljqxWLAHcLixYnsRYXCAHtUQcunjVDLnNyeo97KX3xAMoJr0Z 5SfJAbFdkjKREw+q9Mtl0ffuKASWKcn7N0F0BmgFbcVUy46FHcanqfHr21qQWcr5jW pN5brAKAoAMh9KQ2wKajox7TxwfMs/QXMXRrm6/pCm731qJU0brAVO4+ppYe1VSs4W QVZYWrlfndxZlKOun9t4YN97qlLu70ioNm4z4BQTX+PWQAgemaKean1qRUpkEbbfK2 Ylrgvy+1T3A3hOJGk4nx9jTZUzT20nRhHgy1MV+6HzvLQExySYnOpZQXYHoPDsoTcO 4j7djgWhTTvDQ== Date: Tue, 27 Oct 2026 08:46:04 +0000 From: "Lorenzo Stoakes (ARM)" To: "David Hildenbrand (Arm)" Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Baolin Wang , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kees Cook , Peter Zijlstra , Jann Horn , Pedro Falcato , Hugh Dickins , Pasha Tatashin , Pratyush Yadav , "Matthew Wilcox (Oracle)" , Jan Kara , Zi Yan , Lance Yang , Rik van Riel , Harry Yoo , Alistair Popple , Nico Pache , Ryan Roberts , Dev Jain , Usama Arif , Kiryl Shutsemau , Matthew Brost , John Hubbard , Peter Xu , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Oscar Salvador , Minchan Kim , Muchun Song , Xu Xin , Chengming Zhou , Chris Li , Kemeng Shi , Nhat Pham , Youngjun Park , Gregory Price , Joshua Hahn , Rakie Kim , Ying Huang , Jason Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-cxl@vger.kernel.org, Josef Bacik , stable@vger.kernel.org Subject: Re: [PATCH 04/46] mm/memfd: make f_pos accesses atomic Message-ID: References: <20261027-fixes_core-v1-0-6f74bec9a5bb@kernel.org> <20261027-fixes_core-v1-4-6f74bec9a5bb@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261027-fixes_core-v1-4-6f74bec9a5bb@kernel.org> X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" X-Spam-Status: No, score=-16.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Tue, Oct 27, 2026 at 12:17:24AM +0100, David Hildenbrand (Arm) wrote: > From: Josef Bacik > > Commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") made > read(), write() and lseek() on a shared open file description of a regular > file serialize on ->f_pos_lock. The flag that asks for it, > FMODE_ATOMIC_POS, is set in do_dentry_open(). A memfd is a regular file > too, but its struct file comes from alloc_file_pseudo() and never passes > through do_dentry_open(), so it does not get the flag: > > CPU0 CPU1 > write(memfd, "a", 1) write(memfd, "b", 1) > pos = file->f_pos (= 0) > pos = file->f_pos (= 0) > vfs_write(..., &pos) > vfs_write(..., &pos) > file->f_pos = pos (= 1) > file->f_pos = pos (= 1) > > One of the two bytes is overwritten. Two threads writing 100000 bytes > each, one at a time, to a memfd end up with a file that is far shorter than > 200000 bytes, while the same program on a tmpfs file never loses a byte. > > Set the flag when the memfd is created. As for any other regular file the > lock is only taken if the file is reachable in more than one way, and > nothing a memfd does in read or write waits for another operation on the > same file. This cannot be done for all alloc_file_pseudo() files: anon > inode files such as eventfd are S_IFREG as well, and a blocked read holding > ->f_pos_lock would lock out the write that is supposed to wake it. > > Fix generated by an LLM and sent by Josef Bacik in private. > > Fixes: 9183df25fe7b ("shm: add memfd_create() syscall") > Cc: stable@vger.kernel.org > Signed-off-by: Josef Bacik > [ david: minor description tweaks ] > Signed-off-by: David Hildenbrand (Arm) Acked-by: Lorenzo Stoakes (ARM) > --- > mm/memfd.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/mm/memfd.c b/mm/memfd.c > index 7d2da23157003..6f5fbce74f21e 100644 > --- a/mm/memfd.c > +++ b/mm/memfd.c > @@ -484,6 +484,8 @@ struct file *memfd_alloc_file(const char *name, unsigned int flags) > } > > file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; > + /* a regular file like any other: POSIX.1-2008/SUSv4 Section XSI 2.9.7 */ > + file->f_mode |= FMODE_ATOMIC_POS; > file->f_flags |= O_LARGEFILE; > > if (flags & MFD_NOEXEC_SEAL) { > > -- > 2.43.0 > -- Cheers, Lorenzo