Skip to contentSkip to conversation

power: introduce library for device-specific OPPs

PATCH Superseded Nishanth Menon 3 participants 13 messages Oct 1, 2010 – Oct 5, 2010

Subsystems (4)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 power: introduce library for device-specific OPPs

SoCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of OPP varies over silicon versions. For a specific domain, we can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a default set of these are activated based on the precise nature of device. Further on operation, based on conditions prevailing in the system (such as temperature), some OPP availability may be temporarily controlled by the SoC frameworks.

To implement an OPP, some sort of power management support is necessary hence this library depends on CONFIG_PM.

Contributions include:
Sanjeev Premi for the initial concept:

	http://patchwork.kernel.org/patch/50998/

Kevin Hilman for converting original design to device-based Kevin Hilman and Paul Walmsey for cleaning up many of the function abstractions, improvements and data structure handling
Romit Dasgupta for using enums instead of opp pointers
Thara Gopinath, Eduardo Valentin and Vishwanath BS for fixes and cleanups.
Linus Walleij for recommending this layer be made generic for usage in other architectures beyond OMAP and ARM.
Mark Brown, Andrew Morton, Rafael J Wysocki, Paul E McKenney for valuable improvements.

Discussions and comments from:
http://marc.info/?l=linux-omap&m=126033945313269&w=2
http://marc.info/?l=linux-omap&m=125482970102327&w=2
http://marc.info/?t=125809247500002&r=1&w=2
http://marc.info/?l=linux-omap&m=126025973426007&w=2
http://marc.info/?t=128152609200064&r=1&w=2
http://marc.info/?t=128468723000002&r=1&w=2
incorporated.

Cc Benoit Cousson <[email protected]>
Cc Madhusudhan Chikkature Rajashekar <[email protected]>
Cc Phil Carmody <[email protected]>
Cc Roberto Granados Dorado <[email protected]>
Cc Santosh Shilimkar <[email protected]>
Cc Sergio Alberto Aguirre Rodriguez <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Eduardo Valentin <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Sanjeev Premi <[email protected]>
Cc Thara Gopinath <[email protected]>
Cc Vishwanath BS <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Mark Brown <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Paul E. McKenney <[email protected]>

---
V5:
opp pointers were being passed to callers outside rcu locks, this
causes the pointers to be potentially invalid between calls if opp_enable/disable calls take place.
The solution for this cannot be reference counters either, as opp pointer without control of opp library and rcu locks are completely untrustworthy. The better approach is to enforce restrictions on callers of query and retrieval functions to wrap the relevant parts under rcu locks. This allows for tremendous flexibility in the way opp libraries can be used as the users can optimize their implementation based on the requirements.
NOTE: find_device_opp is implicitly wrapped as a result.
(ref, v4 comments)
One more side-effect has been that the opp_enable and disable
functions changed their prototypes to make it a little more easier for the users to use them without having to get the pointers before hand, as well as do the functionality that is being provided currently as well.
opp_init_cpufreq_table now returns int and result of operation for
callers to adequately handle their error conditions.
pr_fmt is being used instead of duplicating "%s" .. __func__ with
every pr_xxx call.
Documentation update to reflect these.

Sample usage:
http://pastebin.mozilla.org/802375: cleanups for omap pm layer
http://pastebin.mozilla.org/802374: omap opp initialization

V4:
http://marc.info/?t=128533285000002&r=1&w=2
Cc list trimmed to just the MLs.
RCU implementation - thanks for the pointers Rafael re-tested on OMAP3630 with lock debugging enabled including cpufreq, idle paths and "exception cases - enable/disable". available_opp_count removed as it complicated the updates altogether
+ the usage latencies are not too high given small lists.
In removed list_reverse usage as rcu has no list reverse equivalents
+ searching either direction has no real difference when the list is ordered.
change in protos for get_{voltage,frequency} - const removed from parameter as rcu_dereference causes build warnings introduced opp_set_availability helper to wrap common code between
opp_{enable,disable} - leaving these as is as they are more readable from usage perspective + these are not meant to be used in hot paths anyways.
Header guard __ASM_OPP_H replaced with __LINUX_OPP_H__ for opp.h

V3:
https://patchwork.kernel.org/patch/200382/
Bunch of documentation update based on feedback removed opp_def - opp_add enables the opp by default lock usage optimization

Sample usage:
http://pastebin.mozilla.org/794786 -> cleanups for OMAP power
http://pastebin.mozilla.org/794787 -> Sample OPP initialization for OMAP

V2:
https://patchwork.kernel.org/patch/189532/
Incorporated review comments from v1. major changes being:
$subject change to reflect this is for power. Documentation revamp and including it in the patch :) OPP_DEF removed - lets introduce this if needed or leave it to SOC frameworks to organize code as needed.
Rename of enable to available to better reflect the intent few fixes and typos
Introduced mutex based locking for controlling access to list modification (note: query functions are still unsafe- rationale
in the patch below)
A new home for opp.c in drivers/base/power (moved from lib/) A few optimization in function flow and additional error checks added to exposed functions
offline aligned with Kevin for cleaning up the copyrights

V1:
http://marc.info/?t=128468723000002&r=1&w=2

 Documentation/power/00-INDEX |    2 +
 Documentation/power/opp.txt  |  372 +++++++++++++++++++++++++
 drivers/base/power/Makefile  |    1 +
 drivers/base/power/opp.c     |  630 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/opp.h          |  105 +++++++
 kernel/power/Kconfig         |   14 +
 6 files changed, 1124 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/power/opp.txt
 create mode 100644 drivers/base/power/opp.c
 create mode 100644 include/linux/opp.h
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
 	- Power management user interface in /sys/power
 notifiers.txt
 	- Registering suspend notifiers in device drivers
+opp.txt
+	- Operating Performance Point library
 pci.txt
 	- How the PCI Subsystem Does Power Management
 pm_qos_interface.txt
Documentation/power/opp.txt new file +372 -0

372 lines not on this page · raw message

drivers/base/power/Makefile +1 -0
index cbccf9a..abe46ed 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_PM_SLEEP)	+= main.o wakeup.o
 obj-$(CONFIG_PM_RUNTIME)	+= runtime.o
 obj-$(CONFIG_PM_OPS)	+= generic_ops.o
 obj-$(CONFIG_PM_TRACE_RTC)	+= trace.o
+obj-$(CONFIG_PM_OPP)	+= opp.o
 
 ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG
 ccflags-$(CONFIG_PM_VERBOSE)   += -DDEBUG
drivers/base/power/opp.c new file +630 -0

630 lines not on this page · raw message

include/linux/opp.h new file +105 -0

105 lines not on this page · raw message

kernel/power/Kconfig +14 -0
index ca6066a..634eab6 100644
@@ -242,3 +242,17 @@ config PM_OPS
 	bool
 	depends on PM_SLEEP || PM_RUNTIME
 	default y
+
+config PM_OPP
+	bool "Enable Operating Performance Point(OPP) Layer library"
+	depends on PM
+	---help---
+	  SOCs have a standard set of tuples consisting of frequency and
+	  voltage pairs that the device will support per voltage domain. This
+	  is called Operating Performance Point or OPP. The actual definitions
+	  of OPP varies over silicon within the same family of devices.
+
+	  OPP layer organizes the data internally using device pointers
+	  representing individual voltage domains and provides SOC
+	  implementations a ready to use framework to manage OPPs.
+	  For more information, read <file:Documentation/power/opp.txt>
Signature · 1 line
1.6.3.3
#1 lore raw
Quoted 10 lines · Nishanth Menon

SoCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of OPP varies over silicon versions. For a specific domain, we can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a default set of these are activated based on the precise nature of device. Further on operation, based on conditions prevailing in the system (such as temperature), some OPP availability may be temporarily controlled by the SoC frameworks.

Quoted 2 lines

To implement an OPP, some sort of power management support is necessary hence this library depends on CONFIG_PM.

Well, I still have some comments.

...

Quoted 7 lines

+/**
+ * opp_add() - Add an OPP table from a table definitions
+ * @dev: device for which we do this operation
+ * @freq: Frequency in Hz for this OPP
+ * @u_volt: Voltage in uVolts for this OPP
+ *
+ * This function adds an opp definition to the opp list and returns status.

Quoted 8 lines

+ * The opp is made available by default and it can be controlled using
+ * opp_enable/disable functions.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex cannot be locked.

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

Quoted 6 lines

+ */

