Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp11285pxv; Wed, 14 Jul 2021 21:45:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzs4nvNpnnCsJGDCj4YAzSW76NExkdnUf4b3d4iQAT89l4wbQFTqP8l9ClFGPtnLVnPvtvW X-Received: by 2002:a5d:974f:: with SMTP id c15mr1532011ioo.190.1626324321844; Wed, 14 Jul 2021 21:45:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626324321; cv=none; d=google.com; s=arc-20160816; b=UhnQuAlBwkZ21aV2gkqCDBuFrAaNxc2YWXm3JJTIzRpL7h8Wj4fJrwjeJn0uRFgmm6 pX8wpLPe8X7m4e9gRDLLgPVzq/yBWIYTwJUyPS36Gdm/TvSgUnvGm5PXQ/XkxlRZlSM2 g0F4oLzPjW1ZQhFvUKKQhrDBrgZ9mxh0E7LVxWRCLw8lG48bOBSQVlMLgUf63g5q56xP nx/TWmZ/z4eSBAyxLQQEgT76NKIwiOXLqKY8VJzm4csVgxEyQnJIGDJMw1tU2eXC7TqK zxGKy5K0vzTaUIMeiNyq108NWXT8G79TC5Gu5/fCTkxBiy83GmTO6SwX5DLDXzBPuNZl 5wLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jaOH2K1i0UCcCHDJat4DXALtWZjmQx4kJWboCw70cWs=; b=VZturIxYQ8UhfAJeZ0K1jiGT6E3/dZH0FK0ahAFM8KX0LCkxNeQwOdg/lrrVqetMzM RUvNAOkeagQhWWJ6PLlGso6Sb8UAUjdADKMutiqsMLpdUzNFHA06Q7BrIb1aPudRdZOK BC1t4/+1lmRmFSbAPCAKdmHUPR5Qx9miBkrDUTEQEe3tr/K7izhT0r8gS4Cv4v+Vj994 a6Lc8cmFUGEmoJOR24pgDvSsaC8+AiIb89m8r1FZ3Rpr1naXTS4Fnrrhbd91ig6YOQUH l6/sNmyiHoZPGev4W/e/8CPEEFkmNbNRSbwwx/0f03G+IgowqwS5xWj4FS1DCTOCzdr6 EEfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=lt4vCVlw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l14si5885998jah.73.2021.07.14.21.45.09; Wed, 14 Jul 2021 21:45:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=lt4vCVlw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234555AbhGOD4O (ORCPT + 99 others); Wed, 14 Jul 2021 23:56:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234188AbhGOD4O (ORCPT ); Wed, 14 Jul 2021 23:56:14 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92CD7C06175F; Wed, 14 Jul 2021 20:53:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=jaOH2K1i0UCcCHDJat4DXALtWZjmQx4kJWboCw70cWs=; b=lt4vCVlw0S7uuQ78g/lAxdVJaf Wov74cv3q+YJqfUI+kcnldSH8RSeXI5Ybh035+BMDSixxX8ffPuM9nvjtOA7bBPQ02ZbxS6FUnW9G tO/QlWszNc+JDETEJlwv3egWWe9qI86P3EKuIwhoc0iNLm+9LdL3GmmFAsTg/2Rm4C7a5kCq3udlY LAsoF9l3xaXZyFABRVdTPQ4yf3OaewLoZuLwRL2lbxwLBkoWGn5FuSoF+81i2DS0QIoAYLHe8quk2 wjwXmTgjhp05h+gWP7hMagN7fr6uY/9eibqm0mh/Uc9WUXY3QoCnfzC4dOOBXA+2kkiz2wDd8+w8q vDdaGwIg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1m3sOn-002v7V-If; Thu, 15 Jul 2021 03:51:36 +0000 From: "Matthew Wilcox (Oracle)" To: linux-kernel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Christoph Hellwig , Jeff Layton , "Kirill A . Shutemov" , Vlastimil Babka , William Kucharski , David Howells Subject: [PATCH v14 018/138] mm/filemap: Add folio_lock() Date: Thu, 15 Jul 2021 04:35:04 +0100 Message-Id: <20210715033704.692967-19-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210715033704.692967-1-willy@infradead.org> References: <20210715033704.692967-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is like lock_page() but for use by callers who know they have a folio. Convert __lock_page() to be __folio_lock(). This saves one call to compound_head() per contended call to lock_page(). Saves 455 bytes of text; mostly from improved register allocation and inlining decisions. __folio_lock is 59 bytes while __lock_page was 79. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Christoph Hellwig Acked-by: Jeff Layton Acked-by: Kirill A. Shutemov Acked-by: Vlastimil Babka Reviewed-by: William Kucharski Reviewed-by: David Howells --- include/linux/pagemap.h | 24 +++++++++++++++++++----- mm/filemap.c | 29 +++++++++++++++-------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index a13edc7a2916..c3673c55125b 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -653,7 +653,7 @@ static inline bool wake_page_match(struct wait_page_queue *wait_page, return true; } -extern void __lock_page(struct page *page); +void __folio_lock(struct folio *folio); extern int __lock_page_killable(struct page *page); extern int __lock_page_async(struct page *page, struct wait_page_queue *wait); extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm, @@ -661,13 +661,24 @@ extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm, void unlock_page(struct page *page); void folio_unlock(struct folio *folio); +static inline bool folio_trylock(struct folio *folio) +{ + return likely(!test_and_set_bit_lock(PG_locked, folio_flags(folio, 0))); +} + /* * Return true if the page was successfully locked */ static inline int trylock_page(struct page *page) { - page = compound_head(page); - return (likely(!test_and_set_bit_lock(PG_locked, &page->flags))); + return folio_trylock(page_folio(page)); +} + +static inline void folio_lock(struct folio *folio) +{ + might_sleep(); + if (!folio_trylock(folio)) + __folio_lock(folio); } /* @@ -675,9 +686,12 @@ static inline int trylock_page(struct page *page) */ static inline void lock_page(struct page *page) { + struct folio *folio; might_sleep(); - if (!trylock_page(page)) - __lock_page(page); + + folio = page_folio(page); + if (!folio_trylock(folio)) + __folio_lock(folio); } /* diff --git a/mm/filemap.c b/mm/filemap.c index 1af67ef94e4c..95f89656f126 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1187,7 +1187,7 @@ static void wake_up_page(struct page *page, int bit) */ enum behavior { EXCLUSIVE, /* Hold ref to page and take the bit when woken, like - * __lock_page() waiting on then setting PG_locked. + * __folio_lock() waiting on then setting PG_locked. */ SHARED, /* Hold ref to page and check the bit when woken, like * wait_on_page_writeback() waiting on PG_writeback. @@ -1578,17 +1578,16 @@ void page_endio(struct page *page, bool is_write, int err) EXPORT_SYMBOL_GPL(page_endio); /** - * __lock_page - get a lock on the page, assuming we need to sleep to get it - * @__page: the page to lock + * __folio_lock - Get a lock on the folio, assuming we need to sleep to get it. + * @folio: The folio to lock */ -void __lock_page(struct page *__page) +void __folio_lock(struct folio *folio) { - struct page *page = compound_head(__page); - wait_queue_head_t *q = page_waitqueue(page); - wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, + wait_queue_head_t *q = page_waitqueue(&folio->page); + wait_on_page_bit_common(q, &folio->page, PG_locked, TASK_UNINTERRUPTIBLE, EXCLUSIVE); } -EXPORT_SYMBOL(__lock_page); +EXPORT_SYMBOL(__folio_lock); int __lock_page_killable(struct page *__page) { @@ -1663,10 +1662,10 @@ int __lock_page_or_retry(struct page *page, struct mm_struct *mm, return 0; } } else { - __lock_page(page); + __folio_lock(page_folio(page)); } - return 1; + return 1; } /** @@ -2837,7 +2836,9 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page, struct file **fpin) { - if (trylock_page(page)) + struct folio *folio = page_folio(page); + + if (folio_trylock(folio)) return 1; /* @@ -2850,7 +2851,7 @@ static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page, *fpin = maybe_unlock_mmap_for_io(vmf, *fpin); if (vmf->flags & FAULT_FLAG_KILLABLE) { - if (__lock_page_killable(page)) { + if (__lock_page_killable(&folio->page)) { /* * We didn't have the right flags to drop the mmap_lock, * but all fault_handlers only check for fatal signals @@ -2862,11 +2863,11 @@ static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page, return 0; } } else - __lock_page(page); + __folio_lock(folio); + return 1; } - /* * Synchronous readahead happens when we don't even find a page in the page * cache at all. We don't want to perform IO under the mmap sem, so if we have -- 2.30.2