Skip to content

Commit eebd2aa

Browse files
Christoph LameterLinus Torvalds
authored andcommitted
Pagecache zeroing: zero_user_segment, zero_user_segments and zero_user
Simplify page cache zeroing of segments of pages through 3 functions zero_user_segments(page, start1, end1, start2, end2) Zeros two segments of the page. It takes the position where to start and end the zeroing which avoids length calculations and makes code clearer. zero_user_segment(page, start, end) Same for a single segment. zero_user(page, start, length) Length variant for the case where we know the length. We remove the zero_user_page macro. Issues: 1. Its a macro. Inline functions are preferable. 2. The KM_USER0 macro is only defined for HIGHMEM. Having to treat this special case everywhere makes the code needlessly complex. The parameter for zeroing is always KM_USER0 except in one single case that we open code. Avoiding KM_USER0 makes a lot of code not having to be dealing with the special casing for HIGHMEM anymore. Dealing with kmap is only necessary for HIGHMEM configurations. In those configurations we use KM_USER0 like we do for a series of other functions defined in highmem.h. Since KM_USER0 is depends on HIGHMEM the existing zero_user_page function could not be a macro. zero_user_* functions introduced here can be be inline because that constant is not used when these functions are called. Also extract the flushing of the caches to be outside of the kmap. [akpm@linux-foundation.org: fix nfs and ntfs build] [akpm@linux-foundation.org: fix ntfs build some more] Signed-off-by: Christoph Lameter <clameter@sgi.com> Cc: Steven French <sfrench@us.ibm.com> Cc: Michael Halcrow <mhalcrow@us.ibm.com> Cc: <linux-ext4@vger.kernel.org> Cc: Steven Whitehouse <swhiteho@redhat.com> Cc: Trond Myklebust <trond.myklebust@fys.uio.no> Cc: "J. Bruce Fields" <bfields@fieldses.org> Cc: Anton Altaparmakov <aia21@cantab.net> Cc: Mark Fasheh <mark.fasheh@oracle.com> Cc: David Chinner <dgc@sgi.com> Cc: Michael Halcrow <mhalcrow@us.ibm.com> Cc: Steven French <sfrench@us.ibm.com> Cc: Steven Whitehouse <swhiteho@redhat.com> Cc: Trond Myklebust <trond.myklebust@fys.uio.no> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b98348b commit eebd2aa

22 files changed

Lines changed: 103 additions & 116 deletions

File tree

‎fs/buffer.c‎

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
17981798
start = max(from, block_start);
17991799
size = min(to, block_end) - start;
18001800

1801-
zero_user_page(page, start, size, KM_USER0);
1801+
zero_user(page, start, size);
18021802
set_buffer_uptodate(bh);
18031803
}
18041804

