Skip to contentSkip to conversation

multipath-tools: asynchronous checker framework

PATCH In review Martin Wilck 4 participants 30 messages May 22 – Jun 2 Device mapper

lore
Share & tools
lore
#0 lore raw

v2 cover multipath-tools: asynchronous checker framework

Based on my previous patch set [1], which introduced the "runner" thread handling code as an abstract implementation of the asynchronous checker logic in the TUR checker, this set introduces a generic "async_checker" type and converts all existing checkers except directio to this framework. With this set applied, all checkers run asyncrhonously unless explicitly forbidden to do so with the "force_sync" option.

Patch 1-3 are minor preparations. Patch 4-8 introduce a general concept of a refcounted "shared" pointer and use it to replace multiple instances of hard-coded refcounts on objects. Patch 9 converts the current TUR code into a generic asynchronous checker type by separating the actual TUR code from the handling of the async runners. Patch 10 adds support for this async checker type in the checkers code by searching for "libcheck_async_func" with dlsym() in checker DSOs. Patch 11-15 convert all checkers except emc_clariion to the async_checker framework. emc_clariion is special because it needs extra context both for the checker itself and for multipath-related information. Patch 16 and 17 add the respective additional fields to the async_checker framework. The mpcontext handling is rewritten, because the current apporach of handling void ** pointers would be disastrous in scenarious with truly asynchronous checker threads that may hang forever. Patch 18 finally converts emc_clariion, using the concepts from patch 16 and 17.

Comments and reviews welcome.

Regards,
Martin

[1] https://lore.kernel.org/dm-devel/[email protected]/T/#m9369993a81c82b10ef42896505d4a354b6be3f79

Changes v1 -> v2 (suggestions by Ben Marzinski)

- Patch 05: rework the shared_ptr test
- Patch 06: new, separate patch for removing libcheck_tread code; remove libcheck_thread and start_checker_thread() from checkers.h Following patches are renumbered
- Patch 10 (was 09): add alignment for checker_ctx field in struct runner_data remove the unused chkr field from struct async_checker_context
- Patch 12: remove struct checker_context from checkers.h,
remove unused MSG_TUR_FAILED,
- patch 16: rename checker symbols to "libcheck_async_context_size" and "libcheck_async_init"
- Patch 17 (was 12): Try to set TAS bit in rdac checker (libcheck_async_init()); this requires moving it after patch 16

Martin Wilck (19):
libmpathutil: runner: reduce a message loglevel
libmultipath: checkers: add two generic checker messages
libmultipath: checkers: move checker_class definition to checkers.h
libmpathutil: add implementation of generic shared pointer multipath-tools tests: add tests for shared pointer code
libmultipath: remove libcheck_thread related code
libmultipath: use shared_ptr for checker classes
libmultipath: use shared_ptr for prioritizers
libmpathutil: runner: use shared_ptr
libmultipath: add generic async path checker code
libmultipath: checkers: add support for async checker
libmultipath: convert TUR checker to an async checker instance
libmultipath: convert cciss_tur checker to async checker
libmultipath: convert readsector0 checker to async checker
libmultipath: convert hp_sw checker to async checker
libmultipath: async_checker: add context_size and init symbols
libmultipath: convert RDAC checker to async checker
libmultipath: checkers: rework mpcontext passing
libmultipath: convert emc_clariion to async_checker

 libmpathutil/libmpathutil.version    |   7 +
 libmpathutil/runner.c                |  32 ++--
 libmpathutil/util.c                  |  45 ++++-
 libmpathutil/util.h                  |   5 +
 libmultipath/Makefile                |   2 +-
 libmultipath/async_checker.c         | 249 ++++++++++++++++++++++++++
 libmultipath/async_checker.h         |  36 ++++
 libmultipath/checkers.c              | 236 +++++++++----------------
 libmultipath/checkers.h              |  79 +++++----
 libmultipath/checkers/cciss_tur.c    |  41 ++---
 libmultipath/checkers/directio.c     |   6 +-
 libmultipath/checkers/emc_clariion.c | 104 +++++------
 libmultipath/checkers/hp_sw.c        |  32 +---
 libmultipath/checkers/rdac.c         |  38 ++--
 libmultipath/checkers/readsector0.c  |  28 +--
 libmultipath/checkers/tur.c          | 253 ++-------------------------
 libmultipath/discovery.c             |   8 +-
 libmultipath/libmultipath.version    |   3 +-
 libmultipath/prio.c                  |  47 +++--
 libmultipath/prio.h                  |   1 -
 libmultipath/structs.c               |   3 +-
 libmultipath/structs.h               |   3 +-
 tests/Makefile                       |   4 +-
 tests/directio.c                     |   2 +-
 tests/shared_ptr.c                   | 120 +++++++++++++
 25 files changed, 728 insertions(+), 656 deletions(-)
 create mode 100644 libmultipath/async_checker.c
 create mode 100644 libmultipath/async_checker.h
 create mode 100644 tests/shared_ptr.c
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/19 libmpathutil: runner: reduce a message loglevel

Don't "log runner 0x7b724a07a480 cancelled in state 'done'" at level 3. It's a fairly normal thing to happen.

---
 libmpathutil/runner.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmpathutil/runner.c +1 -1
index da57ff0..8c6d6b9 100644
@@ -134,7 +134,7 @@ repeat:
 		break;
 	case RUNNER_DONE:
 		st_new = RUNNER_DEAD;
-		/* fallthrough */
+		break;
 	case RUNNER_DEAD:
 		level = 3;
 		break;
Signature · 1 line
2.54.0
#2 lore raw

PATCH v2 2/19 libmultipath: checkers: add two generic checker messages

In reply to Martin Wilck

Add generic messages for "timed out" and "still running". They will be needed by follow-up patches.

---
 libmultipath/checkers.c | 2 ++
 libmultipath/checkers.h | 2 ++
 2 files changed, 4 insertions(+)
libmultipath/checkers.c +2 -0
index bb6ad1e..8a90ade 100644
@@ -365,6 +365,8 @@ static const char *generic_msg[CHECKER_GENERIC_MSGTABLE_SIZE] = {
 	[CHECKER_MSGID_GHOST] = " reports path is ghost",
 	[CHECKER_MSGID_UNSUPPORTED] = " doesn't support this device",
 	[CHECKER_MSGID_DISCONNECTED] = " no access to this device",
+	[CHECKER_MSGID_TIMEOUT] = " timed out",
+	[CHECKER_MSGID_RUNNING] = " still running",
 };
 
 const char *checker_message(const struct checker *c)
libmultipath/checkers.h +2 -0
index a969e7d..2317cd1 100644
@@ -124,6 +124,8 @@ enum {
 	CHECKER_MSGID_GHOST,
 	CHECKER_MSGID_UNSUPPORTED,
 	CHECKER_MSGID_DISCONNECTED,
+	CHECKER_MSGID_TIMEOUT,
+	CHECKER_MSGID_RUNNING,
 	CHECKER_GENERIC_MSGTABLE_SIZE,
 	CHECKER_FIRST_MSGID = 100,   /* lowest msgid for checkers */
 	CHECKER_MSGTABLE_SIZE = 100, /* max msg table size for checkers */
Signature · 1 line
2.54.0
#3 lore raw

PATCH v2 3/19 libmultipath: checkers: move checker_class definition to checkers.h

In reply to Martin Wilck

Make the contents of struct checker_class accessible to other code.

---
 libmultipath/checkers.c | 18 ------------------
 libmultipath/checkers.h | 20 +++++++++++++++++++-
 2 files changed, 19 insertions(+), 19 deletions(-)
libmultipath/checkers.c +0 -18
index 8a90ade..8f18ca6 100644
@@ -14,24 +14,6 @@
 
 static const char * const checker_dir = MULTIPATH_DIR;
 
-struct checker_class {
-	struct list_head node;
-	void *handle;
-	int refcount;
-	int sync;
-	char name[CHECKER_NAME_LEN];
-	int (*check)(struct checker *);
-	int (*init)(struct checker *);       /* to allocate the context */
-	int (*mp_init)(struct checker *);    /* to allocate the mpcontext */
-	void (*free)(struct checker *);      /* to free the context */
-	void (*reset)(void);		     /* to reset the global variables */
-	void *(*thread)(void *);	     /* async thread entry point */
-	int (*pending)(struct checker *);    /* to recheck pending paths */
-	bool (*need_wait)(struct checker *); /* checker needs waiting for */
-	const char **msgtable;
-	short msgtable_size;
-};
-
 static const char *checker_state_names[PATH_MAX_STATE] = {
 	[PATH_WILD] = "wild",
 	[PATH_UNCHECKED] = "unchecked",
libmultipath/checkers.h +19 -1
index 2317cd1..694dfa3 100644
@@ -131,7 +131,25 @@ enum {
 	CHECKER_MSGTABLE_SIZE = 100, /* max msg table size for checkers */
 };
 
-struct checker_class;
+struct checker;
+struct checker_class {
+	struct list_head node;
+	void *handle;
+	int refcount;
+	int sync;
+	char name[CHECKER_NAME_LEN];
+	int (*check)(struct checker *);
+	int (*init)(struct checker *);	  /* to allocate the context */
+	int (*mp_init)(struct checker *); /* to allocate the mpcontext */
+	void (*free)(struct checker *);	  /* to free the context */
+	void (*reset)(void);		  /* to reset the global variables */
+	void *(*thread)(void *);	  /* async thread entry point */
+	int (*pending)(struct checker *); /* to recheck pending paths */
+	bool (*need_wait)(struct checker *); /* checker needs waiting for */
+	const char **msgtable;
+	short msgtable_size;
+};
+
 struct checker {
 	struct checker_class *cls;
 	int fd;
Signature · 1 line
2.54.0
#4 lore raw

PATCH v2 4/19 libmpathutil: add implementation of generic shared pointer

In reply to Martin Wilck

Add a set of simple functions to handle refcounted pointers.

---
 libmpathutil/libmpathutil.version |  7 +++++
 libmpathutil/util.c               | 45 ++++++++++++++++++++++++++++---
 libmpathutil/util.h               |  5 ++++
 3 files changed, 53 insertions(+), 4 deletions(-)
libmpathutil/libmpathutil.version +7 -0
index c69120d..51594ad 100644
@@ -194,3 +194,10 @@ global:
 local:
 	*;
 };
+
+LIBMPATHUTIL_6.1 {
+global:
+	alloc_shared_ptr;
+	get_shared_ptr;
+	put_shared_ptr;
+} LIBMPATHUTIL_6.0;
libmpathutil/util.c +41 -4

59 lines not on this page · raw message

libmpathutil/util.h +5 -0
index aed1bc1..ffbb182 100644
@@ -160,4 +160,9 @@ void cleanup_charp(char **p);
 void cleanup_ucharp(unsigned char **p);
 void cleanup_udev_device(struct udev_device **udd);
 void cleanup_bitfield(union bitfield **p);
+
+void *alloc_shared_ptr(size_t size, void (*destructor)(void *));
+void get_shared_ptr(void *ptr);
+void put_shared_ptr(void *ptr);
+
 #endif /* UTIL_H_INCLUDED */
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH v2 04/19] libmpathutil: add implementation of generic shared pointer

Quoted 10 lines · Martin Wilck

Add a set of simple functions to handle refcounted pointers.

---
  libmpathutil/libmpathutil.version |  7 +++++
  libmpathutil/util.c               | 45 ++++++++++++++++++++++++++++---
  libmpathutil/util.h               |  5 ++++
  3 files changed, 53 insertions(+), 4 deletions(-)

That really looks like the Hazard pointers from Paul McK and Mathieu Desnoyers. Maybe have a look and see if you cannot re-use the approach here.

Quoted 70 lines
libmpathutil/libmpathutil.version +7 -0
index c69120d..51594ad 100644
@@ -194,3 +194,10 @@ global:
  local:
  	*;
  };
+
+LIBMPATHUTIL_6.1 {
+global:
+	alloc_shared_ptr;
+	get_shared_ptr;
+	put_shared_ptr;
+} LIBMPATHUTIL_6.0;
libmpathutil/util.c +31 -4
index 23a9797..3c623ec 100644
@@ -12,15 +12,13 @@
  #include <dirent.h>
  #include <unistd.h>
  #include <errno.h>
+#include <urcu/uatomic.h>
  #include "mt-udev-wrap.h"
 
  #include "util.h"
  #include "debug.h"
-#include "checkers.h"
  #include "vector.h"
-#include "structs.h"
-#include "config.h"
-#include "log.h"
+#include "list.h"
 
  size_t
  strchop(char *str)
@@ -384,3 +382,42 @@ void cleanup_udev_device(struct udev_device **udd)
  	if (*udd)
  		udev_device_unref(*udd);
  }
