Skip to contentSkip to conversation

mdadm: Replace obsolete usleep with nanosleep

PATCH Applied Mateusz Grzonka 2 participants 2 messages Aug 12, 2022 – Aug 22, 2022 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mdadm: Replace obsolete usleep with nanosleep

According to POSIX.1-2001, usleep is considered obsolete. Replace it with a wrapper that uses nanosleep, as recommended in man. Add handy macros for conversions between msec, usec and nsec.

---
 Assemble.c    |  2 +-
 Grow.c        |  4 ++--
 Manage.c      | 10 +++++-----
 managemon.c   |  8 ++++----
 mdadm.h       |  4 ++++
 mdmon.c       |  4 ++--
 super-intel.c |  6 +++---
 util.c        | 42 +++++++++++++++++++++++++++++++++---------
 8 files changed, 54 insertions(+), 26 deletions(-)
Assemble.c +1 -1
index 9eac9ce0..1f14175d 100644
@@ -1949,7 +1949,7 @@ out:
 						break;
 					close(mdfd);
 				}
-				usleep(usecs);
+				sleep_for(0, USEC_TO_NSEC(usecs), true);
 				usecs <<= 1;
 			}
 		}
Grow.c +2 -2
index 8a242b0f..6610423a 100644
@@ -954,7 +954,7 @@ int start_reshape(struct mdinfo *sra, int already_running,
 			err = sysfs_set_str(sra, NULL, "sync_action",
 					    "reshape");
 			if (err)
-				sleep(1);
+				sleep_for(1, 0, true);
 		} while (err && errno == EBUSY && cnt-- > 0);
 	}
 	return err;
@@ -5048,7 +5048,7 @@ int Grow_continue_command(char *devname, int fd,
 			}
 			st->ss->getinfo_super(st, content, NULL);
 			if (!content->reshape_active)
-				sleep(3);
+				sleep_for(3, 0, true);
 			else
 				break;
 		} while (cnt-- > 0);
Manage.c +5 -5
index f789e0c1..150a684c 100644
@@ -244,7 +244,7 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
 					    "array_state",
 					    "inactive")) < 0 &&
 		       errno == EBUSY) {
-			usleep(200000);
+			sleep_for(0, MSEC_TO_NSEC(200), true);
 			count--;
 		}
 		if (err) {
@@ -328,7 +328,7 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
 		       sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) == 0) {
 			/* must be in the critical section - wait a bit */
 			delay -= 1;
-			usleep(100000);
+			sleep_for(0, MSEC_TO_NSEC(100), true);
 		}
 
 		if (sysfs_set_str(mdi, NULL, "sync_action", "frozen") != 0)
@@ -405,7 +405,7 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
 				 * quite started yet.  Wait a bit and
 				 * check  'sync_action' to see.
 				 */
-				usleep(10000);
+				sleep_for(0, MSEC_TO_NSEC(10), true);
 				sysfs_get_str(mdi, NULL, "sync_action", buf, sizeof(buf));
 				if (strncmp(buf, "reshape", 7) != 0)
 					break;
