From: Eric Wong Subject: Re: epoll: possible bug from wakeup_source activation Date: Fri, 8 Mar 2013 20:49:44 +0000 Message-ID: <20130308204944.GA27379@dcvr.yhbt.net> References: <20130307112639.GA25130@dcvr.yhbt.net> <20130308013027.GA31830@dcvr.yhbt.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: NeilBrown , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, Davide Libenzi , Alexander Viro , linux-fsdevel@vger.kernel.org, Andrew Morton To: Arve =?utf-8?B?SGrDuG5uZXbDpWc=?= Return-path: Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org Arve Hj=C3=B8nnev=C3=A5g wrote: > On Thu, Mar 7, 2013 at 5:30 PM, Eric Wong wro= te: > > Eric Wong wrote: > >> Hi Arve, looking at commit 4d7e30d98939a0340022ccd49325a3d70f7e023= 8 > >> (epoll: Add a flag, EPOLLWAKEUP, to prevent suspend ...) > >> > >> I think the reason for using ep->ws instead of epi->ws in the unli= kely > >> ovflist case applies to the likely rdllist case, too. Since epi->= ws is > >> only protected by ep->mtx, it can also be deactivated while inside > >> ep_poll_callback. > >> > >> So something like the following patch might be necessary > >> (shown here with extra context): > >> > >> --- a/fs/eventpoll.c > >> +++ b/fs/eventpoll.c > >> @@ -968,39 +968,45 @@ static int ep_poll_callback(wait_queue_t *wa= it, unsigned mode, int sync, void *k > >> if (unlikely(ep->ovflist !=3D EP_UNACTIVE_PTR)) { > >> if (epi->next =3D=3D EP_UNACTIVE_PTR) { > >> epi->next =3D ep->ovflist; > >> ep->ovflist =3D epi; > >> if (epi->ws) { > >> /* > >> * Activate ep->ws since epi->ws may= get > >> * deactivated at any time. > >> */ > >> __pm_stay_awake(ep->ws); > >> } > >> > >> } > > > > Thinking about this more, it looks like the original ep->ovflist ca= se of > > using ep->ws is unnecessary. > > > > ep->ovflist !=3D EP_UNACTIVE_PTR can only happen while ep->mtx is h= eld (in > > ep_scan_ready_list); which means ep_modify+friends cannot remove ep= i->ws. > > >=20 > The callback function in ep_scan_ready_list can call __pm_relax on it= though. >=20 > > ep_poll_callback holding ep->lock means ep_poll_callback prevents > > ep_scan_ready_list from setting ep->ovflist =3D EP_UNACTIVE_PTR and > > releasing ep->mtx. > > This code is reached when ep_scan_ready_list has set ep->ovflist to > NULL before releasing ep->lock. Since the callback function can call > __pm_relax on epi->ws without holding ep->lock we call __pm_stay_awak= e > in ep->ws here (the callback does not call __pm_relax on that). Thanks for the explanation. I got "deactivate" and "destroy" mixed up. However, I'm still concerned about the "destroy" case: > > > >> goto out_unlock; > >> } > >> > >> /* If this file is already in the ready list we exit soon */ > >> if (!ep_is_linked(&epi->rdllink)) { > >> list_add_tail(&epi->rdllink, &ep->rdllist); > >> - __pm_stay_awake(epi->ws); > >> + if (epi->ws) { > >> + /* > >> + * Activate ep->ws since epi->ws may get > >> + * deactivated at any time. > >> + */ > >> + __pm_stay_awake(ep->ws); > >> + } > >> } > > > > I still think ep->ws needs to be used in the common ep->rdllist cas= e. >=20 > ep_scan_ready_list calls __pm_relax on ep->ws when it is done, so thi= s > will not work. ep->ws is not a "ep->rdllist not empty wakeup_source i= s > is a "ep_scan_ready_list is running" wakeup_source. What happens if ep_modify calls ep_destroy_wakeup_source while __pm_stay_awake is running on the same epi->ws?