Re: [RFC PATCH v2 22/22] fat: Use MAX_BUF_PER_PAGE_SIZE_MAX to size buffer_head arrays
From: OGAWA Hirofumi
Date: Sat Dec 05 2026 - 02:43:02 EST
Eric Curtin <ericcurtin17@xxxxxxxxx> writes:
> From: Ryan Roberts <ryan.roberts@xxxxxxx>
>
> MAX_BUF_PER_PAGE is derived from PAGE_SIZE, so with boot-time page size
> selection it cannot be used to size an on-stack array.
>
> Add MAX_BUF_PER_PAGE_SIZE_MAX, the value of MAX_BUF_PER_PAGE at
> PAGE_SIZE_MAX, and use it for the bhs[] arrays in fat_alloc_new_dir(),
> fat_add_new_entries(), __fat_alloc_clusters() and fat_free_clusters().
>
> No functional change for compile-time page sizes.
>
> Signed-off-by: Ryan Roberts <ryan.roberts@xxxxxxx>
> Signed-off-by: Eric Curtin <eric.curtin@xxxxxxxxxx>
Hm, this usage was to prevent stack overflow. So it should not use
possible maximum value.
Looks like no other users than fatfs anymore. So instead, could you copy
it to fs/fat/fat.h as FAT_BUF_ON_STACK or such? (and MAX_BUF_PER_PAGE
can change to it or remove)
#define FAT_BUF_ON_STACK 8
Thanks.
> ---
> fs/fat/dir.c | 4 ++--
> fs/fat/fatent.c | 4 ++--
> include/linux/buffer_head.h | 1 +
> 3 files changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/fs/fat/dir.c b/fs/fat/dir.c
> index 0cebcaf82426..3923d68153dd 100644
> --- a/fs/fat/dir.c
> +++ b/fs/fat/dir.c
> @@ -1184,7 +1184,7 @@ int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts)
> {
> struct super_block *sb = dir->i_sb;
> struct msdos_sb_info *sbi = MSDOS_SB(sb);
> - struct buffer_head *bhs[MAX_BUF_PER_PAGE];
> + struct buffer_head *bhs[MAX_BUF_PER_PAGE_SIZE_MAX];
> struct msdos_dir_entry *de;
> sector_t blknr;
> __le16 date, time;
> @@ -1251,7 +1251,7 @@ static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
> {
> struct super_block *sb = dir->i_sb;
> struct msdos_sb_info *sbi = MSDOS_SB(sb);
> - struct buffer_head *bhs[MAX_BUF_PER_PAGE];
> + struct buffer_head *bhs[MAX_BUF_PER_PAGE_SIZE_MAX];
> sector_t blknr, start_blknr, last_blknr;
> unsigned long size, copy;
> int err, i, n, offset, cluster[2];
> diff --git a/fs/fat/fatent.c b/fs/fat/fatent.c
> index 2d10a815a579..1b2d925656fb 100644
> --- a/fs/fat/fatent.c
> +++ b/fs/fat/fatent.c
> @@ -510,7 +510,7 @@ static int __fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluste
> struct msdos_sb_info *sbi = MSDOS_SB(sb);
> const struct fatent_operations *ops = sbi->fatent_ops;
> struct fat_entry fatent, prev_ent;
> - struct buffer_head *bhs[MAX_BUF_PER_PAGE];
> + struct buffer_head *bhs[MAX_BUF_PER_PAGE_SIZE_MAX];
> int i, count, err, nr_bhs, idx_clus;
>
> lock_fat(sbi);
> @@ -616,7 +616,7 @@ int fat_free_clusters(struct inode *inode, int cluster)
> struct msdos_sb_info *sbi = MSDOS_SB(sb);
> const struct fatent_operations *ops = sbi->fatent_ops;
> struct fat_entry fatent;
> - struct buffer_head *bhs[MAX_BUF_PER_PAGE];
> + struct buffer_head *bhs[MAX_BUF_PER_PAGE_SIZE_MAX];
> int i, err, nr_bhs;
> int first_cl = cluster, dirty_fsinfo = 0;
>
> diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
> index f8782b719026..dbf1948950af 100644
> --- a/include/linux/buffer_head.h
> +++ b/include/linux/buffer_head.h
> @@ -42,6 +42,7 @@ enum bh_state_bits {
> };
>
> #define MAX_BUF_PER_PAGE (PAGE_SIZE / 512)
> +#define MAX_BUF_PER_PAGE_SIZE_MAX (PAGE_SIZE_MAX / 512)
>
> struct page;
> struct buffer_head;
>
--
OGAWA Hirofumi <hirofumi@xxxxxxxxxxxxxxxxxx>