Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 303
b: refs/heads/master
c: 5c11c52
h: refs/heads/master
i:
  301: a4b25ce
  299: bec5e7a
  295: bc93221
  287: feef0a3
v: v3
  • Loading branch information
minyard@acm.org authored and Greg KH committed Apr 19, 2005
1 parent 4ceee47 commit c025114
Show file tree
Hide file tree
Showing 77 changed files with 709 additions and 588 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: c3c661932cd53582c5b03692b99649300977248a
refs/heads/master: 5c11c52046eb0f7252574bad161db53d0345ea50
1 change: 0 additions & 1 deletion trunk/Documentation/aoe/mkdevs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ n_partitions=${n_partitions:-16}

if test "$#" != "1"; then
echo "Usage: sh `basename $0` {dir}" 1>&2
echo " n_partitions=16 sh `basename $0` {dir}" 1>&2
exit 1
fi
dir=$1
Expand Down
1 change: 0 additions & 1 deletion trunk/Documentation/aoe/mkshelf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

if test "$#" != "2"; then
echo "Usage: sh `basename $0` {dir} {shelfaddress}" 1>&2
echo " n_partitions=16 sh `basename $0` {dir} {shelfaddress}" 1>&2
exit 1
fi
n_partitions=${n_partitions:-16}
Expand Down
14 changes: 0 additions & 14 deletions trunk/Documentation/aoe/todo.txt

This file was deleted.

6 changes: 1 addition & 5 deletions trunk/Documentation/aoe/udev-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ fi
# /etc/udev/rules.d
#
rules_d="`sed -n '/^udev_rules=/{ s!udev_rules=!!; s!\"!!g; p; }' $conf`"
if test -z "$rules_d" || test ! -d "$rules_d"; then
echo "$me Error: cannot find udev rules directory" 1>&2
exit 1
fi
sh -xc "cp `dirname $0`/udev.txt $rules_d/60-aoe.rules"
test "$rules_d" && sh -xc "cp `dirname $0`/udev.txt $rules_d/60-aoe.rules"
211 changes: 211 additions & 0 deletions trunk/Documentation/kref.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@

krefs allow you to add reference counters to your objects. If you
have objects that are used in multiple places and passed around, and
you don't have refcounts, your code is almost certainly broken. If
you want refcounts, krefs are the way to go.

To use a kref, add one to your data structures like:

struct my_data
{
.
.
struct kref refcount;
.
.
};

The kref can occur anywhere within the data structure.

You must initialize the kref after you allocate it. To do this, call
kref_init as so:

struct my_data *data;

data = kmalloc(sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
kref_init(&data->refcount);

This sets the refcount in the kref to 1.

Once you have an initialized kref, you must follow the following
rules:

1) If you make a non-temporary copy of a pointer, especially if
it can be passed to another thread of execution, you must
increment the refcount with kref_get() before passing it off:
kref_get(&data->refcount);
If you already have a valid pointer to a kref-ed structure (the
refcount cannot go to zero) you may do this without a lock.

2) When you are done with a pointer, you must call kref_put():
kref_put(&data->refcount, data_release);
If this is the last reference to the pointer, the release
routine will be called. If the code never tries to get
a valid pointer to a kref-ed structure without already
holding a valid pointer, it is safe to do this without
a lock.

3) If the code attempts to gain a reference to a kref-ed structure
without already holding a valid pointer, it must serialize access
where a kref_put() cannot occur during the kref_get(), and the
structure must remain valid during the kref_get().

For example, if you allocate some data and then pass it to another
thread to process:

void data_release(struct kref *ref)
{
struct my_data *data = container_of(ref, struct my_data, refcount);
kfree(data);
}

void more_data_handling(void *cb_data)
{
struct my_data *data = cb_data;
.
. do stuff with data here
.
kref_put(data, data_release);
}

