Race condition between aio_complete and aio_read_evt

Chen, Kenneth W (kenneth.w.chen@intel.com)
Tue, 8 Jul 2003 14:52:28 -0700


This is a MIME-formatted message. If you see this text it means that your
E-mail software does not support MIME-formatted messages.

--=_courier-23965-1057701206-0001-2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

We hit a memory ordering race condition on AIO ring buffer tail pointer
between function aio_complete() and aio_read_evt().

What happens is that on an architecture that has a relaxed memory
ordering model like IPF(ia64), explicit memory barrier is required in a
SMP execution environment. Considering the following case:

1 CPU is executing a tight loop of aio_read_evt. It is pulling event
off the ring buffer. During that loop, another CPU is executing
aio_complete() where it is putting event into the ring buffer and then
update the tail pointer. However, due to relaxed memory ordering model,
the tail pointer can be visible before the actual event is being
updated. So the other CPU sees the updated tail pointer but picks up a
staled event data.

A memory barrier is required in this case between the event data and
tail pointer update. Same is true for the head pointer but the window
of the race condition is nil. For function correctness, it is fixed
here as well.

By the way, this bug is fixed in the major distributor's kernel on 2.4.x
kernel series for a while, but somehow hasn't been propagated to 2.5
kernel yet.

The patch is relative to 2.5.74.

- Ken

--=_courier-23965-1057701206-0001-2
Content-Type: application/octet-stream; name="aio.memorder.patch"
Content-Transfer-Encoding: base64
Content-Description: aio.memorder.patch
Content-Disposition: attachment;
filename="aio.memorder.patch"

ZGlmZiAtTnVyIGxpbnV4LTIuNS43NC9mcy9haW8uYyBsaW51eC0yLjUuNzQuYWlvL2ZzL2Fpby5j
DQotLS0gbGludXgtMi41Ljc0L2ZzL2Fpby5jCVN1biBKdW4gMjIgMTE6MzI6NDMgMjAwMw0KKysr
IGxpbnV4LTIuNS43NC5haW8vZnMvYWlvLmMJVHVlIEp1bCAgOCAxNDo0OToyNyAyMDAzDQpAQCAt
Njc5LDEyICs2NzksMTEgQEANCiAJLyogYWZ0ZXIgZmxhZ2dpbmcgdGhlIHJlcXVlc3QgYXMgZG9u
ZSwgd2UNCiAJICogbXVzdCBuZXZlciBldmVuIGxvb2sgYXQgaXQgYWd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