Skip to contentSkip to conversation

IB/core: Make ib_mad_client_id atomic

PATCH In review Håkon Bugge 6 participants 17 messages Apr 18, 2018 – May 1, 2018 2 messages not in the archive InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 IB/core: Make ib_mad_client_id atomic

Two kernel threads may get the same value for agent.hi_tid, if the agents are registered for different ports. As of now, this works, as the agent list is per port.

It is however confusing and not future robust. Hence, making it atomic.

---
 drivers/infiniband/core/mad.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/infiniband/core/mad.c +2 -2
index c50596f7f98a..b28452a55a08 100644
@@ -59,7 +59,7 @@ module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
 MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
 
 static struct list_head ib_mad_port_list;
-static u32 ib_mad_client_id = 0;
+static atomic_t ib_mad_client_id = ATOMIC_INIT(0);
 
 /* Port list lock */
 static DEFINE_SPINLOCK(ib_mad_port_list_lock);
@@ -377,7 +377,7 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
 	}
 
 	spin_lock_irqsave(&port_priv->reg_lock, flags);
-	mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
+	mad_agent_priv->agent.hi_tid = atomic_inc_return(&ib_mad_client_id);
 
 	/*
 	 * Make sure MAD registration (if supplied)
Signature · 1 line
2.13.6
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines

Two kernel threads may get the same value for agent.hi_tid, if the agents are registered for different ports. As of now, this works, as the agent list is per port.

It is however confusing and not future robust. Hence, making it atomic.

Quoted 31 lines
---
 drivers/infiniband/core/mad.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/infiniband/core/mad.c index renamed from drivers/infiniband/core/mad.c +0 -0

c50596f7f98a..b28452a55a08 100644

drivers/infiniband/core/mad.c +0 -0
@@ -59,7 +59,7 @@ module_param_named(recv_queue_size, mad_recvq_size,

int, 0444); MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");

static struct list_head ib_mad_port_list;

drivers/infiniband/core/mad.c +2 -2
-static u32 ib_mad_client_id = 0;
+static atomic_t ib_mad_client_id = ATOMIC_INIT(0);
 
 /* Port list lock */
 static DEFINE_SPINLOCK(ib_mad_port_list_lock);
@@ -377,7 +377,7 @@ struct ib_mad_agent *ib_register_mad_agent(struct
 ib_device *device,
 	}
 
 	spin_lock_irqsave(&port_priv->reg_lock, flags);
-	mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
+	mad_agent_priv->agent.hi_tid =
 atomic_inc_return(&ib_mad_client_id);
 
drivers/infiniband/core/mad.c +0 -1
 	/*
 	 * Make sure MAD registration (if supplied)
--

2.13.6

Reviewed-by on v1 1/1
#2 lore raw
In reply to Håkon Bugge
Quoted 9 lines · Håkon Bugge

Two kernel threads may get the same value for agent.hi_tid, if the agents are registered for different ports. As of now, this works, as the agent list is per port.

It is however confusing and not future robust. Hence, making it atomic.

Quoted 26 lines
---
  drivers/infiniband/core/mad.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/infiniband/core/mad.c +2 -2
index c50596f7f98a..b28452a55a08 100644
@@ -59,7 +59,7 @@ module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
  MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
 
  static struct list_head ib_mad_port_list;
-static u32 ib_mad_client_id = 0;
+static atomic_t ib_mad_client_id = ATOMIC_INIT(0);
 
  /* Port list lock */
  static DEFINE_SPINLOCK(ib_mad_port_list_lock);
@@ -377,7 +377,7 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
  	}
 
  	spin_lock_irqsave(&port_priv->reg_lock, flags);