@@ -447,7 +447,7 @@ done:
 	count = 25; err = 0;
 	while (count && fd >= 0 &&
 	       (err = ioctl(fd, STOP_ARRAY, NULL)) < 0 && errno == EBUSY) {
-		usleep(200000);
+		sleep_for(0, MSEC_TO_NSEC(200), true);
 		count --;
 	}
 	if (fd >= 0 && err) {
@@ -1105,7 +1105,7 @@ int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
 				ret = sysfs_unique_holder(devnm, rdev);
 				if (ret < 2)
 					break;
-				usleep(100 * 1000);	/* 100ms */
+				sleep_for(0, MSEC_TO_NSEC(100), true);
 			} while (--count > 0);
 
 			if (ret == 0) {
managemon.c +4 -4
index 0e9bdf00..a7bfa8f6 100644
@@ -207,7 +207,7 @@ static void replace_array(struct supertype *container,
 	remove_old();
 	while (pending_discard) {
 		while (discard_this == NULL)
-			sleep(1);
+			sleep_for(1, 0, true);
 		remove_old();
 	}
 	pending_discard = old;
@@ -568,7 +568,7 @@ static void manage_member(struct mdstat_ent *mdstat,
 		updates = NULL;
 		while (update_queue_pending || update_queue) {
 			check_update_queue(container);
-			usleep(15*1000);
+			sleep_for(0, MSEC_TO_NSEC(15), true);
 		}
 		replace_array(container, a, newa);
 		if (sysfs_set_str(&a->info, NULL,
@@ -822,7 +822,7 @@ static void handle_message(struct supertype *container, struct metadata_update *
 	if (msg->len <= 0)
 		while (update_queue_pending || update_queue) {
 			check_update_queue(container);
-			usleep(15*1000);
+			sleep_for(0, MSEC_TO_NSEC(15), true);
 		}
 
 	if (msg->len == 0) { /* ping_monitor */
@@ -836,7 +836,7 @@ static void handle_message(struct supertype *container, struct metadata_update *
 		wakeup_monitor();
 
 		while (monitor_loop_cnt - cnt < 0)
-			usleep(10 * 1000);
+			sleep_for(0, MSEC_TO_NSEC(10), true);
 	} else if (msg->len == -1) { /* ping_manager */
 		struct mdstat_ent *mdstat = mdstat_read(1, 0);
 
mdadm.h +4 -0
index 09915a00..1c3bc9e3 100644
@@ -1707,6 +1707,10 @@ extern int cluster_get_dlmlock(void);
 extern int cluster_release_dlmlock(void);
 extern void set_dlm_hooks(void);
 
+#define MSEC_TO_NSEC(msec) ((msec) * 1000000)
+#define USEC_TO_NSEC(usec) ((usec) * 1000)
+extern void sleep_for(unsigned int sec, long nsec, bool wake_after_interrupt);
+
 #define _ROUND_UP(val, base)	(((val) + (base) - 1) & ~(base - 1))
 #define ROUND_UP(val, base)	_ROUND_UP(val, (typeof(val))(base))
 #define ROUND_UP_PTR(ptr, base)	((typeof(ptr)) \
mdmon.c +2 -2
index 5570574b..2f8e676f 100644
@@ -99,7 +99,7 @@ static int clone_monitor(struct supertype *container)
 	if (rc)
 		return rc;
 	while (mon_tid == -1)
-		usleep(10);
+		sleep_for(0, USEC_TO_NSEC(10), true);
 	pthread_attr_destroy(&attr);
 
 	mgr_tid = syscall(SYS_gettid);
@@ -209,7 +209,7 @@ static void try_kill_monitor(pid_t pid, char *devname, int sock)
 		rv = kill(pid, SIGUSR1);
 		if (rv < 0)
 			break;
-		usleep(200000);
+		sleep_for(0, MSEC_TO_NSEC(200), true);
 	}
 }
 
super-intel.c +3 -3
index ba3bd41f..65c42089 100644
@@ -5220,7 +5220,7 @@ static int get_super_block(struct intel_super **super_list, char *devnm, char *d
 	/* retry the load if we might have raced against mdmon */
 	if (err == 3 && devnm && mdmon_running(devnm))
 		for (retry = 0; retry < 3; retry++) {
-			usleep(3000);
+			sleep_for(0, MSEC_TO_NSEC(3), true);
 			err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
 			if (err != 3)
 				break;
@@ -5322,7 +5322,7 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
 
 		if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
 			for (retry = 0; retry < 3; retry++) {
-				usleep(3000);
+				sleep_for(0, MSEC_TO_NSEC(3), true);
 				rv = load_and_parse_mpb(fd, super, devname, 0);
 				if (rv != 3)
 					break;
@@ -11991,7 +11991,7 @@ int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
 				close(fd);
 				return 1;
 			}
-			usleep(30000);
+			sleep_for(0, MSEC_TO_NSEC(30), true);
 		} else
 			break;
 	} while (retry--);
util.c +33 -9

87 lines not on this page · raw message

Signature · 1 line
2.26.2
Applied
#1 lore raw
Quoted 5 lines · Mateusz Grzonka

According to POSIX.1-2001, usleep is considered obsolete. Replace it with a wrapper that uses nanosleep, as recommended in man. Add handy macros for conversions between msec, usec and nsec.

Quoted 11 lines
---
 Assemble.c    |  2 +-
 Grow.c        |  4 ++--
 Manage.c      | 10 +++++-----
 managemon.c   |  8 ++++----
 mdadm.h       |  4 ++++
 mdmon.c       |  4 ++--
 super-intel.c |  6 +++---
 util.c        | 42 +++++++++++++++++++++++++++++++++---------
 8 files changed, 54 insertions(+), 26 deletions(-)

Applied!

Thanks,
Jes