Skip to content

Commit

Permalink
dept: Track the potential waits of PG_{locked,writeback}
Browse files Browse the repository at this point in the history
Currently, Dept only tracks the real waits of PG_{locked,writeback} that
actually happened having gone through __schedule() to avoid false
positives. However, it ends in limited capacity for deadlock detection,
because anyway there might be still way more potential dependencies by
the waits that have yet to happen but may happen in the future so as to
cause a deadlock.

So let Dept assume that when PG_{locked,writeback} bit gets cleared,
there might be waits on the bit to be woken up.

Even though false positives may increase with the aggressive tracking,
it's worth doing it because it's going to be useful in practice. See the
following link for instance:

   https://lore.kernel.org/lkml/1674268856-31807-1-git-send-email-byungchul.park@lge.com/

Signed-off-by: Byungchul Park <max.byungchul.park@gmail.com>
  • Loading branch information
maxbyungchulpark authored and intel-lab-lkp committed Jan 28, 2023
1 parent 292c315 commit ba934a0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 15 deletions.
3 changes: 3 additions & 0 deletions include/linux/mm_types.h
Expand Up @@ -19,6 +19,7 @@
#include <linux/workqueue.h>
#include <linux/seqlock.h>
#include <linux/percpu_counter.h>
#include <linux/dept.h>

#include <asm/mmu.h>

Expand Down Expand Up @@ -252,6 +253,8 @@ struct page {
#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
int _last_cpupid;
#endif
struct dept_ext_wgen PG_locked_wgen;
struct dept_ext_wgen PG_writeback_wgen;
} _struct_page_alignment;

/*
Expand Down
112 changes: 100 additions & 12 deletions include/linux/page-flags.h
Expand Up @@ -202,6 +202,50 @@ enum pageflags {

#ifndef __GENERATING_BOUNDS_H

#ifdef CONFIG_DEPT
#include <linux/kernel.h>
#include <linux/dept.h>

extern struct dept_map PG_locked_map;
extern struct dept_map PG_writeback_map;

/*
* Place the following annotations in its suitable point in code:
*
* Annotate dept_page_set_bit() around firstly set_bit*()
* Annotate dept_page_clear_bit() around clear_bit*()
* Annotate dept_page_wait_on_bit() around wait_on_bit*()
*/

static inline void dept_page_set_bit(struct page *p, int bit_nr)
{
if (bit_nr == PG_locked)
dept_request_event(&PG_locked_map, &p->PG_locked_wgen);
else if (bit_nr == PG_writeback)
dept_request_event(&PG_writeback_map, &p->PG_writeback_wgen);
}

static inline void dept_page_clear_bit(struct page *p, int bit_nr)
{
if (bit_nr == PG_locked)
dept_event(&PG_locked_map, 1UL, _RET_IP_, __func__, &p->PG_locked_wgen);
else if (bit_nr == PG_writeback)
dept_event(&PG_writeback_map, 1UL, _RET_IP_, __func__, &p->PG_writeback_wgen);
}

static inline void dept_page_wait_on_bit(struct page *p, int bit_nr)
{
if (bit_nr == PG_locked)
dept_wait(&PG_locked_map, 1UL, _RET_IP_, __func__, 0, -1L);
else if (bit_nr == PG_writeback)
dept_wait(&PG_writeback_map, 1UL, _RET_IP_, __func__, 0, -1L);
}
#else
#define dept_page_set_bit(p, bit_nr) do { } while (0)
#define dept_page_clear_bit(p, bit_nr) do { } while (0)
#define dept_page_wait_on_bit(p, bit_nr) do { } while (0)
#endif

#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
DECLARE_STATIC_KEY_FALSE(hugetlb_optimize_vmemmap_key);

