From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,T_DKIMWL_WL_MED,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 516F7C072B1 for ; Tue, 28 May 2019 18:29:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 29F9B20B7C for ; Tue, 28 May 2019 18:29:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bsnCWvyd" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728157AbfE1S3y (ORCPT ); Tue, 28 May 2019 14:29:54 -0400 Received: from mail-oi1-f202.google.com ([209.85.167.202]:42018 "EHLO mail-oi1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728014AbfE1S3w (ORCPT ); Tue, 28 May 2019 14:29:52 -0400 Received: by mail-oi1-f202.google.com with SMTP id y14so6712257oia.9 for ; Tue, 28 May 2019 11:29:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=lfVPYgVsrgTm5HSRXwm4saqwmib2k5XfcIamVFoAWPg=; b=bsnCWvydPDTWyFLpW3/zbC88qkCf051v0n/sK15ngfL7uZDNj2iVe7fCJM7B6ewdaP z50mWaIWAUekPnO4QKlJ2I+G3LHSKdqBB0oiIb4MLGeyFKiG3NAYIgJsejRcuwAbspdK 9L3wEP7/9s0/rUVT13ACQaB/se2/phN+8HhdDccfnCNc8XNWf69kZDHu7Ou+9pVdGJrW oFNacl/jFcAJbMveDbJnxCJYQBO+EQUAQx3XwcYiJ+6ces6fhD93X2/wDwqCviyzaFQr YCayewCEXA96LjxdECJIhUsKgWVCsMeu9ervpqlWNJh+TuAg8X9b05NlJE1/LsY4jtIC vjaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=lfVPYgVsrgTm5HSRXwm4saqwmib2k5XfcIamVFoAWPg=; b=CJ6RglF0I5kLkQ7j1ZI2NZM+dCHZrgOItNnA7y0KhAinMZgYCfFzlPwJHIIMTqUARc ZOBCF8yiX+yhGejKCvbecCx7NvfjEtvvftBQpYGPrx+t0/NdyLFCkbNb0dtEonWHPiS6 A0HR1RomZYDRttYCbtRAXQaUc1Afv5H3X/Vnu2S2s7JmY+Ict5M4BBqcKvy4pkg6h8L7 FM+++D0xCfPiRB+yQYgdVbnTzwCLLxKPM9O/FxfJUBm684jwqrn4Vb0eCMkf5S2Vl3xq pWaN065mnZAE4RZMLcgFAVHbRc8I8zjfXFywme3E413iAg7zMCEF05Ew16eY7I7NHEss ZN8A== X-Gm-Message-State: APjAAAU9waMwcFscmALo8mw/m+lkvS84HL1jNuFcsB+++z7c7Q7j+9pv kd7h09JFRBDFxIjjOSlFhN7CO7M= X-Google-Smtp-Source: APXvYqybmb7CtYZcZ7uZMCRDD2BKcgJ5fu7zv/g35JgmErtJ6JzPhNaPtkZd+sUPQbGgHy/Z5hBPC1k= X-Received: by 2002:a9d:661:: with SMTP id 88mr35748119otn.214.1559068191802; Tue, 28 May 2019 11:29:51 -0700 (PDT) Date: Tue, 28 May 2019 11:29:44 -0700 In-Reply-To: <20190528182946.3633-1-sdf@google.com> Message-Id: <20190528182946.3633-2-sdf@google.com> Mime-Version: 1.0 References: <20190528182946.3633-1-sdf@google.com> X-Mailer: git-send-email 2.22.0.rc1.257.g3120a18244-goog Subject: [PATCH bpf-next v3 2/4] bpf: media: properly use bpf_prog_array api From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev , linux-media@vger.kernel.org, Mauro Carvalho Chehab , Sean Young Content-Type: text/plain; charset="UTF-8" Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Now that we don't have __rcu markers on the bpf_prog_array helpers, let's use proper rcu_dereference_protected to obtain array pointer under mutex. Cc: linux-media@vger.kernel.org Cc: Mauro Carvalho Chehab Cc: Sean Young Signed-off-by: Stanislav Fomichev --- drivers/media/rc/bpf-lirc.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/drivers/media/rc/bpf-lirc.c b/drivers/media/rc/bpf-lirc.c index ee657003c1a1..0a0ce620e4a2 100644 --- a/drivers/media/rc/bpf-lirc.c +++ b/drivers/media/rc/bpf-lirc.c @@ -8,6 +8,9 @@ #include #include "rc-core-priv.h" +#define lirc_rcu_dereference(p) \ + rcu_dereference_protected(p, lockdep_is_held(&ir_raw_handler_lock)) + /* * BPF interface for raw IR */ @@ -136,7 +139,7 @@ const struct bpf_verifier_ops lirc_mode2_verifier_ops = { static int lirc_bpf_attach(struct rc_dev *rcdev, struct bpf_prog *prog) { - struct bpf_prog_array __rcu *old_array; + struct bpf_prog_array *old_array; struct bpf_prog_array *new_array; struct ir_raw_event_ctrl *raw; int ret; @@ -154,12 +157,12 @@ static int lirc_bpf_attach(struct rc_dev *rcdev, struct bpf_prog *prog) goto unlock; } - if (raw->progs && bpf_prog_array_length(raw->progs) >= BPF_MAX_PROGS) { + old_array = lirc_rcu_dereference(raw->progs); + if (old_array && bpf_prog_array_length(old_array) >= BPF_MAX_PROGS) { ret = -E2BIG; goto unlock; } - old_array = raw->progs; ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); if (ret < 0) goto unlock; @@ -174,7 +177,7 @@ static int lirc_bpf_attach(struct rc_dev *rcdev, struct bpf_prog *prog) static int lirc_bpf_detach(struct rc_dev *rcdev, struct bpf_prog *prog) { - struct bpf_prog_array __rcu *old_array; + struct bpf_prog_array *old_array; struct bpf_prog_array *new_array; struct ir_raw_event_ctrl *raw; int ret; @@ -192,7 +195,7 @@ static int lirc_bpf_detach(struct rc_dev *rcdev, struct bpf_prog *prog) goto unlock; } - old_array = raw->progs; + old_array = lirc_rcu_dereference(raw->progs); ret = bpf_prog_array_copy(old_array, prog, NULL, &new_array); /* * Do not use bpf_prog_array_delete_safe() as we would end up @@ -223,21 +226,22 @@ void lirc_bpf_run(struct rc_dev *rcdev, u32 sample) /* * This should be called once the rc thread has been stopped, so there can be * no concurrent bpf execution. + * + * Should be called with the ir_raw_handler_lock held. */ void lirc_bpf_free(struct rc_dev *rcdev) { struct bpf_prog_array_item *item; + struct bpf_prog_array *array; - if (!rcdev->raw->progs) + array = lirc_rcu_dereference(rcdev->raw->progs); + if (!array) return; - item = rcu_dereference(rcdev->raw->progs)->items; - while (item->prog) { + for (item = array->items; item->prog; item++) bpf_prog_put(item->prog); - item++; - } - bpf_prog_array_free(rcdev->raw->progs); + bpf_prog_array_free(array); } int lirc_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog) @@ -290,7 +294,7 @@ int lirc_prog_detach(const union bpf_attr *attr) int lirc_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr) { __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids); - struct bpf_prog_array __rcu *progs; + struct bpf_prog_array *progs; struct rc_dev *rcdev; u32 cnt, flags = 0; int ret; @@ -311,7 +315,7 @@ int lirc_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr) if (ret) goto put; - progs = rcdev->raw->progs; + progs = lirc_rcu_dereference(rcdev->raw->progs); cnt = progs ? bpf_prog_array_length(progs) : 0; if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt))) { -- 2.22.0.rc1.257.g3120a18244-goog