+
+struct shared_ptr {
+	long refcnt;
+	void (*destructor)(void *);
+	char __attribute__((aligned(sizeof(void *)))) ptr[];
+};
+
+void *alloc_shared_ptr(size_t size, void (*destructor)(void *))
+{
+	struct shared_ptr *sp = malloc(sizeof(*sp) + size);
+
+	if (!sp)
+		return NULL;
+	uatomic_set(&sp->refcnt, 1);
+	sp->destructor = destructor;
+	return sp->ptr;
+}
+
+void get_shared_ptr(void *ptr)
+{
+	struct shared_ptr *sp = container_of(ptr, struct shared_ptr, ptr);
+
+	if (uatomic_add_return(&sp->refcnt, 1) < 0)
+		condlog(0, "%s: refcount overflow", __func__);
+}
+
+void put_shared_ptr(void *ptr)
+{
+	struct shared_ptr *sp;

libmpathutil/util.c void cleanup_udev_device(struct udev_device **udd) · line 423

Quoted 10 lines
libmpathutil/util.c +10 -0
+
+	if (!ptr)
+		return;
+	sp = container_of(ptr, struct shared_ptr, ptr);
+	if (uatomic_sub_return(&sp->refcnt, 1) == 0) {
+		if (sp->destructor)
+			sp->destructor(ptr);
+		free(sp);
+	}
+}

I _think_ the could be implemented better using RCU primitives; in the end, what you really care about is whether the pointer is valid (9which is what RCU provides).
Or indeed use the hazard pointer approach, and count the number of RCU references to that object ...

Quoted 5 lines
libmpathutil/util.h +0 -0
index aed1bc1..ffbb182 100644

libmpathutil/util.h void cleanup_charp(char **p); · line 168

Quoted 9 lines
libmpathutil/util.h +5 -0
@@ -160,4 +160,9 @@ void cleanup_charp(char **p);
  void cleanup_ucharp(unsigned char **p);
  void cleanup_udev_device(struct udev_device **udd);
  void cleanup_bitfield(union bitfield **p);
+
+void *alloc_shared_ptr(size_t size, void (*destructor)(void *));
+void get_shared_ptr(void *ptr);
+void put_shared_ptr(void *ptr);
+
  #endif /* UTIL_H_INCLUDED */

... or just leave it as it is if the approach is to complex. I doubt it's time-critical.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#6 lore raw

Re: [PATCH v2 04/19] libmpathutil: add implementation of generic shared pointer

Hi Hannes,

Quoted 12 lines · Hannes Reinecke
Quoted 11 lines · Martin Wilck

Add a set of simple functions to handle refcounted pointers.

---
  libmpathutil/libmpathutil.version |  7 +++++
  libmpathutil/util.c               | 45
++++++++++++++++++++++++++++---
  libmpathutil/util.h               |  5 ++++

  3 files changed, 53 insertions(+), 4 deletions(-)
Quoted 3 lines

That really looks like the Hazard pointers from Paul McK and Mathieu Desnoyers. Maybe have a look and see if you cannot re-use the approach here.

Thanks for the hint. Hazard pointers are a far more advanced complicated concept, focussed on cache efficiency and performance. That's important for the kernel, but it would be over-engineered for libmultipath, IMO. We just need to make sure that the pointer is not freed as long as it's still referenced somewhere.

AFAICS, Mathieu's code is targeted at the kernel. We could consider using some generic user-space hazard pointer implementation for libmultipath if there is something we could take off the shelf (like, as a part of liburcu, which we're using anyway). But I don't see a strong need for it. There's no doubt that libmultipath is inefficient in many ways, but it isn't CPU-intensive, and thus correctness and simplicity matter more than efficiency.

Quoted 80 lines
Quoted 80 lines
libmpathutil/libmpathutil.version +0 -0
index c69120d..51594ad 100644
@@ -194,3 +194,10 @@ global:

  local:
   *;
  };

libmpathutil/libmpathutil.version +7 -0
+
+LIBMPATHUTIL_6.1 {
+global:
+	alloc_shared_ptr;
+	get_shared_ptr;
+	put_shared_ptr;
+} LIBMPATHUTIL_6.0;
libmpathutil/util.c +1 -0
index 23a9797..3c623ec 100644
@@ -12,15 +12,13 @@
   #include <dirent.h>
   #include <unistd.h>
   #include <errno.h>
+#include <urcu/uatomic.h>
   #include "mt-udev-wrap.h"


  #include "util.h"
  #include "debug.h"
-#include "checkers.h"
  #include "vector.h"

libmpathutil/util.c +1 -3
-#include "structs.h"
-#include "config.h"
-#include "log.h"
+#include "list.h"


  size_t
  strchop(char *str)

libmpathutil/util.c +0 -0
@@ -384,3 +382,42 @@ void cleanup_udev_device(struct udev_device
 **udd)
   	if (*udd)
   		udev_device_unref(*udd);

  }

libmpathutil/util.c +21 -0
+
+struct shared_ptr {
+	long refcnt;
+	void (*destructor)(void *);
+	char __attribute__((aligned(sizeof(void *)))) ptr[];
+};
+
+void *alloc_shared_ptr(size_t size, void (*destructor)(void *))
+{
+	struct shared_ptr *sp = malloc(sizeof(*sp) + size);
+
+	if (!sp)
+		return NULL;
+	uatomic_set(&sp->refcnt, 1);
+	sp->destructor = destructor;
+	return sp->ptr;
+}
+
+void get_shared_ptr(void *ptr)
+{
+	struct shared_ptr *sp = container_of(ptr, struct

shared_ptr, ptr);

libmpathutil/util.c +14 -0
+
+	if (uatomic_add_return(&sp->refcnt, 1) < 0)
+		condlog(0, "%s: refcount overflow", __func__);
+}
+
+void put_shared_ptr(void *ptr)
+{
+	struct shared_ptr *sp;
+
+	if (!ptr)
+		return;
+	sp = container_of(ptr, struct shared_ptr, ptr);
+	if (uatomic_sub_return(&sp->refcnt, 1) == 0) {
+		if (sp->destructor)
Quoted 8 lines

libmpathutil/util.c void cleanup_udev_device(struct udev_device **udd) · line 423

Quoted 4 lines
libmpathutil/util.c +4 -0
+			sp->destructor(ptr);
+		free(sp);
+	}
+}

I _think_ the could be implemented better using RCU primitives; in the end, what you really care about is whether the pointer is valid (9which is what RCU provides).

What we want to achieve is just that (the destructor is called and) the memory is freed when the last reference is dropped, but no sooner. It's the same concept that the kernel uses for refcounted objects all over the place.

Quoted 2 lines

Or indeed use the hazard pointer approach, and count the number of RCU references to that object ...

Like I said, I'd like to keep it simple. The basic refcounting that this patch implements does the job for libmultipath.

I just created this admittedly simple abstraction because the multipath code was using reference counters in multiple places, and I wanted to consolidate the code and add a unit test for it. It's not the purpose of this patch set to optimize for performance. If we want to optimize it, the API is simple enough that we can substitute the implementation easily with something more efficient in the future.

Quoted 11 lines
Quoted 11 lines
libmpathutil/util.h +1 -0
index aed1bc1..ffbb182 100644
@@ -160,4 +160,9 @@ void cleanup_charp(char **p);
   void cleanup_ucharp(unsigned char **p);
   void cleanup_udev_device(struct udev_device **udd);
   void cleanup_bitfield(union bitfield **p);
+
Quoted 9 lines

libmpathutil/util.h void cleanup_charp(char **p); · line 168

Quoted 5 lines
libmpathutil/util.h +4 -0
+void *alloc_shared_ptr(size_t size, void (*destructor)(void *));
+void get_shared_ptr(void *ptr);
+void put_shared_ptr(void *ptr);
+
   #endif /* UTIL_H_INCLUDED */

... or just leave it as it is if the approach is to complex. I doubt it's time-critical.

Exactly. I'd like to see what Ben and others think.

Regards,
Martin

Quoted 3 lines

Cheers,

Hannes

#7 lore raw

Re: [PATCH v2 04/19] libmpathutil: add implementation of generic shared pointer

In reply to Martin Wilck
Quoted 28 lines · Martin Wilck

Add a set of simple functions to handle refcounted pointers.

---
 libmpathutil/libmpathutil.version |  7 +++++
 libmpathutil/util.c               | 45 ++++++++++++++++++++++++++++---
 libmpathutil/util.h               |  5 ++++
 3 files changed, 53 insertions(+), 4 deletions(-)
libmpathutil/util.c +10 -0
index 23a9797..3c623ec 100644
@@ -384,3 +382,42 @@ void cleanup_udev_device(struct udev_device **udd)
 	if (*udd)
 		udev_device_unref(*udd);
 }
+
+struct shared_ptr {
+	long refcnt;
+	void (*destructor)(void *);
+	char __attribute__((aligned(sizeof(void *)))) ptr[];
+};
+
+void *alloc_shared_ptr(size_t size, void (*destructor)(void *))
+{
+	struct shared_ptr *sp = malloc(sizeof(*sp) + size);

libmpathutil/util.c void cleanup_udev_device(struct udev_device **udd) · line 404

Quoted 10 lines
libmpathutil/util.c +10 -0
+
+	if (!sp)
+		return NULL;
+	uatomic_set(&sp->refcnt, 1);
+	sp->destructor = destructor;
+	return sp->ptr;
+}
+
+void get_shared_ptr(void *ptr)
+{

We should probably return here if ptr is NULL, like we do in put_shared_ptr(). Alternatively, this might be a reasonable place for an assert(), since nobody should be calling these with NULL pointers.

As far as the general question Hannes raise about this implentantion, I'm happy with it as-is.

-Ben

Quoted 19 lines
libmpathutil/util.c +19 -0
+	struct shared_ptr *sp = container_of(ptr, struct shared_ptr, ptr);
+
+	if (uatomic_add_return(&sp->refcnt, 1) < 0)
+		condlog(0, "%s: refcount overflow", __func__);
+}
+
+void put_shared_ptr(void *ptr)
+{
+	struct shared_ptr *sp;
+
+	if (!ptr)
+		return;
+	sp = container_of(ptr, struct shared_ptr, ptr);
+	if (uatomic_sub_return(&sp->refcnt, 1) == 0) {
+		if (sp->destructor)
+			sp->destructor(ptr);
+		free(sp);
+	}
+}
#8 lore raw

Re: [PATCH v2 04/19] libmpathutil: add implementation of generic shared pointer

Quoted 38 lines · Benjamin Marzinski
Quoted 37 lines · Martin Wilck

Add a set of simple functions to handle refcounted pointers.

---
 libmpathutil/libmpathutil.version |  7 +++++
 libmpathutil/util.c               | 45
++++++++++++++++++++++++++++---
 libmpathutil/util.h               |  5 ++++

 3 files changed, 53 insertions(+), 4 deletions(-)
libmpathutil/util.c +0 -0
index 23a9797..3c623ec 100644
@@ -384,3 +382,42 @@ void cleanup_udev_device(struct udev_device
 **udd)
  	if (*udd)
  		udev_device_unref(*udd);

 }

libmpathutil/util.c +17 -0
+
+struct shared_ptr {
+	long refcnt;
+	void (*destructor)(void *);
+	char __attribute__((aligned(sizeof(void *)))) ptr[];
+};
+
+void *alloc_shared_ptr(size_t size, void (*destructor)(void *))
+{
+	struct shared_ptr *sp = malloc(sizeof(*sp) + size);
+
+	if (!sp)
+		return NULL;
+	uatomic_set(&sp->refcnt, 1);
+	sp->destructor = destructor;
+	return sp->ptr;
+}
Quoted 8 lines

libmpathutil/util.c void cleanup_udev_device(struct udev_device **udd) · line 404