int my_data_handler(void)
{
int rv = 0;
struct my_data *data;
struct task_struct *task;
data = kmalloc(sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
kref_init(&data->refcount);

kref_get(&data->refcount);
task = kthread_run(more_data_handling, data, "more_data_handling");
if (task == ERR_PTR(-ENOMEM)) {
rv = -ENOMEM;
kref_put(&data->refcount, data_release);
goto out;
}

.
. do stuff with data here
.
out:
kref_put(&data->refcount, data_release);
return rv;
}

This way, it doesn't matter what order the two threads handle the
data, the kref_put() handles knowing when the data is not referenced
any more and releasing it. The kref_get() does not require a lock,
since we already have a valid pointer that we own a refcount for. The
put needs no lock because nothing tries to get the data without
already holding a pointer.

Note that the "before" in rule 1 is very important. You should never
do something like:

task = kthread_run(more_data_handling, data, "more_data_handling");
if (task == ERR_PTR(-ENOMEM)) {
rv = -ENOMEM;
goto out;
} else
/* BAD BAD BAD - get is after the handoff */
kref_get(&data->refcount);

Don't assume you know what you are doing and use the above construct.
First of all, you may not know what you are doing. Second, you may
know what you are doing (there are some situations where locking is
involved where the above may be legal) but someone else who doesn't
know what they are doing may change the code or copy the code. It's
bad style. Don't do it.

There are some situations where you can optimize the gets and puts.
For instance, if you are done with an object and enqueuing it for
something else or passing it off to something else, there is no reason
to do a get then a put:

/* Silly extra get and put */
kref_get(&obj->ref);
enqueue(obj);
kref_put(&obj->ref, obj_cleanup);

Just do the enqueue. A comment about this is always welcome:

enqueue(obj);
/* We are done with obj, so we pass our refcount off
to the queue. DON'T TOUCH obj AFTER HERE! */

The last rule (rule 3) is the nastiest one to handle. Say, for
instance, you have a list of items that are each kref-ed, and you wish
to get the first one. You can't just pull the first item off the list
and kref_get() it. That violates rule 3 because you are not already
holding a valid pointer. You must add locks or semaphores. For
instance:

static DECLARE_MUTEX(sem);
static LIST_HEAD(q);
struct my_data
{
struct kref refcount;
struct list_head link;
};

static struct my_data *get_entry()
{
struct my_data *entry = NULL;
down(&sem);
if (!list_empty(&q)) {
entry = container_of(q.next, struct my_q_entry, link);
kref_get(&entry->refcount);
}
up(&sem);
return entry;
}

static void release_entry(struct kref *ref)
{
struct my_data *entry = container_of(ref, struct my_data, refcount);

list_del(&entry->link);
kfree(entry);
}

static void put_entry(struct my_data *entry)
{
down(&sem);
kref_put(&entry->refcount, release_entry);
up(&sem);
}

The kref_put() return value is useful if you do not want to hold the
lock during the whole release operation. Say you didn't want to call
kfree() with the lock held in the example above (since it is kind of
pointless to do so). You could use kref_put() as follows:

static void release_entry(struct kref *ref)
{
/* All work is done after the return from kref_put(). */
}

static void put_entry(struct my_data *entry)
{
down(&sem);
if (kref_put(&entry->refcount, release_entry)) {
list_del(&entry->link);
up(&sem);
kfree(entry);
} else
up(&sem);
}

This is really more useful if you have to call other routines as part
of the free operations that could take a long time or might claim the
same lock. Note that doing everything in the release routine is still
preferred as it is a little neater.


Corey Minyard <minyard@acm.org>

A lot of this was lifted from Greg KH's OLS presentation on krefs.
23 changes: 8 additions & 15 deletions trunk/drivers/block/aoe/aoe.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
/* Copyright (c) 2004 Coraid, Inc. See COPYING for GPL terms. */
#define VERSION "6"
#define VERSION "5"
#define AOE_MAJOR 152
#define DEVICE_NAME "aoe"

/* set AOE_PARTITIONS to 1 to use whole-disks only
* default is 16, which is 15 partitions plus the whole disk
*/
#ifndef AOE_PARTITIONS
#define AOE_PARTITIONS 16
#endif

#define SYSMINOR(aoemajor, aoeminor) ((aoemajor) * 10 + (aoeminor))
#define AOEMAJOR(sysminor) ((sysminor) / 10)
#define AOEMINOR(sysminor) ((sysminor) % 10)
Expand Down Expand Up @@ -39,13 +34,13 @@ enum {
struct aoe_hdr {
unsigned char dst[6];
unsigned char src[6];
__be16 type;
unsigned char type[2];
unsigned char verfl;
unsigned char err;
__be16 major;
unsigned char major[2];
unsigned char minor;
unsigned char cmd;
__be32 tag;
unsigned char tag[4];
};

struct aoe_atahdr {
Expand All @@ -63,8 +58,8 @@ struct aoe_atahdr {
};

struct aoe_cfghdr {
__be16 bufcnt;
__be16 fwver;
unsigned char bufcnt[2];
unsigned char fwver[2];
unsigned char res;
unsigned char aoeccmd;
unsigned char cslen[2];
Expand All @@ -90,7 +85,6 @@ enum {

struct buf {
struct list_head bufs;
ulong start_time; /* for disk stats */
ulong flags;
ulong nframesout;
char *bufaddr;
Expand Down Expand Up @@ -131,8 +125,7 @@ struct aoedev {
struct timer_list timer;
spinlock_t lock;
struct net_device *ifp; /* interface ed is attached to */
struct sk_buff *sendq_hd; /* packets needing to be sent, list head */
struct sk_buff *sendq_tl;
struct sk_buff *skblist;/* packets needing to be sent */
mempool_t *bufpool; /* for deadlock-free Buf allocation */
struct list_head bufq; /* queue of bios to work on */
struct buf *inprocess; /* the one we're currently working on */
Expand All @@ -158,7 +151,7 @@ void aoecmd_cfg_rsp(struct sk_buff *);

int aoedev_init(void);
void aoedev_exit(void);
struct aoedev *aoedev_by_aoeaddr(int maj, int min);
struct aoedev *aoedev_bymac(unsigned char *);
void aoedev_downdev(struct aoedev *d);
struct aoedev *aoedev_set(ulong, unsigned char *, struct net_device *, ulong);
int aoedev_busy(void);
Expand Down
5 changes: 2 additions & 3 deletions trunk/drivers/block/aoe/aoeblk.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ aoeblk_make_request(request_queue_t *q, struct bio *bio)
}
memset(buf, 0, sizeof(*buf));
INIT_LIST_HEAD(&buf->bufs);
buf->start_time = jiffies;
buf->bio = bio;
buf->resid = bio->bi_size;
buf->sector = bio->bi_sector;
Expand All @@ -147,8 +146,8 @@ aoeblk_make_request(request_queue_t *q, struct bio *bio)
list_add_tail(&buf->bufs, &d->bufq);
aoecmd_work(d);

sl = d->sendq_hd;
d->sendq_hd = d->sendq_tl = NULL;
sl = d->skblist;
d->skblist = NULL;

spin_unlock_irqrestore(&d->lock, flags);

Expand Down
Loading

0 comments on commit c025114

Please sign in to comment.