Skip to content

Commit dd6bd0d

Browse files
Matthew Wilcoxtorvalds
authored andcommitted
swap: use bdev_read_page() / bdev_write_page()
By calling the device driver to write the page directly, we avoid allocating a BIO, which allows us to free memory without allocating memory. [[email protected]: fix used-uninitialized bug] Signed-off-by: Matthew Wilcox <[email protected]> Cc: Dave Chinner <[email protected]> Cc: Dheeraj Reddy <[email protected]> Cc: Hugh Dickins <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 47a191f commit dd6bd0d

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

mm/page_io.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,16 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
248248
return ret;
249249
}
250250

251+
static sector_t swap_page_sector(struct page *page)
252+
{
253+
return (sector_t)__page_file_index(page) << (PAGE_CACHE_SHIFT - 9);
254+
}
255+
251256
int __swap_writepage(struct page *page, struct writeback_control *wbc,
252257
void (*end_write_func)(struct bio *, int))
253258
{
254259
struct bio *bio;
255-
int ret = 0, rw = WRITE;
260+
int ret, rw = WRITE;
256261
struct swap_info_struct *sis = page_swap_info(page);
257262

258263
if (sis->flags & SWP_FILE) {
@@ -297,6 +302,13 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
297302
return ret;
298303
}
299304

305+
ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
306+
if (!ret) {
307+
count_vm_event(PSWPOUT);
308+
return 0;
309+
}
310+
311+
ret = 0;
300312
bio = get_swap_bio(GFP_NOIO, page, end_write_func);
301313
if (bio == NULL) {
302314
set_page_dirty(page);
@@ -338,6 +350,13 @@ int swap_readpage(struct page *page)
338350
return ret;
339351
}
340352

353+
ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
354+
if (!ret) {
355+
count_vm_event(PSWPIN);
356+
return 0;
357+
}
358+
359+
ret = 0;
341360
bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
342361
if (bio == NULL) {
343362
unlock_page(page);

0 commit comments

Comments
 (0)