Skip to content

Commit 2718177

Browse files
ShawnZhongdkruces
authored andcommitted
libpf-tools/readahead: Fix attachment failure since v5.16 (iovisor#5086)
This PR accounts for the rename/refactor of the following functions __do_page_cache_readahead -> do_page_cache_ra -> page_cache_ra_order by torvalds/linux@8238287eadb2 and torvalds/linux@56a4d67c264e __page_cache_alloc -> filemap_alloc_folio -> filemap_alloc_folio_noprof by torvalds/linux@bb3c579e25e5 and torvalds/linux@b951aaff5035.
1 parent 6c6269b commit 2718177

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

libbpf-tools/readahead.bpf.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ int BPF_PROG(do_page_cache_ra)
3434
return 0;
3535
}
3636

37-
SEC("fexit/__page_cache_alloc")
38-
int BPF_PROG(page_cache_alloc_ret, gfp_t gfp, struct page *ret)
37+
static __always_inline
38+
int alloc_done(struct page *page)
3939
{
4040
u32 pid = bpf_get_current_pid_tgid();
4141
u64 ts;
@@ -44,13 +44,33 @@ int BPF_PROG(page_cache_alloc_ret, gfp_t gfp, struct page *ret)
4444
return 0;
4545

4646
ts = bpf_ktime_get_ns();
47-
bpf_map_update_elem(&birth, &ret, &ts, 0);
47+
bpf_map_update_elem(&birth, &page, &ts, 0);
4848
__sync_fetch_and_add(&hist.unused, 1);
4949
__sync_fetch_and_add(&hist.total, 1);
5050

5151
return 0;
5252
}
5353

54+
SEC("fexit/__page_cache_alloc")
55+
int BPF_PROG(page_cache_alloc_ret, gfp_t gfp, struct page *ret)
56+
{
57+
return alloc_done(ret);
58+
}
59+
60+
SEC("fexit/filemap_alloc_folio")
61+
int BPF_PROG(filemap_alloc_folio_ret, gfp_t gfp, unsigned int order,
62+
struct folio *ret)
63+
{
64+
return alloc_done(&ret->page);
65+
}
66+
67+
SEC("fexit/filemap_alloc_folio_noprof")
68+
int BPF_PROG(filemap_alloc_folio_noprof_ret, gfp_t gfp, unsigned int order,
69+
struct folio *ret)
70+
{
71+
return alloc_done(&ret->page);
72+
}
73+
5474
SEC("fexit/do_page_cache_ra")
5575
int BPF_PROG(do_page_cache_ra_ret)
5676
{

libbpf-tools/readahead.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ static int readahead__set_attach_target(struct bpf_program *prog)
8080
{
8181
int err;
8282

83+
/*
84+
* 56a4d67c264e ("mm/readahead: Switch to page_cache_ra_order") in v5.18
85+
* renamed do_page_cache_ra to page_cache_ra_order
86+
*/
87+
err = bpf_program__set_attach_target(prog, 0, "page_cache_ra_order");
88+
if (!err)
89+
return 0;
90+
91+
/*
92+
* 8238287eadb2 ("mm/readahead: make do_page_cache_ra take a readahead_control")
93+
* in v5.10 renamed __do_page_cache_readahead to do_page_cache_ra
94+
*/
8395
err = bpf_program__set_attach_target(prog, 0, "do_page_cache_ra");
8496
if (!err)
8597
return 0;
@@ -94,6 +106,33 @@ static int readahead__set_attach_target(struct bpf_program *prog)
94106
return err;
95107
}
96108

109+
static int attach_alloc_ret(struct readahead_bpf *obj)
110+
{
111+
bpf_program__set_autoload(obj->progs.page_cache_alloc_ret, false);
112+
bpf_program__set_autoload(obj->progs.filemap_alloc_folio_ret, false);
113+
bpf_program__set_autoload(obj->progs.filemap_alloc_folio_noprof_ret, false);
114+
115+
/*
116+
* b951aaff5035 ("mm: enable page allocation tagging") in v6.10
117+
* renamed filemap_alloc_folio to filemap_alloc_folio_noprof
118+
*/
119+
if (fentry_can_attach("filemap_alloc_folio_noprof", NULL))
120+
return bpf_program__set_autoload(obj->progs.filemap_alloc_folio_noprof_ret, true);
121+
122+
/*
123+
* bb3c579e25e5 ("mm/filemap: Add filemap_alloc_folio") in v5.16
124+
* changed __page_cache_alloc to be a wrapper of filemap_alloc_folio
125+
*/
126+
if (fentry_can_attach("filemap_alloc_folio", NULL))
127+
return bpf_program__set_autoload(obj->progs.filemap_alloc_folio_ret, true);
128+
129+
if (fentry_can_attach("__page_cache_alloc", NULL))
130+
return bpf_program__set_autoload(obj->progs.page_cache_alloc_ret, true);
131+
132+
fprintf(stderr, "failed to attach to alloc functions\n");
133+
return -1;
134+
}
135+
97136
int main(int argc, char **argv)
98137
{
99138
static const struct argp argp = {
@@ -117,10 +156,9 @@ int main(int argc, char **argv)
117156
return 1;
118157
}
119158

120-
/*
121-
* Starting from v5.10-rc1 (8238287), __do_page_cache_readahead has
122-
* renamed to do_page_cache_ra. So we specify the function dynamically.
123-
*/
159+
err = attach_alloc_ret(obj);
160+
if (err)
161+
goto cleanup;
124162
err = readahead__set_attach_target(obj->progs.do_page_cache_ra);
125163
if (err)
126164
goto cleanup;

0 commit comments

Comments
 (0)