Received: by 10.223.176.5 with SMTP id f5csp947840wra; Sat, 3 Feb 2018 14:11:28 -0800 (PST) X-Google-Smtp-Source: AH8x226LoJk2kk+9gV7O0CKf0rN7vlYV4aKn1iGdSErgdMKk7i8y/KtkrhnBfKDbofpUC85ylAIu X-Received: by 2002:a17:902:bc85:: with SMTP id bb5-v6mr13094928plb.425.1517695888634; Sat, 03 Feb 2018 14:11:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517695888; cv=none; d=google.com; s=arc-20160816; b=YWYpbNrgW2sRF5za0zf1EaUGct0TG8SYlP9F+wnopZzV1CloDhllYLhNdketEF1ddT +rtte3lW7UFjwsw03KnY86Bk03iI4K2hO6xYEbBZ+qs4F3lQUgK/UizTP0o5fRHU0ra4 5fI9xaZWdUGwVpVjh6LtykcRAvp/zrzPlXOwn5UsGz0U1dsXvuoXqBSNU/zOjhOYPxHP GhhF6ug0+Q0ib2+uKbZNglAH6P83QaLVEGGlu40HwMLQ8FJpqNWsQXKdiNpNLLp4cxY8 A15TGQ4XYHbpUDT4wZlWSmp27KvLJY5JVqnpVzJn58FGuuQfLUkAbtEoY4ViPlz7wWlg p4dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=CY3M4P5pq51n1e+kfF0hFpo1e2SL25aehtTcUe9GYmk=; b=Dumi5PzYkO6CpEWt1lBbJpCPilK0cWLusRAyuk1nR35wJtJwKVTWtQ/tG2k54YzpXl 6eYyxgmBSjyTgRkQx+Ue/XWLnm0y6SVDZti1wY5DFiXyxFpf8OzTHCAJRimseo22ENyV MNe69F3RvU5ZjkLtkKX1LXEuEkMosM7FnvrfUXdUQy2hnDaoDKPh67um+Lc//JIluPw/ q0oi3jyPLQzBgUIbqFg/qsCyqD5vLBm5RiheO72Ec+/gm1nNrmmqJio2/TjIByAvPzb7 POVISagB6pHeZscqFa0yqyZHkm6MM51eVPBn02cV49FMoS9sho37u1dIvlItmMMqaeD7 emqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=EOcEzQji; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g18-v6si2636498plj.754.2018.02.03.14.11.14; Sat, 03 Feb 2018 14:11:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=EOcEzQji; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755345AbeBCSpo (ORCPT + 99 others); Sat, 3 Feb 2018 13:45:44 -0500 Received: from mail-bl2nam02on0127.outbound.protection.outlook.com ([104.47.38.127]:37417 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753155AbeBCSCx (ORCPT ); Sat, 3 Feb 2018 13:02:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=CY3M4P5pq51n1e+kfF0hFpo1e2SL25aehtTcUe9GYmk=; b=EOcEzQjiwVfaAm6O7HD969MgMpQcz4/kLwXxYoJWl7QVw4s84E3xpVZ3jBJr9RonJsoojbdHYrmr86q9+B/lw7mydbh59MrDzlSk1Np7n5T1Caa50avG3wUhviU27Wv2XputLBey3a8b8QLQ9mEMpS6ViJju2qicTAfJoNAg0xw= Received: from BL0PR2101MB1027.namprd21.prod.outlook.com (52.132.20.161) by BL0PR2101MB1041.namprd21.prod.outlook.com (52.132.23.161) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.506.3; Sat, 3 Feb 2018 18:02:12 +0000 Received: from BL0PR2101MB1027.namprd21.prod.outlook.com ([fe80::a8da:b5d9:d710:9bf9]) by BL0PR2101MB1027.namprd21.prod.outlook.com ([fe80::a8da:b5d9:d710:9bf9%3]) with mapi id 15.20.0485.006; Sat, 3 Feb 2018 18:02:12 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Leon Romanovsky , Jason Gunthorpe , Sasha Levin Subject: [PATCH AUTOSEL for 4.14 081/110] RDMA/netlink: Fix locking around __ib_get_device_by_index Thread-Topic: [PATCH AUTOSEL for 4.14 081/110] RDMA/netlink: Fix locking around __ib_get_device_by_index Thread-Index: AQHTnRj/30vh9vLqMkqOZXWWcfqCtg== Date: Sat, 3 Feb 2018 18:01:20 +0000 Message-ID: <20180203180015.29073-81-alexander.levin@microsoft.com> References: <20180203180015.29073-1-alexander.levin@microsoft.com> In-Reply-To: <20180203180015.29073-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;BL0PR2101MB1041;7:xVPA14qtMtT0jXxBdBdy2fyManOLZDTYhdkVYpecbqpPPKNPbjD+hiwcA+f+1L0kqPZcNWAILkF56QdWypXRWpE58nay8UFaJKxZHz+HKu/9clGTNH/Ho1xIGOgUc/a2zE6gV4IvMYGdhiqaEKtaTIAaXbXTqtCoGPrbdX8RT/jhKtiWH54tU+H5cq1DNj21dn3KqauOsDcHAJBxnzyus9RPVeTGJucDnAKw5Bu56891WUaZc4PHnvlaE4lE5If7 x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: b0afdb45-eea4-4ea6-ccd2-08d56b304014 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(4534165)(4627221)(201703031133081)(201702281549075)(5600026)(4604075)(3008032)(2017052603307)(7193020);SRVR:BL0PR2101MB1041; x-ms-traffictypediagnostic: BL0PR2101MB1041: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(61425038)(6040501)(2401047)(5005006)(8121501046)(3231101)(2400082)(944501161)(93006095)(93001095)(3002001)(10201501046)(6055026)(61426038)(61427038)(6041288)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123558120)(20161123562045)(20161123564045)(6072148)(201708071742011);SRVR:BL0PR2101MB1041;BCL:0;PCL:0;RULEID:;SRVR:BL0PR2101MB1041; x-forefront-prvs: 05724A8921 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39380400002)(396003)(366004)(39860400002)(376002)(346002)(189003)(199004)(2501003)(305945005)(3660700001)(5250100002)(81156014)(99286004)(81166006)(36756003)(7736002)(10290500003)(106356001)(53936002)(5660300001)(316002)(22452003)(8936002)(54906003)(110136005)(3280700002)(478600001)(72206003)(4326008)(25786009)(107886003)(102836004)(97736004)(76176011)(59450400001)(6666003)(66066001)(6506007)(2950100002)(2900100001)(68736007)(8676002)(6116002)(3846002)(186003)(1076002)(6346003)(105586002)(26005)(10090500001)(2906002)(6512007)(86612001)(14454004)(575784001)(86362001)(6436002)(6486002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:BL0PR2101MB1041;H:BL0PR2101MB1027.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-message-info: k5ic3DXvhH5g1vkEEp+YWKLseqbIQ571n3+bRPlieXCGX8tPmgqOmqBJOrs3zg/b/DIv5dFo0rRJ9Fx0AI4NMQ== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: b0afdb45-eea4-4ea6-ccd2-08d56b304014 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Feb 2018 18:01:20.9722 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR2101MB1041 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Leon Romanovsky [ Upstream commit f8978bd95cf92f869f3d9b34c1b699f49253b8c6 ] Holding locks is mandatory when calling __ib_device_get_by_index, otherwise there are races during the list iteration with device removal. Since the locks are static to device.c, __ib_device_get_by_index can never be called correctly by any user out side the file. Make the function static and provide a safe function that gets the correct locks and returns a kref'd pointer. Fix all callers. Fixes: e5c9469efcb1 ("RDMA/netlink: Add nldev device doit implementation") Fixes: c3f66f7b0052 ("RDMA/netlink: Implement nldev port doit callback") Fixes: 7d02f605f0dc ("RDMA/netlink: Add nldev port dumpit implementation") Reviewed-by: Mark Bloch Signed-off-by: Leon Romanovsky Signed-off-by: Jason Gunthorpe Signed-off-by: Sasha Levin --- drivers/infiniband/core/core_priv.h | 2 +- drivers/infiniband/core/device.c | 18 ++++++++++++- drivers/infiniband/core/nldev.c | 54 ++++++++++++++++++++++++---------= ---- 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/drivers/infiniband/core/core_priv.h b/drivers/infiniband/core/= core_priv.h index a1d687a664f8..66f0268f37a6 100644 --- a/drivers/infiniband/core/core_priv.h +++ b/drivers/infiniband/core/core_priv.h @@ -314,7 +314,7 @@ static inline int ib_mad_enforce_security(struct ib_mad= _agent_private *map, } #endif =20 -struct ib_device *__ib_device_get_by_index(u32 ifindex); +struct ib_device *ib_device_get_by_index(u32 ifindex); /* RDMA device netlink */ void nldev_init(void); void nldev_exit(void); diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/dev= ice.c index 84fc32a2c8b3..f65f64cb8455 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -134,7 +134,7 @@ static int ib_device_check_mandatory(struct ib_device *= device) return 0; } =20 -struct ib_device *__ib_device_get_by_index(u32 index) +static struct ib_device *__ib_device_get_by_index(u32 index) { struct ib_device *device; =20 @@ -145,6 +145,22 @@ struct ib_device *__ib_device_get_by_index(u32 index) return NULL; } =20 +/* + * Caller is responsible to return refrerence count by calling put_device(= ) + */ +struct ib_device *ib_device_get_by_index(u32 index) +{ + struct ib_device *device; + + down_read(&lists_rwsem); + device =3D __ib_device_get_by_index(index); + if (device) + get_device(&device->dev); + + up_read(&lists_rwsem); + return device; +} + static struct ib_device *__ib_device_get_by_name(const char *name) { struct ib_device *device; diff --git a/drivers/infiniband/core/nldev.c b/drivers/infiniband/core/nlde= v.c index 2fae850a3eff..4fbb9681f5cf 100644 --- a/drivers/infiniband/core/nldev.c +++ b/drivers/infiniband/core/nldev.c @@ -142,27 +142,34 @@ static int nldev_get_doit(struct sk_buff *skb, struct= nlmsghdr *nlh, =20 index =3D nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); =20 - device =3D __ib_device_get_by_index(index); + device =3D ib_device_get_by_index(index); if (!device) return -EINVAL; =20 msg =3D nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); - if (!msg) - return -ENOMEM; + if (!msg) { + err =3D -ENOMEM; + goto err; + } =20 nlh =3D nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET), 0, 0); =20 err =3D fill_dev_info(msg, device); - if (err) { - nlmsg_free(msg); - return err; - } + if (err) + goto err_free; =20 nlmsg_end(msg, nlh); =20 + put_device(&device->dev); return rdma_nl_unicast(msg, NETLINK_CB(skb).portid); + +err_free: + nlmsg_free(msg); +err: + put_device(&device->dev); + return err; } =20 static int _nldev_get_dumpit(struct ib_device *device, @@ -220,31 +227,40 @@ static int nldev_port_get_doit(struct sk_buff *skb, s= truct nlmsghdr *nlh, return -EINVAL; =20 index =3D nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); - device =3D __ib_device_get_by_index(index); + device =3D ib_device_get_by_index(index); if (!device) return -EINVAL; =20 port =3D nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); - if (!rdma_is_port_valid(device, port)) - return -EINVAL; + if (!rdma_is_port_valid(device, port)) { + err =3D -EINVAL; + goto err; + } =20 msg =3D nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); - if (!msg) - return -ENOMEM; + if (!msg) { + err =3D -ENOMEM; + goto err; + } =20 nlh =3D nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET), 0, 0); =20 err =3D fill_port_info(msg, device, port); - if (err) { - nlmsg_free(msg); - return err; - } + if (err) + goto err_free; =20 nlmsg_end(msg, nlh); + put_device(&device->dev); =20 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid); + +err_free: + nlmsg_free(msg); +err: + put_device(&device->dev); + return err; } =20 static int nldev_port_get_dumpit(struct sk_buff *skb, @@ -265,7 +281,7 @@ static int nldev_port_get_dumpit(struct sk_buff *skb, return -EINVAL; =20 ifindex =3D nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); - device =3D __ib_device_get_by_index(ifindex); + device =3D ib_device_get_by_index(ifindex); if (!device) return -EINVAL; =20 @@ -299,7 +315,9 @@ static int nldev_port_get_dumpit(struct sk_buff *skb, nlmsg_end(skb, nlh); } =20 -out: cb->args[0] =3D idx; +out: + put_device(&device->dev); + cb->args[0] =3D idx; return skb->len; } =20 --=20 2.11.0