Skip to contentSkip to conversation

bitmap: Introduce alloc/free helpers

PATCH Superseded Andy Shevchenko 7 participants 24 messages Jun 15, 2018 – Jun 20, 2018

Subsystems (6)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover bitmap: Introduce alloc/free helpers

A lot of code is using allocation of bitmaps using BITS_PER_LONG() macro and sizeof(unsigned long) operator. The readability suffers because of this.

The series introduces three helpers, i.e. bitmap_alloc(), bitmap_zalloc() and bitmap_free(), to make it more cleaner.

Patch 1 is a preparatory to avoid namespace collisions between bitmap API and MD bitmap. No functional changes intended.

Patch 2 is just orphaned from previous release cycle.

Patch 3 introduces new helpers.

Patches 4 and 5 is just an example how to use new helpers. Locally I have like dozen of them against different subsystems and drivers.

Ideally it would go through Input subsystem, thus, needs an Ack from MD maintainer(s).

Since v2:
- added namespace fix patch against MD bitmap API
- moved functions to lib/bitmap.c to avoid circular dependencies
- appended Dmitry's tags

Andy Shevchenko (5):
md: Avoid namespace collision with bitmap API
bitmap: Drop unnecessary 0 check for u32 array operations
bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()
Input: gpio-keys - Switch to bitmap_zalloc()
Input: evdev - Switch to bitmap_zalloc()

 drivers/input/evdev.c                         |  16 +-
 drivers/input/keyboard/gpio_keys.c            |   8 +-
 drivers/md/dm-raid.c                          |   6 +-
 drivers/md/md-bitmap.c                        | 301 +++++++++---------
 drivers/md/md-bitmap.h                        |  46 +--
 drivers/md/md-cluster.c                       |  16 +-
 drivers/md/md.c                               |  44 +--
 .../md/persistent-data/dm-space-map-common.c  |   8 +-
 drivers/md/raid1.c                            |  20 +-
 drivers/md/raid10.c                           |  26 +-
 drivers/md/raid5-cache.c                      |   2 +-
 drivers/md/raid5.c                            |  24 +-
 include/linux/bitmap.h                        |   8 +
 lib/bitmap.c                                  |  28 +-
 14 files changed, 281 insertions(+), 272 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/5 md: Avoid namespace collision with bitmap API

bitmap API (include/linux/bitmap.h) has 'bitmap' prefix for its methods.

On the other hand MD bitmap API is special case. Adding 'md' prefix to it to avoid name space collision.

---
 drivers/md/dm-raid.c                          |   6 +-
 drivers/md/md-bitmap.c                        | 301 +++++++++---------
 drivers/md/md-bitmap.h                        |  46 +--
 drivers/md/md-cluster.c                       |  16 +-
 drivers/md/md.c                               |  44 +--
 .../md/persistent-data/dm-space-map-common.c  |   8 +-
 drivers/md/raid1.c                            |  20 +-
 drivers/md/raid10.c                           |  26 +-
 drivers/md/raid5-cache.c                      |   2 +-
 drivers/md/raid5.c                            |  24 +-
 10 files changed, 242 insertions(+), 251 deletions(-)
drivers/md/dm-raid.c +3 -3
index ab13fcec3fca..71d158560e69 100644
@@ -3859,7 +3859,7 @@ static int __load_dirty_region_bitmap(struct raid_set *rs)
 	/* Try loading the bitmap unless "raid0", which does not have one */
 	if (!rs_is_raid0(rs) &&
 	    !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
-		r = bitmap_load(&rs->md);
+		r = md_bitmap_load(&rs->md);
 		if (r)
 			DMERR("Failed to load bitmap");
 	}
@@ -3987,8 +3987,8 @@ static int raid_preresume(struct dm_target *ti)
 	/* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */
 	if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && mddev->bitmap &&
 	    mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) {
-		r = bitmap_resize(mddev->bitmap, mddev->dev_sectors,
-				  to_bytes(rs->requested_bitmap_chunk_sectors), 0);
+		r = md_bitmap_resize(mddev->bitmap, mddev->dev_sectors,
+				     to_bytes(rs->requested_bitmap_chunk_sectors), 0);
 		if (r)
 			DMERR("Failed to resize bitmap");
 	}
drivers/md/md-bitmap.c +146 -155

891 lines not on this page · raw message

drivers/md/md-bitmap.h +23 -23

66 lines not on this page · raw message

drivers/md/md-cluster.c +8 -8
index 021cbf9ef1bf..8f40c8e50c69 100644
@@ -326,7 +326,7 @@ static void recover_bitmaps(struct md_thread *thread)
 					str, ret);
 			goto clear_bit;
 		}