drivers/base/power/opp.c new file +5 -0
+int opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
+{
+	struct device_opp *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;

drivers/base/power/opp.c line 387

Quoted 10 lines
drivers/base/power/opp.c new file +10 -0
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		pr_warning("Unable to allocate new opp node\n");
+		return -ENOMEM;
+	}
+
+	/* Check for existing list for 'dev' */
+	rcu_read_lock();

If you acquire dev_opp_list_lock here, you won't need the rcu_read_lock(), because every other updater will block on dev_opp_list_lock until you're done.

drivers/base/power/opp.c line 390

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	rcu_read_unlock();
+	if (!dev_opp) {

Now you can drop dev_opp_list_lock temporarily, because the allocation doesn't need synchronization.

drivers/base/power/opp.c line 402

Quoted 12 lines
drivers/base/power/opp.c new file +12 -0
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			kfree(new_opp);
+			pr_warning("Unable to allocate device structure\n");
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+		mutex_init(&dev_opp->lock);
+

Reacquire dev_opp_list_lock at this point and hold it to the end of the routine.

drivers/base/power/opp.c line 404

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+		/* Secure the device list modification */
+		mutex_lock(&dev_opp_list_lock);

This won't be necessary any more.

drivers/base/power/opp.c line 405

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		list_add_rcu(&dev_opp->node, &dev_opp_list);

Of course, this is still needed.

drivers/base/power/opp.c line 406

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		mutex_unlock(&dev_opp_list_lock);

Not necessary.

drivers/base/power/opp.c line 416

Quoted 10 lines
drivers/base/power/opp.c new file +10 -0
+	}
+
+	/* populate the opp table */
+	new_opp->dev_opp = dev_opp;
+	new_opp->rate = freq;
+	new_opp->u_volt = u_volt;
+	new_opp->available = true;
+
+	/* make the dev_opp modification safe */
+	mutex_lock(&dev_opp->lock);

That's not necessary.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	rcu_read_lock();

Ditto.

drivers/base/power/opp.c line 427

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate < opp->rate)
+			break;
+		else
+			head = &opp->node;
+	}
+	rcu_read_unlock();

Ditto.

drivers/base/power/opp.c line 430

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+	list_add_rcu(&new_opp->node, head);
+	mutex_unlock(&dev_opp->lock);

Now release dev_opp_list_lock instead.

And remember to call synchronize_rcu() when you're done.

Quoted 27 lines
drivers/base/power/opp.c new file +27 -0
+	return 0;
+}
+
+/**
+ * opp_set_availability() - helper to set the availability of an opp
+ * @dev:		device for which we do this operation
+ * @freq:		OPP frequency to modify availability
+ * @availability_req:	availability status requested for this opp
+ *
+ * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
+ * share a common logic which is isolated here.
+ *
+ * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
+ * copy operation, returns 0 if no modifcation was done OR modification was
+ * successful.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex locking or synchronize_rcu() blocking calls cannot be used.
+ */
+static int opp_set_availability(struct device *dev, unsigned long freq,
+		bool availability_req)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);

drivers/base/power/opp.c line 468

Quoted 10 lines
drivers/base/power/opp.c new file +10 -0
+	int r = 0;
+
+	/* keep the node allocated */
+	new_opp = kmalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		pr_warning("Unable to allocate opp\n");
+		return -ENOMEM;
+	}
+
+	rcu_read_lock();

Acquire dev_opp_list_lock instead.

drivers/base/power/opp.c line 471

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+
+	/* Find the device_opp */
+	list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {

You can use a normal list_for_each_entry here, because it's under the lock.

drivers/base/power/opp.c line 477

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+	dev_opp = find_device_opp(dev);

Hmm. I wonder why this is necessary?

drivers/base/power/opp.c line 485

Quoted 8 lines
drivers/base/power/opp.c new file +8 -0
+	if (IS_ERR(dev_opp)) {
+		r = PTR_ERR(dev_opp);
+		pr_warning("Unable to find device\n");
+		goto err;
+	}
+
+	/* Do we have the frequency? */
+	list_for_each_entry_rcu(tmp_opp, &dev_opp->opp_list, node) {

Use list_for_each_entry here too.

drivers/base/power/opp.c line 496

Quoted 11 lines
drivers/base/power/opp.c new file +11 -0
+		if (tmp_opp->rate == freq) {
+			opp = tmp_opp;
+			break;
+		}
+	}
+	if (IS_ERR(opp)) {
+		r = PTR_ERR(opp);
+		goto err;
+	}
+
+	mutex_lock(&opp->dev_opp->lock);

And that won't be necessary any more.

Review

Quoted 1 line
(quoted diff) +1 -0
+	tmp_opp = rcu_dereference(opp);

Ditto (we're an updater, not a reader).

drivers/base/power/opp.c line 503

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+	/* Is update really needed? */
+	if (tmp_opp->available == availability_req)
+		goto out1;
+	/* copy the old data over */
+	*new_opp = *tmp_opp;
+	rcu_read_unlock();

Not necessary.

drivers/base/power/opp.c line 508

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+	/* plug in new node */
+	new_opp->available = availability_req;
+	list_replace_rcu(&opp->node, &new_opp->node);
+	mutex_unlock(&opp->dev_opp->lock);

Now unlock dev_opp_list_lock instead.

drivers/base/power/opp.c line 510

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+	synchronize_rcu();
+

And rework the exit code below accordingly.

Quoted 13 lines
drivers/base/power/opp.c new file +13 -0
+	/* clean up old opp */
+	new_opp = opp;
+	goto out;
+
+out1:
+	mutex_unlock(&opp->dev_opp->lock);
+err:
+	rcu_read_unlock();
+out:
+	kfree(new_opp);
+	return r;
+}
+

...

drivers/base/power/opp.c line 598

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+int opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	rcu_read_lock();

I would pretend I'm an updater here and acquire dev_opp_list_lock instead.

drivers/base/power/opp.c line 503

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		rcu_read_unlock();

So that won't be necessary.

drivers/base/power/opp.c line 605

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		pr_warning("Unable to find device\n");
+		return PTR_ERR(dev_opp);
+	}
+

Now, you can sleep with the mutex held, so GFP_KERNEL may be used below.

drivers/base/power/opp.c line 609

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (opp_get_opp_count(dev) + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		rcu_read_unlock();

Drop dev_opp_list_lock instead.

drivers/base/power/opp.c line 614

Quoted 5 lines
drivers/base/power/opp.c new file +5 -0
+		pr_warning("Failed to allocate frequency table\n");
+		return -ENOMEM;
+	}
+
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {

That may be list_for_each_entry() now.

drivers/base/power/opp.c line 621

Quoted 7 lines
drivers/base/power/opp.c new file +7 -0
+		if (opp->available) {
+			freq_table[i].index = i;
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+	rcu_read_unlock();

Drop dev_opp_list_lock instead.

drivers/base/power/opp.c line 629

Quoted 8 lines
drivers/base/power/opp.c new file +8 -0
+
+	freq_table[i].index = i;
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+
+	return 0;
+}

I think I didn't confuse anything, but surely Paul will fix me if I did. :-)

Thanks,
Rafael

#2 lore raw
Quoted 1 line · Rafael J. Wysocki

On Friday, October 01, 2010, Nishanth Menon wrote:

...

Quoted 12 lines

drivers/base/power/opp.c line 598

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+int opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	rcu_read_lock();

I would pretend I'm an updater here and acquire dev_opp_list_lock instead.

Quoted 5 lines

drivers/base/power/opp.c line 503

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		rcu_read_unlock();

So that won't be necessary.

Or rather the lock will have to be dropped here instead.

Quoted 4 lines

drivers/base/power/opp.c line 605

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		pr_warning("Unable to find device\n");
+		return PTR_ERR(dev_opp);
+	}
+

Thanks,
Rafael

#3 lore raw
In reply to Rafael J. Wysocki
Quoted 1 line · Rafael J. Wysocki

On Friday, October 01, 2010, Nishanth Menon wrote:

...

Quoted 12 lines

drivers/base/power/opp.c line 598

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+int opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	rcu_read_lock();

I would pretend I'm an updater here and acquire dev_opp_list_lock instead.

Quoted 5 lines

drivers/base/power/opp.c line 503

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		rcu_read_unlock();

So that won't be necessary.

