migrate: Avoid unbounded blocks in MIGRATE_SYNC_LIGHT
Subsystems (5)
v2 cover migrate: Avoid unbounded blocks in MIGRATE_SYNC_LIGHT
This series is the result of discussion around my RFC patch [1] where I talked about completely removing the waits for the folio_lock in migrate_folio_unmap().
This new series should, I think, be more palatable to folks. Please let me know what you think!
Most of the description of why I think we want this patch series can be seen in patch #3 ("migrate_pages: Don't wait forever locking pages in MIGRATE_SYNC_LIGHT"). I won't repeat all that information here and would humbly request that anyone wishing to comment on the overall patch series respond there. ;-)
[1] https://lore.kernel.org/r/20230413182313.RFC.1.Ia86ccac02a303154a0b8bc60567e7a95d34c96d3@changeid
Changes in v2:
- "Add folio_lock_timeout()" new for v2.
- "Add lock_buffer_timeout()" new for v2.
- Keep unbounded delay in "SYNC", delay with a timeout in "SYNC_LIGHT"
- "Don't wait forever locking buffers in MIGRATE_SYNC_LIGHT" new for v2.
Douglas Anderson (4):
mm/filemap: Add folio_lock_timeout()
buffer: Add lock_buffer_timeout()
migrate_pages: Don't wait forever locking pages in MIGRATE_SYNC_LIGHT
migrate_pages: Don't wait forever locking buffers in
MIGRATE_SYNC_LIGHT
fs/buffer.c | 7 ++++++ include/linux/buffer_head.h | 10 ++++++++ include/linux/pagemap.h | 16 +++++++++++++ include/linux/wait_bit.h | 24 +++++++++++++++++++ kernel/sched/wait_bit.c | 14 +++++++++++ mm/filemap.c | 47 +++++++++++++++++++++++++++---------- mm/migrate.c | 45 +++++++++++++++++++++-------------- 7 files changed, 132 insertions(+), 31 deletions(-)
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
PATCH v2 1/4 mm/filemap: Add folio_lock_timeout()
Add a variant of folio_lock() that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.
---
Changes in v2:
- "Add folio_lock_timeout()" new for v2.
include/linux/pagemap.h | 16 ++++++++++++++ mm/filemap.c | 47 +++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 13 deletions(-)
include/linux/pagemap.h +16 -0
@@ -892,6 +892,7 @@ static inline bool wake_page_match(struct wait_page_queue *wait_page, }void __folio_lock (struct folio *folio); +int __folio_lock_timeout (struct folio *folio,long timeout);int __folio_lock_killable (struct folio *folio);bool __folio_lock_or_retry (struct folio *folio,struct mm_struct *mm,unsigned int flags);
@@ -952,6 +953,21 @@ static inline void folio_lock(struct folio *folio)__folio_lock (folio); } +/** +* folio_lock_timeout() - Lock this folio, with a timeout. +* @folio: The folio to lock. +* @timeout: The timeout in jiffies; %MAX_SCHEDULE_TIMEOUT means wait forever. +* +* Return: 0 upon success; -ETIMEDOUT upon failure. +*/ +static inline int folio_lock_timeout (struct folio *folio,long timeout) +{ +might_sleep (); +if (!folio_trylock (folio)) +return __folio_lock_timeout (folio, timeout); +return 0 ; +} +/** * lock_page() - Lock the folio containing this page. * @page: The page to lock.
mm/filemap.c +34 -13
119 lines not on this page · raw message
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
On 21 Apr 2023 15:12:45 -0700 Douglas Anderson <[email protected]>
Quoted 2 lines
Add a variant of
folio_lock()that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.
Given no mutex_lock_timeout() (perhaps because timeout makes no sense for spinlock), I suspect your fix lies in the right layer. If waiting for page under IO causes trouble for you, another simpler option is make IO faster (perhaps all you can do) for instance. If kcompactd is waken up by kswapd, waiting for slow IO is the right thing to do.
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Quoted 9 lines · Hillf Danton
On 21 Apr 2023 15:12:45 -0700 Douglas Anderson <[email protected]>
Quoted 2 lines
Add a variant of
folio_lock()that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.Given no
mutex_lock_timeout()(perhaps because timeout makes no sense for spinlock), I suspect your fix lies in the right layer. If waiting for page under IO causes trouble for you, another simpler option is make IO faster (perhaps all you can do) for instance. If kcompactd is waken up by kswapd, waiting for slow IO is the right thing to do.
A bit out of topic. That is almost our original inital use scenarios for EROFS [1] although we didn't actually test Chrome OS, there lies four points:
1) 128kb compressed size unit is not suitable for memory constraint workload, especially memory pressure scenarios, that amplify both I/Os and memory footprints (EROFS was initially optimized with 4KiB pclusters);
2) If you turn into a small compressed size (e.g. 4 KiB), some fs behaves ineffective since its on-disk compressed index isn't designed to be random accessed (another in-memory cache for random access) so you have to count one by one to calculate physical data offset if cache miss;
3) compressed data needs to take extra memory during I/O (especially low-ended devices) that makes the cases worse and our camera app workloads once cannot be properly launched under heavy memory pressure, but in order to keep user best experience we have to keep as many as apps active so that it's hard to kill apps directly. So inplace I/O + decompression is needed in addition to small compressed sizes for overall performance.
4) If considering real-time performance, some algorithms are not quite
suitable for extreme pressure cases;
etc.
I could give more details on this year LSF/MM about this, although it's not a new topic and I'm not a Android guy now.
[1] https://www.usenix.org/conference/atc19/presentation/gao
Thanks,
Gao Xiang
Quoted 2 lines
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Hi Hillf,
Quoted 19 lines · Hillf Danton
On 23 Apr 2023 14:08:49 +0800 Gao Xiang <[email protected]>
Quoted 5 lines · Hillf Danton
Quoted 4 lines
On 21 Apr 2023 15:12:45 -0700 Douglas Anderson <[email protected]>
Quoted 2 lines
Add a variant of
folio_lock()that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.Quoted 7 lines
Quoted 5 lines
Given no
mutex_lock_timeout()(perhaps because timeout makes no sense for spinlock), I suspect your fix lies in the right layer. If waiting for page under IO causes trouble for you, another simpler option is make IO faster (perhaps all you can do) for instance. If kcompactd is waken up by kswapd, waiting for slow IO is the right thing to do.A bit out of topic. That is almost our original inital use scenarios for
Thanks for taking a look.
Quoted 3 lines
EROFS [1] although we didn't actually test Chrome OS, there lies four points:
Quoted 8 lines
Quoted 4 lines
1) 128kb compressed size unit is not suitable for memory constraint workload, especially memory pressure scenarios, that amplify both I/Os and memory footprints (EROFS was initially optimized with 4KiB pclusters);
Feel free to take another one at 2M THP [1].
Honestly I don't catch your point here, does THP has some relationship with this? Almost all smartphones (but I don't know Chromebook honestly) didn't use THP at that time.
Quoted 14 lines
Quoted 6 lines
2) If you turn into a small compressed size (e.g. 4 KiB), some fs behaves ineffective since its on-disk compressed index isn't designed to be random accessed (another in-memory cache for random access) so you have to count one by one to calculate physical data offset if cache miss;
Quoted 7 lines
3) compressed data needs to take extra memory during I/O (especially low-ended devices) that makes the cases worse and our camera app workloads once cannot be properly launched under heavy memory pressure, but in order to keep user best experience we have to keep as many as apps active so that it's hard to kill apps directly. So inplace I/O + decompression is needed in addition to small compressed sizes for overall performance.
Quoted 1 line
Frankly nowadays I have no interest in running linux with <16M RAM for example.
Our cases are tested on 2016-2018 devices under 3 to 6 GB memory if you take a glance at the original ATC paper, the page 9 (section 5.1) wrote:
"However, it costed too much CPU and memory resources, and when trying to
run a camera application, the phone froze for tens of seconds before it finally failed."
I have no idea how 16M RAM here comes from but smartphones doesn't have such limited memory. In brief, if you runs few app, you have few problem. but as long as you keeps more apps in background (and running), then the memory will eventually suffer pressure.
Quoted 6 lines
Quoted 3 lines
4) If considering real-time performance, some algorithms are not quite
suitable for extreme pressure cases;Neither in considering any perf under extreme memory pressure (16M or 64G RAM) because of crystally pure waste of time.
Personally I don't think so, if you'd like to land an effective compression approach for end users and avoid user complaints (app lagging, app frozen, etc). I think these all need to be considered in practice.
Thanks,
Gao Xiang
Quoted 12 lines
Quoted 5 lines
etc.
I could give more details on this year LSF/MM about this, although it's not a new topic and I'm not a Android guy now.
Did you book the air ticket? How many bucks?
Quoted 5 lines
[1] https://www.usenix.org/conference/atc19/presentation/gao
Thanks,
Gao Xiang
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
On 23 Apr 2023 16:35:26 +0800 Gao Xiang <[email protected]>
Quoted 25 lines · Hillf Danton
Quoted 24 lines
On 23 Apr 2023 14:08:49 +0800 Gao Xiang <[email protected]>
Quoted 5 lines · Hillf Danton
Quoted 4 lines
On 21 Apr 2023 15:12:45 -0700 Douglas Anderson <[email protected]>
Quoted 2 lines
Add a variant of
folio_lock()that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.Quoted 7 lines
Quoted 5 lines
Given no
mutex_lock_timeout()(perhaps because timeout makes no sense for spinlock), I suspect your fix lies in the right layer. If waiting for page under IO causes trouble for you, another simpler option is make IO faster (perhaps all you can do) for instance. If kcompactd is waken up by kswapd, waiting for slow IO is the right thing to do.A bit out of topic. That is almost our original inital use scenarios for
Thanks for taking a look.
Quoted 7 lines
EROFS [1] although we didn't actually test Chrome OS, there lies four points:
1) 128kb compressed size unit is not suitable for memory constraint workload, especially memory pressure scenarios, that amplify both I/Os and memory footprints (EROFS was initially optimized with 4KiB pclusters);
Quoted 5 lines
Quoted 3 lines
Feel free to take another one at 2M THP [1].
Honestly I don't catch your point here, does THP has some relationship with
THP tests ended without the help of timeout helpers.
Quoted 24 lines
this? Almost all smartphones (but I don't know Chromebook honestly) didn't use THP at that time.
Quoted 14 lines
Quoted 6 lines
2) If you turn into a small compressed size (e.g. 4 KiB), some fs behaves ineffective since its on-disk compressed index isn't designed to be random accessed (another in-memory cache for random access) so you have to count one by one to calculate physical data offset if cache miss;
Quoted 7 lines
3) compressed data needs to take extra memory during I/O (especially low-ended devices) that makes the cases worse and our camera app workloads once cannot be properly launched under heavy memory pressure, but in order to keep user best experience we have to keep as many as apps active so that it's hard to kill apps directly. So inplace I/O + decompression is needed in addition to small compressed sizes for overall performance.
Quoted 1 line
Frankly nowadays I have no interest in running linux with <16M RAM for example.
Our cases are tested on 2016-2018 devices under 3 to 6 GB memory if you take a glance at the original ATC paper, the page 9 (section 5.1) wrote:
"However, it costed too much CPU and memory resources, and when trying to
run a camera application, the phone froze for tens of seconds before it finally failed."
Quoted 4 lines
I have no idea how 16M RAM here comes from but smartphones doesn't have such limited memory. In brief, if you runs few app, you have few problem. but as long as you keeps more apps in background (and running), then the memory will eventually suffer pressure.
Given no complaints in case of running 16 apps with 1G RAM for instance, what is the point of running 256 apps with the same RAM? And adding changes because of ill designed phone products?
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Quoted 58 lines · Hillf Danton
On 23 Apr 2023 16:35:26 +0800 Gao Xiang <[email protected]>
Quoted 25 lines · Hillf Danton
Quoted 24 lines
On 23 Apr 2023 14:08:49 +0800 Gao Xiang <[email protected]>
Quoted 5 lines · Hillf Danton
Quoted 4 lines
On 21 Apr 2023 15:12:45 -0700 Douglas Anderson <[email protected]>
Quoted 2 lines
Add a variant of
folio_lock()that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.Quoted 7 lines
Quoted 5 lines
Given no
mutex_lock_timeout()(perhaps because timeout makes no sense for spinlock), I suspect your fix lies in the right layer. If waiting for page under IO causes trouble for you, another simpler option is make IO faster (perhaps all you can do) for instance. If kcompactd is waken up by kswapd, waiting for slow IO is the right thing to do.A bit out of topic. That is almost our original inital use scenarios for
Thanks for taking a look.
Quoted 7 lines
EROFS [1] although we didn't actually test Chrome OS, there lies four points:
1) 128kb compressed size unit is not suitable for memory constraint workload, especially memory pressure scenarios, that amplify both I/Os and memory footprints (EROFS was initially optimized with 4KiB pclusters);
Quoted 5 lines
Quoted 3 lines
Feel free to take another one at 2M THP [1].
Honestly I don't catch your point here, does THP has some relationship with
THP tests ended without the help of timeout helpers.
Quoted 24 lines
this? Almost all smartphones (but I don't know Chromebook honestly) didn't use THP at that time.
Quoted 14 lines
Quoted 6 lines
2) If you turn into a small compressed size (e.g. 4 KiB), some fs behaves ineffective since its on-disk compressed index isn't designed to be random accessed (another in-memory cache for random access) so you have to count one by one to calculate physical data offset if cache miss;
Quoted 7 lines
3) compressed data needs to take extra memory during I/O (especially low-ended devices) that makes the cases worse and our camera app workloads once cannot be properly launched under heavy memory pressure, but in order to keep user best experience we have to keep as many as apps active so that it's hard to kill apps directly. So inplace I/O + decompression is needed in addition to small compressed sizes for overall performance.
Quoted 1 line
Frankly nowadays I have no interest in running linux with <16M RAM for example.
Our cases are tested on 2016-2018 devices under 3 to 6 GB memory if you take a glance at the original ATC paper, the page 9 (section 5.1) wrote:
"However, it costed too much CPU and memory resources, and when trying to
run a camera application, the phone froze for tens of seconds before it finally failed."
Quoted 7 lines
Quoted 4 lines
I have no idea how 16M RAM here comes from but smartphones doesn't have such limited memory. In brief, if you runs few app, you have few problem. but as long as you keeps more apps in background (and running), then the memory will eventually suffer pressure.
Given no complaints in case of running 16 apps with 1G RAM for instance, what is the point of running 256 apps with the same RAM? And adding changes
I don't think the ill-designed word is helpful to the overall topic.
I'm not sure if my description is confusing:
1) First, I never said running 256 apps with the same RAM. In fact, in 2018 there are indeed some phones still with 1G RAM, if my memory is correct, such 1G phones couldn't run 16 latest mainstream super apps at the same time smoothly, and, previously compression will lead this worse. Even such phones cannot use a full Android but a minimized Android Go [1] instead. The worst case I've heard on phones with 1G RAM would be "after you checked a new message from friends on a superapp by switch out, another previous one with some incomplete registeration form could be killed and you have to restart and refill the form."
2) apps and baseos can be upgraded over time, especially apps, since Android ecosystem is open. It's hard to get over it.
Thanks,
Gao Xiang
Quoted 1 line
because of ill designed phone products?
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
In reply to Hillf DantonHi,
Quoted 7 lines · Hillf Danton
On 21 Apr 2023 15:12:45 -0700 Douglas Anderson <[email protected]>
Quoted 2 lines
Add a variant of
folio_lock()that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.Given no
mutex_lock_timeout()(perhaps because timeout makes no sense for spinlock), I suspect your fix lies in the right layer.
I'm not 100% sure I understood the above comment, but I think you're saying that the approach my patch takes seems OK.
Quoted 3 lines
If waiting for
page under IO causes trouble for you, another simpler option is make IO faster (perhaps all you can do) for instance.
Yeah, this gets into the discussion about whether our current squashfs settings actually make sense. I suspect that they don't and that we should look into EROFS like Gao suggested, or at least choose different squashfs settings (smaller block sizes, ZSTD instead of zlib). Unfortunately I believe that the current squashfs settings were chosen because disk space is a concern.
Quoted 2 lines
If kcompactd is waken
up by kswapd, waiting for slow IO is the right thing to do.
I don't have enough intuition here, so I'm happy to take others' advice here. I guess my thought was that kcompactd is explicitly not using the full "sync" and instead choosing the "sync light". To me that means we shouldn't block for _too_ long.
-Doug
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
On 24 Apr 2023 09:56:58 -0700 Douglas Anderson <[email protected]>
Quoted 8 lines · On Fri, Apr 21, 2023 at 10:19=E2=80=AFPM Hillf Danton <[email protected]> wrote:
Quoted 2 lines
If kcompactd is waken
up by kswapd, waiting for slow IO is the right thing to do.I don't have enough intuition here, so I'm happy to take others' advice here. I guess my thought was that kcompactd is explicitly not using the full "sync" and instead choosing the "sync light". To me that means we shouldn't block for _too_ long.
Take a look at another case of lock wait [1].
[1] https://lore.kernel.org/lkml/CAHk-=wgyL9OujQ72er7oXt_VsMeno4bMKCTydBT1WSaagZ_5CA@mail.gmail.com/
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Hi,
Quoted 6 lines · Hillf Danton
On 24 Apr 2023 09:56:58 -0700 Douglas Anderson <[email protected]>
Quoted 4 lines · On Fri, Apr 21, 2023 at 10:19=E2=80=AFPM Hillf Danton <[email protected]> wrote:
Quoted 2 lines
If kcompactd is waken
up by kswapd, waiting for slow IO is the right thing to do.
Quoted 8 lines
Quoted 4 lines
I don't have enough intuition here, so I'm happy to take others' advice here. I guess my thought was that kcompactd is explicitly not using the full "sync" and instead choosing the "sync light". To me that means we shouldn't block for _too_ long.
Take a look at another case of lock wait [1].
[1] https://lore.kernel.org/lkml/CAHk-=wgyL9OujQ72er7oXt_VsMeno4bMKCTydBT1WSaagZ_5CA@mail.gmail.com/
So is this an explicit NAK on this approach, then? It still feels worthwhile to me given the current kcompactd design where there is a single thread that's in charge of going through and cleaning up all of memory. Any single pags isn't _that_ important for kcompactd to deal with and it's nice not to block the whole task's ability to make progress. kcompactd is already very much designed in this model (which is why SYNC_LIGHT exists in the first place) and that's why my patch series was relatively simple/short. That being said, if people really don't think I should pursue this then I won't send another version and we can drop it.
-Doug
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
On 25 Apr 2023 07:19:48 -0700 Douglas Anderson <[email protected]>
Quoted 2 lines
So is this an explicit NAK on this approach, then?
Ah I see your point. You misunderstood because I dont think NAk is needed in 99.999% cases, given the fact that 1) your patch will never be able to escape from standing ovation 2) every mutex_trylock() hints the straws in mind.
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Hi,
Quoted 8 lines · Hillf Danton
On 25 Apr 2023 07:19:48 -0700 Douglas Anderson <[email protected]>
Quoted 2 lines
So is this an explicit NAK on this approach, then?
Ah I see your point. You misunderstood because I dont think NAk is needed in 99.999% cases, given the fact that 1) your patch will never be able to escape from standing ovation 2) every
mutex_trylock()hints the straws in mind.
I'm afraid I'm still super confused about what you're saying. You think I should abandon this patch series, or that it might be OK to continue with it?
-Doug
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
In reply to Doug AndersonQuoted 18 lines · Doug Anderson
Hi,
Quoted 6 lines · On Mon, Apr 24, 2023 at 6:09???PM Hillf Danton <[email protected]> wrote:
On 24 Apr 2023 09:56:58 -0700 Douglas Anderson <[email protected]>
Quoted 4 lines · On Fri, Apr 21, 2023 at 10:19=E2=80=AFPM Hillf Danton <[email protected]> wrote:
Quoted 2 lines
If kcompactd is waken
up by kswapd, waiting for slow IO is the right thing to do.Quoted 8 lines
Quoted 4 lines
I don't have enough intuition here, so I'm happy to take others' advice here. I guess my thought was that kcompactd is explicitly not using the full "sync" and instead choosing the "sync light". To me that means we shouldn't block for _too_ long.
Take a look at another case of lock wait [1].
[1] https://lore.kernel.org/lkml/CAHk-=wgyL9OujQ72er7oXt_VsMeno4bMKCTydBT1WSaagZ_5CA@mail.gmail.com/
Quoted 10 lines
So is this an explicit NAK on this approach, then? It still feels worthwhile to me given the current kcompactd design where there is a single thread that's in charge of going through and cleaning up all of memory. Any single pags isn't _that_ important for kcompactd to deal with and it's nice not to block the whole task's ability to make progress. kcompactd is already very much designed in this model (which is why
SYNC_LIGHTexists in the first place) and that's why my patch series was relatively simple/short. That being said, if people really don't think I should pursue this then I won't send another version and we can drop it.
I don't consider it to be an explicit NAK but lets
cc Linus because it's a valid question. Linus, the patch is
https://lore.kernel.org/lkml/20230421151135.v2.1.I2b71e11264c5c214bc59744b9e13e4c353bc5714@changeid/
asnd it's adding folio_lock_timeout which in older terms is a lock_page_timout. The intended use is kcompactd doing out-of-line compaction (like kswapd does out-of-line reclaim) to try lock a page in MIGRATE_SYNC_LIGHT mode but if it cannot be locked quickly then give up and move on to another migration candidate. The MIGRATE_SYNC_LIGHT is expected to incur some delays while trying to make forward progress and the overall problem is that kcompactd can sometimes stall for many seconds and sometimes minutes on one page.
The reason I don't consider this patch a NAK candidate is that this is not conditional locking as such because no special action is taken if the lock cannot be acquired. In the referenced mail, I think the context for the IO NOWAIT stuff is "try lock and if that fails, delegate the work to an async context". That is not necessarily a universal win and it's potentially complex. It's not a universal win because it's unknown how long it would take to acquire the lock and it may be a short enough period to be cheaper than the setup_for_async+context_switch+completion handler. If that happens often enough in a short window then delegation may be slower overall than doing the work synchronously. It's potentially complex because the setup for async handling and completion needs code that must be maintained.
The kcompactd case using folio_lock_timeout is different. If the lock fails, it's not being explicitly delegated to another context, the page is simply ignored and kcompactd moves on. Fair enough, another context may end up migrating the same page in direct compaction or kcompactd at a later time but there is no complex setup for that and it's not explicit delegation. It's vaguely similar to how shrink_folio_list() calls folio_trylock and if that fails, keep the page on the LRU for a future attempt with the main difference being that some time is spent on trylock. This is *also* not necessarily a universal win because kcompactd could find a suitable migration candidate quicker by a plain trylock but that's what MIGRATE_ASYNC is for, MIGRATE_SYNC_LIGHT is expected to delay for short periods of time when MIGRATE_ASYNC fails and the problem being solved is the folio lock taking minutes to acquire.
Signature · 2 lines
Mel Gorman SUSE Labs
Matthew Wilcox
maintainerRe: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Quoted 41 lines · Mel Gorman
Quoted 5 lines · Doug Anderson
Quoted 3 lines · On Mon, Apr 24, 2023 at 6:09???PM Hillf Danton <[email protected]> wrote:
Take a look at another case of lock wait [1].
[1] https://lore.kernel.org/lkml/CAHk-=wgyL9OujQ72er7oXt_VsMeno4bMKCTydBT1WSaagZ_5CA@mail.gmail.com/
Quoted 10 lines
So is this an explicit NAK on this approach, then? It still feels worthwhile to me given the current kcompactd design where there is a single thread that's in charge of going through and cleaning up all of memory. Any single pags isn't _that_ important for kcompactd to deal with and it's nice not to block the whole task's ability to make progress. kcompactd is already very much designed in this model (which is why
SYNC_LIGHTexists in the first place) and that's why my patch series was relatively simple/short. That being said, if people really don't think I should pursue this then I won't send another version and we can drop it.I don't consider it to be an explicit NAK but lets
cc Linus because it's a valid question. Linus, the patch is
https://lore.kernel.org/lkml/20230421151135.v2.1.I2b71e11264c5c214bc59744b9e13e4c353bc5714@changeid/
asnd it's addingfolio_lock_timeoutwhich in older terms is alock_page_timout. The intended use is kcompactd doing out-of-line compaction (like kswapd does out-of-line reclaim) to try lock a page inMIGRATE_SYNC_LIGHTmode but if it cannot be locked quickly then give up and move on to another migration candidate. TheMIGRATE_SYNC_LIGHTis expected to incur some delays while trying to make forward progress and the overall problem is that kcompactd can sometimes stall for many seconds and sometimes minutes on one page.The reason I don't consider this patch a NAK candidate is that this is not conditional locking as such because no special action is taken if the lock cannot be acquired. In the referenced mail, I think the context for the IO NOWAIT stuff is "try lock and if that fails, delegate the work to an async context". That is not necessarily a universal win and it's potentially complex. It's not a universal win because it's unknown how long it would take to acquire the lock and it may be a short enough period to be cheaper than the setup_for_async+context_switch+completion handler. If that happens often enough in a short window then delegation may be slower overall than doing the work synchronously. It's potentially complex because the setup for async handling and completion needs code that must be maintained.
Quoted 13 lines
The kcompactd case using
folio_lock_timeoutis different. If the lock fails, it's not being explicitly delegated to another context, the page is simply ignored and kcompactd moves on. Fair enough, another context may end up migrating the same page in direct compaction or kcompactd at a later time but there is no complex setup for that and it's not explicit delegation. It's vaguely similar to howshrink_folio_list()callsfolio_trylockand if that fails, keep the page on the LRU for a future attempt with the main difference being that some time is spent on trylock. This is *also* not necessarily a universal win because kcompactd could find a suitable migration candidate quicker by a plain trylock but that's whatMIGRATE_ASYNCis for,MIGRATE_SYNC_LIGHTis expected to delay for short periods of time whenMIGRATE_ASYNCfails and the problem being solved is the folio lock taking minutes to acquire.
I'm not generally a fan of lock-with-timeout approaches. I think the rationale for this one makes sense, but we're going to see some people try to use this for situations where it doesn't make sense. I almost wonder if we shouldn't spin rather than sleep on this lock, since the window of time we're willing to wait is so short. I'm certainly not willing to NAK this patch since it's clearly fixing a real problem.
Hm. If the problem is that we want to wait for the lock unless the lock is being held for I/O, we can actually tell that in the caller.
if (folio_test_uptodate(folio)) folio_lock(folio); else folio_trylock(folio);
(the folio lock isn't held for writeback, just taken and released; if the folio is uptodate, the folio lock should only be taken for a short time; if it's !uptodate then it's probably being read)
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Hi,
Quoted 4 lines · Matthew Wilcox
I'm not generally a fan of lock-with-timeout approaches. I think the rationale for this one makes sense, but we're going to see some people try to use this for situations where it doesn't make sense.
Although it won't completely prevent the issue, I could add a comment to the function (and the similar lock_buffer_timeout() added in patch #2 [1] at least warning people that it's discouraged to use the function without very careful consideration.
[1] https://lore.kernel.org/r/20230421151135.v2.2.Ie146eec4d41480ebeb15f0cfdfb3bc9095e4ebd9@changeid/
Quoted 3 lines
I almost
wonder if we shouldn't spin rather than sleep on this lock, since the window of time we're willing to wait is so short.
It doesn't feel like spinning is the right move in this particular case. While we want to enable kcompactd to move forward, it's not so urgent that it needs to take up lots of CPU cycles doing so. ...and, in fact, the cases I've seen us be blocked is when we're under memory pressure and spinning would be counterproductive to getting out of that pressure.
Quoted 6 lines
I'm certainly not
willing to NAK this patch since it's clearly fixing a real problem.Hm. If the problem is that we want to wait for the lock unless the lock is being held for I/O, we can actually tell that in the caller.
Quoted 8 lines
if (folio_test_uptodate(folio)) folio_lock(folio); else folio_trylock(folio);(the folio lock isn't held for writeback, just taken and released; if the folio is uptodate, the folio lock should only be taken for a short time; if it's !uptodate then it's probably being read)
The current place in patch #3 where I'm using folio_lock_timeout() only calls it if a folio_trylock() already failed [2]. So I guess the idea would be that if the trylock failed and folio_test_uptodate() returns 0 then we immediately fail, otherwise we call the unbounded folio_trylock()?
I put some traces in and ran my test and it turns out that in every case (except one) where the tre initial folio_trylock() failed I saw folio_test_uptodate() return 0. Assuming my test case is typical, I think that means that coding it with folio_test_uptodate() is roughly the same as just never waiting at all for the folio lock in the SYNC_LIGHT case. In the original discussion of my v1 patch people didn't like that idea. ...so I think that for now I'm going to keep it with the timeout flow.
--
After all of the discussion and continued digging into the code that I've done, I'm still of the opinion that this patch series is worthwhile and in the spirit of the SYNC_LIGHT mode of migration, but I also don't believe it's going to be a panacea for any particular case. Presumably even if kcompactd gets blocked for a second or two under a lot of memory pressure it won't be the absolute end of the world.
In that spirit, I'll plan to post v3 in a day or two, but I'll also continue to say that if someone tells me that they really hate it that I can put it on the back burner.
[2] https://lore.kernel.org/r/20230421151135.v2.3.Ia86ccac02a303154a0b8bc60567e7a95d34c96d3@changeid/
Matthew Wilcox
maintainerRe: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Quoted 7 lines · Doug Anderson
Quoted 4 lines · Matthew Wilcox
I'm not generally a fan of lock-with-timeout approaches. I think the rationale for this one makes sense, but we're going to see some people try to use this for situations where it doesn't make sense.
Although it won't completely prevent the issue, I could add a comment
People don't read comments.
Quoted 12 lines
Quoted 11 lines
Hm. If the problem is that we want to wait for the lock unless the lock is being held for I/O, we can actually tell that in the caller.
if (folio_test_uptodate(folio)) folio_lock(folio); else folio_trylock(folio);(the folio lock isn't held for writeback, just taken and released; if the folio is uptodate, the folio lock should only be taken for a short time; if it's !uptodate then it's probably being read)
Quoted 5 lines
The current place in patch #3 where I'm using
folio_lock_timeout()only calls it if afolio_trylock()already failed [2]. So I guess the idea would be that if the trylock failed andfolio_test_uptodate()returns 0 then we immediately fail, otherwise we call the unboundedfolio_trylock()?
Looking at the actual code, here's what I'd do:
+++ b/mm/migrate.c
@@ -1156,6 +1156,14 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page
if (current->flags & PF_MEMALLOC)
goto out;+ /*
+ * In "light" mode, we can wait for transient locks (eg
+ * inserting a page into the page table), but it's not
+ * worth waiting for I/O.
+ */
+ if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(folio))
+ goto out;
+
folio_lock(src);
}
locked = true;Quoted 8 lines
I put some traces in and ran my test and it turns out that in every case (except one) where the tre initial
folio_trylock()failed I sawfolio_test_uptodate()return 0. Assuming my test case is typical, I think that means that coding it withfolio_test_uptodate()is roughly the same as just never waiting at all for the folio lock in theSYNC_LIGHTcase. In the original discussion of my v1 patch people didn't like that idea. ...so I think that for now I'm going to keep it with the timeout flow.
I think that means that your specific test is generally going to exercise the case where the lock is held because we're waiting for I/O. That's exactly what you set it up to produce, after all! But it won't affect the cases where the folio lock is being held for other reasons, which your testcase is incredibly unlikely to produce.
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Hi,
Quoted 11 lines · Matthew Wilcox
Quoted 7 lines · Doug Anderson
Quoted 4 lines · Matthew Wilcox
I'm not generally a fan of lock-with-timeout approaches. I think the rationale for this one makes sense, but we're going to see some people try to use this for situations where it doesn't make sense.
Although it won't completely prevent the issue, I could add a comment
People don't read comments.
Agreed, it's just better than nothing...
Quoted 46 lines
Quoted 12 lines
Quoted 11 lines
Hm. If the problem is that we want to wait for the lock unless the lock is being held for I/O, we can actually tell that in the caller.
if (folio_test_uptodate(folio)) folio_lock(folio); else folio_trylock(folio);(the folio lock isn't held for writeback, just taken and released; if the folio is uptodate, the folio lock should only be taken for a short time; if it's !uptodate then it's probably being read)
Quoted 5 lines
The current place in patch #3 where I'm using
folio_lock_timeout()only calls it if afolio_trylock()already failed [2]. So I guess the idea would be that if the trylock failed andfolio_test_uptodate()returns 0 then we immediately fail, otherwise we call the unboundedfolio_trylock()?Looking at the actual code, here's what I'd do:
+++
b/mm/migrate.c(quoted diff) +8
-0@@ -1156,6 +1156,14 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page if (current->flags & PF_MEMALLOC) goto out; + /* + * In "light" mode, we can wait for transient locks (eg + * inserting a page into the page table), but it's not + * worth waiting for I/O. + */ + if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(folio)) + goto out; + folio_lock(src); } locked = true;Quoted 9 lines
I put some traces in and ran my test and it turns out that in every case (except one) where the tre initial
folio_trylock()failed I sawfolio_test_uptodate()return 0. Assuming my test case is typical, I think that means that coding it withfolio_test_uptodate()is roughly the same as just never waiting at all for the folio lock in theSYNC_LIGHTcase. In the original discussion of my v1 patch people didn't like that idea. ...so I think that for now I'm going to keep it with the timeout flow.
Quoted 5 lines
I think that means that your specific test is generally going to exercise the case where the lock is held because we're waiting for I/O. That's exactly what you set it up to produce, after all! But it won't affect the cases where the folio lock is being held for other reasons, which your testcase is incredibly unlikely to produce.
Sure, I'm happy to do it like you say. Do you have any suggestions for the similar lock_buffer() case, or are you OK w/ the timeout there?
Mel: do you have any comments? In your previous response [1] you seemed to indicate that you thought that short waits for read were a good idea.
[1] https://lore.kernel.org/r/[email protected]
-Doug
Matthew Wilcox
maintainerRe: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Quoted 2 lines · Doug Anderson
Sure, I'm happy to do it like you say. Do you have any suggestions for the similar
lock_buffer()case, or are you OK w/ the timeout there?
I'd do that similarly, ie:
+++ b/mm/migrate.c
@@ -691,38 +691,30 @@ EXPORT_SYMBOL(migrate_folio);
static bool buffer_migrate_lock_buffers(struct buffer_head *head,
enum migrate_mode mode)
{
- struct buffer_head *bh = head;
+ struct buffer_head *failed_bh, *bh = head;
- /* Simple case, sync compaction */
- if (mode != MIGRATE_ASYNC) {
- do {
- lock_buffer(bh);
- bh = bh->b_this_page;
-
- } while (bh != head);
-
- return true;
- }
-
- /* async case, we cannot block on lock_buffer so use trylock_buffer */
do {
if (!trylock_buffer(bh)) {
- /*
- * We failed to lock the buffer and cannot stall in
- * async migration. Release the taken locks
- */
- struct buffer_head *failed_bh = bh;
- bh = head;
- while (bh != failed_bh) {
- unlock_buffer(bh);
- bh = bh->b_this_page;
- }
- return false;
+ if (mode == MIGRATE_ASYNC)
+ goto unlock;
+ if (mode == MIGRATE_SYNC_LIGHT && !buffer_uptodate(bh))
+ goto unlock;
+ lock_buffer(bh);
}
bh = bh->b_this_page;
} while (bh != head);
return true;
+
+unlock:
+ /* Failed to lock the buffer and cannot stall */
+ failed_bh = bh;
+ bh = head;
+ while (bh != failed_bh) {+ unlock_buffer(bh);
+ bh = bh->b_this_page;
+ }
+ return false;
}
static int __buffer_migrate_folio(struct address_space *mapping,
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
In reply to Doug AndersonQuoted 11 lines · Doug Anderson
Hi,
Quoted 8 lines · On Wed, Apr 26, 2023 at 2:27???PM Matthew Wilcox <[email protected]> wrote:
Quoted 6 lines · Doug Anderson
Quoted 4 lines · On Wed, Apr 26, 2023 at 8:14???AM Matthew Wilcox <[email protected]> wrote:
I'm not generally a fan of lock-with-timeout approaches. I think the rationale for this one makes sense, but we're going to see some people try to use this for situations where it doesn't make sense.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Although it won't completely prevent the issue, I could add a comment
People don't read comments.
Agreed, it's just better than nothing...
It also acts as something to point to if an ill-advised use of trylock_timeout was used.
Quoted 53 lines
Quoted 47 lines
Quoted 13 lines
Quoted 4 lines
Hm. If the problem is that we want to wait for the lock unless the lock is being held for I/O, we can actually tell that in the caller.
Quoted 8 lines
if (folio_test_uptodate(folio)) folio_lock(folio); else folio_trylock(folio);(the folio lock isn't held for writeback, just taken and released; if the folio is uptodate, the folio lock should only be taken for a short time; if it's !uptodate then it's probably being read)
Quoted 5 lines
The current place in patch #3 where I'm using
folio_lock_timeout()only calls it if afolio_trylock()already failed [2]. So I guess the idea would be that if the trylock failed andfolio_test_uptodate()returns 0 then we immediately fail, otherwise we call the unboundedfolio_trylock()?Looking at the actual code, here's what I'd do:
+++
b/mm/migrate.c(quoted diff) +8
-0@@ -1156,6 +1156,14 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page if (current->flags & PF_MEMALLOC) goto out; + /* + * In "light" mode, we can wait for transient locks (eg + * inserting a page into the page table), but it's not + * worth waiting for I/O. + */ + if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(folio)) + goto out; + folio_lock(src); } locked = true;Quoted 9 lines
I put some traces in and ran my test and it turns out that in every case (except one) where the tre initial
folio_trylock()failed I sawfolio_test_uptodate()return 0. Assuming my test case is typical, I think that means that coding it withfolio_test_uptodate()is roughly the same as just never waiting at all for the folio lock in theSYNC_LIGHTcase. In the original discussion of my v1 patch people didn't like that idea. ...so I think that for now I'm going to keep it with the timeout flow.Quoted 5 lines
I think that means that your specific test is generally going to exercise the case where the lock is held because we're waiting for I/O. That's exactly what you set it up to produce, after all! But it won't affect the cases where the folio lock is being held for other reasons, which your testcase is incredibly unlikely to produce.
Quoted 7 lines
Sure, I'm happy to do it like you say. Do you have any suggestions for the similar
lock_buffer()case, or are you OK w/ the timeout there?Mel: do you have any comments? In your previous response [1] you seemed to indicate that you thought that short waits for read were a good idea.
I have no objection and it's worth trying the patch against your test case although note that short-lived locks may still be skipped (but that's ok). Either kcompactd still waits for minutes on folio lock or it does not. The potential downside is that kcompactd skips candidates that it shouldn't have which will manifest as a drop in compaction efficiency that will be difficult to detect. With Johannes' compaction series in the mix though, it's more likely we'll notice a drop in efficiency in the short term.
I prefer the uptodate check a lot more than "spin rather than sleep on this lock". Spinning is surprisingly difficult to get right (see all the changes and subsequent fixes that went into mutexes or the changes in "polling on the way to idle" logic in cpuidle), it also occupies CPU time when a system is already under stress and it's not a great fit for this specific case. The timeout is currently specified in ticks and ticking for 1-4ms on common kernel configs is a *long* time from a scheduler perspective even if it's a non-issue for MM which sometimes deals with time frames suitable for slow storage. It's even worse if there is no preemption point and even if there was, it hits potential hazards with CFS dealing with the fairness of a useless spinning task vs tasks trying to do real work. Of course the spin time could be based on cond_resched or a timeout in nanoseconds but cond_resched is a random timeout and picking a specific timeout is difficult and will attract negative attention eventually. My MM hat says "ah, it'll be fine" and my scheduler hat says "uhhh, don't do that".
Matthew's suggestion on checking uptodate is a good one. If he's right, the problem goes away. If the problem still persists, I am willing to bet that it's mitigated a lot *and* it becomes interesting to find out why a !uptodate page could take a very long time to lock because it's possibly revealing another real bug. If the idea does not work at all, the timeout patch still exists, add to the changelog why checking uptodate doesn't work and Linus spelled out clearly why it should be ok in this specific case even if we have to watch out for bad users of the new interface.
Signature · 2 lines
Mel Gorman SUSE Labs
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
On 27 Apr 2023 10:48:47 +0100 Mel Gorman <[email protected]>
Quoted 9 lines
Matthew's suggestion on checking uptodate is a good one. If he's right, the problem goes away. If the problem still persists, I am willing to bet that it's mitigated a lot *and* it becomes interesting to find out why a !uptodate page could take a very long time to lock because it's possibly revealing another real bug. If the idea does not work at all, the timeout patch still exists, add to the changelog why checking uptodate doesn't work and Linus spelled out clearly why it should be ok in this specific case even if we have to watch out for bad users of the new interface.
Timeout detects slow IO in this case, while hardware vonders provide IO speed info for free, so SYNC_LIGHT likely breaks with slow hardware after adding timeout.
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
In reply to Mel GormanQuoted 4 lines · Mel Gorman
The reason I don't consider this patch a NAK candidate is that this is not conditional locking as such because no special action is taken if the lock cannot be acquired.
If this comes from my rant against not having conditional locking for the O_NDELAY (well, the io_uring equivalent) IO path, then no, I don't think something like lock_page_timout() is "conditional locking".
Some caller wanting to get the lock, but being willing to just go on and do something else after a timeout is fine. Within the context of something like memory compaction internally for the kernel that is fundamentally opportunistic anyway, that sounds fine to me.
In fact, in contexts like that, even trylock is fine. We obviously have trylock in lots of places of the kernel.
My "no conditional locking" is really that I do not think it's sane to have user IO fail with EAGAIN just because some data structure happened to be busy. It's a debugging nightmare with unlikely things that happen only in special conditions. Doing IO is not some "opportunistic" thing.
We've actually had things like that before where people tried to make O_NDELAY mean "no locking" (I think that was driven at least partly by the old in-kernel web server patches), and it also causes actual problems with user space then busy-looping in a select() loop, because the select doesn't consider some low-level lock to be a waiting event.
(The io_uring case is _slightly_ different from our historical issues in this area, in that the kernel can fall back to the user worker thread case, but it's all mixed up in that same IO path and that's why I absolutely hated that "if (X) trylock else proper_lock" approach).
So a unconditional "lock with timeout" in the context of "we can just skip this if it times out" is perfectly fine by me.
That said - the kcompactd code is not code I know, so maybe there are *other* issues with that patch, so this is also not an ACK from me.
So please consider this just a "that is a very different case from the one I complained about".
Linus
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
In reply to Douglas AndersonQuoted 26 lines · Douglas Anderson
Add a variant of
folio_lock()that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.Signed-off-by Douglas Anderson <[email protected]>---
Changes in v2:
- "Addfolio_lock_timeout()" new for v2.include/linux/pagemap.h | 16 ++++++++++++++ mm/filemap.c | 47 +++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 13 deletions(-)include/linux/pagemap.h +1
-0@@ -892,6 +892,7 @@ static inline bool wake_page_match(struct wait_page_queue *wait_page, }void __folio_lock (struct folio *folio); +int __folio_lock_timeout (struct folio *folio,long timeout);int __folio_lock_killable (struct folio *folio);bool __folio_lock_or_retry (struct folio *folio,struct mm_struct *mm,unsigned int flags);
include/linux/pagemap.h static inline void folio_lock(struct folio *folio) · line 961
Quoted 9 lines
include/linux/pagemap.h +6
-0@@ -952,6 +953,21 @@ static inline void folio_lock(struct folio *folio)__folio_lock (folio); } +/** +* folio_lock_timeout() - Lock this folio, with a timeout. +* @folio: The folio to lock. +* @timeout: The timeout in jiffies; %MAX_SCHEDULE_TIMEOUT means wait forever. +* +* Return: 0 upon success; -ETIMEDOUT upon failure.
IIUC, the funtion may return -EINTR too.
Otherwise looks good to me. Thanks!
Best Regards,
Huang, Ying
Quoted 144 lines
include/linux/pagemap.h +9
-0+*/ +static inline int folio_lock_timeout (struct folio *folio,long timeout) +{ +might_sleep (); +if (!folio_trylock (folio)) +return __folio_lock_timeout (folio, timeout); +return 0 ; +} +/** * lock_page() - Lock the folio containing this page. * @page: The page to lock. mm/filemap.c +34
-13@@ -1220,7 +1220,7 @@ static inline bool folio_trylock_flag(struct folio *folio, int bit_nr,int sysctl_page_lock_unfairness =5 ;static inline int folio_wait_bit_common (struct folio *folio,int bit_nr, -int state,enum behavior behavior) +int state,enum behavior behavior,long timeout) {wait_queue_head_t *q =folio_waitqueue (folio);int unfairness = sysctl_page_lock_unfairness;@@ -1229,6 +1229,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,bool thrashing = false;unsigned long pflags;bool in_thrashing; +int err;if (bit_nr == PG_locked && !folio_test_uptodate (folio) &&folio_test_workingset (folio)) {@@ -1295,10 +1296,13 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,/* Loop until we've been woken or interrupted */ flags =smp_load_acquire (&wait->flags);if (!(flags &WQ_FLAG_WOKEN )) { +if (!timeout) +break ; +if (signal_pending_state (state, current))break ; -io_schedule (); + timeout =io_schedule_timeout (timeout);continue ; }@@ -1324,10 +1328,10 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr, }/* -* If a signal happened, this 'finish_wait()' may remove the last -* waiter from the wait-queues, but the folio waiters bit will remain -* set. That's ok. The next wakeup will take care of it, and trying -* to do it here would be difficult and prone to races. +* If a signal/timeout happened, this 'finish_wait()' may remove the +* last waiter from the wait-queues, but the folio waiters bit will +* remain set. That's ok. The next wakeup will take care of it, and +* trying to do it here would be difficult and prone to races. */ finish_wait (q, wait);@@ -1336,6 +1340,13 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,psi_memstall_leave (&pflags); } +/* +* If we don't meet the success criteria below then we've got an error +* of some sort. Differentiate between the two error cases. If there's +* no time left it must have been a timeout. +*/ + err = !timeout ? -ETIMEDOUT : -EINTR ; +/* * NOTE! The wait->flags weren't stable until we've done the * 'finish_wait()', and we could have exited the loop above due @@ -1350,9 +1361,9 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,* waiter, but an exclusive one requires WQ_FLAG_DONE. */ if (behavior ==EXCLUSIVE ) -return wait->flags &WQ_FLAG_DONE ?0 : -EINTR ; +return wait->flags &WQ_FLAG_DONE ?0 : err; -return wait->flags &WQ_FLAG_WOKEN ?0 : -EINTR ; +return wait->flags &WQ_FLAG_WOKEN ?0 : err; }#ifdef CONFIG_MIGRATION @@ -1442,13 +1453,15 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,void folio_wait_bit (struct folio *folio,int bit_nr) { -folio_wait_bit_common (folio, bit_nr,TASK_UNINTERRUPTIBLE ,SHARED ); +folio_wait_bit_common (folio, bit_nr,TASK_UNINTERRUPTIBLE ,SHARED , +MAX_SCHEDULE_TIMEOUT ); }EXPORT_SYMBOL (folio_wait_bit);int folio_wait_bit_killable (struct folio *folio,int bit_nr) { -return folio_wait_bit_common (folio, bit_nr,TASK_KILLABLE ,SHARED ); +return folio_wait_bit_common (folio, bit_nr,TASK_KILLABLE ,SHARED , +MAX_SCHEDULE_TIMEOUT ); }EXPORT_SYMBOL (folio_wait_bit_killable);@@ -1467,7 +1480,8 @@ EXPORT_SYMBOL(folio_wait_bit_killable);*/ static int folio_put_wait_locked (struct folio *folio,int state) { -return folio_wait_bit_common (folio, PG_locked, state,DROP ); +return folio_wait_bit_common (folio, PG_locked, state,DROP , +MAX_SCHEDULE_TIMEOUT ); }/** @@ -1662,17 +1676,24 @@ EXPORT_SYMBOL_GPL(page_endio);void __folio_lock (struct folio *folio) {folio_wait_bit_common (folio, PG_locked,TASK_UNINTERRUPTIBLE , -EXCLUSIVE ); +EXCLUSIVE ,MAX_SCHEDULE_TIMEOUT ); }EXPORT_SYMBOL (__folio_lock);int __folio_lock_killable (struct folio *folio) {return folio_wait_bit_common (folio, PG_locked,TASK_KILLABLE , -EXCLUSIVE ); +EXCLUSIVE ,MAX_SCHEDULE_TIMEOUT ); }EXPORT_SYMBOL_GPL (__folio_lock_killable); +int __folio_lock_timeout (struct folio *folio,long timeout) +{ +return folio_wait_bit_common (folio, PG_locked,TASK_KILLABLE , +EXCLUSIVE , timeout); +} +EXPORT_SYMBOL_GPL (__folio_lock_timeout); +static int __folio_lock_async (struct folio *folio,struct wait_page_queue *wait) {struct wait_queue_head *q =folio_waitqueue (folio);
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
In reply to Douglas AndersonQuoted 26 lines · Douglas Anderson
Add a variant of
folio_lock()that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.Signed-off-by Douglas Anderson <[email protected]>---
Changes in v2:
- "Addfolio_lock_timeout()" new for v2.include/linux/pagemap.h | 16 ++++++++++++++ mm/filemap.c | 47 +++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 13 deletions(-)include/linux/pagemap.h +1
-0@@ -892,6 +892,7 @@ static inline bool wake_page_match(struct wait_page_queue *wait_page, }void __folio_lock (struct folio *folio); +int __folio_lock_timeout (struct folio *folio,long timeout);int __folio_lock_killable (struct folio *folio);bool __folio_lock_or_retry (struct folio *folio,struct mm_struct *mm,unsigned int flags);
include/linux/pagemap.h static inline void folio_lock(struct folio *folio) · line 962
Quoted 10 lines
include/linux/pagemap.h +7
-0@@ -952,6 +953,21 @@ static inline void folio_lock(struct folio *folio)__folio_lock (folio); } +/** +* folio_lock_timeout() - Lock this folio, with a timeout. +* @folio: The folio to lock. +* @timeout: The timeout in jiffies; %MAX_SCHEDULE_TIMEOUT means wait forever. +* +* Return: 0 upon success; -ETIMEDOUT upon failure. +*/
May return -EINTR?
Quoted 33 lines
include/linux/pagemap.h +8
-0+static inline int folio_lock_timeout (struct folio *folio,long timeout) +{ +might_sleep (); +if (!folio_trylock (folio)) +return __folio_lock_timeout (folio, timeout); +return 0 ; +} +/** * lock_page() - Lock the folio containing this page. * @page: The page to lock. mm/filemap.c +2
-1@@ -1220,7 +1220,7 @@ static inline bool folio_trylock_flag(struct folio *folio, int bit_nr,int sysctl_page_lock_unfairness =5 ;static inline int folio_wait_bit_common (struct folio *folio,int bit_nr, -int state,enum behavior behavior) +int state,enum behavior behavior,long timeout) {wait_queue_head_t *q =folio_waitqueue (folio);int unfairness = sysctl_page_lock_unfairness;@@ -1229,6 +1229,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,bool thrashing = false;unsigned long pflags;bool in_thrashing; +int err;if (bit_nr == PG_locked && !folio_test_uptodate (folio) &&folio_test_workingset (folio)) {
mm/filemap.c static inline int folio_wait_bit_common(struct folio *folio, int bit_nr, · line 1301
Quoted 6 lines
mm/filemap.c +3
-0@@ -1295,10 +1296,13 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,/* Loop until we've been woken or interrupted */ flags =smp_load_acquire (&wait->flags);if (!(flags &WQ_FLAG_WOKEN )) { +if (!timeout) +break ; +
An io_schedule_timeout of 0 is valid so why the special handling? It's negative timeouts that cause schedule_timeout() to complain.
Quoted 107 lines
mm/filemap.c +29
-12if (signal_pending_state (state, current))break ; -io_schedule (); + timeout =io_schedule_timeout (timeout);continue ; }@@ -1324,10 +1328,10 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr, }/* -* If a signal happened, this 'finish_wait()' may remove the last -* waiter from the wait-queues, but the folio waiters bit will remain -* set. That's ok. The next wakeup will take care of it, and trying -* to do it here would be difficult and prone to races. +* If a signal/timeout happened, this 'finish_wait()' may remove the +* last waiter from the wait-queues, but the folio waiters bit will +* remain set. That's ok. The next wakeup will take care of it, and +* trying to do it here would be difficult and prone to races. */ finish_wait (q, wait);@@ -1336,6 +1340,13 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,psi_memstall_leave (&pflags); } +/* +* If we don't meet the success criteria below then we've got an error +* of some sort. Differentiate between the two error cases. If there's +* no time left it must have been a timeout. +*/ + err = !timeout ? -ETIMEDOUT : -EINTR ; +/* * NOTE! The wait->flags weren't stable until we've done the * 'finish_wait()', and we could have exited the loop above due @@ -1350,9 +1361,9 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,* waiter, but an exclusive one requires WQ_FLAG_DONE. */ if (behavior ==EXCLUSIVE ) -return wait->flags &WQ_FLAG_DONE ?0 : -EINTR ; +return wait->flags &WQ_FLAG_DONE ?0 : err; -return wait->flags &WQ_FLAG_WOKEN ?0 : -EINTR ; +return wait->flags &WQ_FLAG_WOKEN ?0 : err; }#ifdef CONFIG_MIGRATION @@ -1442,13 +1453,15 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,void folio_wait_bit (struct folio *folio,int bit_nr) { -folio_wait_bit_common (folio, bit_nr,TASK_UNINTERRUPTIBLE ,SHARED ); +folio_wait_bit_common (folio, bit_nr,TASK_UNINTERRUPTIBLE ,SHARED , +MAX_SCHEDULE_TIMEOUT ); }EXPORT_SYMBOL (folio_wait_bit);int folio_wait_bit_killable (struct folio *folio,int bit_nr) { -return folio_wait_bit_common (folio, bit_nr,TASK_KILLABLE ,SHARED ); +return folio_wait_bit_common (folio, bit_nr,TASK_KILLABLE ,SHARED , +MAX_SCHEDULE_TIMEOUT ); }EXPORT_SYMBOL (folio_wait_bit_killable);@@ -1467,7 +1480,8 @@ EXPORT_SYMBOL(folio_wait_bit_killable);*/ static int folio_put_wait_locked (struct folio *folio,int state) { -return folio_wait_bit_common (folio, PG_locked, state,DROP ); +return folio_wait_bit_common (folio, PG_locked, state,DROP , +MAX_SCHEDULE_TIMEOUT ); }/** @@ -1662,17 +1676,24 @@ EXPORT_SYMBOL_GPL(page_endio);void __folio_lock (struct folio *folio) {folio_wait_bit_common (folio, PG_locked,TASK_UNINTERRUPTIBLE , -EXCLUSIVE ); +EXCLUSIVE ,MAX_SCHEDULE_TIMEOUT ); }EXPORT_SYMBOL (__folio_lock);int __folio_lock_killable (struct folio *folio) {return folio_wait_bit_common (folio, PG_locked,TASK_KILLABLE , -EXCLUSIVE ); +EXCLUSIVE ,MAX_SCHEDULE_TIMEOUT ); }EXPORT_SYMBOL_GPL (__folio_lock_killable); +int __folio_lock_timeout (struct folio *folio,long timeout) +{ +return folio_wait_bit_common (folio, PG_locked,TASK_KILLABLE , +EXCLUSIVE , timeout); +} +EXPORT_SYMBOL_GPL (__folio_lock_timeout); +static int __folio_lock_async (struct folio *folio,struct wait_page_queue *wait) {struct wait_queue_head *q =folio_waitqueue (folio);Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
Signature · 2 lines
Mel Gorman SUSE Labs
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Hi,
Quoted 11 lines · Mel Gorman
mm/filemap.c static inline int folio_wait_bit_common(struct folio *folio, int bit_nr, · line 1301
Quoted 8 lines
mm/filemap.c +3
-0@@ -1295,10 +1296,13 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,/* Loop until we've been woken or interrupted */ flags =smp_load_acquire (&wait->flags);if (!(flags &WQ_FLAG_WOKEN )) { +if (!timeout) +break ; +An
io_schedule_timeoutof 0 is valid so why the special handling? It's negative timeouts that causeschedule_timeout()to complain.
It's not expected that the caller passes in a timeout of 0 here. The test here actually handles the case that the previous call to io_schedule_timeout() returned 0. In my patch, after the call to io_schedule_timeout() we unconditionally "continue" and end up back at the top of the loop. The next time through the loop if we don't see the WOKEN flag then we'll check for the two "error" conditions (timeout or signal pending) and break for either of them.
To make it clearer, I'll add this comment for the next version:
/* Break if the last io_schedule_timeout() said no time left */
Re: [PATCH v2 1/4] mm/filemap: Add folio_lock_timeout()
Quoted 15 lines · Doug Anderson
Hi,
Quoted 11 lines · On Mon, Apr 24, 2023 at 1:22???AM Mel Gorman <[email protected]>…
mm/filemap.c static inline int folio_wait_bit_common(struct folio *folio, int bit_nr, · line 1301
Quoted 8 lines
mm/filemap.c +3
-0@@ -1295,10 +1296,13 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,/* Loop until we've been woken or interrupted */ flags =smp_load_acquire (&wait->flags);if (!(flags &WQ_FLAG_WOKEN )) { +if (!timeout) +break ; +An
io_schedule_timeoutof 0 is valid so why the special handling? It's negative timeouts that causeschedule_timeout()to complain.
Quoted 7 lines
It's not expected that the caller passes in a timeout of 0 here. The test here actually handles the case that the previous call to
io_schedule_timeout()returned 0. In my patch, after the call toio_schedule_timeout()we unconditionally "continue" and end up back at the top of the loop. The next time through the loop if we don't see the WOKEN flag then we'll check for the two "error" conditions (timeout or signal pending) and break for either of them.
Ah, I see!
Quoted 4 lines
To make it clearer, I'll add this comment for the next version:
/* Break if the last
io_schedule_timeout()said no time left */
Yes please.
Signature · 2 lines
Mel Gorman SUSE Labs
PATCH v2 2/4 buffer: Add lock_buffer_timeout()
In reply to Douglas AndersonAdd a variant of lock_buffer() that can timeout. This is useful to avoid unbounded waits for the page lock in kcompactd.
---
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(+)
fs/buffer.c +7 -0
@@ -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);
include/linux/buffer_head.h +10 -0
@@ -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 ; +} +static inline struct buffer_head *getblk_unmovable (struct block_device *bdev,sector_t block,unsigned size)
include/linux/wait_bit.h +24 -0
@@ -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
kernel/sched/wait_bit.c +14 -0
@@ -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);
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
Re: [PATCH v2 2/4] buffer: Add lock_buffer_timeout()
Quoted 48 lines · Douglas Anderson
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 <[email protected]>---
Changes in v2:
- "Addlock_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(+)fs/buffer.c +7
-0@@ -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);include/linux/buffer_head.h +1
-0@@ -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); }
include/linux/buffer_head.h static inline void lock_buffer(struct buffer_head *bh) · line 412
Quoted 10 lines
include/linux/buffer_head.h +9
-0+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
Quoted 70 lines
include/linux/buffer_head.h +0
-0static inline struct buffer_head *getblk_unmovable (struct block_device *bdev,sector_t block,unsigned size)include/linux/wait_bit.h +24
-0@@ -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 kernel/sched/wait_bit.c +14
-0@@ -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);
PATCH v2 3/4 migrate_pages: Don't wait forever locking pages in MIGRATE_SYNC_LIGHT
In reply to Douglas AndersonThe MIGRATE_SYNC_LIGHT mode is intended to block for things that will finish quickly but not for things that will take a long time. Exactly how long is too long is not well defined, but waits of tens of milliseconds is likely non-ideal.
Waiting on the folio lock in isolate_movable_page() is something that usually is pretty quick, but is not officially bounded. Nothing stops another process from holding a folio lock while doing an expensive operation. Having an unbounded wait like this is not within the design goals of MIGRATE_SYNC_LIGHT.
When putting a Chromebook under memory pressure (opening over 90 tabs on a 4GB machine) it was fairly easy to see delays waiting for the lock of > 100 ms. While the laptop wasn't amazingly usable in this state, it was still limping along and this state isn't something artificial. Sometimes we simply end up with a lot of memory pressure.
Putting the same Chromebook under memory pressure while it was running Android apps (though not stressing them) showed a much worse result (NOTE: this was on a older kernel but the codepaths here are similar). Android apps on ChromeOS currently run from a 128K-block, zlib-compressed, loopback-mounted squashfs disk. If we get a page fault from something backed by the squashfs filesystem we could end up holding a folio lock while reading enough from disk to decompress 128K (and then decompressing it using the somewhat slow zlib algorithms). That reading goes through the ext4 subsystem (because it's a loopback mount) before eventually ending up in the block subsystem. This extra jaunt adds extra overhead. Without much work I could see cases where we ended up blocked on a folio lock for over a second. With more more extreme memory pressure I could see up to 25 seconds.
Let's bound the amount of time we can wait for the folio lock. The SYNC_LIGHT migration mode can already handle failure for things that are slow, so adding this timeout in is fairly straightforward.
With this timeout, it can be seen that kcompactd can move on to more productive tasks if it's taking a long time to acquire a lock.
NOTE: The reason I stated digging into this isn't because some benchmark had gone awry, but because we've received in-the-field crash reports where we have a hung task waiting on the page lock (which is the equivalent code path on old kernels). While the root cause of those crashes is likely unrelated and won't be fixed by this patch, analyzing those crash reports did point out this unbounded wait and it seemed like something good to fix.
ALSO NOTE: the timeout mechanism used here uses "jiffies" and we also will retry up to 7 times. That doesn't give us much accuracy in specifying the timeout. On 1000 Hz machines we'll end up timing out in 7-14 ms. On 100 Hz machines we'll end up in 70-140 ms. Given that we don't have a strong definition of how long "too long" is, this is probably OK.
---
Changes in v2:
- Keep unbounded delay in "SYNC", delay with a timeout in "SYNC_LIGHT"
mm/migrate.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
mm/migrate.c +19 -1
@@ -58,6 +58,23 @@#include "internal.h" +/* Returns the schedule timeout for a non-async mode */ +static long timeout_for_mode (enum migrate_mode mode) +{ +/* +* We'll always return 1 jiffy as the timeout. Since all places using +* this timeout are in a retry loop this means that the maximum time +* we might block is actually NR_MAX_MIGRATE_SYNC_RETRY jiffies. +* If a jiffy is 1 ms that's 7 ms, though with the accuracy of the +* timeouts it often ends up more like 14 ms; if a jiffy is 10 ms +* that's 70-140 ms. +*/ +if (mode ==MIGRATE_SYNC_LIGHT ) +return 1 ; + +return MAX_SCHEDULE_TIMEOUT ; +} +bool isolate_movable_page (struct page *page,isolate_mode_t mode) {struct folio *folio =folio_get_nontail_page (page);
@@ -1162,7 +1179,8 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_pageif (current->flags &PF_MEMALLOC ) goto out; -folio_lock (src); +if (folio_lock_timeout (src,timeout_for_mode (mode))) + goto out; } locked = true;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
Re: [PATCH v2 3/4] migrate_pages: Don't wait forever locking pages in MIGRATE_SYNC_LIGHT
Quoted 31 lines · Douglas Anderson
The
MIGRATE_SYNC_LIGHTmode is intended to block for things that will finish quickly but not for things that will take a long time. Exactly how long is too long is not well defined, but waits of tens of milliseconds is likely non-ideal.Waiting on the folio lock in
isolate_movable_page()is something that usually is pretty quick, but is not officially bounded. Nothing stops another process from holding a folio lock while doing an expensive operation. Having an unbounded wait like this is not within the design goals ofMIGRATE_SYNC_LIGHT.When putting a Chromebook under memory pressure (opening over 90 tabs on a 4GB machine) it was fairly easy to see delays waiting for the lock of > 100 ms. While the laptop wasn't amazingly usable in this state, it was still limping along and this state isn't something artificial. Sometimes we simply end up with a lot of memory pressure.
Putting the same Chromebook under memory pressure while it was running Android apps (though not stressing them) showed a much worse result (NOTE: this was on a older kernel but the codepaths here are similar). Android apps on ChromeOS currently run from a 128K-block, zlib-compressed, loopback-mounted squashfs disk. If we get a page fault from something backed by the squashfs filesystem we could end up holding a folio lock while reading enough from disk to decompress 128K (and then decompressing it using the somewhat slow zlib algorithms). That reading goes through the ext4 subsystem (because it's a loopback mount) before eventually ending up in the block subsystem. This extra jaunt adds extra overhead. Without much work I could see cases where we ended up blocked on a folio lock for over a second. With more more extreme memory pressure I could see up to 25 seconds.
Quoted 6 lines
Let's bound the amount of time we can wait for the folio lock. The
SYNC_LIGHTmigration mode can already handle failure for things that are slow, so adding this timeout in is fairly straightforward.With this timeout, it can be seen that kcompactd can move on to more productive tasks if it's taking a long time to acquire a lock.
How long is the max wait time of folio_lock_timeout()?
Quoted 8 lines
NOTE: The reason I stated digging into this isn't because some benchmark had gone awry, but because we've received in-the-field crash reports where we have a hung task waiting on the page lock (which is the equivalent code path on old kernels). While the root cause of those crashes is likely unrelated and won't be fixed by this patch, analyzing those crash reports did point out this unbounded wait and it seemed like something good to fix.
Quoted 6 lines
ALSO NOTE: the timeout mechanism used here uses "jiffies" and we also will retry up to 7 times. That doesn't give us much accuracy in specifying the timeout. On 1000 Hz machines we'll end up timing out in 7-14 ms. On 100 Hz machines we'll end up in 70-140 ms. Given that we don't have a strong definition of how long "too long" is, this is probably OK.
You can use HZ to work with different configuration. It doesn't help much if your target is 1ms. But I think that it's possible to set it to longer than that in the future. So, some general definition looks better.
Best Regards,
Huang, Ying
Quoted 47 lines
Suggested-by Mel Gorman <[email protected]>Signed-off-by Douglas Anderson <[email protected]>---
Changes in v2:
- Keep unbounded delay in "SYNC", delay with a timeout in "SYNC_LIGHT"mm/migrate.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)mm/migrate.c +19
-1@@ -58,6 +58,23 @@#include "internal.h" +/* Returns the schedule timeout for a non-async mode */ +static long timeout_for_mode (enum migrate_mode mode) +{ +/* +* We'll always return 1 jiffy as the timeout. Since all places using +* this timeout are in a retry loop this means that the maximum time +* we might block is actually NR_MAX_MIGRATE_SYNC_RETRY jiffies. +* If a jiffy is 1 ms that's 7 ms, though with the accuracy of the +* timeouts it often ends up more like 14 ms; if a jiffy is 10 ms +* that's 70-140 ms. +*/ +if (mode ==MIGRATE_SYNC_LIGHT ) +return 1 ; + +return MAX_SCHEDULE_TIMEOUT ; +} +bool isolate_movable_page (struct page *page,isolate_mode_t mode) {struct folio *folio =folio_get_nontail_page (page);@@ -1162,7 +1179,8 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_pageif (current->flags &PF_MEMALLOC ) goto out; -folio_lock (src); +if (folio_lock_timeout (src,timeout_for_mode (mode))) + goto out; } locked = true;
Re: [PATCH v2 3/4] migrate_pages: Don't wait forever locking pages in MIGRATE_SYNC_LIGHT
In reply to Douglas AndersonQuoted 75 lines · Douglas Anderson
The
MIGRATE_SYNC_LIGHTmode is intended to block for things that will finish quickly but not for things that will take a long time. Exactly how long is too long is not well defined, but waits of tens of milliseconds is likely non-ideal.Waiting on the folio lock in
isolate_movable_page()is something that usually is pretty quick, but is not officially bounded. Nothing stops another process from holding a folio lock while doing an expensive operation. Having an unbounded wait like this is not within the design goals ofMIGRATE_SYNC_LIGHT.When putting a Chromebook under memory pressure (opening over 90 tabs on a 4GB machine) it was fairly easy to see delays waiting for the lock of > 100 ms. While the laptop wasn't amazingly usable in this state, it was still limping along and this state isn't something artificial. Sometimes we simply end up with a lot of memory pressure.
Putting the same Chromebook under memory pressure while it was running Android apps (though not stressing them) showed a much worse result (NOTE: this was on a older kernel but the codepaths here are similar). Android apps on ChromeOS currently run from a 128K-block, zlib-compressed, loopback-mounted squashfs disk. If we get a page fault from something backed by the squashfs filesystem we could end up holding a folio lock while reading enough from disk to decompress 128K (and then decompressing it using the somewhat slow zlib algorithms). That reading goes through the ext4 subsystem (because it's a loopback mount) before eventually ending up in the block subsystem. This extra jaunt adds extra overhead. Without much work I could see cases where we ended up blocked on a folio lock for over a second. With more more extreme memory pressure I could see up to 25 seconds.
Let's bound the amount of time we can wait for the folio lock. The
SYNC_LIGHTmigration mode can already handle failure for things that are slow, so adding this timeout in is fairly straightforward.With this timeout, it can be seen that kcompactd can move on to more productive tasks if it's taking a long time to acquire a lock.
NOTE: The reason I stated digging into this isn't because some benchmark had gone awry, but because we've received in-the-field crash reports where we have a hung task waiting on the page lock (which is the equivalent code path on old kernels). While the root cause of those crashes is likely unrelated and won't be fixed by this patch, analyzing those crash reports did point out this unbounded wait and it seemed like something good to fix.
ALSO NOTE: the timeout mechanism used here uses "jiffies" and we also will retry up to 7 times. That doesn't give us much accuracy in specifying the timeout. On 1000 Hz machines we'll end up timing out in 7-14 ms. On 100 Hz machines we'll end up in 70-140 ms. Given that we don't have a strong definition of how long "too long" is, this is probably OK.
Suggested-by Mel Gorman <[email protected]>Signed-off-by Douglas Anderson <[email protected]>---
Changes in v2:
- Keep unbounded delay in "SYNC", delay with a timeout in "SYNC_LIGHT"mm/migrate.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)mm/migrate.c +4
-0@@ -58,6 +58,23 @@#include "internal.h" +/* Returns the schedule timeout for a non-async mode */ +static long timeout_for_mode (enum migrate_mode mode) +{ +/*
mm/migrate.c line 74
Quoted 10 lines
mm/migrate.c +10
-0+* We'll always return 1 jiffy as the timeout. Since all places using +* this timeout are in a retry loop this means that the maximum time +* we might block is actually NR_MAX_MIGRATE_SYNC_RETRY jiffies. +* If a jiffy is 1 ms that's 7 ms, though with the accuracy of the +* timeouts it often ends up more like 14 ms; if a jiffy is 10 ms +* that's 70-140 ms. +*/ +if (mode ==MIGRATE_SYNC_LIGHT ) +return 1 ; +
Use switch and WARN_ON_ONCE if MIGRATE_ASYNC with a fallthrough to MIGRATE_SYNC_LIGHT?
mm/migrate.c line 77
Quoted 3 lines
mm/migrate.c +3
-0+return MAX_SCHEDULE_TIMEOUT ; +} +
Even though HZ is defined at compile time, it is underdesirable to use a constant timeout unrelated to HZ because it's normal case is variable depending on CONFIG_HZ. Please use a value like DIV_ROUND_UP(HZ/250) or DIV_ROUND_UP(HZ/1000) for a 4ms or 1ms timeout respectively. Even though it's still potentially variable, it would make any hypothetical transition to [milli|micro|nano]seconds easier in the future as the intent would be known. While there are no plans for change as such, working in jiffies is occasionally problematic in kernel/sched/. At OSPM this year, the notion of dynamic HZ was brought up (it would be hard) and a preliminary step would be converting all uses of HZ to normal time.
Signature · 2 lines
Mel Gorman SUSE Labs
PATCH v2 4/4 migrate_pages: Don't wait forever locking buffers in MIGRATE_SYNC_LIGHT
In reply to Douglas AndersonJust as talked about in the patch ("migrate_pages: Don't wait forever locking pages in MIGRATE_SYNC_LIGHT"), we don't really want unbounded waits when we're running in MIGRATE_SYNC_LIGHT mode. Waiting on the buffer lock is a second such unbounded wait. Let's put a timeout on it.
While measurement didn't show this wait to be quite as bad as the one waiting for the folio lock, it could still be measured to be over a second in some cases.
---
Changes in v2:
- "Don't wait forever locking buffers in MIGRATE_SYNC_LIGHT" new for v2.
mm/migrate.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)
mm/migrate.c +8 -17
@@ -715,25 +715,16 @@ static bool buffer_migrate_lock_buffers(struct buffer_head *head, enum migrate_mode mode) {struct buffer_head *bh = head; +bool locked; -/* Simple case, sync compaction */ -if (mode !=MIGRATE_ASYNC ) { -do { -lock_buffer (bh); - bh = bh->b_this_page; - - }while (bh != head); - -return true; - } - -/* async case, we cannot block on lock_buffer so use trylock_buffer */ do { - if (!trylock_buffer(bh)) { - /* - * We failed to lock the buffer and cannot stall in - * async migration. Release the taken locks - */ + if (mode == MIGRATE_ASYNC) + locked = trylock_buffer(bh); + else + locked = !lock_buffer_timeout(bh, timeout_for_mode(mode)); + + if (!locked) { + /* We failed to lock the buffer. Release the taken locks. */ struct buffer_head *failed_bh = bh; bh = head; while (bh != failed_bh) {
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog