All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Aring <alex.aring@gmail.com>
To: linux-wpan@vger.kernel.org
Cc: kernel@pengutronix.de, Alexander Aring <alex.aring@gmail.com>
Subject: [PATCH bluetooth-next 01/18] ieee802154: rework wpan_phy index assignment
Date: Wed,  5 Nov 2014 20:51:12 +0100	[thread overview]
Message-ID: <1415217089-24919-2-git-send-email-alex.aring@gmail.com> (raw)
In-Reply-To: <1415217089-24919-1-git-send-email-alex.aring@gmail.com>

This patch reworks the wpan_phy index incrementation. It's now similar
like wireless wiphy index incrementation. We move the wpan_phy index
attribute inside of cfg802154_registered_device and use atomic
operations instead locking mechanism via wpan_phy_mutex.

Signed-off-by: Alexander Aring <alex.aring@gmail.com>
---
 include/net/cfg802154.h |  1 -
 net/ieee802154/core.c   | 30 +++++++++++-------------------
 net/ieee802154/core.h   |  3 +++
 3 files changed, 14 insertions(+), 20 deletions(-)

diff --git a/include/net/cfg802154.h b/include/net/cfg802154.h
index 864bce2..29c6de5 100644
--- a/include/net/cfg802154.h
+++ b/include/net/cfg802154.h
@@ -61,7 +61,6 @@ struct wpan_phy {
 	s32 cca_ed_level;
 
 	struct device dev;
-	int idx;
 
 	char priv[0] __aligned(NETDEV_ALIGN);
 };
diff --git a/net/ieee802154/core.c b/net/ieee802154/core.c
index ed5b014..d1cd0ed 100644
--- a/net/ieee802154/core.c
+++ b/net/ieee802154/core.c
@@ -23,9 +23,6 @@
 #include "sysfs.h"
 #include "core.h"
 
-static DEFINE_MUTEX(wpan_phy_mutex);
-static int wpan_phy_idx;
-
 static int wpan_phy_match(struct device *dev, const void *data)
 {
 	return !strcmp(dev_name(dev), (const char *)data);
@@ -72,14 +69,10 @@ int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
 }
 EXPORT_SYMBOL(wpan_phy_for_each);
 
-static int wpan_phy_idx_valid(int idx)
-{
-	return idx >= 0;
-}
-
 struct wpan_phy *
 wpan_phy_alloc(const struct cfg802154_ops *ops, size_t priv_size)
 {
+	static atomic_t wpan_phy_counter = ATOMIC_INIT(0);
 	struct cfg802154_registered_device *rdev;
 	size_t alloc_size;
 
@@ -90,28 +83,27 @@ wpan_phy_alloc(const struct cfg802154_ops *ops, size_t priv_size)
 
 	rdev->ops = ops;
 
-	mutex_lock(&wpan_phy_mutex);
-	rdev->wpan_phy.idx = wpan_phy_idx++;
-	if (unlikely(!wpan_phy_idx_valid(rdev->wpan_phy.idx))) {
-		wpan_phy_idx--;
-		mutex_unlock(&wpan_phy_mutex);
+	rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter);
+
+	if (unlikely(rdev->wpan_phy_idx < 0)) {
+		/* ugh, wrapped! */
+		atomic_dec(&wpan_phy_counter);
 		kfree(rdev);
-		goto out;
+		return NULL;
 	}
-	mutex_unlock(&wpan_phy_mutex);
+
+	/* atomic_inc_return makes it start at 1, make it start at 0 */
+	rdev->wpan_phy_idx--;
 
 	mutex_init(&rdev->wpan_phy.pib_lock);
 
 	device_initialize(&rdev->wpan_phy.dev);
-	dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy.idx);
+	dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy_idx);
 
 	rdev->wpan_phy.dev.class = &wpan_phy_class;
 	rdev->wpan_phy.dev.platform_data = rdev;
 
 	return &rdev->wpan_phy;
-
-out:
-	return NULL;
 }
 EXPORT_SYMBOL(wpan_phy_alloc);
 
diff --git a/net/ieee802154/core.h b/net/ieee802154/core.h
index 1bc1725..fea60b3 100644
--- a/net/ieee802154/core.h
+++ b/net/ieee802154/core.h
@@ -6,6 +6,9 @@
 struct cfg802154_registered_device {
 	const struct cfg802154_ops *ops;
 
+	/* wpan_phy index, internal only */
+	int wpan_phy_idx;
+
 	/* must be last because of the way we do wpan_phy_priv(),
 	 * and it should at least be aligned to NETDEV_ALIGN
 	 */
-- 
2.1.3


  reply	other threads:[~2014-11-05 19:52 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-05 19:51 [PATCH bluetooth-next 00/18] ieee802154: interface registration and perm address support Alexander Aring
2014-11-05 19:51 ` Alexander Aring [this message]
2014-11-05 19:51 ` [PATCH bluetooth-next 02/18] ieee802154: remove nl802154 unused functions Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 03/18] mac802154: move interface del handling in iface Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 04/18] mac802154: move interface add " Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 05/18] mac802154: move dev_hold out of ieee802154_if_add Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 06/18] ieee802154: rework interface registration Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 07/18] ieee802154: remove mlme get_phy callback Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 08/18] mac802154: add default interface registration Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 09/18] mac802154: add ieee802154_vif struct Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 10/18] ieee802154: add IEEE802154_EXTENDED_ADDR_LEN Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 11/18] ieee802154: add ieee802154_random_extended_addr Alexander Aring
2014-11-06  1:54   ` Varka Bhadram
2014-11-06 10:03     ` Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 12/18] mac802154: add ieee802154_le64_to_be64 Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 13/18] mac802154: cleanup ieee802154_netdev_to_extended_addr Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 14/18] mac802154: add support for perm_extended_addr Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 15/18] at86rf230: generate random perm extended address Alexander Aring
2014-11-06  5:52   ` Varka Bhadram
2014-11-06 10:05     ` Alexander Aring
2014-12-12 14:56       ` Alexander Aring
     [not found]         ` <CAEUmHya_1iG9Rs-Wq+bq8MT2rwjgG_zUvr8wB-zTLnSCyj-StA@mail.gmail.com>
2014-12-13  9:08           ` Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 16/18] at86rf230: add force slotted operation bit Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 17/18] mac802154: use IEEE802154_EXTENDED_ADDR_LEN Alexander Aring
2014-11-05 19:51 ` [PATCH bluetooth-next 18/18] mac802154: fix typo promisuous to promiscuous Alexander Aring
2014-11-05 20:54 ` [PATCH bluetooth-next 00/18] ieee802154: interface registration and perm address support Marcel Holtmann

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=1415217089-24919-2-git-send-email-alex.aring@gmail.com \
    --to=alex.aring@gmail.com \
    --cc=kernel@pengutronix.de \
    --cc=linux-wpan@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.