@@ -57,7 +57,6 @@ static size_t huge_class_size;
5757
5858static const struct block_device_operations zram_devops ;
5959
60- static void zram_free_page (struct zram * zram , size_t index );
6160static int zram_read_from_zspool (struct zram * zram , struct page * page ,
6261 u32 index );
6362
@@ -96,7 +95,7 @@ static __must_check bool zram_slot_trylock(struct zram *zram, u32 index)
9695 return false;
9796}
9897
99- static void zram_slot_lock (struct zram * zram , u32 index )
98+ void zram_slot_lock (struct zram * zram , u32 index )
10099{
101100 unsigned long * lock = & zram -> table [index ].flags ;
102101
@@ -105,7 +104,7 @@ static void zram_slot_lock(struct zram *zram, u32 index)
105104 lock_acquired (slot_dep_map (zram , index ), _RET_IP_ );
106105}
107106
108- static void zram_slot_unlock (struct zram * zram , u32 index )
107+ void zram_slot_unlock (struct zram * zram , u32 index )
109108{
110109 unsigned long * lock = & zram -> table [index ].flags ;
111110
@@ -128,19 +127,17 @@ static unsigned long zram_get_handle(struct zram *zram, u32 index)
128127 return zram -> table [index ].handle ;
129128}
130129
131- static void zram_set_handle (struct zram * zram , u32 index , unsigned long handle )
130+ void zram_set_handle (struct zram * zram , u32 index , unsigned long handle )
132131{
133132 zram -> table [index ].handle = handle ;
134133}
135134
136- static bool zram_test_flag (struct zram * zram , u32 index ,
137- enum zram_pageflags flag )
135+ bool zram_test_flag (struct zram * zram , u32 index , enum zram_pageflags flag )
138136{
139137 return zram -> table [index ].flags & BIT (flag );
140138}
141139
142- static void zram_set_flag (struct zram * zram , u32 index ,
143- enum zram_pageflags flag )
140+ void zram_set_flag (struct zram * zram , u32 index , enum zram_pageflags flag )
144141{
145142 zram -> table [index ].flags |= BIT (flag );
146143}
@@ -243,22 +240,33 @@ static struct zram_pp_ctl *init_pp_ctl(void)
243240 if (!ctl )
244241 return NULL ;
245242
243+ init_completion (& ctl -> all_done );
244+ atomic_set (& ctl -> num_pp_slots , 0 );
246245 for (idx = 0 ; idx < NUM_PP_BUCKETS ; idx ++ )
247246 INIT_LIST_HEAD (& ctl -> pp_buckets [idx ]);
248247 return ctl ;
249248}
250249
251- static void release_pp_slot ( struct zram * zram , struct zram_pp_slot * pps )
250+ static void remove_pp_slot_from_ctl ( struct zram_pp_slot * pps )
252251{
253252 list_del_init (& pps -> entry );
253+ }
254254
255+ void free_pp_slot (struct zram * zram , struct zram_pp_slot * pps )
256+ {
255257 zram_slot_lock (zram , pps -> index );
256258 zram_clear_flag (zram , pps -> index , ZRAM_PP_SLOT );
257259 zram_slot_unlock (zram , pps -> index );
258260
259261 kfree (pps );
260262}
261263
264+ static void release_pp_slot (struct zram * zram , struct zram_pp_slot * pps )
265+ {
266+ remove_pp_slot_from_ctl (pps );
267+ free_pp_slot (zram , pps );
268+ }
269+
262270static void release_pp_ctl (struct zram * zram , struct zram_pp_ctl * ctl )
263271{
264272 u32 idx ;
@@ -297,6 +305,7 @@ static bool place_pp_slot(struct zram *zram, struct zram_pp_ctl *ctl,
297305 list_add (& pps -> entry , & ctl -> pp_buckets [bid ]);
298306
299307 zram_set_flag (zram , pps -> index , ZRAM_PP_SLOT );
308+ atomic_inc (& ctl -> num_pp_slots );
300309 return true;
301310}
302311
@@ -697,18 +706,18 @@ static void read_from_bdev_async(struct zram *zram, struct page *page,
697706static int zram_writeback_slots (struct zram * zram , struct zram_pp_ctl * ctl )
698707{
699708 unsigned long blk_idx = 0 ;
700- struct page * page = NULL ;
701709 struct zram_pp_slot * pps ;
702- struct bio_vec bio_vec ;
703- struct bio bio ;
704- int ret = 0 , err ;
710+ int ret = 0 ;
705711 u32 index ;
712+ int nr_pps = atomic_read (& ctl -> num_pp_slots );
706713
707- page = alloc_page (GFP_KERNEL );
708- if (!page )
709- return - ENOMEM ;
714+ if (!nr_pps )
715+ return 0 ;
710716
711717 while ((pps = select_pp_slot (ctl ))) {
718+ struct zram_wb_request * req ;
719+ struct page * page ;
720+
712721 spin_lock (& zram -> wb_limit_lock );
713722 if (zram -> wb_limit_enable && !zram -> bd_wb_limit ) {
714723 spin_unlock (& zram -> wb_limit_lock );
@@ -725,6 +734,13 @@ static int zram_writeback_slots(struct zram *zram, struct zram_pp_ctl *ctl)
725734 }
726735 }
727736
737+ req = alloc_wb_request (zram , pps , ctl , blk_idx );
738+ if (IS_ERR (req )) {
739+ ret = PTR_ERR (req );
740+ break ;
741+ }
742+ page = bio_first_page_all (req -> bio );
743+
728744 index = pps -> index ;
729745 zram_slot_lock (zram , index );
730746 /*
@@ -739,63 +755,28 @@ static int zram_writeback_slots(struct zram *zram, struct zram_pp_ctl *ctl)
739755 goto next ;
740756 zram_slot_unlock (zram , index );
741757
742- bio_init (& bio , zram -> bdev , & bio_vec , 1 ,
743- REQ_OP_WRITE | REQ_SYNC );
744- bio .bi_iter .bi_sector = blk_idx * (PAGE_SIZE >> 9 );
745- __bio_add_page (& bio , page , PAGE_SIZE , 0 );
746-
747- /*
748- * XXX: A single page IO would be inefficient for write
749- * but it would be not bad as starter.
750- */
751- err = submit_bio_wait (& bio );
752- if (err ) {
753- release_pp_slot (zram , pps );
754- /*
755- * BIO errors are not fatal, we continue and simply
756- * attempt to writeback the remaining objects (pages).
757- * At the same time we need to signal user-space that
758- * some writes (at least one, but also could be all of
759- * them) were not successful and we do so by returning
760- * the most recent BIO error.
761- */
762- ret = err ;
763- continue ;
764- }
765-
766- atomic64_inc (& zram -> stats .bd_writes );
767- zram_slot_lock (zram , index );
768- /*
769- * Same as above, we release slot lock during writeback so
770- * slot can change under us: slot_free() or slot_free() and
771- * reallocation (zram_write_page()). In both cases slot loses
772- * ZRAM_PP_SLOT flag. No concurrent post-processing can set
773- * ZRAM_PP_SLOT on such slots until current post-processing
774- * finishes.
775- */
776- if (!zram_test_flag (zram , index , ZRAM_PP_SLOT ))
777- goto next ;
778-
779- zram_free_page (zram , index );
780- zram_set_flag (zram , index , ZRAM_WB );
781- zram_set_handle (zram , index , blk_idx );
758+ nr_pps -- ;
759+ remove_pp_slot_from_ctl (pps );
782760 blk_idx = 0 ;
783- atomic64_inc (& zram -> stats .pages_stored );
784- spin_lock (& zram -> wb_limit_lock );
785- if (zram -> wb_limit_enable && zram -> bd_wb_limit > 0 )
786- zram -> bd_wb_limit -= 1UL << (PAGE_SHIFT - 12 );
787- spin_unlock (& zram -> wb_limit_lock );
761+ submit_bio (req -> bio );
762+ continue ;
763+
788764next :
789765 zram_slot_unlock (zram , index );
790766 release_pp_slot (zram , pps );
767+ free_wb_request (req );
791768
792769 cond_resched ();
793770 }
794771
795772 if (blk_idx )
796773 free_block_bdev (zram , blk_idx );
797- if (page )
798- __free_page (page );
774+
775+ if (nr_pps && atomic_sub_and_test (nr_pps , & ctl -> num_pp_slots ))
776+ complete (& ctl -> all_done );
777+
778+ /* wait until all async bios completed */
779+ wait_for_completion (& ctl -> all_done );
799780
800781 return ret ;
801782}
@@ -1579,7 +1560,7 @@ static bool zram_meta_alloc(struct zram *zram, u64 disksize)
15791560 return true;
15801561}
15811562
1582- static void zram_free_page (struct zram * zram , size_t index )
1563+ void zram_free_page (struct zram * zram , size_t index )
15831564{
15841565 unsigned long handle ;
15851566
0 commit comments