06 Apr, 2019

2 commits

  • [ Upstream commit 840018668ce2d96783356204ff282d6c9b0e5f66 ]

    When pmu::setup_aux() is called the coresight PMU needs to know which
    sink to use for the session by looking up the information in the
    event's attr::config2 field.

    As such simply replace the cpu information by the complete perf_event
    structure and change all affected customers.

    Signed-off-by: Mathieu Poirier
    Reviewed-by: Suzuki Poulouse
    Acked-by: Peter Zijlstra
    Cc: Adrian Hunter
    Cc: Alexander Shishkin
    Cc: Alexei Starovoitov
    Cc: Greg Kroah-Hartman
    Cc: H. Peter Anvin
    Cc: Heiko Carstens
    Cc: Jiri Olsa
    Cc: Mark Rutland
    Cc: Martin Schwidefsky
    Cc: Namhyung Kim
    Cc: Thomas Gleixner
    Cc: Will Deacon
    Cc: linux-arm-kernel@lists.infradead.org
    Cc: linux-s390@vger.kernel.org
    Link: http://lkml.kernel.org/r/20190131184714.20388-2-mathieu.poirier@linaro.org
    Signed-off-by: Arnaldo Carvalho de Melo
    Signed-off-by: Sasha Levin

    Mathieu Poirier
     
  • [ Upstream commit 5666dfd1d8a45a167f0d8b4ef47ea7f780b1f24a ]

    SDM845 has ETMv4.2 and can use the existing etm4x driver.
    But the current etm driver checks only for ETMv4.0 and
    errors out for other etm4x versions. This patch adds this
    missing support to enable SoC's with ETMv4x to use same
    driver by checking only the ETM architecture major version
    number.

    Without this change, we get below error during etm probe:

    / # dmesg | grep etm
    [ 6.660093] coresight-etm4x: probe of 7040000.etm failed with error -22
    [ 6.666902] coresight-etm4x: probe of 7140000.etm failed with error -22
    [ 6.673708] coresight-etm4x: probe of 7240000.etm failed with error -22
    [ 6.680511] coresight-etm4x: probe of 7340000.etm failed with error -22
    [ 6.687313] coresight-etm4x: probe of 7440000.etm failed with error -22
    [ 6.694113] coresight-etm4x: probe of 7540000.etm failed with error -22
    [ 6.700914] coresight-etm4x: probe of 7640000.etm failed with error -22
    [ 6.707717] coresight-etm4x: probe of 7740000.etm failed with error -22

    With this change, etm probe is successful:

    / # dmesg | grep etm
    [ 6.659198] coresight-etm4x 7040000.etm: CPU0: ETM v4.2 initialized
    [ 6.665848] coresight-etm4x 7140000.etm: CPU1: ETM v4.2 initialized
    [ 6.672493] coresight-etm4x 7240000.etm: CPU2: ETM v4.2 initialized
    [ 6.679129] coresight-etm4x 7340000.etm: CPU3: ETM v4.2 initialized
    [ 6.685770] coresight-etm4x 7440000.etm: CPU4: ETM v4.2 initialized
    [ 6.692403] coresight-etm4x 7540000.etm: CPU5: ETM v4.2 initialized
    [ 6.699024] coresight-etm4x 7640000.etm: CPU6: ETM v4.2 initialized
    [ 6.705646] coresight-etm4x 7740000.etm: CPU7: ETM v4.2 initialized

    Signed-off-by: Sai Prakash Ranjan
    Reviewed-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman
    Signed-off-by: Sasha Levin

    Sai Prakash Ranjan
     

24 Mar, 2019