Or rather the lock will have to be dropped here instead.

Quoted 4 lines

drivers/base/power/opp.c line 605

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		pr_warning("Unable to find device\n");
+		return PTR_ERR(dev_opp);
+	}
+

Thanks,
Rafael

#4 lore raw
In reply to Rafael J. Wysocki

Rafael J. Wysocki had written, on 10/04/2010 05:36 PM, the following:

Quoted 11 lines · Nishanth Menon
Quoted 10 lines

SoCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of OPP varies over silicon versions. For a specific domain, we can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a default set of these are activated based on the precise nature of device. Further on operation, based on conditions prevailing in the system (such as temperature), some OPP availability may be temporarily controlled by the SoC frameworks.

Quoted 4 lines
Quoted 2 lines

To implement an OPP, some sort of power management support is necessary hence this library depends on CONFIG_PM.

Well, I still have some comments.

Thanks for the same. Few views below.

Quoted 18 lines

...

Quoted 7 lines

+/**
+ * opp_add() - Add an OPP table from a table definitions
+ * @dev: device for which we do this operation
+ * @freq: Frequency in Hz for this OPP
+ * @u_volt: Voltage in uVolts for this OPP
+ *
+ * This function adds an opp definition to the opp list and returns status.

Quoted 8 lines

+ * The opp is made available by default and it can be controlled using
+ * opp_enable/disable functions.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex cannot be locked.

Quoted 3 lines

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

I did consider using dev_opp_list_lock to lock everything *but* here is the contention:

dev_opp_list_lock locks modification for addition of domains device. This operation happens usually during init stage.

each domain device has multiple opps, new opps can be added, but the more often usage will probably be opp_enable and disable. domain are usually modifiable independent of each other - device_opp->lock provides device level lock allowing for each domain device opp list to be modified independent of each other. e.g. on thermal overage we may choose to lower mpu domain while a coprocessor driver in parallel might choose to disable co-processor domain in parallel.

Wondering why you'd like a single lock for all domains and restrict parallelization?

Quoted 12 lines
Quoted 12 lines

+ */

drivers/base/power/opp.c new file +10 -0
+int opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
+{
+	struct device_opp *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		pr_warning("Unable to allocate new opp node\n");
Quoted 8 lines

drivers/base/power/opp.c line 387

Quoted 5 lines
drivers/base/power/opp.c new file +5 -0
+		return -ENOMEM;
+	}
+
+	/* Check for existing list for 'dev' */
+	rcu_read_lock();

If you acquire dev_opp_list_lock here, you won't need the rcu_read_lock(), because every other updater will block on dev_opp_list_lock until you're done.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 7 lines

drivers/base/power/opp.c line 390

Quoted 4 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	rcu_read_unlock();
+	if (!dev_opp) {

Now you can drop dev_opp_list_lock temporarily, because the allocation doesn't need synchronization.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 15 lines

drivers/base/power/opp.c line 402

Quoted 13 lines
drivers/base/power/opp.c new file +12 -0
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			kfree(new_opp);
+			pr_warning("Unable to allocate device structure\n");
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+		mutex_init(&dev_opp->lock);
+

Reacquire dev_opp_list_lock at this point and hold it to the end of the routine.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 6 lines

drivers/base/power/opp.c line 404

Quoted 3 lines
drivers/base/power/opp.c new file +2 -0
+		/* Secure the device list modification */
+		mutex_lock(&dev_opp_list_lock);

This won't be necessary any more.

Quoted 7 lines

drivers/base/power/opp.c line 405

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		list_add_rcu(&dev_opp->node, &dev_opp_list);

Of course, this is still needed.

drivers/base/power/opp.c line 406

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		mutex_unlock(&dev_opp_list_lock);

Not necessary.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 13 lines

drivers/base/power/opp.c line 416

Quoted 11 lines
drivers/base/power/opp.c new file +10 -0
+	}
+
+	/* populate the opp table */
+	new_opp->dev_opp = dev_opp;
+	new_opp->rate = freq;
+	new_opp->u_volt = u_volt;
+	new_opp->available = true;
+
+	/* make the dev_opp modification safe */
+	mutex_lock(&dev_opp->lock);

That's not necessary.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+	rcu_read_lock();

Ditto.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 12 lines

drivers/base/power/opp.c line 427

Quoted 10 lines
drivers/base/power/opp.c new file +9 -0
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate < opp->rate)
+			break;
+		else
+			head = &opp->node;
+	}
+	rcu_read_unlock();

Ditto.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 5 lines

drivers/base/power/opp.c line 430

Quoted 3 lines
drivers/base/power/opp.c new file +2 -0
+	list_add_rcu(&new_opp->node, head);
+	mutex_unlock(&dev_opp->lock);

Now release dev_opp_list_lock instead.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 2 lines

And remember to call synchronize_rcu() when you're done.

we dont need synchronize_rcu() for list_add_rcu as Paul pointed out in [1]. quote:
"

drivers/base/power/opp.c

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		mutex_lock(&dev_opp_list_lock);
+		list_add_rcu(&dev_opp->node, &dev_opp_list);
+		mutex_unlock(&dev_opp_list_lock);
+		synchronize_rcu();

You do not need to wait for an RCU grace period when adding objects, only between removing them and freeing them.
"

in our usage, this points to list_replace_rcu

Quoted 32 lines
Quoted 32 lines
drivers/base/power/opp.c new file +31 -0
+	return 0;
+}
+
+/**
+ * opp_set_availability() - helper to set the availability of an opp
+ * @dev:		device for which we do this operation
+ * @freq:		OPP frequency to modify availability
+ * @availability_req:	availability status requested for this opp
+ *
+ * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
+ * share a common logic which is isolated here.
+ *
+ * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
+ * copy operation, returns 0 if no modifcation was done OR modification was
+ * successful.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex locking or synchronize_rcu() blocking calls cannot be used.
+ */
+static int opp_set_availability(struct device *dev, unsigned long freq,
+		bool availability_req)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
+	int r = 0;
+
+	/* keep the node allocated */
+	new_opp = kmalloc(sizeof(struct opp), GFP_KERNEL);
Quoted 8 lines

drivers/base/power/opp.c line 468

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+	if (!new_opp) {
+		pr_warning("Unable to allocate opp\n");
+		return -ENOMEM;
+	}
+
+	rcu_read_lock();

Acquire dev_opp_list_lock instead.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 6 lines

drivers/base/power/opp.c line 471

Quoted 4 lines
drivers/base/power/opp.c new file +3 -0
+
+	/* Find the device_opp */
+	list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {

You can use a normal list_for_each_entry here, because it's under the lock.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 9 lines

drivers/base/power/opp.c line 477

Quoted 7 lines
drivers/base/power/opp.c new file +6 -0
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+	dev_opp = find_device_opp(dev);

Hmm. I wonder why this is necessary?

arrgh.. yep, you are right.. will kick this out..

Quoted 11 lines

drivers/base/power/opp.c line 485

Quoted 9 lines
drivers/base/power/opp.c new file +8 -0
+	if (IS_ERR(dev_opp)) {
+		r = PTR_ERR(dev_opp);
+		pr_warning("Unable to find device\n");
+		goto err;
+	}
+
+	/* Do we have the frequency? */
+	list_for_each_entry_rcu(tmp_opp, &dev_opp->opp_list, node) {

Use list_for_each_entry here too.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 14 lines

drivers/base/power/opp.c line 496

Quoted 12 lines
drivers/base/power/opp.c new file +11 -0
+		if (tmp_opp->rate == freq) {
+			opp = tmp_opp;
+			break;
+		}
+	}
+	if (IS_ERR(opp)) {
+		r = PTR_ERR(opp);
+		goto err;
+	}
+
+	mutex_lock(&opp->dev_opp->lock);

And that won't be necessary any more.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	tmp_opp = rcu_dereference(opp);

Ditto (we're an updater, not a reader).

At this point are'nt we still a reader?

Quoted 9 lines

drivers/base/power/opp.c line 503

Quoted 7 lines
drivers/base/power/opp.c new file +6 -0
+	/* Is update really needed? */
+	if (tmp_opp->available == availability_req)
+		goto out1;
+	/* copy the old data over */
+	*new_opp = *tmp_opp;
+	rcu_read_unlock();

Not necessary.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 7 lines

drivers/base/power/opp.c line 508

Quoted 5 lines
drivers/base/power/opp.c new file +4 -0
+	/* plug in new node */
+	new_opp->available = availability_req;
+	list_replace_rcu(&opp->node, &new_opp->node);
+	mutex_unlock(&opp->dev_opp->lock);

Now unlock dev_opp_list_lock instead.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 20 lines

drivers/base/power/opp.c line 510

Quoted 3 lines
drivers/base/power/opp.c new file +2 -0
+	synchronize_rcu();
+

And rework the exit code below accordingly.

Quoted 13 lines
drivers/base/power/opp.c new file +13 -0
+	/* clean up old opp */
+	new_opp = opp;
+	goto out;
+
+out1:
+	mutex_unlock(&opp->dev_opp->lock);
+err:
+	rcu_read_unlock();
+out:
+	kfree(new_opp);
+	return r;
+}
+

...

Quoted 11 lines

drivers/base/power/opp.c line 598

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+int opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	rcu_read_lock();

I would pretend I'm an updater here and acquire dev_opp_list_lock instead.

why?

Quoted 8 lines

drivers/base/power/opp.c line 503

Quoted 4 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		rcu_read_unlock();

So that won't be necessary.

Quoted 6 lines

drivers/base/power/opp.c line 605

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		pr_warning("Unable to find device\n");
+		return PTR_ERR(dev_opp);
+	}
+

