From patchwork Fri Dec 13 23:13:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 13908021 Received: from fhigh-a5-smtp.messagingengine.com (fhigh-a5-smtp.messagingengine.com [103.168.172.156]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3D86193073 for ; Fri, 13 Dec 2024 23:15:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.156 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734131732; cv=none; b=Rq+7nz3WCubtzem74UaZ5YcMBV51r/7voNDOlpHj15PoifGcFPHofFjHskYsrf8EbpwInt4P25v7Ann+XkZEPBsYuk3nao8RCn8NXP449LnrmaAtWiHff12gjPgLeOwsUh9XHXKaiLF/PM8oy75tjUT4bfCOi58/aF9Sg56U6cg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734131732; c=relaxed/simple; bh=Awh/xf/BjxugPsDblwlH/zn3h8Se5Xzw0Xc9gbG6Xxo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rAp7jY6C+YA173BOawUNQZwx02u86Bm1E69Q5/irVS3h8/vXK12GH9hC7G4A6yNDkZJ2CXy8J659BY2ePWvf32VeqT9UcultiMiXgcoHoieihtpwDgUZRBDKRAoEexU9eCrgWd7IxCJSdX2XQ5LpZ6Bwe5Pjj3jel5y21zZgeNY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io; spf=pass smtp.mailfrom=bur.io; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b=c/OnLmcR; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=ylVxEUTX; arc=none smtp.client-ip=103.168.172.156 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bur.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="c/OnLmcR"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="ylVxEUTX" Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfhigh.phl.internal (Postfix) with ESMTP id D8BED1140154; Fri, 13 Dec 2024 18:15:29 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Fri, 13 Dec 2024 18:15:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=cc :content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1734131729; x= 1734218129; bh=XyrE1aVFJGzs9pH0NiVvoKUoqK9YcvBRiPwnLYrV8zI=; b=c /OnLmcR4452ajmqT7nNR+y3wVxXZA4R3M7HljoA38qmQlwd8uqip1sbkPnx6qvzz bS8NWu8iMYl+4s2R+F8pYjaAbMGnWhr8nZXpkiayh1UKknOF8Hi75Cz+z2JttmfN 7U6a4reqOvuLgX8DXqf5EOFKAZMmhuH212iaceAJm7TPsNaSaxooDuM6V14HYD7k aiPIHGOga/uOIeSvMXWtUXvU828bQ2OCbEBj+gR574wNTG7ZhLljrZKhkOEKDPPL Eyl92isufXqfCcgFzE3NubRLhnvaHJ9ZI+W9Mqsx94C3RS4iE20FNWXZyQHzbodQ V6XfNWgHi+PauK5T+8UkQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1734131729; x=1734218129; bh=XyrE1aVFJGzs9pH0NiVvoKUoqK9Y cvBRiPwnLYrV8zI=; b=ylVxEUTXt83YlR/QqIbyC5x89l1i3XV+s1NHy5ZGabjO vDIPb2eQByrv1uLNKnI4DW1IF/4uYCuPHn8b4GIRpK6IyLD0S9IStAWsLeMSByKw 46sNZBgWZrIa9u061HsybeCQYKH1sGZfTjwMxKTMZoF0POm7xNL460M/7OSaXOt/ a5Z5aw9yDojpdK0UroyipxY3fS1KddgmpIQD9XDQhShksqivzSgnzheXI3yfplpr JQraGn91poasdTyfjOK0gH5/SpY58walS4P1rJp0dEcWCh5aRmR2EnBVbFFmob3g 1S7jHVexIpi85EapKzLpPYpl09W8pgYqALfzRt/Gxw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrkeekgddtjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhephffvufffkf fojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhrihhsuceuuhhrkhhovhcuoegs ohhrihhssegsuhhrrdhioheqnecuggftrfgrthhtvghrnhepieeuffeuvdeiueejhfehie fgkeevudejjeejffevvdehtddufeeihfekgeeuheelnecuvehluhhsthgvrhfuihiivgep tdenucfrrghrrghmpehmrghilhhfrhhomhepsghorhhishessghurhdrihhopdhnsggprh gtphhtthhopedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehlihhnuhigqdgs thhrfhhssehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepkhgvrhhnvghlqd htvggrmhesfhgsrdgtohhm X-ME-Proxy: Feedback-ID: i083147f8:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 13 Dec 2024 18:15:29 -0500 (EST) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 2/2] btrfs: fix btrfs_read_folio race in send Date: Fri, 13 Dec 2024 15:13:14 -0800 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When we call btrfs_read_folio we get an unlocked folio, so it is possible for a different thread to concurrently modify folio->mapping. We must check that this hasn't happened once we do have the lock. Signed-off-by: Boris Burkov --- fs/btrfs/send.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c index c5a318feb8ae..f437138fefbc 100644 --- a/fs/btrfs/send.c +++ b/fs/btrfs/send.c @@ -5280,6 +5280,7 @@ static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len) unsigned cur_len = min_t(unsigned, len, PAGE_SIZE - pg_offset); +again: folio = filemap_lock_folio(mapping, index); if (IS_ERR(folio)) { page_cache_sync_readahead(mapping, @@ -5312,6 +5313,11 @@ static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len) ret = -EIO; break; } + if (folio->mapping != mapping) { + folio_unlock(folio); + folio_put(folio); + goto again; + } } memcpy_from_folio(sctx->send_buf + sctx->send_size, folio,