From nobody Sun Dec 14 21:23:42 2025 Received: from mail-io1-f52.google.com (mail-io1-f52.google.com [209.85.166.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0ED1D21B908 for ; Fri, 20 Dec 2024 15:48:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734709725; cv=none; b=aL3rLm15GOV7uPCeZU82XPi9IAkp2kcK2nXq7GCj48oGa3NiVk8AAuvG4n3qBAjqBvRNcA763eEb+l2MZ3qxdTyuO/usZhqDKdZvwPXALyVgtaqT8SwtN5EUHrCE44vytvkNGrScJyAKsoPCtv2SatLfMh11jCZV1A1m8NnRmlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734709725; c=relaxed/simple; bh=hlD654noMspdeqksOMGzv5hrDtGKzL2oiFa3eBWzT6w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hJXLSj5fxCtSgXUYrQRngtaEv7ATTrYDWrTp4E9SsUpui0PqtF/wxV9nXHEdxzetYMMe3oqRuGUHh2Pjyc7z0qKVsHsv5+3UBWHEt8br5ZNtPnl214l6GaS9IaosCgnzLKfM9bXTtMUDqLcS5czzmnGoMpG78kYYIIo4SN/1asc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=GFIoSUsA; arc=none smtp.client-ip=209.85.166.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="GFIoSUsA" Received: by mail-io1-f52.google.com with SMTP id ca18e2360f4ac-844ef6275c5so70171539f.0 for ; Fri, 20 Dec 2024 07:48:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1734709723; x=1735314523; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hj114LsQAeZlZx1JIhp9yNhhSBSN5VfiZ1DE7mn9M1E=; b=GFIoSUsAw7RekpRQilDffDad1KbwSZM1hR0LU4Qn3qd5kH4bD6IlPJu6ZYVTgRnnJO OD36Oyg0QQoAICyGD8yqeDiGamkRD5sejSyB3N7ghYOf6U3+xdFTWsQsaaLSbvLy81ZP voY5m/+wX5ztbehFd1nKvmfI1KZmWRRldniEXZ7VMqJNi/naRHFQ/atyibjfZ28I0dNC 6fr0u4rVA/B1zBrnszuq4a1EIo+FyC/L/tS/p7QGhi/1IUPqlaAhdBQKZG55disGFjS7 Pxsia5sj1pcqLkPWF39CBq1ZGip4hwod6++xWDn/ca8awvF9pIIW/AfTgtnGpVdu65Cz SOkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734709723; x=1735314523; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hj114LsQAeZlZx1JIhp9yNhhSBSN5VfiZ1DE7mn9M1E=; b=gaum+iJCAo5vEvI9PBLVuKBxm2tTf+OY1qEo8Brw+8krZdXWgLKeKWTbJYptvV6T1N sO+nKAJ7MS8XqFw675r9irj5RN1/hZza60FBrW83TSzFw6xqKwxtAUfpuJTJebzjW9Th icGjpWr8KS+xfcSW4QwtgFK9z6jEG6eRDRoOt7mQB1ALgVHcvXYqHzFzynQRGBwvpenL yhvkQoQT1Ct/QzG0uByfaq9leDDUWPO3G6AHF8XRjOkxZPPuat4eE/8sbm28WgxbMP1X tqu9SN2nBwUU3h1Xg4IgJSU4CawWlp/IR6u3e8PZTHbcJTEnGDq/qHgdOS0vfPP1gOvc sYiA== X-Forwarded-Encrypted: i=1; AJvYcCW5/7hdSCXTyim1wwLo7NLMUfpV3nziWopdDJx1RIoN5qM4w4HaYFQ4Ars8mJ2rHaXtqORv4WILYDO/950=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1x3ylflmCdXBWOoZo2MRmAop3VWgNB5Sp1KuqdSUsJcJx8K7w K4PqeRUjUC5dPn1gNzRY7V8ti15Sqt0acYra/RO3PtkqZZXsJM1GZdUuY3ItmgI= X-Gm-Gg: ASbGncsopPW2HeZPdBTGVTt7uP3pXuAzDHfF9XWguY2oJa/aOR/IjMWDOewHs2YH7EC vlk7W2YwCNfbvNKadgfWytkfPc8H507pFdPnrcVAM1FJZukEOO80qQJBHPxoPiPPo4ucXEpD+ol 11L+wAirDiwNDX4wZaihB5inoR4dmBhGM/ORowShZOWPYSGHiflK/+5O/Asv0vWPmrePBbY50HL V1NwxO5f5ZDwWcXFduM3JMbcd3+BVSp4+XXQ5mXDzViK1bflMGya4wSf4P+ X-Google-Smtp-Source: AGHT+IECOzR43hLv/D5wLt0YmDzKJdUpeT0f1YGhsBnnrmOZn/oym4rW0bRMY26iQGlNbtfBjUSBgg== X-Received: by 2002:a05:6e02:3683:b0:3a7:8720:9de5 with SMTP id e9e14a558f8ab-3c2d1aa27e8mr34276355ab.1.1734709723099; Fri, 20 Dec 2024 07:48:43 -0800 (PST) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4e68bf66ed9sm837821173.45.2024.12.20.07.48.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2024 07:48:42 -0800 (PST) From: Jens Axboe To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: hannes@cmpxchg.org, clm@meta.com, linux-kernel@vger.kernel.org, willy@infradead.org, kirill@shutemov.name, bfoster@redhat.com, Jens Axboe Subject: [PATCH 06/12] mm/truncate: add folio_unmap_invalidate() helper Date: Fri, 20 Dec 2024 08:47:44 -0700 Message-ID: <20241220154831.1086649-7-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241220154831.1086649-1-axboe@kernel.dk> References: <20241220154831.1086649-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a folio_unmap_invalidate() helper, which unmaps and invalidates a given folio. The caller must already have locked the folio. Embed the old invalidate_complete_folio2() helper in there as well, as nobody else calls it. Use this new helper in invalidate_inode_pages2_range(), rather than duplicate the code there. In preparation for using this elsewhere as well, have it take a gfp_t mask rather than assume GFP_KERNEL is the right choice. This bubbles back to invalidate_complete_folio2() as well. Signed-off-by: Jens Axboe --- mm/internal.h | 2 ++ mm/truncate.c | 53 +++++++++++++++++++++++++++------------------------ 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index cb8d8e8e3ffa..ed3c3690eb03 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -392,6 +392,8 @@ void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long addr, unsigned long end, struct zap_details *details); +int folio_unmap_invalidate(struct address_space *mapping, struct folio *fo= lio, + gfp_t gfp); =20 void page_cache_ra_order(struct readahead_control *, struct file_ra_state = *, unsigned int order); diff --git a/mm/truncate.c b/mm/truncate.c index 7c304d2f0052..e2e115adfbc5 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -525,6 +525,15 @@ unsigned long invalidate_mapping_pages(struct address_= space *mapping, } EXPORT_SYMBOL(invalidate_mapping_pages); =20 +static int folio_launder(struct address_space *mapping, struct folio *foli= o) +{ + if (!folio_test_dirty(folio)) + return 0; + if (folio->mapping !=3D mapping || mapping->a_ops->launder_folio =3D=3D N= ULL) + return 0; + return mapping->a_ops->launder_folio(folio); +} + /* * This is like mapping_evict_folio(), except it ignores the folio's * refcount. We do this because invalidate_inode_pages2() needs stronger @@ -532,14 +541,26 @@ EXPORT_SYMBOL(invalidate_mapping_pages); * shrink_folio_list() has a temp ref on them, or because they're transien= tly * sitting in the folio_add_lru() caches. */ -static int invalidate_complete_folio2(struct address_space *mapping, - struct folio *folio) +int folio_unmap_invalidate(struct address_space *mapping, struct folio *fo= lio, + gfp_t gfp) { - if (folio->mapping !=3D mapping) - return 0; + int ret; + + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); =20 - if (!filemap_release_folio(folio, GFP_KERNEL)) + if (folio_test_dirty(folio)) return 0; + if (folio_mapped(folio)) + unmap_mapping_folio(folio); + BUG_ON(folio_mapped(folio)); + + ret =3D folio_launder(mapping, folio); + if (ret) + return ret; + if (folio->mapping !=3D mapping) + return -EBUSY; + if (!filemap_release_folio(folio, gfp)) + return -EBUSY; =20 spin_lock(&mapping->host->i_lock); xa_lock_irq(&mapping->i_pages); @@ -558,16 +579,7 @@ static int invalidate_complete_folio2(struct address_s= pace *mapping, failed: xa_unlock_irq(&mapping->i_pages); spin_unlock(&mapping->host->i_lock); - return 0; -} - -static int folio_launder(struct address_space *mapping, struct folio *foli= o) -{ - if (!folio_test_dirty(folio)) - return 0; - if (folio->mapping !=3D mapping || mapping->a_ops->launder_folio =3D=3D N= ULL) - return 0; - return mapping->a_ops->launder_folio(folio); + return -EBUSY; } =20 /** @@ -631,16 +643,7 @@ int invalidate_inode_pages2_range(struct address_space= *mapping, } VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio); folio_wait_writeback(folio); - - if (folio_mapped(folio)) - unmap_mapping_folio(folio); - BUG_ON(folio_mapped(folio)); - - ret2 =3D folio_launder(mapping, folio); - if (ret2 =3D=3D 0) { - if (!invalidate_complete_folio2(mapping, folio)) - ret2 =3D -EBUSY; - } + ret2 =3D folio_unmap_invalidate(mapping, folio, GFP_KERNEL); if (ret2 < 0) ret =3D ret2; folio_unlock(folio); --=20 2.45.2