2010-08-03 16:03:32 +00:00
|
|
|
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
|
|
|
|
|
|
|
|
/* Check brick
|
|
|
|
* checks various semantic properties, uses watchdog to find lost callbacks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define BRICK_DEBUGGING
|
|
|
|
//#define MARS_DEBUGGING
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/kthread.h>
|
|
|
|
|
|
|
|
#include "mars.h"
|
|
|
|
|
|
|
|
///////////////////////// own type definitions ////////////////////////
|
|
|
|
|
|
|
|
#include "mars_check.h"
|
|
|
|
|
|
|
|
///////////////////////// own helper functions ////////////////////////
|
|
|
|
|
2010-08-20 10:58:24 +00:00
|
|
|
#define CHECK_ERR(output,fmt,args...) \
|
|
|
|
do { \
|
|
|
|
struct check_input *input = (output)->brick->inputs[0]; \
|
|
|
|
struct generic_output *other = (void*)input->connect; \
|
|
|
|
if (other) { \
|
|
|
|
MARS_ERR("instance %d/%s/%s: " fmt, \
|
|
|
|
(output)->instance_nr, \
|
|
|
|
other->type->type_name, \
|
|
|
|
other->output_name, \
|
|
|
|
##args); \
|
|
|
|
} else { \
|
|
|
|
MARS_ERR("instance %d: " fmt, \
|
|
|
|
(output)->instance_nr, \
|
|
|
|
##args); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2010-08-08 14:02:54 +00:00
|
|
|
static void check_buf_endio(struct generic_callback *cb)
|
2010-08-03 16:03:32 +00:00
|
|
|
{
|
2010-08-10 17:39:30 +00:00
|
|
|
struct check_mars_ref_aspect *mref_a;
|
|
|
|
struct mars_ref_object *mref;
|
|
|
|
struct check_output *output;
|
|
|
|
struct check_input *input;
|
2010-08-09 16:57:56 +00:00
|
|
|
struct generic_callback *prev_cb;
|
2010-08-03 16:03:32 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
mref_a = cb->cb_private;
|
|
|
|
if (unlikely(!mref_a)) {
|
2010-08-05 08:36:36 +00:00
|
|
|
MARS_FAT("cannot get aspect -- hanging up\n");
|
2010-08-10 17:39:30 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
if (unlikely(&mref_a->cb != cb)) {
|
|
|
|
MARS_FAT("bad callback -- hanging up\n");
|
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
|
|
|
|
mref = mref_a->object;
|
|
|
|
if (unlikely(!mref)) {
|
|
|
|
MARS_FAT("bad mref -- hanging up\n");
|
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
|
|
|
|
output = mref_a->output;
|
|
|
|
if (unlikely(!output)) {
|
|
|
|
MARS_FAT("bad output -- hanging up\n");
|
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
|
|
|
|
input = output->brick->inputs[0];
|
|
|
|
if (unlikely(!input)) {
|
|
|
|
MARS_FAT("bad input -- hanging up\n");
|
|
|
|
goto fatal;
|
2010-08-05 08:36:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-09 16:57:56 +00:00
|
|
|
if (atomic_dec_and_test(&mref_a->callback_count)) {
|
|
|
|
atomic_set(&mref_a->callback_count, 1);
|
2010-08-20 10:58:24 +00:00
|
|
|
CHECK_ERR(output, "too many callbacks on %p\n", mref);
|
2010-08-05 08:36:36 +00:00
|
|
|
}
|
2010-08-08 09:03:42 +00:00
|
|
|
|
|
|
|
#ifdef CHECK_LOCK
|
|
|
|
traced_lock(&output->check_lock, flags);
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
if (list_empty(&mref_a->mref_head)) {
|
2010-08-20 10:58:24 +00:00
|
|
|
CHECK_ERR(output, "list entry missing on %p\n", mref);
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
2010-08-05 15:54:48 +00:00
|
|
|
list_del_init(&mref_a->mref_head);
|
2010-08-05 08:36:36 +00:00
|
|
|
|
2010-08-07 07:59:34 +00:00
|
|
|
traced_unlock(&output->check_lock, flags);
|
2010-08-09 16:57:56 +00:00
|
|
|
#else
|
|
|
|
(void)flags;
|
2010-08-08 09:03:42 +00:00
|
|
|
#endif
|
2010-08-03 16:03:32 +00:00
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
mref_a->last_jiffies = jiffies;
|
2010-08-08 14:02:54 +00:00
|
|
|
|
2010-08-09 16:57:56 +00:00
|
|
|
prev_cb = cb->cb_prev;
|
|
|
|
if (!prev_cb) {
|
2010-08-08 14:02:54 +00:00
|
|
|
MARS_FAT("cannot get chain callback\n");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-20 10:58:24 +00:00
|
|
|
#if 1
|
|
|
|
mref->ref_cb = prev_cb;
|
|
|
|
mref_a->installed = false;
|
|
|
|
#endif
|
2010-08-09 16:57:56 +00:00
|
|
|
prev_cb->cb_fn(prev_cb);
|
2010-08-10 17:39:30 +00:00
|
|
|
return;
|
|
|
|
fatal:
|
|
|
|
msleep(60000);
|
|
|
|
return;
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-09 16:57:56 +00:00
|
|
|
#ifdef CHECK_LOCK
|
2010-08-04 17:32:04 +00:00
|
|
|
static void dump_mem(void *data, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *tmp;
|
|
|
|
char buf[256];
|
|
|
|
for (i = 0, tmp = buf; i < len; i++) {
|
|
|
|
unsigned char byte = ((unsigned char*)data)[i];
|
|
|
|
if (!(i % 8)) {
|
|
|
|
if (tmp != buf) {
|
2010-08-07 07:59:34 +00:00
|
|
|
printk("%4d: %s\n", i, buf);
|
2010-08-04 17:32:04 +00:00
|
|
|
}
|
|
|
|
tmp = buf;
|
|
|
|
}
|
|
|
|
tmp += sprintf(tmp, " %02x", byte);
|
|
|
|
}
|
|
|
|
if (tmp != buf) {
|
2010-08-07 07:59:34 +00:00
|
|
|
printk("%4d: %s\n", i, buf);
|
2010-08-04 17:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-03 16:03:32 +00:00
|
|
|
static int check_watchdog(void *data)
|
|
|
|
{
|
|
|
|
struct check_output *output = data;
|
|
|
|
MARS_INF("watchdog has started.\n");
|
|
|
|
while (!kthread_should_stop()) {
|
|
|
|
struct list_head *h;
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long now;
|
|
|
|
|
|
|
|
msleep_interruptible(5000);
|
|
|
|
|
2010-08-07 07:59:34 +00:00
|
|
|
traced_lock(&output->check_lock, flags);
|
2010-08-03 16:03:32 +00:00
|
|
|
|
|
|
|
now = jiffies;
|
2010-08-05 15:54:48 +00:00
|
|
|
for (h = output->mref_anchor.next; h != &output->mref_anchor; h = h->next) {
|
2010-08-07 07:59:34 +00:00
|
|
|
static int limit = 1;
|
|
|
|
const int timeout = 30;
|
2010-08-05 15:54:48 +00:00
|
|
|
struct check_mars_ref_aspect *mref_a;
|
|
|
|
struct mars_ref_object *mref;
|
2010-08-03 16:03:32 +00:00
|
|
|
unsigned long elapsed;
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
mref_a = container_of(h, struct check_mars_ref_aspect, mref_head);
|
|
|
|
mref = mref_a->object;
|
|
|
|
elapsed = now - mref_a->last_jiffies;
|
2010-08-07 07:59:34 +00:00
|
|
|
if (elapsed > timeout * HZ && limit-- > 0) {
|
2010-08-04 17:32:04 +00:00
|
|
|
struct generic_object_layout *object_layout;
|
|
|
|
int i;
|
2010-08-05 15:54:48 +00:00
|
|
|
mref_a->last_jiffies = now + 600 * HZ;
|
2010-08-20 10:58:24 +00:00
|
|
|
MARS_INF("================================\n");
|
|
|
|
CHECK_ERR(output, "mref %p callback is missing for more than %d seconds.\n", mref, timeout);
|
2010-08-05 15:54:48 +00:00
|
|
|
object_layout = (void*)mref->object_layout;
|
|
|
|
//dump_mem(mref, object_layout->object_size);
|
2010-08-04 17:32:04 +00:00
|
|
|
for (i = 0; i < object_layout->aspect_count; i++) {
|
|
|
|
struct generic_aspect_layout *aspect_layout;
|
|
|
|
int pos;
|
|
|
|
aspect_layout = object_layout->aspect_layouts[i];
|
|
|
|
pos = aspect_layout->aspect_offset;
|
|
|
|
if (i == 0) {
|
|
|
|
MARS_INF("object %s:\n", object_layout->object_type->object_type_name);
|
2010-08-05 15:54:48 +00:00
|
|
|
dump_mem(mref, pos);
|
2010-08-04 17:32:04 +00:00
|
|
|
}
|
|
|
|
MARS_INF("--- aspect %s ---:\n", aspect_layout->aspect_type->aspect_type_name);
|
2010-08-05 15:54:48 +00:00
|
|
|
dump_mem(((void*)mref + pos), aspect_layout->aspect_type->aspect_size);
|
2010-08-04 17:32:04 +00:00
|
|
|
}
|
2010-08-20 10:58:24 +00:00
|
|
|
MARS_INF("================================\n");
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-08 09:03:42 +00:00
|
|
|
|
2010-08-07 07:59:34 +00:00
|
|
|
traced_unlock(&output->check_lock, flags);
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-09 16:57:56 +00:00
|
|
|
#endif
|
2010-08-03 16:03:32 +00:00
|
|
|
|
|
|
|
////////////////// own brick / input / output operations //////////////////
|
|
|
|
|
|
|
|
static int check_get_info(struct check_output *output, struct mars_info *info)
|
|
|
|
{
|
|
|
|
struct check_input *input = output->brick->inputs[0];
|
|
|
|
return GENERIC_INPUT_CALL(input, mars_get_info, info);
|
|
|
|
}
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
static int check_ref_get(struct check_output *output, struct mars_ref_object *mref)
|
2010-08-03 16:03:32 +00:00
|
|
|
{
|
|
|
|
struct check_input *input = output->brick->inputs[0];
|
2010-08-05 15:54:48 +00:00
|
|
|
return GENERIC_INPUT_CALL(input, mars_ref_get, mref);
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
static void check_ref_put(struct check_output *output, struct mars_ref_object *mref)
|
2010-08-03 16:03:32 +00:00
|
|
|
{
|
|
|
|
struct check_input *input = output->brick->inputs[0];
|
2010-08-05 15:54:48 +00:00
|
|
|
GENERIC_INPUT_CALL(input, mars_ref_put, mref);
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
static void check_ref_io(struct check_output *output, struct mars_ref_object *mref, int rw)
|
2010-08-03 16:03:32 +00:00
|
|
|
{
|
|
|
|
struct check_input *input = output->brick->inputs[0];
|
2010-08-05 15:54:48 +00:00
|
|
|
struct check_mars_ref_aspect *mref_a = check_mars_ref_get_aspect(output, mref);
|
2010-08-03 16:03:32 +00:00
|
|
|
unsigned long flags;
|
2010-08-05 08:36:36 +00:00
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
if (!mref_a) {
|
2010-08-05 08:36:36 +00:00
|
|
|
MARS_FAT("cannot get aspect -- hanging up\n");
|
|
|
|
msleep(60000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-09 16:57:56 +00:00
|
|
|
if (atomic_dec_and_test(&mref_a->call_count)) {
|
|
|
|
atomic_set(&mref_a->call_count, 1);
|
2010-08-20 10:58:24 +00:00
|
|
|
CHECK_ERR(output, "multiple parallel calls on %p\n", mref);
|
2010-08-05 08:36:36 +00:00
|
|
|
}
|
2010-08-09 16:57:56 +00:00
|
|
|
atomic_set(&mref_a->callback_count, 2);
|
2010-08-08 09:03:42 +00:00
|
|
|
|
|
|
|
#ifdef CHECK_LOCK
|
|
|
|
traced_lock(&output->check_lock, flags);
|
2010-08-10 17:39:30 +00:00
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
if (!list_empty(&mref_a->mref_head)) {
|
2010-08-20 10:58:24 +00:00
|
|
|
CHECK_ERR(output, "list head not empty on %p\n", mref);
|
2010-08-10 17:39:30 +00:00
|
|
|
list_del(&mref_a->mref_head);
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
2010-08-05 15:54:48 +00:00
|
|
|
list_add_tail(&mref_a->mref_head, &output->mref_anchor);
|
2010-08-10 17:39:30 +00:00
|
|
|
|
2010-08-08 09:03:42 +00:00
|
|
|
traced_unlock(&output->check_lock, flags);
|
2010-08-09 16:57:56 +00:00
|
|
|
#else
|
|
|
|
(void)flags;
|
2010-08-08 09:03:42 +00:00
|
|
|
#endif
|
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
if (!mref_a->installed) {
|
|
|
|
struct generic_callback *cb = &mref_a->cb;
|
|
|
|
mref_a->installed = true;
|
|
|
|
mref_a->output = output;
|
2010-08-08 14:02:54 +00:00
|
|
|
cb->cb_fn = check_buf_endio;
|
|
|
|
cb->cb_private = mref_a;
|
|
|
|
cb->cb_error = 0;
|
|
|
|
cb->cb_prev = mref->ref_cb;
|
|
|
|
mref->ref_cb = cb;
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
2010-08-05 15:54:48 +00:00
|
|
|
mref_a->last_jiffies = jiffies;
|
2010-08-05 08:36:36 +00:00
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
GENERIC_INPUT_CALL(input, mars_ref_io, mref, rw);
|
2010-08-09 16:57:56 +00:00
|
|
|
|
|
|
|
atomic_inc(&mref_a->call_count);
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////// object / aspect constructors / destructors ///////////////
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
static int check_mars_ref_aspect_init_fn(struct generic_aspect *_ini, void *_init_data)
|
2010-08-03 16:03:32 +00:00
|
|
|
{
|
2010-08-05 15:54:48 +00:00
|
|
|
struct check_mars_ref_aspect *ini = (void*)_ini;
|
2010-08-08 09:03:42 +00:00
|
|
|
#ifdef CHECK_LOCK
|
2010-08-05 15:54:48 +00:00
|
|
|
INIT_LIST_HEAD(&ini->mref_head);
|
2010-08-08 09:03:42 +00:00
|
|
|
#endif
|
|
|
|
ini->last_jiffies = jiffies;
|
2010-08-09 16:57:56 +00:00
|
|
|
atomic_set(&ini->call_count, 2);
|
|
|
|
atomic_set(&ini->callback_count, 1);
|
2010-08-10 17:39:30 +00:00
|
|
|
ini->installed = false;
|
2010-08-03 16:03:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-08 09:03:42 +00:00
|
|
|
static void check_mars_ref_aspect_exit_fn(struct generic_aspect *_ini, void *_init_data)
|
|
|
|
{
|
|
|
|
struct check_mars_ref_aspect *ini = (void*)_ini;
|
|
|
|
(void)ini;
|
|
|
|
#ifdef CHECK_LOCK
|
2010-08-20 10:58:24 +00:00
|
|
|
if (!list_empty(&ini->mref_head)) {
|
|
|
|
struct check_output *output = ini->output;
|
|
|
|
if (output) {
|
|
|
|
CHECK_ERR(output, "list head not empty on %p\n", ini->object);
|
|
|
|
INIT_LIST_HEAD(&ini->mref_head);
|
|
|
|
} else {
|
|
|
|
CHECK_HEAD_EMPTY(&ini->mref_head);
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 09:03:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-08-03 16:03:32 +00:00
|
|
|
MARS_MAKE_STATICS(check);
|
|
|
|
|
|
|
|
////////////////////// brick constructors / destructors ////////////////////
|
|
|
|
|
|
|
|
static int check_brick_construct(struct check_brick *brick)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_output_construct(struct check_output *output)
|
|
|
|
{
|
|
|
|
static int count = 0;
|
2010-08-09 16:57:56 +00:00
|
|
|
#ifdef CHECK_LOCK
|
2010-08-03 16:03:32 +00:00
|
|
|
struct task_struct *watchdog;
|
2010-08-05 08:36:36 +00:00
|
|
|
|
2010-08-08 09:03:42 +00:00
|
|
|
spin_lock_init(&output->check_lock);
|
2010-08-05 15:54:48 +00:00
|
|
|
INIT_LIST_HEAD(&output->mref_anchor);
|
2010-08-03 16:03:32 +00:00
|
|
|
watchdog = kthread_create(check_watchdog, output, "check_watchdog%d", output->instance_nr);
|
|
|
|
if (!IS_ERR(watchdog)) {
|
|
|
|
output->watchdog = watchdog;
|
|
|
|
wake_up_process(watchdog);
|
|
|
|
}
|
2010-08-09 16:57:56 +00:00
|
|
|
#endif
|
|
|
|
output->instance_nr = ++count;
|
2010-08-03 16:03:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////// static structs ////////////////////////
|
|
|
|
|
|
|
|
static struct check_brick_ops check_brick_ops = {
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct check_output_ops check_output_ops = {
|
|
|
|
.make_object_layout = check_make_object_layout,
|
|
|
|
.mars_get_info = check_get_info,
|
2010-08-05 15:54:48 +00:00
|
|
|
.mars_ref_get = check_ref_get,
|
|
|
|
.mars_ref_put = check_ref_put,
|
|
|
|
.mars_ref_io = check_ref_io,
|
2010-08-03 16:03:32 +00:00
|
|
|
};
|
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
const struct check_input_type check_input_type = {
|
2010-08-03 16:03:32 +00:00
|
|
|
.type_name = "check_input",
|
|
|
|
.input_size = sizeof(struct check_input),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct check_input_type *check_input_types[] = {
|
|
|
|
&check_input_type,
|
|
|
|
};
|
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
const struct check_output_type check_output_type = {
|
2010-08-03 16:03:32 +00:00
|
|
|
.type_name = "check_output",
|
|
|
|
.output_size = sizeof(struct check_output),
|
|
|
|
.master_ops = &check_output_ops,
|
|
|
|
.output_construct = &check_output_construct,
|
|
|
|
.aspect_types = check_aspect_types,
|
|
|
|
.layout_code = {
|
2010-08-05 15:54:48 +00:00
|
|
|
[BRICK_OBJ_MARS_REF] = LAYOUT_ALL,
|
2010-08-03 16:03:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct check_output_type *check_output_types[] = {
|
|
|
|
&check_output_type,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct check_brick_type check_brick_type = {
|
|
|
|
.type_name = "check_brick",
|
|
|
|
.brick_size = sizeof(struct check_brick),
|
|
|
|
.max_inputs = 1,
|
|
|
|
.max_outputs = 1,
|
|
|
|
.master_ops = &check_brick_ops,
|
|
|
|
.default_input_types = check_input_types,
|
|
|
|
.default_output_types = check_output_types,
|
|
|
|
.brick_construct = &check_brick_construct,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(check_brick_type);
|
|
|
|
|
|
|
|
////////////////// module init stuff /////////////////////////
|
|
|
|
|
|
|
|
static int __init init_check(void)
|
|
|
|
{
|
|
|
|
printk(MARS_INFO "init_check()\n");
|
|
|
|
return check_register_brick_type();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit exit_check(void)
|
|
|
|
{
|
|
|
|
printk(MARS_INFO "exit_check()\n");
|
|
|
|
check_unregister_brick_type();
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("MARS check brick");
|
|
|
|
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
module_init(init_check);
|
|
|
|
module_exit(exit_check);
|