Skip to contentSkip to conversation

net/mlx4_core: Fix racy flow in the driver CQ completion handler

RFC PATCH Or Gerlitz 4 participants 9 messages Sep 9, 2012 – Sep 11, 2012 12 messages not in the archive InfiniBand

lore
Share & tools
lore

Or Gerlitz

author
#2 lore raw
In reply to a message not in the archive
Quoted 21 lines · Max Matveev
Quoted 13 lines · Roland Dreier
Quoted 11 lines · Or Gerlitz
Quoted 1 line · Roland Dreier

Can you be explicit about the race you're worried about?

few

1. on the time CQ A is deleted an interrupt that relates to CQ B takes place and a radix
tree lookup is running while an element is being deleted from the tree, looking on the radix tree API, I don't see that this is allowed.

Quoted 4 lines

I don't think this is a real problem; the radix tree code is explicitly designed for RCU use, and the data structure is pretty clearly safe for looking up one slot while another slot is being cleared. In fact it's hard to see how this could screw up.

Quoted 5 lines

What about races between radix_tree_extend and radix_tree_lookup?

As far as I can see (even on newer kernels with rcu support) there is nothing which protects changes to node->height and it is used to decide how deep lookup must go to find the actual data.

Max,

I'm putting your response on [email protected] as it was wrongly sent to [email protected] ...

Or.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#6 lore raw
In reply to a message not in the archive
Quoted 13 lines · Max Matveev

On Tue, 4 Sep 2012 11:02:32 -0700, Roland Dreier wrote:

Quoted 5 lines · Max Matveev
Quoted 5 lines

What about races between radix_tree_extend and radix_tree_lookup?

As far as I can see (even on newer kernels with rcu support) there is nothing which protects changes to node->height and it is used to decide how deep lookup must go to find the actual data.

roland> I assume things are ordered so that lookups that follow the roland> old data can still find old entries, and that updates become roland> visible in a consistent order.

Quoted 7 lines

I honestly do see how - the height of the root node is updated indepedently of the slots, so if someone managed to get the updated height there is nothing from stoping radix_tree_lookup from going too deep into the chain of slots.

Also most other places which call radix_tree_lookup do use some kind of lock around it.

same here putting your response on [email protected] as it was wrongly sent to [email protected] ...

Or.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
In reply to a message not in the archive
Quoted 4 lines · Or Gerlitz
Quoted 4 lines

I honestly do see how - the height of the root node is updated indepedently of the slots, so if someone managed to get the updated height there is nothing from stoping radix_tree_lookup from going too deep into the chain of slots.

Please look at commit 7cf9c2c76c1a ("[PATCH] radix-tree: RCU lockless readside")

Make radix tree lookups safe to be performed without locks. Readers are protected against nodes being deleted by using RCU based freeing. Readers are protected against new node insertion by using memory barriers to ensure the node itself will be properly written before it is visible in the radix tree.

etc. Also the API description in <linux/radix-tree.h> is helpful.

Before we randomly throw in locking, let's please have at least a theory about what race we're actually protecting against.

- R.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
In reply to a message not in the archive
Quoted 3 lines · Roland Dreier

Please look at commit 7cf9c2c76c1a ("[PATCH] radix-tree: RCU lockless readside")

Roland,

