USB: usbfs: Use a spinlock instead of atomic accesses to tally used memory.
authorIngo Rohloff <ingo.rohloff@lauterbach.com>
Wed, 9 Feb 2022 12:33:03 +0000 (13:33 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 11 Feb 2022 10:01:09 +0000 (11:01 +0100)
commit6a3cd5bef2531a1178234efa3bed788e3b3831f0
tree9a7b97a9e84d34ed8c4a933d1848a6b55de6a05b
parentc3c9cee592828528fd228b01d312c7526c584a42
USB: usbfs: Use a spinlock instead of atomic accesses to tally used memory.

While the existing code code imposes a limit on the used memory, it might be
over pessimistic (even if this is unlikely).

Example scenario:
8 threads running in parallel, all entering
"usbfs_increase_memory_usage()" at the same time.
The atomic accesses in "usbfs_increase_memory_usage()" could be
serialized like this:
  8 x "atomic64_add"
  8 x "atomic64_read"
If the 8 x "atomic64_add" raise "usbfs_memory_usage" above the limit,
then all 8 calls of "usbfs_increase_memory_usage()" will return with
-ENOMEM.  If you instead serialize over the whole access to
"usbfs_memory_usage" by using a spinlock, some of these calls will
succeed.

Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>
Link: https://lore.kernel.org/r/20220209123303.103340-2-ingo.rohloff@lauterbach.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/core/devio.c