From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D95BC77B73 for ; Sun, 23 Apr 2023 08:49:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B06EF6B0072; Sun, 23 Apr 2023 04:49:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A8D0F6B0074; Sun, 23 Apr 2023 04:49:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 907116B0075; Sun, 23 Apr 2023 04:49:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7E76E6B0072 for ; Sun, 23 Apr 2023 04:49:13 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 52384AC557 for ; Sun, 23 Apr 2023 08:49:13 +0000 (UTC) X-FDA: 80712031386.11.2C9EDF8 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by imf14.hostedemail.com (Postfix) with ESMTP id 536E3100008 for ; Sun, 23 Apr 2023 08:49:10 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jrzoJ2iG; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682239751; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=j3+0zfn8cNlVMJMbmm5Vq5FMuYDoSo3umxlEbpqHEH4=; b=uP8/U5kKWchKwUhe/PPkzeTSL+qaAw+cpnUiVRq5l3/HiTHAiu15EMfg4MNB0D5R8U6qVF 1QAvAqxBZ1DKzcSjhSq+5siBYT5hW3kjygEFomJdy4FVU/kLyTG67h8Cu11fBw/Mf/krI5 w6rxh6VHd9Cdri0iu3sybDFK9tV6pyo= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jrzoJ2iG; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682239751; a=rsa-sha256; cv=none; b=m1Nw+MYlXictDPUbtf5pXrHTgmgIZ8OhgkMftC2kM2NYXROQEVcGYRt6XMdLdx+PpOexLk JPnstL162XeuUjhLO80mccZxAr5hhwkQ2Qc3jvWzWLfTEixV8CLGN8WMiI628x5zG4fdFw VI28Gm54H9N3w0qIVKta4UEw5wBSUm0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1682239750; x=1713775750; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=SVZBxnuP4vBlqDAzvKI6hIM4GkXNlEyFfSy/CkYXJ7w=; b=jrzoJ2iGp8Y/fieb5b/H600d3jC6EBkkOd5EVjOUyiLaJipXZ0vB4/YH sq1NMN75ugXeC4wlkJVmMxoiUFGh4w6VRcBeZ4t7CUcgou12atBVHIUOT +w8miLepNJYABfIIabUItLF8Dp+4b8koIZVxeoGi0V4qZ9h+NJCfmaCmS 4n37GtMfhuVPkbfqxnPVvK/VIXkRG+qVXkA40kbj3aCbXutRyozTW2RHr J8nrJcOoAKMYWAE0X6Y7bvvUYxohbtUCaNS1KSA/ROxJcb9oDd72AgMTb AyfMOo6c44FAY2GEicWFGE9kXX0gfcWQYXIQnTCxVBauj5Srb5Hx29579 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10688"; a="326585500" X-IronPort-AV: E=Sophos;i="5.99,220,1677571200"; d="scan'208";a="326585500" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Apr 2023 01:49:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10688"; a="686359883" X-IronPort-AV: E=Sophos;i="5.99,220,1677571200"; d="scan'208";a="686359883" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Apr 2023 01:49:02 -0700 From: "Huang, Ying" To: Douglas Anderson Cc: Andrew Morton , Mel Gorman , Vlastimil Babka , Alexander Viro , Christian Brauner , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Yu Zhao , linux-fsdevel@vger.kernel.org, Matthew Wilcox , Bart Van Assche , Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Ingo Molnar , Jan Kara , Juri Lelli , Mel Gorman , Mikulas Patocka , Peter Zijlstra , "Ritesh Harjani (IBM)" , Steven Rostedt , Valentin Schneider , Vincent Guittot , Will Deacon , Zhang Yi Subject: Re: [PATCH v2 2/4] buffer: Add lock_buffer_timeout() In-Reply-To: <20230421151135.v2.2.Ie146eec4d41480ebeb15f0cfdfb3bc9095e4ebd9@changeid> (Douglas Anderson's message of "Fri, 21 Apr 2023 15:12:46 -0700") References: <20230421221249.1616168-1-dianders@chromium.org> <20230421151135.v2.2.Ie146eec4d41480ebeb15f0cfdfb3bc9095e4ebd9@changeid> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) Date: Sun, 23 Apr 2023 16:47:58 +0800 Message-ID: <87bkjfkmrl.fsf@yhuang6-desk2.ccr.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-Rspamd-Queue-Id: 536E3100008 X-Stat-Signature: whcsgtf9xms4o13hifanmqrz4pr497yc X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1682239750-855026 X-HE-Meta: U2FsdGVkX191q+V68ZWUZCcOd0TNMo0cr6bsGRRL/ZWTJ9cJ+b7K4GDRuce/xcHynQ4bGcwkEUV/D794NGzHhdKcsGn1eoj0FvOLFacpBl+e4bnU8t4gLF+3UHh0+AZK9yZUYgiMCnbB2jDKKwWLFyZDp4r3hGCLe5lKQpIOpVo0L3mFc/QVt2R1lcjs/hfR03Qfb2WgV3YMj8Kf2JnW6Xti+9o8ScwjpgDmp+lYzc/gfPuWQ/7o4i4sbu++TWaQJGaRNKGq8pyGpbJyNSFI+4EXGdXZST5ohVOcZj9Z0q9JHuE2l53XLfqN0ZxC0FtgyHQEpFLtJQjrdzQ77uKgzZ/l1BCC4lSLaiFT8Vnv8hAHgPQDBEtYvuwapTwEpb+v1+zS+i98QJ084zyZsrEdMidLkiTuE1l9T/849vOsmwHXPxuak1UJSJHHb3aTSrXi23JhCI9PxFIGnbPUM6qg8uekSGaMPrFPXCL1vno5dDqAUe+1vrOlL34Q2lUDxBiHhCjt5OxZ/1z9kvAbAyuLQYFLWOP4rCfW6/thYQGXU+znESIUnTBNlKjhl56+O0VsWqCDHEqGHAquxD2sw073ayg/N79rZOQT4zpjEtO4K41zgBLbtEjjlaRsEt/SVJKVe0pJgOe+E+X6E1z1qgmh9ZTagRk5qb5KdZsF0LrLS25e57Bnqv2661vsXMi2UmY5tHaqacfWpkvoJiEIb4puOwEu0nG6s/juUcv+Fg7facl178Ek8amhlhllpEPduuqhB4TRnBHIyC0LWLfBH/EHrA6K3z7hWY8iobg2Gg/ssZSihsekWiV3voBcPN5+7U9z71qUnGhBISvgR71wcW14O7YMfR0eR5xwALbNut14ZaKH5YUErpqs1fv/EaUcvi/tiQeucXacd9Tlk3wBExjQfqqaI5qy2QyCUlnE1YLKNZgxTZIgLJ24jcD+Yc98twS/OxS7IIK1EX33wFkTxEG 1NyBWmtx VgO4j6yNselR4vOn6xFeVwvFiAZ5zJgUCDPll73uY4VYnbDW+cPAOcFPKvzPYQ7oarEK5nuiFn3eQZzukxeRCfluXMh6THDYOCCRD8Rg7Gvvacq5/byZo7onRmpb7ZVhQvjUt+1GfDLrpo/oVMJpwYU1D11zxAcEIEiaqDwsuFP402TE7+QgwMkwNhLd3ZakLBkaGwzCw/fq87Gw6mAwzD34v8FC+sZ/VmBCBwO8gfPbtbFcZLvPwd732RUtd1cTQe2i+w2Dbup6prgpVFMcTcU7S6bw6m3pkZevSkv1zn/J/jeWz3I4hXCof+rcsshGS2SAhYGyHbFdbbHM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Douglas Anderson writes: > Add a variant of lock_buffer() that can timeout. This is useful to > avoid unbounded waits for the page lock in kcompactd. > > Signed-off-by: Douglas Anderson > --- > > Changes in v2: > - "Add lock_buffer_timeout()" new for v2. > > fs/buffer.c | 7 +++++++ > include/linux/buffer_head.h | 10 ++++++++++ > include/linux/wait_bit.h | 24 ++++++++++++++++++++++++ > kernel/sched/wait_bit.c | 14 ++++++++++++++ > 4 files changed, 55 insertions(+) > > diff --git a/fs/buffer.c b/fs/buffer.c > index 9e1e2add541e..fcd19c270024 100644 > --- a/fs/buffer.c > +++ b/fs/buffer.c > @@ -71,6 +71,13 @@ void __lock_buffer(struct buffer_head *bh) > } > EXPORT_SYMBOL(__lock_buffer); > > +int __lock_buffer_timeout(struct buffer_head *bh, unsigned long timeout) > +{ > + return wait_on_bit_lock_io_timeout(&bh->b_state, BH_Lock, > + TASK_UNINTERRUPTIBLE, timeout); > +} > +EXPORT_SYMBOL(__lock_buffer_timeout); > + > void unlock_buffer(struct buffer_head *bh) > { > clear_bit_unlock(BH_Lock, &bh->b_state); > diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h > index 8f14dca5fed7..2bae464f89d5 100644 > --- a/include/linux/buffer_head.h > +++ b/include/linux/buffer_head.h > @@ -237,6 +237,7 @@ struct buffer_head *alloc_buffer_head(gfp_t gfp_flags); > void free_buffer_head(struct buffer_head * bh); > void unlock_buffer(struct buffer_head *bh); > void __lock_buffer(struct buffer_head *bh); > +int __lock_buffer_timeout(struct buffer_head *bh, unsigned long timeout); > int sync_dirty_buffer(struct buffer_head *bh); > int __sync_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags); > void write_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags); > @@ -400,6 +401,15 @@ static inline void lock_buffer(struct buffer_head *bh) > __lock_buffer(bh); > } > > +static inline int lock_buffer_timeout(struct buffer_head *bh, > + unsigned long timeout) > +{ > + might_sleep(); > + if (!trylock_buffer(bh)) > + return __lock_buffer_timeout(bh, timeout); > + return 0; > +} > + Add document about return value of lock_buffer_timeout()? Otherwise looks good to me. Best Regards, Huang, Ying > static inline struct buffer_head *getblk_unmovable(struct block_device *bdev, > sector_t block, > unsigned size) > diff --git a/include/linux/wait_bit.h b/include/linux/wait_bit.h > index 7725b7579b78..33f0f60b1c8c 100644 > --- a/include/linux/wait_bit.h > +++ b/include/linux/wait_bit.h > @@ -30,6 +30,7 @@ void wake_up_bit(void *word, int bit); > int out_of_line_wait_on_bit(void *word, int, wait_bit_action_f *action, unsigned int mode); > int out_of_line_wait_on_bit_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout); > int out_of_line_wait_on_bit_lock(void *word, int, wait_bit_action_f *action, unsigned int mode); > +int out_of_line_wait_on_bit_lock_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout); > struct wait_queue_head *bit_waitqueue(void *word, int bit); > extern void __init wait_bit_init(void); > > @@ -208,6 +209,29 @@ wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode) > return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode); > } > > +/** > + * wait_on_bit_lock_io_timeout - wait_on_bit_lock_io() with a timeout > + * @word: the word being waited on, a kernel virtual address > + * @bit: the bit of the word being waited on > + * @mode: the task state to sleep in > + * @timeout: the timeout in jiffies; %MAX_SCHEDULE_TIMEOUT means wait forever > + * > + * Returns zero if the bit was (eventually) found to be clear and was > + * set. Returns non-zero if a timeout happened or a signal was delivered to > + * the process and the @mode allows that signal to wake the process. > + */ > +static inline int > +wait_on_bit_lock_io_timeout(unsigned long *word, int bit, unsigned mode, > + unsigned long timeout) > +{ > + might_sleep(); > + if (!test_and_set_bit(bit, word)) > + return 0; > + return out_of_line_wait_on_bit_lock_timeout(word, bit, > + bit_wait_io_timeout, > + mode, timeout); > +} > + > /** > * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it > * @word: the word being waited on, a kernel virtual address > diff --git a/kernel/sched/wait_bit.c b/kernel/sched/wait_bit.c > index 0b1cd985dc27..629acd1c6c79 100644 > --- a/kernel/sched/wait_bit.c > +++ b/kernel/sched/wait_bit.c > @@ -118,6 +118,20 @@ int __sched out_of_line_wait_on_bit_lock(void *word, int bit, > } > EXPORT_SYMBOL(out_of_line_wait_on_bit_lock); > > +int __sched out_of_line_wait_on_bit_lock_timeout(void *word, int bit, > + wait_bit_action_f *action, > + unsigned mode, > + unsigned long timeout) > +{ > + struct wait_queue_head *wq_head = bit_waitqueue(word, bit); > + DEFINE_WAIT_BIT(wq_entry, word, bit); > + > + wq_entry.key.timeout = jiffies + timeout; > + > + return __wait_on_bit_lock(wq_head, &wq_entry, action, mode); > +} > +EXPORT_SYMBOL(out_of_line_wait_on_bit_lock_timeout); > + > void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit) > { > struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);