What about the following note (from the commit diff mentioned above):
+/**
+ * Radix-tree synchronization
+ *
+ * The radix-tree API requires that users provide all synchronisation (with
+ * specific exceptions, noted below).
+ *
+ * Synchronization of access to the data items being stored in the tree, and
+ * management of their lifetimes must be completely managed by API users.
+ *
+ * For API usage, in general,
+ * - any function _modifying_ the the tree or tags (inserting or deleting
+ * items, setting or clearing tags must exclude other modifications, and
+ * exclude any functions reading the tree.
+ * - any function _reading_ the the tree or tags (looking up items or tags,
+ * gang lookups) must exclude modifications to the tree, but may occur
+ * concurrently with other readers.
+ *
+ * The notable exceptions to this rule are the following functions:
+ * radix_tree_lookup
+ * radix_tree_tag_get
+ * radix_tree_gang_lookup
+ * radix_tree_gang_lookup_tag
+ * radix_tree_tagged
+ *

======= JPM -- Note the following text! =========================
+ * The first 4 functions are able to be called locklessly, using RCU. The
+ * caller must ensure calls to these functions are made within rcu_read_lock()
+ * regions.
===========================================

+ * Other readers (lock-free or otherwise) and modifications may be
+ * running concurrently.

Roland, the above states explicitly that radix_tree_lookup should be called only under rcu_read_lock()! This is NOT the case in our driver, in mlx4_eq_int()/mlx4_cq_completion() .

Looks to me, then, that the spinlock in mlx4_cq_completion() is required.

- Jack
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#12 lore raw
In reply to a message not in the archive
Quoted 23 lines · Jack Morgenstein

+ *
+ * For API usage, in general,
+ * - any function _modifying_ the the tree or tags (inserting or deleting
+ * items, setting or clearing tags must exclude other modifications, and
+ * exclude any functions reading the tree.
+ * - any function _reading_ the the tree or tags (looking up items or tags,
+ * gang lookups) must exclude modifications to the tree, but may occur
+ * concurrently with other readers.
+ *
+ * The notable exceptions to this rule are the following functions:
+ * radix_tree_lookup
+ * radix_tree_tag_get
+ * radix_tree_gang_lookup
+ * radix_tree_gang_lookup_tag
+ * radix_tree_tagged
+ *

======= JPM -- Note the following text! =========================
+ * The first 4 functions are able to be called locklessly, using RCU. The
+ * caller must ensure calls to these functions are made within rcu_read_lock()
+ * regions.
===========================================

Quoted 9 lines

+ * Other readers (lock-free or otherwise) and modifications may be
+ * running concurrently.

Roland, the above states explicitly that radix_tree_lookup should be called only under rcu_read_lock()! This is NOT the case in our driver, in mlx4_eq_int()/mlx4_cq_completion() .

Looks to me, then, that the spinlock in mlx4_cq_completion() is required.

Jack, Max

Actually, can't we do well with rcu_read_lock() in mlx4_cq_completion() as that commit documentation suggests?

Or.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#14 lore raw
In reply to a message not in the archive
Quoted 5 lines · Or Gerlitz

Jack,  Max

Actually, can't we do well with rcu_read_lock() in mlx4_cq_completion() as that commit documentation suggests?

I don't know. I do notice (in file include/linux/rcupdate.h) that rcu_read_lock/unlock
is meant to be used in the interrupt context. Would it be sufficient (besides rcu_read_lock/unlock calls) to add a call rcu_synchronize() in mlx4_cq_free (after calling synchronize_irq)?

Could we also then dispense with the spinlocks in mlx4_cq_event() as well?

Acquiring the SINGLE cq_table->lock spinlock for EVERY completion event of EVERY cq seems very nasty to me (probably why Roland did not do this), and it would clearly be desirable not to have to do this.

-Jack
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#16 lore raw
In reply to a message not in the archive
Quoted 1 line · Jack Morgenstein

I don't know. I do notice (in file include/linux/rcupdate.h) that rcu_read_lock/unlock is meant to be used in the interrupt context. Would it be sufficient (besides rcu_read_lock/unlock calls) to add a call rcu_synchronize() in mlx4_cq_free (after calling synchronize_irq)?

I took a look on the practice/wrapping used over the mm subsystem for radix_tree_lookup calls, whose maintainer, Andrew Morton is signed on the patch Roland pointed to, its just rcu_read_lock/unlock, seems this is what to do as well.

Quoted 8 lines

mm/readahead.c-179- rcu_read_lock();
mm/readahead.c:180: page = radix_tree_lookup(&mapping->page_tree, page_offset);
mm/readahead.c-181- rcu_read_unlock();
--
mm/shmem.c-278- rcu_read_lock();
mm/shmem.c:279: item = radix_tree_lookup(&mapping->page_tree, index); mm/shmem.c-280- rcu_read_unlock();
--

Quoted 6 lines
mm/vmalloc.c-986-       rcu_read_lock();
mm/vmalloc.c:987:       vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
mm/vmalloc.c-988-       rcu_read_unlock();

Could we also then dispense with the spinlocks in mlx4_cq_event() as well?

I don't see why mlx4_cq_event should be treated differently.

Quoted 1 line

Acquiring the SINGLE cq_table->lock spinlock for EVERY completion event of EVERY cq seems very nasty to me (probably why Roland did not do this), and it would clearly be desirable not to have to do this

so we have a way to avoid it, with rcu_read_lock, Max/Roland, agree?

Or.

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#18 lore raw
In reply to a message not in the archive
Quoted 5 lines · Or Gerlitz

I  took a look on the practice/wrapping used over the mm subsystem for radix_tree_lookup calls, whose maintainer,
Andrew Morton is signed on the patch Roland pointed to, its just rcu_read_lock/unlock, seems this is what to do as well.

In addition, need to do a synchronize_rcu when deleting, per the comment in include/linux/rcupdate.h:

* It is still required that the caller manage the synchronization and lifetimes
* of the items. So if RCU lock-free lookups are used, typically this would mean
* that the items have their own locks, or are amenable to lock-free access; and
* that the items are freed by RCU (or only freed after having been deleted from
* the radix tree *and* a synchronize_rcu() grace period).

-Jack
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#20 lore raw
In reply to a message not in the archive
Quoted 7 lines · Jack Morgenstein
Quoted 5 lines · Or Gerlitz

I took a look on the practice/wrapping used over the mm subsystem for radix_tree_lookup calls, whose maintainer,
Andrew Morton is signed on the patch Roland pointed to, its just rcu_read_lock/unlock, seems this is what to do as well.

In addition, need to do a synchronize_rcu when deleting

patch?
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html