From ecd0d11254a8e8637e85a12dffb0cb1b800bffcf Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fischer@muhq.space>
Date: Thu, 17 Mar 2022 18:14:07 +0100
Subject: [PATCH] io: do not use IO_FORGOTTEN_FUTURES_SKIP_CQE on linux <= 5.17

Apparantly my assumption that io_uring ignores unknown cqe flags is
wrong. And io_uring < Linux 5.17 completes forgotten futures with
-EINVAL.

Fixes: e140759dd601cf1eda2b179449170b87f6ee6fae
---
 emper/Emper.cpp        | 1 +
 emper/Emper.hpp        | 2 ++
 emper/io/IoContext.cpp | 4 ++--
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/emper/Emper.cpp b/emper/Emper.cpp
index 9b8e29cf..9f8527a5 100644
--- a/emper/Emper.cpp
+++ b/emper/Emper.cpp
@@ -141,6 +141,7 @@ void printInfo(std::ostream& strm) {
 }
 
 const bool IO_MUST_INVALIDATE_BROKEN_CHAIN = EMPER_LINUX_LT("5.15.0");
+const bool IO_FORGOTTEN_FUTURES_SKIP_CQE = EMPER_LINUX_GE("5.17");
 
 auto getFullVersion() -> std::string { return EMPER_FULL_VERSION; }
 
diff --git a/emper/Emper.hpp b/emper/Emper.hpp
index 77c40ed1..42a7465a 100644
--- a/emper/Emper.hpp
+++ b/emper/Emper.hpp
@@ -212,6 +212,8 @@ const bool WAITFREE_IO_STEALING =
 // warnings during the comparison use not yet initialized components is reduced.
 extern const bool IO_MUST_INVALIDATE_BROKEN_CHAIN;
 
+extern const bool IO_FORGOTTEN_FUTURES_SKIP_CQE;
+
 enum class IoSqPoller {
 	off,
 	one,
diff --git a/emper/io/IoContext.cpp b/emper/io/IoContext.cpp
index 0d94256f..22aeaa2d 100644
--- a/emper/io/IoContext.cpp
+++ b/emper/io/IoContext.cpp
@@ -81,8 +81,8 @@ auto IoContext::prepareFutureChain(Future &future, unsigned chain_length) -> uns
 	if (future.isForgotten()) {
 		// Do not generate cqes for forgotten futures this reduces the kernel
 		// and user space overhead for cqes we do not need in the first place.
-		// This has no effect on kernel versions not supporing IOSQE_CQE_SKIP_SUCCESS.
-		sqe->flags |= IOSQE_CQE_SKIP_SUCCESS;
+		// IOSQE_CQE_SKIP_SUCCESS is supported since Linux 5.17
+		if (IO_FORGOTTEN_FUTURES_SKIP_CQE) sqe->flags |= IOSQE_CQE_SKIP_SUCCESS;
 	}
 
 	// If we use a single io_uring and a SubmitActor the submission and preparation
-- 
GitLab