DPDK patches and discussions
 help / color / mirror / Atom feed
From: Yipeng Wang <yipeng1.wang@intel.com>
To: bruce.richardson@intel.com
Cc: dev@dpdk.org, yipeng1.wang@intel.com, honnappa.nagarahalli@arm.com
Subject: [dpdk-dev] [PATCH v8 1/4] hash: fix race condition in iterate
Date: Mon, 22 Oct 2018 11:39:45 -0700	[thread overview]
Message-ID: <1540233588-202969-2-git-send-email-yipeng1.wang@intel.com> (raw)
In-Reply-To: <1540233588-202969-1-git-send-email-yipeng1.wang@intel.com>

In rte_hash_iterate, the reader lock did not protect the
while loop which checks empty entry. This created a race
condition that the entry may become empty when enters
the lock, then a wrong key data value would be read out.

This commit reads out the position in the while condition,
which makes sure that the position will not be changed
to empty before entering the lock.

Fixes: f2e3001b53ec ("hash: support read/write concurrency")
Cc: stable@dpdk.org

Signed-off-by: Yipeng Wang <yipeng1.wang@intel.com>
Reported-by: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>
Reviewed-by: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>
Acked-by: Dharmik Thakkar <dharmik.thakkar@arm.com>
---
 lib/librte_hash/rte_cuckoo_hash.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/lib/librte_hash/rte_cuckoo_hash.c b/lib/librte_hash/rte_cuckoo_hash.c
index f7b86c8..da8ddf4 100644
--- a/lib/librte_hash/rte_cuckoo_hash.c
+++ b/lib/librte_hash/rte_cuckoo_hash.c
@@ -1318,7 +1318,7 @@ rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32
 	idx = *next % RTE_HASH_BUCKET_ENTRIES;
 
 	/* If current position is empty, go to the next one */
-	while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
+	while ((position = h->buckets[bucket_idx].key_idx[idx]) == EMPTY_SLOT) {
 		(*next)++;
 		/* End of table */
 		if (*next == total_entries)
@@ -1326,9 +1326,8 @@ rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32
 		bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
 		idx = *next % RTE_HASH_BUCKET_ENTRIES;
 	}
+
 	__hash_rw_reader_lock(h);
-	/* Get position of entry in key table */
-	position = h->buckets[bucket_idx].key_idx[idx];
 	next_key = (struct rte_hash_key *) ((char *)h->key_store +
 				position * h->key_entry_size);
 	/* Return key and data */
-- 
2.7.4

  reply	other threads:[~2018-10-23  1:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-22 18:39 [dpdk-dev] [PATCH v8 0/4] hash: add extendable bucket and partial key hashing Yipeng Wang
2018-10-22 18:39 ` Yipeng Wang [this message]
2018-10-23  8:53   ` [dpdk-dev] [PATCH v8 1/4] hash: fix race condition in iterate Bruce Richardson
2018-10-22 18:39 ` [dpdk-dev] [PATCH v8 2/4] hash: add extendable bucket feature Yipeng Wang
2018-10-23  8:55   ` Bruce Richardson
2018-10-22 18:39 ` [dpdk-dev] [PATCH v8 3/4] test/hash: implement extendable bucket hash test Yipeng Wang
2018-10-23  9:04   ` Bruce Richardson
2018-10-22 18:39 ` [dpdk-dev] [PATCH v8 4/4] hash: use partial-key hashing Yipeng Wang
2018-10-23  9:07   ` Bruce Richardson
2018-10-25 22:35 ` [dpdk-dev] [PATCH v8 0/4] hash: add extendable bucket and partial key hashing Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1540233588-202969-2-git-send-email-yipeng1.wang@intel.com \
    --to=yipeng1.wang@intel.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=honnappa.nagarahalli@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).