Quoted 3 lines
libmpathutil/util.c +3 -0
+
+void get_shared_ptr(void *ptr)
+{

We should probably return here if ptr is NULL, like we do in put_shared_ptr(). Alternatively, this might be a reasonable place for an
assert(), since nobody should be calling these with NULL pointers.

That makes sense. I'll add an assert(), and change put_shared_ptr() accordingly.

Martin

#9 lore raw

PATCH v2 5/19 multipath-tools tests: add tests for shared pointer code

In reply to Martin Wilck

Add a few tests (mostly meaningful with valgrind or ASAN) to test the shared pointer code.

---
 tests/Makefile     |   4 +-
 tests/shared_ptr.c | 120 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 tests/shared_ptr.c
tests/Makefile +3 -1
index f0e5dd3..fc2a7ed 100644
@@ -9,7 +9,8 @@ CFLAGS += $(BIN_CFLAGS) -Wno-unused-parameter -Wno-unused-function $(W_MISSING_I
 LIBDEPS += -L. -L $(mpathutildir) -L$(mpathcmddir) -lmultipath -lmpathutil -lmpathcmd -lcmocka
 
 TESTS := uevent parser util dmevents hwtable blacklist unaligned vpd pgpolicy \
-	 alias directio valid devt mpathvalid strbuf sysfs features cli mapinfo runner
+	 alias directio valid devt mpathvalid strbuf sysfs features cli mapinfo runner \
+	 shared_ptr
 HELPERS := test-lib.o test-log.o
 
 .PRECIOUS: $(TESTS:%=%-test)
@@ -66,6 +67,7 @@ features-test_OBJDEPS := $(mpathutildir)/mt-libudev.o
 cli-test_OBJDEPS := $(daemondir)/cli.o
 mapinfo-test_LIBDEPS = -lpthread -ldevmapper
 runner-test_LIBDEPS = -lpthread
+shared_ptr-test_LIBDEPS = -lpthread
 
 %.o: %.c
 	@echo building $@ because of $?
tests/shared_ptr.c new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
2.54.0
#10 lore raw

PATCH v2 6/19 libmultipath: remove libcheck_thread related code

In reply to Martin Wilck

After d1ebd97 ("libmultipath: TUR checker: use runner threads"), this code is not used any more. Remove it.

---
 libmultipath/checkers.c           | 40 +------------------------------
 libmultipath/checkers.h           | 22 +----------------
 libmultipath/libmultipath.version |  1 -
 3 files changed, 2 insertions(+), 61 deletions(-)
libmultipath/checkers.c +1 -39
index 8f18ca6..5655146 100644
@@ -163,8 +163,7 @@ static struct checker_class *add_checker_class(const char *name)
 		goto out;
 
 	c->mp_init = (int (*)(struct checker *)) dlsym(c->handle, "libcheck_mp_init");
-	c->reset = (void (*)(void)) dlsym(c->handle, "libcheck_reset");
-	c->thread = (void *(*)(void*)) dlsym(c->handle, "libcheck_thread");
+	c->reset = (void (*)(void))dlsym(c->handle, "libcheck_reset");
 	c->pending = (int (*)(struct checker *)) dlsym(c->handle, "libcheck_pending");
 	c->need_wait = (bool (*)(struct checker *)) dlsym(c->handle, "libcheck_need_wait");
 	/* These 5 functions can be NULL. call dlerror() to clear out any
@@ -371,43 +370,6 @@ bad_id:
 	return generic_msg[CHECKER_MSGID_NONE];
 }
 
-static void checker_cleanup_thread(void *arg)
-{
-	struct checker_class *cls = arg;
-
-	free_checker_class(cls);
-	rcu_unregister_thread();
-}
-
-static void *checker_thread_entry(void *arg)
-{
-	struct checker_context *ctx = arg;
-	void *rv;
-
-	rcu_register_thread();
-	pthread_cleanup_push(checker_cleanup_thread, ctx->cls);
-	rv = ctx->cls->thread(ctx);
-	pthread_cleanup_pop(1);
-	return rv;
-}
-
-int start_checker_thread(pthread_t *thread, const pthread_attr_t *attr,
-			 struct checker_context *ctx)
-{
-	int rv;
-
-	assert(ctx && ctx->cls && ctx->cls->thread);
-	/* Take a ref here, lest the class be freed before the thread starts */
-	(void)checker_class_ref(ctx->cls);
-	rv = pthread_create(thread, attr, checker_thread_entry, ctx);
-	if (rv != 0) {
-		condlog(1, "failed to start checker thread for %s: %m",
-			ctx->cls->name);
-		checker_class_unref(ctx->cls);
-	}
-	return rv;
-}
-
 void checker_clear_message (struct checker *c)
 {
 	if (!c)
libmultipath/checkers.h +1 -21
index 694dfa3..e69dfd9 100644
@@ -143,7 +143,6 @@ struct checker_class {
 	int (*mp_init)(struct checker *); /* to allocate the mpcontext */
 	void (*free)(struct checker *);	  /* to free the context */
 	void (*reset)(void);		  /* to reset the global variables */
-	void *(*thread)(void *);	  /* async thread entry point */
 	int (*pending)(struct checker *); /* to recheck pending paths */
 	bool (*need_wait)(struct checker *); /* checker needs waiting for */
 	const char **msgtable;
@@ -179,29 +178,10 @@ void checker_set_sync (struct checker *);
 void checker_set_async (struct checker *);
 void checker_set_fd (struct checker *, int);
 void checker_enable (struct checker *);
-void checker_disable (struct checker *);
-/*
- * start_checker_thread(): start async path checker thread
- *
- * This function provides a wrapper around pthread_create().
- * The created thread will call the DSO's "libcheck_thread" function with the
- * checker context as argument.
- *
- * Rationale:
- * Path checkers that do I/O may hang forever. To avoid blocking, some
- * checkers therefore use asynchronous, detached threads for checking
- * the paths. These threads may continue hanging if multipathd is stopped.
- * In this case, we can't unload the checker DSO at exit. In order to
- * avoid race conditions and crashes, the entry point of the thread
- * needs to be in libmultipath, not in the DSO itself.
- *
- * @param arg: pointer to struct checker_context.
- */
+void checker_disable(struct checker *);
 struct checker_context {
 	struct checker_class *cls;
 };
-int start_checker_thread (pthread_t *thread, const pthread_attr_t *attr,
-			  struct checker_context *ctx);
 int checker_get_state(struct checker *c);
 bool checker_need_wait(struct checker *c);
 void checker_check (struct checker *, int);
libmultipath/libmultipath.version +0 -1
index 78fa2d4..5deee32 100644
@@ -221,7 +221,6 @@ global:
 	/* checkers */
 	checker_is_sync;
 	sg_read;
-	start_checker_thread;
 
 	/* prioritizers */
 	get_asymmetric_access_state;
Signature · 1 line
2.54.0
#11 lore raw

PATCH v2 7/19 libmultipath: use shared_ptr for checker classes

In reply to Martin Wilck

Utilize the share_ptr code for tracking the refcount of checker classes.

---
 libmultipath/checkers.c | 58 ++++++++++++++---------------------------
 libmultipath/checkers.h |  2 --
 2 files changed, 19 insertions(+), 41 deletions(-)
libmultipath/checkers.c +19 -39
index 5655146..a3b9cc8 100644
@@ -39,41 +39,11 @@ const char *checker_state_name(int i)
 	return checker_state_names[i];
 }
 
-static struct checker_class *alloc_checker_class(void)
+static void free_checker_class(void *ptr)
 {
-	struct checker_class *c;
-
-	c = calloc(1, sizeof(struct checker_class));
-	if (c) {
-		INIT_LIST_HEAD(&c->node);
-		uatomic_set(&c->refcount, 1);
-	}
-	return c;
-}
-
-/* Use uatomic_{sub,add}_return() to ensure proper memory barriers */
-static int checker_class_ref(struct checker_class *cls)
-{
-	return uatomic_add_return(&cls->refcount, 1);
-}
-
-static int checker_class_unref(struct checker_class *cls)
-{
-	return uatomic_sub_return(&cls->refcount, 1);
-}
-
-void free_checker_class(struct checker_class *c)
-{
-	int cnt;
-
+	struct checker_class *c = ptr;
 	if (!c)
 		return;
-	cnt = checker_class_unref(c);
-	if (cnt != 0) {
-		condlog(cnt < 0 ? 1 : 4, "%s checker refcount %d",
-			c->name, cnt);
-		return;
-	}
 	condlog(3, "unloading %s checker", c->name);
 	list_del(&c->node);
 	if (c->reset)
@@ -84,7 +54,18 @@ void free_checker_class(struct checker_class *c)
 				c->name, dlerror());
 		}
 	}
-	free(c);
+}
+
+static struct checker_class *alloc_checker_class(void)
+{
+	struct checker_class *c;
+
+	c = alloc_shared_ptr(sizeof(*c), free_checker_class);
+	if (c) {
+		memset(c, 0, sizeof(*c));
+		INIT_LIST_HEAD(&c->node);
+	}
+	return c;
 }
 
 void cleanup_checkers (void)
@@ -92,9 +73,8 @@ void cleanup_checkers (void)
 	struct checker_class *checker_loop;
 	struct checker_class *checker_temp;
 
-	list_for_each_entry_safe(checker_loop, checker_temp, &checkers, node) {
-		free_checker_class(checker_loop);
-	}
+	list_for_each_entry_safe(checker_loop, checker_temp, &checkers, node)
+		put_shared_ptr(checker_loop);
 }
 
 static struct checker_class *checker_class_lookup(const char *name)
@@ -198,7 +178,7 @@ done:
 	list_add(&c->node, &checkers);
 	return c;
 out:
-	free_checker_class(c);
+	put_shared_ptr(c);
 	return NULL;
 }
 
@@ -284,7 +264,7 @@ void checker_put (struct checker * dst)
 	if (src && src->free)
 		src->free(dst);
 	checker_clear(dst);
-	free_checker_class(src);
+	put_shared_ptr(src);
 }
 
 int checker_get_state(struct checker *c)
@@ -393,7 +373,7 @@ void checker_get(struct checker *dst, const char *name)
 	if (!src)
 		return;
 
-	(void)checker_class_ref(dst->cls);
+	get_shared_ptr(dst->cls);
 }
 
 int init_checkers(void)
libmultipath/checkers.h +0 -2
index e69dfd9..7ff8e01 100644
@@ -135,7 +135,6 @@ struct checker;
 struct checker_class {
 	struct list_head node;
 	void *handle;
-	int refcount;
 	int sync;
 	char name[CHECKER_NAME_LEN];
 	int (*check)(struct checker *);
@@ -200,7 +199,6 @@ void checker_get(struct checker *, const char *);
 int libcheck_check(struct checker *);
 int libcheck_init(struct checker *);
 void libcheck_free(struct checker *);
-void *libcheck_thread(struct checker_context *ctx);
 void libcheck_reset(void);
 int libcheck_mp_init(struct checker *);
 int libcheck_pending(struct checker *c);
Signature · 1 line
2.54.0
#12 lore raw

PATCH v2 8/19 libmultipath: use shared_ptr for prioritizers

In reply to Martin Wilck

Replace the hardcoded refcount handling in prio.c with shared_ptr.

---
 libmultipath/prio.c | 47 +++++++++++++++++++--------------------------
 libmultipath/prio.h |  1 -
 2 files changed, 20 insertions(+), 28 deletions(-)
libmultipath/prio.c +20 -27
index 24f825b..bf388e9 100644
@@ -53,28 +53,11 @@ int init_prio(void)
 	return 0;
 }
 
-static struct prio * alloc_prio (void)
-{
-	struct prio *p;
-
-	p = calloc(1, sizeof(struct prio));
-	if (p) {
-		INIT_LIST_HEAD(&p->node);
-		p->refcount = 1;
-	}
-	return p;
-}
-
-void free_prio (struct prio * p)
+void free_prio(void *ptr)
 {
+	struct prio *p = ptr;
 	if (!p)
 		return;
-	p->refcount--;
-	if (p->refcount) {
-		condlog(4, "%s prioritizer refcount %d",
-			p->name, p->refcount);
-		return;
-	}
 	condlog(3, "unloading %s prioritizer", p->name);
 	list_del(&p->node);
 	if (p->handle) {
@@ -83,7 +66,18 @@ void free_prio (struct prio * p)
 				p->name, dlerror());
 		}
 	}
-	free(p);
+}
+
+static struct prio *alloc_prio(void)
+{
+	struct prio *p;
+
+	p = alloc_shared_ptr(sizeof(*p), free_prio);
+	if (p) {
+		memset(p, 0, sizeof(*p));
+		INIT_LIST_HEAD(&p->node);
+	}
+	return p;
 }
 
 void cleanup_prio(void)
@@ -91,9 +85,8 @@ void cleanup_prio(void)
 	struct prio * prio_loop;
 	struct prio * prio_temp;
 
-	list_for_each_entry_safe(prio_loop, prio_temp, &prioritizers, node) {
-		free_prio(prio_loop);
-	}
+	list_for_each_entry_safe(prio_loop, prio_temp, &prioritizers, node)
+		put_shared_ptr(prio_loop);
 }
 
 static struct prio *prio_lookup(const char *name)
@@ -150,7 +143,7 @@ struct prio *add_prio (const char *name)
 	list_add(&p->node, &prioritizers);
 	return p;
 out:
-	free_prio(p);
+	put_shared_ptr(p);
 	return NULL;
 }
 
@@ -199,17 +192,17 @@ void prio_get(struct prio *dst, const char *name, const char *args)
 	dst->getprio = src->getprio;
 	dst->handle = NULL;
 
-	src->refcount++;
+	get_shared_ptr(src);
 }
 
 void prio_put (struct prio * dst)
 {
-	struct prio * src;
+	struct prio *src;
 
 	if (!dst || !dst->getprio)
 		return;
 
 	src = prio_lookup(dst->name);
 	memset(dst, 0x0, sizeof(struct prio));
-	free_prio(src);
+	put_shared_ptr(src);
 }
