Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2036236pxb; Mon, 12 Apr 2021 12:35:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwtV+mJE8XfYMPZU8sMr6HM5I9mza83KVCaiSDeJtaQCT86nr5j7Woai51ab5+pE7Q1wrVK X-Received: by 2002:aa7:c451:: with SMTP id n17mr30806698edr.197.1618256130624; Mon, 12 Apr 2021 12:35:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618256130; cv=none; d=google.com; s=arc-20160816; b=XpLIVfq5aDL1pkYUwCWI0Gq8Oq+YBz6MYreRugAKcfSJPy0/0YnD3wVHKkylz62Fxh yrbCKdafo+l1x3NPRuJKJjvKRFp4MN909zp/pJf9M9jQwrdx+gllVU2gQHesmbRKMol7 lvL0M8FpoeVlkTiV6Ez93ppLtOsnQgjdaY42knndBuC3siy7xBwtWEtNguTmLBe3gDq0 WjR46zoABTox3Gcpz1zIvziM/rF3un/0rgFPltGivYHWDIVPmCF0TGcgo2RjOCA5mPsS 6FCWMH9Dw4Nk+AtOIhPrRC1/ip5vaQj72EGqaqK/K2Cx7DfxtdtdwE3W6jntBbsl9xVX 3oog== 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=ufaSi1rZQAKbyxb7kcD2XgtW7XZl3GaxUzjtuwdKI0g=; b=g9PtC13wiFeS+liMCHuvVpVkhmc40byqqPl49FrnrvmiqDQebCh0ZTeAxWntT+8W4V 1pBpvLrmG7Yq2WqjaWeTuQ+Cf22f/AM4LR1dlB1LCixkems5vB8gnGPyGG/0KNCq0NLV U97DeA+gr2as8cxHPOtpk6Wo4xAJlephIf3yxKCY8vtbK55f63A9HLwlV2I+D/A06luf DYeAyiMx7evQw7ecWVym3cddRm7qJzCcwNZelJf4TDgq0V24pFEe58YeJAzT79y7oiCV 2riDZ8vZILRVctt80s8qe7zTo8wkRpUsAweOrfKHfefcWz9XwvE+NWGSWBkw5gRWq4Wn Lgpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZQvFi33d; 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 r10si6448977ejs.531.2021.04.12.12.35.03; Mon, 12 Apr 2021 12:35:30 -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=ZQvFi33d; 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 S237481AbhDLIpN (ORCPT + 99 others); Mon, 12 Apr 2021 04:45:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:36600 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237642AbhDLIoa (ORCPT ); Mon, 12 Apr 2021 04:44:30 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7473F60241; Mon, 12 Apr 2021 08:44:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1618217053; bh=ie4AqU6PGRCGqkdDrKAMALP35KYc4CX5LNy4K6p6SSo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZQvFi33dssdYIxtRw3sILBJtyCRGE1HDLN/hLNFUAZeMWheWgaZZMMK8h13VjyVsT SEB0T7sVYYrp8PX4K90cYar3zPo6webVg9wW8Mh/I8XYH9fd7H9ITE3QpQzpvXVMX9 oNHlnzh/qEo6+8n3DQfqdRhxCBfwVqlzh6WB7Q44= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kumar Kartikeya Dwivedi , "David S. Miller" Subject: [PATCH 4.19 52/66] net: sched: bump refcount for new action in ACT replace mode Date: Mon, 12 Apr 2021 10:40:58 +0200 Message-Id: <20210412083959.808110959@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210412083958.129944265@linuxfoundation.org> References: <20210412083958.129944265@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: Kumar Kartikeya Dwivedi commit 6855e8213e06efcaf7c02a15e12b1ae64b9a7149 upstream. Currently, action creation using ACT API in replace mode is buggy. When invoking for non-existent action index 42, tc action replace action bpf obj foo.o sec index 42 kernel creates the action, fills up the netlink response, and then just deletes the action after notifying userspace. tc action show action bpf doesn't list the action. This happens due to the following sequence when ovr = 1 (replace mode) is enabled: tcf_idr_check_alloc is used to atomically check and either obtain reference for existing action at index, or reserve the index slot using a dummy entry (ERR_PTR(-EBUSY)). This is necessary as pointers to these actions will be held after dropping the idrinfo lock, so bumping the reference count is necessary as we need to insert the actions, and notify userspace by dumping their attributes. Finally, we drop the reference we took using the tcf_action_put_many call in tcf_action_add. However, for the case where a new action is created due to free index, its refcount remains one. This when paired with the put_many call leads to the kernel setting up the action, notifying userspace of its creation, and then tearing it down. For existing actions, the refcount is still held so they remain unaffected. Fortunately due to rtnl_lock serialization requirement, such an action with refcount == 1 will not be concurrently deleted by anything else, at best CLS API can move its refcount up and down by binding to it after it has been published from tcf_idr_insert_many. Since refcount is atleast one until put_many call, CLS API cannot delete it. Also __tcf_action_put release path already ensures deterministic outcome (either new action will be created or existing action will be reused in case CLS API tries to bind to action concurrently) due to idr lock serialization. We fix this by making refcount of newly created actions as 2 in ACT API replace mode. A relaxed store will suffice as visibility is ensured only after the tcf_idr_insert_many call. Note that in case of creation or overwriting using CLS API only (i.e. bind = 1), overwriting existing action object is not allowed, and any such request is silently ignored (without error). The refcount bump that occurs in tcf_idr_check_alloc call there for existing action will pair with tcf_exts_destroy call made from the owner module for the same action. In case of action creation, there is no existing action, so no tcf_exts_destroy callback happens. This means no code changes for CLS API. Fixes: cae422f379f3 ("net: sched: use reference counting action init") Signed-off-by: Kumar Kartikeya Dwivedi Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/sched/act_api.c | 3 +++ 1 file changed, 3 insertions(+) --- a/net/sched/act_api.c +++ b/net/sched/act_api.c @@ -900,6 +900,9 @@ struct tc_action *tcf_action_init_1(stru return ERR_PTR(-EINVAL); } + if (!bind && ovr && err == ACT_P_CREATED) + refcount_set(&a->tcfa_refcnt, 2); + return a; err_mod: