convert the node watch code to use kmem_alloc() instead of malloc()

This commit is contained in:
jdolecek 2020-04-07 14:07:01 +00:00
parent 786f885750
commit 418f6d13ac
4 changed files with 43 additions and 48 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: xenbus.h,v 1.18 2020/04/07 13:38:50 jdolecek Exp $ */
/* $NetBSD: xenbus.h,v 1.19 2020/04/07 14:07:01 jdolecek Exp $ */
/******************************************************************************
* xenbus.h
*
@ -58,6 +58,7 @@ struct xenbus_watch {
/* Path being watched. */
char *node;
size_t node_sz;
/* Callback (executed in a process context with no locks held). */
void (*xbw_callback)(struct xenbus_watch *,
@ -180,6 +181,8 @@ int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
void (*callback)(struct xenbus_watch *,
const char **, unsigned int));
/* Unregister the watch, and free associated internal structures. */
void xenbus_unwatch_path(struct xenbus_watch *);
/**
* Advertise in the store a change of the given driver to the given new_state.

View File

@ -1,4 +1,4 @@
/* $NetBSD: xbdback_xenbus.c,v 1.76 2020/04/07 13:36:22 jdolecek Exp $ */
/* $NetBSD: xbdback_xenbus.c,v 1.77 2020/04/07 14:07:01 jdolecek Exp $ */
/*
* Copyright (c) 2006 Manuel Bouyer.
@ -26,7 +26,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: xbdback_xenbus.c,v 1.76 2020/04/07 13:36:22 jdolecek Exp $");
__KERNEL_RCSID(0, "$NetBSD: xbdback_xenbus.c,v 1.77 2020/04/07 14:07:01 jdolecek Exp $");
#include <sys/atomic.h>
#include <sys/buf.h>
@ -487,11 +487,9 @@ xbdback_xenbus_destroy(void *arg)
xbdback_disconnect(xbdi);
/* unregister watch */
if (xbdi->xbdi_watch.node) {
unregister_xenbus_watch(&xbdi->xbdi_watch);
free(xbdi->xbdi_watch.node, M_DEVBUF);
xbdi->xbdi_watch.node = NULL;
}
if (xbdi->xbdi_watch.node)
xenbus_unwatch_path(&xbdi->xbdi_watch);
/* unmap ring */
if (xbdi->xbdi_ring_va != 0) {
ungrop.host_addr = xbdi->xbdi_ring_va;

View File

@ -1,4 +1,4 @@
/* $NetBSD: xenbus_client.c,v 1.15 2020/04/07 13:38:50 jdolecek Exp $ */
/* $NetBSD: xenbus_client.c,v 1.16 2020/04/07 14:07:01 jdolecek Exp $ */
/******************************************************************************
* Client-facing interface for the Xenbus driver. In other words, the
* interface between the Xenbus and the device-specific code, be it the
@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.15 2020/04/07 13:38:50 jdolecek Exp $");
__KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.16 2020/04/07 14:07:01 jdolecek Exp $");
#if 0
#define DPRINTK(fmt, args...) \
@ -42,6 +42,7 @@ __KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.15 2020/04/07 13:38:50 jdolecek
#include <sys/null.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <xen/xen.h>
@ -50,30 +51,6 @@ __KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.15 2020/04/07 13:38:50 jdolecek
#include <xen/xenbus.h>
#include <xen/granttables.h>
static int
xenbus_watch_path(struct xenbus_device *dev, char *path,
struct xenbus_watch *watch,
void (*callback)(struct xenbus_watch *,
const char **, unsigned int))
{
int err;
watch->node = path;
watch->xbw_callback = callback;
err = register_xenbus_watch(watch);
if (err) {
watch->node = NULL;
watch->xbw_callback = NULL;
xenbus_dev_fatal(dev, err, "adding watch on %s", path);
}
err = 0;
return err;
}
int
xenbus_watch_path2(struct xenbus_device *dev, const char *path,
const char *path2, struct xenbus_watch *watch,
@ -84,21 +61,37 @@ xenbus_watch_path2(struct xenbus_device *dev, const char *path,
char *state;
DPRINTK("xenbus_watch_path2 path %s path2 %s\n", path, path2);
state =
malloc(strlen(path) + 1 + strlen(path2) + 1, M_DEVBUF,
M_WAITOK);
watch->node_sz = strlen(path) + 1 + strlen(path2) + 1;
state = kmem_alloc(watch->node_sz, KM_SLEEP);
strcpy(state, path);
strcat(state, "/");
strcat(state, path2);
err = xenbus_watch_path(dev, state, watch, callback);
watch->node = state;
watch->xbw_callback = callback;
err = register_xenbus_watch(watch);
if (err) {
free(state, M_DEVBUF);
watch->node = NULL;
watch->node_sz = 0;
watch->xbw_callback = NULL;
xenbus_dev_fatal(dev, err, "adding watch on %s", state);
kmem_free(state, watch->node_sz);
}
return err;
}
void
xenbus_unwatch_path(struct xenbus_watch *watch)
{
if (watch->node != NULL) {
unregister_xenbus_watch(watch);
kmem_free(watch->node, watch->node_sz);
watch->node = NULL;
}
}
int
xenbus_switch_state(struct xenbus_device *dev,

View File

@ -1,4 +1,4 @@
/* $NetBSD: xenbus_probe.c,v 1.43 2020/04/07 13:36:22 jdolecek Exp $ */
/* $NetBSD: xenbus_probe.c,v 1.44 2020/04/07 14:07:01 jdolecek Exp $ */
/******************************************************************************
* Talks to Xen Store to figure out what devices we have.
*
@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.43 2020/04/07 13:36:22 jdolecek Exp $");
__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.44 2020/04/07 14:07:01 jdolecek Exp $");
#if 0
#define DPRINTK(fmt, args...) \
@ -42,6 +42,7 @@ __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.43 2020/04/07 13:36:22 jdolecek E
#include <sys/null.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/kthread.h>
@ -232,11 +233,8 @@ free_otherend_details(struct xenbus_device *dev)
static void
free_otherend_watch(struct xenbus_device *dev)
{
if (dev->xbusd_otherend_watch.node) {
unregister_xenbus_watch(&dev->xbusd_otherend_watch);
free(dev->xbusd_otherend_watch.node, M_DEVBUF);
dev->xbusd_otherend_watch.node = NULL;
}
if (dev->xbusd_otherend_watch.node)
xenbus_unwatch_path(&dev->xbusd_otherend_watch);
}
static void
@ -615,11 +613,14 @@ xenbus_probe(void *unused)
xenbus_probe_backends();
/* Watch for changes. */
fe_watch.node = malloc(strlen("device") + 1, M_DEVBUF, M_NOWAIT);
fe_watch.node_sz = strlen("device") + 1;
fe_watch.node = kmem_alloc(fe_watch.node_sz, KM_SLEEP);
strcpy(fe_watch.node, "device");
fe_watch.xbw_callback = frontend_changed;
register_xenbus_watch(&fe_watch);
be_watch.node = malloc(strlen("backend") + 1, M_DEVBUF, M_NOWAIT);
be_watch.node_sz = strlen("backend") + 1;
be_watch.node = kmem_alloc(be_watch.node_sz, KM_SLEEP);
strcpy(be_watch.node, "backend");
be_watch.xbw_callback = backend_changed;
register_xenbus_watch(&be_watch);