3 commits

  • commit 9ed3f22223c33347ed963e7c7019cf2956dd4e37 upstream.

    When an output port driver is removed, also remove references to it from
    any masters. Failing to do this causes a NULL ptr dereference when
    configuring another output port:

    > BUG: unable to handle kernel NULL pointer dereference at 000000000000000d
    > RIP: 0010:master_attr_store+0x9d/0x160 [intel_th_gth]
    > Call Trace:
    > dev_attr_store+0x1b/0x30
    > sysfs_kf_write+0x3c/0x50
    > kernfs_fop_write+0x125/0x1a0
    > __vfs_write+0x3a/0x190
    > ? __vfs_write+0x5/0x190
    > ? _cond_resched+0x1a/0x50
    > ? rcu_all_qs+0x5/0xb0
    > ? __vfs_write+0x5/0x190
    > vfs_write+0xb8/0x1b0
    > ksys_write+0x55/0xc0
    > __x64_sys_write+0x1a/0x20
    > do_syscall_64+0x5a/0x140
    > entry_SYSCALL_64_after_hwframe+0x44/0xa9

    Signed-off-by: Alexander Shishkin
    Fixes: b27a6a3f97b9 ("intel_th: Add Global Trace Hub driver")
    CC: stable@vger.kernel.org # v4.4+
    Reported-by: Ammy Yi
    Signed-off-by: Greg Kroah-Hartman

    Alexander Shishkin
     
  • commit bf7cbaae0831252b416f375ca9b1027ecd4642dd upstream.

    Using STP_POLICY_ID_SET ioctl command with dummy_stm device, or any STM
    device that supplies zero mmio channel size, will trigger a division by
    zero bug in the kernel.

    Prevent this by disallowing channel widths other than 1 for such devices.

    Signed-off-by: Alexander Shishkin
    Fixes: 7bd1d4093c2f ("stm class: Introduce an abstraction for System Trace Module devices")
    CC: stable@vger.kernel.org # v4.4+
    Signed-off-by: Greg Kroah-Hartman

    Alexander Shishkin
     
  • commit a1d75dad3a2c689e70a1c4e0214cca9de741d0aa upstream.

    There is a bug in the channel allocation logic that leads to an endless
    loop when looking for a contiguous range of channels in a range with a
    mixture of free and occupied channels. For example, opening three
    consequtive channels, closing the first two and requesting 4 channels in
    a row will trigger this soft lockup. The bug is that the search loop
    forgets to skip over the range once it detects that one channel in that
    range is occupied.

    Restore the original intent to the logic by fixing the omission.

    Signed-off-by: Zhi Jin
    Signed-off-by: Alexander Shishkin
    Fixes: 7bd1d4093c2f ("stm class: Introduce an abstraction for System Trace Module devices")
    CC: stable@vger.kernel.org # v4.4+
    Signed-off-by: Greg Kroah-Hartman

    Zhi Jin
     

13 Jan, 2019

1 commit

  • commit ec5b5ad6e272d8d6b92d1007f79574919862a2d2 upstream.

    The 'nr_pages' attribute of the 'msc' subdevices parses a comma-separated
    list of window sizes, passed from userspace. However, there is a bug in
    the string parsing logic wherein it doesn't exclude the comma character
    from the range of characters as it consumes them. This leads to an
    out-of-bounds access given a sufficiently long list. For example:

    > # echo 8,8,8,8 > /sys/bus/intel_th/devices/0-msc0/nr_pages
    > ==================================================================
    > BUG: KASAN: slab-out-of-bounds in memchr+0x1e/0x40
    > Read of size 1 at addr ffff8803ffcebcd1 by task sh/825
    >
    > CPU: 3 PID: 825 Comm: npktest.sh Tainted: G W 4.20.0-rc1+
    > Call Trace:
    > dump_stack+0x7c/0xc0
    > print_address_description+0x6c/0x23c
    > ? memchr+0x1e/0x40
    > kasan_report.cold.5+0x241/0x308
    > memchr+0x1e/0x40
    > nr_pages_store+0x203/0xd00 [intel_th_msu]

    Fix this by accounting for the comma character.

    Signed-off-by: Alexander Shishkin
    Fixes: ba82664c134ef ("intel_th: Add Memory Storage Unit driver")
    Cc: stable@vger.kernel.org # v4.4+
    Signed-off-by: Greg Kroah-Hartman

    Alexander Shishkin
     

14 Nov, 2018

1 commit

  • [ Upstream commit 987d1e8dcd370d96029a3d76a0031b043c4a69ae ]

    If the ETB is already enabled in sysfs mode, the ETB reports
    success even if a perf mode is requested. Fix this by checking
    the requested mode.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman
    Signed-off-by: Sasha Levin
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     

