Re: [PATCH v2] fs/ext2: Avoid page_address on pages returned by ext2_get_page

From: Ira Weiny
Date: Wed Jul 14 2021 - 13:07:48 EST


On Wed, Jul 14, 2021 at 06:54:48PM +0200, Javier Pello wrote:
> From: Javier Pello <javier.pello@xxxxxxx>
>
> Commit 782b76d7abdf02b12c46ed6f1e9bf715569027f7 ("fs/ext2: Replace
> kmap() with kmap_local_page()") replaced the kmap/kunmap calls in
> ext2_get_page/ext2_put_page with kmap_local_page/kunmap_local for
> efficiency reasons. As a necessary side change, the commit also
> made ext2_get_page (and ext2_find_entry and ext2_dotdot) return
> the mapping address along with the page itself, as it is required
> for kunmap_local, and converted uses of page_address on such pages
> to use the newly returned address instead. However, uses of
> page_address on such pages were missed in ext2_check_page and
> ext2_delete_entry, which triggers oopses if kmap_local_page happens
> to return an address from high memory. Fix this now by converting
> the remaining uses of page_address to use the right address, as
> returned by kmap_local_page.
>

Again thanks for catching this!

Reviewed-by: Ira Weiny <ira.weiny@xxxxxxxxx>

> Signed-off-by: Javier Pello <javier.pello@xxxxxxx>
> Fixes: 782b76d7abdf ("fs/ext2: Replace kmap() with kmap_local_page()")
> ---
>
> v2: Use char * for the new parameter to ext2_delete_entry.
>
> fs/ext2/dir.c | 12 ++++++------
> fs/ext2/ext2.h | 3 ++-
> fs/ext2/namei.c | 4 ++--
> 3 files changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/fs/ext2/dir.c b/fs/ext2/dir.c
> index 14292dba..2c2f179b 100644
> --- a/fs/ext2/dir.c
> +++ b/fs/ext2/dir.c
> @@ -106,12 +106,11 @@ static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
> return err;
> }
>
> -static bool ext2_check_page(struct page *page, int quiet)
> +static bool ext2_check_page(struct page *page, int quiet, char *kaddr)
> {
> struct inode *dir = page->mapping->host;
> struct super_block *sb = dir->i_sb;
> unsigned chunk_size = ext2_chunk_size(dir);
> - char *kaddr = page_address(page);
> u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
> unsigned offs, rec_len;
> unsigned limit = PAGE_SIZE;
> @@ -205,7 +204,8 @@ static struct page * ext2_get_page(struct inode *dir, unsigned long n,
> if (!IS_ERR(page)) {
> *page_addr = kmap_local_page(page);
> if (unlikely(!PageChecked(page))) {
> - if (PageError(page) || !ext2_check_page(page, quiet))
> + if (PageError(page) || !ext2_check_page(page, quiet,
> + *page_addr))
> goto fail;
> }
> }
> @@ -584,10 +584,10 @@ int ext2_add_link (struct dentry *dentry, struct inode *inode)
> * ext2_delete_entry deletes a directory entry by merging it with the
> * previous entry. Page is up-to-date.
> */
> -int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
> +int ext2_delete_entry (struct ext2_dir_entry_2 *dir, struct page *page,
> + char *kaddr)
> {
> struct inode *inode = page->mapping->host;
> - char *kaddr = page_address(page);
> unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
> unsigned to = ((char *)dir - kaddr) +
> ext2_rec_len_from_disk(dir->rec_len);
> @@ -607,7 +607,7 @@ int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
> de = ext2_next_entry(de);
> }
> if (pde)
> - from = (char*)pde - (char*)page_address(page);
> + from = (char *)pde - kaddr;
> pos = page_offset(page) + from;
> lock_page(page);
> err = ext2_prepare_chunk(page, pos, to - from);
> diff --git a/fs/ext2/ext2.h b/fs/ext2/ext2.h
> index b0a69482..e512630c 100644
> --- a/fs/ext2/ext2.h
> +++ b/fs/ext2/ext2.h
> @@ -740,7 +740,8 @@ extern int ext2_inode_by_name(struct inode *dir,
> extern int ext2_make_empty(struct inode *, struct inode *);
> extern struct ext2_dir_entry_2 *ext2_find_entry(struct inode *, const struct qstr *,
> struct page **, void **res_page_addr);
> -extern int ext2_delete_entry (struct ext2_dir_entry_2 *, struct page *);
> +extern int ext2_delete_entry(struct ext2_dir_entry_2 *dir, struct page *page,
> + char *kaddr);
> extern int ext2_empty_dir (struct inode *);
> extern struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct page **p, void **pa);
> extern void ext2_set_link(struct inode *, struct ext2_dir_entry_2 *, struct page *, void *,
> diff --git a/fs/ext2/namei.c b/fs/ext2/namei.c
> index 1f69b816..5f6b7560 100644
> --- a/fs/ext2/namei.c
> +++ b/fs/ext2/namei.c
> @@ -293,7 +293,7 @@ static int ext2_unlink(struct inode * dir, struct dentry *dentry)
> goto out;
> }
>
> - err = ext2_delete_entry (de, page);
> + err = ext2_delete_entry (de, page, page_addr);
> ext2_put_page(page, page_addr);
> if (err)
> goto out;
> @@ -397,7 +397,7 @@ static int ext2_rename (struct user_namespace * mnt_userns,
> old_inode->i_ctime = current_time(old_inode);
> mark_inode_dirty(old_inode);
>
> - ext2_delete_entry(old_de, old_page);
> + ext2_delete_entry(old_de, old_page, old_page_addr);
>
> if (dir_de) {
> if (old_dir != new_dir)
> --
> 2.30.1