@@ -1861,19 +1861,10 @@ static int __block_prepare_write(struct inode *inode, struct page *page,
18611861
mark_buffer_dirty(bh);
18621862
continue;
18631863
}
1864-
if (block_end > to || block_start < from) {
1865-
void *kaddr;
1866-
1867-
kaddr = kmap_atomic(page, KM_USER0);
1868-
if (block_end > to)
1869-
memset(kaddr+to, 0,
1870-
block_end-to);
1871-
if (block_start < from)
1872-
memset(kaddr+block_start,
1873-
0, from-block_start);
1874-
flush_dcache_page(page);
1875-
kunmap_atomic(kaddr, KM_USER0);
1876-
}
1864+
if (block_end > to || block_start < from)
1865+
zero_user_segments(page,
1866+
to, block_end,
1867+
block_start, from);
18771868
continue;
18781869
}
18791870
}
@@ -2104,8 +2095,7 @@ int block_read_full_page(struct page *page, get_block_t *get_block)
21042095
SetPageError(page);
21052096
}
21062097
if (!buffer_mapped(bh)) {
2107-
zero_user_page(page, i * blocksize, blocksize,
2108-
KM_USER0);
2098+
zero_user(page, i * blocksize, blocksize);
21092099
if (!err)
21102100
set_buffer_uptodate(bh);
21112101
continue;
@@ -2218,7 +2208,7 @@ int cont_expand_zero(struct file *file, struct address_space *mapping,
22182208
&page, &fsdata);
22192209
if (err)
22202210
goto out;
2221-
zero_user_page(page, zerofrom, len, KM_USER0);
2211+
zero_user(page, zerofrom, len);
22222212
err = pagecache_write_end(file, mapping, curpos, len, len,
22232213
page, fsdata);
22242214
if (err < 0)
@@ -2245,7 +2235,7 @@ int cont_expand_zero(struct file *file, struct address_space *mapping,
22452235
&page, &fsdata);
22462236
if (err)
22472237
goto out;
2248-
zero_user_page(page, zerofrom, len, KM_USER0);
2238+
zero_user(page, zerofrom, len);
22492239
err = pagecache_write_end(file, mapping, curpos, len, len,
22502240
page, fsdata);
22512241
if (err < 0)
@@ -2422,7 +2412,6 @@ int nobh_write_begin(struct file *file, struct address_space *mapping,
24222412
unsigned block_in_page;
24232413
unsigned block_start, block_end;
24242414
sector_t block_in_file;
2425-
char *kaddr;
24262415
int nr_reads = 0;
24272416
int ret = 0;
24282417
int is_mapped_to_disk = 1;
@@ -2493,13 +2482,8 @@ int nobh_write_begin(struct file *file, struct address_space *mapping,
24932482
continue;
24942483
}
24952484
if (buffer_new(bh) || !buffer_mapped(bh)) {
2496-
kaddr = kmap_atomic(page, KM_USER0);
2497-
if (block_start < from)
2498-
memset(kaddr+block_start, 0, from-block_start);
2499-
if (block_end > to)
2500-
memset(kaddr + to, 0, block_end - to);
2501-
flush_dcache_page(page);
2502-
kunmap_atomic(kaddr, KM_USER0);
2485+
zero_user_segments(page, block_start, from,
2486+
to, block_end);
25032487
continue;
25042488
}
25052489
if (buffer_uptodate(bh))
@@ -2636,7 +2620,7 @@ int nobh_writepage(struct page *page, get_block_t *get_block,
26362620
* the page size, the remaining memory is zeroed when mapped, and
26372621
* writes to that region are not written out to the file."
26382622
*/
2639-
zero_user_page(page, offset, PAGE_CACHE_SIZE - offset, KM_USER0);
2623+
zero_user_segment(page, offset, PAGE_CACHE_SIZE);
26402624
out:
26412625
ret = mpage_writepage(page, get_block, wbc);
26422626
if (ret == -EAGAIN)
@@ -2709,7 +2693,7 @@ int nobh_truncate_page(struct address_space *mapping,
27092693
if (page_has_buffers(page))
27102694
goto has_buffers;
27112695
}
2712-
zero_user_page(page, offset, length, KM_USER0);
2696+
zero_user(page, offset, length);
27132697
set_page_dirty(page);
27142698
err = 0;
27152699

@@ -2785,7 +2769,7 @@ int block_truncate_page(struct address_space *mapping,
27852769
goto unlock;
27862770
}
27872771

2788-
zero_user_page(page, offset, length, KM_USER0);
2772+
zero_user(page, offset, length);
27892773
mark_buffer_dirty(bh);
27902774
err = 0;
27912775

@@ -2831,7 +2815,7 @@ int block_write_full_page(struct page *page, get_block_t *get_block,
28312815
* the page size, the remaining memory is zeroed when mapped, and
28322816
* writes to that region are not written out to the file."
28332817
*/
2834-
zero_user_page(page, offset, PAGE_CACHE_SIZE - offset, KM_USER0);
2818+
zero_user_segment(page, offset, PAGE_CACHE_SIZE);
28352819
return __block_write_full_page(inode, page, get_block, wbc);
28362820
}
28372821

‎fs/cifs/inode.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ static int cifs_truncate_page(struct address_space *mapping, loff_t from)
13861386
if (!page)
13871387
return -ENOMEM;
13881388

1389-
zero_user_page(page, offset, PAGE_CACHE_SIZE - offset, KM_USER0);
1389+
zero_user_segment(page, offset, PAGE_CACHE_SIZE);
13901390
unlock_page(page);
13911391
page_cache_release(page);
13921392
return rc;

‎fs/direct-io.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,8 @@ static int do_direct_IO(struct dio *dio)
878878
page_cache_release(page);
879879
goto out;
880880
}
881-
zero_user_page(page, block_in_page << blkbits,
882-
1 << blkbits, KM_USER0);
881+
zero_user(page, block_in_page << blkbits,
882+
1 << blkbits);
883883
dio->block_in_file++;
884884
block_in_page++;
885885
goto next_block;

‎fs/ecryptfs/mmap.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
257257
end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
258258
if (to > end_byte_in_page)
259259
end_byte_in_page = to;
260-
zero_user_page(page, end_byte_in_page,
261-
PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
260+
zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
262261
out:
263262
return 0;
264263
}
@@ -307,7 +306,7 @@ static int ecryptfs_prepare_write(struct file *file, struct page *page,
307306
*/
308307
if ((i_size_read(page->mapping->host) == prev_page_end_size) &&
309308
(from != 0)) {
310-
zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
309+
zero_user(page, 0, PAGE_CACHE_SIZE);
311310
}
312311
out:
313312
return rc;

‎fs/ext3/inode.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,7 +1845,7 @@ static int ext3_block_truncate_page(handle_t *handle, struct page *page,
18451845
*/
18461846
if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
18471847
ext3_should_writeback_data(inode) && PageUptodate(page)) {
1848-
zero_user_page(page, offset, length, KM_USER0);
1848+
zero_user(page, offset, length);
18491849
set_page_dirty(page);
18501850
goto unlock;
18511851
}
@@ -1898,7 +1898,7 @@ static int ext3_block_truncate_page(handle_t *handle, struct page *page,
18981898
goto unlock;
18991899
}
19001900

1901-
zero_user_page(page, offset, length, KM_USER0);
1901+
zero_user(page, offset, length);
19021902
BUFFER_TRACE(bh, "zeroed end of block");
19031903

19041904
err = 0;

‎fs/ext4/inode.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ int ext4_block_truncate_page(handle_t *handle, struct page *page,
18401840
*/
18411841
if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
18421842
ext4_should_writeback_data(inode) && PageUptodate(page)) {
1843-
zero_user_page(page, offset, length, KM_USER0);
1843+
zero_user(page, offset, length);
18441844
set_page_dirty(page);
18451845
goto unlock;
18461846
}
@@ -1893,7 +1893,7 @@ int ext4_block_truncate_page(handle_t *handle, struct page *page,
18931893
goto unlock;
18941894
}
18951895

1896-
zero_user_page(page, offset, length, KM_USER0);
1896+
zero_user(page, offset, length);
18971897

18981898
BUFFER_TRACE(bh, "zeroed end of block");
18991899

‎fs/gfs2/bmap.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ static int gfs2_block_truncate_page(struct address_space *mapping)
932932
if (!gfs2_is_writeback(ip))
933933
gfs2_trans_add_bh(ip->i_gl, bh, 0);
934934

935-
zero_user_page(page, offset, length, KM_USER0);
935+
zero_user(page, offset, length);
936936

937937
unlock:
938938
unlock_page(page);

‎fs/gfs2/ops_address.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
446446
* so we need to supply one here. It doesn't happen often.
447447
*/
448448
if (unlikely(page->index)) {
449-
zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
449+
zero_user(page, 0, PAGE_CACHE_SIZE);
450450
return 0;
451451
}
452452

‎fs/libfs.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,10 @@ int simple_prepare_write(struct file *file, struct page *page,
341341
unsigned from, unsigned to)
342342
{
343343
if (!PageUptodate(page)) {
344-
if (to - from != PAGE_CACHE_SIZE) {
345-
void *kaddr = kmap_atomic(page, KM_USER0);
346-
memset(kaddr, 0, from);
347-
memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
348-
flush_dcache_page(page);
349-
kunmap_atomic(kaddr, KM_USER0);
350-
}
344+
if (to - from != PAGE_CACHE_SIZE)
345+
zero_user_segments(page,
346+
0, from,
347+
to, PAGE_CACHE_SIZE);
351348
}
352349
return 0;
353350
}

‎fs/mpage.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
276276
}
277277

278278
if (first_hole != blocks_per_page) {
279-
zero_user_page(page, first_hole << blkbits,
280-
PAGE_CACHE_SIZE - (first_hole << blkbits),
281-
KM_USER0);
279+
zero_user_segment(page, first_hole << blkbits, PAGE_CACHE_SIZE);
282280
if (first_hole == 0) {
283281
SetPageUptodate(page);
284282
unlock_page(page);
@@ -571,8 +569,7 @@ static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
571569

572570
if (page->index > end_index || !offset)
573571
goto confused;
574-
zero_user_page(page, offset, PAGE_CACHE_SIZE - offset,
575-
KM_USER0);
572+
zero_user_segment(page, offset, PAGE_CACHE_SIZE);
576573
}
577574

578575
/*

0 commit comments

Comments
 (0)