Now, you can sleep with the mutex held, so GFP_KERNEL may be used below.

ack. will fix in v6.

Quoted 26 lines

drivers/base/power/opp.c line 609

Quoted 5 lines
drivers/base/power/opp.c new file +4 -0
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (opp_get_opp_count(dev) + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		rcu_read_unlock();

Drop dev_opp_list_lock instead.

drivers/base/power/opp.c line 614

Quoted 5 lines
drivers/base/power/opp.c new file +5 -0
+		pr_warning("Failed to allocate frequency table\n");
+		return -ENOMEM;
+	}
+
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {

That may be list_for_each_entry() now.

drivers/base/power/opp.c line 621

Quoted 7 lines
drivers/base/power/opp.c new file +7 -0
+		if (opp->available) {
+			freq_table[i].index = i;
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+	rcu_read_unlock();

Drop dev_opp_list_lock instead.

Quoted 10 lines

drivers/base/power/opp.c line 629

Quoted 8 lines
drivers/base/power/opp.c new file +8 -0
+
+	freq_table[i].index = i;
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+
+	return 0;
+}

I think I didn't confuse anything, but surely Paul will fix me if I did. :-)

My contention is the preference for a single lock for everything instead of domain level locks for majority of the usage. i dont see why we should loose parallelization capability.

[1] http://marc.info/?l=linux-omap&m=128535708720191&w=2

Signature · 2 lines
Regards,
Nishanth Menon
#5 lore raw
Quoted 54 lines · Nishanth Menon

Rafael J. Wysocki had written, on 10/04/2010 05:36 PM, the following:

Quoted 11 lines · Nishanth Menon
Quoted 10 lines

SoCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of OPP varies over silicon versions. For a specific domain, we can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a default set of these are activated based on the precise nature of device. Further on operation, based on conditions prevailing in the system (such as temperature), some OPP availability may be temporarily controlled by the SoC frameworks.

Quoted 4 lines
Quoted 2 lines

To implement an OPP, some sort of power management support is necessary hence this library depends on CONFIG_PM.

Well, I still have some comments.

Thanks for the same. Few views below.

Quoted 18 lines

...

Quoted 7 lines

+/**
+ * opp_add() - Add an OPP table from a table definitions
+ * @dev: device for which we do this operation
+ * @freq: Frequency in Hz for this OPP
+ * @u_volt: Voltage in uVolts for this OPP
+ *
+ * This function adds an opp definition to the opp list and returns status.

Quoted 8 lines

+ * The opp is made available by default and it can be controlled using
+ * opp_enable/disable functions.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex cannot be locked.

Quoted 3 lines

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

I did consider using dev_opp_list_lock to lock everything *but* here is the contention:

dev_opp_list_lock locks modification for addition of domains device. This operation happens usually during init stage.

each domain device has multiple opps, new opps can be added, but the more often usage will probably be opp_enable and disable. domain are usually modifiable independent of each other - device_opp->lock provides device level lock allowing for each domain device opp list to be modified independent of each other. e.g. on thermal overage we may choose to lower mpu domain while a coprocessor driver in parallel might choose to disable co-processor domain in parallel.

Quoted 2 lines

Wondering why you'd like a single lock for all domains and restrict parallelization?

Because of the simplicity, mostly. If there's only a relatively short period when the lock will be contended for, that still is not too bad and it's much easier to get the synchronization right with just one lock for starters.

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines

+ */

drivers/base/power/opp.c new file +10 -0
+int opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
+{
+	struct device_opp *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		pr_warning("Unable to allocate new opp node\n");
Quoted 8 lines

drivers/base/power/opp.c line 387

Quoted 5 lines
drivers/base/power/opp.c new file +5 -0
+		return -ENOMEM;
+	}
+
+	/* Check for existing list for 'dev' */
+	rcu_read_lock();

If you acquire dev_opp_list_lock here, you won't need the rcu_read_lock(), because every other updater will block on dev_opp_list_lock until you're done.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

I don't think it's a big issue at this point.

Quoted 7 lines
Quoted 6 lines

drivers/base/power/opp.c line 390

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	rcu_read_unlock();
+	if (!dev_opp) {

Now you can drop dev_opp_list_lock temporarily, because the allocation doesn't need synchronization.

[contention on using a single lock dev_opp_list_lock as discussed above]

Well, see above.

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			kfree(new_opp);
+			pr_warning("Unable to allocate device structure\n");
+			return -ENOMEM;
Quoted 8 lines

drivers/base/power/opp.c line 402

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+		}
+
+		dev_opp->dev = dev;
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+		mutex_init(&dev_opp->lock);
+

Reacquire dev_opp_list_lock at this point and hold it to the end of the routine.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 5 lines
Quoted 5 lines

drivers/base/power/opp.c line 404

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+		/* Secure the device list modification */
+		mutex_lock(&dev_opp_list_lock);

This won't be necessary any more.

Quoted 8 lines
Quoted 7 lines

drivers/base/power/opp.c line 405

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		list_add_rcu(&dev_opp->node, &dev_opp_list);

Of course, this is still needed.

drivers/base/power/opp.c line 406

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		mutex_unlock(&dev_opp_list_lock);

Not necessary.

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+	}
+
+	/* populate the opp table */
+	new_opp->dev_opp = dev_opp;
Quoted 8 lines

drivers/base/power/opp.c line 416

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+	new_opp->rate = freq;
+	new_opp->u_volt = u_volt;
+	new_opp->available = true;
+
+	/* make the dev_opp modification safe */
+	mutex_lock(&dev_opp->lock);

That's not necessary.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 5 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	rcu_read_lock();

Ditto.

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 11 lines
Quoted 11 lines

drivers/base/power/opp.c line 427

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate < opp->rate)
+			break;
+		else
+			head = &opp->node;
+	}
+	rcu_read_unlock();

Ditto.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 5 lines
Quoted 4 lines

drivers/base/power/opp.c line 430

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+	list_add_rcu(&new_opp->node, head);
+	mutex_unlock(&dev_opp->lock);

Now release dev_opp_list_lock instead.

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 5 lines
Quoted 1 line

And remember to call synchronize_rcu() when you're done.

we dont need synchronize_rcu() for list_add_rcu as Paul pointed out in [1]. quote:
"

Quoted 8 lines

drivers/base/power/opp.c

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		mutex_lock(&dev_opp_list_lock);
+		list_add_rcu(&dev_opp->node, &dev_opp_list);
+		mutex_unlock(&dev_opp_list_lock);
+		synchronize_rcu();

You do not need to wait for an RCU grace period when adding objects, only between removing them and freeing them.
"

OK

Quoted 43 lines

in our usage, this points to list_replace_rcu

