ieee1394: reduce size of hpsb_host by 252 bytes
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
RFC PATCH 1/3 ieee1394: reduce size of hpsb_host by 252 bytes
Struct hpsb_host contains struct hpsb_tlabel_pool 63 times, therefore each word that can be shaved off of hpsb_tlabel_pool is a win.
Since hpsb_tlabel_pool.next is accessed twice as often as .allocations, .next is put into the lower part of the word. The value of .allocations rolls over after about 67 million now but we don't care.
--- drivers/ieee1394/ieee1394_transactions.c | 22 ++++++++++++---------- drivers/ieee1394/ieee1394_types.h | 9 +++++++-- 2 files changed, 19 insertions(+), 12 deletions(-)
drivers/ieee1394/ieee1394_types.h +7 -2
@@ -17,8 +17,13 @@struct hpsb_tlabel_pool { DECLARE_BITMAP(pool, 64);spinlock_t lock; -u8 next; -u32 allocations; +#ifdef __BIG_ENDIAN_BITFIELD +u32 allocations :26 __attribute__((packed)); +u32 next :6 __attribute__((packed)); +#else +u32 next :6 __attribute__((packed)); +u32 allocations :26 __attribute__((packed)); +#endif struct semaphore count; };
drivers/ieee1394/ieee1394_transactions.c +12 -10
@@ -136,7 +136,7 @@ int hpsb_get_tlabel(struct hpsb_packet * {unsigned long flags;struct hpsb_tlabel_pool *tp; -int n =NODEID_TO_NODE (packet->node_id); +int tlabel, n =NODEID_TO_NODE (packet->node_id);if (unlikely (n ==ALL_NODES ))return 0 ;
@@ -151,15 +151,17 @@ int hpsb_get_tlabel(struct hpsb_packet *spin_lock_irqsave (&tp->lock, flags); - packet->tlabel =find_next_zero_bit (tp->pool,64 , tp->next); -if (packet->tlabel >63 ) - packet->tlabel =find_first_zero_bit (tp->pool,64 ); - tp->next = (packet->tlabel +1 ) %64 ; + tlabel =find_next_zero_bit (tp->pool,64 , tp->next); +if (tlabel >63 ) + tlabel =find_first_zero_bit (tp->pool,64 ); +/* tp->next is 6 bits wide, thus rolls over from 63 to 0 */ + tp->next = (tlabel +1 );/* Should _never_ happen */ -BUG_ON (test_and_set_bit (packet->tlabel, tp->pool)); +BUG_ON (test_and_set_bit (tlabel, tp->pool)); tp->allocations++; -spin_unlock_irqrestore (&tp->lock, flags); +spin_unlock_irqrestore (&tp->lock, flags); + packet->tlabel = tlabel;return 0 ; }
@@ -178,16 +180,16 @@ void hpsb_free_tlabel(struct hpsb_packet {unsigned long flags;struct hpsb_tlabel_pool *tp; -int n =NODEID_TO_NODE (packet->node_id); +int tlabel = packet->tlabel, n =NODEID_TO_NODE (packet->node_id);if (unlikely (n ==ALL_NODES ))return ; tp = &packet->host->tpool[n]; -BUG_ON (packet->tlabel >63 || packet->tlabel <0 ); +BUG_ON (tlabel >63 || tlabel <0 );spin_lock_irqsave (&tp->lock, flags); -BUG_ON (!test_and_clear_bit (packet->tlabel, tp->pool)); +BUG_ON (!test_and_clear_bit (tlabel, tp->pool));spin_unlock_irqrestore (&tp->lock, flags);up (&tp->count);
RFC PATCH 2/3 ieee1394: coarser locking for tlabel allocation
Full parallelism in getting and freeing transaction labels of different nodes is not really required. Therefore reduce the number of locks for access to tlabel pools from 63 locks per host to 1 global lock.
--- drivers/ieee1394/ieee1394_transactions.c | 10 ++++++---- drivers/ieee1394/ieee1394_types.h | 2 -- 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/ieee1394/ieee1394_types.h +0 -2
@@ -16,7 +16,6 @@/* Transaction Label handling */ struct hpsb_tlabel_pool { DECLARE_BITMAP(pool, 64); -spinlock_t lock;#ifdef __BIG_ENDIAN_BITFIELDu32 allocations :26 __attribute__((packed));u32 next :6 __attribute__((packed));
@@ -30,7 +29,6 @@ struct hpsb_tlabel_pool {#define HPSB_TPOOL_INIT (_tp) \ do { \ bitmap_zero((_tp)->pool, 64); \ - spin_lock_init(&(_tp)->lock); \ (_tp)->next = 0; \ (_tp)->allocations = 0; \ sema_init(&(_tp)->count, 63); \
drivers/ieee1394/ieee1394_transactions.c +6 -4
@@ -31,6 +31,8 @@ packet->header[1 ] = (packet->host->node_id <<16 ) | (addr >>32 ); \ packet->header[2] = addr & 0xffffffff +static spinlock_t hpsb_tlabel_lock =SPIN_LOCK_UNLOCKED ; +static void fill_async_readquad (struct hpsb_packet *packet,u64 addr) {PREP_ASYNC_HEAD_ADDRESS (TCODE_READQ );
@@ -149,7 +151,7 @@ int hpsb_get_tlabel(struct hpsb_packet *down (&tp->count); } -spin_lock_irqsave (&tp->lock, flags); +spin_lock_irqsave (&hpsb_tlabel_lock, flags); tlabel =find_next_zero_bit (tp->pool,64 , tp->next);if (tlabel >63 )
@@ -160,7 +162,7 @@ int hpsb_get_tlabel(struct hpsb_packet *BUG_ON (test_and_set_bit (tlabel, tp->pool)); tp->allocations++; -spin_unlock_irqrestore (&tp->lock, flags); +spin_unlock_irqrestore (&hpsb_tlabel_lock, flags); packet->tlabel = tlabel;return 0 ; }
@@ -188,9 +190,9 @@ void hpsb_free_tlabel(struct hpsb_packetBUG_ON (tlabel >63 || tlabel <0 ); -spin_lock_irqsave (&tp->lock, flags); +spin_lock_irqsave (&hpsb_tlabel_lock, flags);BUG_ON (!test_and_clear_bit (tlabel, tp->pool)); -spin_unlock_irqrestore (&tp->lock, flags); +spin_unlock_irqrestore (&hpsb_tlabel_lock, flags);up (&tp->count); }
RFC PATCH 3/3 ieee1394: nodemgr: read tlabel attributes atomically
A node_entry's sysfs attributes tlabels_free, tlabels_allocations, and tlabels_mask may theoretically be sometimes wrong if they were read without taking the respective lock. Although this is not very serious since these attributes are rarely accessed and not interesting outside of driver or application development, why not do it properly.
---
applies after patch "ieee1394: nodemgr: do not peek into struct semaphore"
http://marc.theaimsgroup.com/?l=linux-mm-commits&m=115113030025655
drivers/ieee1394/ieee1394_transactions.c | 2 - drivers/ieee1394/ieee1394_transactions.h | 5 +++ drivers/ieee1394/nodemgr.c | 31 ++++++++++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-)
drivers/ieee1394/ieee1394_transactions.h +5 -0
@@ -1,6 +1,11 @@#ifndef _IEEE1394_TRANSACTIONS_H#define _IEEE1394_TRANSACTIONS_H +#include <linux/spinlock_types.h> + +/* internal to ieee1394 core */ +extern spinlock_t hpsb_tlabel_lock; +#include "ieee1394_core.h"
drivers/ieee1394/ieee1394_transactions.c +1 -1
@@ -31,7 +31,7 @@ packet->header[1 ] = (packet->host->node_id <<16 ) | (addr >>32 ); \ packet->header[2] = addr & 0xffffffff -static spinlock_t hpsb_tlabel_lock =SPIN_LOCK_UNLOCKED ; +spinlock_t hpsb_tlabel_lock =SPIN_LOCK_UNLOCKED ;static void fill_async_readquad (struct hpsb_packet *packet,u64 addr) {
drivers/ieee1394/nodemgr.c +25 -6
@@ -327,12 +327,17 @@ static ssize_t fw_show_ne_bus_options(st static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL); -/* tlabels_free, tlabels_allocations, tlabels_mask are read non-atomically -* here, therefore displayed values may be occasionally wrong. */ static ssize_t fw_show_ne_tlabels_free (struct device *dev,struct device_attribute *attr,char *buf) { struct node_entry *ne = container_of(dev, struct node_entry, device); -return sprintf (buf,"%d\n" ,64 -bitmap_weight (ne->tpool->pool,64 )); +unsigned long flags; +int tf; + +spin_lock_irqsave (&hpsb_tlabel_lock, flags); + tf =64 -bitmap_weight (ne->tpool->pool,64 ); +spin_unlock_irqrestore (&hpsb_tlabel_lock, flags); + +return sprintf (buf,"%d\n" , tf); } static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
@@ -340,7 +345,14 @@ static DEVICE_ATTR(tlabels_free,S_IRUGO,static ssize_t fw_show_ne_tlabels_allocations (struct device *dev,struct device_attribute *attr,char *buf) { struct node_entry *ne = container_of(dev, struct node_entry, device); -return sprintf (buf,"%u\n" , ne->tpool->allocations); +unsigned long flags; +int ta; + +spin_lock_irqsave (&hpsb_tlabel_lock, flags); + ta = ne->tpool->allocations; +spin_unlock_irqrestore (&hpsb_tlabel_lock, flags); + +return sprintf (buf,"%u\n" , ta); } static DEVICE_ATTR(tlabels_allocations,S_IRUGO,fw_show_ne_tlabels_allocations,NULL);
@@ -348,11 +360,18 @@ static DEVICE_ATTR(tlabels_allocations,Sstatic ssize_t fw_show_ne_tlabels_mask (struct device *dev,struct device_attribute *attr,char *buf) { struct node_entry *ne = container_of(dev, struct node_entry, device); +unsigned long flags; +u64 tm; + +spin_lock_irqsave (&hpsb_tlabel_lock, flags);#if (BITS_PER_LONG <=32 ) -return sprintf (buf,"0x%08lx%08lx\n" , ne->tpool->pool[0 ], ne->tpool->pool[1 ]); + tm = ((u64 )ne->tpool->pool[0 ] <<32 ) + ne->tpool->pool[1 ];#else -return sprintf (buf,"0x%016lx\n" , ne->tpool->pool[0 ]); + tm = ne->tpool->pool[0 ];#endif +spin_unlock_irqrestore (&hpsb_tlabel_lock, flags); + +return sprintf (buf,"0x%016llx\n" , tm); } static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
[RFC PATCH 2.6.17-mm1 4/3] ieee1394: convert ieee1394_transactions from semaphores to waitqueue
There were 63 instances of counting semaphores used for each hpsb_host. All of them are now replaced by a single wait_queue.
Note, it is rather unlikely that a process has to wait for a transaction label to become free. This condition occurs only if more than 64 transactions are initiated before the receiving node is able to complete any of them.
A slight change in behaviour is introduced: Allocation of a tlabel will fail if a signal was received while the process had to sleep until a tlabel becomes available. Previously, the process slept uninterruptibly on the semaphore until availability of a tlabel. Furthermore, error return values of hpsb_get_tlabel() are now -ERESTARTSYS or -EAGAIN instead of 1. However an error return value has always been defined as "non-zero" in the API and all in-tree callers respect that.
The macro HPSB_TPOOL_INIT is no longer necessary since struct hpsb_host is kzalloc'd.
The spinlock-protected code which accesses tlabel pools can use the non-atomic variants of bitmap manipulation functions.
--- drivers/ieee1394/hosts.c | 3 drivers/ieee1394/ieee1394_transactions.c | 82 ++++++++++++----------- drivers/ieee1394/ieee1394_types.h | 10 -- 3 files changed, 46 insertions(+), 49 deletions(-)
drivers/ieee1394/ieee1394_types.h +0 -10
@@ -23,18 +23,8 @@ struct hpsb_tlabel_pool { u32 next :6 __attribute__((packed)); u32 allocations :26 __attribute__((packed));#endif -struct semaphore count; }; -#define HPSB_TPOOL_INIT (_tp) \ -do { \ - bitmap_zero((_tp)->pool, 64); \ - (_tp)->next = 0; \ - (_tp)->allocations = 0; \ - sema_init(&(_tp)->count, 63); \ -} while (0) - -typedef u32 quadlet_t ;typedef u64 octlet_t ;typedef u16 nodeid_t ;
drivers/ieee1394/hosts.c +0 -3
@@ -143,9 +143,6 @@ struct hpsb_host *hpsb_alloc_host(structfor (i =2 ; i <16 ; i++) h->csr.gen_timestamp[i] = jiffies -60 *HZ ; -for (i =0 ; i <ARRAY_SIZE (h->tpool); i++) -HPSB_TPOOL_INIT (&h->tpool[i]); -atomic_set (&h->generation,0 );INIT_WORK (&h->delayed_reset, delayed_reset_bus, h);
drivers/ieee1394/ieee1394_transactions.c +46 -36
136 lines not on this page · raw message
Re: [RFC PATCH 2.6.17-mm1 4/3] ieee1394: convert ieee1394_transactions from semaphores to waitqueue
Quoted 2 lines · I wrote:
There were 63 instances of counting semaphores used for each
hpsb_host. All of them are now replaced by a singlewait_queue.
After this and patch "ieee1394: dv1394: sem2mutex conversion", the following semaphores remain in the ieee1394 subsystem:
highlevel.c: hl_drivers_sem (RW semaphore) nodemgr.c: subsys.rwsem (driver core's RW semaphores) raw1394.c: fi->complete_sem (completion semaphore)
Signature · 3 lines
Stefan Richter -=====-=-==- -==- ==--- http://arcgraph.de/sr/
Re: [RFC PATCH 2.6.17-mm1 4/3] ieee1394: convert ieee1394_transactions from semaphores to waitqueue
Quoted 10 lines · Stefan Richter
Quoted 2 lines · I wrote:
There were 63 instances of counting semaphores used for each
hpsb_host. All of them are now replaced by a singlewait_queue.After this and patch "ieee1394: dv1394: sem2mutex conversion", the following semaphores remain in the ieee1394 subsystem:
highlevel.c: hl_drivers_sem (RW semaphore) nodemgr.c: subsys.rwsem (driver core's RW semaphores) raw1394.c: fi->complete_sem (completion semaphore)
Hi,
can this last one move to an actual completion? That would get rid of it nicely ;)
Greetings,
Arjan van de Ven
Re: [RFC PATCH 2.6.17-mm1 4/3] ieee1394: convert ieee1394_transactions from semaphores to waitqueue
Quoted 9 lines · Arjan van de Ven
Quoted 5 lines · Stefan Richter
following semaphores remain in the ieee1394 subsystem:
highlevel.c: hl_drivers_sem (RW semaphore) nodemgr.c: subsys.rwsem (driver core's RW semaphores) raw1394.c: fi->complete_sem (completion semaphore)can this last one move to an actual completion? That would get rid of it nicely ;)
Hmm. There are dozens of points in raw1394 which call __queue_complete_req() which up()s the semaphore. ("fi" is the private_data of a struct file. Multiple outstanding requests may be associated with a file.) Then there are two places which wait on the semaphore:
raw1394_read(), called when somebody reads /dev/raw1394:
if (file->f_flags & O_NONBLOCK) {
if (down_trylock(&fi->complete_sem))
return -EAGAIN;
} else {
if (down_interruptible(&fi->complete_sem))
return -ERESTARTSYS;
}raw1394_release(), called when somebody closes (releases) /dev/raw1394:
done = 0;
while (!done) {
/* free all complete requests */
/* set "done" if there are no more pending requests */
if (!done)
down_interruptible(&fi->complete_sem);
}
/* cleanup, free fi */It looks like fi->complete_sem is a actually a counting semaphore. It could perhaps be replaced by a wait queue plus an atomic counter. There is even already a wait queue in "fi" for use with poll_wait() via raw1394_poll().
Signature · 3 lines
Stefan Richter -=====-=-==- -==- ==--- http://arcgraph.de/sr/
Re: [RFC PATCH 2.6.17-mm1 4/3] ieee1394: convert ieee1394_transactions from semaphores to waitqueue
Quoted 1 line · I wrote:
It looks like
fi->complete_semis a actually a counting semaphore. It could perhaps be replaced by a wait queue plus an atomic counter.
...which would be a semaphore. %-|
Signature · 3 lines
Stefan Richter -=====-=-==- -==- ==--- http://arcgraph.de/sr/
[RFC PATCH 2.6.17-mm1 5/3] ieee1394: raw1394: remove redundant counting semaphore
In reply to Arjan van de VenAn already existing wait queue replaces raw1394's complete_sem which was maintained in parallel to the wait queue. The role of the semaphore's counter is taken over by a direct check of what was really counted: The presence of items in the list of completed requests.
Notes:
- raw1394_release() sleeps uninterruptibly until all requests were completed. This is the same behaviour as before the patch.
- The macros wait_event and wait_event_interruptible are called with a condition argument which has a side effect, i.e. manipulation of the requests list. This side effect happens only if the condition is true. The patch relies on the fact that wait_event[_interruptible] does not evaluate the condition again after it became true.
- The diffstat looks unfavorable with respect to added lines of code. However 6 of them are comments, and some are due to separation of existing code blocks into two small helper functions.
--- drivers/ieee1394/raw1394-private.h | 3 - drivers/ieee1394/raw1394.c | 79 ++++++++++++++++------------- 2 files changed, 46 insertions(+), 36 deletions(-)
This patch won't apply without the -mm patch because of the list_move patch in -mm. Variants of this and other 1394 patches for older kernels can be found at http://me.in-berlin.de/~s5r6/linux1394/updates/.
I tested the patch on 2.6.16.x SMP PREEMPT x86 K7 uniprocessor with the libraw1394 apps 1394commander and gscanbus, issuing asynchronous transactions, bus resets, and PHY packets, which trigger raw1394_open, raw1394_write, raw1394_read, raw1394_poll, raw1394_release.
drivers/ieee1394/raw1394-private.h +1 -2
@@ -29,9 +29,8 @@ struct file_info {struct list_head req_pending;struct list_head req_complete; -struct semaphore complete_sem;spinlock_t reqlists_lock; -wait_queue_head_t poll_wait_complete; +wait_queue_head_t wait_complete;struct list_head addr_list;
drivers/ieee1394/raw1394.c +45 -34
138 lines not on this page · raw message
Re: [RFC PATCH 2.6.17-mm1 5/3] ieee1394: raw1394: remove redundant counting semaphore
I wrote:
...
Quoted 1 line
(quoted diff) +0
-0@@ -2864,25 +2877,23 @@ static int raw1394_release(struct inode
...
drivers/ieee1394/raw1394.c static int raw1394_release(struct inode · line 2896
Quoted 4 lines
drivers/ieee1394/raw1394.c +4
-0+/* Sleep until more requests can be freed. +* FIXME? This sleeps uninterruptibly. */ +wait_event (fi->wait_complete, (req =next_complete_req (fi))); +free_pending_request (req);
We certainly cannot allow anybody to close the file with pending or orphaned complete requests, so maybe I should white-out that FIXME.
Signature · 3 lines
Stefan Richter -=====-=-==- -==- ==--= http://arcgraph.de/sr/
Re: [RFC PATCH 2.6.17-mm1 4/3] ieee1394: convert ieee1394_transactions from semaphores to waitqueue
In reply to Stefan RichterI wrote on 2006-06-24:
drivers/ieee1394/ieee1394_transactions.c int hpsb_get_tlabel(struct hpsb_packet * · line 177
Quoted 2 lines
drivers/ieee1394/ieee1394_transactions.c +2
-0+return wait_event_interruptible (tlabel_wq, + !hpsb_get_tlabel_atomic (packet));
Hmm. "Linux Device Drivers" says about wait_event_interruptible(wq, condition): ''Note that @condition may be evaluated an arbitrary number of times, so it should not have any side effects.''
Alas the hpsb_get_tlabel_atomic() _does_ have a side effect, but only when !hpsb_get_tlabel_atomic(packet) is true.
The current implementation of wait_event_interruptible() seems to evaluate @condition multiple times if it is false but only _once_ while it is true. May I rely on this fact or do I have to rewrite the condition to be completely free of side effects?
I don't believe there would be ever a sensible implementation of wait_event_interruptible() which would evaluate @condition again after it became true.
Thanks,
Signature · 3 lines
Stefan Richter -=====-=-==- -==- ==--= http://arcgraph.de/sr/