Re: kernel - MPSAFE kern_mpipe.c
--0016e6d9780147ffa9048fd20126
Content-Type: text/plain; charset=UTF-8
A patch to convert to a per-pipe token; it hasn't exploded my vkernel yet...
:D
diff --git a/sys/kern/kern_mpipe.c b/sys/kern/kern_mpipe.c
index 8a8559a..5b4a667 100644
--- a/sys/kern/kern_mpipe.c
+++ b/sys/kern/kern_mpipe.c
@@ -53,8 +53,6 @@
static MALLOC_DEFINE(M_MPIPEARY, "MPipe Array", "Auxillary MPIPE
structure");
-static struct lwkt_token mpipe_token =
LWKT_TOKEN_MP_INITIALIZER(mpipe_token);
-
/*
* Initialize a malloc pipeline for the specified malloc type and
allocation
* size. Create an array to cache up to nom_count buffers and preallocate
@@ -93,6 +91,8 @@ mpipe_init(malloc_pipe_t mpipe, malloc_type_t type, int
bytes,
++mpipe->free_count;
++mpipe->total_count;
}
+
+ lwkt_token_init(&mpipe->token, 1, "malloc pipeline token");
}
/*
@@ -121,6 +121,8 @@ mpipe_done(malloc_pipe_t mpipe)
kfree(mpipe->array, M_MPIPEARY);
mpipe->array = NULL;
}
+
+ lwkt_token_uninit(&mpipe->token);
}
/*
@@ -135,7 +137,7 @@ mpipe_alloc_nowait(malloc_pipe_t mpipe)
void *buf;
int n;
- lwkt_gettoken(&mpipe_token);
+ lwkt_gettoken(&mpipe->token);
if ((n = mpipe->free_count) != 0) {
/*
* Use a free entry if it exists.
@@ -157,7 +159,7 @@ mpipe_alloc_nowait(malloc_pipe_t mpipe)
if (buf)
++mpipe->total_count;
}
- lwkt_reltoken(&mpipe_token);
+ lwkt_reltoken(&mpipe->token);
return(buf);
}
@@ -172,7 +174,7 @@ mpipe_alloc_waitok(malloc_pipe_t mpipe)
int n;
int mfailed;
- lwkt_gettoken(&mpipe_token);
+ lwkt_gettoken(&mpipe->token);
mfailed = 0;
for (;;) {
if ((n = mpipe->free_count) != 0) {
@@ -204,7 +206,7 @@ mpipe_alloc_waitok(malloc_pipe_t mpipe)
}
mfailed = 1;
}
- lwkt_reltoken(&mpipe_token);
+ lwkt_reltoken(&mpipe->token);
return(buf);
}
@@ -219,7 +221,7 @@ mpipe_free(malloc_pipe_t mpipe, void *buf)
if (buf == NULL)
return;
- lwkt_gettoken(&mpipe_token);
+ lwkt_gettoken(&mpipe->token);
if ((n = mpipe->free_count) < mpipe->ary_count) {
/*
* Free slot available in free array (LIFO)
@@ -228,7 +230,7 @@ mpipe_free(malloc_pipe_t mpipe, void *buf)
++mpipe->free_count;
if ((mpipe->mpflags & (MPF_CACHEDATA|MPF_NOZERO)) == 0)
bzero(buf, mpipe->bytes);
- lwkt_reltoken(&mpipe_token);
+ lwkt_reltoken(&mpipe->token);
/*
* Wakeup anyone blocked in mpipe_alloc_*().
@@ -245,7 +247,7 @@ mpipe_free(malloc_pipe_t mpipe, void *buf)
KKASSERT(mpipe->total_count >= mpipe->free_count);
if (mpipe->deconstruct)
mpipe->deconstruct(mpipe, buf);
- lwkt_reltoken(&mpipe_token);
+ lwkt_reltoken(&mpipe->token);
kfree(buf, mpipe->type);
}
}
diff --git a/sys/sys/mpipe.h b/sys/sys/mpipe.h
index 832b518..b54959e 100644
--- a/sys/sys/mpipe.h
+++ b/sys/sys/mpipe.h
@@ -83,6 +83,7 @@ struct malloc_pipe {
int total_count; /* total outstanding allocations incl free */
int ary_count; /* guarenteed allocation count */
int max_count; /* maximum count (M_NOWAIT used beyond nom) */
+ lwkt_token token;
void **array; /* array[ary_count] */
void (*deconstruct)(struct malloc_pipe *, void *buf);
};
-- vs
--0016e6d9780147ffa9048fd20126
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div>A patch to convert to a per-pipe token; it hasn't exploded my vker=
nel yet... :D</div><div><br></div><div>diff --git a/sys/kern/kern_mpipe.c b=
/sys/kern/kern_mpipe.c</div><div>index 8a8559a..5b4a667 100644</div><div>
--- a/sys/kern/kern_mpipe.c</div><div>+++ b/sys/kern/kern_mpipe.c</div><div=
>@@ -53,8 +53,6 @@</div><div>=C2=A0</div><div>=C2=A0static MALLOC_DEFINE(M_=
MPIPEARY, "MPipe Array", "Auxillary MPIPE structure");<=
/div>
<div>
=C2=A0</div><div>-static struct lwkt_token mpipe_token =3D LWKT_TOKEN_MP_IN=
ITIALIZER(mpipe_token);</div><div>-</div><div>=C2=A0/*</div><div>=C2=A0=C2=
=A0* Initialize a malloc pipeline for the specified malloc type and allocat=
ion</div><div>=C2=A0=C2=A0* size. =C2=A0Create an array to cache up to nom_=
count buffers and preallocate</div>
<div>@@ -93,6 +91,8 @@ mpipe_init(malloc_pipe_t mpipe, malloc_type_t type, =
int bytes,</div><div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre"> </span>++mpipe->free_count;</div><div>=C2=A0<span class=3D"App=
le-tab-span" style=3D"white-space:pre"> </span>++mpipe->total_count;</di=
v>
<div>=C2=A0=C2=A0 =C2=A0 }</div><div>+</div><div>+ =C2=A0 lwkt_token_init(&=
amp;mpipe->token, 1, "malloc pipeline token");</div><div>=C2=
=A0}</div><div>=C2=A0</div><div>=C2=A0/*</div><div>@@ -121,6 +121,8 @@ mpip=
e_done(malloc_pipe_t mpipe)</div>
<div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span=
>kfree(mpipe->array, M_MPIPEARY);</div><div>=C2=A0<span class=3D"Apple-t=
ab-span" style=3D"white-space:pre"> </span>mpipe->array =3D NULL;</div><=
div>=C2=A0=C2=A0 =C2=A0 }</div>
<div>+</div><div>+ =C2=A0 =C2=A0lwkt_token_uninit(&mpipe->token);</d=
iv><div>=C2=A0}</div><div>=C2=A0</div><div>=C2=A0/*</div><div>@@ -135,7 +13=
7,7 @@ mpipe_alloc_nowait(malloc_pipe_t mpipe)</div><div>=C2=A0=C2=A0 =C2=
=A0 void *buf;</div><div>=C2=A0=C2=A0 =C2=A0 int n;</div>
<div>=C2=A0</div><div>- =C2=A0 =C2=A0lwkt_gettoken(&mpipe_token);</div>=
<div>+ =C2=A0 =C2=A0lwkt_gettoken(&mpipe->token);</div><div>=C2=A0=
=C2=A0 =C2=A0 if ((n =3D mpipe->free_count) !=3D 0) {</div><div>=C2=A0<s=
pan class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>/*</div>
<div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span=
> * Use a free entry if it exists.</div><div>@@ -157,7 +159,7 @@ mpipe_allo=
c_nowait(malloc_pipe_t mpipe)</div><div>=C2=A0<span class=3D"Apple-tab-span=
" style=3D"white-space:pre"> </span>if (buf)</div>
<div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span=
> =C2=A0 =C2=A0++mpipe->total_count;</div><div>=C2=A0=C2=A0 =C2=A0 }</di=
v><div>- =C2=A0 =C2=A0lwkt_reltoken(&mpipe_token);</div><div>+ =C2=A0 =
=C2=A0lwkt_reltoken(&mpipe->token);</div><div>
=C2=A0=C2=A0 =C2=A0 return(buf);</div><div>=C2=A0}</div><div>=C2=A0</div><d=
iv>@@ -172,7 +174,7 @@ mpipe_alloc_waitok(malloc_pipe_t mpipe)</div><div>=
=C2=A0=C2=A0 =C2=A0 int n;</div><div>=C2=A0=C2=A0 =C2=A0 int mfailed;</div>=
<div>=C2=A0</div><div>- =C2=A0 =C2=A0lwkt_gettoken(&mpipe_token);</div>
<div>+ =C2=A0 =C2=A0lwkt_gettoken(&mpipe->token);</div><div>=C2=A0=
=C2=A0 =C2=A0 mfailed =3D 0;</div><div>=C2=A0=C2=A0 =C2=A0 for (;;) {</div>=
<div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span=
>if ((n =3D mpipe->free_count) !=3D 0) {</div><div>
@@ -204,7 +206,7 @@ mpipe_alloc_waitok(malloc_pipe_t mpipe)</div><div>=C2=
=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>}</div>=
<div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span=
>mfailed =3D 1;</div>
<div>=C2=A0=C2=A0 =C2=A0 }</div><div>- =C2=A0 =C2=A0lwkt_reltoken(&mpip=
e_token);</div><div>+ =C2=A0 =C2=A0lwkt_reltoken(&mpipe->token);</di=
v><div>=C2=A0=C2=A0 =C2=A0 return(buf);</div><div>=C2=A0}</div><div>=C2=A0<=
/div><div>@@ -219,7 +221,7 @@ mpipe_free(malloc_pipe_t mpipe, void *buf)</d=
iv>
<div>=C2=A0=C2=A0 =C2=A0 if (buf =3D=3D NULL)</div><div>=C2=A0<span class=
=3D"Apple-tab-span" style=3D"white-space:pre"> </span>return;</div><div>=C2=
=A0</div><div>- =C2=A0 =C2=A0lwkt_gettoken(&mpipe_token);</div><div>+ =
=C2=A0 =C2=A0lwkt_gettoken(&mpipe->token);</div>
<div>=C2=A0=C2=A0 =C2=A0 if ((n =3D mpipe->free_count) < mpipe->ar=
y_count) {</div><div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre"> </span>/*</div><div>=C2=A0<span class=3D"Apple-tab-span" style=3D=
"white-space:pre"> </span> * Free slot available in free array (LIFO)</div>
<div>@@ -228,7 +230,7 @@ mpipe_free(malloc_pipe_t mpipe, void *buf)</div><d=
iv>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>+=
+mpipe->free_count;</div><div>=C2=A0<span class=3D"Apple-tab-span" style=
=3D"white-space:pre"> </span>if ((mpipe->mpflags & (MPF_CACHEDATA|MP=
F_NOZERO)) =3D=3D 0)=C2=A0</div>
<div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span=
> =C2=A0 =C2=A0bzero(buf, mpipe->bytes);</div><div>-<span class=3D"Apple=
-tab-span" style=3D"white-space:pre"> </span>lwkt_reltoken(&mpipe_token=
);</div><div>+<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </s=
pan>lwkt_reltoken(&mpipe->token);</div>
<div>=C2=A0</div><div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-s=
pace:pre"> </span>/*</div><div>=C2=A0<span class=3D"Apple-tab-span" style=
=3D"white-space:pre"> </span> * Wakeup anyone blocked in mpipe_alloc_*().</=
div><div>@@ -245,7 +247,7 @@ mpipe_free(malloc_pipe_t mpipe, void *buf)</di=
v>
<div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span=
>KKASSERT(mpipe->total_count >=3D mpipe->free_count);</div><div>=
=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>if (=
mpipe->deconstruct)</div>
<div>=C2=A0<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span=
> =C2=A0 =C2=A0mpipe->deconstruct(mpipe, buf);</div><div>-<span class=3D=
"Apple-tab-span" style=3D"white-space:pre"> </span>lwkt_reltoken(&mpipe=
_token);</div><div>
+<span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>lwkt_relt=
oken(&mpipe->token);</div><div>=C2=A0<span class=3D"Apple-tab-span" =
style=3D"white-space:pre"> </span>kfree(buf, mpipe->type);</div><div>=C2=
=A0=C2=A0 =C2=A0 }</div>
<div>=C2=A0}</div><div>diff --git a/sys/sys/mpipe.h b/sys/sys/mpipe.h</div>=
<div>index 832b518..b54959e 100644</div><div>--- a/sys/sys/mpipe.h</div><di=
v>+++ b/sys/sys/mpipe.h</div><div>@@ -83,6 +83,7 @@ struct malloc_pipe {</d=
iv>
<div>=C2=A0=C2=A0 =C2=A0 int<span class=3D"Apple-tab-span" style=3D"white-s=
pace:pre"> </span>total_count;<span class=3D"Apple-tab-span" style=3D"whit=
e-space:pre"> </span>/* total outstanding allocations incl free */</div><di=
v>=C2=A0=C2=A0 =C2=A0 int<span class=3D"Apple-tab-span" style=3D"white-spac=
e:pre"> </span>ary_count;<span class=3D"Apple-tab-span" style=3D"white-spa=
ce:pre"> </span>/* guarenteed allocation count */</div>
<div>=C2=A0=C2=A0 =C2=A0 int<span class=3D"Apple-tab-span" style=3D"white-s=
pace:pre"> </span>max_count;<span class=3D"Apple-tab-span" style=3D"white-=
space:pre"> </span>/* maximum count (M_NOWAIT used beyond nom) */</div><div=
>+ =C2=A0 =C2=A0lwkt_token<span class=3D"Apple-tab-span" style=3D"white-spa=
ce:pre"> </span>token;</div>
<div>=C2=A0=C2=A0 =C2=A0 void<span class=3D"Apple-tab-span" style=3D"white-=
space:pre"> </span>**array;<span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre"> </span>/* array[ary_count] */</div><div>=C2=A0=C2=A0 =C2=A0 void<=
span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>(*deconstru=
ct)(struct malloc_pipe *, void *buf);</div>
<div>=C2=A0};</div><div><br></div>-- vs<br>
--0016e6d9780147ffa9048fd20126--
討論串 (同標題文章)