-	mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
+	mad_agent_priv->agent.hi_tid = atomic_inc_return(&ib_mad_client_id);
 
  	/*
  	 * Make sure MAD registration (if supplied)
#3 lore raw
In reply to Håkon Bugge
Quoted 7 lines · Håkon Bugge

Two kernel threads may get the same value for agent.hi_tid, if the agents are registered for different ports. As of now, this works, as the agent list is per port.

It is however confusing and not future robust. Hence, making it atomic.

People sometimes underestimate the performance penalty of atomic ops. Every atomic op is the equivalent of a spin_lock/spin_unlock pair. This is why two atomics are worse than taking a spin_lock, doing what you have to do, and releasing the spin_lock. Is this really what you want for a "confusing, let's make it robust" issue?

Signature · 3 lines
Doug Ledford <[email protected]>
    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD
#4 lore raw
Quoted 9 lines · Doug Ledford
Quoted 7 lines · Håkon Bugge

Two kernel threads may get the same value for agent.hi_tid, if the agents are registered for different ports. As of now, this works, as the agent list is per port.

It is however confusing and not future robust. Hence, making it atomic.

Quoted 5 lines

People sometimes underestimate the performance penalty of atomic ops. Every atomic op is the equivalent of a spin_lock/spin_unlock pair. This is why two atomics are worse than taking a spin_lock, doing what you have to do, and releasing the spin_lock. Is this really what you want for a "confusing, let's make it robust" issue?

But it is on the ib_register_mad_agent() path which is not a performance path..

This actually looks like a genuine bug, why is it described only as 'confusing'? ib_register_mad_agent is callable from userspace, so at least two userspace agents can race and get the same TID's.

TIDs need to be globally unique on the entire machine.

Jason

#5 lore raw

+Jack

Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Doug Ledford
Quoted 4 lines · Håkon Bugge

Two kernel threads may get the same value for agent.hi_tid, if the agents are registered for different ports. As of now, this works, as the agent list is per port.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

It is however confusing and not future robust. Hence, making it atomic.

People sometimes underestimate the performance penalty of atomic ops. Every atomic op is the equivalent of a spin_lock/spin_unlock pair.

Well, may be this holds true if the mutex and the variable is located in the same cacheline.

Quoted 11 lines
Quoted 4 lines

This
is why two atomics are worse than taking a spin_lock, doing what you have to do, and releasing the spin_lock. Is this really what you want for a "confusing, let's make it robust" issue?

But it is on the ib_register_mad_agent() path which is not a performance path..

This actually looks like a genuine bug, why is it described only as 'confusing'? ib_register_mad_agent is callable from userspace, so at least two userspace agents can race and get the same TID’s.

My understanding is that every lookup is using the {port, TID} tuple. As such, it is not a bug, but, very confusing.

Quoted 1 line

TIDs need to be globally unique on the entire machine.

If you are correct Jason, let me reword the commit message.

Thxs, Håkon

#6 lore raw
Quoted 7 lines · On Mon, 23 Apr 2018 16:19:57 +0200 Håkon Bugge <[email protected]> wrote:
Quoted 4 lines

This actually looks like a genuine bug, why is it described only as 'confusing'? ib_register_mad_agent is callable from userspace, so at least two userspace agents can race and get the same TID’s.

My understanding is that every lookup is using the {port, TID} tuple. As such, it is not a bug, but, very confusing.

Haakon, you are correct (see snippet from the IB spec, below).

We will NOT have a situation where there are 2 threads/apps with the same agent ID on the *same port* (accessing the agent ID allocator is protected by a per-port spinlock). Having the same agent ID on DIFFERENT ports is OK.
Thus, there is NO bug here. (But as Haakon says, IMHO it is more robust to avoid having the same agent ID for 2 agents even if those agents are on different ports).

Quoted 3 lines
Quoted 2 lines

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

From the IB Spec 1.3, C13-18.1.1 (in section 13.4.6.4 - TransactionID usage):
"When initiating a new operation, MADHeader:TransactionID shall be set to such a value that within that MAD the combination of TID, SGID, and MgmtClass is different from that of any other currently executing operation. If the MAD does not have a GRH, its SLID is used in the combination in place of an SGID."

Since the SGID/SLID is different for each port, the per-port guarantee of no 2 agents receiving the same agent-ID value is sufficient.

-Jack

#7 lore raw
Quoted 38 lines · jackm
Quoted 7 lines · On Mon, 23 Apr 2018 16:19:57 +0200 Håkon Bugge <[email protected]> wrote:
Quoted 4 lines

This actually looks like a genuine bug, why is it described only as 'confusing'? ib_register_mad_agent is callable from userspace, so at least two userspace agents can race and get the same TID’s.

My understanding is that every lookup is using the {port, TID} tuple. As such, it is not a bug, but, very confusing.

Haakon, you are correct (see snippet from the IB spec, below).

We will NOT have a situation where there are 2 threads/apps with the same agent ID on the *same port* (accessing the agent ID allocator is protected by a per-port spinlock). Having the same agent ID on DIFFERENT ports is OK.
Thus, there is NO bug here. (But as Haakon says, IMHO it is more robust to avoid having the same agent ID for 2 agents even if those agents are on different ports).

Quoted 3 lines
Quoted 2 lines

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

From the IB Spec 1.3, C13-18.1.1 (in section 13.4.6.4 - TransactionID usage):
"When initiating a new operation, MADHeader:TransactionID shall be set to such a value that within that MAD the combination of TID, SGID, and MgmtClass is different from that of any other currently executing operation. If the MAD does not have a GRH, its SLID is used in the combination in place of an SGID."

Quoted 4 lines

Since the SGID/SLID is different for each port, the per-port guarantee of no 2 agents receiving the same agent-ID value is sufficient.

-Jack

Shall I interpret this silence as my commit is good to go or that I should add Jack’s tangible information to the commit message?

Thxs, Håkon

#8 lore raw
Quoted 37 lines · On Thu, 26 Apr 2018 18:06:10 +0200 Håkon Bugge <[email protected]> wrote:
Quoted 37 lines · jackm
Quoted 8 lines · On Mon, 23 Apr 2018 16:19:57 +0200 Håkon Bugge <[email protected]> wrote:
Quoted 5 lines

This actually looks like a genuine bug, why is it described only as 'confusing'? ib_register_mad_agent is callable from userspace, so at least two userspace agents can race and get the same TID’s.

My understanding is that every lookup is using the {port, TID} tuple. As such, it is not a bug, but, very confusing.

Haakon, you are correct (see snippet from the IB spec, below).

We will NOT have a situation where there are 2 threads/apps with the same agent ID on the *same port* (accessing the agent ID allocator is protected by a per-port spinlock). Having the same agent ID on DIFFERENT ports is OK.
Thus, there is NO bug here. (But as Haakon says, IMHO it is more robust to avoid having the same agent ID for 2 agents even if those agents are on different ports).

Quoted 3 lines
Quoted 2 lines

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

From the IB Spec 1.3, C13-18.1.1 (in section 13.4.6.4 - TransactionID usage):
"When initiating a new operation, MADHeader:TransactionID shall be set to such a value that within that MAD the combination of TID, SGID, and MgmtClass is different from that of any other currently executing operation. If the MAD does not have a GRH, its SLID is used in the combination in place of an SGID."

Quoted 10 lines
Quoted 5 lines

Since the SGID/SLID is different for each port, the per-port guarantee of no 2 agents receiving the same agent-ID value is sufficient.

-Jack

Shall I interpret this silence as my commit is good to go or that I should add Jack’s tangible information to the commit message?

Haakon, I think Jason is on vacation this week. Best to wait for his response.

-Jack

Quoted 2 lines

Thxs, Håkon

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]

Reviewed-by Jack Morgenstein Reviewed-by Ira Weiny Reviewed-by Zhu Yanjun
#11 lore raw
Quoted 4 lines · Håkon Bugge
Quoted 2 lines

Jason is out this week. I'll end up processing this one (probably later today). But I’ll fix up the commit message to suit my tastes when I do.

Thank you, Doug and Jack,

I reworded the commit message, let me know if you think I worded it wrong:

commit 69f01b81539c62f3dd96f9f02138ad7b839a0c70 (HEAD -> k.o/wip/dl-for-rc)

Author: Håkon Bugge <[email protected]>
Date:   Wed Apr 18 16:24:50 2018 +0200

IB/core: Make ib_mad_client_id atomic

Currently, kernel protects access to the agent ID allocator on a per port basis using a spinlock, so it is impossible for two apps/threads on the same port to get the same TID, but it is entirely possible for two threads on different ports to end up with the same TID. As this can be confusing (regardless of it being legal according to the IB Spec 1.3, C13-18.1.1, in section 13.4.6.4 - TransactionID usage: "Then initiating a new operation, MADHeader:TransactionID shall be set to such a value that within that MAD the combination of TIG, SGID, and MgmtClass is different from that of any other currently executing operation. If the MAD does not have a GRH, its SLID is used in the combination in place of an SGID." which guarantees we are legal because our different ports will have different SGID/SLID creating a unique tuple even if the TIDs are identical), and as we might want to open the TID allocator up to more parallel usage later, make the TID an atomic type so that no two allocations, regardless of port number, will be the same.

Signature · 3 lines
Doug Ledford <[email protected]>
    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD
#12 lore raw
Quoted 19 lines · Doug Ledford
Quoted 4 lines · Håkon Bugge
Quoted 2 lines

Jason is out this week. I'll end up processing this one (probably later today). But I’ll fix up the commit message to suit my tastes when I do.

Thank you, Doug and Jack,

I reworded the commit message, let me know if you think I worded it wrong:

commit 69f01b81539c62f3dd96f9f02138ad7b839a0c70 (HEAD -> k.o/wip/dl-for-rc)

Author: Håkon Bugge <[email protected]>
Date:   Wed Apr 18 16:24:50 2018 +0200

IB/core: Make ib_mad_client_id atomic

Quoted 15 lines

Currently, kernel protects access to the agent ID allocator on a per port basis using a spinlock, so it is impossible for two apps/threads on the same port to get the same TID, but it is entirely possible for two threads on different ports to end up with the same TID. As this can be confusing (regardless of it being legal according to the IB Spec 1.3, C13-18.1.1, in section 13.4.6.4 - TransactionID usage: "Then initiating a new operation, MADHeader:TransactionID shall be set to such a value that within that MAD the combination of TIG, SGID, and MgmtClass is different from that of any other currently executing operation. If the MAD does not have a GRH, its SLID is used in the combination in place of an SGID." which guarantees we are legal because our different ports will have different SGID/SLID creating a unique tuple even if the TIDs are identical), and as we might want to open the TID allocator up to more parallel usage later, make the TID an atomic type so that no two allocations, regardless of port number, will be the same.

The wording is OK per se, but the last sentence spans 12 lines - and as such - a little bit hard to comprehend. I suggest just to refer to IB Spec 1.3, clause C13-18.1.1 and not quote it and make the last sub-sentence (starting with “and as we might want to open”) a sentence by its own.

Thxs, Håkon

Quoted 11 lines
Signature · 3 lines
Doug Ledford <[email protected]>
   GPG KeyID: B826A3330E572FDD
   Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD
#13 lore raw
In reply to jackm
Quoted 3 lines · jackm
Quoted 1 line
Quoted 1 line

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

The expecation for /dev/umad users is that they all receive locally unique TID prefixes. The kernel may be OK to keep things port-specific but it is slightly breaking the API we are presenting to userspace to allow them to alias..

Jason

Reviewed-by Jack Morgenstein Reviewed-by Ira Weiny Reviewed-by Zhu Yanjun
#14 lore raw
Quoted 11 lines · Jason Gunthorpe
Quoted 2 lines · jackm
Quoted 1 line
Quoted 1 line

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

The expecation for /dev/umad users is that they all receive locally unique TID prefixes. The kernel may be OK to keep things port-specific but it is slightly breaking the API we are presenting to userspace to allow them to alias..

Jason

Would people be happier with this commit message then:

IB/core: Make ib_mad_client_id atomic

Currently, the kernel protects access to the agent ID allocator on a per port basis using a spinlock, so it is impossible for two apps/threads on the same port to get the same TID, but it is entirely possible for two threads on different ports to end up with the same TID.

As this can be confusing (regardless of it being legal according to the IB Spec 1.3, C13-18.1.1, in section 13.4.6.4 - TransactionID usage), and as the rdma-core user space API for /dev/umad devices implies unique TIDs even across ports, make the TID an atomic type so that no two allocations, regardless of port number, will be the same.

Signature · 3 lines
Doug Ledford <[email protected]>
    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD
#15 lore raw

Looks fine to me.

Quoted 50 lines

-----Original Message-----
From: Doug Ledford [mailto:[email protected]]
Sent: Monday, April 30, 2018 10:11 AM
To: Jason Gunthorpe <[email protected]>; jackm <[email protected]>
Cc: Håkon Bugge <[email protected]>; Hiatt, Don <[email protected]>; Dasaratharaman Chandramouli <[email protected]>; Weiny, Ira <[email protected]>; Hefty, Sean <[email protected]>; OFED mailing list <linux- [email protected]>; [email protected]
Subject: Re: [PATCH] IB/core: Make ib_mad_client_id atomic

Quoted 11 lines · Jason Gunthorpe
Quoted 2 lines · jackm
Quoted 1 line
Quoted 1 line

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

The expecation for /dev/umad users is that they all receive locally unique TID prefixes. The kernel may be OK to keep things port-specific but it is slightly breaking the API we are presenting to userspace to allow them to alias..

Jason

Would people be happier with this commit message then:

IB/core: Make ib_mad_client_id atomic

Currently, the kernel protects access to the agent ID allocator on a per port basis using a spinlock, so it is impossible for two apps/threads on the same port to get the same TID, but it is entirely possible for two threads on different ports to end up with the same TID.

As this can be confusing (regardless of it being legal according to the IB Spec 1.3, C13-18.1.1, in section 13.4.6.4 - TransactionID usage), and as the rdma-core user space API for /dev/umad devices implies unique TIDs even across ports, make the TID an atomic type so that no two allocations, regardless of port number, will be the same.

--
Doug Ledford <[email protected]>

    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD
#16 lore raw
In reply to Doug Ledford
Quoted 28 lines · Doug Ledford
Quoted 11 lines · Jason Gunthorpe
Quoted 2 lines · jackm
Quoted 1 line
Quoted 1 line

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

The expecation for /dev/umad users is that they all receive locally unique TID prefixes. The kernel may be OK to keep things port-specific but it is slightly breaking the API we are presenting to userspace to allow them to alias..

Jason

Would people be happier with this commit message then:

IB/core: Make ib_mad_client_id atomic

Currently, the kernel protects access to the agent ID allocator on a per port basis using a spinlock, so it is impossible for two apps/threads on the same port to get the same TID, but it is entirely possible for two threads on different ports to end up with the same TID.

As this can be confusing (regardless of it being legal according to the IB Spec 1.3, C13-18.1.1, in section 13.4.6.4 - TransactionID usage), and as the rdma-core user space API for /dev/umad devices implies unique TIDs even across ports, make the TID an atomic type so that no two allocations, regardless of port number, will be the same.

Quoted 5 lines

fine for me

Jason

#17 lore raw
In reply to Doug Ledford

On Mon, 30 Apr 2018 13:10:49 -0400
Doug Ledford <[email protected]> wrote:

Looks good!

-Jack

Quoted 36 lines · Jason Gunthorpe
Quoted 11 lines · jackm
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

The expecation for /dev/umad users is that they all receive locally unique TID prefixes. The kernel may be OK to keep things port-specific but it is slightly breaking the API we are presenting to userspace to allow them to alias..

Jason

Would people be happier with this commit message then:

IB/core: Make ib_mad_client_id atomic

Currently, the kernel protects access to the agent ID allocator on a per port basis using a spinlock, so it is impossible for two apps/threads on the same port to get the same TID, but it is entirely possible for two threads on different ports to end up with the same TID.

As this can be confusing (regardless of it being legal according to the IB Spec 1.3, C13-18.1.1, in section 13.4.6.4 - TransactionID usage), and as the rdma-core user space API for /dev/umad devices implies unique TIDs even across ports, make the TID an atomic type so that no two allocations, regardless of port number, will be the same.

#18 lore raw
Quoted 8 lines · jackm

On Mon, 30 Apr 2018 13:10:49 -0400
Doug Ledford <[email protected]> wrote:

Looks good!

Yes, absolutely!

Håkon

Quoted 44 lines

-Jack

Quoted 29 lines · Jason Gunthorpe
Quoted 11 lines · jackm
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines

TIDs need to be globally unique on the entire machine.

Jason, that is not exactly correct.

The expecation for /dev/umad users is that they all receive locally unique TID prefixes. The kernel may be OK to keep things port-specific but it is slightly breaking the API we are presenting to userspace to allow them to alias..

Jason

Would people be happier with this commit message then:

IB/core: Make ib_mad_client_id atomic

Currently, the kernel protects access to the agent ID allocator on a per port basis using a spinlock, so it is impossible for two apps/threads on the same port to get the same TID, but it is entirely possible for two threads on different ports to end up with the same TID.

As this can be confusing (regardless of it being legal according to the IB Spec 1.3, C13-18.1.1, in section 13.4.6.4 - TransactionID usage), and as the rdma-core user space API for /dev/umad devices implies unique TIDs even across ports, make the TID an atomic type so that no two allocations, regardless of port number, will be the same.

Quoted 7 lines

--
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