PM / Domains: Rework default device stop governor function, v2

The existing default device stop governor function for PM domains,
default_stop_ok(), is supposed to check whether or not the device's
PM QoS latency constraint will be violated if the device is stopped
by pm_genpd_runtime_suspend().  However, the computations carried out
by it don't reflect the definition of the PM QoS latency constrait in
Documentation/ABI/testing/sysfs-devices-power.

Make default_stop_ok() follow the definition of the PM QoS latency
constrait.  In particular, make it take the device's start and stop
latencies correctly.

Add a new field, effective_constraint_ns, to struct gpd_timing_data
and use it to store the difference between the device's PM QoS
constraint and its resume latency for use by the device's parent
(the effective_constraint_ns values for the children are used for
computing the parent's one along with its PM QoS constraint).

Remove the break_even_ns field from struct gpd_timing_data, because
it's not used any more.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
PM / Domains: Rework default domain power off governor function, v2

The existing default domain power down governor function for PM
domains, default_power_down_ok(), is supposed to check whether or not
the PM QoS latency constraints of the devices in the domain will be
violated if the domain is turned off by pm_genpd_poweroff().
However, the computations carried out by it don't reflect the
definition of the PM QoS latency constrait in
Documentation/ABI/testing/sysfs-devices-power.

Make default_power_down_ok() follow the definition of the PM QoS
latency constrait.  In particular, make it only take latencies into
account, because it doesn't matter how much time has elapsed since
the domain's devices were suspended for the computation.

Remove the break_even_ns and power_off_time fields from
struct generic_pm_domain, because they are not necessary any more.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
PM / Domains: Make device removal more straightforward

The removal of a device from a PM domain doesn't have to browse
the domain's device list, because it can check directly if the
device belongs to the given domain.  Moreover, it should clear
the domain_data pointer in dev->power.subsys_data, because
dev_pm_put_subsys_data(dev) may not remove dev->power.subsys_data
and the stale domain data pointer may cause problems to happen.

Rework pm_genpd_remove_device() taking the above observations into
account.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
PM / Domains: Cache device stop and domain power off governor results, v3

The results of the default device stop and domain power off governor
functions for generic PM domains, default_stop_ok() and
default_power_down_ok(), depend only on the timing data of devices,
which are static, and on their PM QoS constraints.  Thus, in theory,
these functions only need to carry out their computations, which may
be time consuming in general, when it is known that the PM QoS
constraint of at least one of the devices in question has changed.

Use the PM QoS notifiers of devices to implement that.  First,
introduce new fields, constraint_changed and max_off_time_changed,
into struct gpd_timing_data and struct generic_pm_domain,
respectively, and register a PM QoS notifier function when adding
a device into a domain that will set those fields to 'true' whenever
the device's PM QoS constraint is modified.  Second, make
default_stop_ok() and default_power_down_ok() use those fields to
decide whether or not to carry out their computations from scratch.

The device and PM domain hierarchies are taken into account in that
and the expense is that the changes of PM QoS constraints of
suspended devices will not be taken into account immediately, which
isn't guaranteed anyway in general.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
PM / Domains: Fix link checking when add subdomain

Current pm_genpd_add_subdomain() will allow duplicated link between
master and slave domain.  This patch fixed it.

Because when current pm_genpd_add_subdomain() checks whether the link
between the master and slave generic PM domain already exists,
slave_links instead of master_links of master domain is used.

Signed-off-by: Huang Ying <ying.huang@intel.com>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
PM / Domains: Make it possible to add devices to inactive domains

The generic PM domains core code currently requires domains to be in
the "power on" state for adding devices to them, but this limitation
turns out to be inconvenient in some situations, so remove it.

For this purpose, make __pm_genpd_add_device() set the device's
need_restore flag if the domain is in the "power off" state, so that
the device's "restore state" (usually .runtime_resume()) callback
is executed when it is resumed after the domain has been turned on.
If the domain is in the "power on" state, the device's need_restore
flag will be cleared by __pm_genpd_add_device(), so that its "save
state" (usually .runtime_suspend()) callback is executed when the
domain is about to be turned off.  However, since that default
behavior need not be always desirable, add a helper function
pm_genpd_dev_need_restore() allowing a device's need_restore flag
to be set/unset at any time.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
PM / Domains: Fix computation of maximum domain off time

The default domain power off governor function for generic PM
domains, default_power_down_ok(), may violate subdomain maximum
off time limit by allowing the master domain to be off for too
long.  Namely, it only finds the minium of all device maximum
off times over the domain's devices and uses that to compute the
domain's maximum off time, but it should do the same for the
subdomains.

Fix this problem by modifying default_power_down_ok() to compute
the given domain's maximum off time as the difference between the
minimum off time over all devices and subdomains in the domain and
its power on latency.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
FIX "driver core: move the deferred probe pointer into the private area"

(moddingg33k): 'INIT_LIST_HEAD(&dev->deferred_probe);' should not exist
		any longer in 'void device_initialize()' after this patch.

		--> So let's remove it.

Nothing outside of the driver core needs to get to the deferred probe
pointer, so move it inside the private area of 'struct device' so no one
tries to mess around with it.

Cc: Grant Likely <grant.likely@secretlab.ca>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Remove dead code on some header files

Signed-off-by: moddingg33k <moddingg33k@t-online.de>
PM / Sleep: Print name of wakeup source that aborts suspend

Change-Id: I32610f93bedef1bbc396d55e09263b4d2424503c
Signed-off-by: Todd Poynor <toddpoynor@google.com>
PM / Sleep: Add wakeup_source_activate and wakeup_source_deactivate tracepoints

Add tracepoints to wakeup_source_activate and wakeup_source_deactivate.
Useful for checking that specific wakeup sources overlap as expected.

Signed-off-by: Arve Hjønnevåg <arve@android.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
ext4: Add support for FIDTRIM, a best-effort ioctl for deep discard trim

* What
This provides an interface for issuing an FITRIM which uses the
secure discard instead of just a discard.
Only the eMMC command is "secure", and not how the FS uses it:
due to the fact that the FS might reassign a region somewhere else,
the original deleted data will not be affected by the "trim" which only
handles un-used regions.
So we'll just call it "deep discard", and note that this is a
"best effort" cleanup.

* Why
Once in a while, We want to be able to cleanup most of the unused blocks
after erasing a bunch of files.
We don't want to constantly secure-discard via a mount option.

From an eMMC spec perspective, it tells the device to really get rid of
all the data for the specified blocks and not just put them back into the
pool of free ones (unlike the normal TRIM). The eMMC spec says the
secure trim handling must make sure the data (and metadata) is not available
anymore. A simple TRIM doesn't clear the data, it just puts blocks in the
free pool.
JEDEC Standard No. 84-A441
  7.6.9 Secure Erase
  7.6.10 Secure Trim

From an FS perspective, it is acceptable to leave some data behind.
 - directory entries related to deleted files
 - databases entries related to deleted files
 - small-file data stored in inode extents
 - blocks held by the FS waiting to be re-used (mitigated by sync).
 - blocks reassigned by the FS prior to FIDTRIM.

Change-Id: I676a1404a80130d93930c84898360f2e6fb2f81e
Signed-off-by: Geremy Condra <gcondra@google.com>
Signed-off-by: JP Abgrall <jpa@google.com>
ext4: remove huawei debug code

Signed-off-by: moddingg33k <moddingg33k@t-online.de>
ext4: protect group inode free counting with group lock

Now when we set the group inode free count, we don't have a proper
group lock so that multiple threads may decrease the inode free
count at the same time. And e2fsck will complain something like:

Free inodes count wrong for group #1 (1, counted=0).
Fix? no

Free inodes count wrong for group #2 (3, counted=0).
Fix? no

Directories count wrong for group #2 (780, counted=779).
Fix? no

Free inodes count wrong for group #3 (2272, counted=2273).
Fix? no

So this patch try to protect it with the ext4_lock_group.

btw, it is found by xfstests test case 269 and the volume is
mkfsed with the parameter
"-O ^resize_inode,^uninit_bg,extent,meta_bg,flex_bg,ext_attr"
and I have run it 100 times and the error in e2fsck doesn't
show up again.

Change-Id: Icfa95256688b50734e9152cbdc84a851a39b8ed5
Signed-off-by: Tao Ma <boyu.mt@taobao.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Undo stable backports

This is a squash of a bunch of stable backport patches that we
shouldn't be holding onto in msm-3.4 because they only apply to
kernels before v3.4.

Change-Id: I20e63e7a9ca69155e3e11dbb16d5ee21c7e49574
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
ashmem: fix 2 deadlock scenarios

	ashmem: avoid deadlock between read and mmap calls

	Avoid holding ashmem_mutex across code that can page fault.  Page faults
	grab the mmap_sem for the process, which are also held by mmap calls
	prior to calling ashmem_mmap, which locks ashmem_mutex.  The reversed
	order of locking between the two can deadlock.

	The calls that can page fault are read() and the ASHMEM_SET_NAME and
	ASHMEM_GET_NAME ioctls.  Move the code that accesses userspace pages
	outside the ashmem_mutex.

		Bug: 9261835
		Change-Id: If1322e981d29c889a56cdc9dfcbc6df2729a45e9
		Signed-off-by: Todd Poynor <toddpoynor@google.com>

	staging: android: ashmem: Avoid deadlock with mmap/shrink

	Both ashmem_mmap and ashmem_shrink take the ashmem_lock. It may
	be possible for ashmem_mmap to invoke ashmem_shrink:

	-000|mutex_lock(lock = 0x0)
	-001|ashmem_shrink(?, sc = 0x0) <--- try to take ashmem_mutex again
	-002|shrink_slab(shrink = 0xDA5F1CC0, nr_pages_scanned = 0, lru_pages
	-002|=
	-002|124)
	-003|try_to_free_pages(zonelist = 0x0, ?, ?, ?)
	-004|__alloc_pages_nodemask(gfp_mask = 21200, order = 1, zonelist =
	-004|0xC11D0940,
	-005|new_slab(s = 0xE4841E80, ?, node = -1)
	-006|__slab_alloc.isra.43.constprop.50(s = 0xE4841E80, gfpflags =
	-006|2148925462, ad
	-007|kmem_cache_alloc(s = 0xE4841E80, gfpflags = 208)
	-008|shmem_alloc_inode(?)
	-009|alloc_inode(sb = 0xE480E800)
	-010|new_inode_pseudo(?)
	-011|new_inode(?)
	-012|shmem_get_inode(sb = 0xE480E800, dir = 0x0, ?, dev = 0, flags =
	-012|187)
	-013|shmem_file_setup(?, ?, flags = 187)
	-014|ashmem_mmap(?, vma = 0xC5D64210) <---- Acquire ashmem_mutex
	-015|mmap_region(file = 0xDF8E2C00, addr = 1772974080, len = 233472,
	-015|flags = 57,
	-016|sys_mmap_pgoff(addr = 0, len = 230400, prot = 3, flags = 1, fd =
	-016|157, pgoff
	-017|ret_fast_syscall(asm)
	-->|exception
	-018|NUR:0x40097508(asm)
	---|end of frame

	Avoid this deadlock by using mutex_trylock in ashmem_shrink; if the mutex
	is already held, do not attempt to shrink.

		Change-Id: I222bbf55856d5849da813b730de0636c80966c8e
		Reported-by: Matt Wagantall <mattw@codeaurora.org>
		Reported-by: Syed Rameez Mustafa <rameezmustafa@codeaurora.org>
		Reported-by: Osvaldo Banuelos <osvaldob@codeaurora.org>
		Reported-by: Subbaraman Narayanamurthy <subbaram@codeaurora.org>
		Signed-off-by: Laura Abbott <lauraa@codeaurora.org>

Signed-off-by: moddingg33k <moddingg33k@t-online.de>
USB: gadget: f_audio_source: New gadget driver for audio output

This driver presents a standard USB audio class interface to the host
and an ALSA PCM device to userspace

Change-Id: If16b14a5ff27045f9cb2daaf1ae9195c5eeab7d0
Signed-off-by: Mike Lockwood <lockwood@google.com>
usb: gadget: f_audio_source: change max ISO packet size

Re-applying from
https://gitorious.org/shr/linux/commit/eb4c9d2db894c3492c0a848581bd4f6790f93d5f

Most USB-AUDIO devices are limited to 256 byte for max iso buffer size.
If a IN_EP_MAX_PACKET_SIZE is bigger than a USB-AUDIO device's max iso
buffer size, it will cause noise. This patch will prevent this case as
possibe by reducing packet size. When using 44.1khz, 2ch, 16bit audio
data, if max packet size is bigger than 176 bytes, it's no problem.

Credits to: Iliyan Malchev <malchev@google.com>

Change-Id: I61435bc9ad7ab05faba16cf80bfb9ff0431c3002
Signed-off-by: Anson Jacob <ansonkuzhumbil@gmail.com>
usb: gadget: f_audio_source: Fixed USB Audio Class Interface Descriptor

Fixed Android Issue #56549.

When both Vendor Class and Audio Class are activated for AOA 2.0,
the baInterfaceNr of the AudioControl Interface Descriptor points
to wrong interface numbers. They should be pointing to
Audio Control Device and Audio Streaming interfaces.

Replaced baInterfaceNr with the correct value.

Change-Id: Ib20f20c1d65887bac051b7ff9453dc53fc23a8d0
Signed-off-by: Anson Jacob <ansonkuzhumbil@gmail.com>
cgroup: Take css_set_lock from cgroup_css_sets_empty()

As indicated in the comment above cgroup_css_sets_empty it needs the
css_set_lock. But neither of the 2 call points have it, so rather then fixing
the callers just take the lock inside cgroup_css_sets_empty().

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Change-Id: If7aea71824f6d0e3f2cc6c1ce236c3ae6be2037b