* [PATCH 03/13] drivers/acpi: convert seqno seqnum_ops
2020-11-10 19:53 [PATCH 00/13] Introduce seqnum_ops Shuah Khan
@ 2020-11-10 19:53 ` Shuah Khan
2020-11-10 19:53 ` [PATCH 04/13] drivers/acpi/apei: convert seqno to seqnum_ops Shuah Khan
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: Shuah Khan @ 2020-11-10 19:53 UTC (permalink / raw)
To: rafael, lenb, gregkh, keescook, peterz
Cc: Shuah Khan, linux-acpi, linux-kernel
seqnum_ops api is introduced to be used when a variable is used as
a sequence/stat counter and doesn't guard object lifetimes. This
clearly differentiates atomic_t usages that guard object lifetimes.
seqnum32 variables wrap around to INT_MIN when it overflows and
should not be used to guard resource lifetimes, device usage and
open counts that control state changes, and pm states.
seqno is a sequence number counter for logging. This counter gets
incremented. Unsure if there is a chance of this overflowing. It
doesn't look like overflowing causes any problems since it is used
to tag the log messages and nothing more. This conversion doesn't
change the overflow wrap around behavior.
Convert it to use seqnum_ops. This conversion replaces inc_return()
with _inc() followed by _read().
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
drivers/acpi/acpi_extlog.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/acpi/acpi_extlog.c b/drivers/acpi/acpi_extlog.c
index 72f1fb77abcd..1e2b36aab9aa 100644
--- a/drivers/acpi/acpi_extlog.c
+++ b/drivers/acpi/acpi_extlog.c
@@ -12,6 +12,7 @@
#include <linux/ratelimit.h>
#include <linux/edac.h>
#include <linux/ras.h>
+#include <linux/seqnum_ops.h>
#include <asm/cpu.h>
#include <asm/mce.h>
@@ -93,7 +94,7 @@ static struct acpi_hest_generic_status *extlog_elog_entry_check(int cpu, int ban
static void __print_extlog_rcd(const char *pfx,
struct acpi_hest_generic_status *estatus, int cpu)
{
- static atomic_t seqno;
+ static struct seqnum32 seqno;
unsigned int curr_seqno;
char pfx_seq[64];
@@ -103,7 +104,8 @@ static void __print_extlog_rcd(const char *pfx,
else
pfx = KERN_ERR;
}
- curr_seqno = atomic_inc_return(&seqno);
+ seqnum32_inc(&seqno);
+ curr_seqno = seqnum32_read(&seqno);
snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno);
printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu);
cper_estatus_print(pfx_seq, estatus);
--
2.27.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 04/13] drivers/acpi/apei: convert seqno to seqnum_ops
2020-11-10 19:53 [PATCH 00/13] Introduce seqnum_ops Shuah Khan
2020-11-10 19:53 ` [PATCH 03/13] drivers/acpi: convert seqno seqnum_ops Shuah Khan
@ 2020-11-10 19:53 ` Shuah Khan
2020-11-10 20:44 ` [PATCH 00/13] Introduce seqnum_ops Alan Stern
2020-11-11 4:33 ` Matthew Wilcox
3 siblings, 0 replies; 8+ messages in thread
From: Shuah Khan @ 2020-11-10 19:53 UTC (permalink / raw)
To: rafael, james.morse, tony.luck, bp, gregkh, keescook, peterz
Cc: Shuah Khan, linux-acpi, linux-kernel
seqnum_ops api is introduced to be used when a variable is used as
a sequence/stat counter and doesn't guard object lifetimes. This
clearly differentiates atomic_t usages that guard object lifetimes.
seqnum32 variables wrap around to INT_MIN when it overflows and
should not be used to guard resource lifetimes, device usage and
open counts that control state changes, and pm states.
seqno is a sequence number counter for logging. This counter gets
incremented. Unsure if there is a chance of this overflowing. It
doesn't look like overflowing causes any problems since it is used
to tag the log messages and nothing more. This conversion doesn't
change the overflow wrap around behavior.
Convert it to use seqnum_ops. This conversion replaces inc_return()
with _inc() followed by _read().
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
drivers/acpi/apei/ghes.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
index fce7ade2aba9..aa91998ce6f4 100644
--- a/drivers/acpi/apei/ghes.c
+++ b/drivers/acpi/apei/ghes.c
@@ -41,6 +41,7 @@
#include <linux/uuid.h>
#include <linux/ras.h>
#include <linux/task_work.h>
+#include <linux/seqnum_ops.h>
#include <acpi/actbl1.h>
#include <acpi/ghes.h>
@@ -625,7 +626,7 @@ static void __ghes_print_estatus(const char *pfx,
const struct acpi_hest_generic *generic,
const struct acpi_hest_generic_status *estatus)
{
- static atomic_t seqno;
+ static struct seqnum32 seqno = SEQNUM_INIT(0);
unsigned int curr_seqno;
char pfx_seq[64];
@@ -636,7 +637,8 @@ static void __ghes_print_estatus(const char *pfx,
else
pfx = KERN_ERR;
}
- curr_seqno = atomic_inc_return(&seqno);
+ seqnum32_inc(&seqno);
+ curr_seqno = seqnum32_read(&seqno);
snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
pfx_seq, generic->header.source_id);
--
2.27.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 00/13] Introduce seqnum_ops
2020-11-10 19:53 [PATCH 00/13] Introduce seqnum_ops Shuah Khan
2020-11-10 19:53 ` [PATCH 03/13] drivers/acpi: convert seqno seqnum_ops Shuah Khan
2020-11-10 19:53 ` [PATCH 04/13] drivers/acpi/apei: convert seqno to seqnum_ops Shuah Khan
@ 2020-11-10 20:44 ` Alan Stern
2020-11-10 22:42 ` Shuah Khan
2020-11-11 4:33 ` Matthew Wilcox
3 siblings, 1 reply; 8+ messages in thread
From: Alan Stern @ 2020-11-10 20:44 UTC (permalink / raw)
To: Shuah Khan
Cc: corbet, keescook, gregkh, peterz, rafael, lenb, james.morse,
tony.luck, bp, minyard, arnd, mchehab, rric, valentina.manea.m,
shuah, zohar, dmitry.kasatkin, jmorris, serge, linux-doc,
linux-kernel, linux-kselftest, linux-acpi, openipmi-developer,
linux-edac, linux-usb, linux-integrity, linux-security-module
On Tue, Nov 10, 2020 at 12:53:26PM -0700, Shuah Khan wrote:
> There are a number of atomic_t usages in the kernel where atomic_t api
> is used strictly for counting sequence numbers and other statistical
> counters and not for managing object lifetime.
>
> The purpose of these Sequence Number Ops is to clearly differentiate
> atomic_t counter usages from atomic_t usages that guard object lifetimes,
> hence prone to overflow and underflow errors.
>
> The atomic_t api provides a wide range of atomic operations as a base
> api to implement atomic counters, bitops, spinlock interfaces. The usages
> also evolved into being used for resource lifetimes and state management.
> The refcount_t api was introduced to address resource lifetime problems
> related to atomic_t wrapping. There is a large overlap between the
> atomic_t api used for resource lifetimes and just counters, stats, and
> sequence numbers. It has become difficult to differentiate between the
> atomic_t usages that should be converted to refcount_t and the ones that
> can be left alone. Introducing seqnum_ops to wrap the usages that are
> stats, counters, sequence numbers makes it easier for tools that scan
> for underflow and overflow on atomic_t usages to detect overflow and
> underflows to scan just the cases that are prone to errors.
>
> Sequence Number api provides interfaces for simple atomic_t counter usages
> that just count, and don't guard resource lifetimes. The seqnum_ops are
> built on top of atomic_t api, providing a smaller subset of atomic_t
> interfaces necessary to support atomic_t usages as simple counters.
> This api has init/set/inc/dec/read and doesn't support any other atomic_t
> ops with the intent to restrict the use of these interfaces as simple
> counting usages.
>
> Sequence Numbers wrap around to INT_MIN when it overflows and should not
> be used to guard resource lifetimes, device usage and open counts that
> control state changes, and pm states. Overflowing to INT_MIN is consistent
> with the atomic_t api, which it is built on top of.
If Sequence Numbers are subject to wraparound then they aren't reliable.
Given that they aren't reliable, why use atomic instructions at all?
Why not just use plain regular integers with READ_ONCE and WRITE_ONCE?
Alan Stern
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 00/13] Introduce seqnum_ops
2020-11-10 20:44 ` [PATCH 00/13] Introduce seqnum_ops Alan Stern
@ 2020-11-10 22:42 ` Shuah Khan
0 siblings, 0 replies; 8+ messages in thread
From: Shuah Khan @ 2020-11-10 22:42 UTC (permalink / raw)
To: Alan Stern
Cc: corbet, keescook, gregkh, peterz, rafael, lenb, james.morse,
tony.luck, bp, minyard, arnd, mchehab, rric, valentina.manea.m,
shuah, zohar, dmitry.kasatkin, jmorris, serge, linux-doc,
linux-kernel, linux-kselftest, linux-acpi, openipmi-developer,
linux-edac, linux-usb, linux-integrity, linux-security-module
On 11/10/20 1:44 PM, Alan Stern wrote:
> On Tue, Nov 10, 2020 at 12:53:26PM -0700, Shuah Khan wrote:
>> There are a number of atomic_t usages in the kernel where atomic_t api
>> is used strictly for counting sequence numbers and other statistical
>> counters and not for managing object lifetime.
>>
>> The purpose of these Sequence Number Ops is to clearly differentiate
>> atomic_t counter usages from atomic_t usages that guard object lifetimes,
>> hence prone to overflow and underflow errors.
>>
>> The atomic_t api provides a wide range of atomic operations as a base
>> api to implement atomic counters, bitops, spinlock interfaces. The usages
>> also evolved into being used for resource lifetimes and state management.
>> The refcount_t api was introduced to address resource lifetime problems
>> related to atomic_t wrapping. There is a large overlap between the
>> atomic_t api used for resource lifetimes and just counters, stats, and
>> sequence numbers. It has become difficult to differentiate between the
>> atomic_t usages that should be converted to refcount_t and the ones that
>> can be left alone. Introducing seqnum_ops to wrap the usages that are
>> stats, counters, sequence numbers makes it easier for tools that scan
>> for underflow and overflow on atomic_t usages to detect overflow and
>> underflows to scan just the cases that are prone to errors.
>>
>> Sequence Number api provides interfaces for simple atomic_t counter usages
>> that just count, and don't guard resource lifetimes. The seqnum_ops are
>> built on top of atomic_t api, providing a smaller subset of atomic_t
>> interfaces necessary to support atomic_t usages as simple counters.
>> This api has init/set/inc/dec/read and doesn't support any other atomic_t
>> ops with the intent to restrict the use of these interfaces as simple
>> counting usages.
>>
>> Sequence Numbers wrap around to INT_MIN when it overflows and should not
>> be used to guard resource lifetimes, device usage and open counts that
>> control state changes, and pm states. Overflowing to INT_MIN is consistent
>> with the atomic_t api, which it is built on top of.
>
> If Sequence Numbers are subject to wraparound then they aren't reliable.
> Given that they aren't reliable, why use atomic instructions at all?
> Why not just use plain regular integers with READ_ONCE and WRITE_ONCE?
>
You still need atomic update for these numbers. The intent is to provide
atomic api for cases where the variable doesn't guard lifetimes and yet
needs atomic instructions.
Several such usages where atomic_t is used for up counting, also use
upper bounds. It is also an option to switch to seqnum64 to avoid
wrap around in case there is a concern.
thanks,
-- Shuah
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 00/13] Introduce seqnum_ops
2020-11-10 19:53 [PATCH 00/13] Introduce seqnum_ops Shuah Khan
` (2 preceding siblings ...)
2020-11-10 20:44 ` [PATCH 00/13] Introduce seqnum_ops Alan Stern
@ 2020-11-11 4:33 ` Matthew Wilcox
2020-11-11 16:03 ` Shuah Khan
3 siblings, 1 reply; 8+ messages in thread
From: Matthew Wilcox @ 2020-11-11 4:33 UTC (permalink / raw)
To: Shuah Khan
Cc: corbet, keescook, gregkh, peterz, rafael, lenb, james.morse,
tony.luck, bp, minyard, arnd, mchehab, rric, valentina.manea.m,
shuah, zohar, dmitry.kasatkin, jmorris, serge, linux-doc,
linux-kernel, linux-kselftest, linux-acpi, openipmi-developer,
linux-edac, linux-usb, linux-integrity, linux-security-module
On Tue, Nov 10, 2020 at 12:53:26PM -0700, Shuah Khan wrote:
> There are a number of atomic_t usages in the kernel where atomic_t api
> is used strictly for counting sequence numbers and other statistical
> counters and not for managing object lifetime.
We already have something in Linux called a sequence counter, and it's
different from this. ID counter? instance number? monotonic_t? stat_t?
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH 00/13] Introduce seqnum_ops
2020-11-11 4:33 ` Matthew Wilcox
@ 2020-11-11 16:03 ` Shuah Khan
2020-11-11 16:41 ` Matthew Wilcox
0 siblings, 1 reply; 8+ messages in thread
From: Shuah Khan @ 2020-11-11 16:03 UTC (permalink / raw)
To: Matthew Wilcox
Cc: corbet, keescook, gregkh, peterz, rafael, lenb, james.morse,
tony.luck, bp, minyard, arnd, mchehab, rric, valentina.manea.m,
shuah, zohar, dmitry.kasatkin, jmorris, serge, linux-doc,
linux-kernel, linux-kselftest, linux-acpi, openipmi-developer,
linux-edac, linux-usb, linux-integrity, linux-security-module,
skhan
On 11/10/20 9:33 PM, Matthew Wilcox wrote:
> On Tue, Nov 10, 2020 at 12:53:26PM -0700, Shuah Khan wrote:
>> There are a number of atomic_t usages in the kernel where atomic_t api
>> is used strictly for counting sequence numbers and other statistical
>> counters and not for managing object lifetime.
>
> We already have something in Linux called a sequence counter, and it's
> different from this. ID counter? instance number? monotonic_t? stat_t?
>
No results for monotonic_t or stat_t. Can you give me a pointer to what
your referring to.
thanks,
-- Shuah
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 00/13] Introduce seqnum_ops
2020-11-11 16:03 ` Shuah Khan
@ 2020-11-11 16:41 ` Matthew Wilcox
0 siblings, 0 replies; 8+ messages in thread
From: Matthew Wilcox @ 2020-11-11 16:41 UTC (permalink / raw)
To: Shuah Khan
Cc: corbet, keescook, gregkh, peterz, rafael, lenb, james.morse,
tony.luck, bp, minyard, arnd, mchehab, rric, valentina.manea.m,
shuah, zohar, dmitry.kasatkin, jmorris, serge, linux-doc,
linux-kernel, linux-kselftest, linux-acpi, openipmi-developer,
linux-edac, linux-usb, linux-integrity, linux-security-module
On Wed, Nov 11, 2020 at 09:03:20AM -0700, Shuah Khan wrote:
> On 11/10/20 9:33 PM, Matthew Wilcox wrote:
> > On Tue, Nov 10, 2020 at 12:53:26PM -0700, Shuah Khan wrote:
> > > There are a number of atomic_t usages in the kernel where atomic_t api
> > > is used strictly for counting sequence numbers and other statistical
> > > counters and not for managing object lifetime.
> >
> > We already have something in Linux called a sequence counter, and it's
> > different from this. ID counter? instance number? monotonic_t? stat_t?
> >
>
> No results for monotonic_t or stat_t. Can you give me a pointer to what
> your referring to.
We have a seqcount_t. We need to call this something different.
maybe we should call it stat_t (and for that usage, stat_add() as well
as stat_inc() is a legitimate API to have).
^ permalink raw reply [flat|nested] 8+ messages in thread