UBIFS: do not allocate too much

Bulk-read allocates 128KiB or more using kmalloc. The allocation
starts failing often when the memory gets fragmented. UBIFS still
works fine in this case, because it falls-back to standard
(non-optimized) read method, though. This patch teaches bulk-read
to allocate exactly the amount of memory it needs, instead of
allocating 128KiB every time.

This patch is also a preparation to the further fix where we'll
have a pre-allocated bulk-read buffer as well. For example, now
the @bu object is prepared in 'ubifs_bulk_read()', so we could
path either pre-allocated or allocated information to
'ubifs_do_bulk_read()' later. Or teaching 'ubifs_do_bulk_read()'
not to allocate 'bu->buf' if it is already there.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>

+60 -33
+46 -24
fs/ubifs/file.c
··· 691 /** 692 * ubifs_do_bulk_read - do bulk-read. 693 * @c: UBIFS file-system description object 694 - * @page1: first page 695 * 696 * This function returns %1 if the bulk-read is done, otherwise %0 is returned. 697 */ 698 - static int ubifs_do_bulk_read(struct ubifs_info *c, struct page *page1) 699 { 700 pgoff_t offset = page1->index, end_index; 701 struct address_space *mapping = page1->mapping; 702 struct inode *inode = mapping->host; 703 struct ubifs_inode *ui = ubifs_inode(inode); 704 - struct bu_info *bu; 705 int err, page_idx, page_cnt, ret = 0, n = 0; 706 loff_t isize; 707 - 708 - bu = kmalloc(sizeof(struct bu_info), GFP_NOFS | __GFP_NOWARN); 709 - if (!bu) 710 - return 0; 711 - 712 - bu->buf_len = c->bulk_read_buf_size; 713 - bu->buf = kmalloc(bu->buf_len, GFP_NOFS | __GFP_NOWARN); 714 - if (!bu->buf) 715 - goto out_free; 716 - 717 - data_key_init(c, &bu->key, inode->i_ino, 718 - offset << UBIFS_BLOCKS_PER_PAGE_SHIFT); 719 720 err = ubifs_tnc_get_bu_keys(c, bu); 721 if (err) ··· 725 * together. If all the pages were like this, bulk-read would 726 * reduce performance, so we turn it off for a while. 727 */ 728 - ui->read_in_a_row = 0; 729 - ui->bulk_read = 0; 730 - goto out_free; 731 } 732 733 if (bu->cnt) { 734 err = ubifs_tnc_bulk_read(c, bu); 735 if (err) 736 goto out_warn; ··· 782 ui->last_page_read = offset + page_idx - 1; 783 784 out_free: 785 - kfree(bu->buf); 786 - kfree(bu); 787 return ret; 788 789 out_warn: 790 ubifs_warn("ignoring error %d and skipping bulk-read", err); 791 goto out_free; 792 } 793 ··· 810 struct ubifs_info *c = inode->i_sb->s_fs_info; 811 struct ubifs_inode *ui = ubifs_inode(inode); 812 pgoff_t index = page->index, last_page_read = ui->last_page_read; 813 - int ret = 0; 814 815 ui->last_page_read = index; 816 - 817 if (!c->bulk_read) 818 return 0; 819 /* 820 * Bulk-read is protected by ui_mutex, but it is an optimization, so 821 * don't bother if we cannot lock the mutex. 822 */ 823 if (!mutex_trylock(&ui->ui_mutex)) 824 return 0; 825 if (index != last_page_read + 1) { 826 /* Turn off bulk-read if we stop reading sequentially */ 827 ui->read_in_a_row = 1; ··· 831 ui->bulk_read = 0; 832 goto out_unlock; 833 } 834 if (!ui->bulk_read) { 835 ui->read_in_a_row += 1; 836 if (ui->read_in_a_row < 3) ··· 839 /* Three reads in a row, so switch on bulk-read */ 840 ui->bulk_read = 1; 841 } 842 - ret = ubifs_do_bulk_read(c, page); 843 out_unlock: 844 mutex_unlock(&ui->ui_mutex); 845 - return ret; 846 } 847 848 static int ubifs_readpage(struct file *file, struct page *page)
··· 691 /** 692 * ubifs_do_bulk_read - do bulk-read. 693 * @c: UBIFS file-system description object 694 + * @bu: bulk-read information 695 + * @page1: first page to read 696 * 697 * This function returns %1 if the bulk-read is done, otherwise %0 is returned. 698 */ 699 + static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu, 700 + struct page *page1) 701 { 702 pgoff_t offset = page1->index, end_index; 703 struct address_space *mapping = page1->mapping; 704 struct inode *inode = mapping->host; 705 struct ubifs_inode *ui = ubifs_inode(inode); 706 int err, page_idx, page_cnt, ret = 0, n = 0; 707 + int allocate = bu->buf ? 0 : 1; 708 loff_t isize; 709 710 err = ubifs_tnc_get_bu_keys(c, bu); 711 if (err) ··· 735 * together. If all the pages were like this, bulk-read would 736 * reduce performance, so we turn it off for a while. 737 */ 738 + goto out_bu_off; 739 } 740 741 if (bu->cnt) { 742 + if (allocate) { 743 + /* 744 + * Allocate bulk-read buffer depending on how many data 745 + * nodes we are going to read. 746 + */ 747 + bu->buf_len = bu->zbranch[bu->cnt - 1].offs + 748 + bu->zbranch[bu->cnt - 1].len - 749 + bu->zbranch[0].offs; 750 + ubifs_assert(bu->buf_len > 0); 751 + ubifs_assert(bu->buf_len <= c->leb_size); 752 + bu->buf = kmalloc(bu->buf_len, GFP_NOFS | __GFP_NOWARN); 753 + if (!bu->buf) 754 + goto out_bu_off; 755 + } 756 + 757 err = ubifs_tnc_bulk_read(c, bu); 758 if (err) 759 goto out_warn; ··· 779 ui->last_page_read = offset + page_idx - 1; 780 781 out_free: 782 + if (allocate) 783 + kfree(bu->buf); 784 return ret; 785 786 out_warn: 787 ubifs_warn("ignoring error %d and skipping bulk-read", err); 788 + goto out_free; 789 + 790 + out_bu_off: 791 + ui->read_in_a_row = ui->bulk_read = 0; 792 goto out_free; 793 } 794 ··· 803 struct ubifs_info *c = inode->i_sb->s_fs_info; 804 struct ubifs_inode *ui = ubifs_inode(inode); 805 pgoff_t index = page->index, last_page_read = ui->last_page_read; 806 + struct bu_info *bu; 807 + int err = 0; 808 809 ui->last_page_read = index; 810 if (!c->bulk_read) 811 return 0; 812 + 813 /* 814 * Bulk-read is protected by ui_mutex, but it is an optimization, so 815 * don't bother if we cannot lock the mutex. 816 */ 817 if (!mutex_trylock(&ui->ui_mutex)) 818 return 0; 819 + 820 if (index != last_page_read + 1) { 821 /* Turn off bulk-read if we stop reading sequentially */ 822 ui->read_in_a_row = 1; ··· 822 ui->bulk_read = 0; 823 goto out_unlock; 824 } 825 + 826 if (!ui->bulk_read) { 827 ui->read_in_a_row += 1; 828 if (ui->read_in_a_row < 3) ··· 829 /* Three reads in a row, so switch on bulk-read */ 830 ui->bulk_read = 1; 831 } 832 + 833 + bu = kmalloc(sizeof(struct bu_info), GFP_NOFS | __GFP_NOWARN); 834 + if (!bu) 835 + return 0; 836 + 837 + bu->buf = NULL; 838 + bu->buf_len = c->max_bu_buf_len; 839 + data_key_init(c, &bu->key, inode->i_ino, 840 + page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT); 841 + 842 + err = ubifs_do_bulk_read(c, bu, page); 843 + kfree(bu); 844 + 845 out_unlock: 846 mutex_unlock(&ui->ui_mutex); 847 + return err; 848 } 849 850 static int ubifs_readpage(struct file *file, struct page *page)
+6 -6
fs/ubifs/super.c
··· 569 c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ; 570 571 /* Buffer size for bulk-reads */ 572 - c->bulk_read_buf_size = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ; 573 - if (c->bulk_read_buf_size > c->leb_size) 574 - c->bulk_read_buf_size = c->leb_size; 575 - if (c->bulk_read_buf_size > UBIFS_KMALLOC_OK) { 576 /* Check if we can kmalloc that much */ 577 - void *try = kmalloc(c->bulk_read_buf_size, 578 GFP_KERNEL | __GFP_NOWARN); 579 kfree(try); 580 if (!try) 581 - c->bulk_read_buf_size = UBIFS_KMALLOC_OK; 582 } 583 return 0; 584 }
··· 569 c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ; 570 571 /* Buffer size for bulk-reads */ 572 + c->max_bu_buf_len = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ; 573 + if (c->max_bu_buf_len > c->leb_size) 574 + c->max_bu_buf_len = c->leb_size; 575 + if (c->max_bu_buf_len > UBIFS_KMALLOC_OK) { 576 /* Check if we can kmalloc that much */ 577 + void *try = kmalloc(c->max_bu_buf_len, 578 GFP_KERNEL | __GFP_NOWARN); 579 kfree(try); 580 if (!try) 581 + c->max_bu_buf_len = UBIFS_KMALLOC_OK; 582 } 583 return 0; 584 }
+6 -1
fs/ubifs/tnc.c
··· 1501 * @bu: bulk-read parameters and results 1502 * 1503 * Lookup consecutive data node keys for the same inode that reside 1504 - * consecutively in the same LEB. 1505 */ 1506 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu) 1507 {
··· 1501 * @bu: bulk-read parameters and results 1502 * 1503 * Lookup consecutive data node keys for the same inode that reside 1504 + * consecutively in the same LEB. This function returns zero in case of success 1505 + * and a negative error code in case of failure. 1506 + * 1507 + * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function 1508 + * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares 1509 + * maxumum possible amount of nodes for bulk-read. 1510 */ 1511 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu) 1512 {
+2 -2
fs/ubifs/ubifs.h
··· 969 * @mst_node: master node 970 * @mst_offs: offset of valid master node 971 * @mst_mutex: protects the master node area, @mst_node, and @mst_offs 972 - * @bulk_read_buf_size: buffer size for bulk-reads 973 * 974 * @log_lebs: number of logical eraseblocks in the log 975 * @log_bytes: log size in bytes ··· 1217 struct ubifs_mst_node *mst_node; 1218 int mst_offs; 1219 struct mutex mst_mutex; 1220 - int bulk_read_buf_size; 1221 1222 int log_lebs; 1223 long long log_bytes;
··· 969 * @mst_node: master node 970 * @mst_offs: offset of valid master node 971 * @mst_mutex: protects the master node area, @mst_node, and @mst_offs 972 + * @max_bu_buf_len: maximum bulk-read buffer length 973 * 974 * @log_lebs: number of logical eraseblocks in the log 975 * @log_bytes: log size in bytes ··· 1217 struct ubifs_mst_node *mst_node; 1218 int mst_offs; 1219 struct mutex mst_mutex; 1220 + int max_bu_buf_len; 1221 1222 int log_lebs; 1223 long long log_bytes;