Expand Down Expand Up @@ -383,44 +427,88 @@ static __always_inline int Page##uname(struct page *page) \
#define SETPAGEFLAG(uname, lname, policy) \
static __always_inline \
void folio_set_##lname(struct folio *folio) \
{ set_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); } \
{ \
set_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); \
dept_page_set_bit(&folio->page, PG_##lname); \
} \
static __always_inline void SetPage##uname(struct page *page) \
{ set_bit(PG_##lname, &policy(page, 1)->flags); }
{ \
set_bit(PG_##lname, &policy(page, 1)->flags); \
dept_page_set_bit(page, PG_##lname); \
}

#define CLEARPAGEFLAG(uname, lname, policy) \
static __always_inline \
void folio_clear_##lname(struct folio *folio) \
{ clear_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); } \
{ \
clear_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); \
dept_page_clear_bit(&folio->page, PG_##lname); \
} \
static __always_inline void ClearPage##uname(struct page *page) \
{ clear_bit(PG_##lname, &policy(page, 1)->flags); }
{ \
clear_bit(PG_##lname, &policy(page, 1)->flags); \
dept_page_clear_bit(page, PG_##lname); \
}

#define __SETPAGEFLAG(uname, lname, policy) \
static __always_inline \
void __folio_set_##lname(struct folio *folio) \
{ __set_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); } \
{ \
__set_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); \
dept_page_set_bit(&folio->page, PG_##lname); \
} \
static __always_inline void __SetPage##uname(struct page *page) \
{ __set_bit(PG_##lname, &policy(page, 1)->flags); }
{ \
__set_bit(PG_##lname, &policy(page, 1)->flags); \
dept_page_set_bit(page, PG_##lname); \
}

#define __CLEARPAGEFLAG(uname, lname, policy) \
static __always_inline \
void __folio_clear_##lname(struct folio *folio) \
{ __clear_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); } \
{ \
__clear_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); \
dept_page_clear_bit(&folio->page, PG_##lname); \
} \
static __always_inline void __ClearPage##uname(struct page *page) \
{ __clear_bit(PG_##lname, &policy(page, 1)->flags); }
{ \
__clear_bit(PG_##lname, &policy(page, 1)->flags); \
dept_page_clear_bit(page, PG_##lname); \
}

#define TESTSETFLAG(uname, lname, policy) \
static __always_inline \
bool folio_test_set_##lname(struct folio *folio) \
{ return test_and_set_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); } \
{ \
bool ret = test_and_set_bit(PG_##lname, folio_flags(folio, FOLIO_##policy));\
if (!ret) \
dept_page_set_bit(&folio->page, PG_##lname); \
return ret; \
} \
static __always_inline int TestSetPage##uname(struct page *page) \
{ return test_and_set_bit(PG_##lname, &policy(page, 1)->flags); }
{ \
bool ret = test_and_set_bit(PG_##lname, &policy(page, 1)->flags);\
if (!ret) \
dept_page_set_bit(page, PG_##lname); \
return ret; \
}

#define TESTCLEARFLAG(uname, lname, policy) \
static __always_inline \
bool folio_test_clear_##lname(struct folio *folio) \
{ return test_and_clear_bit(PG_##lname, folio_flags(folio, FOLIO_##policy)); } \
{ \
bool ret = test_and_clear_bit(PG_##lname, folio_flags(folio, FOLIO_##policy));\
if (ret) \
dept_page_clear_bit(&folio->page, PG_##lname); \
return ret; \
} \
static __always_inline int TestClearPage##uname(struct page *page) \
{ return test_and_clear_bit(PG_##lname, &policy(page, 1)->flags); }
{ \
bool ret = test_and_clear_bit(PG_##lname, &policy(page, 1)->flags);\
if (ret) \
dept_page_clear_bit(page, PG_##lname); \
return ret; \
}

#define PAGEFLAG(uname, lname, policy) \
TESTPAGEFLAG(uname, lname, policy) \
Expand Down
7 changes: 6 additions & 1 deletion include/linux/pagemap.h
Expand Up @@ -898,7 +898,12 @@ void folio_unlock(struct folio *folio);
*/
static inline bool folio_trylock(struct folio *folio)
{
return likely(!test_and_set_bit_lock(PG_locked, folio_flags(folio, 0)));
bool ret = !test_and_set_bit_lock(PG_locked, folio_flags(folio, 0));

if (ret)
dept_page_set_bit(&folio->page, PG_locked);

return likely(ret);
}

/*
Expand Down
9 changes: 7 additions & 2 deletions mm/filemap.c
Expand Up @@ -1097,6 +1097,7 @@ static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync,
if (flags & WQ_FLAG_CUSTOM) {
if (test_and_set_bit(key->bit_nr, &key->folio->flags))
return -1;
dept_page_set_bit(&key->folio->page, key->bit_nr);
flags |= WQ_FLAG_DONE;
}
}
Expand Down Expand Up @@ -1206,6 +1207,7 @@ static inline bool folio_trylock_flag(struct folio *folio, int bit_nr,
if (wait->flags & WQ_FLAG_EXCLUSIVE) {
if (test_and_set_bit(bit_nr, &folio->flags))
return false;
dept_page_set_bit(&folio->page, bit_nr);
} else if (test_bit(bit_nr, &folio->flags))
return false;

Expand All @@ -1216,8 +1218,8 @@ static inline bool folio_trylock_flag(struct folio *folio, int bit_nr,
/* How many times do we accept lock stealing from under a waiter? */
int sysctl_page_lock_unfairness = 5;

static struct dept_map __maybe_unused PG_locked_map = DEPT_MAP_INITIALIZER(PG_locked_map, NULL);
static struct dept_map __maybe_unused PG_writeback_map = DEPT_MAP_INITIALIZER(PG_writeback_map, NULL);
struct dept_map __maybe_unused PG_locked_map = DEPT_MAP_INITIALIZER(PG_locked_map, NULL);
struct dept_map __maybe_unused PG_writeback_map = DEPT_MAP_INITIALIZER(PG_writeback_map, NULL);

static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
int state, enum behavior behavior)
Expand All @@ -1230,6 +1232,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
unsigned long pflags;
bool in_thrashing;

dept_page_wait_on_bit(&folio->page, bit_nr);
if (bit_nr == PG_locked)
sdt_might_sleep_start(&PG_locked_map);
else if (bit_nr == PG_writeback)
Expand Down Expand Up @@ -1327,6 +1330,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
wait->flags |= WQ_FLAG_DONE;
break;
}
dept_page_set_bit(&folio->page, bit_nr);

/*
* If a signal happened, this 'finish_wait()' may remove the last
Expand Down Expand Up @@ -1534,6 +1538,7 @@ void folio_unlock(struct folio *folio)
BUILD_BUG_ON(PG_waiters != 7);
BUILD_BUG_ON(PG_locked > 7);
VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
dept_page_clear_bit(&folio->page, PG_locked);
if (clear_bit_unlock_is_negative_byte(PG_locked, folio_flags(folio, 0)))
folio_wake_bit(folio, PG_locked);
}
Expand Down
3 changes: 3 additions & 0 deletions mm/page_alloc.c
Expand Up @@ -76,6 +76,7 @@
#include <linux/khugepaged.h>
#include <linux/buffer_head.h>
#include <linux/delayacct.h>
#include <linux/dept.h>
#include <asm/sections.h>
#include <asm/tlbflush.h>
#include <asm/div64.h>
Expand Down Expand Up @@ -1626,6 +1627,8 @@ static void __meminit __init_single_page(struct page *page, unsigned long pfn,
page_mapcount_reset(page);
page_cpupid_reset_last(page);
page_kasan_tag_reset(page);
dept_ext_wgen_init(&page->PG_locked_wgen);
dept_ext_wgen_init(&page->PG_writeback_wgen);

INIT_LIST_HEAD(&page->lru);
#ifdef WANT_PAGE_VIRTUAL
Expand Down

0 comments on commit ba934a0

Please sign in to comment.