@@ -729,14 +729,19 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
729
729
return ret ;
730
730
}
731
731
732
- void f2fs_decompress_cluster (struct decompress_io_ctx * dic )
732
+ static int f2fs_prepare_decomp_mem (struct decompress_io_ctx * dic ,
733
+ bool pre_alloc );
734
+ static void f2fs_release_decomp_mem (struct decompress_io_ctx * dic ,
735
+ bool bypass_destroy_callback , bool pre_alloc );
736
+
737
+ void f2fs_decompress_cluster (struct decompress_io_ctx * dic , bool in_task )
733
738
{
734
739
struct f2fs_sb_info * sbi = F2FS_I_SB (dic -> inode );
735
740
struct f2fs_inode_info * fi = F2FS_I (dic -> inode );
736
741
const struct f2fs_compress_ops * cops =
737
742
f2fs_cops [fi -> i_compress_algorithm ];
743
+ bool bypass_callback = false;
738
744
int ret ;
739
- int i ;
740
745
741
746
trace_f2fs_decompress_pages_start (dic -> inode , dic -> cluster_idx ,
742
747
dic -> cluster_size , fi -> i_compress_algorithm );
@@ -746,49 +751,18 @@ void f2fs_decompress_cluster(struct decompress_io_ctx *dic)
746
751
goto out_end_io ;
747
752
}
748
753
749
- dic -> tpages = page_array_alloc (dic -> inode , dic -> cluster_size );
750
- if (!dic -> tpages ) {
751
- ret = - ENOMEM ;
752
- goto out_end_io ;
753
- }
754
-
755
- for (i = 0 ; i < dic -> cluster_size ; i ++ ) {
756
- if (dic -> rpages [i ]) {
757
- dic -> tpages [i ] = dic -> rpages [i ];
758
- continue ;
759
- }
760
-
761
- dic -> tpages [i ] = f2fs_compress_alloc_page ();
762
- if (!dic -> tpages [i ]) {
763
- ret = - ENOMEM ;
764
- goto out_end_io ;
765
- }
766
- }
767
-
768
- if (cops -> init_decompress_ctx ) {
769
- ret = cops -> init_decompress_ctx (dic );
770
- if (ret )
771
- goto out_end_io ;
772
- }
773
-
774
- dic -> rbuf = f2fs_vmap (dic -> tpages , dic -> cluster_size );
775
- if (!dic -> rbuf ) {
776
- ret = - ENOMEM ;
777
- goto out_destroy_decompress_ctx ;
778
- }
779
-
780
- dic -> cbuf = f2fs_vmap (dic -> cpages , dic -> nr_cpages );
781
- if (!dic -> cbuf ) {
782
- ret = - ENOMEM ;
783
- goto out_vunmap_rbuf ;
754
+ ret = f2fs_prepare_decomp_mem (dic , false);
755
+ if (ret ) {
756
+ bypass_callback = true;
757
+ goto out_release ;
784
758
}
785
759
786
760
dic -> clen = le32_to_cpu (dic -> cbuf -> clen );
787
761
dic -> rlen = PAGE_SIZE << dic -> log_cluster_size ;
788
762
789
763
if (dic -> clen > PAGE_SIZE * dic -> nr_cpages - COMPRESS_HEADER_SIZE ) {
790
764
ret = - EFSCORRUPTED ;
791
- goto out_vunmap_cbuf ;
765
+ goto out_release ;
792
766
}
793
767
794
768
ret = cops -> decompress_pages (dic );
@@ -809,17 +783,13 @@ void f2fs_decompress_cluster(struct decompress_io_ctx *dic)
809
783
}
810
784
}
811
785
812
- out_vunmap_cbuf :
813
- vm_unmap_ram (dic -> cbuf , dic -> nr_cpages );
814
- out_vunmap_rbuf :
815
- vm_unmap_ram (dic -> rbuf , dic -> cluster_size );
816
- out_destroy_decompress_ctx :
817
- if (cops -> destroy_decompress_ctx )
818
- cops -> destroy_decompress_ctx (dic );
786
+ out_release :
787
+ f2fs_release_decomp_mem (dic , bypass_callback , false);
788
+
819
789
out_end_io :
820
790
trace_f2fs_decompress_pages_end (dic -> inode , dic -> cluster_idx ,
821
791
dic -> clen , ret );
822
- f2fs_decompress_end_io (dic , ret );
792
+ f2fs_decompress_end_io (dic , ret , in_task );
823
793
}
824
794
825
795
/*
@@ -829,7 +799,7 @@ void f2fs_decompress_cluster(struct decompress_io_ctx *dic)
829
799
* (or in the case of a failure, cleans up without actually decompressing).
830
800
*/
831
801
void f2fs_end_read_compressed_page (struct page * page , bool failed ,
832
- block_t blkaddr )
802
+ block_t blkaddr , bool in_task )
833
803
{
834
804
struct decompress_io_ctx * dic =
835
805
(struct decompress_io_ctx * )page_private (page );
@@ -839,12 +809,12 @@ void f2fs_end_read_compressed_page(struct page *page, bool failed,
839
809
840
810
if (failed )
841
811
WRITE_ONCE (dic -> failed , true);
842
- else if (blkaddr )
812
+ else if (blkaddr && in_task )
843
813
f2fs_cache_compressed_page (sbi , page ,
844
814
dic -> inode -> i_ino , blkaddr );
845
815
846
816
if (atomic_dec_and_test (& dic -> remaining_pages ))
847
- f2fs_decompress_cluster (dic );
817
+ f2fs_decompress_cluster (dic , in_task );
848
818
}
849
819
850
820
static bool is_page_in_cluster (struct compress_ctx * cc , pgoff_t index )
@@ -1552,16 +1522,85 @@ int f2fs_write_multi_pages(struct compress_ctx *cc,
1552
1522
return err ;
1553
1523
}
1554
1524
1555
- static void f2fs_free_dic (struct decompress_io_ctx * dic );
1525
+ static inline bool allow_memalloc_for_decomp (struct f2fs_sb_info * sbi ,
1526
+ bool pre_alloc )
1527
+ {
1528
+ return pre_alloc ^ f2fs_low_mem_mode (sbi );
1529
+ }
1530
+
1531
+ static int f2fs_prepare_decomp_mem (struct decompress_io_ctx * dic ,
1532
+ bool pre_alloc )
1533
+ {
1534
+ const struct f2fs_compress_ops * cops =
1535
+ f2fs_cops [F2FS_I (dic -> inode )-> i_compress_algorithm ];
1536
+ int i ;
1537
+
1538
+ if (!allow_memalloc_for_decomp (F2FS_I_SB (dic -> inode ), pre_alloc ))
1539
+ return 0 ;
1540
+
1541
+ dic -> tpages = page_array_alloc (dic -> inode , dic -> cluster_size );
1542
+ if (!dic -> tpages )
1543
+ return - ENOMEM ;
1544
+
1545
+ for (i = 0 ; i < dic -> cluster_size ; i ++ ) {
1546
+ if (dic -> rpages [i ]) {
1547
+ dic -> tpages [i ] = dic -> rpages [i ];
1548
+ continue ;
1549
+ }
1550
+
1551
+ dic -> tpages [i ] = f2fs_compress_alloc_page ();
1552
+ if (!dic -> tpages [i ])
1553
+ return - ENOMEM ;
1554
+ }
1555
+
1556
+ dic -> rbuf = f2fs_vmap (dic -> tpages , dic -> cluster_size );
1557
+ if (!dic -> rbuf )
1558
+ return - ENOMEM ;
1559
+
1560
+ dic -> cbuf = f2fs_vmap (dic -> cpages , dic -> nr_cpages );
1561
+ if (!dic -> cbuf )
1562
+ return - ENOMEM ;
1563
+
1564
+ if (cops -> init_decompress_ctx ) {
1565
+ int ret = cops -> init_decompress_ctx (dic );
1566
+
1567
+ if (ret )
1568
+ return ret ;
1569
+ }
1570
+
1571
+ return 0 ;
1572
+ }
1573
+
1574
+ static void f2fs_release_decomp_mem (struct decompress_io_ctx * dic ,
1575
+ bool bypass_destroy_callback , bool pre_alloc )
1576
+ {
1577
+ const struct f2fs_compress_ops * cops =
1578
+ f2fs_cops [F2FS_I (dic -> inode )-> i_compress_algorithm ];
1579
+
1580
+ if (!allow_memalloc_for_decomp (F2FS_I_SB (dic -> inode ), pre_alloc ))
1581
+ return ;
1582
+
1583
+ if (!bypass_destroy_callback && cops -> destroy_decompress_ctx )
1584
+ cops -> destroy_decompress_ctx (dic );
1585
+
1586
+ if (dic -> cbuf )
1587
+ vm_unmap_ram (dic -> cbuf , dic -> nr_cpages );
1588
+
1589
+ if (dic -> rbuf )
1590
+ vm_unmap_ram (dic -> rbuf , dic -> cluster_size );
1591
+ }
1592
+
1593
+ static void f2fs_free_dic (struct decompress_io_ctx * dic ,
1594
+ bool bypass_destroy_callback );
1556
1595
1557
1596
struct decompress_io_ctx * f2fs_alloc_dic (struct compress_ctx * cc )
1558
1597
{
1559
1598
struct decompress_io_ctx * dic ;
1560
1599
pgoff_t start_idx = start_idx_of_cluster (cc );
1561
- int i ;
1600
+ struct f2fs_sb_info * sbi = F2FS_I_SB (cc -> inode );
1601
+ int i , ret ;
1562
1602
1563
- dic = f2fs_kmem_cache_alloc (dic_entry_slab , GFP_F2FS_ZERO ,
1564
- false, F2FS_I_SB (cc -> inode ));
1603
+ dic = f2fs_kmem_cache_alloc (dic_entry_slab , GFP_F2FS_ZERO , false, sbi );
1565
1604
if (!dic )
1566
1605
return ERR_PTR (- ENOMEM );
1567
1606
@@ -1587,32 +1626,43 @@ struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1587
1626
dic -> nr_rpages = cc -> cluster_size ;
1588
1627
1589
1628
dic -> cpages = page_array_alloc (dic -> inode , dic -> nr_cpages );
1590
- if (!dic -> cpages )
1629
+ if (!dic -> cpages ) {
1630
+ ret = - ENOMEM ;
1591
1631
goto out_free ;
1632
+ }
1592
1633
1593
1634
for (i = 0 ; i < dic -> nr_cpages ; i ++ ) {
1594
1635
struct page * page ;
1595
1636
1596
1637
page = f2fs_compress_alloc_page ();
1597
- if (!page )
1638
+ if (!page ) {
1639
+ ret = - ENOMEM ;
1598
1640
goto out_free ;
1641
+ }
1599
1642
1600
1643
f2fs_set_compressed_page (page , cc -> inode ,
1601
1644
start_idx + i + 1 , dic );
1602
1645
dic -> cpages [i ] = page ;
1603
1646
}
1604
1647
1648
+ ret = f2fs_prepare_decomp_mem (dic , true);
1649
+ if (ret )
1650
+ goto out_free ;
1651
+
1605
1652
return dic ;
1606
1653
1607
1654
out_free :
1608
- f2fs_free_dic (dic );
1609
- return ERR_PTR (- ENOMEM );
1655
+ f2fs_free_dic (dic , true );
1656
+ return ERR_PTR (ret );
1610
1657
}
1611
1658
1612
- static void f2fs_free_dic (struct decompress_io_ctx * dic )
1659
+ static void f2fs_free_dic (struct decompress_io_ctx * dic ,
1660
+ bool bypass_destroy_callback )
1613
1661
{
1614
1662
int i ;
1615
1663
1664
+ f2fs_release_decomp_mem (dic , bypass_destroy_callback , true);
1665
+
1616
1666
if (dic -> tpages ) {
1617
1667
for (i = 0 ; i < dic -> cluster_size ; i ++ ) {
1618
1668
if (dic -> rpages [i ])
@@ -1637,17 +1687,33 @@ static void f2fs_free_dic(struct decompress_io_ctx *dic)
1637
1687
kmem_cache_free (dic_entry_slab , dic );
1638
1688
}
1639
1689
1640
- static void f2fs_put_dic (struct decompress_io_ctx * dic )
1690
+ static void f2fs_late_free_dic (struct work_struct * work )
1691
+ {
1692
+ struct decompress_io_ctx * dic =
1693
+ container_of (work , struct decompress_io_ctx , free_work );
1694
+
1695
+ f2fs_free_dic (dic , false);
1696
+ }
1697
+
1698
+ static void f2fs_put_dic (struct decompress_io_ctx * dic , bool in_task )
1641
1699
{
1642
- if (refcount_dec_and_test (& dic -> refcnt ))
1643
- f2fs_free_dic (dic );
1700
+ if (refcount_dec_and_test (& dic -> refcnt )) {
1701
+ if (in_task ) {
1702
+ f2fs_free_dic (dic , false);
1703
+ } else {
1704
+ INIT_WORK (& dic -> free_work , f2fs_late_free_dic );
1705
+ queue_work (F2FS_I_SB (dic -> inode )-> post_read_wq ,
1706
+ & dic -> free_work );
1707
+ }
1708
+ }
1644
1709
}
1645
1710
1646
1711
/*
1647
1712
* Update and unlock the cluster's pagecache pages, and release the reference to
1648
1713
* the decompress_io_ctx that was being held for I/O completion.
1649
1714
*/
1650
- static void __f2fs_decompress_end_io (struct decompress_io_ctx * dic , bool failed )
1715
+ static void __f2fs_decompress_end_io (struct decompress_io_ctx * dic , bool failed ,
1716
+ bool in_task )
1651
1717
{
1652
1718
int i ;
1653
1719
@@ -1668,7 +1734,7 @@ static void __f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed)
1668
1734
unlock_page (rpage );
1669
1735
}
1670
1736
1671
- f2fs_put_dic (dic );
1737
+ f2fs_put_dic (dic , in_task );
1672
1738
}
1673
1739
1674
1740
static void f2fs_verify_cluster (struct work_struct * work )
@@ -1685,14 +1751,15 @@ static void f2fs_verify_cluster(struct work_struct *work)
1685
1751
SetPageError (rpage );
1686
1752
}
1687
1753
1688
- __f2fs_decompress_end_io (dic , false);
1754
+ __f2fs_decompress_end_io (dic , false, true );
1689
1755
}
1690
1756
1691
1757
/*
1692
1758
* This is called when a compressed cluster has been decompressed
1693
1759
* (or failed to be read and/or decompressed).
1694
1760
*/
1695
- void f2fs_decompress_end_io (struct decompress_io_ctx * dic , bool failed )
1761
+ void f2fs_decompress_end_io (struct decompress_io_ctx * dic , bool failed ,
1762
+ bool in_task )
1696
1763
{
1697
1764
if (!failed && dic -> need_verity ) {
1698
1765
/*
@@ -1704,7 +1771,7 @@ void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed)
1704
1771
INIT_WORK (& dic -> verity_work , f2fs_verify_cluster );
1705
1772
fsverity_enqueue_verify_work (& dic -> verity_work );
1706
1773
} else {
1707
- __f2fs_decompress_end_io (dic , failed );
1774
+ __f2fs_decompress_end_io (dic , failed , in_task );
1708
1775
}
1709
1776
}
1710
1777
@@ -1713,12 +1780,12 @@ void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed)
1713
1780
*
1714
1781
* This is called when the page is no longer needed and can be freed.
1715
1782
*/
1716
- void f2fs_put_page_dic (struct page * page )
1783
+ void f2fs_put_page_dic (struct page * page , bool in_task )
1717
1784
{
1718
1785
struct decompress_io_ctx * dic =
1719
1786
(struct decompress_io_ctx * )page_private (page );
1720
1787
1721
- f2fs_put_dic (dic );
1788
+ f2fs_put_dic (dic , in_task );
1722
1789
}
1723
1790
1724
1791
/*
0 commit comments