Quoted 32 lines
Quoted 32 lines
drivers/base/power/opp.c new file +31 -0
+	return 0;
+}
+
+/**
+ * opp_set_availability() - helper to set the availability of an opp
+ * @dev:		device for which we do this operation
+ * @freq:		OPP frequency to modify availability
+ * @availability_req:	availability status requested for this opp
+ *
+ * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
+ * share a common logic which is isolated here.
+ *
+ * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
+ * copy operation, returns 0 if no modifcation was done OR modification was
+ * successful.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex locking or synchronize_rcu() blocking calls cannot be used.
+ */
+static int opp_set_availability(struct device *dev, unsigned long freq,
+		bool availability_req)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
+	int r = 0;
+
+	/* keep the node allocated */
+	new_opp = kmalloc(sizeof(struct opp), GFP_KERNEL);
Quoted 8 lines

drivers/base/power/opp.c line 468

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+	if (!new_opp) {
+		pr_warning("Unable to allocate opp\n");
+		return -ENOMEM;
+	}
+
+	rcu_read_lock();

Acquire dev_opp_list_lock instead.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

Well, I'm tired of this.

To summarize, I don't think the synchronization will work correctly in the version of the patch you've submitted and I suggested you how it can be fixed.

As I said, I don't think the lock contention is a big issue at this point.

Thanks,
Rafael

#6 lore raw
Quoted 3 lines · Rafael J. Wysocki
Quoted 2 lines · Nishanth Menon

Rafael J. Wysocki had written, on 10/04/2010 05:36 PM, the following:

Quoted 1 line · Nishanth Menon

[...]

Quoted 19 lines
Quoted 19 lines
Quoted 4 lines

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

I did consider using dev_opp_list_lock to lock everything *but* here is the contention:

dev_opp_list_lock locks modification for addition of domains device. This operation happens usually during init stage.

each domain device has multiple opps, new opps can be added, but the more often usage will probably be opp_enable and disable. domain are usually modifiable independent of each other - device_opp->lock provides device level lock allowing for each domain device opp list to be modified independent of each other. e.g. on thermal overage we may choose to lower mpu domain while a coprocessor driver in parallel might choose to disable co-processor domain in parallel.

Quoted 6 lines
Quoted 2 lines

Wondering why you'd like a single lock for all domains and restrict parallelization?

Because of the simplicity, mostly. If there's only a relatively short period when the lock will be contended for, that still is not too bad and it's much easier to get the synchronization right with just one lock for starters.

FWIW, I agree with Rafael

These are not going be highly contended locks, and the lock durations are very short, so simplifying the locking is a big win for readability.

Kevin

#7 lore raw

Kevin Hilman had written, on 10/05/2010 03:50 PM, the following:

Quoted 31 lines
Quoted 3 lines · Rafael J. Wysocki
Quoted 2 lines · Nishanth Menon

Rafael J. Wysocki had written, on 10/04/2010 05:36 PM, the following:

Quoted 1 line · Nishanth Menon

[...]

Quoted 17 lines
Quoted 17 lines
Quoted 3 lines

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

I did consider using dev_opp_list_lock to lock everything *but* here is the contention:

dev_opp_list_lock locks modification for addition of domains device. This operation happens usually during init stage.

each domain device has multiple opps, new opps can be added, but the more often usage will probably be opp_enable and disable. domain are usually modifiable independent of each other - device_opp->lock provides device level lock allowing for each domain device opp list to be modified independent of each other. e.g. on thermal overage we may choose to lower mpu domain while a coprocessor driver in parallel might choose to disable co-processor domain in parallel.

Quoted 5 lines
Quoted 2 lines

Wondering why you'd like a single lock for all domains and restrict parallelization?

Because of the simplicity, mostly. If there's only a relatively short period when the lock will be contended for, that still is not too bad and it's much easier to get the synchronization right with just one lock for starters.

Quoted 6 lines

FWIW, I agree with Rafael

These are not going be highly contended locks, and the lock durations are very short, so simplifying the locking is a big win for readability.

Kevin

Fair enough. we can relook if the lock becomes a contended lock in the future. I do agree that simplifying the locking will benefit readability. Will post a v6 with a singular lock and updated documentation for the same.

Signature · 2 lines
Regards,
Nishanth Menon
#8 lore raw
In reply to Kevin Hilman

Kevin Hilman had written, on 10/05/2010 03:50 PM, the following:

Quoted 31 lines
Quoted 3 lines · Rafael J. Wysocki
Quoted 2 lines · Nishanth Menon

Rafael J. Wysocki had written, on 10/04/2010 05:36 PM, the following:

Quoted 1 line · Nishanth Menon

[...]

Quoted 17 lines
Quoted 17 lines
Quoted 3 lines

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

I did consider using dev_opp_list_lock to lock everything *but* here is the contention:

dev_opp_list_lock locks modification for addition of domains device. This operation happens usually during init stage.

each domain device has multiple opps, new opps can be added, but the more often usage will probably be opp_enable and disable. domain are usually modifiable independent of each other - device_opp->lock provides device level lock allowing for each domain device opp list to be modified independent of each other. e.g. on thermal overage we may choose to lower mpu domain while a coprocessor driver in parallel might choose to disable co-processor domain in parallel.

Quoted 5 lines
Quoted 2 lines

Wondering why you'd like a single lock for all domains and restrict parallelization?

Because of the simplicity, mostly. If there's only a relatively short period when the lock will be contended for, that still is not too bad and it's much easier to get the synchronization right with just one lock for starters.

Quoted 6 lines

FWIW, I agree with Rafael

These are not going be highly contended locks, and the lock durations are very short, so simplifying the locking is a big win for readability.

Kevin

Fair enough. we can relook if the lock becomes a contended lock in the future. I do agree that simplifying the locking will benefit readability. Will post a v6 with a singular lock and updated documentation for the same.

Signature · 2 lines
Regards,
Nishanth Menon
#9 lore raw
In reply to Rafael J. Wysocki
Quoted 3 lines · Rafael J. Wysocki
Quoted 2 lines · Nishanth Menon

Rafael J. Wysocki had written, on 10/04/2010 05:36 PM, the following:

Quoted 1 line · Nishanth Menon

[...]

Quoted 19 lines
Quoted 19 lines
Quoted 4 lines

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

I did consider using dev_opp_list_lock to lock everything *but* here is the contention:

dev_opp_list_lock locks modification for addition of domains device. This operation happens usually during init stage.

each domain device has multiple opps, new opps can be added, but the more often usage will probably be opp_enable and disable. domain are usually modifiable independent of each other - device_opp->lock provides device level lock allowing for each domain device opp list to be modified independent of each other. e.g. on thermal overage we may choose to lower mpu domain while a coprocessor driver in parallel might choose to disable co-processor domain in parallel.

Quoted 6 lines
Quoted 2 lines

Wondering why you'd like a single lock for all domains and restrict parallelization?

Because of the simplicity, mostly. If there's only a relatively short period when the lock will be contended for, that still is not too bad and it's much easier to get the synchronization right with just one lock for starters.

FWIW, I agree with Rafael

These are not going be highly contended locks, and the lock durations are very short, so simplifying the locking is a big win for readability.

Kevin

#10 lore raw
In reply to Nishanth Menon
Quoted 54 lines · Nishanth Menon

Rafael J. Wysocki had written, on 10/04/2010 05:36 PM, the following:

Quoted 11 lines · Nishanth Menon
Quoted 10 lines

SoCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of OPP varies over silicon versions. For a specific domain, we can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a default set of these are activated based on the precise nature of device. Further on operation, based on conditions prevailing in the system (such as temperature), some OPP availability may be temporarily controlled by the SoC frameworks.

Quoted 4 lines
Quoted 2 lines

To implement an OPP, some sort of power management support is necessary hence this library depends on CONFIG_PM.

Well, I still have some comments.

Thanks for the same. Few views below.

Quoted 18 lines

...

Quoted 7 lines

+/**
+ * opp_add() - Add an OPP table from a table definitions
+ * @dev: device for which we do this operation
+ * @freq: Frequency in Hz for this OPP
+ * @u_volt: Voltage in uVolts for this OPP
+ *
+ * This function adds an opp definition to the opp list and returns status.

Quoted 8 lines

+ * The opp is made available by default and it can be controlled using
+ * opp_enable/disable functions.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex cannot be locked.

Quoted 3 lines

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

I did consider using dev_opp_list_lock to lock everything *but* here is the contention:

dev_opp_list_lock locks modification for addition of domains device. This operation happens usually during init stage.

each domain device has multiple opps, new opps can be added, but the more often usage will probably be opp_enable and disable. domain are usually modifiable independent of each other - device_opp->lock provides device level lock allowing for each domain device opp list to be modified independent of each other. e.g. on thermal overage we may choose to lower mpu domain while a coprocessor driver in parallel might choose to disable co-processor domain in parallel.

Quoted 2 lines

Wondering why you'd like a single lock for all domains and restrict parallelization?

Because of the simplicity, mostly. If there's only a relatively short period when the lock will be contended for, that still is not too bad and it's much easier to get the synchronization right with just one lock for starters.

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines

+ */

