From patchwork Sat May 20 00:26:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Grzegorz Uriasz X-Patchwork-Id: 13249072 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 864EFC77B7F for ; Sat, 20 May 2023 00:28:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230001AbjETA2W (ORCPT ); Fri, 19 May 2023 20:28:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229436AbjETA2V (ORCPT ); Fri, 19 May 2023 20:28:21 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16E19107; Fri, 19 May 2023 17:28:19 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-510d92184faso6024956a12.1; Fri, 19 May 2023 17:28:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684542497; x=1687134497; h=subject:from:cc:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=FIdg/B+Ax41yv5HfdnGlZ+teeEBZ6tUjD7i0GnpAY4U=; b=WgUm/5L9cwRzFTBmmYflolTP+R3KTNl4Eoq+BFlc6V74sIFMY2OuziyuB9p96+BbXR IyRAvVqTMZaFzfAWL/z74XYpvTulqRIuKJswyl2zPtitpSfwpY6Tmg1dcVvIRsMobpvU x5Oj5HqJ449clNmGAf6P07gfH03pxVSFbXxDKWgoUTp4A3k1vjp16erFaqdWLvVECVI1 YTQ9S3H19EuG0DWwiinXMZiqPM50osuQZuPAfOKyJAMWpRer5QhHr6OtsVAgehuPXzvj NOEoz0xi5FlcdT/UNA6nMe7XSM9wR+KRM1C9/8dYrYDDicCnYis0yltYsb0Xvd3Mjhev 0biw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684542497; x=1687134497; h=subject:from:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FIdg/B+Ax41yv5HfdnGlZ+teeEBZ6tUjD7i0GnpAY4U=; b=VgasD1dCnNY6Gf8OeN0IQ8RM2QuwUwwSNFP3WHTODd1YsYzvbsR+6LG5yQ8OCqWp5b QLOddcDB6UgGIrqmpIcWRa1SlH5W3zsuV9SnTjzQpD0sUvR9GD8F/Xr369vblnxmEY6Z nGJE3RI1luX6h4smQVpz9nxVlFJx+CyhD/79BAowRHZ3fip8tLM8H+dv2ftYyW1b04LL DERrg1J6rf53WgACDWHlCiHp+XVyjcpelkOfIcApD79kROX4LRqwNyJ4T472IUtom+j9 HAjCs2ULIcEoBATgXL9Xbh+DkMEbPnzXTUhTOTUyedflfQ13tSxFBInM10nhNQzgyR/n pE7Q== X-Gm-Message-State: AC+VfDx3aRj3vrOWNF2XfJM5V2Y5rjE7qu3wsvcSEtWCi99BGt8r1aER WjQzUP3e+ktG/u426MwVo6w= X-Google-Smtp-Source: ACHHUZ4Lm37O5xH+xU4N67k5ZsuvCi3ElCxEeF7tF9yJK1Qi6uQJvprAVOYSiZGIQdsm/g9jlX4d0w== X-Received: by 2002:a17:906:3051:b0:94a:845c:3528 with SMTP id d17-20020a170906305100b0094a845c3528mr2851817ejd.45.1684542497169; Fri, 19 May 2023 17:28:17 -0700 (PDT) Received: from [192.168.88.92] (95-158-68-28.static.chello.pl. [95.158.68.28]) by smtp.gmail.com with ESMTPSA id de28-20020a1709069bdc00b0096f70609406sm213691ejc.4.2023.05.19.17.28.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 19 May 2023 17:28:16 -0700 (PDT) Message-ID: <5f637569-36af-a8d0-e378-b27a63f08501@gmail.com> Date: Sat, 20 May 2023 02:26:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Content-Language: en-US To: "Martin K. Petersen" , linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: dutkahugo@gmail.com From: Grzegorz Uriasz Subject: [PATCH] scsi: target: Fix data corruption under concurrent target configuration Precedence: bulk List-ID: X-Mailing-List: target-devel@vger.kernel.org This fixes data corruptions arising from concurrent enabling of a target devices. When multiple enable calls are made concurrently then it is possible for the target device to be set up twice which results in a kernel BUG. Introduces a per target device mutex for serializing enable requests. Signed-off-by: Grzegorz Uriasz ---  drivers/target/target_core_device.c | 17 +++++++++++++----  include/target/target_core_base.h   |  1 +  2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/drivers/target/target_core_device.c b/drivers/target/target_core_device.c index 90f3f4926172..6d8fb962c780 100644 --- a/drivers/target/target_core_device.c +++ b/drivers/target/target_core_device.c @@ -742,6 +742,7 @@ struct se_device *target_alloc_device(struct se_hba *hba, const char *name)      INIT_WORK(&dev->delayed_cmd_work, target_do_delayed_work);      mutex_init(&dev->lun_reset_mutex); +    mutex_init(&dev->configure_mutex);      dev->t10_wwn.t10_dev = dev;      /* @@ -904,10 +905,15 @@ int target_configure_device(struct se_device *dev)      struct se_hba *hba = dev->se_hba;      int ret, id; +    ret = mutex_lock_interruptible(&dev->configure_mutex); +    if (ret) +        return ret; +      if (target_dev_configured(dev)) {          pr_err("se_dev->se_dev_ptr already set for storage"                  " object\n"); -        return -EEXIST; +        ret = -EEXIST; +        goto out_release_mutex;      }      /* @@ -923,7 +929,7 @@ int target_configure_device(struct se_device *dev)      mutex_unlock(&device_mutex);      if (id < 0) {          ret = -ENOMEM; -        goto out; +        goto out_release_vpd;      }      dev->dev_index = id; @@ -969,7 +975,8 @@ int target_configure_device(struct se_device *dev)      dev->dev_flags |= DF_CONFIGURED; -    return 0; +    ret = 0; +    goto out_release_mutex;  out_destroy_device:      dev->transport->destroy_device(dev); @@ -977,8 +984,10 @@ int target_configure_device(struct se_device *dev)      mutex_lock(&device_mutex);      idr_remove(&devices_idr, dev->dev_index);      mutex_unlock(&device_mutex); -out: +out_release_vpd:      se_release_vpd_for_dev(dev); +out_release_mutex: +    mutex_unlock(&dev->configure_mutex);      return ret;  } diff --git a/include/target/target_core_base.h b/include/target/target_core_base.h index 5f8e96f1516f..b3f9bd641688 100644 --- a/include/target/target_core_base.h +++ b/include/target/target_core_base.h @@ -869,6 +869,7 @@ struct se_device {      int            queue_cnt;      struct se_device_queue    *queues;      struct mutex        lun_reset_mutex; +    struct mutex        configure_mutex;  };  struct target_opcode_descriptor {