1. 03 8月, 2018 4 次提交
  2. 13 6月, 2018 1 次提交
    • K
      treewide: kzalloc() -> kcalloc() · 6396bb22
      Kees Cook 提交于
      The kzalloc() function has a 2-factor argument form, kcalloc(). This
      patch replaces cases of:
      
              kzalloc(a * b, gfp)
      
      with:
              kcalloc(a * b, gfp)
      
      as well as handling cases of:
      
              kzalloc(a * b * c, gfp)
      
      with:
      
              kzalloc(array3_size(a, b, c), gfp)
      
      as it's slightly less ugly than:
      
              kzalloc_array(array_size(a, b), c, gfp)
      
      This does, however, attempt to ignore constant size factors like:
      
              kzalloc(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.
      
      The Coccinelle script used for this was:
      
      // Fix redundant parens around sizeof().
      @@
      type TYPE;
      expression THING, E;
      @@
      
      (
        kzalloc(
      -	(sizeof(TYPE)) * E
      +	sizeof(TYPE) * E
        , ...)
      |
        kzalloc(
      -	(sizeof(THING)) * E
      +	sizeof(THING) * E
        , ...)
      )
      
      // Drop single-byte sizes and redundant parens.
      @@
      expression COUNT;
      typedef u8;
      typedef __u8;
      @@
      
      (
        kzalloc(
      -	sizeof(u8) * (COUNT)
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(__u8) * (COUNT)
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(char) * (COUNT)
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(unsigned char) * (COUNT)
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(u8) * COUNT
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(__u8) * COUNT
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(char) * COUNT
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(unsigned char) * COUNT
      +	COUNT
        , ...)
      )
      
      // 2-factor product with sizeof(type/expression) and identifier or constant.
      @@
      type TYPE;
      expression THING;
      identifier COUNT_ID;
      constant COUNT_CONST;
      @@
      
      (
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * (COUNT_ID)
      +	COUNT_ID, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * COUNT_ID
      +	COUNT_ID, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * (COUNT_CONST)
      +	COUNT_CONST, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * COUNT_CONST
      +	COUNT_CONST, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * (COUNT_ID)
      +	COUNT_ID, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * COUNT_ID
      +	COUNT_ID, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * (COUNT_CONST)
      +	COUNT_CONST, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * COUNT_CONST
      +	COUNT_CONST, sizeof(THING)
        , ...)
      )
      
      // 2-factor product, only identifiers.
      @@
      identifier SIZE, COUNT;
      @@
      
      - kzalloc
      + kcalloc
        (
      -	SIZE * COUNT
      +	COUNT, SIZE
        , ...)
      
      // 3-factor product with 1 sizeof(type) or sizeof(expression), with
      // redundant parens removed.
      @@
      expression THING;
      identifier STRIDE, COUNT;
      type TYPE;
      @@
      
      (
        kzalloc(
      -	sizeof(TYPE) * (COUNT) * (STRIDE)
      +	array3_size(COUNT, STRIDE, sizeof(TYPE))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE) * (COUNT) * STRIDE
      +	array3_size(COUNT, STRIDE, sizeof(TYPE))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE) * COUNT * (STRIDE)
      +	array3_size(COUNT, STRIDE, sizeof(TYPE))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE) * COUNT * STRIDE
      +	array3_size(COUNT, STRIDE, sizeof(TYPE))
        , ...)
      |
        kzalloc(
      -	sizeof(THING) * (COUNT) * (STRIDE)
      +	array3_size(COUNT, STRIDE, sizeof(THING))
        , ...)
      |
        kzalloc(
      -	sizeof(THING) * (COUNT) * STRIDE
      +	array3_size(COUNT, STRIDE, sizeof(THING))
        , ...)
      |
        kzalloc(
      -	sizeof(THING) * COUNT * (STRIDE)
      +	array3_size(COUNT, STRIDE, sizeof(THING))
        , ...)
      |
        kzalloc(
      -	sizeof(THING) * COUNT * STRIDE
      +	array3_size(COUNT, STRIDE, sizeof(THING))
        , ...)
      )
      
      // 3-factor product with 2 sizeof(variable), with redundant parens removed.
      @@
      expression THING1, THING2;
      identifier COUNT;
      type TYPE1, TYPE2;
      @@
      
      (
        kzalloc(
      -	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
      +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
      +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
        , ...)
      |
        kzalloc(
      -	sizeof(THING1) * sizeof(THING2) * COUNT
      +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
        , ...)
      |
        kzalloc(
      -	sizeof(THING1) * sizeof(THING2) * (COUNT)
      +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE1) * sizeof(THING2) * COUNT
      +	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
      +	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
        , ...)
      )
      
      // 3-factor product, only identifiers, with redundant parens removed.
      @@
      identifier STRIDE, SIZE, COUNT;
      @@
      
      (
        kzalloc(
      -	(COUNT) * STRIDE * SIZE
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	COUNT * (STRIDE) * SIZE
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	COUNT * STRIDE * (SIZE)
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	(COUNT) * (STRIDE) * SIZE
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	COUNT * (STRIDE) * (SIZE)
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	(COUNT) * STRIDE * (SIZE)
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	(COUNT) * (STRIDE) * (SIZE)
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	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 E1, E2, E3;
      constant C1, C2, C3;
      @@
      
      (
        kzalloc(C1 * C2 * C3, ...)
      |
        kzalloc(
      -	(E1) * E2 * E3
      +	array3_size(E1, E2, E3)
        , ...)
      |
        kzalloc(
      -	(E1) * (E2) * E3
      +	array3_size(E1, E2, E3)
        , ...)
      |
        kzalloc(
      -	(E1) * (E2) * (E3)
      +	array3_size(E1, E2, E3)
        , ...)
      |
        kzalloc(
      -	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 THING, E1, E2;
      type TYPE;
      constant C1, C2, C3;
      @@
      
      (
        kzalloc(sizeof(THING) * C2, ...)
      |
        kzalloc(sizeof(TYPE) * C2, ...)
      |
        kzalloc(C1 * C2 * C3, ...)
      |
        kzalloc(C1 * C2, ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * (E2)
      +	E2, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * E2
      +	E2, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * (E2)
      +	E2, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * E2
      +	E2, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	(E1) * E2
      +	E1, E2
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	(E1) * (E2)
      +	E1, E2
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	E1 * E2
      +	E1, E2
        , ...)
      )
      Signed-off-by: NKees Cook <keescook@chromium.org>
      6396bb22
  3. 08 5月, 2018 1 次提交
  4. 10 4月, 2018 1 次提交
    • B
      scsi: qla2xxx: Fix small memory leak in qla2x00_probe_one on probe failure · 6d634067
      Bill Kuzeja 提交于
      The code that fixes the crashes in the following commit introduced a small
      memory leak:
      
      commit 6a2cf8d3 ("scsi: qla2xxx: Fix crashes in qla2x00_probe_one on probe failure")
      
      Fixing this requires a bit of reworking, which I've explained. Also provide
      some code cleanup.
      
      There is a small window in qla2x00_probe_one where if qla2x00_alloc_queues
      fails, we end up never freeing req and rsp and leak 0xc0 and 0xc8 bytes
      respectively (the sizes of req and rsp).
      
      I originally put in checks to test for this condition which were based on
      the incorrect assumption that if ha->rsp_q_map and ha->req_q_map were
      allocated, then rsp and req were allocated as well. This is incorrect.
      There is a window between these allocations:
      
             ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
                      goto probe_hw_failed;
      
      [if successful, both rsp and req allocated]
      
             base_vha = qla2x00_create_host(sht, ha);
                      goto probe_hw_failed;
      
             ret = qla2x00_request_irqs(ha, rsp);
                      goto probe_failed;
      
             if (qla2x00_alloc_queues(ha, req, rsp)) {
                      goto probe_failed;
      
      [if successful, now ha->rsp_q_map and ha->req_q_map allocated]
      
      To simplify this, we should just set req and rsp to NULL after we free
      them. Sounds simple enough? The problem is that req and rsp are pointers
      defined in the qla2x00_probe_one and they are not always passed by reference
      to the routines that free them.
      
      Here are paths which can free req and rsp:
      
      PATH 1:
      qla2x00_probe_one
         ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
         [req and rsp are passed by reference, but if this fails, we currently
          do not NULL out req and rsp. Easily fixed]
      
      PATH 2:
      qla2x00_probe_one
         failing in qla2x00_request_irqs or qla2x00_alloc_queues
            probe_failed:
               qla2x00_free_device(base_vha);
                  qla2x00_free_req_que(ha, req)
                  qla2x00_free_rsp_que(ha, rsp)
      
      PATH 3:
      qla2x00_probe_one:
         failing in qla2x00_mem_alloc or qla2x00_create_host
            probe_hw_failed:
               qla2x00_free_req_que(ha, req)
               qla2x00_free_rsp_que(ha, rsp)
      
      PATH 1: This should currently work, but it doesn't because rsp and rsp are
      not set to NULL in qla2x00_mem_alloc. Easily remedied.
      
      PATH 2: req and rsp aren't passed in at all to qla2x00_free_device but are
      derived from ha->req_q_map[0] and ha->rsp_q_map[0]. These are only set up if
      qla2x00_alloc_queues succeeds.
      
      In qla2x00_free_queues, we are protected from crashing if these don't exist
      because req_qid_map and rsp_qid_map are only set on their allocation. We are
      guarded in this way:
      
              for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
                      if (!test_bit(cnt, ha->req_qid_map))
                              continue;
      
      PATH 3: This works. We haven't freed req or rsp yet (or they were never
      allocated if qla2x00_mem_alloc failed), so we'll attempt to free them here.
      
      To summarize, there are a few small changes to make this work correctly and
      (and for some cleanup):
      
      1) (For PATH 1) Set *rsp and *req to NULL in case of failure in
      qla2x00_mem_alloc so these are correctly set to NULL back in
      qla2x00_probe_one
      
      2) After jumping to probe_failed: and calling qla2x00_free_device,
      explicitly set rsp and req to NULL so further calls with these pointers do
      not crash, i.e. the free queue calls in the probe_hw_failed section we fall
      through to.
      
      3) Fix return code check in the call to qla2x00_alloc_queues. We currently
      drop the return code on the floor. The probe fails but the caller of the
      probe doesn't have an error code, so it attaches to pci. This can result in
      a crash on module shutdown.
      
      4) Remove unnecessary NULL checks in qla2x00_free_req_que,
      qla2x00_free_rsp_que, and the egregious NULL checks before kfrees and vfrees
      in qla2x00_mem_free.
      
      I tested this out running a scenario where the card breaks at various times
      during initialization. I made sure I forced every error exit path in
      qla2x00_probe_one.
      
      Cc: <stable@vger.kernel.org> # v4.16
      Fixes: 6a2cf8d3 ("scsi: qla2xxx: Fix crashes in qla2x00_probe_one on probe failure")
      Signed-off-by: NBill Kuzeja <william.kuzeja@stratus.com>
      Acked-by: NHimanshu Madhani <himanshu.madhani@cavium.com>
      Signed-off-by: NMartin K. Petersen <martin.petersen@oracle.com>
      6d634067
  5. 28 3月, 2018 1 次提交
  6. 22 3月, 2018 4 次提交
  7. 07 3月, 2018 1 次提交
  8. 02 3月, 2018 2 次提交
  9. 31 1月, 2018 1 次提交
  10. 17 1月, 2018 1 次提交
    • H
      scsi: qla2xxx: Fix NULL pointer crash due to probe failure · d64d6c56
      himanshu.madhani@cavium.com 提交于
      This patch fixes regression added by commit d7459527
      ("scsi: qla2xxx: Add multiple queue pair functionality.").
      
      When driver is not able to get reqeusted IRQs from the system, driver will
      attempt tp clean up memory before failing hardware probe. During this cleanup,
      driver assigns NULL value to the pointer which has not been allocated by
      driver yet. This results in a NULL pointer access.
      
      Log file will show following message and stack trace
      
      qla2xxx [0000:a3:00.1]-00c7:21: MSI-X: Failed to enable support, giving up -- 32/-1.
      qla2xxx [0000:a3:00.1]-0037:21: Falling back-to MSI mode --1.
      qla2xxx [0000:a3:00.1]-003a:21: Failed to reserve interrupt 821 already in use.
      BUG: unable to handle kernel NULL pointer dereference at (null)
      IP: [<ffffffffc010c4b6>] qla2x00_probe_one+0x18b6/0x2730 [qla2xxx]
      PGD 0
      Oops: 0002 [#1] SMP
      
      Fixes: d7459527 ("scsi: qla2xxx: Add multiple queue pair functionality.").
      Cc: <stable@vger.kernel.org> # 4.10
      Signed-off-by: NHimanshu Madhani <himanshu.madhani@cavium.com>
      Signed-off-by: NMartin K. Petersen <martin.petersen@oracle.com>
      d64d6c56
  11. 04 1月, 2018 13 次提交
  12. 08 12月, 2017 6 次提交
  13. 09 11月, 2017 1 次提交
  14. 07 11月, 2017 1 次提交
    • B
      scsi: qla2xxx: Suppress a kernel complaint in qla_init_base_qpair() · 86531887
      Bart Van Assche 提交于
      Avoid that the following is reported while loading the qla2xxx
      kernel module:
      
      BUG: using smp_processor_id() in preemptible [00000000] code: modprobe/783
      caller is debug_smp_processor_id+0x17/0x20
      CPU: 7 PID: 783 Comm: modprobe Not tainted 4.14.0-rc8-dbg+ #2
      Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
      Call Trace:
       dump_stack+0x8e/0xce
       check_preemption_disabled+0xe3/0xf0
       debug_smp_processor_id+0x17/0x20
       qla2x00_probe_one+0xf43/0x26c0 [qla2xxx]
       pci_device_probe+0xca/0x140
       driver_probe_device+0x2e2/0x440
       __driver_attach+0xa3/0xe0
       bus_for_each_dev+0x5f/0x90
       driver_attach+0x19/0x20
       bus_add_driver+0x1c0/0x260
       driver_register+0x5b/0xd0
       __pci_register_driver+0x63/0x70
       qla2x00_module_init+0x1d6/0x222 [qla2xxx]
       do_one_initcall+0x3c/0x163
       do_init_module+0x55/0x1eb
       load_module+0x20a2/0x2890
       SYSC_finit_module+0xd7/0xf0
       SyS_finit_module+0x9/0x10
       entry_SYSCALL_64_fastpath+0x23/0xc2
      
      Fixes: commit 8abfa9e2 ("scsi: qla2xxx: Add function call to qpair for door bell")
      Signed-off-by: NBart Van Assche <bart.vanassche@wdc.com>
      Cc: Quinn Tran <quinn.tran@cavium.com>
      Cc: Himanshu Madhani <himanshu.madhani@cavium.com>
      Cc: <stable@vger.kernel.org>
      Acked-by: NHimanshu Madhani <himanshu.madhani@cavium.com>
      Signed-off-by: NMartin K. Petersen <martin.petersen@oracle.com>
      86531887
  15. 31 10月, 2017 1 次提交
  16. 17 10月, 2017 1 次提交