Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1534375pxb; Thu, 16 Sep 2021 09:24:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrFSat2Frl2md+0yzONNNSzOCdNUkNywNRnDPYvV6fDtXEqegmHlhihLbDHeJ0gNbUI4ub X-Received: by 2002:a17:906:318c:: with SMTP id 12mr7443967ejy.28.1631809450441; Thu, 16 Sep 2021 09:24:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631809450; cv=none; d=google.com; s=arc-20160816; b=Qpu5KuFV+tBOTlsw3B/VA3U8ZPiKCUriocRO/QWxKQOJxdF/AtfBonVuSMUSjPD9lJ 1cyMTouSPGc/JJWWWwmSz2QVXfPfukAseNestiQobFzb542VLhN70E/Qp3WBADANhiLx fQ6TR3gitp8k7Xk7bMcxqrOeWQct0MoGwKEReiOzOy3S7bQvnqxAQcQ+hCg/2xvbOEss qfxH8ZZrUfWWBwovOjJC5MVTs9ru85IKHI+mtQp9D/tR9FS1bLu4TkHYCnbgeZGTJ4EI u+fpW0YTCqmpzzJT9i0ZO8KAY11IOaefhi+jpxNT3ufoOcK2SZlhKi55Gv+t+RhIzH/3 AA8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=GIDOFgH1PRWCW+dzA2FhRzcPAO0iNc1Psv5YMp88u7g=; b=EKfEfwBr6l8ZUst6otRRs37J3RbMr/qFCzGjxuAtF6AxHvQfnVA2iCtUzr3HS9D2D+ PQtTLDmKMBZt7KV3gUZX1s5POW8D8NHn95y0yCLhJChvNawBe6mfI6MYyPEA3Ur3oKg4 3BTP9aaxCutElHG6ygBZaMW5j2iOwfuLu111kiL0oY3G7BF3Cekvp425XrjTXGTs4XCp PtKBgAozSOkf7sDX2m+awvmAQv/+oWJWjs/8393KsM85CdHSdniDlPogn5E7ObifQW7v mJJXSgTy877ZovNTTu1TIySSdjhIoVlNRvY3ttBYODNH0x/L+tctZipC1//W7Q7tmVES IWUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ezzpT+Cp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 9si3981703ejo.527.2021.09.16.09.23.42; Thu, 16 Sep 2021 09:24:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ezzpT+Cp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232910AbhIPQWs (ORCPT + 99 others); Thu, 16 Sep 2021 12:22:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:55070 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240862AbhIPQOd (ORCPT ); Thu, 16 Sep 2021 12:14:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 893B161246; Thu, 16 Sep 2021 16:10:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631808640; bh=8e0DDvsPXf1kH0LnFdERMGCtpGzYk7PCmA9MgBhGK6g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ezzpT+CpYcb0jGOUs+aX6iEVZFrg0nqNkd+SL2Z+EhNGO6IkY5k//KcaBmosjCJfu RrpScqW3ow4/IzF5SW8ZdEAHVLdnpUp6S1XFNETWIQpRcn4UVBisdQCqgIHiFPzQez 9uEDyqN5yu9wWSH1PsRglJlArFc3rM2yGWoWb39Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Martynas Pumputis , Andrii Nakryiko , Sasha Levin Subject: [PATCH 5.10 167/306] libbpf: Fix race when pinning maps in parallel Date: Thu, 16 Sep 2021 17:58:32 +0200 Message-Id: <20210916155759.774620841@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155753.903069397@linuxfoundation.org> References: <20210916155753.903069397@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Martynas Pumputis [ Upstream commit 043c5bb3c4f43670ab4fea0b847373ab42d25f3e ] When loading in parallel multiple programs which use the same to-be pinned map, it is possible that two instances of the loader will call bpf_object__create_maps() at the same time. If the map doesn't exist when both instances call bpf_object__reuse_map(), then one of the instances will fail with EEXIST when calling bpf_map__pin(). Fix the race by retrying reusing a map if bpf_map__pin() returns EEXIST. The fix is similar to the one in iproute2: e4c4685fd6e4 ("bpf: Fix race condition with map pinning"). Before retrying the pinning, we don't do any special cleaning of an internal map state. The closer code inspection revealed that it's not required: - bpf_object__create_map(): map->inner_map is destroyed after a successful call, map->fd is closed if pinning fails. - bpf_object__populate_internal_map(): created map elements is destroyed upon close(map->fd). - init_map_slots(): slots are freed after their initialization. Signed-off-by: Martynas Pumputis Signed-off-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20210726152001.34845-1-m@lambda.lt Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 0dad862b3b9d..b337d6f29098 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -4284,10 +4284,13 @@ bpf_object__create_maps(struct bpf_object *obj) char *cp, errmsg[STRERR_BUFSIZE]; unsigned int i, j; int err; + bool retried; for (i = 0; i < obj->nr_maps; i++) { map = &obj->maps[i]; + retried = false; +retry: if (map->pin_path) { err = bpf_object__reuse_map(map); if (err) { @@ -4295,6 +4298,12 @@ bpf_object__create_maps(struct bpf_object *obj) map->name); goto err_out; } + if (retried && map->fd < 0) { + pr_warn("map '%s': cannot find pinned map\n", + map->name); + err = -ENOENT; + goto err_out; + } } if (map->fd >= 0) { @@ -4328,9 +4337,13 @@ bpf_object__create_maps(struct bpf_object *obj) if (map->pin_path && !map->pinned) { err = bpf_map__pin(map, NULL); if (err) { + zclose(map->fd); + if (!retried && err == -EEXIST) { + retried = true; + goto retry; + } pr_warn("map '%s': failed to auto-pin at '%s': %d\n", map->name, map->pin_path, err); - zclose(map->fd); goto err_out; } } -- 2.30.2