-		ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
+		ret = md_bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
 		if (ret) {
 			pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
 			goto clear_bit;
@@ -480,7 +480,7 @@ static void process_suspend_info(struct mddev *mddev,
 	 * resync thread is running in another node,
 	 * so we don't need to do the resync again
 	 * with the same section */
-	bitmap_sync_with_cluster(mddev, cinfo->sync_low,
+	md_bitmap_sync_with_cluster(mddev, cinfo->sync_low,
 					cinfo->sync_hi,
 					lo, hi);
 	cinfo->sync_low = lo;
@@ -829,7 +829,7 @@ static int gather_all_resync_info(struct mddev *mddev, int total_slots)
 		}
 
 		/* Read the disk bitmap sb and check if it needs recovery */
-		ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
+		ret = md_bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
 		if (ret) {
 			pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
 			lockres_free(bm_lockres);
@@ -1127,13 +1127,13 @@ static int cluster_check_sync_size(struct mddev *mddev)
 		bm_lockres = lockres_init(mddev, str, NULL, 1);
 		if (!bm_lockres) {
 			pr_err("md-cluster: Cannot initialize %s\n", str);
-			bitmap_free(bitmap);
+			md_bitmap_free(bitmap);
 			return -1;
 		}
 		bm_lockres->flags |= DLM_LKF_NOQUEUE;
 		rv = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
 		if (!rv)
-			bitmap_update_sb(bitmap);
+			md_bitmap_update_sb(bitmap);
 		lockres_free(bm_lockres);
 
 		sb = kmap_atomic(bitmap->storage.sb_page);
@@ -1141,11 +1141,11 @@ static int cluster_check_sync_size(struct mddev *mddev)
 			sync_size = sb->sync_size;
 		else if (sync_size != sb->sync_size) {
 			kunmap_atomic(sb);
-			bitmap_free(bitmap);
+			md_bitmap_free(bitmap);
 			return -1;
 		}
 		kunmap_atomic(sb);
-		bitmap_free(bitmap);
+		md_bitmap_free(bitmap);
 	}
 
 	return (my_sync_size == sync_size) ? 0 : -1;
@@ -1442,7 +1442,7 @@ static int gather_bitmaps(struct md_rdev *rdev)
 	for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
 		if (sn == (cinfo->slot_number - 1))
 			continue;
-		err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
+		err = md_bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
 		if (err) {
 			pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
 			goto out;
drivers/md/md.c +22 -22

154 lines not on this page · raw message

drivers/md/persistent-data/dm-space-map-common.c +4 -4
index 829b4ce057d8..121712886abe 100644
@@ -69,7 +69,7 @@ static struct dm_block_validator index_validator = {
  */
 #define BITMAP_CSUM_XOR 240779
 
-static void bitmap_prepare_for_write(struct dm_block_validator *v,
+static void md_bitmap_prepare_for_write(struct dm_block_validator *v,
 				     struct dm_block *b,
 				     size_t block_size)
 {
@@ -81,7 +81,7 @@ static void bitmap_prepare_for_write(struct dm_block_validator *v,
 						       BITMAP_CSUM_XOR));
 }
 
-static int bitmap_check(struct dm_block_validator *v,
+static int md_bitmap_check(struct dm_block_validator *v,
 			struct dm_block *b,
 			size_t block_size)
 {
@@ -124,7 +124,7 @@ static void *dm_bitmap_data(struct dm_block *b)
 
 #define WORD_MASK_HIGH 0xAAAAAAAAAAAAAAAAULL
 
-static unsigned bitmap_word_used(void *addr, unsigned b)
+static unsigned md_bitmap_word_used(void *addr, unsigned b)
 {
 	__le64 *words_le = addr;
 	__le64 *w_le = words_le + (b >> ENTRIES_SHIFT);
@@ -170,7 +170,7 @@ static int sm_find_free(void *addr, unsigned begin, unsigned end,
 {
 	while (begin < end) {
 		if (!(begin & (ENTRIES_PER_WORD - 1)) &&
-		    bitmap_word_used(addr, begin)) {
+		    md_bitmap_word_used(addr, begin)) {
 			begin += ENTRIES_PER_WORD;
 			continue;
 		}
drivers/md/raid1.c +10 -10
index 8e05c1092aef..ee6e0c26fada 100644
@@ -385,7 +385,7 @@ static void close_write(struct r1bio *r1_bio)
 		r1_bio->behind_master_bio = NULL;
 	}
 	/* clear the bitmap if all writes complete successfully */
-	bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
+	md_bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
 			r1_bio->sectors,
 			!test_bit(R1BIO_Degraded, &r1_bio->state),
 			test_bit(R1BIO_BehindIO, &r1_bio->state));
@@ -781,7 +781,7 @@ static int raid1_congested(struct mddev *mddev, int bits)
 static void flush_bio_list(struct r1conf *conf, struct bio *bio)
 {
 	/* flush any pending bitmap writes to disk before proceeding w/ I/O */
-	bitmap_unplug(conf->mddev->bitmap);
+	md_bitmap_unplug(conf->mddev->bitmap);
 	wake_up(&conf->wait_barrier);
 
 	while (bio) { /* submit pending writes */
@@ -1470,7 +1470,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 				alloc_behind_master_bio(r1_bio, bio);
 			}
 
-			bitmap_startwrite(bitmap, r1_bio->sector,
+			md_bitmap_startwrite(bitmap, r1_bio->sector,
 					  r1_bio->sectors,
 					  test_bit(R1BIO_BehindIO,
 						   &r1_bio->state));
@@ -1880,7 +1880,7 @@ static void end_sync_write(struct bio *bio)
 		long sectors_to_go = r1_bio->sectors;
 		/* make sure these bits doesn't get cleared. */
 		do {
-			bitmap_end_sync(mddev->bitmap, s,
+			md_bitmap_end_sync(mddev->bitmap, s,
 					&sync_blocks, 1);
 			s += sync_blocks;
 			sectors_to_go -= sync_blocks;
@@ -2626,12 +2626,12 @@ static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
 		 * We can find the current addess in mddev->curr_resync
 		 */
 		if (mddev->curr_resync < max_sector) /* aborted */
-			bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
+			md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
 						&sync_blocks, 1);
 		else /* completed sync */
 			conf->fullsync = 0;
 
-		bitmap_close_sync(mddev->bitmap);
+		md_bitmap_close_sync(mddev->bitmap);
 		close_sync(conf);
 
 		if (mddev_is_clustered(mddev)) {
@@ -2651,7 +2651,7 @@ static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
 	/* before building a request, check if we can skip these blocks..
 	 * This call the bitmap_start_sync doesn't actually record anything
 	 */
-	if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
+	if (!md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
 	    !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
 		/* We can skip this block, and probably several more */
 		*skipped = 1;
@@ -2669,7 +2669,7 @@ static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
 	 * sector_nr + two times RESYNC_SECTORS
 	 */
 
-	bitmap_cond_end_sync(mddev->bitmap, sector_nr,
+	md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
 		mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
 
 
@@ -2828,7 +2828,7 @@ static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
 		if (len == 0)
 			break;
 		if (sync_blocks == 0) {
-			if (!bitmap_start_sync(mddev->bitmap, sector_nr,
+			if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
 					       &sync_blocks, still_degraded) &&
 			    !conf->fullsync &&
 			    !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
@@ -3165,7 +3165,7 @@ static int raid1_resize(struct mddev *mddev, sector_t sectors)
 	    mddev->array_sectors > newsize)
 		return -EINVAL;
 	if (mddev->bitmap) {
-		int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
+		int ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
 		if (ret)
 			return ret;
 	}
drivers/md/raid10.c +13 -13
index 478cf446827f..2f71130ffd6c 100644
@@ -440,7 +440,7 @@ static void raid10_end_read_request(struct bio *bio)
 static void close_write(struct r10bio *r10_bio)
 {
 	/* clear the bitmap if all writes complete successfully */
-	bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
+	md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
 			r10_bio->sectors,
 			!test_bit(R10BIO_Degraded, &r10_bio->state),
 			0);
@@ -917,7 +917,7 @@ static void flush_pending_writes(struct r10conf *conf)
 		blk_start_plug(&plug);
 		/* flush any pending bitmap writes to disk
 		 * before proceeding w/ I/O */
-		bitmap_unplug(conf->mddev->bitmap);
+		md_bitmap_unplug(conf->mddev->bitmap);
 		wake_up(&conf->wait_barrier);
 
 		while (bio) { /* submit pending writes */
@@ -1102,7 +1102,7 @@ static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
 
 	/* we aren't scheduling, so we can do the write-out directly. */
 	bio = bio_list_get(&plug->pending);
-	bitmap_unplug(mddev->bitmap);
+	md_bitmap_unplug(mddev->bitmap);
 	wake_up(&conf->wait_barrier);
 
 	while (bio) { /* submit pending writes */
@@ -1519,7 +1519,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,
 	}
 
 	atomic_set(&r10_bio->remaining, 1);
-	bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
+	md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
 
 	for (i = 0; i < conf->copies; i++) {
 		if (r10_bio->devs[i].bio)
@@ -2991,12 +2991,12 @@ static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
 
 		if (mddev->curr_resync < max_sector) { /* aborted */
 			if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
-				bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
+				md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
 						&sync_blocks, 1);
 			else for (i = 0; i < conf->geo.raid_disks; i++) {
 				sector_t sect =
 					raid10_find_virt(conf, mddev->curr_resync, i);
-				bitmap_end_sync(mddev->bitmap, sect,
+				md_bitmap_end_sync(mddev->bitmap, sect,
 						&sync_blocks, 1);
 			}
 		} else {
@@ -3018,7 +3018,7 @@ static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
 			}
 			conf->fullsync = 0;
 		}
-		bitmap_close_sync(mddev->bitmap);
+		md_bitmap_close_sync(mddev->bitmap);
 		close_sync(conf);
 		*skipped = 1;
 		return sectors_skipped;
@@ -3112,7 +3112,7 @@ static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
 			 * we only need to recover the block if it is set in
 			 * the bitmap
 			 */
-			must_sync = bitmap_start_sync(mddev->bitmap, sect,
+			must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
 						      &sync_blocks, 1);
 			if (sync_blocks < max_sync)
 				max_sync = sync_blocks;
@@ -3158,7 +3158,7 @@ static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
 				}
 			}
 
-			must_sync = bitmap_start_sync(mddev->bitmap, sect,
+			must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
 						      &sync_blocks, still_degraded);
 
 			any_working = 0;
@@ -3335,12 +3335,12 @@ static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
 		 * safety reason, which ensures curr_resync_completed is
 		 * updated in bitmap_cond_end_sync.
 		 */
-		bitmap_cond_end_sync(mddev->bitmap, sector_nr,
+		md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
 				     mddev_is_clustered(mddev) &&
 				     (sector_nr + 2 * RESYNC_SECTORS >
 				      conf->cluster_sync_high));
 
-		if (!bitmap_start_sync(mddev->bitmap, sector_nr,
+		if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
 				       &sync_blocks, mddev->degraded) &&
 		    !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
 						 &mddev->recovery)) {
@@ -4015,7 +4015,7 @@ static int raid10_resize(struct mddev *mddev, sector_t sectors)
 	    mddev->array_sectors > size)
 		return -EINVAL;
 	if (mddev->bitmap) {
-		int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
+		int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
 		if (ret)
 			return ret;
 	}
@@ -4280,7 +4280,7 @@ static int raid10_start_reshape(struct mddev *mddev)
 	spin_unlock_irq(&conf->device_lock);
 
 	if (mddev->delta_disks && mddev->bitmap) {
-		ret = bitmap_resize(mddev->bitmap,
+		ret = md_bitmap_resize(mddev->bitmap,
 				    raid10_size(mddev, 0,
 						conf->geo.raid_disks),
 				    0, 0);
drivers/md/raid5-cache.c +1 -1
index 2b775abf377b..99f9081aefce 100644
@@ -324,7 +324,7 @@ void r5c_handle_cached_data_endio(struct r5conf *conf,
 		if (sh->dev[i].written) {
 			set_bit(R5_UPTODATE, &sh->dev[i].flags);
 			r5c_return_dev_pending_writes(conf, &sh->dev[i]);
-			bitmap_endwrite(conf->mddev->bitmap, sh->sector,
+			md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
 					STRIPE_SECTORS,
 					!test_bit(STRIPE_DEGRADED, &sh->state),
 					0);
drivers/md/raid5.c +12 -12
index 2031506a0ecd..8ce2c5467736 100644
@@ -3301,7 +3301,7 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
 		 */
 		set_bit(STRIPE_BITMAP_PENDING, &sh->state);
 		spin_unlock_irq(&sh->stripe_lock);
-		bitmap_startwrite(conf->mddev->bitmap, sh->sector,
+		md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
 				  STRIPE_SECTORS, 0);
 		spin_lock_irq(&sh->stripe_lock);
 		clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
@@ -3392,7 +3392,7 @@ handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
 			bi = nextbi;
 		}
 		if (bitmap_end)
-			bitmap_endwrite(conf->mddev->bitmap, sh->sector,
+			md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
 				STRIPE_SECTORS, 0, 0);
 		bitmap_end = 0;
 		/* and fail all 'written' */
@@ -3438,7 +3438,7 @@ handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
 			}
 		}
 		if (bitmap_end)
-			bitmap_endwrite(conf->mddev->bitmap, sh->sector,
+			md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
 					STRIPE_SECTORS, 0, 0);
 		/* If we were in the middle of a write the parity block might
 		 * still be locked - so just clear all R5_LOCKED flags
@@ -3779,7 +3779,7 @@ static void handle_stripe_clean_event(struct r5conf *conf,
 					bio_endio(wbi);
 					wbi = wbi2;
 				}
-				bitmap_endwrite(conf->mddev->bitmap, sh->sector,
+				md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
 						STRIPE_SECTORS,
 					 !test_bit(STRIPE_DEGRADED, &sh->state),
 						0);
@@ -5539,7 +5539,7 @@ static void make_discard_request(struct mddev *mddev, struct bio *bi)
 			for (d = 0;
 			     d < conf->raid_disks - conf->max_degraded;
 			     d++)
-				bitmap_startwrite(mddev->bitmap,
+				md_bitmap_startwrite(mddev->bitmap,
 						  sh->sector,
 						  STRIPE_SECTORS,
 						  0);
@@ -6020,11 +6020,11 @@ static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_n
 		}
 
 		if (mddev->curr_resync < max_sector) /* aborted */
-			bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
+			md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
 					&sync_blocks, 1);
 		else /* completed sync */
 			conf->fullsync = 0;
-		bitmap_close_sync(mddev->bitmap);
+		md_bitmap_close_sync(mddev->bitmap);
 
 		return 0;
 	}
@@ -6053,7 +6053,7 @@ static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_n
 	}
 	if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
 	    !conf->fullsync &&
-	    !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
+	    !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
 	    sync_blocks >= STRIPE_SECTORS) {
 		/* we can skip this block, and probably more */
 		sync_blocks /= STRIPE_SECTORS;
@@ -6061,7 +6061,7 @@ static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_n
 		return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
 	}
 
-	bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
+	md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
 
 	sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
 	if (sh == NULL) {
@@ -6084,7 +6084,7 @@ static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_n
 	}
 	rcu_read_unlock();
 
-	bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
+	md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
 
 	set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
 	set_bit(STRIPE_HANDLE, &sh->state);
@@ -6285,7 +6285,7 @@ static void raid5d(struct md_thread *thread)
 			/* Now is a good time to flush some bitmap updates */
 			conf->seq_flush++;
 			spin_unlock_irq(&conf->device_lock);
-			bitmap_unplug(mddev->bitmap);
+			md_bitmap_unplug(mddev->bitmap);
 			spin_lock_irq(&conf->device_lock);
 			conf->seq_write = conf->seq_flush;
 			activate_bit_delay(conf, conf->temp_inactive_list);
@@ -7741,7 +7741,7 @@ static int raid5_resize(struct mddev *mddev, sector_t sectors)
 	    mddev->array_sectors > newsize)
 		return -EINVAL;
 	if (mddev->bitmap) {
-		int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
+		int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v2 1/5] md: Avoid namespace collision with bitmap API

I love your patch! Yet something to improve:

Hi Andy,

I love your patch! Yet something to improve:

[auto build test ERROR on next-20180615]
[also build test ERROR on v4.17]
[cannot apply to md/for-next linus/master dm/for-next v4.17 v4.17-rc7 v4.17-rc6] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Andy-Shevchenko/bitmap-Introduce-alloc-free-helpers/20180615-214724
config: i386-randconfig-b0-06141412 (attached as .config)
compiler: gcc-4.9 (Debian 4.9.4-2) 4.9.4 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

>> drivers/md/persistent-data/dm-space-map-common.c:111:23: error: 'bitmap_prepare_for_write' undeclared here (not in a function)
     .prepare_for_write = bitmap_prepare_for_write,
                          ^
>> drivers/md/persistent-data/dm-space-map-common.c:112:11: error: 'bitmap_check' undeclared here (not in a function)

.check = bitmap_check
^
drivers/md/persistent-data/dm-space-map-common.c:72:13: warning: 'md_bitmap_prepare_for_write' defined but not used [-Wunused-function]
static void md_bitmap_prepare_for_write(struct dm_block_validator *v,
^
drivers/md/persistent-data/dm-space-map-common.c:84:12: warning: 'md_bitmap_check' defined but not used [-Wunused-function]
static int md_bitmap_check(struct dm_block_validator *v,
^

vim +/bitmap_prepare_for_write +111 drivers/md/persistent-data/dm-space-map-common.c

3241b1d3 Joe Thornber 2011-10-31  108  
3241b1d3 Joe Thornber 2011-10-31  109  static struct dm_block_validator dm_sm_bitmap_validator = {
3241b1d3 Joe Thornber 2011-10-31  110  	.name = "sm_bitmap",
3241b1d3 Joe Thornber 2011-10-31 @111  	.prepare_for_write = bitmap_prepare_for_write,
3241b1d3 Joe Thornber 2011-10-31 @112  	.check = bitmap_check
3241b1d3 Joe Thornber 2011-10-31  113  };

3241b1d3 Joe Thornber 2011-10-31 114

:::::: The code at line 111 was first introduced by commit
:::::: 3241b1d3e0aaafbfcd320f4d71ade629728cc4f4 dm: add persistent data library

:::::: TO: Joe Thornber <[email protected]>
:::::: CC: Alasdair G Kergon <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw

Re: [PATCH v2 1/5] md: Avoid namespace collision with bitmap API

Quoted 3 lines · kbuild test robot

Hi Andy,

I love your patch! Yet something to improve:

Thanks!

I fixed that locally (definitely that module wasn't compiled by default). Though I would wait to gather more comments before sending v3.

Quoted 63 lines

[auto build test ERROR on next-20180615]
[also build test ERROR on v4.17]
[cannot apply to md/for-next linus/master dm/for-next v4.17 v4.17-rc7 v4.17-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Andy-Shevchenko/bitma
p-Introduce-alloc-free-helpers/20180615-214724
config: i386-randconfig-b0-06141412 (attached as .config)
compiler: gcc-4.9 (Debian 4.9.4-2) 4.9.4 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

Quoted 2 lines
Quoted 2 lines

drivers/md/persistent-data/dm-space-map-common.c:111:23: error:
'bitmap_prepare_for_write' undeclared here (not in a function)

.prepare_for_write = bitmap_prepare_for_write,
^

Quoted 2 lines
Quoted 2 lines

drivers/md/persistent-data/dm-space-map-common.c:112:11: error:
'bitmap_check' undeclared here (not in a function)

.check = bitmap_check
^
drivers/md/persistent-data/dm-space-map-common.c:72:13: warning:
'md_bitmap_prepare_for_write' defined but not used [-Wunused-function]
static void md_bitmap_prepare_for_write(struct dm_block_validator
*v,

                ^

drivers/md/persistent-data/dm-space-map-common.c:84:12: warning:
'md_bitmap_check' defined but not used [-Wunused-function]
static int md_bitmap_check(struct dm_block_validator *v,
^

vim +/bitmap_prepare_for_write +111 drivers/md/persistent-data/dm- space-map-common.c

3241b1d3 Joe Thornber 2011-10-31 108 3241b1d3 Joe Thornber 2011-10-31 109 static struct dm_block_validator dm_sm_bitmap_validator = {
3241b1d3 Joe Thornber 2011-10-31 110 .name = "sm_bitmap",
3241b1d3 Joe Thornber 2011-10-31 @111 .prepare_for_write =
bitmap_prepare_for_write,
3241b1d3 Joe Thornber 2011-10-31 @112 .check = bitmap_check
3241b1d3 Joe Thornber 2011-10-31 113 }; 3241b1d3 Joe Thornber 2011-10-31 114

:::::: The code at line 111 was first introduced by commit :::::: 3241b1d3e0aaafbfcd320f4d71ade629728cc4f4 dm: add persistent data library

:::::: TO: Joe Thornber <[email protected]>
:::::: CC: Alasdair G Kergon <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology
Center
https://lists.01.org/pipermail/kbuild-all Intel
Corporation

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#4 lore raw

Re: [PATCH v2 1/5] md: Avoid namespace collision with bitmap API

Quoted 10 lines · Andy Shevchenko
Quoted 3 lines · kbuild test robot

Hi Andy,

I love your patch! Yet something to improve:

Thanks!

I fixed that locally (definitely that module wasn't compiled by default). Though I would wait to gather more comments before sending v3.

Hi,

DM is not MD. drivers/md/persistent-data/* is only used by DM.

Please rename all the drivers/md/persistent-data/ changes to use a "dm_bitmap" prefix.

Thanks,
Mike

Quoted 71 lines
Quoted 62 lines

[auto build test ERROR on next-20180615]
[also build test ERROR on v4.17]
[cannot apply to md/for-next linus/master dm/for-next v4.17 v4.17-rc7 v4.17-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Andy-Shevchenko/bitma
p-Introduce-alloc-free-helpers/20180615-214724
config: i386-randconfig-b0-06141412 (attached as .config)
compiler: gcc-4.9 (Debian 4.9.4-2) 4.9.4 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

Quoted 2 lines
Quoted 2 lines

drivers/md/persistent-data/dm-space-map-common.c:111:23: error:
'bitmap_prepare_for_write' undeclared here (not in a function)

.prepare_for_write = bitmap_prepare_for_write,
^

Quoted 2 lines
Quoted 2 lines

drivers/md/persistent-data/dm-space-map-common.c:112:11: error:
'bitmap_check' undeclared here (not in a function)

.check = bitmap_check
^
drivers/md/persistent-data/dm-space-map-common.c:72:13: warning:
'md_bitmap_prepare_for_write' defined but not used [-Wunused-function]
static void md_bitmap_prepare_for_write(struct dm_block_validator
*v,

                ^

drivers/md/persistent-data/dm-space-map-common.c:84:12: warning:
'md_bitmap_check' defined but not used [-Wunused-function]
static int md_bitmap_check(struct dm_block_validator *v,
^

vim +/bitmap_prepare_for_write +111 drivers/md/persistent-data/dm- space-map-common.c

3241b1d3 Joe Thornber 2011-10-31 108 3241b1d3 Joe Thornber 2011-10-31 109 static struct dm_block_validator dm_sm_bitmap_validator = {
3241b1d3 Joe Thornber 2011-10-31 110 .name = "sm_bitmap",
3241b1d3 Joe Thornber 2011-10-31 @111 .prepare_for_write =
bitmap_prepare_for_write,
3241b1d3 Joe Thornber 2011-10-31 @112 .check = bitmap_check
3241b1d3 Joe Thornber 2011-10-31 113 }; 3241b1d3 Joe Thornber 2011-10-31 114

:::::: The code at line 111 was first introduced by commit :::::: 3241b1d3e0aaafbfcd320f4d71ade629728cc4f4 dm: add persistent data library

:::::: TO: Joe Thornber <[email protected]>
:::::: CC: Alasdair G Kergon <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology
Center
https://lists.01.org/pipermail/kbuild-all Intel
Corporation

Signature · 7 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
#5 lore raw

Re: [PATCH v2 1/5] md: Avoid namespace collision with bitmap API

In reply to Andy Shevchenko
I love your patch! Yet something to improve:

Hi Andy,

I love your patch! Yet something to improve:

[auto build test ERROR on next-20180615]
[also build test ERROR on v4.17]
[cannot apply to md/for-next linus/master dm/for-next v4.17 v4.17-rc7 v4.17-rc6] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Andy-Shevchenko/bitmap-Introduce-alloc-free-helpers/20180615-214724
config: x86_64-randconfig-x008-201823 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

>> drivers/md/persistent-data/dm-space-map-common.c:111:23: error: 'bitmap_prepare_for_write' undeclared here (not in a function); did you mean 'md_bitmap_prepare_for_write'?
     .prepare_for_write = bitmap_prepare_for_write,
                          ^~~~~~~~~~~~~~~~~~~~~~~~
                          md_bitmap_prepare_for_write
>> drivers/md/persistent-data/dm-space-map-common.c:112:11: error: 'bitmap_check' undeclared here (not in a function); did you mean 'md_bitmap_check'?

.check = bitmap_check
^~~~~~~~~~~~
md_bitmap_check
drivers/md/persistent-data/dm-space-map-common.c:84:12: warning: 'md_bitmap_check' defined but not used [-Wunused-function]
static int md_bitmap_check(struct dm_block_validator *v,
^~~~~~~~~~~~~~~
drivers/md/persistent-data/dm-space-map-common.c:72:13: warning: 'md_bitmap_prepare_for_write' defined but not used [-Wunused-function]
static void md_bitmap_prepare_for_write(struct dm_block_validator *v,
^~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +111 drivers/md/persistent-data/dm-space-map-common.c

3241b1d3 Joe Thornber 2011-10-31  108  
3241b1d3 Joe Thornber 2011-10-31  109  static struct dm_block_validator dm_sm_bitmap_validator = {
3241b1d3 Joe Thornber 2011-10-31  110  	.name = "sm_bitmap",
3241b1d3 Joe Thornber 2011-10-31 @111  	.prepare_for_write = bitmap_prepare_for_write,
3241b1d3 Joe Thornber 2011-10-31 @112  	.check = bitmap_check
3241b1d3 Joe Thornber 2011-10-31  113  };

3241b1d3 Joe Thornber 2011-10-31 114

:::::: The code at line 111 was first introduced by commit
:::::: 3241b1d3e0aaafbfcd320f4d71ade629728cc4f4 dm: add persistent data library

:::::: TO: Joe Thornber <[email protected]>
:::::: CC: Alasdair G Kergon <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#6 lore raw

PATCH v2 2/5 bitmap: Drop unnecessary 0 check for u32 array operations

In reply to Andy Shevchenko

The nbits == 0 is safe to be supplied to the function body, so, remove unnecessary checks in bitmap_to_arr32() and bitmap_from_arr32().

---
 lib/bitmap.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
lib/bitmap.c +1 -8
index 58f9750e49c6..33e95cd359a2 100644
@@ -1132,14 +1132,10 @@ EXPORT_SYMBOL(bitmap_copy_le);
  *	@buf: array of u32 (in host byte order), the source bitmap
  *	@nbits: number of bits in @bitmap
  */
-void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
-						unsigned int nbits)
+void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
 {
 	unsigned int i, halfwords;
 
-	if (!nbits)
-		return;
-
 	halfwords = DIV_ROUND_UP(nbits, 32);
 	for (i = 0; i < halfwords; i++) {
 		bitmap[i/2] = (unsigned long) buf[i];
@@ -1163,9 +1159,6 @@ void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
 {
 	unsigned int i, halfwords;
 
-	if (!nbits)
-		return;
-
 	halfwords = DIV_ROUND_UP(nbits, 32);
 	for (i = 0; i < halfwords; i++) {
 		buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
Signature · 1 line
2.17.1
#7 lore raw

PATCH v2 3/5 bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()

In reply to Andy Shevchenko

A lot of code become ugly because of open coding allocations for bitmaps.

Introduce three helpers to allow users be more clear of intention and keep their code neat.

---
 include/linux/bitmap.h |  8 ++++++++
 lib/bitmap.c           | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)
include/linux/bitmap.h +8 -0
index 1ee46f492267..acf5e8df3504 100644
@@ -104,6 +104,14 @@
  * contain all bit positions from 0 to 'bits' - 1.
  */
 
+/*
+ * Allocation and deallocation of bitmap.
+ * Provided in lib/bitmap.c to avoid circular dependency.
+ */
+extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
+extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
+extern void bitmap_free(const unsigned long *bitmap);
+
 /*
  * lib/bitmap.c provides these functions:
  */
lib/bitmap.c +19 -0
index 33e95cd359a2..09acf2fd6a35 100644
@@ -13,6 +13,7 @@
 #include <linux/bitops.h>
 #include <linux/bug.h>
 #include <linux/kernel.h>
+#include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/uaccess.h>
 
@@ -1125,6 +1126,24 @@ void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int n
 EXPORT_SYMBOL(bitmap_copy_le);
 #endif
 
+unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
+{
+	return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long), flags);
+}
+EXPORT_SYMBOL(bitmap_alloc);
+
+unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
+{
+	return bitmap_alloc(nbits, flags | __GFP_ZERO);
+}
+EXPORT_SYMBOL(bitmap_zalloc);
+
+void bitmap_free(const unsigned long *bitmap)
+{
+	kfree(bitmap);
+}
+EXPORT_SYMBOL(bitmap_free);
+
 #if BITS_PER_LONG == 64
 /**
  * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
Signature · 1 line
2.17.1
#8 lore raw

Re: [PATCH v2 3/5] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()

Hi Andy,

Quoted 4 lines · Andy Shevchenko

A lot of code become ugly because of open coding allocations for bitmaps.

Introduce three helpers to allow users be more clear of intention and keep their code neat.

I like the idea. But in following patches you switch to new API only couple of drivers. I think, it worth to switch more, especially core users to make new API visible for developers. Brief grepping for candidates showse only 17 suspected places:
yury:linux$ git grep BITS_TO_LONGS | grep alloc | grep -v drivers | grep -v arch
kernel/events/uprobes.c:1188: area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
kernel/sysctl.c:3004: tmp_bitmap = kzalloc(BITS_TO_LONGS(bitmap_len) * sizeof(unsigned long),
lib/genalloc.c:188: BITS_TO_LONGS(nbits) * sizeof(long);
lib/test_printf.c:378: unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
mm/percpu.c:1109: chunk->alloc_map = memblock_virt_alloc(BITS_TO_LONGS(region_bits) *
mm/percpu.c:1111: chunk->bound_map = memblock_virt_alloc(BITS_TO_LONGS(region_bits + 1) *
mm/percpu.c:1170: chunk->alloc_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits) *
mm/percpu.c:1175: chunk->bound_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits + 1) *
mm/slub.c:3668: unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
mm/slub.c:4435: unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
mm/slub.c:4596: unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
mm/swapfile.c:3219: frontswap_map = kvzalloc(BITS_TO_LONGS(maxpages) * sizeof(long),
net/bridge/br_if.c:328: inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
net/bridge/br_vlan.c:836: changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
net/mac80211/mesh_plink.c:465: aid_map = kcalloc(BITS_TO_LONGS(IEEE80211_MAX_AID + 1),
net/sched/cls_u32.c:737: unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long),
tools/include/linux/bitmap.h:105: return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));

Yury

Quoted 65 lines
---
 include/linux/bitmap.h |  8 ++++++++
 lib/bitmap.c           | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)
include/linux/bitmap.h +8 -0
index 1ee46f492267..acf5e8df3504 100644
@@ -104,6 +104,14 @@
  * contain all bit positions from 0 to 'bits' - 1.
  */
 
+/*
+ * Allocation and deallocation of bitmap.
+ * Provided in lib/bitmap.c to avoid circular dependency.
+ */
+extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
+extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
+extern void bitmap_free(const unsigned long *bitmap);
+
 /*
  * lib/bitmap.c provides these functions:
  */
lib/bitmap.c +19 -1
index 33e95cd359a2..09acf2fd6a35 100644
@@ -13,6 +13,7 @@
 #include <linux/bitops.h>
 #include <linux/bug.h>
 #include <linux/kernel.h>
+#include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/uaccess.h>
 
@@ -1125,6 +1126,24 @@ void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int n
 EXPORT_SYMBOL(bitmap_copy_le);
 #endif
 
+unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
+{
+       return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long), flags);
+}
+EXPORT_SYMBOL(bitmap_alloc);
+
+unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
+{
+       return bitmap_alloc(nbits, flags | __GFP_ZERO);
+}
+EXPORT_SYMBOL(bitmap_zalloc);
+
+void bitmap_free(const unsigned long *bitmap)
+{
+       kfree(bitmap);
+}
+EXPORT_SYMBOL(bitmap_free);
+
 #if BITS_PER_LONG == 64
 /**
  * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
--

2.17.1

#9 lore raw

Re: [PATCH v2 3/5] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()

Quoted 8 lines · Yury Norov
Quoted 4 lines · Andy Shevchenko

A lot of code become ugly because of open coding allocations for bitmaps.

Introduce three helpers to allow users be more clear of intention and keep their code neat.

I like the idea. But in following patches you switch to new API only couple of drivers.

I have converted much more, indeed.
I published only for couple of drivers as an example. This is I mentioned in cover letter.

Quoted 3 lines

I think, it worth to switch more, especially core users to make new API visible for developers. Brief grepping for candidates showse only 17 suspected places:

Yes, but not in this series. Since it covers many subsystems and drivers the applying that kind of series would take ages. That's why I have decided to submit for one subsystem only.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

PATCH v2 4/5 Input: gpio-keys - Switch to bitmap_zalloc()

In reply to Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

---
 drivers/input/keyboard/gpio_keys.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/input/keyboard/gpio_keys.c +4 -4
index 052e37675086..492a971b95b5 100644
@@ -196,7 +196,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
 	ssize_t ret;
 	int i;
 
-	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
+	bits = bitmap_zalloc(n_events, GFP_KERNEL);
 	if (!bits)
 		return -ENOMEM;
 
@@ -216,7 +216,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
 	buf[ret++] = '\n';
 	buf[ret] = '\0';
 
-	kfree(bits);
+	bitmap_free(bits);
 
 	return ret;
 }
@@ -240,7 +240,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
 	ssize_t error;
 	int i;
 
-	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
+	bits = bitmap_zalloc(n_events, GFP_KERNEL);
 	if (!bits)
 		return -ENOMEM;
 
@@ -284,7 +284,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
 	mutex_unlock(&ddata->disable_lock);
 
 out:
-	kfree(bits);
+	bitmap_free(bits);
 	return error;
 }
 
Signature · 1 line
2.17.1
#11 lore raw

Re: [PATCH v2 4/5] Input: gpio-keys - Switch to bitmap_zalloc()

Quoted 4 lines · Andy Shevchenko

External Email

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

Nit. There's no special type for bitmaps, bitmap_zalloc() returns long *. Is it less opaque than void *, not sure.

Quoted 48 lines
---
 drivers/input/keyboard/gpio_keys.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/input/keyboard/gpio_keys.c +4 -5
index 052e37675086..492a971b95b5 100644
@@ -196,7 +196,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
        ssize_t ret;
        int i;
 
-       bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
+       bits = bitmap_zalloc(n_events, GFP_KERNEL);
        if (!bits)
                return -ENOMEM;
 
@@ -216,7 +216,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
        buf[ret++] = '\n';
        buf[ret] = '\0';
 
-       kfree(bits);
+       bitmap_free(bits);
 
        return ret;
 }
@@ -240,7 +240,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
        ssize_t error;
        int i;
 
-       bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
+       bits = bitmap_zalloc(n_events, GFP_KERNEL);
        if (!bits)
                return -ENOMEM;
 
@@ -284,7 +284,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
        mutex_unlock(&ddata->disable_lock);
 
 out:
-       kfree(bits);
+       bitmap_free(bits);
        return error;
 }
 
--

2.17.1

#12 lore raw

Re: [PATCH v2 4/5] Input: gpio-keys - Switch to bitmap_zalloc()

Quoted 9 lines · Yury Norov
Quoted 4 lines · Andy Shevchenko

External Email

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

Nit. There's no special type for bitmaps, bitmap_zalloc() returns long *. Is it less opaque than void *, not sure.

Yes, it enables type checking by compiler, which is good thing. You can't supply other pointer to this API, like

unsigned char *bar = kmalloc(BITS_PER_LONGS(nbits) * sizeof(unsigned long), GFP_KERNEL);
...

bitmap_free(bar); //<< compiler will not pass this silently!

Or other way around:

unsigned char *foo = bitmap_alloc(nbits, GFP_KERNEL); //<< wrong types!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw

PATCH v2 5/5 Input: evdev - Switch to bitmap_zalloc()

In reply to Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

---
 drivers/input/evdev.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/input/evdev.c +7 -9
index c81c79d01d93..370206f987f9 100644
@@ -481,7 +481,7 @@ static int evdev_release(struct inode *inode, struct file *file)
 	evdev_detach_client(evdev, client);
 
 	for (i = 0; i < EV_CNT; ++i)
-		kfree(client->evmasks[i]);
+		bitmap_free(client->evmasks[i]);
 
 	kvfree(client);
 
@@ -925,17 +925,15 @@ static int evdev_handle_get_val(struct evdev_client *client,
 {
 	int ret;
 	unsigned long *mem;
-	size_t len;
 
-	len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
-	mem = kmalloc(len, GFP_KERNEL);
+	mem = bitmap_alloc(maxbit, GFP_KERNEL);
 	if (!mem)
 		return -ENOMEM;
 
 	spin_lock_irq(&dev->event_lock);
 	spin_lock(&client->buffer_lock);
 
-	memcpy(mem, bits, len);
+	bitmap_copy(mem, bits, maxbit);
 
 	spin_unlock(&dev->event_lock);
 
@@ -947,7 +945,7 @@ static int evdev_handle_get_val(struct evdev_client *client,
 	if (ret < 0)
 		evdev_queue_syn_dropped(client);
 
-	kfree(mem);
+	bitmap_free(mem);
 
 	return ret;
 }
@@ -1003,13 +1001,13 @@ static int evdev_set_mask(struct evdev_client *client,
 	if (!cnt)
 		return 0;
 
-	mask = kcalloc(sizeof(unsigned long), BITS_TO_LONGS(cnt), GFP_KERNEL);
+	mask = bitmap_zalloc(cnt, GFP_KERNEL);
 	if (!mask)
 		return -ENOMEM;
 
 	error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
 	if (error < 0) {
-		kfree(mask);
+		bitmap_free(mask);
 		return error;
 	}
 
@@ -1018,7 +1016,7 @@ static int evdev_set_mask(struct evdev_client *client,
 	client->evmasks[type] = mask;
 	spin_unlock_irqrestore(&client->buffer_lock, flags);
 
-	kfree(oldmask);
+	bitmap_free(oldmask);
 
 	return 0;
 }
Signature · 1 line
2.17.1
#14 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Hi Andy,

Quoted 23 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

---
 drivers/input/evdev.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/input/evdev.c +1 -1
index c81c79d01d93..370206f987f9 100644
@@ -481,7 +481,7 @@ static int evdev_release(struct inode *inode, struct file *file)
        evdev_detach_client(evdev, client);
 
        for (i = 0; i < EV_CNT; ++i)
-               kfree(client->evmasks[i]);
+               bitmap_free(client->evmasks[i]);
 
        kvfree(client);
 

drivers/input/evdev.c static int evdev_handle_get_val(struct evdev_client *client, · line 931

Quoted 10 lines
drivers/input/evdev.c +1 -3
@@ -925,17 +925,15 @@ static int evdev_handle_get_val(struct evdev_client *client,
 {
        int ret;
        unsigned long *mem;
-       size_t len;
 
-       len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
-       mem = kmalloc(len, GFP_KERNEL);
+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

Quoted 19 lines
drivers/input/evdev.c +2 -2
        spin_lock_irq(&dev->event_lock);
        spin_lock(&client->buffer_lock);
 
-       memcpy(mem, bits, len);
+       bitmap_copy(mem, bits, maxbit);
 
        spin_unlock(&dev->event_lock);
 
@@ -947,7 +945,7 @@ static int evdev_handle_get_val(struct evdev_client *client,
        if (ret < 0)
                evdev_queue_syn_dropped(client);
 
-       kfree(mem);
+       bitmap_free(mem);
 
        return ret;
 }

drivers/input/evdev.c static int evdev_set_mask(struct evdev_client *client, · line 1008

Quoted 9 lines
drivers/input/evdev.c +1 -1
@@ -1003,13 +1001,13 @@ static int evdev_set_mask(struct evdev_client *client,
        if (!cnt)
                return 0;
 
-       mask = kcalloc(sizeof(unsigned long), BITS_TO_LONGS(cnt), GFP_KERNEL);
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;
 
        error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);

If my understanding of bits_from_user() correct, here you can also use bitmap_alloc(), true?

Quoted 17 lines
drivers/input/evdev.c +2 -3
        if (error < 0) {
-               kfree(mask);
+               bitmap_free(mask);
                return error;
        }
 
@@ -1018,7 +1016,7 @@ static int evdev_set_mask(struct evdev_client *client,
        client->evmasks[type] = mask;
        spin_unlock_irqrestore(&client->buffer_lock, flags);
 
-       kfree(oldmask);
+       bitmap_free(oldmask);
 
        return 0;
 }
--

2.17.1

#15 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Quoted 5 lines · Yury Norov
Quoted 2 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

Quoted 7 lines
Quoted 3 lines
+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

There are two places, one with alloc, another with zalloc. I will clarify this in commit message of next version.

Quoted 8 lines
Quoted 5 lines
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;

        error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);

If my understanding of bits_from_user() correct, here you can also use bitmap_alloc(), true?

While it might be true, it's a material for separate change. Original code uses zalloc version.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#16 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Quoted 10 lines · Andy Shevchenko
Quoted 9 lines · Yury Norov
Quoted 7 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;
Quoted 15 lines
Quoted 3 lines

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

There are two places, one with alloc, another with zalloc. I will clarify this in commit message of next version.

Quoted 8 lines
Quoted 5 lines
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;

        error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);

If my understanding of bits_from_user() correct, here you can also use bitmap_alloc(), true?

Also it might be useful to have a separate bitmap_from_user to alloc and copy.

#17 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Quoted 28 lines · Joe Perches
Quoted 27 lines · Andy Shevchenko
Quoted 7 lines · Yury Norov
Quoted 4 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating.
Besides that it returns pointer of bitmap type instead of opaque void *.

Quoted 7 lines
Quoted 3 lines
+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

There are two places, one with alloc, another with zalloc. I will clarify this in commit message of next version.

Quoted 7 lines
Quoted 6 lines
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;

        error = bits_from_user(mask, cnt - 1, codes_size, codes,

compat);

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

If my understanding of bits_from_user() correct, here you can also use
bitmap_alloc(), true?

Also it might be useful to have a separate bitmap_from_user to alloc and copy.

Maybe. I didn't check if there are such users except this driver.

Anyway, it's out of scope of the series.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#18 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Quoted 33 lines · Andy Shevchenko
Quoted 32 lines · Joe Perches
Quoted 30 lines · Andy Shevchenko
Quoted 7 lines · Yury Norov
Quoted 4 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating.
Besides that it returns pointer of bitmap type instead of opaque void *.

Quoted 7 lines
Quoted 3 lines
+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

There are two places, one with alloc, another with zalloc. I will clarify this in commit message of next version.

Quoted 10 lines
Quoted 6 lines
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;

        error = bits_from_user(mask, cnt - 1, codes_size, codes,

compat);

If my understanding of bits_from_user() correct, here you can also use
bitmap_alloc(), true?

Quoted 6 lines
Quoted 2 lines

Also it might be useful to have a separate bitmap_from_user to alloc and copy.

Maybe. I didn't check if there are such users except this driver.

Anyway, it's out of scope of the series.

That seems incorrect as you are introducing alloc/free helpers.

Perhaps bitmap_dup_user [or some better name] could or should be one of the helpers.

#19 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Quoted 37 lines · Joe Perches
Quoted 36 lines · Andy Shevchenko
Quoted 34 lines · Joe Perches
Quoted 13 lines · Andy Shevchenko
Quoted 11 lines · Yury Norov
Quoted 9 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating.
Besides that it returns pointer of bitmap type instead of opaque void *.

+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;
Quoted 17 lines
Quoted 3 lines

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

There are two places, one with alloc, another with zalloc. I will clarify this in commit message of next version.

Quoted 10 lines
Quoted 6 lines
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;

        error = bits_from_user(mask, cnt - 1, codes_size, codes,

compat);

If my understanding of bits_from_user() correct, here you can also use
bitmap_alloc(), true?

Also it might be useful to have a separate bitmap_from_user to alloc and copy.

Quoted 8 lines
Quoted 3 lines

Maybe. I didn't check if there are such users except this driver.

Anyway, it's out of scope of the series.

That seems incorrect as you are introducing alloc/free helpers.

Perhaps bitmap_dup_user [or some better name] could or should be one of the helpers.

Can you help with estimation how many existing users need this kind of functionality? One of them evdev, which has an open coded variant.

Also, pay attention to that fact we have already bitmap_parse_user() and bitmap_parlelist_user() which are called by several users.

If the estimation will show something like 3+, it would definitely make sense, otherwise, I wouldn't like spend time on it.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#20 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Quoted 6 lines · Andy Shevchenko
Quoted 2 lines · Joe Perches

Perhaps bitmap_dup_user [or some better name] could or should be one of the helpers.

Can you help with estimation how many existing users need this kind of functionality? One of them evdev, which has an open coded variant.

My estimation via cocci script below is 1 existing user, so it's almost certainly not worthwhile.

$ cat copy_from_user.cocci 
@@
unsigned long *p;
expression e1, e2;
@@

* copy_from_user(p, e1, e2)

maintainer
#21 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

In reply to Yury Norov
Quoted 68 lines · Yury Norov

Hi Andy,

Quoted 23 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

---
 drivers/input/evdev.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/input/evdev.c +1 -1
index c81c79d01d93..370206f987f9 100644
@@ -481,7 +481,7 @@ static int evdev_release(struct inode *inode, struct file *file)
        evdev_detach_client(evdev, client);
 
        for (i = 0; i < EV_CNT; ++i)
-               kfree(client->evmasks[i]);
+               bitmap_free(client->evmasks[i]);
 
        kvfree(client);
 

drivers/input/evdev.c static int evdev_handle_get_val(struct evdev_client *client, · line 931

Quoted 10 lines
drivers/input/evdev.c +1 -3
@@ -925,17 +925,15 @@ static int evdev_handle_get_val(struct evdev_client *client,
 {
        int ret;
        unsigned long *mem;
-       size_t len;
 
-       len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
-       mem = kmalloc(len, GFP_KERNEL);
+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

Quoted 27 lines
drivers/input/evdev.c +3 -3
        spin_lock_irq(&dev->event_lock);
        spin_lock(&client->buffer_lock);
 
-       memcpy(mem, bits, len);
+       bitmap_copy(mem, bits, maxbit);
 
        spin_unlock(&dev->event_lock);
 
@@ -947,7 +945,7 @@ static int evdev_handle_get_val(struct evdev_client *client,
        if (ret < 0)
                evdev_queue_syn_dropped(client);
 
-       kfree(mem);
+       bitmap_free(mem);
 
        return ret;
 }
@@ -1003,13 +1001,13 @@ static int evdev_set_mask(struct evdev_client *client,
        if (!cnt)
                return 0;
 
-       mask = kcalloc(sizeof(unsigned long), BITS_TO_LONGS(cnt), GFP_KERNEL);
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;
 
Quoted 4 lines

drivers/input/evdev.c static int evdev_set_mask(struct evdev_client *client, · line 1008

Quoted 1 line
drivers/input/evdev.c +0 -0
        error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);

If my understanding of bits_from_user() correct, here you can also use bitmap_alloc(), true?

bits_from_user() copies as much as user supplied, we want to zero out the tail to make sure there is no garbage, so we want to use kcalloc/kzalloc/bitmap_zalloc here.

Thanks.

Signature · 1 line
Dmitry
#22 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Quoted 71 lines · Dmitry Torokhov

External Email

Quoted 68 lines · Yury Norov

Hi Andy,

Quoted 23 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

---
 drivers/input/evdev.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/input/evdev.c +1 -1
index c81c79d01d93..370206f987f9 100644
@@ -481,7 +481,7 @@ static int evdev_release(struct inode *inode, struct file *file)
        evdev_detach_client(evdev, client);
 
        for (i = 0; i < EV_CNT; ++i)
-               kfree(client->evmasks[i]);
+               bitmap_free(client->evmasks[i]);
 
        kvfree(client);
 

drivers/input/evdev.c static int evdev_handle_get_val(struct evdev_client *client, · line 931

Quoted 10 lines
drivers/input/evdev.c +1 -3
@@ -925,17 +925,15 @@ static int evdev_handle_get_val(struct evdev_client *client,
 {
        int ret;
        unsigned long *mem;
-       size_t len;
 
-       len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
-       mem = kmalloc(len, GFP_KERNEL);
+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

Quoted 27 lines
drivers/input/evdev.c +3 -3
        spin_lock_irq(&dev->event_lock);
        spin_lock(&client->buffer_lock);
 
-       memcpy(mem, bits, len);
+       bitmap_copy(mem, bits, maxbit);
 
        spin_unlock(&dev->event_lock);
 
@@ -947,7 +945,7 @@ static int evdev_handle_get_val(struct evdev_client *client,
        if (ret < 0)
                evdev_queue_syn_dropped(client);
 
-       kfree(mem);
+       bitmap_free(mem);
 
        return ret;
 }
@@ -1003,13 +1001,13 @@ static int evdev_set_mask(struct evdev_client *client,
        if (!cnt)
                return 0;
 
-       mask = kcalloc(sizeof(unsigned long), BITS_TO_LONGS(cnt), GFP_KERNEL);
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;
 
Quoted 8 lines
Quoted 4 lines

drivers/input/evdev.c static int evdev_set_mask(struct evdev_client *client, · line 1008

Quoted 1 line
drivers/input/evdev.c +0 -0
        error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);

If my understanding of bits_from_user() correct, here you can also use bitmap_alloc(), true?

bits_from_user() copies as much as user supplied, we want to zero out the tail to make sure there is no garbage, so we want to use kcalloc/kzalloc/bitmap_zalloc here.

I don't understand that. Tail bits of bitmap (i.e. after last used bit till the end of last word) are always ignored by kernel code and there's no matter what was stored in that bits.

(With the exception of copying bitmap from kernel to userspace. For this case we have bitmap_copy_clear_tail() to avoid unintended exposing kernel data to user.)

If you know any bitmap function that don't ignore tail bits, this is a bug and should be fixed.

By the way, bits_from_user() is bad-designed because it takes 2 size arguments - maxbit and maxlen, and should be reworked. There's a single user of this function, and I suspect, it can be switched to existing core API, like bitmap_from_arr32().

Yury

maintainer
#23 lore raw

Re: [PATCH v2 5/5] Input: evdev - Switch to bitmap_zalloc()

Quoted 77 lines · Yury Norov
Quoted 76 lines · Dmitry Torokhov

External Email

Quoted 72 lines · Yury Norov

Hi Andy,

Quoted 23 lines · Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating. Besides that it returns pointer of bitmap type instead of opaque void *.

---
 drivers/input/evdev.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/input/evdev.c +1 -1
index c81c79d01d93..370206f987f9 100644
@@ -481,7 +481,7 @@ static int evdev_release(struct inode *inode, struct file *file)
        evdev_detach_client(evdev, client);
 
        for (i = 0; i < EV_CNT; ++i)
-               kfree(client->evmasks[i]);
+               bitmap_free(client->evmasks[i]);
 
        kvfree(client);
 

drivers/input/evdev.c static int evdev_handle_get_val(struct evdev_client *client, · line 931

Quoted 10 lines
drivers/input/evdev.c +1 -3
@@ -925,17 +925,15 @@ static int evdev_handle_get_val(struct evdev_client *client,
 {
        int ret;
        unsigned long *mem;
-       size_t len;
 
-       len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
-       mem = kmalloc(len, GFP_KERNEL);
+       mem = bitmap_alloc(maxbit, GFP_KERNEL);
        if (!mem)
                return -ENOMEM;

But in commit message you say you switch to bitmap_zalloc(). IIUC bitmap_alloc() is OK here. But could you please update comment to avoid confusing.

Quoted 19 lines
drivers/input/evdev.c +2 -2
        spin_lock_irq(&dev->event_lock);
        spin_lock(&client->buffer_lock);
 
-       memcpy(mem, bits, len);
+       bitmap_copy(mem, bits, maxbit);
 
        spin_unlock(&dev->event_lock);
 
@@ -947,7 +945,7 @@ static int evdev_handle_get_val(struct evdev_client *client,
        if (ret < 0)
                evdev_queue_syn_dropped(client);
 
-       kfree(mem);
+       bitmap_free(mem);
 
        return ret;
 }

drivers/input/evdev.c static int evdev_set_mask(struct evdev_client *client, · line 1008

Quoted 9 lines
drivers/input/evdev.c +1 -1
@@ -1003,13 +1001,13 @@ static int evdev_set_mask(struct evdev_client *client,
        if (!cnt)
                return 0;
 
-       mask = kcalloc(sizeof(unsigned long), BITS_TO_LONGS(cnt), GFP_KERNEL);
+       mask = bitmap_zalloc(cnt, GFP_KERNEL);
        if (!mask)
                return -ENOMEM;
 
        error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);

If my understanding of bits_from_user() correct, here you can also use bitmap_alloc(), true?

Quoted 7 lines
Quoted 3 lines

bits_from_user() copies as much as user supplied, we want to zero out the tail to make sure there is no garbage, so we want to use kcalloc/kzalloc/bitmap_zalloc here.

I don't understand that. Tail bits of bitmap (i.e. after last used bit till the end of last word) are always ignored by kernel code and there's no matter what was stored in that bits.

Users can supply as little as one long word worth of data (codes_size = maxlen = 4). You really do not want the rest of the mask you will be applying to contain random heap garbage.

Quoted 5 lines

(With the exception of copying bitmap from kernel to userspace. For this case we have bitmap_copy_clear_tail() to avoid unintended exposing kernel data to user.)

Quoted 7 lines

If you know any bitmap function that don't ignore tail bits, this is a bug and should be fixed.

By the way, bits_from_user() is bad-designed because it takes 2 size arguments - maxbit and maxlen, and should be reworked. There's a single user of this function, and I suspect, it can be switched to existing core API, like bitmap_from_arr32().

I'm afraid you suspect wrong, as (unfortunately, but it is ABI now) we are not dealing with masks consisting of u32 or u64 elements, but "unsigned long" elements, which change size depending on 32/64 bit architecture and whether we are dealing with compat or native userspace.

It also needs both maxbit and maxlen, because one is kernel's limit while the other is limit from userspace POV and you need to reconcile both to make sure you do not overrun buffers on either side.

Thanks.

Signature · 1 line
Dmitry