libmultipath/prio.h +0 -1
index 119b75f..7ff1a24 100644
@@ -45,7 +45,6 @@ struct path;
 
 struct prio {
 	void *handle;
-	int refcount;
 	struct list_head node;
 	char name[PRIO_NAME_LEN];
 	char args[PRIO_ARGS_LEN];
Signature · 1 line
2.54.0
#13 lore raw

PATCH v2 9/19 libmpathutil: runner: use shared_ptr

In reply to Martin Wilck

Use shared_ptr to track the runner_context refcount.

---
 libmpathutil/runner.c | 30 +++++++++---------------------
 1 file changed, 9 insertions(+), 21 deletions(-)
libmpathutil/runner.c +9 -21
index 8c6d6b9..56abd03 100644
@@ -30,7 +30,6 @@ const char *runner_state_name(int state)
 }
 
 struct runner_context {
-	int refcount;
 	int status;
 	struct timespec deadline;
 	pthread_t thr;
@@ -39,17 +38,6 @@ struct runner_context {
 	char __attribute__((aligned(sizeof(void *)))) data[];
 };
 
-static void release_context(struct runner_context *rctx)
-{
-	int n;
-
-	n = uatomic_sub_return(&rctx->refcount, 1);
-	assert(n >= 0);
-
-	if (n == 0)
-		free(rctx);
-}
-
 static void cleanup_context(struct runner_context **prctx)
 {
 	struct runner_context *rctx = *prctx;
@@ -65,7 +53,7 @@ static void cleanup_context(struct runner_context **prctx)
 			"%s: runner %p finished in state '%s'", __func__, rctx,
 			runner_state_name(st));
 	}
-	release_context(rctx);
+	put_shared_ptr(rctx);
 }
 
 static void *runner_thread(void *arg)
@@ -147,7 +135,7 @@ repeat:
 void release_runner(struct runner_context *rctx)
 {
 	cancel_runner(rctx);
-	release_context(rctx);
+	put_shared_ptr(rctx);
 }
 
 int check_runner(struct runner_context *rctx, void *data, unsigned int size)
@@ -195,17 +183,17 @@ struct runner_context *get_runner(runner_func func, void *data,
 		return NULL;
 	}
 
-	rctx = malloc(sizeof(*rctx) + size);
+	rctx = alloc_shared_ptr(sizeof(*rctx) + size, NULL);
 	if (!rctx)
 		return NULL;
 
 	rctx->func = func;
 	/*
-	 * We have to set the refcount to 2 here. The runner thread may be
-	 * cancelled before it even had the chance to increase the refcount,
-	 * which could result in a use-after-free in cleanup_context().
+	 * Take an additional reference here. The runner thread may be
+	 * cancelled before it even had the chance to take a reference, which
+	 * could result in a use-after-free in cleanup_context().
 	 */
-	uatomic_set(&rctx->refcount, 2);
+	get_shared_ptr(rctx);
 	uatomic_set(&rctx->status, RUNNER_IDLE);
 	memcpy(rctx->data, data, size);
 
@@ -222,8 +210,8 @@ struct runner_context *get_runner(runner_func func, void *data,
 
 	if (rc) {
 		condlog(1, "%s: pthread_create(): %s", __func__, strerror(rc));
-		uatomic_dec(&rctx->refcount);
-		release_context(rctx);
+		put_shared_ptr(rctx);
+		put_shared_ptr(rctx);
 		return NULL;
 	}
 	return rctx;
Signature · 1 line
2.54.0
#14 lore raw

Re: [PATCH v2 09/19] libmpathutil: runner: use shared_ptr

Quoted 80 lines · Martin Wilck

Use shared_ptr to track the runner_context refcount.

---
  libmpathutil/runner.c | 30 +++++++++---------------------
  1 file changed, 9 insertions(+), 21 deletions(-)
libmpathutil/runner.c +7 -19
index 8c6d6b9..56abd03 100644
@@ -30,7 +30,6 @@ const char *runner_state_name(int state)
  }
 
  struct runner_context {
-	int refcount;
  	int status;
  	struct timespec deadline;
  	pthread_t thr;
@@ -39,17 +38,6 @@ struct runner_context {
  	char __attribute__((aligned(sizeof(void *)))) data[];
  };
 
-static void release_context(struct runner_context *rctx)
-{
-	int n;
-
-	n = uatomic_sub_return(&rctx->refcount, 1);
-	assert(n >= 0);
-
-	if (n == 0)
-		free(rctx);
-}
-
  static void cleanup_context(struct runner_context **prctx)
  {
  	struct runner_context *rctx = *prctx;
@@ -65,7 +53,7 @@ static void cleanup_context(struct runner_context **prctx)
  			"%s: runner %p finished in state '%s'", __func__, rctx,
  			runner_state_name(st));
  	}
-	release_context(rctx);
+	put_shared_ptr(rctx);
  }
 
  static void *runner_thread(void *arg)
@@ -147,7 +135,7 @@ repeat:
  void release_runner(struct runner_context *rctx)
  {
  	cancel_runner(rctx);
-	release_context(rctx);
+	put_shared_ptr(rctx);
  }
 
  int check_runner(struct runner_context *rctx, void *data, unsigned int size)
@@ -195,17 +183,17 @@ struct runner_context *get_runner(runner_func func, void *data,
  		return NULL;
  	}
 
-	rctx = malloc(sizeof(*rctx) + size);
+	rctx = alloc_shared_ptr(sizeof(*rctx) + size, NULL);
  	if (!rctx)
  		return NULL;
 
  	rctx->func = func;
  	/*
-	 * We have to set the refcount to 2 here. The runner thread may be
-	 * cancelled before it even had the chance to increase the refcount,
-	 * which could result in a use-after-free in cleanup_context().
+	 * Take an additional reference here. The runner thread may be
+	 * cancelled before it even had the chance to take a reference, which
+	 * could result in a use-after-free in cleanup_context().
  	 */
-	uatomic_set(&rctx->refcount, 2);
+	get_shared_ptr(rctx);
  	uatomic_set(&rctx->status, RUNNER_IDLE);
  	memcpy(rctx->data, data, size);
 

libmpathutil/runner.c struct runner_context *get_runner(runner_func func, void *data, · line 217

Quoted 10 lines
libmpathutil/runner.c +2 -2
@@ -222,8 +210,8 @@ struct runner_context *get_runner(runner_func func, void *data,
 
  	if (rc) {
  		condlog(1, "%s: pthread_create(): %s", __func__, strerror(rc));
-		uatomic_dec(&rctx->refcount);
-		release_context(rctx);
+		put_shared_ptr(rctx);
+		put_shared_ptr(rctx);
  		return NULL;
  	}
  	return rctx;

Hmm. While I fully get the idea why one would want to use refcounted objects, there really is only one snag: this is multipath. We really, _really_, should make sure the daemon can execute even if
the root filesystem is not available.
So if we start dropping the reference for the checker (eg if the last path drops and the checker is no longer used), we might end up freeing the memory (and dropping the modules altogether).
But if the path comes back we need to reinstate the checker, which not
only requires additional memory (which might need a recursion into the
filesystem to get free pages) but we also might need to read the checker
module from disk (again). So plenty of opportunity to deaslock waithing
for the disk be become readable.

Maybe it's an idea to have multipathd loading the modules initially, keeping a reference to them, and then dropping the reference once multipathd itself shuts down?

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#15 lore raw

Re: [PATCH v2 09/19] libmpathutil: runner: use shared_ptr

Quoted 103 lines · Hannes Reinecke
Quoted 98 lines · Martin Wilck

Use shared_ptr to track the runner_context refcount.

---
  libmpathutil/runner.c | 30 +++++++++---------------------

  1 file changed, 9 insertions(+), 21 deletions(-)
libmpathutil/runner.c +0 -0
index 8c6d6b9..56abd03 100644
@@ -30,7 +30,6 @@ const char *runner_state_name(int state)
   }


  struct runner_context {

libmpathutil/runner.c +0 -1
-	int refcount;

   int status;
   struct timespec deadline;
   pthread_t thr;

libmpathutil/runner.c +0 -0
@@ -39,17 +38,6 @@ struct runner_context {
   	char __attribute__((aligned(sizeof(void *)))) data[];
   };

libmpathutil/runner.c +0 -11
-static void release_context(struct runner_context *rctx)
-{
-	int n;
-
-	n = uatomic_sub_return(&rctx->refcount, 1);
-	assert(n >= 0);
-
-	if (n == 0)
-		free(rctx);
-}
-

  static void cleanup_context(struct runner_context **prctx) {
   struct runner_context *rctx = *prctx;

libmpathutil/runner.c +1 -1
@@ -65,7 +53,7 @@ static void cleanup_context(struct runner_context
 **prctx)
   			"%s: runner %p finished in state '%s'",
 __func__, rctx,
   			runner_state_name(st));
   	}
-	release_context(rctx);
+	put_shared_ptr(rctx);
   }


  static void *runner_thread(void *arg)

libmpathutil/runner.c +1 -1
@@ -147,7 +135,7 @@ repeat:
   void release_runner(struct runner_context *rctx)
   {
   	cancel_runner(rctx);
-	release_context(rctx);
+	put_shared_ptr(rctx);
   }


  int check_runner(struct runner_context *rctx, void *data, unsigned int size)

libmpathutil/runner.c +0 -0
@@ -195,17 +183,17 @@ struct runner_context *get_runner(runner_func
 func, void *data,
   		return NULL;
   	}

libmpathutil/runner.c +1 -1
-	rctx = malloc(sizeof(*rctx) + size);
+	rctx = alloc_shared_ptr(sizeof(*rctx) + size, NULL);

   if (!rctx)
   return NULL;

   rctx->func = func;
   /*

libmpathutil/runner.c +0 -1
-	 * We have to set the refcount to 2 here. The runner

thread may be

libmpathutil/runner.c +0 -1
-	 * cancelled before it even had the chance to increase the

refcount,

libmpathutil/runner.c +0 -1
-	 * which could result in a use-after-free in

cleanup_context().

libmpathutil/runner.c +1 -0
+	 * Take an additional reference here. The runner thread

may be

libmpathutil/runner.c +1 -0
+	 * cancelled before it even had the chance to take a

reference, which

libmpathutil/runner.c +1 -0
+	 * could result in a use-after-free in cleanup_context().

   */

libmpathutil/runner.c +1 -1
-	uatomic_set(&rctx->refcount, 2);
+	get_shared_ptr(rctx);

   uatomic_set(&rctx->status, RUNNER_IDLE);
   memcpy(rctx->data, data, size);

libmpathutil/runner.c +0 -0
@@ -222,8 +210,8 @@ struct runner_context *get_runner(runner_func
 func, void *data,


   if (rc) {
   condlog(1, "%s: pthread_create(): %s", __func__, strerror(rc));

libmpathutil/runner.c +2 -2
-		uatomic_dec(&rctx->refcount);
-		release_context(rctx);
+		put_shared_ptr(rctx);
+		put_shared_ptr(rctx);
Quoted 3 lines

   return NULL;
   }
   return rctx;

Quoted 8 lines

Hmm. While I fully get the idea why one would want to use refcounted objects, there really is only one snag: this is multipath. We really, _really_, should make sure the daemon can execute even if the root filesystem is not available.
So if we start dropping the reference for the checker (eg if the last path drops and the checker is no longer used), we might end up freeing
the memory (and dropping the modules altogether).

I am pretty sure that we handle this correctly. The current patch set doesn't change anything in this respect, except translating the hard- coded refcounting into the shared_ptr framework.

Quoted 9 lines

But if the path comes back we need to reinstate the checker, which not
only requires additional memory (which might need a recursion into the
filesystem to get free pages) but we also might need to read the checker
module from disk (again). So plenty of opportunity to deaslock waithing
for the disk be become readable.

In checker_get(), we call add_checker_class() if the class is not yet loaded, which will take one ref on the class, and then get_shared_ptr() for every path (including the one for which add_checker_class() had been called), which means we have (number of paths + 1) references on the class, and will only drop the last ref when multipathd calls cleanup_checkers() during exit.

Therefore I don't think it's possible that we unload the shared library prematurely, and have to reload it later.

Regards
Martin

#16 lore raw

Re: [PATCH v2 09/19] libmpathutil: runner: use shared_ptr

Quoted 134 lines · Martin Wilck
Quoted 103 lines · Hannes Reinecke
Quoted 99 lines · Martin Wilck

Use shared_ptr to track the runner_context refcount.

---
   libmpathutil/runner.c | 30 +++++++++---------------------
   1 file changed, 9 insertions(+), 21 deletions(-)
libmpathutil/runner.c +1 -13
index 8c6d6b9..56abd03 100644
@@ -30,7 +30,6 @@ const char *runner_state_name(int state)
   }
 
   struct runner_context {
-	int refcount;
   	int status;
   	struct timespec deadline;
   	pthread_t thr;
@@ -39,17 +38,6 @@ struct runner_context {
   	char __attribute__((aligned(sizeof(void *)))) data[];
   };
 
-static void release_context(struct runner_context *rctx)
-{
-	int n;
-
-	n = uatomic_sub_return(&rctx->refcount, 1);
-	assert(n >= 0);
-
-	if (n == 0)
-		free(rctx);
-}
-
   static void cleanup_context(struct runner_context **prctx)
   {
   	struct runner_context *rctx = *prctx;
@@ -65,7 +53,7 @@ static void cleanup_context(struct runner_context
 **prctx)
   			"%s: runner %p finished in state '%s'",
 __func__, rctx,
   			runner_state_name(st));
   	}
