Skip to content

Commit bb13534

Browse files
committed
Merge PR ceph#60220 into main
* refs/pull/60220/head: msg/async/AsyncConnection: move the writeCallback instead of copying it msg/async/AsyncConnection: do not wrap writeCallback in `std::optional` msg/async/frames_v2: use zero-initialization instead of memset() msg/async/Event: use zero-initialization instead of memset() msg/Message: use zero-initialization instead of memset() msg/async/ProtocolV2: eliminate redundant std::map lookups msg/async/ProtocolV[12]: reverse the std::map sort order msg/async/ProtocolV[12]: use `auto` msg/async/ProtocolV[12]: use range-based `for` msg/async/ProtocolV1: use zero-initialization instead of memset() Reviewed-by: Patrick Donnelly <[email protected]>
2 parents 76478d0 + 425fc4d commit bb13534

File tree

9 files changed

+59
-77
lines changed

9 files changed

+59
-77
lines changed

src/msg/Message.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ class Message : public RefCountedObject {
262262
#endif
263263

264264
protected:
265-
ceph_msg_header header; // headerelope
266-
ceph_msg_footer footer;
265+
ceph_msg_header header{}; // headerelope
266+
ceph_msg_footer footer{};
267267
ceph::buffer::list payload; // "front" unaligned blob
268268
ceph::buffer::list middle; // "middle" unaligned blob
269269
ceph::buffer::list data; // data payload (page-alignment will be preserved where possible)
@@ -332,16 +332,11 @@ class Message : public RefCountedObject {
332332
friend class Messenger;
333333

334334
public:
335-
Message() {
336-
memset(&header, 0, sizeof(header));
337-
memset(&footer, 0, sizeof(footer));
338-
}
335+
Message() = default;
339336
Message(int t, int version=1, int compat_version=0) {
340-
memset(&header, 0, sizeof(header));
341337
header.type = t;
342338
header.version = version;
343339
header.compat_version = compat_version;
344-
memset(&footer, 0, sizeof(footer));
345340
}
346341

347342
Message *get() {

src/msg/async/AsyncConnection.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ ssize_t AsyncConnection::write(ceph::buffer::list &bl,
310310
outgoing_bl.claim_append(bl);
311311
ssize_t r = _try_send(more);
312312
if (r > 0) {
313-
writeCallback = callback;
313+
writeCallback = std::move(callback);
314314
}
315315
return r;
316316
}
@@ -621,7 +621,7 @@ void AsyncConnection::fault()
621621
}
622622

623623
void AsyncConnection::_stop() {
624-
writeCallback.reset();
624+
writeCallback = {};
625625
dispatch_queue->discard_queue(conn_id);
626626
async_msgr->unregister_conn(this);
627627
worker->release_worker();
@@ -737,8 +737,7 @@ void AsyncConnection::handle_write_callback() {
737737
recv_start_time = ceph::mono_clock::now();
738738
write_lock.lock();
739739
if (writeCallback) {
740-
auto callback = *writeCallback;
741-
writeCallback.reset();
740+
auto callback = std::move(writeCallback);
742741
write_lock.unlock();
743742
callback(0);
744743
return;

src/msg/async/AsyncConnection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class AsyncConnection : public Connection {
223223

224224
std::unique_ptr<Protocol> protocol;
225225

226-
std::optional<std::function<void(ssize_t)>> writeCallback;
226+
std::function<void(ssize_t)> writeCallback;
227227
std::function<void(char *, ssize_t)> readCallback;
228228
std::optional<unsigned> pendingReadLen;
229229
char *read_buffer;

src/msg/async/Event.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ class EventCenter {
9797
using clock_type = ceph::coarse_mono_clock;
9898

9999
struct AssociatedCenters {
100-
EventCenter *centers[MAX_EVENTCENTER];
101-
AssociatedCenters() {
102-
// FIPS zeroization audit 20191115: this memset is not security related.
103-
memset(centers, 0, MAX_EVENTCENTER * sizeof(EventCenter*));
104-
}
100+
EventCenter *centers[MAX_EVENTCENTER]{};
105101
};
106102

107103
struct FileEvent {

src/msg/async/ProtocolV1.cc

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ void ProtocolV1::connect() {
9090

9191
// reset connect state variables
9292
authorizer_buf.clear();
93-
// FIPS zeroization audit 20191115: these memsets are not security related.
94-
memset(&connect_msg, 0, sizeof(connect_msg));
95-
memset(&connect_reply, 0, sizeof(connect_reply));
93+
connect_msg = {};
94+
connect_reply = {};
9695

9796
global_seq = messenger->get_global_seq();
9897
}
@@ -820,7 +819,7 @@ CtPtr ProtocolV1::read_message_data_prepare() {
820819
#if 0
821820
// rx_buffers is broken by design... see
822821
// http://tracker.ceph.com/issues/22480
823-
map<ceph_tid_t, pair<ceph::buffer::list, int> >::iterator p =
822+
const auto p =
824823
connection->rx_buffers.find(current_header.tid);
825824
if (p != connection->rx_buffers.end()) {
826825
ldout(cct, 10) << __func__ << " seleting rx buffer v " << p->second.second
@@ -1205,7 +1204,7 @@ void ProtocolV1::requeue_sent() {
12051204
return;
12061205
}
12071206

1208-
list<out_q_entry_t> &rq = out_q[CEPH_MSG_PRIO_HIGHEST];
1207+
auto &rq = out_q[CEPH_MSG_PRIO_HIGHEST];
12091208
out_seq -= sent.size();
12101209
while (!sent.empty()) {
12111210
Message *m = sent.back();
@@ -1220,10 +1219,11 @@ void ProtocolV1::requeue_sent() {
12201219
uint64_t ProtocolV1::discard_requeued_up_to(uint64_t out_seq, uint64_t seq) {
12211220
ldout(cct, 10) << __func__ << " " << seq << dendl;
12221221
std::lock_guard<std::mutex> l(connection->write_lock);
1223-
if (out_q.count(CEPH_MSG_PRIO_HIGHEST) == 0) {
1222+
const auto it = out_q.find(CEPH_MSG_PRIO_HIGHEST);
1223+
if (it == out_q.end()) {
12241224
return seq;
12251225
}
1226-
list<out_q_entry_t> &rq = out_q[CEPH_MSG_PRIO_HIGHEST];
1226+
auto &rq = it->second;
12271227
uint64_t count = out_seq;
12281228
while (!rq.empty()) {
12291229
Message* const m = rq.front().m;
@@ -1235,7 +1235,7 @@ uint64_t ProtocolV1::discard_requeued_up_to(uint64_t out_seq, uint64_t seq) {
12351235
rq.pop_front();
12361236
count++;
12371237
}
1238-
if (rq.empty()) out_q.erase(CEPH_MSG_PRIO_HIGHEST);
1238+
if (rq.empty()) out_q.erase(it);
12391239
return count;
12401240
}
12411241

@@ -1246,18 +1246,16 @@ uint64_t ProtocolV1::discard_requeued_up_to(uint64_t out_seq, uint64_t seq) {
12461246
void ProtocolV1::discard_out_queue() {
12471247
ldout(cct, 10) << __func__ << " started" << dendl;
12481248

1249-
for (list<Message *>::iterator p = sent.begin(); p != sent.end(); ++p) {
1250-
ldout(cct, 20) << __func__ << " discard " << *p << dendl;
1251-
(*p)->put();
1249+
for (Message *msg : sent) {
1250+
ldout(cct, 20) << __func__ << " discard " << msg << dendl;
1251+
msg->put();
12521252
}
12531253
sent.clear();
1254-
for (map<int, list<out_q_entry_t>>::iterator p =
1255-
out_q.begin();
1256-
p != out_q.end(); ++p) {
1257-
for (list<out_q_entry_t>::iterator r = p->second.begin();
1258-
r != p->second.end(); ++r) {
1259-
ldout(cct, 20) << __func__ << " discard " << r->m << dendl;
1260-
r->m->put();
1254+
for (auto& [ prio, entries ] : out_q) {
1255+
static_cast<void>(prio);
1256+
for (auto& entry : entries) {
1257+
ldout(cct, 20) << __func__ << " discard " << entry.m << dendl;
1258+
entry.m->put();
12611259
}
12621260
}
12631261
out_q.clear();
@@ -1296,7 +1294,7 @@ void ProtocolV1::reset_recv_state()
12961294

12971295
// clean read and write callbacks
12981296
connection->pendingReadLen.reset();
1299-
connection->writeCallback.reset();
1297+
connection->writeCallback = {};
13001298

13011299
if (state > THROTTLE_MESSAGE && state <= READ_FOOTER_AND_DISPATCH &&
13021300
connection->policy.throttler_messages) {
@@ -1328,14 +1326,12 @@ void ProtocolV1::reset_recv_state()
13281326

13291327
ProtocolV1::out_q_entry_t ProtocolV1::_get_next_outgoing() {
13301328
out_q_entry_t out_entry;
1331-
if (!out_q.empty()) {
1332-
map<int, list<out_q_entry_t>>::reverse_iterator it =
1333-
out_q.rbegin();
1329+
if (const auto it = out_q.begin(); it != out_q.end()) {
13341330
ceph_assert(!it->second.empty());
1335-
list<out_q_entry_t>::iterator p = it->second.begin();
1331+
const auto p = it->second.begin();
13361332
out_entry = *p;
13371333
it->second.erase(p);
1338-
if (it->second.empty()) out_q.erase(it->first);
1334+
if (it->second.empty()) out_q.erase(it);
13391335
}
13401336
return out_entry;
13411337
}
@@ -1572,8 +1568,7 @@ CtPtr ProtocolV1::handle_connect_message_write(int r) {
15721568
CtPtr ProtocolV1::wait_connect_reply() {
15731569
ldout(cct, 20) << __func__ << dendl;
15741570

1575-
// FIPS zeroization audit 20191115: this memset is not security related.
1576-
memset(&connect_reply, 0, sizeof(connect_reply));
1571+
connect_reply = {};
15771572
return READ(sizeof(connect_reply), handle_connect_reply_1);
15781573
}
15791574

@@ -1923,8 +1918,7 @@ CtPtr ProtocolV1::handle_client_banner(char *buffer, int r) {
19231918
CtPtr ProtocolV1::wait_connect_message() {
19241919
ldout(cct, 20) << __func__ << dendl;
19251920

1926-
// FIPS zeroization audit 20191115: this memset is not security related.
1927-
memset(&connect_msg, 0, sizeof(connect_msg));
1921+
connect_msg = {};
19281922
return READ(sizeof(connect_msg), handle_connect_message_1);
19291923
}
19301924

@@ -1988,8 +1982,7 @@ CtPtr ProtocolV1::handle_connect_message_2() {
19881982
ceph_msg_connect_reply reply;
19891983
ceph::buffer::list authorizer_reply;
19901984

1991-
// FIPS zeroization audit 20191115: this memset is not security related.
1992-
memset(&reply, 0, sizeof(reply));
1985+
reply = {};
19931986
reply.protocol_version =
19941987
messenger->get_proto_version(connection->peer_type, false);
19951988

@@ -2616,8 +2609,7 @@ CtPtr ProtocolV1::server_ready() {
26162609
<< dendl;
26172610

26182611
ldout(cct, 20) << __func__ << " accept done" << dendl;
2619-
// FIPS zeroization audit 20191115: this memset is not security related.
2620-
memset(&connect_msg, 0, sizeof(connect_msg));
2612+
connect_msg = {};
26212613

26222614
if (connection->delay_state) {
26232615
ceph_assert(connection->delay_state->ready());

src/msg/async/ProtocolV1.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ handle_tag_ack | v |
112112
bool is_prepared {false};
113113
};
114114
// priority queue for outbound msgs
115-
std::map<int, std::list<out_q_entry_t>> out_q;
115+
116+
/**
117+
* A queue for each priority value, highest priority first.
118+
*/
119+
std::map<int, std::list<out_q_entry_t>, std::greater<int>> out_q;
120+
116121
bool keepalive;
117122
bool write_in_progress = false;
118123

src/msg/async/ProtocolV2.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ bool ProtocolV2::is_connected() { return can_write; }
127127
void ProtocolV2::discard_out_queue() {
128128
ldout(cct, 10) << __func__ << " started" << dendl;
129129

130-
for (auto p = sent.begin(); p != sent.end(); ++p) {
131-
ldout(cct, 20) << __func__ << " discard " << *p << dendl;
132-
(*p)->put();
130+
for (Message *msg : sent) {
131+
ldout(cct, 20) << __func__ << " discard " << msg << dendl;
132+
msg->put();
133133
}
134134
sent.clear();
135135
for (auto& [ prio, entries ] : out_queue) {
@@ -211,10 +211,11 @@ void ProtocolV2::requeue_sent() {
211211
uint64_t ProtocolV2::discard_requeued_up_to(uint64_t out_seq, uint64_t seq) {
212212
ldout(cct, 10) << __func__ << " " << seq << dendl;
213213
std::lock_guard<std::mutex> l(connection->write_lock);
214-
if (out_queue.count(CEPH_MSG_PRIO_HIGHEST) == 0) {
214+
const auto it = out_queue.find(CEPH_MSG_PRIO_HIGHEST);
215+
if (it == out_queue.end()) {
215216
return seq;
216217
}
217-
auto& rq = out_queue[CEPH_MSG_PRIO_HIGHEST];
218+
auto& rq = it->second;
218219
uint64_t count = out_seq;
219220
while (!rq.empty()) {
220221
Message* const m = rq.front().m;
@@ -226,7 +227,7 @@ uint64_t ProtocolV2::discard_requeued_up_to(uint64_t out_seq, uint64_t seq) {
226227
rq.pop_front();
227228
count++;
228229
}
229-
if (rq.empty()) out_queue.erase(CEPH_MSG_PRIO_HIGHEST);
230+
if (rq.empty()) out_queue.erase(it);
230231
return count;
231232
}
232233

@@ -265,7 +266,7 @@ void ProtocolV2::reset_recv_state() {
265266

266267
// clean read and write callbacks
267268
connection->pendingReadLen.reset();
268-
connection->writeCallback.reset();
269+
connection->writeCallback = {};
269270

270271
next_tag = static_cast<Tag>(0);
271272

@@ -507,14 +508,13 @@ void ProtocolV2::read_event() {
507508
ProtocolV2::out_queue_entry_t ProtocolV2::_get_next_outgoing() {
508509
out_queue_entry_t out_entry;
509510

510-
if (!out_queue.empty()) {
511-
auto it = out_queue.rbegin();
511+
if (const auto it = out_queue.begin(); it != out_queue.end()) {
512512
auto& entries = it->second;
513513
ceph_assert(!entries.empty());
514514
out_entry = entries.front();
515515
entries.pop_front();
516516
if (entries.empty()) {
517-
out_queue.erase(it->first);
517+
out_queue.erase(it);
518518
}
519519
}
520520
return out_entry;

src/msg/async/ProtocolV2.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ class ProtocolV2 : public Protocol {
9393
bool is_prepared {false};
9494
Message* m {nullptr};
9595
};
96-
std::map<int, std::list<out_queue_entry_t>> out_queue;
96+
97+
/**
98+
* A queue for each priority value, highest priority first.
99+
*/
100+
std::map<int, std::list<out_queue_entry_t>, std::greater<int>> out_queue;
101+
97102
std::list<Message *> sent;
98103
std::atomic<uint64_t> out_seq{0};
99104
std::atomic<uint64_t> in_seq{0};

src/msg/async/frames_v2.cc

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ static bool check_epilogue_late_status(__u8 late_status) {
6363

6464
void FrameAssembler::fill_preamble(Tag tag,
6565
preamble_block_t& preamble) const {
66-
// FIPS zeroization audit 20191115: this memset is not security related.
67-
::memset(&preamble, 0, sizeof(preamble));
68-
66+
preamble = {};
6967
preamble.tag = static_cast<__u8>(tag);
7068
for (size_t i = 0; i < m_descs.size(); i++) {
7169
preamble.segments[i].length = m_descs[i].logical_len;
@@ -100,9 +98,7 @@ uint64_t FrameAssembler::get_frame_onwire_len() const {
10098

10199
bufferlist FrameAssembler::asm_crc_rev0(const preamble_block_t& preamble,
102100
bufferlist segment_bls[]) const {
103-
epilogue_crc_rev0_block_t epilogue;
104-
// FIPS zeroization audit 20191115: this memset is not security related.
105-
::memset(&epilogue, 0, sizeof(epilogue));
101+
epilogue_crc_rev0_block_t epilogue{};
106102

107103
bufferlist frame_bl(sizeof(preamble) + sizeof(epilogue));
108104
frame_bl.append(reinterpret_cast<const char*>(&preamble), sizeof(preamble));
@@ -123,9 +119,7 @@ bufferlist FrameAssembler::asm_secure_rev0(const preamble_block_t& preamble,
123119
preamble_bl.append(reinterpret_cast<const char*>(&preamble),
124120
sizeof(preamble));
125121

126-
epilogue_secure_rev0_block_t epilogue;
127-
// FIPS zeroization audit 20191115: this memset is not security related.
128-
::memset(&epilogue, 0, sizeof(epilogue));
122+
epilogue_secure_rev0_block_t epilogue{};
129123
bufferlist epilogue_bl(sizeof(epilogue));
130124
epilogue_bl.append(reinterpret_cast<const char*>(&epilogue),
131125
sizeof(epilogue));
@@ -151,9 +145,7 @@ bufferlist FrameAssembler::asm_secure_rev0(const preamble_block_t& preamble,
151145

152146
bufferlist FrameAssembler::asm_crc_rev1(const preamble_block_t& preamble,
153147
bufferlist segment_bls[]) const {
154-
epilogue_crc_rev1_block_t epilogue;
155-
// FIPS zeroization audit 20191115: this memset is not security related.
156-
::memset(&epilogue, 0, sizeof(epilogue));
148+
epilogue_crc_rev1_block_t epilogue{};
157149
epilogue.late_status |= FRAME_LATE_STATUS_COMPLETE;
158150

159151
bufferlist frame_bl(sizeof(preamble) + FRAME_CRC_SIZE + sizeof(epilogue));
@@ -215,9 +207,7 @@ bufferlist FrameAssembler::asm_secure_rev1(const preamble_block_t& preamble,
215207
return frame_bl; // no epilogue if only one segment
216208
}
217209

218-
epilogue_secure_rev1_block_t epilogue;
219-
// FIPS zeroization audit 20191115: this memset is not security related.
220-
::memset(&epilogue, 0, sizeof(epilogue));
210+
epilogue_secure_rev1_block_t epilogue{};
221211
epilogue.late_status |= FRAME_LATE_STATUS_COMPLETE;
222212
bufferlist epilogue_bl(sizeof(epilogue));
223213
epilogue_bl.append(reinterpret_cast<const char*>(&epilogue),

0 commit comments

Comments
 (0)