From mboxrd@z Thu Jan 1 00:00:00 1970 From: der.herr@hofr.at (Nicholas Mc Guire) Date: Sat, 31 Jan 2015 13:04:20 +0100 Subject: spinlock variable protection In-Reply-To: References: Message-ID: <20150131120420.GA21819@opentech.at> To: kernelnewbies@lists.kernelnewbies.org List-Id: kernelnewbies.lists.kernelnewbies.org On Fri, 30 Jan 2015, Matwey V. Kornilov wrote: > 2015-01-30 16:52 GMT+03:00 buyitian : > >> > > Please check the assembly code to double confirm the GCC behavior. > > Why will GCC change the order as what you mentioned? Only assembly code can tell you. > > It does not change at the moment. I think it can change it. > > Because from line > ret = hdl->count; > until line > return ret; > there is no access to either ret or hdl->count. So it is reasonable to > optimizer to think that their values are the same and eliminate > unneeded variable. > If your worry is that it will optimize it out then pack it into an ACCESS_ONCE and that should prevent GCC from doing so. for your case I think ret = ACCESS_ONCE(hdl->count); would be sufficient. thx! hofrat