From patchwork Mon Aug 21 22:28:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hauke Mehrtens X-Patchwork-Id: 9913913 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E7E4B603FF for ; Mon, 21 Aug 2017 22:28:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C8A27286AA for ; Mon, 21 Aug 2017 22:28:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BD552286C5; Mon, 21 Aug 2017 22:28:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7682D286AB for ; Mon, 21 Aug 2017 22:28:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754422AbdHUW2u (ORCPT ); Mon, 21 Aug 2017 18:28:50 -0400 Received: from mx1.mailbox.org ([80.241.60.212]:48955 "EHLO mx1.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754430AbdHUW2t (ORCPT ); Mon, 21 Aug 2017 18:28:49 -0400 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id AC5C442524; Tue, 22 Aug 2017 00:28:48 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id YVSAtNiGiu-k; Tue, 22 Aug 2017 00:28:47 +0200 (CEST) From: Hauke Mehrtens To: johannes@sipsolutions.net Cc: backports@vger.kernel.org, Hauke Mehrtens Subject: [PATCH 19/21] header: add linux/refcount.h Date: Tue, 22 Aug 2017 00:28:15 +0200 Message-Id: <20170821222817.17376-20-hauke@hauke-m.de> In-Reply-To: <20170821222817.17376-1-hauke@hauke-m.de> References: <20170821222817.17376-1-hauke@hauke-m.de> Sender: backports-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: backports@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the new linux/refcount.h header file. This was added in kernel 4.11 and is used by some drivers. Signed-off-by: Hauke Mehrtens --- backport/backport-include/linux/refcount.h | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 backport/backport-include/linux/refcount.h diff --git a/backport/backport-include/linux/refcount.h b/backport/backport-include/linux/refcount.h new file mode 100644 index 00000000..96219f76 --- /dev/null +++ b/backport/backport-include/linux/refcount.h @@ -0,0 +1,47 @@ +#ifndef __BACKPORT_LINUX_REFCOUNT_H +#define __BACKPORT_LINUX_REFCOUNT_H +#if LINUX_VERSION_IS_GEQ(4,11,0) +#include_next +#else + +#include +#include +#include +#include + +/** + * refcount_t - variant of atomic_t specialized for reference counts + * @refs: atomic_t counter field + * + * The counter saturates at UINT_MAX and will not move once + * there. This avoids wrapping the counter and causing 'spurious' + * use-after-free bugs. + */ +typedef struct refcount_struct { + atomic_t refs; +} refcount_t; + +#define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), } + +/** + * refcount_set - set a refcount's value + * @r: the refcount + * @n: value to which the refcount will be set + */ +static inline void refcount_set(refcount_t *r, unsigned int n) +{ + atomic_set(&r->refs, n); +} + +static inline void refcount_inc(refcount_t *r) +{ + atomic_inc(&r->refs); +} + +static inline __must_check bool refcount_dec_and_test(refcount_t *r) +{ + return atomic_dec_and_test(&r->refs); +} + +#endif /* LINUX_VERSION_IS_GEQ(4,11,0) */ +#endif /* __BACKPORT_LINUX_REFCOUNT_H */