drivers/base/power/opp.c new file +10 -0
+int opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
+{
+	struct device_opp *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		pr_warning("Unable to allocate new opp node\n");
Quoted 8 lines

drivers/base/power/opp.c line 387

Quoted 5 lines
drivers/base/power/opp.c new file +5 -0
+		return -ENOMEM;
+	}
+
+	/* Check for existing list for 'dev' */
+	rcu_read_lock();

If you acquire dev_opp_list_lock here, you won't need the rcu_read_lock(), because every other updater will block on dev_opp_list_lock until you're done.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

I don't think it's a big issue at this point.

Quoted 7 lines
Quoted 6 lines

drivers/base/power/opp.c line 390

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	rcu_read_unlock();
+	if (!dev_opp) {

Now you can drop dev_opp_list_lock temporarily, because the allocation doesn't need synchronization.

[contention on using a single lock dev_opp_list_lock as discussed above]

Well, see above.

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			kfree(new_opp);
+			pr_warning("Unable to allocate device structure\n");
+			return -ENOMEM;
Quoted 8 lines

drivers/base/power/opp.c line 402

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+		}
+
+		dev_opp->dev = dev;
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+		mutex_init(&dev_opp->lock);
+

Reacquire dev_opp_list_lock at this point and hold it to the end of the routine.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 5 lines
Quoted 5 lines

drivers/base/power/opp.c line 404

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+		/* Secure the device list modification */
+		mutex_lock(&dev_opp_list_lock);

This won't be necessary any more.

Quoted 8 lines
Quoted 7 lines

drivers/base/power/opp.c line 405

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		list_add_rcu(&dev_opp->node, &dev_opp_list);

Of course, this is still needed.

drivers/base/power/opp.c line 406

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		mutex_unlock(&dev_opp_list_lock);

Not necessary.

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+	}
+
+	/* populate the opp table */
+	new_opp->dev_opp = dev_opp;
Quoted 8 lines

drivers/base/power/opp.c line 416

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+	new_opp->rate = freq;
+	new_opp->u_volt = u_volt;
+	new_opp->available = true;
+
+	/* make the dev_opp modification safe */
+	mutex_lock(&dev_opp->lock);

That's not necessary.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 5 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	rcu_read_lock();

Ditto.

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 11 lines
Quoted 11 lines

drivers/base/power/opp.c line 427

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate < opp->rate)
+			break;
+		else
+			head = &opp->node;
+	}
+	rcu_read_unlock();

Ditto.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 5 lines
Quoted 4 lines

drivers/base/power/opp.c line 430

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+	list_add_rcu(&new_opp->node, head);
+	mutex_unlock(&dev_opp->lock);

Now release dev_opp_list_lock instead.

[contention on using a single lock dev_opp_list_lock as discussed above]

See above.

Quoted 5 lines
Quoted 1 line

And remember to call synchronize_rcu() when you're done.

we dont need synchronize_rcu() for list_add_rcu as Paul pointed out in [1]. quote:
"

Quoted 8 lines

drivers/base/power/opp.c

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		mutex_lock(&dev_opp_list_lock);
+		list_add_rcu(&dev_opp->node, &dev_opp_list);
+		mutex_unlock(&dev_opp_list_lock);
+		synchronize_rcu();

You do not need to wait for an RCU grace period when adding objects, only between removing them and freeing them.
"

OK

Quoted 43 lines

in our usage, this points to list_replace_rcu

Quoted 32 lines
Quoted 32 lines
drivers/base/power/opp.c new file +31 -0
+	return 0;
+}
+
+/**
+ * opp_set_availability() - helper to set the availability of an opp
+ * @dev:		device for which we do this operation
+ * @freq:		OPP frequency to modify availability
+ * @availability_req:	availability status requested for this opp
+ *
+ * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
+ * share a common logic which is isolated here.
+ *
+ * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
+ * copy operation, returns 0 if no modifcation was done OR modification was
+ * successful.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex locking or synchronize_rcu() blocking calls cannot be used.
+ */
+static int opp_set_availability(struct device *dev, unsigned long freq,
+		bool availability_req)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
+	int r = 0;
+
+	/* keep the node allocated */
+	new_opp = kmalloc(sizeof(struct opp), GFP_KERNEL);
Quoted 8 lines

drivers/base/power/opp.c line 468

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+	if (!new_opp) {
+		pr_warning("Unable to allocate opp\n");
+		return -ENOMEM;
+	}
+
+	rcu_read_lock();

Acquire dev_opp_list_lock instead.

Quoted 1 line

[contention on using a single lock dev_opp_list_lock as discussed above]

Well, I'm tired of this.

To summarize, I don't think the synchronization will work correctly in the version of the patch you've submitted and I suggested you how it can be fixed.

As I said, I don't think the lock contention is a big issue at this point.

Thanks,
Rafael

#11 lore raw
In reply to Rafael J. Wysocki

Rafael J. Wysocki had written, on 10/04/2010 05:36 PM, the following:

Quoted 11 lines · Nishanth Menon
Quoted 10 lines

SoCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of OPP varies over silicon versions. For a specific domain, we can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a default set of these are activated based on the precise nature of device. Further on operation, based on conditions prevailing in the system (such as temperature), some OPP availability may be temporarily controlled by the SoC frameworks.

Quoted 4 lines
Quoted 2 lines

To implement an OPP, some sort of power management support is necessary hence this library depends on CONFIG_PM.

Well, I still have some comments.

Thanks for the same. Few views below.

Quoted 18 lines

...

Quoted 7 lines

+/**
+ * opp_add() - Add an OPP table from a table definitions
+ * @dev: device for which we do this operation
+ * @freq: Frequency in Hz for this OPP
+ * @u_volt: Voltage in uVolts for this OPP
+ *
+ * This function adds an opp definition to the opp list and returns status.

Quoted 8 lines

+ * The opp is made available by default and it can be controlled using
+ * opp_enable/disable functions.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex cannot be locked.

Quoted 3 lines

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

I did consider using dev_opp_list_lock to lock everything *but* here is the contention:

dev_opp_list_lock locks modification for addition of domains device. This operation happens usually during init stage.

each domain device has multiple opps, new opps can be added, but the more often usage will probably be opp_enable and disable. domain are usually modifiable independent of each other - device_opp->lock provides device level lock allowing for each domain device opp list to be modified independent of each other. e.g. on thermal overage we may choose to lower mpu domain while a coprocessor driver in parallel might choose to disable co-processor domain in parallel.

Wondering why you'd like a single lock for all domains and restrict parallelization?

Quoted 12 lines
Quoted 12 lines

+ */

drivers/base/power/opp.c new file +10 -0
+int opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
+{
+	struct device_opp *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		pr_warning("Unable to allocate new opp node\n");
Quoted 8 lines

drivers/base/power/opp.c line 387

Quoted 5 lines
drivers/base/power/opp.c new file +5 -0
+		return -ENOMEM;
+	}
+
+	/* Check for existing list for 'dev' */
+	rcu_read_lock();

If you acquire dev_opp_list_lock here, you won't need the rcu_read_lock(), because every other updater will block on dev_opp_list_lock until you're done.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 7 lines

drivers/base/power/opp.c line 390

Quoted 4 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	rcu_read_unlock();
+	if (!dev_opp) {

Now you can drop dev_opp_list_lock temporarily, because the allocation doesn't need synchronization.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 15 lines

drivers/base/power/opp.c line 402

Quoted 13 lines
drivers/base/power/opp.c new file +12 -0
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			kfree(new_opp);
+			pr_warning("Unable to allocate device structure\n");
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+		mutex_init(&dev_opp->lock);
+

Reacquire dev_opp_list_lock at this point and hold it to the end of the routine.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 6 lines

drivers/base/power/opp.c line 404

Quoted 3 lines
drivers/base/power/opp.c new file +2 -0
+		/* Secure the device list modification */
+		mutex_lock(&dev_opp_list_lock);

This won't be necessary any more.

Quoted 7 lines

drivers/base/power/opp.c line 405

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		list_add_rcu(&dev_opp->node, &dev_opp_list);

Of course, this is still needed.

drivers/base/power/opp.c line 406

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		mutex_unlock(&dev_opp_list_lock);

Not necessary.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 13 lines

drivers/base/power/opp.c line 416

Quoted 11 lines
drivers/base/power/opp.c new file +10 -0
+	}
+
+	/* populate the opp table */
+	new_opp->dev_opp = dev_opp;
+	new_opp->rate = freq;
+	new_opp->u_volt = u_volt;
+	new_opp->available = true;
+
+	/* make the dev_opp modification safe */
+	mutex_lock(&dev_opp->lock);