18 Sep, 2018

3 commits

  • This adds Intel(R) Trace Hub PCI ID for Ice Lake PCH.

    Signed-off-by: Alexander Shishkin
    Signed-off-by: Greg Kroah-Hartman

    Alexander Shishkin
     
  • The core of the driver expects the resource array from the glue layer
    to be indexed by even numbers, as is the case for 64-bit PCI resources.
    This doesn't hold true for others, ACPI in this instance, which leads
    to an out-of-bounds access and an ioremap() on whatever address that
    access fetches.

    This patch fixes the problem by reading resource array differently based
    on whether the 64-bit flag is set, which would indicate PCI glue layer.

    Signed-off-by: Alexander Shishkin
    Fixes: ebc57e399b8e ("intel_th: Add ACPI glue layer")
    CC: stable@vger.kernel.org # v4.17+
    Signed-off-by: Greg Kroah-Hartman

    Alexander Shishkin
     
  • Commit a753bfcfdb1f ("intel_th: Make the switch allocate its subdevices")
    brings in new subdevice addition/removal logic that's broken for "host
    mode": the SWITCH device has no children to begin with, which is not
    handled in the code. This results in a null dereference bug later down
    the path.

    This patch fixes the subdevice removal code to handle host mode correctly.

    Signed-off-by: Alexander Shishkin
    Fixes: a753bfcfdb1f ("intel_th: Make the switch allocate its subdevices")
    CC: stable@vger.kernel.org # v4.14+
    Signed-off-by: Greg Kroah-Hartman

    Alexander Shishkin
     

24 Aug, 2018

1 commit

  • Use new return type vm_fault_t for fault handler. For now, this is just
    documenting that the function returns a VM_FAULT value rather than an
    errno. Once all instances are converted, vm_fault_t will become a
    distinct type.

    See 1c8f422059ae ("mm: change return type to vm_fault_t") for reference.

    Link: http://lkml.kernel.org/r/20180702155801.GA4010@jordon-HP-15-Notebook-PC
    Signed-off-by: Souptick Joarder
    Cc: Alexander Shishkin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Souptick Joarder
     

15 Jul, 2018