-	release_context(rctx);
+	put_shared_ptr(rctx);
   }

  static void *runner_thread(void *arg)

libmpathutil/runner.c +1 -1
@@ -147,7 +135,7 @@ repeat:
   void release_runner(struct runner_context *rctx)
   {
   	cancel_runner(rctx);
-	release_context(rctx);
+	put_shared_ptr(rctx);
   }
 
   int check_runner(struct runner_context *rctx, void *data,

unsigned int size)

libmpathutil/runner.c +1 -2
@@ -195,17 +183,17 @@ struct runner_context *get_runner(runner_func
 func, void *data,
   		return NULL;
   	}
 
-	rctx = malloc(sizeof(*rctx) + size);
+	rctx = alloc_shared_ptr(sizeof(*rctx) + size, NULL);
   	if (!rctx)
   		return NULL;
 
   	rctx->func = func;
   	/*
-	 * We have to set the refcount to 2 here. The runner

thread may be

libmpathutil/runner.c +0 -1
-	 * cancelled before it even had the chance to increase the

refcount,

libmpathutil/runner.c +0 -1
-	 * which could result in a use-after-free in

cleanup_context().

libmpathutil/runner.c +1 -0
+	 * Take an additional reference here. The runner thread

may be

libmpathutil/runner.c +1 -0
+	 * cancelled before it even had the chance to take a

reference, which

libmpathutil/runner.c +4 -3
+	 * could result in a use-after-free in cleanup_context().
   	 */
-	uatomic_set(&rctx->refcount, 2);
+	get_shared_ptr(rctx);
   	uatomic_set(&rctx->status, RUNNER_IDLE);
   	memcpy(rctx->data, data, size);
@@ -222,8 +210,8 @@ struct runner_context *get_runner(runner_func
 func, void *data,
 
   	if (rc) {
   		condlog(1, "%s: pthread_create(): %s", __func__,
 strerror(rc));
-		uatomic_dec(&rctx->refcount);
-		release_context(rctx);
+		put_shared_ptr(rctx);
+		put_shared_ptr(rctx);
   		return NULL;
Quoted 2 lines

   }
   return rctx;

Quoted 8 lines

Hmm. While I fully get the idea why one would want to use refcounted objects, there really is only one snag: this is multipath. We really, _really_, should make sure the daemon can execute even if the root filesystem is not available.
So if we start dropping the reference for the checker (eg if the last path drops and the checker is no longer used), we might end up freeing
the memory (and dropping the modules altogether).

I am pretty sure that we handle this correctly. The current patch set doesn't change anything in this respect, except translating the hard- coded refcounting into the shared_ptr framework.

Quoted 9 lines

But if the path comes back we need to reinstate the checker, which not
only requires additional memory (which might need a recursion into the
filesystem to get free pages) but we also might need to read the checker
module from disk (again). So plenty of opportunity to deaslock waithing
for the disk be become readable.

In checker_get(), we call add_checker_class() if the class is not yet loaded, which will take one ref on the class, and then get_shared_ptr() for every path (including the one for which add_checker_class() had been called), which means we have (number of paths + 1) references on the class, and will only drop the last ref when multipathd calls cleanup_checkers() during exit.

Quoted 3 lines

Therefore I don't think it's possible that we unload the shared library prematurely, and have to reload it later.

Hmm.

Really hmmmmm.

If the checkers are loaded during startup, and unloaded on shutdown, there really is no point in refcounting them, is there? Wouldn't global pointers to each check sufficient here, avoiding the need for refcounting altogether?

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#17 lore raw

Re: [PATCH v2 09/19] libmpathutil: runner: use shared_ptr

Quoted 30 lines · Hannes Reinecke
Quoted 25 lines · Martin Wilck
Quoted 11 lines · Hannes Reinecke

But if the path comes back we need to reinstate the checker, which
not
only requires additional memory (which might need a recursion into
the
filesystem to get free pages) but we also might need to read the checker
module from disk (again). So plenty of opportunity to deaslock waithing
for the disk be become readable.

In checker_get(), we call add_checker_class() if the class is not yet
loaded, which will take one ref on the class, and then get_shared_ptr()
for every path (including the one for which add_checker_class() had been called), which means we have (number of paths + 1) references on
the class, and will only drop the last ref when multipathd calls cleanup_checkers() during exit.

Quoted 4 lines

Therefore I don't think it's possible that we unload the shared library
prematurely, and have to reload it later.

Quoted 8 lines

Hmm.

Really hmmmmm.

If the checkers are loaded during startup, and unloaded on shutdown, there really is no point in refcounting them, is there? Wouldn't global pointers to each check sufficient here, avoiding the need for refcounting altogether?

Currently checker and prioritizer DSOs aren't loaded during startup. They are loaded when the first path that uses the given checker is initialized.

I agree that it would be possible to simplify the code by just loading all checkers and prioritizers in advance, in which case we wouldn't need refcounting, like you wrote. Actually, we don't need to use shared objects in the first place; we could simply include all the checker and prioritizer code in libmultipath itself.

The shared object architecture dates back to multipath-tools 0.4.9 (2008). We (actually, you ;-) ) added refcounting to fix races in 323090f ("Use refcounting for checkers"). So far it hasn't occured to me to change this architecture, which has been working well for over a decade.

Anyway, this discussion is orthogonal to my current patch set.

Regards
Martin

#18 lore raw

Re: [PATCH v2 09/19] libmpathutil: runner: use shared_ptr

Quoted 50 lines · Martin Wilck
Quoted 30 lines · Hannes Reinecke
Quoted 25 lines · Martin Wilck
Quoted 11 lines · Hannes Reinecke

But if the path comes back we need to reinstate the checker, which
not
only requires additional memory (which might need a recursion into
the
filesystem to get free pages) but we also might need to read the checker
module from disk (again). So plenty of opportunity to deaslock waithing
for the disk be become readable.

In checker_get(), we call add_checker_class() if the class is not yet
loaded, which will take one ref on the class, and then get_shared_ptr()
for every path (including the one for which add_checker_class() had been called), which means we have (number of paths + 1) references on
the class, and will only drop the last ref when multipathd calls cleanup_checkers() during exit.

Quoted 4 lines

Therefore I don't think it's possible that we unload the shared library
prematurely, and have to reload it later.

Quoted 8 lines

Hmm.

Really hmmmmm.

If the checkers are loaded during startup, and unloaded on shutdown, there really is no point in refcounting them, is there? Wouldn't global pointers to each check sufficient here, avoiding the need for refcounting altogether?

Currently checker and prioritizer DSOs aren't loaded during startup. They are loaded when the first path that uses the given checker is initialized.

I agree that it would be possible to simplify the code by just loading all checkers and prioritizers in advance, in which case we wouldn't need refcounting, like you wrote. Actually, we don't need to use shared objects in the first place; we could simply include all the checker and prioritizer code in libmultipath itself.

Quoted 8 lines

The shared object architecture dates back to multipath-tools 0.4.9 (2008). We (actually, you ;-) ) added refcounting to fix races in 323090f ("Use refcounting for checkers"). So far it hasn't occured to me to change this architecture, which has been working well for over a decade.

Anyway, this discussion is orthogonal to my current patch set.

That is true.

So maybe for a later cleanup.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#19 lore raw

PATCH v2 10/19 libmultipath: add generic async path checker code

In reply to Martin Wilck

Add a framework for an asynchronous path checker utilizing the recently added runner code for thread handling.

This code has been derived from the TUR checker code by removing all references to the actual sending of TUR ioctls.

Follow-up patches will convert the current TUR checker into an instance of this async checker class.

---
 libmultipath/Makefile        |   2 +-
 libmultipath/async_checker.c | 214 +++++++++++++++++++++++++++++++++++
 libmultipath/async_checker.h |  35 ++++++
 libmultipath/checkers.c      |   1 +
 libmultipath/checkers.h      |   2 +
 5 files changed, 253 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/async_checker.c
 create mode 100644 libmultipath/async_checker.h
libmultipath/Makefile +1 -1
index 85767ab..d71a835 100644
@@ -22,7 +22,7 @@ OBJS-O := devmapper.o hwtable.o blacklist.o dmparser.o \
 	configure.o structs_vec.o sysfs.o \
 	lock.o file.o wwids.o prioritizers/alua_rtpg.o prkey.o \
 	io_err_stat.o dm-generic.o generic.o nvme-lib.o \
-	libsg.o valid.o
+	libsg.o valid.o async_checker.o
 
 OBJS := $(OBJS-O) $(OBJS-U)
 
libmultipath/async_checker.c new file +214 -0

214 lines not on this page · raw message

libmultipath/async_checker.h new file +35 -0
new file mode 100644
index 0000000..fc9632d
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// Copyright (c) 2026 SUSE LLC
+#ifndef ASYNC_CHECKER_H_INCLUDED
+#define ASYNC_CHECKER_H_INCLUDED
+
+struct runner_data;
+struct checker;
+typedef int (*async_checker_func)(struct runner_data *);
+
+struct runner_data {
+	int fd;
+	dev_t devt;
+	async_checker_func afunc;
+	unsigned int timeout;
+	int state;
+	short msgid;
+	char __attribute__((aligned(sizeof(void *)))) checker_ctx[];
+};
+
+int async_check_init(struct checker *c);
+void async_check_free(struct checker *c);
+bool async_check_need_wait(struct checker *c);
+int async_check_pending(struct checker *c);
+int async_check_check(struct checker *c);
+
+#define CHECKER_MAX_CONTEXT_SIZE 1024
+
+/* For testing handling of async checker timeouts */
+#ifdef ASYNC_TEST_MAJOR
+static void async_deep_sleep(const struct runner_data *rdata);
+#else
+#define async_deep_sleep(x) do {} while (0)
+#endif
+
+#endif
libmultipath/checkers.c +1 -0
index a3b9cc8..3a1663f 100644
@@ -9,6 +9,7 @@
 
 #include "debug.h"
 #include "checkers.h"
+#include "async_checker.h"
 #include "vector.h"
 #include "util.h"
 
libmultipath/checkers.h +2 -0
index 7ff8e01..05dbd7c 100644
@@ -132,6 +132,7 @@ enum {
 };
 
 struct checker;
+struct runner_data;
 struct checker_class {
 	struct list_head node;
 	void *handle;
@@ -144,6 +145,7 @@ struct checker_class {
 	void (*reset)(void);		  /* to reset the global variables */
 	int (*pending)(struct checker *); /* to recheck pending paths */
 	bool (*need_wait)(struct checker *); /* checker needs waiting for */
+	int (*async_func)(struct runner_data *); /* callback for async_checker */
 	const char **msgtable;
 	short msgtable_size;
 };
Signature · 1 line
2.54.0
#20 lore raw

PATCH v2 11/19 libmultipath: checkers: add support for async checker

In reply to Martin Wilck

If a checker shared library contains a symbol "libcheck_async_func", use this as a callback for the async checker functions, and do not read any other symbols from the .so file except the message table.

---
 libmultipath/checkers.c           | 51 +++++++++++++++++++++----------
 libmultipath/libmultipath.version |  2 +-
 2 files changed, 36 insertions(+), 17 deletions(-)
libmultipath/checkers.c +35 -16
index 3a1663f..2f20b25 100644
@@ -101,6 +101,18 @@ void reset_checker_classes(void)
 	}
 }
 
+static struct checker_class *add_async_checker_class(struct checker_class *c)
+{
+	c->init = async_check_init;
+	c->check = async_check_check;
+	c->need_wait = async_check_need_wait;
+	c->pending = async_check_pending;
+	c->free = async_check_free;
+
+	list_add(&c->node, &checkers);
+	return c;
+}
+
 static struct checker_class *add_checker_class(const char *name)
 {
 	char libname[LIB_CHECKER_NAMELEN];
@@ -129,6 +141,29 @@ static struct checker_class *add_checker_class(const char *name)
 				errstr);
 		goto out;
 	}
+
+	c->msgtable_size = 0;
+	c->msgtable = dlsym(c->handle, "libcheck_msgtable");
+
+	if (c->msgtable != NULL) {
+		const char **p;
+
+		for (p = c->msgtable;
+		     *p && (p - c->msgtable) < CHECKER_MSGTABLE_SIZE; p++)
+			/* nothing */;
+
+		c->msgtable_size = p - c->msgtable;
+	} else
+		c->msgtable_size = 0;
+	condlog(3, "checker %s: message table size = %d", c->name,
+		c->msgtable_size);
+
+	c->async_func = (int (*)(struct runner_data *))
+		dlsym(c->handle, "libcheck_async_func");
+	errstr = dlerror();
+	if (c->async_func)
+		return add_async_checker_class(c);
+
 	c->check = (int (*)(struct checker *)) dlsym(c->handle, "libcheck_check");
 	errstr = dlerror();
 	if (errstr != NULL)
