Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1933575imm; Wed, 16 May 2018 05:26:34 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpSCxCWGbj6EgwedMTz9SDHA86o6wbHG0h47uL4Gvh9TZo9aUoWU5A8coGWDtIxDq77xLaP X-Received: by 2002:a63:b108:: with SMTP id r8-v6mr596362pgf.438.1526473594092; Wed, 16 May 2018 05:26:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526473594; cv=none; d=google.com; s=arc-20160816; b=eqQNk93AQL/Zwfr1phuw0RXg5yIOFugsDg3wYy/y2hvfvkK1Gf+uwfJeidfwvhL0lG BSIgTYwxeP6RifTev4xcKtr3xhKpG/JjBTYt1Hm5+WfSIWQWv1/9xopv1uhMa6Nxp3ox ty29NktHiHIQ2RhrnB4vl+U4osuVttbuQezSjGMDNksCm+dKinvENkC+87UjTtoWyEwY rhssjqtxUO49FcWti35ADCd975xmFYXe8qdtzvH5ACVFNzQdcyfB+EOg3aYGXgosSusN r4p/YGuCN/2sqmaWYGjvP8t14LTMNGKjfowGT7eXWRvlcqS8S4ZvV2B7WSCLXf8126Z1 pRoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=2Mc1NmVvmSlZnDKYzKvR2RqtBZdjm1jJSBZ462AXd1Q=; b=VPol2l4s0CEDoNdpYWjatxkBOKnzrFJLjkNO6nefZo9lD18SGBFlV7yiNtrwTqXsyM qWZlBXm3Fdwm2gqT3E7zoJx9Z7lXx22aRraTfbyMpek9QXmrm3PtB6Pa5r0u+P/ozBP+ xj6DCcgKPZCyQ4OxuBPk12f1B44iPoH7NIC4rmZF4ohFiruEe6kKK4At7ojMIDU+g8NG ya3+gAYWaNr7KW1x/AofL+q8AYNDk2rab2sy7CQtKc3bUkbulh9go5YUWsnENlyNOSLF lmMt9+Ie8my5+DKtty+7zDu01h0NZG7Aj9kGNv7MvXiQwWgpbvlriIx6Pvr4FNIWXQ3x TmYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@resnulli-us.20150623.gappssmtp.com header.s=20150623 header.b=qkcmhsIz; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h8-v6si2561333pfh.278.2018.05.16.05.26.20; Wed, 16 May 2018 05:26:34 -0700 (PDT) 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=@resnulli-us.20150623.gappssmtp.com header.s=20150623 header.b=qkcmhsIz; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752544AbeEPM0G (ORCPT + 99 others); Wed, 16 May 2018 08:26:06 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36294 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752443AbeEPM0C (ORCPT ); Wed, 16 May 2018 08:26:02 -0400 Received: by mail-wm0-f65.google.com with SMTP id n10-v6so1128844wmc.1 for ; Wed, 16 May 2018 05:26:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=2Mc1NmVvmSlZnDKYzKvR2RqtBZdjm1jJSBZ462AXd1Q=; b=qkcmhsIzcy9J2vXBbo2etAyMzEIPWlw+J3uR1Y6uL55+F0tesZ6uuTGRbID2JNeIey G2ldJ0L/4nIdtvTScqHim+jfrIHbDuOGDs8kuV4ooLp95i5wa6sNm9Z/6qKQsXBSlSsM mxazYgqRIupe7shDwR8cZ+dm4P4Cw43tim1O1jJU0kyYLjhEW3TyokWy3JHw25CSiiZq QteAAX+V99Hx3MNE8wtEG7nD/OuHqMzKbRsIVyH1x//JDnvfXX8pfZkyFCzj+g9eSt4g AB7MTZ0Vks5x7dhByu/6+MoZwS6lDLCSzOekmNr/i+XR1D5yqCOryd89ZeLYzJ1daG3x 8n5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=2Mc1NmVvmSlZnDKYzKvR2RqtBZdjm1jJSBZ462AXd1Q=; b=Uk+e9WGQbC/FJaCKm8q9LUlYkyAe4f4uuNfUifx65mGR/W16VXtyc1uedP51wjQfpN d8z84TOFB2zf4lhiPJjUU4DTo6UGhc/3rW3544qztqNoGK+xFAl3gl/bNR9NePN3yMYL 9tvX870MgojZBah8XlnyMAmYCixHPFOaaJHTk78cg6UCRCrqWTFZ6tbNomL90OjVj7ME 8hmj7HYEZdrQ2TYPC4h9jufuVtQ2PaiXNM7G0zbtOD8rdGzlitCwH55m8S1zOh7x7msU zGm7Le2qd4AUigp9WN1e8rtGgdMXe7wMafdO8NSRyZ7UOHZN5b0yMRrJ9vbdonWrRyuS Hf0g== X-Gm-Message-State: ALKqPwf9AIlwkM0FUHfMG5//zBt5VwEkTfHqetkFlSvsTrCi5JazZoMB EaXnhLnLTaU6ucbxXWrshGaTiw== X-Received: by 2002:a1c:e182:: with SMTP id y124-v6mr496190wmg.57.1526473560989; Wed, 16 May 2018 05:26:00 -0700 (PDT) Received: from localhost (ip-94-113-127-8.net.upcbroadband.cz. [94.113.127.8]) by smtp.gmail.com with ESMTPSA id q2-v6sm2583354wrj.57.2018.05.16.05.26.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 16 May 2018 05:26:00 -0700 (PDT) Date: Wed, 16 May 2018 14:26:00 +0200 From: Jiri Pirko To: Vlad Buslov Cc: netdev@vger.kernel.org, davem@davemloft.net, jhs@mojatatu.com, xiyou.wangcong@gmail.com, pablo@netfilter.org, kadlec@blackhole.kfki.hu, fw@strlen.de, ast@kernel.org, daniel@iogearbox.net, edumazet@google.com, keescook@chromium.org, linux-kernel@vger.kernel.org, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, kliteyn@mellanox.com Subject: Re: [PATCH 12/14] net: sched: retry action check-insert on concurrent modification Message-ID: <20180516122600.GM1972@nanopsycho> References: <1526308035-12484-1-git-send-email-vladbu@mellanox.com> <1526308035-12484-13-git-send-email-vladbu@mellanox.com> <20180516095953.GI1972@nanopsycho> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Wed, May 16, 2018 at 01:55:06PM CEST, vladbu@mellanox.com wrote: > >On Wed 16 May 2018 at 09:59, Jiri Pirko wrote: >> Mon, May 14, 2018 at 04:27:13PM CEST, vladbu@mellanox.com wrote: >>>Retry check-insert sequence in action init functions if action with same >>>index was inserted concurrently. >>> >>>Signed-off-by: Vlad Buslov >>>--- >>> net/sched/act_bpf.c | 8 +++++++- >>> net/sched/act_connmark.c | 8 +++++++- >>> net/sched/act_csum.c | 8 +++++++- >>> net/sched/act_gact.c | 8 +++++++- >>> net/sched/act_ife.c | 8 +++++++- >>> net/sched/act_ipt.c | 8 +++++++- >>> net/sched/act_mirred.c | 8 +++++++- >>> net/sched/act_nat.c | 8 +++++++- >>> net/sched/act_pedit.c | 8 +++++++- >>> net/sched/act_police.c | 9 ++++++++- >>> net/sched/act_sample.c | 8 +++++++- >>> net/sched/act_simple.c | 9 ++++++++- >>> net/sched/act_skbedit.c | 8 +++++++- >>> net/sched/act_skbmod.c | 8 +++++++- >>> net/sched/act_tunnel_key.c | 9 ++++++++- >>> net/sched/act_vlan.c | 9 ++++++++- >>> 16 files changed, 116 insertions(+), 16 deletions(-) >>> >>>diff --git a/net/sched/act_bpf.c b/net/sched/act_bpf.c >>>index 5554bf7..7e20fdc 100644 >>>--- a/net/sched/act_bpf.c >>>+++ b/net/sched/act_bpf.c >>>@@ -299,10 +299,16 @@ static int tcf_bpf_init(struct net *net, struct nlattr *nla, >>> >>> parm = nla_data(tb[TCA_ACT_BPF_PARMS]); >>> >>>+replay: >>> if (!tcf_idr_check(tn, parm->index, act, bind)) { >>> ret = tcf_idr_create(tn, parm->index, est, act, >>> &act_bpf_ops, bind, true); >>>- if (ret < 0) >>>+ /* Action with specified index was created concurrently. >>>+ * Check again. >>>+ */ >>>+ if (parm->index && ret == -ENOSPC) >>>+ goto replay; >>>+ else if (ret) >> >> Hmm, looks like you are doing the same/very similar thing in every act >> code. I think it would make sense to introduce a helper function for >> this purpose. > >This code uses goto so it can't be easily refactored into standalone >function. Could you specify which part of this code you suggest to >extract? Hmm, looking at the code, I think that what would help is to have a helper that would atomically check if index exists and if not, it would allocate one. Something like: int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index, struct tc_action **a, int bind) { struct tcf_idrinfo *idrinfo = tn->idrinfo; struct tc_action *p; int err; spin_lock(&idrinfo->lock); if (*index) { p = idr_find(&idrinfo->action_idr, *index); if (p) { if (bind) p->tcfa_bindcnt++; p->tcfa_refcnt++; *a = p; err = 0; } else { *a = NULL; err = idr_alloc_u32(idr, NULL, index, *index, GFP_ATOMIC); } } else { *index = 1; *a = NULL; err = idr_alloc_u32(idr, NULL, index, UINT_MAX, GFP_ATOMIC); } spin_unlock(&idrinfo->lock); return err; } The act code would just check if "a" is NULL and if so, it would call tcf_idr_create() with allocated index as arg. > >> >> [...] >