21 commits

  • Probing the TPIU driver under UBSan triggers an out-of-bounds shift
    warning in coresight_timeout():

    ...
    [ 5.677530] UBSAN: Undefined behaviour in drivers/hwtracing/coresight/coresight.c:929:16
    [ 5.685542] shift exponent 64 is too large for 64-bit type 'long unsigned int'
    ...

    On closer inspection things are exponentially out of whack because we're
    passing a bitmask where a bit number should be. Amusingly, it seems that
    both calls will find their expected values by sheer luck and appear to
    succeed: 1 << FFCR_FON_MAN ends up at bit 64 which whilst undefined
    evaluates as zero in practice, while 1 << FFSR_FT_STOPPED finds bit 2
    (TCPresent) which apparently is usually tied high.

    Following the examples of other drivers, define separate FOO and FOO_BIT
    macros for masks vs. indices, and put things right.

    CC: Robert Walker
    CC: Mike Leach
    CC: Mathieu Poirier
    Fixes: 11595db8e17f ("coresight: Fix disabling of CoreSight TPIU")
    Signed-off-by: Robin Murphy
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Robin Murphy
     
  • Now that we can use a CATU with a scatter gather table, add support
    for the TMC ETR to make use of the connected CATU in translate mode.
    This is done by adding CATU as new buffer mode.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • This patch adds the support for setting up a SG table for use
    by the CATU. We reuse the tmc_sg_table to represent the table/data
    pages, even though the table format is different.

    Similar to ETR SG table, CATU uses a 4KB page size for data buffers
    as well as page tables. All table entries are 64bit wide and have
    the following format:

    63 12 1 0
    x-----------------------------------x
    | Address [63-12] | SBZ | V |
    x-----------------------------------x

    Where [V] -> 0 - Pointer is invalid
    1 - Pointer is Valid

    CATU uses only first half of the page for data page pointers.
    i.e, single table page will only have 256 page pointers, addressing
    upto 1MB of data. The second half of a table page contains only two
    pointers at the end of the page (i.e, pointers at index 510 and 511),
    which are used as links to the "Previous" and "Next" page tables
    respectively.

    The first table page has an "Invalid" previous pointer and the
    next pointer entry points to the second page table if there is one.
    Similarly the last table page has an "Invalid" next pointer to
    indicate the end of the table chain.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • Add the initial support for Coresight Address Translation Unit, which
    augments the TMC in Coresight SoC-600 by providing an improved Scatter
    Gather mechanism. CATU is always connected to a single TMC-ETR and
    converts the AXI address with a translated address (from a given SG
    table with specific format). The CATU should be programmed in pass
    through mode and enabled even if the ETR doesn't use the translation
    by CATU.

    This patch provides mechanism to enable/disable the CATU always in the
    pass through mode.

    We reuse the existing ports mechanism to link the TMC-ETR to the
    connected CATU.

    i.e, TMC-ETR:output_port0 -> CATU:input_port0

    Reference manual for CATU component is avilable in version r2p0 of :
    "Arm Coresight System-on-Chip SoC-600 Technical Reference Manual".

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • Add a new coresight device type, which do not belong to any
    of the existing types, i.e, source, sink, link etc. A helper
    device could be connected to a coresight device, which could
    augment the functionality of the coresight device.

    This is intended to cover Coresight Address Translation Unit (CATU)
    devices, which provide improved Scatter Gather mechanism for TMC
    ETR. The idea is that the helper device could be controlled by
    the driver of the device it is attached to (in this case ETR),
    transparent to the generic coresight driver (and paths).

    The operations include enable(), disable(), both of which could
    accept a device specific "data" which the driving device and
    the helper device could share. Since they don't appear in the
    coresight "path" tracked by software, we have to ensure that
    they are powered up/down whenever the master device is turned
    on.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • If we fail to find the input / output port for a LINK component
    while enabling a path, we should fail gracefully rather than
    assuming port "0".

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • We request for "CORESIGHT_BARRIER_PKT_SIZE" length and we should
    be happy when we get that size.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • The newly introduced code fails to build in some configurations
    unless we include the right headers:

    drivers/hwtracing/coresight/coresight-tmc-etr.c: In function 'tmc_free_table_pages':
    drivers/hwtracing/coresight/coresight-tmc-etr.c:206:3: error: implicit declaration of function 'vunmap'; did you mean 'iounmap'? [-Werror=implicit-function-declaration]

    Fixes: 79613ae8715a ("coresight: Add generic TMC sg table framework")
    Signed-off-by: Arnd Bergmann
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Arnd Bergmann
     
  • Now that we can dynamically switch between contiguous memory and
    SG table depending on the trace buffer size, provide the support
    for selecting an appropriate buffer size.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Acked-by: Rob Herring
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • Add the support for Scatter-Gather mode to the etr-buf layer.
    Since we now have two different modes, we choose the backend
    based on a set of conditions, documented in the code.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • The TMC-ETR can use the target trace buffer in two different modes.
    Normal physically contiguous mode and a discontiguous list pages in
    Scatter-Gather mode. Also we have dedicated Coresight component, CATU
    (Coresight Address Translation Unit) to provide improved scatter-gather
    mode in Coresight SoC-600. This complicates the management of the
    buffer used for trace, depending on the mode in which ETR is configured.

    So, this patch adds a transparent layer for managing the ETR buffer
    which abstracts the basic operations on the buffer (alloc, free,
    sync and retrieve the data) and uses the mode specific helpers to
    do the actual operation. This also allows the ETR driver to choose
    the best mode for a given use case and adds the flexibility to
    fallback to a different mode, without duplicating the code.

    The patch also adds the "normal" flat memory mode and switches
    the sysfs driver to use the new layer.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • This patch adds support for setting up an SG table used by the
    TMC ETR inbuilt SG unit. The TMC ETR uses 4K page sized tables
    to hold pointers to the 4K data pages with the last entry in a
    table pointing to the next table with the entries, by kind of
    chaining. The 2 LSBs determine the type of the table entry, to
    one of :

    Normal - Points to a 4KB data page.
    Last - Points to a 4KB data page, but is the last entry in the
    page table.
    Link - Points to another 4KB table page with pointers to data.

    The code takes care of handling the system page size which could
    be different than 4K. So we could end up putting multiple ETR
    SG tables in a single system page, vice versa for the data pages.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • This patch introduces a generic sg table data structure and
    associated operations. An SG table can be used to map a set
    of Data pages where the trace data could be stored by the TMC
    ETR. The information about the data pages could be stored in
    different formats, depending on the type of the underlying
    SG mechanism (e.g, TMC ETR SG vs Coresight CATU). The generic
    structure provides book keeping of the pages used for the data
    as well as the table contents. The table should be filled by
    the user of the infrastructure.

    A table can be created by specifying the number of data pages
    as well as the number of table pages required to hold the
    pointers, where the latter could be different for different
    types of tables. The pages are mapped in the appropriate dma
    data direction mode (i.e, DMA_TO_DEVICE for table pages
    and DMA_FROM_DEVICE for data pages). The framework can optionally
    accept a set of allocated data pages (e.g, perf ring buffer) and
    map them accordingly. The table and data pages are vmap'ed to allow
    easier access by the drivers. The framework also provides helpers to
    sync the data written to the pages with appropriate directions.

    This will be later used by the TMC ETR SG unit and CATU.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • We are about to add the support for ETR builtin scatter-gather mode
    for dealing with large amount of trace buffers. However, on some of
    the platforms, using the ETR SG mode can lock up the system due to
    the way the ETR is connected to the memory subsystem.

    In SG mode, the ETR performs READ from the scatter-gather table to
    fetch the next page and regular WRITE of trace data. If the READ
    operation doesn't complete(due to the memory subsystem issues,
    which we have seen on a couple of platforms) the trace WRITE
    cannot proceed leading to issues. So, we by default do not
    use the SG mode, unless it is known to be safe on the platform.
    We define a DT property for the TMC node to specify whether we
    have a proper SG mode.

    Cc: Mathieu Poirier
    Cc: Mike Leach
    Cc: Mark Rutland
    Cc: John Horley
    Cc: Robert Walker
    Cc: devicetree@vger.kernel.org
    Cc: frowand.list@gmail.com
    Reviewed-by: Rob Herring
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • Right now we open code filling the trace buffer with synchronization
    packets when the circular buffer wraps around in different drivers.
    Move this to a common place. While at it, clean up the barrier_pkt
    array to strip off the trailing '\0'.

    Cc: Mathieu Poirier
    Cc: Mike Leach
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • We don't support ETR in perf mode yet. So, don't
    even try to enable the hardware, even by mistake.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • We zero out the entire trace buffer used for ETR before it is enabled,
    for helping with debugging. With the addition of scatter-gather mode,
    the buffer could be bigger and non-contiguous.

    Get rid of this step; if someone wants to debug, they can always add it
    as and when needed.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • At the moment we adjust the buffer pointers for reading the trace
    data via misc device in the common code for ETF/ETB and ETR. Since
    we are going to change how we manage the buffer for ETR, let us
    move the buffer manipulation to the respective driver files, hiding
    it from the common code. We do so by adding type specific helpers
    for finding the length of data and the pointer to the buffer,
    for a given length at a file position.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • Add ETM PIDs of the Arm cortex-A CPUs to the white list of ETMs.
    While at it add a helper macro to make it easier to add the new
    entries.

    Cc: Mathieu Poirier
    Signed-off-by: Suzuki K Poulose
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Suzuki K Poulose
     
  • As with ETM3x, the ETM4x tracers can trigger trace acquisition based on
    contextID value, something that isn't useful when PID namespaces are
    enabled. Indeed the PID value of a process has a different representation
    in the kernel and the PID namespace, making the feature confusing and
    potentially leaking internal kernel information.

    As such simply return an error when the feature is being used from a
    PID namespace other than the default one.

    Signed-off-by: Mathieu Poirier
    Reviewed-by: Kim Phillips
    Acked-by: "Eric W. Biederman"
    Signed-off-by: Greg Kroah-Hartman

    Mathieu Poirier
     
  • Tracers can trigger trace acquisition based on contextID value, something
    that isn't useful when PID namespaces are enabled. Indeed the PID value
    of a process has a different representation in the kernel and the PID
    namespace, making the feature confusing and potentially leaking internal
    kernel information.

    As such simply return an error when the feature is being used from a
    PID namespace other than the default one.

    Signed-off-by: Mathieu Poirier
    Reviewed-by: Kim Phillips
    Acked-by: "Eric W. Biederman"
    Signed-off-by: Greg Kroah-Hartman

    Mathieu Poirier
     