@@ -158,22 +193,6 @@ static struct checker_class *add_checker_class(const char *name)
 	if (!c->free)
 		goto out;
 
-	c->msgtable_size = 0;
-	c->msgtable = dlsym(c->handle, "libcheck_msgtable");
-
-	if (c->msgtable != NULL) {
-		const char **p;
-
-		for (p = c->msgtable;
-		     *p && (p - c->msgtable) < CHECKER_MSGTABLE_SIZE; p++)
-			/* nothing */;
-
-		c->msgtable_size = p - c->msgtable;
-	} else
-		c->msgtable_size = 0;
-	condlog(3, "checker %s: message table size = %d",
-		c->name, c->msgtable_size);
-
 done:
 	c->sync = 1;
 	list_add(&c->node, &checkers);
libmultipath/libmultipath.version +1 -1
index 5deee32..c091ce2 100644
@@ -43,7 +43,7 @@ LIBMPATHCOMMON_1.0.0 {
 	put_multipath_config;
 };
 
-LIBMULTIPATH_32.0.0 {
+LIBMULTIPATH_33.0.0 {
 global:
 	/* symbols referenced by multipath and multipathd */
 	add_foreign;
Signature · 1 line
2.54.0
#21 lore raw

PATCH v2 12/19 libmultipath: convert TUR checker to an async checker instance

In reply to Martin Wilck

The TUR checker now just exports the message table and the "libcheck_async_func" symbol. This converts it into an instance of the generic async checker model.

With this change, struct checker_context isn't used any more and can be removed.

---
 libmultipath/checkers.h     |   3 -
 libmultipath/checkers/tur.c | 253 +++---------------------------------
 2 files changed, 15 insertions(+), 241 deletions(-)
libmultipath/checkers.h +0 -3
index 05dbd7c..4846ea9 100644
@@ -180,9 +180,6 @@ void checker_set_async (struct checker *);
 void checker_set_fd (struct checker *, int);
 void checker_enable (struct checker *);
 void checker_disable(struct checker *);
-struct checker_context {
-	struct checker_class *cls;
-};
 int checker_get_state(struct checker *c);
 bool checker_need_wait(struct checker *c);
 void checker_check (struct checker *, int);
libmultipath/checkers/tur.c +15 -238

315 lines not on this page · raw message

Signature · 1 line
2.54.0
#22 lore raw

PATCH v2 13/19 libmultipath: convert cciss_tur checker to async checker

In reply to Martin Wilck

Make the cciss_tur checker use the async checker framework.

---
 libmultipath/checkers/cciss_tur.c | 41 ++++++++-----------------------
 1 file changed, 10 insertions(+), 31 deletions(-)
libmultipath/checkers/cciss_tur.c +10 -31
index c89fe5e..b859138 100644
@@ -24,47 +24,33 @@
 #include <errno.h>
 
 #include "checkers.h"
+#include "async_checker.h"
 
 #include "cciss.h"
 
 #define TUR_CMD_LEN 6
 #define HEAVY_CHECK_COUNT       10
 
-struct cciss_tur_checker_context {
-	void * dummy;
-};
-
-int libcheck_init (__attribute__((unused)) struct checker * c)
-{
-	return 0;
-}
-
-void libcheck_free (__attribute__((unused)) struct checker * c)
-{
-	return;
-}
-
-int libcheck_check(struct checker * c)
+int libcheck_async_func(struct runner_data *rdata)
 {
 	int rc;
 	int ret;
 	unsigned int lun = 0;
-	struct cciss_tur_checker_context * ctxt = NULL;
 	LogvolInfo_struct    lvi;       // logical "volume" info
 	IOCTL_Command_struct cic;       // cciss ioctl command
 
-	if ((c->fd) < 0) {
-		c->msgid = CHECKER_MSGID_NO_FD;
+	if ((rdata->fd) < 0) {
+		rdata->msgid = CHECKER_MSGID_NO_FD;
 		ret = -1;
 		goto out;
 	}
 
-	rc = ioctl(c->fd, CCISS_GETLUNINFO, &lvi);
+	rc = ioctl(rdata->fd, CCISS_GETLUNINFO, &lvi);
 	if ( rc != 0) {
 		perror("Error: ");
 		fprintf(stderr, "cciss TUR  failed in CCISS_GETLUNINFO: %s\n",
 			strerror(errno));
-		c->msgid = CHECKER_MSGID_DOWN;
+		rdata->msgid = CHECKER_MSGID_DOWN;
 		ret = PATH_DOWN;
 		goto out;
 	} else {
@@ -87,31 +73,24 @@ int libcheck_check(struct checker * c)
 	cic.Request.CDB[4] = 0;
 	cic.Request.CDB[5] = 0;
 
-	rc = ioctl(c->fd, CCISS_PASSTHRU, &cic);
+	rc = ioctl(rdata->fd, CCISS_PASSTHRU, &cic);
 	if (rc < 0) {
 		fprintf(stderr, "cciss TUR  failed: %s\n",
 			strerror(errno));
-		c->msgid = CHECKER_MSGID_DOWN;
+		rdata->msgid = CHECKER_MSGID_DOWN;
 		ret = PATH_DOWN;
 		goto out;
 	}
 
 	if ((cic.error_info.CommandStatus | cic.error_info.ScsiStatus )) {
-		c->msgid = CHECKER_MSGID_DOWN;
+		rdata->msgid = CHECKER_MSGID_DOWN;
 		ret = PATH_DOWN;
 		goto out;
 	}
 
-	c->msgid = CHECKER_MSGID_UP;
+	rdata->msgid = CHECKER_MSGID_UP;
 
 	ret = PATH_UP;
 out:
-	/*
-	 * caller told us he doesn't want to keep the context :
-	 * free it
-	 */
-	if (!c->context)
-		free(ctxt);
-
 	return(ret);
 }
Signature · 1 line
2.54.0
#23 lore raw

PATCH v2 14/19 libmultipath: convert readsector0 checker to async checker

In reply to Martin Wilck

Make the readsector0 checker use the async checker framework.

---
 libmultipath/checkers/readsector0.c | 28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)
libmultipath/checkers/readsector0.c +7 -21
index b041f11..fcc4ad1 100644
@@ -1,41 +1,27 @@
 /*
  * Copyright (c) 2004, 2005 Christophe Varoqui
  */
-#include <stdio.h>
-
+#include <sys/types.h>
 #include "checkers.h"
 #include "libsg.h"
+#include "async_checker.h"
 
-struct readsector0_checker_context {
-	void * dummy;
-};
-
-int libcheck_init (__attribute__((unused)) struct checker * c)
-{
-	return 0;
-}
-
-void libcheck_free (__attribute__((unused)) struct checker * c)
-{
-	return;
-}
-
-int libcheck_check (struct checker * c)
+int libcheck_async_func(struct runner_data *rdata)
 {
 	unsigned char buf[4096];
 	unsigned char sbuf[SENSE_BUFF_LEN];
 	int ret;
 
-	ret = sg_read(c->fd, &buf[0], 4096, &sbuf[0],
-		      SENSE_BUFF_LEN, c->timeout);
+	ret = sg_read(rdata->fd, &buf[0], 4096, &sbuf[0], SENSE_BUFF_LEN,
+		      rdata->timeout);
 
 	switch (ret)
 	{
 	case PATH_DOWN:
-		c->msgid = CHECKER_MSGID_DOWN;
+		rdata->msgid = CHECKER_MSGID_DOWN;
 		break;
 	case PATH_UP:
-		c->msgid = CHECKER_MSGID_UP;
+		rdata->msgid = CHECKER_MSGID_UP;
 		break;
 	default:
 		break;
Signature · 1 line
2.54.0
#24 lore raw

PATCH v2 15/19 libmultipath: convert hp_sw checker to async checker

In reply to Martin Wilck

Make the hp_sw checker use the async checker framework.

---
 libmultipath/checkers/hp_sw.c | 32 ++++++++------------------------
 1 file changed, 8 insertions(+), 24 deletions(-)
libmultipath/checkers/hp_sw.c +8 -24
index 1ab7909..770221f 100644
@@ -1,8 +1,6 @@
 /*
  * Copyright (c) 2005 Christophe Varoqui
  */
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -12,7 +10,7 @@
 #include <errno.h>
 
 #include "checkers.h"
-
+#include "async_checker.h"
 #include "sg_include.h"
 #include "unaligned.h"
 
@@ -28,20 +26,6 @@
 #define MX_ALLOC_LEN		255
 #define HEAVY_CHECK_COUNT       10
 
-struct sw_checker_context {
-	void * dummy;
-};
-
-int libcheck_init (__attribute__((unused)) struct checker * c)
-{
-	return 0;
-}
-
-void libcheck_free (__attribute__((unused)) struct checker * c)
-{
-	return;
-}
-
 static int
 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
        void *resp, int mx_resp_len, unsigned int timeout)
@@ -127,24 +111,24 @@ do_tur (int fd, unsigned int timeout)
 	return 0;
 }
 
-int libcheck_check(struct checker * c)
+int libcheck_async_func(struct runner_data *rdata)
 {
 	char buff[MX_ALLOC_LEN];
-	int ret = do_inq(c->fd, 0, 1, 0x80, buff, MX_ALLOC_LEN, c->timeout);
+	int ret = do_inq(rdata->fd, 0, 1, 0x80, buff, MX_ALLOC_LEN, rdata->timeout);
 
 	if (ret == PATH_WILD) {
-		c->msgid = CHECKER_MSGID_UNSUPPORTED;
+		rdata->msgid = CHECKER_MSGID_UNSUPPORTED;
 		return ret;
 	}
 	if (ret != PATH_UP) {
-		c->msgid = CHECKER_MSGID_DOWN;
+		rdata->msgid = CHECKER_MSGID_DOWN;
 		return ret;
 	};
 
-	if (do_tur(c->fd, c->timeout)) {
-		c->msgid = CHECKER_MSGID_GHOST;
+	if (do_tur(rdata->fd, rdata->timeout)) {
+		rdata->msgid = CHECKER_MSGID_GHOST;
 		return PATH_GHOST;
 	}
-	c->msgid = CHECKER_MSGID_UP;
+	rdata->msgid = CHECKER_MSGID_UP;
 	return PATH_UP;
 }
Signature · 1 line
2.54.0
#25 lore raw

PATCH v2 16/19 libmultipath: async_checker: add context_size and init symbols

In reply to Martin Wilck

Add two symbols that can be read from checker .so files with dlsym():
"libcheck_async_context_size" for defining the size of a checker-private context, and "libcheck_async_init" for initializing this context.

---
 libmultipath/async_checker.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
libmultipath/async_checker.c +29 -2
index 02fe765..7a7bfc1 100644
@@ -6,35 +6,62 @@
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <dlfcn.h>
 #include "async_checker.h"
 #include "checkers.h"
 #include "debug.h"
 #include "runner.h"
 
 #define MAX_NR_TIMEOUTS 1
+typedef int (*async_init_func)(struct runner_data *);
 
 struct async_checker_context {
 	int last_runner_state;
 	unsigned int nr_timeouts;
 	struct runner_context *rtx;
+	async_init_func async_init;
+	int async_context_size;
 	struct runner_data rdata;
 };
 
-#define rdata_size(acc) (sizeof(acc->rdata))
+#define rdata_size(acc) (sizeof(acc->rdata) + acc->async_context_size)
 
 int async_check_init(struct checker *c)
 {
 	struct async_checker_context *acc;
 	struct stat sb;
+	const int *p_ctx_size;
+	int ctx_size;
+	char *errstr __attribute__((unused));
 
-	acc = calloc(1, sizeof(*acc));
+	p_ctx_size = dlsym(c->cls->handle, "libcheck_async_context_size");
+	errstr = dlerror();
+	if (p_ctx_size)
+		ctx_size = *p_ctx_size;
+	else
+		ctx_size = 0;
+
+	if (ctx_size < 0 || ctx_size > CHECKER_MAX_CONTEXT_SIZE) {
+		condlog(0, "%s: invalid context size %d", __func__, ctx_size);
+		return -1;
+	}
+	condlog(3, "%s: extra context size: %d", __func__, ctx_size);
+	acc = calloc(1, sizeof(*acc) + ctx_size);
 	if (!acc)
 		return -1;
+
+	acc->async_context_size = ctx_size;
+	acc->async_init = (int (*)(struct runner_data *))
+		dlsym(c->cls->handle, "libcheck_async_init");
+	errstr = dlerror();
+
 	acc->rdata.state = PATH_UNCHECKED;
 	acc->rdata.fd = -1;
 	if (fstat(c->fd, &sb) == 0)
 		acc->rdata.devt = sb.st_rdev;
 	c->context = acc;
+	if (acc->async_init)
+		return acc->async_init(&acc->rdata);
 	return 0;
 }
 
Signature · 1 line
2.54.0
#26 lore raw

PATCH v2 17/19 libmultipath: convert RDAC checker to async checker

In reply to Martin Wilck

Make the RDAC checker use the async checker framework. The function libcheck_init(), which was used to set the TAS bit, is converted to libcheck_async_init().

---
 libmultipath/checkers/rdac.c | 38 +++++++++++++-----------------------
 1 file changed, 14 insertions(+), 24 deletions(-)
libmultipath/checkers/rdac.c +14 -24
index 87b8872..a0f853e 100644
@@ -1,8 +1,6 @@
 /*
  * Copyright (c) 2005 Christophe Varoqui
  */
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -15,6 +13,7 @@
 #include "debug.h"
 
 #include "sg_include.h"
+#include "async_checker.h"
 
 #define INQUIRY_CMDLEN		6
 #define INQUIRY_CMD		0x12
@@ -55,11 +54,7 @@ struct control_mode_page {
 	unsigned char dontcare1[6];
 };
 
-struct rdac_checker_context {
-	void * dummy;
-};
-
-int libcheck_init (struct checker * c)
+int libcheck_async_init(struct runner_data *rdata)
 {
 	unsigned char cmd[MODE_SEN_SEL_CMDLEN];
 	unsigned char sense_b[SENSE_BUFF_LEN];
@@ -85,9 +80,9 @@ int libcheck_init (struct checker * c)
 	io_hdr.dxferp = &current;
 	io_hdr.cmdp = cmd;
 	io_hdr.sbp = sense_b;
-	io_hdr.timeout = c->timeout * 1000;
+	io_hdr.timeout = rdata->timeout * 1000;
 
-	if (ioctl(c->fd, SG_IO, &io_hdr) < 0)
+	if (ioctl(rdata->fd, SG_IO, &io_hdr) < 0)
 		goto out;
 
 	/* check the TAS bit to see if it is already set */
@@ -101,7 +96,7 @@ int libcheck_init (struct checker * c)
 	io_hdr.dxferp = &changeable;
 	memset(&changeable, 0, sizeof(struct control_mode_page));
 
-	if (ioctl(c->fd, SG_IO, &io_hdr) < 0)
+	if (ioctl(rdata->fd, SG_IO, &io_hdr) < 0)
 		goto out;
 
 	/* if TAS bit is not settable exit */
@@ -122,7 +117,7 @@ int libcheck_init (struct checker * c)
 	io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
 	io_hdr.dxferp = &current;
 
-	if (ioctl(c->fd, SG_IO, &io_hdr) < 0)
+	if (ioctl(rdata->fd, SG_IO, &io_hdr) < 0)
 		goto out;
 
 	/* Success */
@@ -133,11 +128,6 @@ out:
 	return 0;
 }
 
-void libcheck_free(__attribute__((unused)) struct checker *c)
-{
-	return;
-}
-
 static int
 do_inq(int sg_fd, unsigned int pg_op, void *resp, int mx_resp_len,
        unsigned int timeout)
@@ -288,15 +278,15 @@ checker_msg_string(const struct volume_access_inq *inq)
 	}
 }
 
-int libcheck_check(struct checker * c)
+int libcheck_async_func(struct runner_data *rdata)
 {
 	struct volume_access_inq inq;
 	int ret, inqfail;
 
 	inqfail = 0;
 	memset(&inq, 0, sizeof(struct volume_access_inq));
-	ret = do_inq(c->fd, 0xC9, &inq, sizeof(struct volume_access_inq),
-		     c->timeout);
+	ret = do_inq(rdata->fd, 0xC9, &inq, sizeof(struct volume_access_inq),
+		     rdata->timeout);
 	if (ret != PATH_UP) {
 		inqfail = 1;
 		goto done;
@@ -340,17 +330,17 @@ int libcheck_check(struct checker * c)
 done:
 	switch (ret) {
 	case PATH_WILD:
-		c->msgid = CHECKER_MSGID_UNSUPPORTED;
+		rdata->msgid = CHECKER_MSGID_UNSUPPORTED;
 		break;
 	case PATH_DOWN:
-		c->msgid = (inqfail ? RDAC_MSGID_INQUIRY_FAILED :
-			    checker_msg_string(&inq));
+		rdata->msgid = (inqfail ? RDAC_MSGID_INQUIRY_FAILED
+					: checker_msg_string(&inq));
 		break;
 	case PATH_UP:
-		c->msgid = CHECKER_MSGID_UP;
+		rdata->msgid = CHECKER_MSGID_UP;
 		break;
 	case PATH_GHOST:
-		c->msgid = CHECKER_MSGID_GHOST;
+		rdata->msgid = CHECKER_MSGID_GHOST;
 		break;
 	}
 
Signature · 1 line
2.54.0
#27 lore raw

PATCH v2 18/19 libmultipath: checkers: rework mpcontext passing

In reply to Martin Wilck

The emc_clariion path checker requires an "mpcontext" that is shared between the checkers for all paths of a given multipath map. This context is currently represented in struct multipath as a void *, the pointer to which is passed to the checker. The checker allocates the actual memory in the libcheck_mp_init() function, and changes the pointer in struct multipath. This is a dangerous layering violation as the checker operates on memory it doesn't own, and not type-safe at all.

This patch changes the mpcontext handling as follows: The void * pointer is replaced by a dedicated union checker_mpcontext. It only contains a long integer element, which is sufficient to store the information needed by emc_clariion.

Instead of initializing this value in mp_init, the address of the union is passed to libcheck_check() and libcheck_pending() if the path that is being checked has an associated struct multipath. Passing a union pointer improves compile-time type safety. In order to check whether the value is initialized, instead of testing for a NULL pointer like before, the code defines an INVALID_MPCONTEXT value. This value must obviously be chosen such that it doesn't represent a valid context value, which is the case for emc_clariion.

For a synchronous checker (like emc_clariion), libcheck_check() is allowed to write to this memory, because at the time of the call we know that pp->mpp is valid. The same holds for libcheck_pending().

This change requires checker_check() and checker_get_state() to be passed a "struct path *" rather than a "struct checker *". The layer separation happens in these functions now.

The async_checker code also gets an implementation of mpcontext handling, in preparation of converting emc_clariion to an async checker.

---
 libmultipath/async_checker.c         | 32 +++++++++++-------
 libmultipath/async_checker.h         |  5 +--
 libmultipath/checkers.c              | 46 +++++++++++---------------
 libmultipath/checkers.h              | 44 ++++++++++++++++---------
 libmultipath/checkers/directio.c     |  6 ++--
 libmultipath/checkers/emc_clariion.c | 49 ++++++++++------------------
 libmultipath/discovery.c             |  8 ++---
 libmultipath/libmultipath.version    |  2 +-
 libmultipath/structs.c               |  3 +-
 libmultipath/structs.h               |  3 +-
 tests/directio.c                     |  2 +-
 11 files changed, 100 insertions(+), 100 deletions(-)
libmultipath/async_checker.c +20 -12
index 7a7bfc1..0023c73 100644
@@ -7,8 +7,8 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <dlfcn.h>
-#include "async_checker.h"
 #include "checkers.h"
+#include "async_checker.h"
 #include "debug.h"
 #include "runner.h"
 
@@ -59,6 +59,7 @@ int async_check_init(struct checker *c)
 	acc->rdata.fd = -1;
 	if (fstat(c->fd, &sb) == 0)
 		acc->rdata.devt = sb.st_rdev;
+	SET_INVALID_MPCONTEXT(acc->rdata.mpc);
 	c->context = acc;
 	if (acc->async_init)
 		return acc->async_init(&acc->rdata);
@@ -147,35 +148,42 @@ bool async_check_need_wait(struct checker *c)
 	return acc && acc->rtx;
 }
 
-int async_check_pending(struct checker *c)
+int async_check_pending(struct checker *c, union checker_mpcontext *mpc)
 {
 	struct async_checker_context *acc = c->context;
+	int rc;
 	/* The if path checker isn't running, just return the exiting value. */
 	if (!acc || !acc->rtx)
 		return c->path_state;
 
-	/* This may nullify ct->rtx */
-	check_runner_state(acc);
+	/* This may nullify acc->rtx */
+	rc = check_runner_state(acc);
 	c->msgid = acc->rdata.msgid;
+	if (rc == RUNNER_DONE && mpc && !IS_INVALID_MPCONTEXT(acc->rdata.mpc))
+		*mpc = acc->rdata.mpc;
 	return acc->rdata.state;
 }
 
-int async_check_check(struct checker *c)
+int async_check_check(struct checker *c, union checker_mpcontext *mpc)
 {
 	struct async_checker_context *acc = c->context;
 
 	if (!acc)
 		return PATH_UNCHECKED;
 
-	if (checker_is_sync(c))
-		return acc->rdata.afunc(&acc->rdata);
+	if (mpc && !IS_INVALID_MPCONTEXT(*mpc))
+		acc->rdata.mpc = *mpc;
 
-	/* Handle the case that the checker just completed */
-	if (acc->rtx) {
-		check_runner_state(acc);
-		c->msgid = acc->rdata.msgid;
-		return acc->rdata.state;
+	if (checker_is_sync(c)) {
+		int rc = acc->rdata.afunc(&acc->rdata);
+
+		if (mpc && !IS_INVALID_MPCONTEXT(acc->rdata.mpc))
+			*mpc = acc->rdata.mpc;
+		return rc;
 	}
+	/* Handle the case that the checker just completed */
+	if (acc->rtx)
+		return async_check_pending(c, mpc);
 
 	/* create new checker thread */
 	acc->rdata.fd = c->fd;
libmultipath/async_checker.h +3 -2
index fc9632d..d7d02e8 100644
@@ -14,14 +14,15 @@ struct runner_data {
 	unsigned int timeout;
 	int state;
 	short msgid;
+	union checker_mpcontext mpc;
 	char __attribute__((aligned(sizeof(void *)))) checker_ctx[];
 };
 
 int async_check_init(struct checker *c);
 void async_check_free(struct checker *c);
 bool async_check_need_wait(struct checker *c);
-int async_check_pending(struct checker *c);
-int async_check_check(struct checker *c);
+int async_check_pending(struct checker *c, union checker_mpcontext *mpctx);
+int async_check_check(struct checker *c, union checker_mpcontext *mpctx);
 
 #define CHECKER_MAX_CONTEXT_SIZE 1024
 
libmultipath/checkers.c +19 -27

100 lines not on this page · raw message

libmultipath/checkers.h +29 -15

85 lines not on this page · raw message

libmultipath/checkers/directio.c +4 -2
index a7422a8..82b2041 100644
@@ -405,7 +405,8 @@ bool libcheck_need_wait(struct checker *c)
 		!ct->checked_state);
 }
 
-int libcheck_pending(struct checker *c)
+int libcheck_pending(struct checker *c,
+		     union checker_mpcontext *mpc __attribute__((unused)))
 {
 	int rc;
 	struct io_event event;
@@ -441,7 +442,8 @@ out:
 	return rc;
 }
 
-int libcheck_check (struct checker * c)
+int libcheck_check(struct checker *c,
+		   union checker_mpcontext *mpctx __attribute__((unused)))
 {
 	int ret;
 	struct directio_context * ct = (struct directio_context *)c->context;
libmultipath/checkers/emc_clariion.c +18 -31

87 lines not on this page · raw message

libmultipath/discovery.c +3 -5
index f59fa6d..ceca9fd 100644
@@ -1998,20 +1998,18 @@ start_checker (struct path * pp, struct config *conf, int daemon, int oldstate)
 			return -1;
 		}
 		checker_set_fd(c, pp->fd);
-		if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
+		if (checker_init(c)) {
 			checker_clear(c);
 			condlog(3, "%s: checker init failed", pp->dev);
 			return -1;
 		}
 	}
-	if (pp->mpp && !c->mpcontext)
-		checker_mp_init(c, &pp->mpp->mpcontext);
 	checker_clear_message(c);
 	if (conf->force_sync == 0)
 		checker_set_async(c);
 	else
 		checker_set_sync(c);
-	checker_check(c, oldstate);
+	checker_check(pp, oldstate);
 	return 0;
 }
 
@@ -2021,7 +2019,7 @@ get_state (struct path * pp)
 	struct checker * c = &pp->checker;
 	int state, lvl;
 
-	state = checker_get_state(c);
+	state = checker_get_state(pp);
 
 	lvl = state == pp->oldstate || state == PATH_PENDING ? 4 : 3;
 	condlog(lvl, "%s: %s state = %s", pp->dev,
libmultipath/libmultipath.version +1 -1
index c091ce2..f631f45 100644
@@ -43,7 +43,7 @@ LIBMPATHCOMMON_1.0.0 {
 	put_multipath_config;
 };
 
-LIBMULTIPATH_33.0.0 {
+LIBMULTIPATH_34.0.0 {
 global:
 	/* symbols referenced by multipath and multipathd */
 	add_foreign;
libmultipath/structs.c +1 -2
index f441312..c627b2c 100644
@@ -261,7 +261,7 @@ alloc_multipath (void)
 
 	if (mpp) {
 		mpp->bestpg = 1;
-		mpp->mpcontext = NULL;
+		SET_INVALID_MPCONTEXT(mpp->mpcontext);
 		mpp->no_path_retry = NO_PATH_RETRY_UNDEF;
 		dm_multipath_to_gen(mpp)->ops = &dm_gen_multipath_ops;
 	}
@@ -330,7 +330,6 @@ void free_multipath(struct multipath *mpp)
 		vector_free(mpp->hwe);
 		mpp->hwe = NULL;
 	}
-	free(mpp->mpcontext);
 	free(mpp);
 }
 
libmultipath/structs.h +1 -2
index 9adedde..8b71aa2 100644
@@ -540,8 +540,7 @@ struct multipath {
 	unsigned int stat_queueing_timeouts;
 	unsigned int stat_map_failures;
 
-	/* checkers shared data */
-	void * mpcontext;
+	union checker_mpcontext mpcontext;
 
 	/* persistent management data*/
 	int prkey_source;
tests/directio.c +1 -1
index fa1e553..addb7cf 100644
@@ -227,7 +227,7 @@ void do_check_state(struct checker *c, int sync, int chk_state)
 
 void do_libcheck_pending(struct checker *c, int chk_state)
 {
-	assert_int_equal(libcheck_pending(c), chk_state);
+	assert_int_equal(libcheck_pending(c, NULL), chk_state);
 	assert_int_equal(ev_off, 0);
 	memset(mock_events, 0, sizeof(mock_events));
 }
Signature · 1 line
2.54.0
#28 lore raw

PATCH v2 19/19 libmultipath: convert emc_clariion to async_checker

In reply to Martin Wilck

Convert also the emc_clariion checker. This is the only checker that uses a checker-specific context and mptcontext.

---
 libmultipath/checkers/emc_clariion.c | 79 ++++++++++++----------------
 1 file changed, 33 insertions(+), 46 deletions(-)
libmultipath/checkers/emc_clariion.c +33 -46
index 24a386c..5b34d68 100644
@@ -1,8 +1,6 @@
 /*
  * Copyright (c) 2004, 2005 Lars Marowsky-Bree
  */
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -14,8 +12,8 @@
 #include "sg_include.h"
 #include "libsg.h"
 #include "checkers.h"
+#include "async_checker.h"
 #include "debug.h"
-#include "util.h"
 
 #define INQUIRY_CMD     0x12
 #define INQUIRY_CMDLEN  6
@@ -33,19 +31,17 @@
  * simple read test would return 02/04/03 instead
  * of 05/25/01 sensekey/ASC/ASCQ data.
  */
-#define IS_INACTIVE_SNAP(mpctx)				\
-	(!mpctx || IS_INVALID_MPCONTEXT(*mpctx) ? 0 : mpctx->long_val)
+#define IS_INACTIVE_SNAP(mpctx)					\
+	(IS_INVALID_MPCONTEXT(mpctx) ? 0 : mpctx.long_val)
 
 #define SET_INACTIVE_SNAP(mpctx)		\
 	do {					\
-		if (mpctx)			\
-			mpctx->long_val = 1;	\
+		mpctx.long_val = 1;		\
 	} while (0);
 
 #define CLR_INACTIVE_SNAP(mpctx)		\
 	do {					\
-		if (mpctx)			\
-			mpctx->long_val = 0;	\
+		mpctx.long_val = 0;		\
 	} while (0);
 
 enum {
@@ -86,7 +82,9 @@ struct emc_clariion_checker_LU_context {
 	int inactive_snap;
 };
 
-void hexadecimal_to_ascii(char * wwn, char *wwnstr)
+int libcheck_async_context_size = sizeof(struct emc_clariion_checker_path_context);
+
+static void hexadecimal_to_ascii(char *wwn, char *wwnstr)
 {
 	int i,j, nbl;
 
@@ -99,33 +97,22 @@ void hexadecimal_to_ascii(char * wwn, char *wwnstr)
 	wwnstr[32]=0;
 }
 
-int libcheck_init (struct checker * c)
+int libcheck_async_init(struct runner_data *rdata)
 {
-	/*
-	 * Allocate and initialize the path specific context.
-	 */
-	c->context = calloc(1, sizeof(struct emc_clariion_checker_path_context));
-	if (!c->context)
-		return 1;
-	((struct emc_clariion_checker_path_context *)c->context)->wwn_set = 0;
+	((struct emc_clariion_checker_path_context *)rdata->checker_ctx)->wwn_set = 0;
 
 	return 0;
 }
 
-void libcheck_free(struct checker *c)
-{
-	free(c->context);
-}
-
-int libcheck_check(struct checker *c, union checker_mpcontext *mpctx)
+int libcheck_async_func(struct runner_data *rdata)
 {
 	unsigned char sense_buffer[128] = { 0, };
 	unsigned char sb[SENSE_BUFF_LEN] = { 0, }, *sbb;
 	unsigned char inqCmdBlk[INQUIRY_CMDLEN] = {INQUIRY_CMD, 1, 0xC0, 0,
 						sizeof(sense_buffer), 0};
 	struct sg_io_hdr io_hdr;
-	struct emc_clariion_checker_path_context * ct =
-		(struct emc_clariion_checker_path_context *)c->context;
+	struct emc_clariion_checker_path_context *ct =
+		(struct emc_clariion_checker_path_context *)rdata->checker_ctx;
 	char wwnstr[33];
 	int ret;
 	int retry_emc = 5;
@@ -142,14 +129,14 @@ retry:
 	io_hdr.dxferp = sense_buffer;
 	io_hdr.cmdp = inqCmdBlk;
 	io_hdr.sbp = sb;
-	io_hdr.timeout = c->timeout * 1000;
+	io_hdr.timeout = rdata->timeout * 1000;
 	io_hdr.pack_id = 0;
-	if (ioctl(c->fd, SG_IO, &io_hdr) < 0) {
+	if (ioctl(rdata->fd, SG_IO, &io_hdr) < 0) {
 		if (errno == ENOTTY) {
-			c->msgid = CHECKER_MSGID_UNSUPPORTED;
+			rdata->msgid = CHECKER_MSGID_UNSUPPORTED;
 			return PATH_WILD;
 		}
-		c->msgid = MSG_CLARIION_QUERY_FAILED;
+		rdata->msgid = MSG_CLARIION_QUERY_FAILED;
 		return PATH_DOWN;
 	}
 
@@ -181,12 +168,12 @@ retry:
 				sense_key = sbp[2] & 0xf;
 
 			if (sense_key == ILLEGAL_REQUEST) {
-				c->msgid = CHECKER_MSGID_UNSUPPORTED;
+				rdata->msgid = CHECKER_MSGID_UNSUPPORTED;
 				return PATH_WILD;
 			} else if (sense_key != RECOVERED_ERROR) {
 				condlog(1, "emc_clariion_checker: INQUIRY failed with sense key %02x",
 					sense_key);
-				c->msgid = MSG_CLARIION_QUERY_ERROR;
+				rdata->msgid = MSG_CLARIION_QUERY_ERROR;
 				return PATH_DOWN;
 			}
 		}
@@ -195,13 +182,13 @@ retry:
 	if (io_hdr.info & SG_INFO_OK_MASK) {
 		condlog(1, "emc_clariion_checker: INQUIRY failed without sense, status %02x",
 			io_hdr.status);
-		c->msgid = MSG_CLARIION_QUERY_ERROR;
+		rdata->msgid = MSG_CLARIION_QUERY_ERROR;
 		return PATH_DOWN;
 	}
 
 	if (/* Verify the code page - right page & revision */
 	    sense_buffer[1] != 0xc0 || sense_buffer[9] != 0x00) {
-		c->msgid = MSG_CLARIION_UNIT_REPORT;
+		rdata->msgid = MSG_CLARIION_UNIT_REPORT;
 		return PATH_DOWN;
 	}
 
@@ -215,19 +202,19 @@ retry:
 		    ((sense_buffer[28] & 0x07) != 0x06))
 		/* Arraycommpath should be set to 1 */
 		|| (sense_buffer[30] & 0x04) != 0x04) {
-		c->msgid = MSG_CLARIION_PATH_CONFIG;
+		rdata->msgid = MSG_CLARIION_PATH_CONFIG;
 		return PATH_DOWN;
 	}
 
 	if ( /* LUN operations should indicate normal operations */
 		sense_buffer[48] != 0x00) {
-		c->msgid = MSG_CLARIION_PATH_NOT_AVAIL;
+		rdata->msgid = MSG_CLARIION_PATH_NOT_AVAIL;
 		return PATH_SHAKY;
 	}
 
 	if ( /* LUN should at least be bound somewhere and not be LUNZ */
 		sense_buffer[4] == 0x00) {
-		c->msgid = MSG_CLARIION_LUN_UNBOUND;
+		rdata->msgid = MSG_CLARIION_LUN_UNBOUND;
 		return PATH_DOWN;
 	}
 
@@ -238,7 +225,7 @@ retry:
 	 */
 	if (ct->wwn_set) {
 		if (memcmp(ct->wwn, &sense_buffer[10], 16) != 0) {
-			c->msgid = MSG_CLARIION_WWN_CHANGED;
+			rdata->msgid = MSG_CLARIION_WWN_CHANGED;
 			return PATH_DOWN;
 		}
 	} else {
@@ -253,8 +240,8 @@ retry:
 		unsigned char buf[4096];
 
 		memset(buf, 0, 4096);
-		ret = sg_read(c->fd, &buf[0], 4096,
-			      sbb = &sb[0], SENSE_BUFF_LEN, c->timeout);
+		ret = sg_read(rdata->fd, &buf[0], 4096, sbb = &sb[0],
+			      SENSE_BUFF_LEN, rdata->timeout);
 		if (ret == PATH_DOWN) {
 			hexadecimal_to_ascii(ct->wwn, wwnstr);
 
@@ -269,7 +256,7 @@ retry:
 				 * passive paths which will return
 				 * 02/04/03 not 05/25/01 on read.
 				 */
-				SET_INACTIVE_SNAP(mpctx);
+				SET_INACTIVE_SNAP(rdata->mpc);
 				condlog(3, "emc_clariion_checker: Active "
 					"path to inactive snapshot WWN %s.",
 					wwnstr);
@@ -278,26 +265,26 @@ retry:
 					"error for WWN %s.  Sense data are "
 					"0x%x/0x%x/0x%x.", wwnstr,
 					sbb[2]&0xf, sbb[12], sbb[13]);
-				c->msgid = MSG_CLARIION_READ_ERROR;
+				rdata->msgid = MSG_CLARIION_READ_ERROR;
 			}
 		} else {
-			c->msgid = MSG_CLARIION_PASSIVE_GOOD;
+			rdata->msgid = MSG_CLARIION_PASSIVE_GOOD;
 			/*
 			 * Remove the path from the set of paths to inactive
 			 * snapshot LUs if it was in this list since the
 			 * snapshot is no longer inactive.
 			 */
-			CLR_INACTIVE_SNAP(mpctx);
+			CLR_INACTIVE_SNAP(rdata->mpc);
 		}
 	} else {
-		if (IS_INACTIVE_SNAP(mpctx)) {
+		if (IS_INACTIVE_SNAP(rdata->mpc)) {
 			hexadecimal_to_ascii(ct->wwn, wwnstr);
 			condlog(3, "emc_clariion_checker: Passive "
 				"path to inactive snapshot WWN %s.",
 				wwnstr);
 			ret = PATH_DOWN;
 		} else {
-			c->msgid = MSG_CLARIION_PASSIVE_GOOD;
+			rdata->msgid = MSG_CLARIION_PASSIVE_GOOD;
 			ret = PATH_UP;	/* not ghost */
 		}
 	}
Signature · 1 line
2.54.0
maintainer Reviewed-by on the v2 series
#29 lore raw
In reply to Martin Wilck
Quoted 23 lines · Martin Wilck

Based on my previous patch set [1], which introduced the "runner" thread handling code as an abstract implementation of the asynchronous checker logic in the TUR checker, this set introduces a generic "async_checker" type and converts all existing checkers except directio to this framework. With this set applied, all checkers run asyncrhonously unless explicitly forbidden to do so with the "force_sync" option.

Patch 1-3 are minor preparations. Patch 4-8 introduce a general concept of a refcounted "shared" pointer and use it to replace multiple instances of hard-coded refcounts on objects. Patch 9 converts the current TUR code into a generic asynchronous checker type by separating the actual TUR code from the handling of the async runners. Patch 10 adds support for this async checker type in the checkers code by searching for "libcheck_async_func" with dlsym() in checker DSOs. Patch 11-15 convert all checkers except emc_clariion to the async_checker framework. emc_clariion is special because it needs extra context both for the checker itself and for multipath-related information. Patch 16 and 17 add the respective additional fields to the async_checker framework. The mpcontext handling is rewritten, because the current apporach of handling void ** pointers would be disastrous in scenarious with truly asynchronous checker threads that may hang forever. Patch 18 finally converts emc_clariion, using the concepts from patch 16 and 17.

Quoted 5 lines

Comments and reviews welcome.

Regards,
Martin

Sorry for the late review. For the set: