From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793206505; b=X46i+YX/wML2iRFVwg+8jnBFIES2sjwvxUMQaZoX0fDoopvZZQ4674w+hRaSVtxaRrer snBkcpHNf4smuLbpJOtfDEyOO0qqQxqjFYh52l14efu6G6WQ8fc2xeaauSgj2gkvMjC7A Dh00qbTHSybB2GbUd9eD/cQeDqRtGxef6+/HWg5xXAiIO0mBL3+EiZQ84PiMmQit3Yd2T L0iWDPo2UPuEK5IQOH7v1cPNEKovTGSP/4s4NFvPMp6F2qkcbsoQeqtP+4191d4Pu9llJ 6CHk3a7eewYGpyymYH3PleP9+mj9Xq1gPsez1cO4Y1lD5WMKoB6bV4obRIVMdaEWCWg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793206505; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; bh=PRHEIBcRVtOU9mxXH6oSTTLdGiFLeS2r2uPk2vm17iM=; b=Rto44ReB1vQmWEdT3+rKMGy8bIGPGJwVAxdItkJo06o7d6lWD/l8cDp0AAYzeg8YWrck OvkjKJXRBKCpmbvEfYC59Vicd2bEADKnTCZCsef1bk7L7WK4vSSBzMYMkRNFWPNs20g6O CF0cTR6mPBTv5AxkyNn0ux42dScI5L9lyXpV7ayV1UG13i4I8uwuhiuy4XR2RGKYcMlz6 vPkx7SqOTzmXhf5bazdoikAB7VpNRngDXMuOn7aM1S7LUGKaRTKXPX0a/mDB0GyYkRaqD rK79pH8KyfeHFCCKlvixPvjLw+8WTVBNnxSxLyzQ1aO70ARZ540CPaMFVZFky9yH0DA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass header.d=soleen.com header.i=@soleen.com header.a=rsa-sha256 header.s=google header.b=YZTcsWCI; arc=none smtp.remote-ip=209.85.222.41 Received: from mail-ua1-f41.google.com (mail-ua1-f41.google.com [209.85.222.41]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29D58606A337 for ; Wed, 28 Oct 2026 16:55:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 29D58606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=soleen.com header.i=@soleen.com header.a=rsa-sha256 header.s=google header.b=YZTcsWCI Received: by mail-ua1-f41.google.com with SMTP id a1e0cc1a2514c-9905d61449cso427961241.0 for ; Wed, 28 Oct 2026 09:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1793206504; x=1793811304; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=PRHEIBcRVtOU9mxXH6oSTTLdGiFLeS2r2uPk2vm17iM=; b=YZTcsWCIUypnB5n4D/hsVZ4sYhEwp0OAWBZMIpgCIgC9VKYTguiLg2qUrAb1YtwQXm fBX2qjbMohrDsF8FpU3xT0wg+Z/De85xmZkcivTaFPK/xC2rSJQKjq22NWOhc1lGVidr hM8JGH+eSNGpRlvJh4EuWmmvnFfWam7TOV3FYkuqC4/2+sI4BlGechXv6xOQOVgypb6q zWTc1Wh0ex2ijemeCLiISE7AQnNSqdbH3U9hHcLHJdV8/WdwHyqp0xajG5zARiSHdoqr pOVMKySmKrvxR+H5Dzu0wqmwb1u32hxlg1K0CKnzvk4A8gUXWUD4WI0PaDrMBKpYlNaK M9iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793206504; x=1793811304; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=PRHEIBcRVtOU9mxXH6oSTTLdGiFLeS2r2uPk2vm17iM=; b=ogjFCdXQHaetHzQHJAKkO0cTXKR4DArUoaKZuXLO75Msk4aFwAHxnEpagBG8Nn6w7V DgRr3tyyyawyncAxVS1ZjagEm2HP11wdUZdgo49sSRhKAi4hS/+kpFrAC0v0pYv6c+gK 1xH5cPYlGnIV2IgoNNnVYgHBr1kh7PisxrsyIdhXd+QLRhG0zq2fj3ZQK2d1CS0alIti NsEqlfzLFcCvGhtXtJb0InCLSzfTl4mQDojSeFBz6xIx2cDc0TKsyq2vsJn8F1T7w88Q quFF4DUM3hpVm0xXZDKu6Wbmiw0IedaiiSXpBjbeyziXQ/EohEklko1PokLFNjBaDDuL pVqA== X-Forwarded-Encrypted: i=1; AKwUvBzp0PtH6enUS9h7xoqtKY0BOXrmNZ6VqNy1NTSv1NLg4HKWWZEXoJuWZEtNOzRHnsw4rsFTuIsi8sgZhv8=@vger.kernel.org X-Gm-Message-State: AFq9FYKnU709KGMtCsn8lPqfoPErbF99zkgRNryg7p1X9eEZUodxQO6o 5vlx+65O9g2EtSp7wp1Q8wmq52Pz0ms7W2hbp6U/NoEjobpKpIttH1fvn4RN/LQ70hg= X-Gm-Gg: AYBFou1uI7G8zV4T0OYDE5mFcz1Pyiu0DLi5q9wwQdlzkEOJ48xKjsjL2D4kHumiTlz jV2irbDcPJV0dvinnTLJub4WiSXmAajxtyVOah5pLz9rG1Io8BJ2Byo71EHO9ramufBFhIWuqCG zEAxfaIwQ3EKMoTkL9nhkFV65STZuosxp1JaBl7c5ec47Cp+Go3EmOFexwoJlQz3IcRv9V13rx+ k9aAmVayZvi/NqsI7C9li+oCTMd3u/Ue/qTjmf36W5chZCwFApNwIdjkgV+b0s1QQo8zhwj14PT kr7kd/ux4aVsQKTWpdcSk17eueJuzK9QXqpnvnpNt+cJhluHRk8ipX4eyDRHs1TUw7VSHthhX0i i9YnjEnKkLSGGI9Ef4wUuuIzm/gx8ogezAa3gn9F11Jen5lW8nisiEX8CxN9ESx7O1DtIpUg8x4 0DafcBgjuqUCotxmEWdtJjBICmqKTIVLrkc9+xZFVQnqCob0docCkCCHOObyLyFr/X7GvVng5lF dz8SherykbMkhznTnuj81rAm2fcaj3HjzJ1RwJHWl1F+cYeSnGGfGEblI5e+CqL5Ca81dvy0rxo q4TmNONKkqulHJLqMNvXUFD4bl/id9+D06W1S/+QXG0K1z4J+jgA7tt2LVaKr4u0mrfsJSXgQWD 20bFBhl7hoIaFyZSujMIXHieBefaJOw== X-Received: by 2002:a05:6102:a54:b0:7b1:6028:5475 with SMTP id ada2fe7eead31-7d03a505a71mr1235376137.12.1793206503993; Wed, 28 Oct 2026 09:55:03 -0700 (PDT) Received: from google.com (16.49.199.35.bc.googleusercontent.com. [35.199.49.16]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-7cea3d32a48sm6300543137.9.2026.10.28.09.55.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Oct 2026 09:55:03 -0700 (PDT) Date: Wed, 28 Oct 2026 12:55:01 -0400 From: Pasha Tatashin To: "David Hildenbrand (Arm)" Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , 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-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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> On 10-27 00:17, 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) Reviewed-by: Pasha Tatashin > --- > 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 >