That's not necessary.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+	rcu_read_lock();

Ditto.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 12 lines

drivers/base/power/opp.c line 427

Quoted 10 lines
drivers/base/power/opp.c new file +9 -0
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate < opp->rate)
+			break;
+		else
+			head = &opp->node;
+	}
+	rcu_read_unlock();

Ditto.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 5 lines

drivers/base/power/opp.c line 430

Quoted 3 lines
drivers/base/power/opp.c new file +2 -0
+	list_add_rcu(&new_opp->node, head);
+	mutex_unlock(&dev_opp->lock);

Now release dev_opp_list_lock instead.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 2 lines

And remember to call synchronize_rcu() when you're done.

we dont need synchronize_rcu() for list_add_rcu as Paul pointed out in [1]. quote:
"

drivers/base/power/opp.c

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		mutex_lock(&dev_opp_list_lock);
+		list_add_rcu(&dev_opp->node, &dev_opp_list);
+		mutex_unlock(&dev_opp_list_lock);
+		synchronize_rcu();

You do not need to wait for an RCU grace period when adding objects, only between removing them and freeing them.
"

in our usage, this points to list_replace_rcu

Quoted 32 lines
Quoted 32 lines
drivers/base/power/opp.c new file +31 -0
+	return 0;
+}
+
+/**
+ * opp_set_availability() - helper to set the availability of an opp
+ * @dev:		device for which we do this operation
+ * @freq:		OPP frequency to modify availability
+ * @availability_req:	availability status requested for this opp
+ *
+ * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
+ * share a common logic which is isolated here.
+ *
+ * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
+ * copy operation, returns 0 if no modifcation was done OR modification was
+ * successful.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex locking or synchronize_rcu() blocking calls cannot be used.
+ */
+static int opp_set_availability(struct device *dev, unsigned long freq,
+		bool availability_req)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
+	int r = 0;
+
+	/* keep the node allocated */
+	new_opp = kmalloc(sizeof(struct opp), GFP_KERNEL);
Quoted 8 lines

drivers/base/power/opp.c line 468

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+	if (!new_opp) {
+		pr_warning("Unable to allocate opp\n");
+		return -ENOMEM;
+	}
+
+	rcu_read_lock();

Acquire dev_opp_list_lock instead.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 6 lines

drivers/base/power/opp.c line 471

Quoted 4 lines
drivers/base/power/opp.c new file +3 -0
+
+	/* Find the device_opp */
+	list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {

You can use a normal list_for_each_entry here, because it's under the lock.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 9 lines

drivers/base/power/opp.c line 477

Quoted 7 lines
drivers/base/power/opp.c new file +6 -0
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+	dev_opp = find_device_opp(dev);

Hmm. I wonder why this is necessary?

arrgh.. yep, you are right.. will kick this out..

Quoted 11 lines

drivers/base/power/opp.c line 485

Quoted 9 lines
drivers/base/power/opp.c new file +8 -0
+	if (IS_ERR(dev_opp)) {
+		r = PTR_ERR(dev_opp);
+		pr_warning("Unable to find device\n");
+		goto err;
+	}
+
+	/* Do we have the frequency? */
+	list_for_each_entry_rcu(tmp_opp, &dev_opp->opp_list, node) {

Use list_for_each_entry here too.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 14 lines

drivers/base/power/opp.c line 496

Quoted 12 lines
drivers/base/power/opp.c new file +11 -0
+		if (tmp_opp->rate == freq) {
+			opp = tmp_opp;
+			break;
+		}
+	}
+	if (IS_ERR(opp)) {
+		r = PTR_ERR(opp);
+		goto err;
+	}
+
+	mutex_lock(&opp->dev_opp->lock);

And that won't be necessary any more.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	tmp_opp = rcu_dereference(opp);

Ditto (we're an updater, not a reader).

At this point are'nt we still a reader?

Quoted 9 lines

drivers/base/power/opp.c line 503

Quoted 7 lines
drivers/base/power/opp.c new file +6 -0
+	/* Is update really needed? */
+	if (tmp_opp->available == availability_req)
+		goto out1;
+	/* copy the old data over */
+	*new_opp = *tmp_opp;
+	rcu_read_unlock();

Not necessary.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 7 lines

drivers/base/power/opp.c line 508

Quoted 5 lines
drivers/base/power/opp.c new file +4 -0
+	/* plug in new node */
+	new_opp->available = availability_req;
+	list_replace_rcu(&opp->node, &new_opp->node);
+	mutex_unlock(&opp->dev_opp->lock);

Now unlock dev_opp_list_lock instead.

[contention on using a single lock dev_opp_list_lock as discussed above]

Quoted 20 lines

drivers/base/power/opp.c line 510

Quoted 3 lines
drivers/base/power/opp.c new file +2 -0
+	synchronize_rcu();
+

And rework the exit code below accordingly.

Quoted 13 lines
drivers/base/power/opp.c new file +13 -0
+	/* clean up old opp */
+	new_opp = opp;
+	goto out;
+
+out1:
+	mutex_unlock(&opp->dev_opp->lock);
+err:
+	rcu_read_unlock();
+out:
+	kfree(new_opp);
+	return r;
+}
+

...

Quoted 11 lines

drivers/base/power/opp.c line 598

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+int opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	rcu_read_lock();

I would pretend I'm an updater here and acquire dev_opp_list_lock instead.

why?

Quoted 8 lines

drivers/base/power/opp.c line 503

Quoted 4 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		rcu_read_unlock();

So that won't be necessary.

Quoted 6 lines

drivers/base/power/opp.c line 605

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		pr_warning("Unable to find device\n");
+		return PTR_ERR(dev_opp);
+	}
+

Now, you can sleep with the mutex held, so GFP_KERNEL may be used below.

ack. will fix in v6.

Quoted 26 lines

drivers/base/power/opp.c line 609

