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 691 /** 692 692 * ubifs_do_bulk_read - do bulk-read. 693 693 * @c: UBIFS file-system description object 694 - * @page1: first page 694 + * @bu: bulk-read information 695 + * @page1: first page to read 695 696 * 696 697 * This function returns %1 if the bulk-read is done, otherwise %0 is returned. 697 698 */ 698 - static int ubifs_do_bulk_read(struct ubifs_info *c, struct page *page1) 699 + static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu, 700 + struct page *page1) 699 701 { 700 702 pgoff_t offset = page1->index, end_index; 701 703 struct address_space *mapping = page1->mapping; 702 704 struct inode *inode = mapping->host; 703 705 struct ubifs_inode *ui = ubifs_inode(inode); 704 - struct bu_info *bu; 705 706 int err, page_idx, page_cnt, ret = 0, n = 0; 707 + int allocate = bu->buf ? 0 : 1; 706 708 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 709 720 710 err = ubifs_tnc_get_bu_keys(c, bu); 721 711 if (err) ··· 725 735 * together. If all the pages were like this, bulk-read would 726 736 * reduce performance, so we turn it off for a while. 727 737 */ 728 - ui->read_in_a_row = 0; 729 - ui->bulk_read = 0; 730 - goto out_free; 738 + goto out_bu_off; 731 739 } 732 740 733 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 + 734 757 err = ubifs_tnc_bulk_read(c, bu); 735 758 if (err) 736 759 goto out_warn; ··· 782 779 ui->last_page_read = offset + page_idx - 1; 783 780 784 781 out_free: 785 - kfree(bu->buf); 786 - kfree(bu); 782 + if (allocate) 783 + kfree(bu->buf); 787 784 return ret; 788 785 789 786 out_warn: 790 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; 791 792 goto out_free; 792 793 } 793 794 ··· 810 803 struct ubifs_info *c = inode->i_sb->s_fs_info; 811 804 struct ubifs_inode *ui = ubifs_inode(inode); 812 805 pgoff_t index = page->index, last_page_read = ui->last_page_read; 813 - int ret = 0; 806 + struct bu_info *bu; 807 + int err = 0; 814 808 815 809 ui->last_page_read = index; 816 - 817 810 if (!c->bulk_read) 818 811 return 0; 812 + 819 813 /* 820 814 * Bulk-read is protected by ui_mutex, but it is an optimization, so 821 815 * don't bother if we cannot lock the mutex. 822 816 */ 823 817 if (!mutex_trylock(&ui->ui_mutex)) 824 818 return 0; 819 + 825 820 if (index != last_page_read + 1) { 826 821 /* Turn off bulk-read if we stop reading sequentially */ 827 822 ui->read_in_a_row = 1; ··· 831 822 ui->bulk_read = 0; 832 823 goto out_unlock; 833 824 } 825 + 834 826 if (!ui->bulk_read) { 835 827 ui->read_in_a_row += 1; 836 828 if (ui->read_in_a_row < 3) ··· 839 829 /* Three reads in a row, so switch on bulk-read */ 840 830 ui->bulk_read = 1; 841 831 } 842 - ret = ubifs_do_bulk_read(c, page); 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 + 843 845 out_unlock: 844 846 mutex_unlock(&ui->ui_mutex); 845 - return ret; 847 + return err; 846 848 } 847 849 848 850 static int ubifs_readpage(struct file *file, struct page *page)
+6 -6
fs/ubifs/super.c
··· 569 569 c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ; 570 570 571 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) { 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 576 /* Check if we can kmalloc that much */ 577 - void *try = kmalloc(c->bulk_read_buf_size, 577 + void *try = kmalloc(c->max_bu_buf_len, 578 578 GFP_KERNEL | __GFP_NOWARN); 579 579 kfree(try); 580 580 if (!try) 581 - c->bulk_read_buf_size = UBIFS_KMALLOC_OK; 581 + c->max_bu_buf_len = UBIFS_KMALLOC_OK; 582 582 } 583 583 return 0; 584 584 }
+6 -1
fs/ubifs/tnc.c
··· 1501 1501 * @bu: bulk-read parameters and results 1502 1502 * 1503 1503 * Lookup consecutive data node keys for the same inode that reside 1504 - * consecutively in the same LEB. 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. 1505 1510 */ 1506 1511 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu) 1507 1512 {
+2 -2
fs/ubifs/ubifs.h
··· 969 969 * @mst_node: master node 970 970 * @mst_offs: offset of valid master node 971 971 * @mst_mutex: protects the master node area, @mst_node, and @mst_offs 972 - * @bulk_read_buf_size: buffer size for bulk-reads 972 + * @max_bu_buf_len: maximum bulk-read buffer length 973 973 * 974 974 * @log_lebs: number of logical eraseblocks in the log 975 975 * @log_bytes: log size in bytes ··· 1217 1217 struct ubifs_mst_node *mst_node; 1218 1218 int mst_offs; 1219 1219 struct mutex mst_mutex; 1220 - int bulk_read_buf_size; 1220 + int max_bu_buf_len; 1221 1221 1222 1222 int log_lebs; 1223 1223 long long log_bytes;