Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp5784026ybl; Tue, 27 Aug 2019 09:35:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqzYT36mBo+dYZu+VuRvc/N3NdxNSC7DRH5zHvdUNgNWjLFgfrc5fjs1BuvUmhXzwAPcwrNn X-Received: by 2002:a17:90a:23c8:: with SMTP id g66mr26336955pje.123.1566923728488; Tue, 27 Aug 2019 09:35:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566923728; cv=none; d=google.com; s=arc-20160816; b=nUQgptseXXPNhazKovr6+l7Z0LUcPfreSkY+i+AySzrZm+iHYfMqanBNSPUtJJAJkm UZexqv2ZucUCZ+9D9uHHAebqonAjQyzfPJf8KjBN8/2Pg4LNkxHVjp4mfg/SWwwRgFJh 9v5l5AedPfgZq0ewJqnaKnfrjacRb5oPWaK9xzSW7c6hNJmorggF0fqyYl2gdc9WeXYd KCry1iHn+xB/KrziP7fnRiF5ZvrzpDp9a0pEppAd2WzMRqJTsdPPOq1JBy12LqAVtalw Yqa5x3pk4U/uLh0NSicamOZvNRywhLenk/Td+aieEyHxvwlqPaoipsetFra+7iLUq/87 Ttqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=mTOe7QdRz8ocBQp7qFX1xgXBCUukmXhyrNPzPaWEJkk=; b=M+AxZuqoxLthYdNvqmHyev6S6DqtZqwFHSxQNwg7iDutyOlBqlwT2RoWOyQC3o1vpB 0Hbg83BEQM2e9mC8/DL45JSGRoqM4LaZSj3PkXb+RH7WZYj8MkwKnf6S/8Xy+7GzpQQp WHJRGoChLQn/jv04ioQmbzO0CRo5ODTiRGREUp6LI2mNZwoTdoT/o8a9dK+RfYc8brhM 4UluKUzxbPIGdVJ6mlWUlFfssJcYxxHgTfnUt3OYonYfJUBo9TaObqJUhTJ2HNumztqZ lAs3n0oXrCOEU5tYaT+E06NJu5Sb3/qEKFusBFG8cfysoSkC3YJPuZPsOSCI/xTgNESI LxgQ== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j12si12319819pgp.261.2019.08.27.09.35.12; Tue, 27 Aug 2019 09:35:28 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730387AbfH0Qcj (ORCPT + 99 others); Tue, 27 Aug 2019 12:32:39 -0400 Received: from foss.arm.com ([217.140.110.172]:47578 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729779AbfH0Qce (ORCPT ); Tue, 27 Aug 2019 12:32:34 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 762491570; Tue, 27 Aug 2019 09:32:33 -0700 (PDT) Received: from fuggles.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3D5D73F59C; Tue, 27 Aug 2019 09:32:32 -0700 (PDT) From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Kees Cook , Ingo Molnar , Elena Reshetova , Peter Zijlstra , Ard Biesheuvel , Hanjun Guo , Jan Glauber Subject: [PATCH v2 2/6] lib/refcount: Ensure integer operands are treated as signed Date: Tue, 27 Aug 2019 17:32:00 +0100 Message-Id: <20190827163204.29903-3-will@kernel.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190827163204.29903-1-will@kernel.org> References: <20190827163204.29903-1-will@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation for changing the saturation point of REFCOUNT_FULL to INT_MIN / 2, change the type of integer operands passed into the API from 'unsigned int' to 'int' so that we can avoid casting during comparisons when we don't want to fall foul of C integral conversion rules for signed and unsigned types. Since the kernel is compiled with '-fno-strict-overflow', we don't need to worry about the UB introduced by signed overflow here. Furthermore, we're already making heavy use of the atomic_t API, which operates exclusively on signed types. Cc: Kees Cook Cc: Ingo Molnar Cc: Elena Reshetova Cc: Peter Zijlstra Cc: Ard Biesheuvel Signed-off-by: Will Deacon --- include/linux/refcount.h | 14 +++++++------- lib/refcount.c | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/linux/refcount.h b/include/linux/refcount.h index 79f62e8d2256..89066a1471dd 100644 --- a/include/linux/refcount.h +++ b/include/linux/refcount.h @@ -28,7 +28,7 @@ typedef struct refcount_struct { * @r: the refcount * @n: value to which the refcount will be set */ -static inline void refcount_set(refcount_t *r, unsigned int n) +static inline void refcount_set(refcount_t *r, int n) { atomic_set(&r->refs, n); } @@ -44,13 +44,13 @@ static inline unsigned int refcount_read(const refcount_t *r) return atomic_read(&r->refs); } -extern __must_check bool refcount_add_not_zero_checked(unsigned int i, refcount_t *r); -extern void refcount_add_checked(unsigned int i, refcount_t *r); +extern __must_check bool refcount_add_not_zero_checked(int i, refcount_t *r); +extern void refcount_add_checked(int i, refcount_t *r); extern __must_check bool refcount_inc_not_zero_checked(refcount_t *r); extern void refcount_inc_checked(refcount_t *r); -extern __must_check bool refcount_sub_and_test_checked(unsigned int i, refcount_t *r); +extern __must_check bool refcount_sub_and_test_checked(int i, refcount_t *r); extern __must_check bool refcount_dec_and_test_checked(refcount_t *r); extern void refcount_dec_checked(refcount_t *r); @@ -79,12 +79,12 @@ extern void refcount_dec_checked(refcount_t *r); # ifdef CONFIG_ARCH_HAS_REFCOUNT # include # else -static inline __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r) +static inline __must_check bool refcount_add_not_zero(int i, refcount_t *r) { return atomic_add_unless(&r->refs, i, 0); } -static inline void refcount_add(unsigned int i, refcount_t *r) +static inline void refcount_add(int i, refcount_t *r) { atomic_add(i, &r->refs); } @@ -99,7 +99,7 @@ static inline void refcount_inc(refcount_t *r) atomic_inc(&r->refs); } -static inline __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r) +static inline __must_check bool refcount_sub_and_test(int i, refcount_t *r) { return atomic_sub_and_test(i, &r->refs); } diff --git a/lib/refcount.c b/lib/refcount.c index 48b78a423d7d..719b0bc42ab1 100644 --- a/lib/refcount.c +++ b/lib/refcount.c @@ -61,7 +61,7 @@ * * Return: false if the passed refcount is 0, true otherwise */ -bool refcount_add_not_zero_checked(unsigned int i, refcount_t *r) +bool refcount_add_not_zero_checked(int i, refcount_t *r) { unsigned int new, val = atomic_read(&r->refs); @@ -101,7 +101,7 @@ EXPORT_SYMBOL(refcount_add_not_zero_checked); * cases, refcount_inc(), or one of its variants, should instead be used to * increment a reference count. */ -void refcount_add_checked(unsigned int i, refcount_t *r) +void refcount_add_checked(int i, refcount_t *r) { WARN_ONCE(!refcount_add_not_zero_checked(i, r), "refcount_t: addition on 0; use-after-free.\n"); } @@ -180,7 +180,7 @@ EXPORT_SYMBOL(refcount_inc_checked); * * Return: true if the resulting refcount is 0, false otherwise */ -bool refcount_sub_and_test_checked(unsigned int i, refcount_t *r) +bool refcount_sub_and_test_checked(int i, refcount_t *r) { unsigned int new, val = atomic_read(&r->refs); -- 2.11.0