Quoted 5 lines
drivers/base/power/opp.c new file +4 -0
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (opp_get_opp_count(dev) + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		rcu_read_unlock();

Drop dev_opp_list_lock instead.

drivers/base/power/opp.c line 614

Quoted 5 lines
drivers/base/power/opp.c new file +5 -0
+		pr_warning("Failed to allocate frequency table\n");
+		return -ENOMEM;
+	}
+
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {

That may be list_for_each_entry() now.

drivers/base/power/opp.c line 621

Quoted 7 lines
drivers/base/power/opp.c new file +7 -0
+		if (opp->available) {
+			freq_table[i].index = i;
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+	rcu_read_unlock();

Drop dev_opp_list_lock instead.

Quoted 10 lines

drivers/base/power/opp.c line 629

Quoted 8 lines
drivers/base/power/opp.c new file +8 -0
+
+	freq_table[i].index = i;
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+
+	return 0;
+}

I think I didn't confuse anything, but surely Paul will fix me if I did. :-)

My contention is the preference for a single lock for everything instead of domain level locks for majority of the usage. i dont see why we should loose parallelization capability.

[1] http://marc.info/?l=linux-omap&m=128535708720191&w=2

Signature · 2 lines
Regards,
Nishanth Menon
#12 lore raw
In reply to Nishanth Menon
Quoted 10 lines · Nishanth Menon

SoCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of OPP varies over silicon versions. For a specific domain, we can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a default set of these are activated based on the precise nature of device. Further on operation, based on conditions prevailing in the system (such as temperature), some OPP availability may be temporarily controlled by the SoC frameworks.

Quoted 2 lines

To implement an OPP, some sort of power management support is necessary hence this library depends on CONFIG_PM.

Well, I still have some comments.

...

Quoted 7 lines

+/**
+ * opp_add() - Add an OPP table from a table definitions
+ * @dev: device for which we do this operation
+ * @freq: Frequency in Hz for this OPP
+ * @u_volt: Voltage in uVolts for this OPP
+ *
+ * This function adds an opp definition to the opp list and returns status.

Quoted 8 lines

+ * The opp is made available by default and it can be controlled using
+ * opp_enable/disable functions.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex cannot be locked.

I'm not really sure why so many mutexes are needed here. I don't think you need a separate mutex in every struct device_opp object. I'd just use dev_opp_list_lock for everything.

Quoted 6 lines

+ */

drivers/base/power/opp.c new file +5 -0
+int opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
+{
+	struct device_opp *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;

drivers/base/power/opp.c line 387

Quoted 10 lines
drivers/base/power/opp.c new file +10 -0
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		pr_warning("Unable to allocate new opp node\n");
+		return -ENOMEM;
+	}
+
+	/* Check for existing list for 'dev' */
+	rcu_read_lock();

If you acquire dev_opp_list_lock here, you won't need the rcu_read_lock(), because every other updater will block on dev_opp_list_lock until you're done.

drivers/base/power/opp.c line 390

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	rcu_read_unlock();
+	if (!dev_opp) {

Now you can drop dev_opp_list_lock temporarily, because the allocation doesn't need synchronization.

drivers/base/power/opp.c line 402

Quoted 12 lines
drivers/base/power/opp.c new file +12 -0
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			kfree(new_opp);
+			pr_warning("Unable to allocate device structure\n");
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+		mutex_init(&dev_opp->lock);
+

Reacquire dev_opp_list_lock at this point and hold it to the end of the routine.

drivers/base/power/opp.c line 404

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+		/* Secure the device list modification */
+		mutex_lock(&dev_opp_list_lock);

This won't be necessary any more.

drivers/base/power/opp.c line 405

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		list_add_rcu(&dev_opp->node, &dev_opp_list);

Of course, this is still needed.

drivers/base/power/opp.c line 406

Quoted 1 line
drivers/base/power/opp.c new file +1 -0
+		mutex_unlock(&dev_opp_list_lock);

Not necessary.

drivers/base/power/opp.c line 416

Quoted 10 lines
drivers/base/power/opp.c new file +10 -0
+	}
+
+	/* populate the opp table */
+	new_opp->dev_opp = dev_opp;
+	new_opp->rate = freq;
+	new_opp->u_volt = u_volt;
+	new_opp->available = true;
+
+	/* make the dev_opp modification safe */
+	mutex_lock(&dev_opp->lock);

That's not necessary.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	rcu_read_lock();

Ditto.

drivers/base/power/opp.c line 427

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate < opp->rate)
+			break;
+		else
+			head = &opp->node;
+	}
+	rcu_read_unlock();

Ditto.

drivers/base/power/opp.c line 430

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+	list_add_rcu(&new_opp->node, head);
+	mutex_unlock(&dev_opp->lock);

Now release dev_opp_list_lock instead.

And remember to call synchronize_rcu() when you're done.

Quoted 27 lines
drivers/base/power/opp.c new file +27 -0
+	return 0;
+}
+
+/**
+ * opp_set_availability() - helper to set the availability of an opp
+ * @dev:		device for which we do this operation
+ * @freq:		OPP frequency to modify availability
+ * @availability_req:	availability status requested for this opp
+ *
+ * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
+ * share a common logic which is isolated here.
+ *
+ * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
+ * copy operation, returns 0 if no modifcation was done OR modification was
+ * successful.
+ *
+ * Locking: The internal device_opp and opp structures are RCU protected.
+ * Hence this function internally uses RCU and mutex locks to keep the
+ * integrity of the internal data structures. Callers should ensure that
+ * this function is *NOT* called under RCU protection or in contexts where
+ * mutex locking or synchronize_rcu() blocking calls cannot be used.
+ */
+static int opp_set_availability(struct device *dev, unsigned long freq,
+		bool availability_req)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);

drivers/base/power/opp.c line 468

Quoted 10 lines
drivers/base/power/opp.c new file +10 -0
+	int r = 0;
+
+	/* keep the node allocated */
+	new_opp = kmalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		pr_warning("Unable to allocate opp\n");
+		return -ENOMEM;
+	}
+
+	rcu_read_lock();

Acquire dev_opp_list_lock instead.

drivers/base/power/opp.c line 471

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+
+	/* Find the device_opp */
+	list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {

You can use a normal list_for_each_entry here, because it's under the lock.

drivers/base/power/opp.c line 477

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+	dev_opp = find_device_opp(dev);

Hmm. I wonder why this is necessary?

drivers/base/power/opp.c line 485

Quoted 8 lines
drivers/base/power/opp.c new file +8 -0
+	if (IS_ERR(dev_opp)) {
+		r = PTR_ERR(dev_opp);
+		pr_warning("Unable to find device\n");
+		goto err;
+	}
+
+	/* Do we have the frequency? */
+	list_for_each_entry_rcu(tmp_opp, &dev_opp->opp_list, node) {

Use list_for_each_entry here too.

drivers/base/power/opp.c line 496

Quoted 11 lines
drivers/base/power/opp.c new file +11 -0
+		if (tmp_opp->rate == freq) {
+			opp = tmp_opp;
+			break;
+		}
+	}
+	if (IS_ERR(opp)) {
+		r = PTR_ERR(opp);
+		goto err;
+	}
+
+	mutex_lock(&opp->dev_opp->lock);

And that won't be necessary any more.

Review

Quoted 1 line
(quoted diff) +1 -0
+	tmp_opp = rcu_dereference(opp);

Ditto (we're an updater, not a reader).

drivers/base/power/opp.c line 503

Quoted 6 lines
drivers/base/power/opp.c new file +6 -0
+	/* Is update really needed? */
+	if (tmp_opp->available == availability_req)
+		goto out1;
+	/* copy the old data over */
+	*new_opp = *tmp_opp;
+	rcu_read_unlock();

Not necessary.

drivers/base/power/opp.c line 508

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+	/* plug in new node */
+	new_opp->available = availability_req;
+	list_replace_rcu(&opp->node, &new_opp->node);
+	mutex_unlock(&opp->dev_opp->lock);

Now unlock dev_opp_list_lock instead.

drivers/base/power/opp.c line 510

Quoted 2 lines
drivers/base/power/opp.c new file +2 -0
+	synchronize_rcu();
+

And rework the exit code below accordingly.

Quoted 13 lines
drivers/base/power/opp.c new file +13 -0
+	/* clean up old opp */
+	new_opp = opp;
+	goto out;
+
+out1:
+	mutex_unlock(&opp->dev_opp->lock);
+err:
+	rcu_read_unlock();
+out:
+	kfree(new_opp);
+	return r;
+}
+

...

drivers/base/power/opp.c line 598

Quoted 9 lines
drivers/base/power/opp.c new file +9 -0
+int opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	rcu_read_lock();

I would pretend I'm an updater here and acquire dev_opp_list_lock instead.

drivers/base/power/opp.c line 503

Quoted 3 lines
drivers/base/power/opp.c new file +3 -0
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		rcu_read_unlock();

So that won't be necessary.

drivers/base/power/opp.c line 605

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+		pr_warning("Unable to find device\n");
+		return PTR_ERR(dev_opp);
+	}
+

Now, you can sleep with the mutex held, so GFP_KERNEL may be used below.

drivers/base/power/opp.c line 609

Quoted 4 lines
drivers/base/power/opp.c new file +4 -0
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (opp_get_opp_count(dev) + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		rcu_read_unlock();

Drop dev_opp_list_lock instead.

drivers/base/power/opp.c line 614

Quoted 5 lines
drivers/base/power/opp.c new file +5 -0
+		pr_warning("Failed to allocate frequency table\n");
+		return -ENOMEM;
+	}
+
+	list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {

That may be list_for_each_entry() now.

drivers/base/power/opp.c line 621

Quoted 7 lines
drivers/base/power/opp.c new file +7 -0
+		if (opp->available) {
+			freq_table[i].index = i;
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+	rcu_read_unlock();

Drop dev_opp_list_lock instead.

drivers/base/power/opp.c line 629

Quoted 8 lines
drivers/base/power/opp.c new file +8 -0
+
+	freq_table[i].index = i;
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+
+	return 0;
+}

I think I didn't confuse anything, but surely Paul will fix me if I did. :-)

Thanks,
Rafael