@@ -1028,6 +1028,41 @@ static ssize_t comp_algorithm_store(struct device *dev,
10281028 return len ;
10291029}
10301030
1031+ static ssize_t use_dedup_show (struct device * dev ,
1032+ struct device_attribute * attr , char * buf )
1033+ {
1034+ bool val ;
1035+ struct zram * zram = dev_to_zram (dev );
1036+
1037+ down_read (& zram -> init_lock );
1038+ val = zram -> use_dedup ;
1039+ up_read (& zram -> init_lock );
1040+
1041+ return scnprintf (buf , PAGE_SIZE , "%d\n" , (int )val );
1042+ }
1043+
1044+ #ifdef CONFIG_ZRAM_DEDUP
1045+ static ssize_t use_dedup_store (struct device * dev ,
1046+ struct device_attribute * attr , const char * buf , size_t len )
1047+ {
1048+ int val ;
1049+ struct zram * zram = dev_to_zram (dev );
1050+
1051+ if (kstrtoint (buf , 10 , & val ) || (val != 0 && val != 1 ))
1052+ return - EINVAL ;
1053+
1054+ down_write (& zram -> init_lock );
1055+ if (init_done (zram )) {
1056+ up_write (& zram -> init_lock );
1057+ pr_info ("Can't change dedup usage for initialized device\n" );
1058+ return - EBUSY ;
1059+ }
1060+ zram -> use_dedup = val ;
1061+ up_write (& zram -> init_lock );
1062+ return len ;
1063+ }
1064+ #endif
1065+
10311066static ssize_t compact_store (struct device * dev ,
10321067 struct device_attribute * attr , const char * buf , size_t len )
10331068{
@@ -1145,21 +1180,35 @@ static DEVICE_ATTR_RO(bd_stat);
11451180#endif
11461181static DEVICE_ATTR_RO (debug_stat );
11471182
1183+ static unsigned long zram_entry_handle (struct zram * zram ,
1184+ struct zram_entry * entry )
1185+ {
1186+ if (zram_dedup_enabled (zram ))
1187+ return entry -> handle ;
1188+ else
1189+ return (unsigned long )entry ;
1190+ }
1191+
1192+
11481193
11491194static struct zram_entry * zram_entry_alloc (struct zram * zram ,
11501195 unsigned int len , gfp_t flags )
11511196{
11521197 struct zram_entry * entry ;
11531198 unsigned long handle ;
11541199
1155- entry = kzalloc ( sizeof ( * entry ),
1156- flags & ~( __GFP_HIGHMEM | __GFP_MOVABLE | __GFP_CMA ));
1157- if (! entry )
1200+ handle = zs_malloc ( zram -> mem_pool , len , flags );
1201+ if (! handle )
1202+
11581203 return NULL ;
11591204
1160- handle = zs_malloc (zram -> mem_pool , len , flags );
1161- if (!handle ) {
1162- kfree (entry );
1205+ if (!zram_dedup_enabled (zram ))
1206+ return (struct zram_entry * )handle ;
1207+
1208+ entry = kzalloc (sizeof (* entry ),
1209+ flags & ~(__GFP_HIGHMEM |__GFP_MOVABLE |__GFP_CMA ));
1210+ if (!entry ) {
1211+ zs_free (zram -> mem_pool , handle );
11631212 return NULL ;
11641213 }
11651214
@@ -1175,7 +1224,11 @@ void zram_entry_free(struct zram *zram, struct zram_entry *entry)
11751224 if (!zram_dedup_put_entry (zram , entry ))
11761225 return ;
11771226
1178- zs_free (zram -> mem_pool , entry -> handle );
1227+ zs_free (zram -> mem_pool , zram_entry_handle (zram , entry ));
1228+
1229+ if (!zram_dedup_enabled (zram ))
1230+ return ;
1231+
11791232 kfree (entry );
11801233
11811234 atomic64_sub (sizeof (* entry ), & zram -> stats .meta_data_size );
@@ -1314,7 +1367,8 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
13141367
13151368 size = zram_get_obj_size (zram , index );
13161369
1317- src = zs_map_object (zram -> mem_pool , entry -> handle , ZS_MM_RO );
1370+ src = zs_map_object (zram -> mem_pool ,
1371+ zram_entry_handle (zram , entry ), ZS_MM_RO );
13181372 if (size == PAGE_SIZE ) {
13191373 dst = kmap_atomic (page );
13201374 memcpy (dst , src , PAGE_SIZE );
@@ -1328,7 +1382,7 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
13281382 kunmap_atomic (dst );
13291383 zcomp_stream_put (zram -> comp );
13301384 }
1331- zs_unmap_object (zram -> mem_pool , entry -> handle );
1385+ zs_unmap_object (zram -> mem_pool , zram_entry_handle ( zram , entry ) );
13321386 zram_slot_unlock (zram , index );
13331387
13341388 /* Should NEVER happen. Return bio error if it does. */
@@ -1456,7 +1510,8 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
14561510 return - ENOMEM ;
14571511 }
14581512
1459- dst = zs_map_object (zram -> mem_pool , entry -> handle , ZS_MM_WO );
1513+ dst = zs_map_object (zram -> mem_pool ,
1514+ zram_entry_handle (zram , entry ), ZS_MM_WO );
14601515
14611516 src = zstrm -> buffer ;
14621517 if (comp_len == PAGE_SIZE )
@@ -1466,7 +1521,7 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
14661521 kunmap_atomic (src );
14671522
14681523 zcomp_stream_put (zram -> comp );
1469- zs_unmap_object (zram -> mem_pool , entry -> handle );
1524+ zs_unmap_object (zram -> mem_pool , zram_entry_handle ( zram , entry ) );
14701525 atomic64_add (comp_len , & zram -> stats .compr_data_size );
14711526 zram_dedup_insert (zram , entry , checksum );
14721527out :
@@ -1904,6 +1959,11 @@ static DEVICE_ATTR_WO(writeback);
19041959static DEVICE_ATTR_RW (writeback_limit );
19051960static DEVICE_ATTR_RW (writeback_limit_enable );
19061961#endif
1962+ #ifdef CONFIG_ZRAM_DEDUP
1963+ static DEVICE_ATTR_RW (use_dedup );
1964+ #else
1965+ static DEVICE_ATTR_RO (use_dedup );
1966+ #endif
19071967
19081968static struct attribute * zram_disk_attrs [] = {
19091969 & dev_attr_disksize .attr ,
@@ -1921,6 +1981,7 @@ static struct attribute *zram_disk_attrs[] = {
19211981 & dev_attr_writeback_limit .attr ,
19221982 & dev_attr_writeback_limit_enable .attr ,
19231983#endif
1984+ & dev_attr_use_dedup .attr ,
19241985 & dev_attr_io_stat .attr ,
19251986 & dev_attr_mm_stat .attr ,
19261987#ifdef CONFIG_ZRAM_WRITEBACK
0 commit comments