13 Jun, 2018

1 commit

  • The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
    This patch replaces cases of:

    devm_kzalloc(handle, a * b, gfp)

    with:
    devm_kcalloc(handle, a * b, gfp)

    as well as handling cases of:

    devm_kzalloc(handle, a * b * c, gfp)

    with:

    devm_kzalloc(handle, array3_size(a, b, c), gfp)

    as it's slightly less ugly than:

    devm_kcalloc(handle, array_size(a, b), c, gfp)

    This does, however, attempt to ignore constant size factors like:

    devm_kzalloc(handle, 4 * 1024, gfp)

    though any constants defined via macros get caught up in the conversion.

    Any factors with a sizeof() of "unsigned char", "char", and "u8" were
    dropped, since they're redundant.

    Some manual whitespace fixes were needed in this patch, as Coccinelle
    really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".

    The Coccinelle script used for this was:

    // Fix redundant parens around sizeof().
    @@
    expression HANDLE;
    type TYPE;
    expression THING, E;
    @@

    (
    devm_kzalloc(HANDLE,
    - (sizeof(TYPE)) * E
    + sizeof(TYPE) * E
    , ...)
    |
    devm_kzalloc(HANDLE,
    - (sizeof(THING)) * E
    + sizeof(THING) * E
    , ...)
    )

    // Drop single-byte sizes and redundant parens.
    @@
    expression HANDLE;
    expression COUNT;
    typedef u8;
    typedef __u8;
    @@

    (
    devm_kzalloc(HANDLE,
    - sizeof(u8) * (COUNT)
    + COUNT
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(__u8) * (COUNT)
    + COUNT
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(char) * (COUNT)
    + COUNT
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(unsigned char) * (COUNT)
    + COUNT
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(u8) * COUNT
    + COUNT
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(__u8) * COUNT
    + COUNT
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(char) * COUNT
    + COUNT
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(unsigned char) * COUNT
    + COUNT
    , ...)
    )

    // 2-factor product with sizeof(type/expression) and identifier or constant.
    @@
    expression HANDLE;
    type TYPE;
    expression THING;
    identifier COUNT_ID;
    constant COUNT_CONST;
    @@

    (
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(TYPE) * (COUNT_ID)
    + COUNT_ID, sizeof(TYPE)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(TYPE) * COUNT_ID
    + COUNT_ID, sizeof(TYPE)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(TYPE) * (COUNT_CONST)
    + COUNT_CONST, sizeof(TYPE)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(TYPE) * COUNT_CONST
    + COUNT_CONST, sizeof(TYPE)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(THING) * (COUNT_ID)
    + COUNT_ID, sizeof(THING)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(THING) * COUNT_ID
    + COUNT_ID, sizeof(THING)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(THING) * (COUNT_CONST)
    + COUNT_CONST, sizeof(THING)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(THING) * COUNT_CONST
    + COUNT_CONST, sizeof(THING)
    , ...)
    )

    // 2-factor product, only identifiers.
    @@
    expression HANDLE;
    identifier SIZE, COUNT;
    @@

    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - SIZE * COUNT
    + COUNT, SIZE
    , ...)

    // 3-factor product with 1 sizeof(type) or sizeof(expression), with
    // redundant parens removed.
    @@
    expression HANDLE;
    expression THING;
    identifier STRIDE, COUNT;
    type TYPE;
    @@

    (
    devm_kzalloc(HANDLE,
    - sizeof(TYPE) * (COUNT) * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(TYPE) * (COUNT) * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(TYPE) * COUNT * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(TYPE) * COUNT * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(THING) * (COUNT) * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(THING) * (COUNT) * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(THING) * COUNT * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(THING) * COUNT * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    )

    // 3-factor product with 2 sizeof(variable), with redundant parens removed.
    @@
    expression HANDLE;
    expression THING1, THING2;
    identifier COUNT;
    type TYPE1, TYPE2;
    @@

    (
    devm_kzalloc(HANDLE,
    - sizeof(TYPE1) * sizeof(TYPE2) * COUNT
    + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(THING1) * sizeof(THING2) * COUNT
    + array3_size(COUNT, sizeof(THING1), sizeof(THING2))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(THING1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(THING1), sizeof(THING2))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(TYPE1) * sizeof(THING2) * COUNT
    + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
    , ...)
    |
    devm_kzalloc(HANDLE,
    - sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
    , ...)
    )

    // 3-factor product, only identifiers, with redundant parens removed.
    @@
    expression HANDLE;
    identifier STRIDE, SIZE, COUNT;
    @@

    (
    devm_kzalloc(HANDLE,
    - (COUNT) * STRIDE * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - COUNT * (STRIDE) * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - COUNT * STRIDE * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - (COUNT) * (STRIDE) * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - COUNT * (STRIDE) * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - (COUNT) * STRIDE * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - (COUNT) * (STRIDE) * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - COUNT * STRIDE * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    )

    // Any remaining multi-factor products, first at least 3-factor products,
    // when they're not all constants...
    @@
    expression HANDLE;
    expression E1, E2, E3;
    constant C1, C2, C3;
    @@

    (
    devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
    |
    devm_kzalloc(HANDLE,
    - (E1) * E2 * E3
    + array3_size(E1, E2, E3)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - (E1) * (E2) * E3
    + array3_size(E1, E2, E3)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - (E1) * (E2) * (E3)
    + array3_size(E1, E2, E3)
    , ...)
    |
    devm_kzalloc(HANDLE,
    - E1 * E2 * E3
    + array3_size(E1, E2, E3)
    , ...)
    )

    // And then all remaining 2 factors products when they're not all constants,
    // keeping sizeof() as the second factor argument.
    @@
    expression HANDLE;
    expression THING, E1, E2;
    type TYPE;
    constant C1, C2, C3;
    @@

    (
    devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
    |
    devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
    |
    devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
    |
    devm_kzalloc(HANDLE, C1 * C2, ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(TYPE) * (E2)
    + E2, sizeof(TYPE)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(TYPE) * E2
    + E2, sizeof(TYPE)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(THING) * (E2)
    + E2, sizeof(THING)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - sizeof(THING) * E2
    + E2, sizeof(THING)
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - (E1) * E2
    + E1, E2
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - (E1) * (E2)
    + E1, E2
    , ...)
    |
    - devm_kzalloc
    + devm_kcalloc
    (HANDLE,
    - E1 * E2
    + E1, E2
    , ...)
    )

    Signed-off-by: Kees Cook

    Kees Cook
     

06 Jun, 2018

1 commit

  • Pull char/misc driver updates from Greg KH:
    "Here is the "big" char and misc driver patches for 4.18-rc1.

    It's not a lot of stuff here, but there are some highlights:

    - coreboot driver updates

    - soundwire driver updates

    - android binder updates

    - fpga big sync, mostly documentation

    - lots of minor driver updates

    All of these have been in linux-next for a while with no reported
    issues"

    * tag 'char-misc-4.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc: (81 commits)
    vmw_balloon: fixing double free when batching mode is off
    MAINTAINERS: Add driver-api/fpga path
    fpga: clarify that unregister functions also free
    documentation: fpga: move fpga-region.txt to driver-api
    documentation: fpga: add bridge document to driver-api
    documentation: fpga: move fpga-mgr.txt to driver-api
    Documentation: fpga: move fpga overview to driver-api
    fpga: region: kernel-doc fixes
    fpga: bridge: kernel-doc fixes
    fpga: mgr: kernel-doc fixes
    fpga: use SPDX
    fpga: region: change api, add fpga_region_create/free
    fpga: bridge: change api, don't use drvdata
    fpga: manager: change api, don't use drvdata
    fpga: region: don't use drvdata in common fpga code
    Drivers: hv: vmbus: Removed an unnecessary cast from void *
    ver_linux: Drop redundant calls to system() to test if file is readable
    ver_linux: Move stderr redirection from function parameter to function body
    misc: IBM Virtual Management Channel Driver (VMC)
    rpmsg: Correct support for MODULE_DEVICE_TABLE()
    ...

    Linus Torvalds
     

26 May, 2018

3 commits

  • Commit b5e2ced9bf81 ("stm class: Use vmalloc for the master map") caused
    a build error on some arches as vmalloc.h was not explicitly included.

    Fix that by adding it to the list of includes.

    Fixes: b5e2ced9bf81 ("stm class: Use vmalloc for the master map")
    Reported-by: kbuild test robot
    Cc: Alexander Shishkin
    Signed-off-by: Greg Kroah-Hartman

    Greg Kroah-Hartman
     
  • Commit d5c435df4a890 ("intel_th: msu: Use the real device in case of IOMMU
    domain allocation") changes dma buffer allocation to use the actual
    underlying device, but forgets to change the deallocation path, which leads
    to (if you've got CAP_SYS_RAWIO):

    > # echo 0,0 > /sys/bus/intel_th/devices/0-msc0/nr_pages
    > ------------[ cut here ]------------
    > kernel BUG at ../linux/drivers/iommu/intel-iommu.c:3670!
    > CPU: 3 PID: 231 Comm: sh Not tainted 4.17.0-rc1+ #2729
    > RIP: 0010:intel_unmap+0x11e/0x130
    ...
    > Call Trace:
    > intel_free_coherent+0x3e/0x60
    > msc_buffer_win_free+0x100/0x160 [intel_th_msu]

    This patch fixes the buffer deallocation code to use the correct device.

    Signed-off-by: Alexander Shishkin
    Fixes: d5c435df4a890 ("intel_th: msu: Use the real device in case of IOMMU domain allocation")
    Reported-by: Baofeng Tian
    CC: stable@vger.kernel.org # v4.14+
    Signed-off-by: Greg Kroah-Hartman

    Alexander Shishkin
     
  • Fengguang is running into a warning from the buddy allocator:

    > swapper/0: page allocation failure: order:9, mode:0x14040c0(GFP_KERNEL|__GFP_COMP), nodemask=(null)
    > CPU: 1 PID: 1 Comm: swapper/0 Not tainted 4.17.0-rc1 #262
    > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014
    > Call Trace:
    ...
    > __kmalloc+0x14b/0x180: ____cache_alloc at mm/slab.c:3127
    > stm_register_device+0xf3/0x5c0: stm_register_device at drivers/hwtracing/stm/core.c:695
    ...

    Which is basically a result of the stm class trying to allocate ~512kB
    for the dummy_stm with its default parameters. There's no reason, however,
    for it not to be vmalloc()ed instead, which is what this patch does.

    Reported-by: Fengguang Wu
    Signed-off-by: Alexander Shishkin
    CC: stable@vger.kernel.org # v4.4+
    Signed-off-by: Greg Kroah-Hartman

    Alexander Shishkin
     

14 May, 2018

3 commits

  • The driver prints pcsr twice: the first time it uses specifier %px to
    print hexadecimal pcsr value and the second time uses specifier %pS for
    output kernel symbols.

    As suggested by Kees, using %pS should be sufficient and %px isn't
    necessary; the reason is if the pcsr is a kernel space address, we can
    easily get to know the code line from %pS format, on the other hand, if
    the pcsr value doesn't fall into kernel space range (e.g. if the CPU is
    stuck in firmware), %pS also gives out pcsr hexadecimal value.

    So this commit removes useless %px and update section "Output format"
    in the document for alignment between the code and document.

    Suggested-by: Kees Cook
    Cc: Mathieu Poirier
    Signed-off-by: Leo Yan
    Reviewed-by: Kees Cook
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Leo Yan
     
  • The simple removal of an extra newline, no change in functionality.

    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Mathieu Poirier
     
  • Variable 'paddr' can't be used if uninitialised but is nonetheless
    confusing to some static checker. As such simply initialise it to zero.

    Reported-by: Dan Carpenter
    Signed-off-by: Mathieu Poirier
    Signed-off-by: Greg Kroah-Hartman

    Mathieu Poirier