Skip to content

Commit 5a5fff6

Browse files
mykyta5Alexei Starovoitov
authored andcommitted
lib/freader: support reading more than 2 folios
freader_fetch currently reads from at most two folios. When a read spans into a third folio, the overflow bytes are copied adjacent to the second folio’s data instead of being handled as a separate folio. This patch modifies fetch algorithm to support reading from many folios. Signed-off-by: Mykyta Yatsenko <[email protected]> Reviewed-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 76e4fed commit 5a5fff6

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/buildid.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,21 @@ const void *freader_fetch(struct freader *r, loff_t file_off, size_t sz)
108108
*/
109109
folio_sz = folio_size(r->folio);
110110
if (file_off + sz > r->folio_off + folio_sz) {
111-
int part_sz = r->folio_off + folio_sz - file_off;
112-
113-
/* copy the part that resides in the current folio */
114-
memcpy(r->buf, r->addr + (file_off - r->folio_off), part_sz);
115-
116-
/* fetch next folio */
117-
r->err = freader_get_folio(r, r->folio_off + folio_sz);
118-
if (r->err)
119-
return NULL;
120-
121-
/* copy the rest of requested data */
122-
memcpy(r->buf + part_sz, r->addr, sz - part_sz);
111+
u64 part_sz = r->folio_off + folio_sz - file_off, off;
112+
113+
memcpy(r->buf, r->addr + file_off - r->folio_off, part_sz);
114+
off = part_sz;
115+
116+
while (off < sz) {
117+
/* fetch next folio */
118+
r->err = freader_get_folio(r, r->folio_off + folio_sz);
119+
if (r->err)
120+
return NULL;
121+
folio_sz = folio_size(r->folio);
122+
part_sz = min_t(u64, sz - off, folio_sz);
123+
memcpy(r->buf + off, r->addr, part_sz);
124+
off += part_sz;
125+
}
123126

124127
return r->buf;
125128
}

0 commit comments

Comments
 (0)