sched.c 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755
  1. /*
  2. * kernel/sched.c
  3. *
  4. * Kernel scheduler and related syscalls
  5. *
  6. * Copyright (C) 1991-2002 Linus Torvalds
  7. *
  8. * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
  9. * make semaphores SMP safe
  10. * 1998-11-19 Implemented schedule_timeout() and related stuff
  11. * by Andrea Arcangeli
  12. * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
  13. * hybrid priority-list and round-robin design with
  14. * an array-switch method of distributing timeslices
  15. * and per-CPU runqueues. Cleanups and useful suggestions
  16. * by Davide Libenzi, preemptible kernel bits by Robert Love.
  17. * 2003-09-03 Interactivity tuning by Con Kolivas.
  18. * 2004-04-02 Scheduler domains code by Nick Piggin
  19. */
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/nmi.h>
  23. #include <linux/init.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/highmem.h>
  26. #include <linux/smp_lock.h>
  27. #include <asm/mmu_context.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/completion.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/security.h>
  32. #include <linux/notifier.h>
  33. #include <linux/profile.h>
  34. #include <linux/suspend.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/delay.h>
  37. #include <linux/smp.h>
  38. #include <linux/threads.h>
  39. #include <linux/timer.h>
  40. #include <linux/rcupdate.h>
  41. #include <linux/cpu.h>
  42. #include <linux/cpuset.h>
  43. #include <linux/percpu.h>
  44. #include <linux/kthread.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/syscalls.h>
  47. #include <linux/times.h>
  48. #include <linux/acct.h>
  49. #include <asm/tlb.h>
  50. #include <asm/unistd.h>
  51. /*
  52. * Convert user-nice values [ -20 ... 0 ... 19 ]
  53. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  54. * and back.
  55. */
  56. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  57. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  58. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  59. /*
  60. * 'User priority' is the nice value converted to something we
  61. * can work with better when scaling various scheduler parameters,
  62. * it's a [ 0 ... 39 ] range.
  63. */
  64. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  65. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  66. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  67. /*
  68. * Some helpers for converting nanosecond timing to jiffy resolution
  69. */
  70. #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
  71. #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
  72. /*
  73. * These are the 'tuning knobs' of the scheduler:
  74. *
  75. * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger),
  76. * default timeslice is 100 msecs, maximum timeslice is 800 msecs.
  77. * Timeslices get refilled after they expire.
  78. */
  79. #define MIN_TIMESLICE max(5 * HZ / 1000, 1)
  80. #define DEF_TIMESLICE (100 * HZ / 1000)
  81. #define ON_RUNQUEUE_WEIGHT 30
  82. #define CHILD_PENALTY 95
  83. #define PARENT_PENALTY 100
  84. #define EXIT_WEIGHT 3
  85. #define PRIO_BONUS_RATIO 25
  86. #define MAX_BONUS (MAX_USER_PRIO * PRIO_BONUS_RATIO / 100)
  87. #define INTERACTIVE_DELTA 2
  88. #define MAX_SLEEP_AVG (DEF_TIMESLICE * MAX_BONUS)
  89. #define STARVATION_LIMIT (MAX_SLEEP_AVG)
  90. #define NS_MAX_SLEEP_AVG (JIFFIES_TO_NS(MAX_SLEEP_AVG))
  91. /*
  92. * If a task is 'interactive' then we reinsert it in the active
  93. * array after it has expired its current timeslice. (it will not
  94. * continue to run immediately, it will still roundrobin with
  95. * other interactive tasks.)
  96. *
  97. * This part scales the interactivity limit depending on niceness.
  98. *
  99. * We scale it linearly, offset by the INTERACTIVE_DELTA delta.
  100. * Here are a few examples of different nice levels:
  101. *
  102. * TASK_INTERACTIVE(-20): [1,1,1,1,1,1,1,1,1,0,0]
  103. * TASK_INTERACTIVE(-10): [1,1,1,1,1,1,1,0,0,0,0]
  104. * TASK_INTERACTIVE( 0): [1,1,1,1,0,0,0,0,0,0,0]
  105. * TASK_INTERACTIVE( 10): [1,1,0,0,0,0,0,0,0,0,0]
  106. * TASK_INTERACTIVE( 19): [0,0,0,0,0,0,0,0,0,0,0]
  107. *
  108. * (the X axis represents the possible -5 ... 0 ... +5 dynamic
  109. * priority range a task can explore, a value of '1' means the
  110. * task is rated interactive.)
  111. *
  112. * Ie. nice +19 tasks can never get 'interactive' enough to be
  113. * reinserted into the active array. And only heavily CPU-hog nice -20
  114. * tasks will be expired. Default nice 0 tasks are somewhere between,
  115. * it takes some effort for them to get interactive, but it's not
  116. * too hard.
  117. */
  118. #define CURRENT_BONUS(p) \
  119. (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \
  120. MAX_SLEEP_AVG)
  121. #define GRANULARITY (10 * HZ / 1000 ? : 1)
  122. #ifdef CONFIG_SMP
  123. #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \
  124. (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)) * \
  125. num_online_cpus())
  126. #else
  127. #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \
  128. (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)))
  129. #endif
  130. #define SCALE(v1,v1_max,v2_max) \
  131. (v1) * (v2_max) / (v1_max)
  132. #define DELTA(p) \
  133. (SCALE(TASK_NICE(p), 40, MAX_BONUS) + INTERACTIVE_DELTA)
  134. #define TASK_INTERACTIVE(p) \
  135. ((p)->prio <= (p)->static_prio - DELTA(p))
  136. #define INTERACTIVE_SLEEP(p) \
  137. (JIFFIES_TO_NS(MAX_SLEEP_AVG * \
  138. (MAX_BONUS / 2 + DELTA((p)) + 1) / MAX_BONUS - 1))
  139. #define TASK_PREEMPTS_CURR(p, rq) \
  140. ((p)->prio < (rq)->curr->prio)
  141. /*
  142. * task_timeslice() scales user-nice values [ -20 ... 0 ... 19 ]
  143. * to time slice values: [800ms ... 100ms ... 5ms]
  144. *
  145. * The higher a thread's priority, the bigger timeslices
  146. * it gets during one round of execution. But even the lowest
  147. * priority thread gets MIN_TIMESLICE worth of execution time.
  148. */
  149. #define SCALE_PRIO(x, prio) \
  150. max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO/2), MIN_TIMESLICE)
  151. static unsigned int task_timeslice(task_t *p)
  152. {
  153. if (p->static_prio < NICE_TO_PRIO(0))
  154. return SCALE_PRIO(DEF_TIMESLICE*4, p->static_prio);
  155. else
  156. return SCALE_PRIO(DEF_TIMESLICE, p->static_prio);
  157. }
  158. #define task_hot(p, now, sd) ((long long) ((now) - (p)->last_ran) \
  159. < (long long) (sd)->cache_hot_time)
  160. void __put_task_struct_cb(struct rcu_head *rhp)
  161. {
  162. __put_task_struct(container_of(rhp, struct task_struct, rcu));
  163. }
  164. EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  165. /*
  166. * These are the runqueue data structures:
  167. */
  168. #define BITMAP_SIZE ((((MAX_PRIO+1+7)/8)+sizeof(long)-1)/sizeof(long))
  169. typedef struct runqueue runqueue_t;
  170. struct prio_array {
  171. unsigned int nr_active;
  172. unsigned long bitmap[BITMAP_SIZE];
  173. struct list_head queue[MAX_PRIO];
  174. };
  175. /*
  176. * This is the main, per-CPU runqueue data structure.
  177. *
  178. * Locking rule: those places that want to lock multiple runqueues
  179. * (such as the load balancing or the thread migration code), lock
  180. * acquire operations must be ordered by ascending &runqueue.
  181. */
  182. struct runqueue {
  183. spinlock_t lock;
  184. /*
  185. * nr_running and cpu_load should be in the same cacheline because
  186. * remote CPUs use both these fields when doing load calculation.
  187. */
  188. unsigned long nr_running;
  189. #ifdef CONFIG_SMP
  190. unsigned long prio_bias;
  191. unsigned long cpu_load[3];
  192. #endif
  193. unsigned long long nr_switches;
  194. /*
  195. * This is part of a global counter where only the total sum
  196. * over all CPUs matters. A task can increase this counter on
  197. * one CPU and if it got migrated afterwards it may decrease
  198. * it on another CPU. Always updated under the runqueue lock:
  199. */
  200. unsigned long nr_uninterruptible;
  201. unsigned long expired_timestamp;
  202. unsigned long long timestamp_last_tick;
  203. task_t *curr, *idle;
  204. struct mm_struct *prev_mm;
  205. prio_array_t *active, *expired, arrays[2];
  206. int best_expired_prio;
  207. atomic_t nr_iowait;
  208. #ifdef CONFIG_SMP
  209. struct sched_domain *sd;
  210. /* For active balancing */
  211. int active_balance;
  212. int push_cpu;
  213. task_t *migration_thread;
  214. struct list_head migration_queue;
  215. #endif
  216. #ifdef CONFIG_SCHEDSTATS
  217. /* latency stats */
  218. struct sched_info rq_sched_info;
  219. /* sys_sched_yield() stats */
  220. unsigned long yld_exp_empty;
  221. unsigned long yld_act_empty;
  222. unsigned long yld_both_empty;
  223. unsigned long yld_cnt;
  224. /* schedule() stats */
  225. unsigned long sched_switch;
  226. unsigned long sched_cnt;
  227. unsigned long sched_goidle;
  228. /* try_to_wake_up() stats */
  229. unsigned long ttwu_cnt;
  230. unsigned long ttwu_local;
  231. #endif
  232. };
  233. static DEFINE_PER_CPU(struct runqueue, runqueues);
  234. /*
  235. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  236. * See detach_destroy_domains: synchronize_sched for details.
  237. *
  238. * The domain tree of any CPU may only be accessed from within
  239. * preempt-disabled sections.
  240. */
  241. #define for_each_domain(cpu, domain) \
  242. for (domain = rcu_dereference(cpu_rq(cpu)->sd); domain; domain = domain->parent)
  243. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  244. #define this_rq() (&__get_cpu_var(runqueues))
  245. #define task_rq(p) cpu_rq(task_cpu(p))
  246. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  247. #ifndef prepare_arch_switch
  248. # define prepare_arch_switch(next) do { } while (0)
  249. #endif
  250. #ifndef finish_arch_switch
  251. # define finish_arch_switch(prev) do { } while (0)
  252. #endif
  253. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  254. static inline int task_running(runqueue_t *rq, task_t *p)
  255. {
  256. return rq->curr == p;
  257. }
  258. static inline void prepare_lock_switch(runqueue_t *rq, task_t *next)
  259. {
  260. }
  261. static inline void finish_lock_switch(runqueue_t *rq, task_t *prev)
  262. {
  263. #ifdef CONFIG_DEBUG_SPINLOCK
  264. /* this is a valid case when another task releases the spinlock */
  265. rq->lock.owner = current;
  266. #endif
  267. spin_unlock_irq(&rq->lock);
  268. }
  269. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  270. static inline int task_running(runqueue_t *rq, task_t *p)
  271. {
  272. #ifdef CONFIG_SMP
  273. return p->oncpu;
  274. #else
  275. return rq->curr == p;
  276. #endif
  277. }
  278. static inline void prepare_lock_switch(runqueue_t *rq, task_t *next)
  279. {
  280. #ifdef CONFIG_SMP
  281. /*
  282. * We can optimise this out completely for !SMP, because the
  283. * SMP rebalancing from interrupt is the only thing that cares
  284. * here.
  285. */
  286. next->oncpu = 1;
  287. #endif
  288. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  289. spin_unlock_irq(&rq->lock);
  290. #else
  291. spin_unlock(&rq->lock);
  292. #endif
  293. }
  294. static inline void finish_lock_switch(runqueue_t *rq, task_t *prev)
  295. {
  296. #ifdef CONFIG_SMP
  297. /*
  298. * After ->oncpu is cleared, the task can be moved to a different CPU.
  299. * We must ensure this doesn't happen until the switch is completely
  300. * finished.
  301. */
  302. smp_wmb();
  303. prev->oncpu = 0;
  304. #endif
  305. #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  306. local_irq_enable();
  307. #endif
  308. }
  309. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  310. /*
  311. * task_rq_lock - lock the runqueue a given task resides on and disable
  312. * interrupts. Note the ordering: we can safely lookup the task_rq without
  313. * explicitly disabling preemption.
  314. */
  315. static inline runqueue_t *task_rq_lock(task_t *p, unsigned long *flags)
  316. __acquires(rq->lock)
  317. {
  318. struct runqueue *rq;
  319. repeat_lock_task:
  320. local_irq_save(*flags);
  321. rq = task_rq(p);
  322. spin_lock(&rq->lock);
  323. if (unlikely(rq != task_rq(p))) {
  324. spin_unlock_irqrestore(&rq->lock, *flags);
  325. goto repeat_lock_task;
  326. }
  327. return rq;
  328. }
  329. static inline void task_rq_unlock(runqueue_t *rq, unsigned long *flags)
  330. __releases(rq->lock)
  331. {
  332. spin_unlock_irqrestore(&rq->lock, *flags);
  333. }
  334. #ifdef CONFIG_SCHEDSTATS
  335. /*
  336. * bump this up when changing the output format or the meaning of an existing
  337. * format, so that tools can adapt (or abort)
  338. */
  339. #define SCHEDSTAT_VERSION 12
  340. static int show_schedstat(struct seq_file *seq, void *v)
  341. {
  342. int cpu;
  343. seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
  344. seq_printf(seq, "timestamp %lu\n", jiffies);
  345. for_each_online_cpu(cpu) {
  346. runqueue_t *rq = cpu_rq(cpu);
  347. #ifdef CONFIG_SMP
  348. struct sched_domain *sd;
  349. int dcnt = 0;
  350. #endif
  351. /* runqueue-specific stats */
  352. seq_printf(seq,
  353. "cpu%d %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
  354. cpu, rq->yld_both_empty,
  355. rq->yld_act_empty, rq->yld_exp_empty, rq->yld_cnt,
  356. rq->sched_switch, rq->sched_cnt, rq->sched_goidle,
  357. rq->ttwu_cnt, rq->ttwu_local,
  358. rq->rq_sched_info.cpu_time,
  359. rq->rq_sched_info.run_delay, rq->rq_sched_info.pcnt);
  360. seq_printf(seq, "\n");
  361. #ifdef CONFIG_SMP
  362. /* domain-specific stats */
  363. preempt_disable();
  364. for_each_domain(cpu, sd) {
  365. enum idle_type itype;
  366. char mask_str[NR_CPUS];
  367. cpumask_scnprintf(mask_str, NR_CPUS, sd->span);
  368. seq_printf(seq, "domain%d %s", dcnt++, mask_str);
  369. for (itype = SCHED_IDLE; itype < MAX_IDLE_TYPES;
  370. itype++) {
  371. seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu",
  372. sd->lb_cnt[itype],
  373. sd->lb_balanced[itype],
  374. sd->lb_failed[itype],
  375. sd->lb_imbalance[itype],
  376. sd->lb_gained[itype],
  377. sd->lb_hot_gained[itype],
  378. sd->lb_nobusyq[itype],
  379. sd->lb_nobusyg[itype]);
  380. }
  381. seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
  382. sd->alb_cnt, sd->alb_failed, sd->alb_pushed,
  383. sd->sbe_cnt, sd->sbe_balanced, sd->sbe_pushed,
  384. sd->sbf_cnt, sd->sbf_balanced, sd->sbf_pushed,
  385. sd->ttwu_wake_remote, sd->ttwu_move_affine, sd->ttwu_move_balance);
  386. }
  387. preempt_enable();
  388. #endif
  389. }
  390. return 0;
  391. }
  392. static int schedstat_open(struct inode *inode, struct file *file)
  393. {
  394. unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32);
  395. char *buf = kmalloc(size, GFP_KERNEL);
  396. struct seq_file *m;
  397. int res;
  398. if (!buf)
  399. return -ENOMEM;
  400. res = single_open(file, show_schedstat, NULL);
  401. if (!res) {
  402. m = file->private_data;
  403. m->buf = buf;
  404. m->size = size;
  405. } else
  406. kfree(buf);
  407. return res;
  408. }
  409. struct file_operations proc_schedstat_operations = {
  410. .open = schedstat_open,
  411. .read = seq_read,
  412. .llseek = seq_lseek,
  413. .release = single_release,
  414. };
  415. # define schedstat_inc(rq, field) do { (rq)->field++; } while (0)
  416. # define schedstat_add(rq, field, amt) do { (rq)->field += (amt); } while (0)
  417. #else /* !CONFIG_SCHEDSTATS */
  418. # define schedstat_inc(rq, field) do { } while (0)
  419. # define schedstat_add(rq, field, amt) do { } while (0)
  420. #endif
  421. /*
  422. * rq_lock - lock a given runqueue and disable interrupts.
  423. */
  424. static inline runqueue_t *this_rq_lock(void)
  425. __acquires(rq->lock)
  426. {
  427. runqueue_t *rq;
  428. local_irq_disable();
  429. rq = this_rq();
  430. spin_lock(&rq->lock);
  431. return rq;
  432. }
  433. #ifdef CONFIG_SCHEDSTATS
  434. /*
  435. * Called when a process is dequeued from the active array and given
  436. * the cpu. We should note that with the exception of interactive
  437. * tasks, the expired queue will become the active queue after the active
  438. * queue is empty, without explicitly dequeuing and requeuing tasks in the
  439. * expired queue. (Interactive tasks may be requeued directly to the
  440. * active queue, thus delaying tasks in the expired queue from running;
  441. * see scheduler_tick()).
  442. *
  443. * This function is only called from sched_info_arrive(), rather than
  444. * dequeue_task(). Even though a task may be queued and dequeued multiple
  445. * times as it is shuffled about, we're really interested in knowing how
  446. * long it was from the *first* time it was queued to the time that it
  447. * finally hit a cpu.
  448. */
  449. static inline void sched_info_dequeued(task_t *t)
  450. {
  451. t->sched_info.last_queued = 0;
  452. }
  453. /*
  454. * Called when a task finally hits the cpu. We can now calculate how
  455. * long it was waiting to run. We also note when it began so that we
  456. * can keep stats on how long its timeslice is.
  457. */
  458. static inline void sched_info_arrive(task_t *t)
  459. {
  460. unsigned long now = jiffies, diff = 0;
  461. struct runqueue *rq = task_rq(t);
  462. if (t->sched_info.last_queued)
  463. diff = now - t->sched_info.last_queued;
  464. sched_info_dequeued(t);
  465. t->sched_info.run_delay += diff;
  466. t->sched_info.last_arrival = now;
  467. t->sched_info.pcnt++;
  468. if (!rq)
  469. return;
  470. rq->rq_sched_info.run_delay += diff;
  471. rq->rq_sched_info.pcnt++;
  472. }
  473. /*
  474. * Called when a process is queued into either the active or expired
  475. * array. The time is noted and later used to determine how long we
  476. * had to wait for us to reach the cpu. Since the expired queue will
  477. * become the active queue after active queue is empty, without dequeuing
  478. * and requeuing any tasks, we are interested in queuing to either. It
  479. * is unusual but not impossible for tasks to be dequeued and immediately
  480. * requeued in the same or another array: this can happen in sched_yield(),
  481. * set_user_nice(), and even load_balance() as it moves tasks from runqueue
  482. * to runqueue.
  483. *
  484. * This function is only called from enqueue_task(), but also only updates
  485. * the timestamp if it is already not set. It's assumed that
  486. * sched_info_dequeued() will clear that stamp when appropriate.
  487. */
  488. static inline void sched_info_queued(task_t *t)
  489. {
  490. if (!t->sched_info.last_queued)
  491. t->sched_info.last_queued = jiffies;
  492. }
  493. /*
  494. * Called when a process ceases being the active-running process, either
  495. * voluntarily or involuntarily. Now we can calculate how long we ran.
  496. */
  497. static inline void sched_info_depart(task_t *t)
  498. {
  499. struct runqueue *rq = task_rq(t);
  500. unsigned long diff = jiffies - t->sched_info.last_arrival;
  501. t->sched_info.cpu_time += diff;
  502. if (rq)
  503. rq->rq_sched_info.cpu_time += diff;
  504. }
  505. /*
  506. * Called when tasks are switched involuntarily due, typically, to expiring
  507. * their time slice. (This may also be called when switching to or from
  508. * the idle task.) We are only called when prev != next.
  509. */
  510. static inline void sched_info_switch(task_t *prev, task_t *next)
  511. {
  512. struct runqueue *rq = task_rq(prev);
  513. /*
  514. * prev now departs the cpu. It's not interesting to record
  515. * stats about how efficient we were at scheduling the idle
  516. * process, however.
  517. */
  518. if (prev != rq->idle)
  519. sched_info_depart(prev);
  520. if (next != rq->idle)
  521. sched_info_arrive(next);
  522. }
  523. #else
  524. #define sched_info_queued(t) do { } while (0)
  525. #define sched_info_switch(t, next) do { } while (0)
  526. #endif /* CONFIG_SCHEDSTATS */
  527. /*
  528. * Adding/removing a task to/from a priority array:
  529. */
  530. static void dequeue_task(struct task_struct *p, prio_array_t *array)
  531. {
  532. array->nr_active--;
  533. list_del(&p->run_list);
  534. if (list_empty(array->queue + p->prio))
  535. __clear_bit(p->prio, array->bitmap);
  536. }
  537. static void enqueue_task(struct task_struct *p, prio_array_t *array)
  538. {
  539. sched_info_queued(p);
  540. list_add_tail(&p->run_list, array->queue + p->prio);
  541. __set_bit(p->prio, array->bitmap);
  542. array->nr_active++;
  543. p->array = array;
  544. }
  545. /*
  546. * Put task to the end of the run list without the overhead of dequeue
  547. * followed by enqueue.
  548. */
  549. static void requeue_task(struct task_struct *p, prio_array_t *array)
  550. {
  551. list_move_tail(&p->run_list, array->queue + p->prio);
  552. }
  553. static inline void enqueue_task_head(struct task_struct *p, prio_array_t *array)
  554. {
  555. list_add(&p->run_list, array->queue + p->prio);
  556. __set_bit(p->prio, array->bitmap);
  557. array->nr_active++;
  558. p->array = array;
  559. }
  560. /*
  561. * effective_prio - return the priority that is based on the static
  562. * priority but is modified by bonuses/penalties.
  563. *
  564. * We scale the actual sleep average [0 .... MAX_SLEEP_AVG]
  565. * into the -5 ... 0 ... +5 bonus/penalty range.
  566. *
  567. * We use 25% of the full 0...39 priority range so that:
  568. *
  569. * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs.
  570. * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks.
  571. *
  572. * Both properties are important to certain workloads.
  573. */
  574. static int effective_prio(task_t *p)
  575. {
  576. int bonus, prio;
  577. if (rt_task(p))
  578. return p->prio;
  579. bonus = CURRENT_BONUS(p) - MAX_BONUS / 2;
  580. prio = p->static_prio - bonus;
  581. if (prio < MAX_RT_PRIO)
  582. prio = MAX_RT_PRIO;
  583. if (prio > MAX_PRIO-1)
  584. prio = MAX_PRIO-1;
  585. return prio;
  586. }
  587. #ifdef CONFIG_SMP
  588. static inline void inc_prio_bias(runqueue_t *rq, int prio)
  589. {
  590. rq->prio_bias += MAX_PRIO - prio;
  591. }
  592. static inline void dec_prio_bias(runqueue_t *rq, int prio)
  593. {
  594. rq->prio_bias -= MAX_PRIO - prio;
  595. }
  596. static inline void inc_nr_running(task_t *p, runqueue_t *rq)
  597. {
  598. rq->nr_running++;
  599. if (rt_task(p)) {
  600. if (p != rq->migration_thread)
  601. /*
  602. * The migration thread does the actual balancing. Do
  603. * not bias by its priority as the ultra high priority
  604. * will skew balancing adversely.
  605. */
  606. inc_prio_bias(rq, p->prio);
  607. } else
  608. inc_prio_bias(rq, p->static_prio);
  609. }
  610. static inline void dec_nr_running(task_t *p, runqueue_t *rq)
  611. {
  612. rq->nr_running--;
  613. if (rt_task(p)) {
  614. if (p != rq->migration_thread)
  615. dec_prio_bias(rq, p->prio);
  616. } else
  617. dec_prio_bias(rq, p->static_prio);
  618. }
  619. #else
  620. static inline void inc_prio_bias(runqueue_t *rq, int prio)
  621. {
  622. }
  623. static inline void dec_prio_bias(runqueue_t *rq, int prio)
  624. {
  625. }
  626. static inline void inc_nr_running(task_t *p, runqueue_t *rq)
  627. {
  628. rq->nr_running++;
  629. }
  630. static inline void dec_nr_running(task_t *p, runqueue_t *rq)
  631. {
  632. rq->nr_running--;
  633. }
  634. #endif
  635. /*
  636. * __activate_task - move a task to the runqueue.
  637. */
  638. static inline void __activate_task(task_t *p, runqueue_t *rq)
  639. {
  640. enqueue_task(p, rq->active);
  641. inc_nr_running(p, rq);
  642. }
  643. /*
  644. * __activate_idle_task - move idle task to the _front_ of runqueue.
  645. */
  646. static inline void __activate_idle_task(task_t *p, runqueue_t *rq)
  647. {
  648. enqueue_task_head(p, rq->active);
  649. inc_nr_running(p, rq);
  650. }
  651. static int recalc_task_prio(task_t *p, unsigned long long now)
  652. {
  653. /* Caller must always ensure 'now >= p->timestamp' */
  654. unsigned long long __sleep_time = now - p->timestamp;
  655. unsigned long sleep_time;
  656. if (__sleep_time > NS_MAX_SLEEP_AVG)
  657. sleep_time = NS_MAX_SLEEP_AVG;
  658. else
  659. sleep_time = (unsigned long)__sleep_time;
  660. if (likely(sleep_time > 0)) {
  661. /*
  662. * User tasks that sleep a long time are categorised as
  663. * idle and will get just interactive status to stay active &
  664. * prevent them suddenly becoming cpu hogs and starving
  665. * other processes.
  666. */
  667. if (p->mm && p->activated != -1 &&
  668. sleep_time > INTERACTIVE_SLEEP(p)) {
  669. p->sleep_avg = JIFFIES_TO_NS(MAX_SLEEP_AVG -
  670. DEF_TIMESLICE);
  671. } else {
  672. /*
  673. * The lower the sleep avg a task has the more
  674. * rapidly it will rise with sleep time.
  675. */
  676. sleep_time *= (MAX_BONUS - CURRENT_BONUS(p)) ? : 1;
  677. /*
  678. * Tasks waking from uninterruptible sleep are
  679. * limited in their sleep_avg rise as they
  680. * are likely to be waiting on I/O
  681. */
  682. if (p->activated == -1 && p->mm) {
  683. if (p->sleep_avg >= INTERACTIVE_SLEEP(p))
  684. sleep_time = 0;
  685. else if (p->sleep_avg + sleep_time >=
  686. INTERACTIVE_SLEEP(p)) {
  687. p->sleep_avg = INTERACTIVE_SLEEP(p);
  688. sleep_time = 0;
  689. }
  690. }
  691. /*
  692. * This code gives a bonus to interactive tasks.
  693. *
  694. * The boost works by updating the 'average sleep time'
  695. * value here, based on ->timestamp. The more time a
  696. * task spends sleeping, the higher the average gets -
  697. * and the higher the priority boost gets as well.
  698. */
  699. p->sleep_avg += sleep_time;
  700. if (p->sleep_avg > NS_MAX_SLEEP_AVG)
  701. p->sleep_avg = NS_MAX_SLEEP_AVG;
  702. }
  703. }
  704. return effective_prio(p);
  705. }
  706. /*
  707. * activate_task - move a task to the runqueue and do priority recalculation
  708. *
  709. * Update all the scheduling statistics stuff. (sleep average
  710. * calculation, priority modifiers, etc.)
  711. */
  712. static void activate_task(task_t *p, runqueue_t *rq, int local)
  713. {
  714. unsigned long long now;
  715. now = sched_clock();
  716. #ifdef CONFIG_SMP
  717. if (!local) {
  718. /* Compensate for drifting sched_clock */
  719. runqueue_t *this_rq = this_rq();
  720. now = (now - this_rq->timestamp_last_tick)
  721. + rq->timestamp_last_tick;
  722. }
  723. #endif
  724. if (!rt_task(p))
  725. p->prio = recalc_task_prio(p, now);
  726. /*
  727. * This checks to make sure it's not an uninterruptible task
  728. * that is now waking up.
  729. */
  730. if (!p->activated) {
  731. /*
  732. * Tasks which were woken up by interrupts (ie. hw events)
  733. * are most likely of interactive nature. So we give them
  734. * the credit of extending their sleep time to the period
  735. * of time they spend on the runqueue, waiting for execution
  736. * on a CPU, first time around:
  737. */
  738. if (in_interrupt())
  739. p->activated = 2;
  740. else {
  741. /*
  742. * Normal first-time wakeups get a credit too for
  743. * on-runqueue time, but it will be weighted down:
  744. */
  745. p->activated = 1;
  746. }
  747. }
  748. p->timestamp = now;
  749. __activate_task(p, rq);
  750. }
  751. /*
  752. * deactivate_task - remove a task from the runqueue.
  753. */
  754. static void deactivate_task(struct task_struct *p, runqueue_t *rq)
  755. {
  756. dec_nr_running(p, rq);
  757. dequeue_task(p, p->array);
  758. p->array = NULL;
  759. }
  760. /*
  761. * resched_task - mark a task 'to be rescheduled now'.
  762. *
  763. * On UP this means the setting of the need_resched flag, on SMP it
  764. * might also involve a cross-CPU call to trigger the scheduler on
  765. * the target CPU.
  766. */
  767. #ifdef CONFIG_SMP
  768. static void resched_task(task_t *p)
  769. {
  770. int cpu;
  771. assert_spin_locked(&task_rq(p)->lock);
  772. if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
  773. return;
  774. set_tsk_thread_flag(p, TIF_NEED_RESCHED);
  775. cpu = task_cpu(p);
  776. if (cpu == smp_processor_id())
  777. return;
  778. /* NEED_RESCHED must be visible before we test POLLING_NRFLAG */
  779. smp_mb();
  780. if (!test_tsk_thread_flag(p, TIF_POLLING_NRFLAG))
  781. smp_send_reschedule(cpu);
  782. }
  783. #else
  784. static inline void resched_task(task_t *p)
  785. {
  786. assert_spin_locked(&task_rq(p)->lock);
  787. set_tsk_need_resched(p);
  788. }
  789. #endif
  790. /**
  791. * task_curr - is this task currently executing on a CPU?
  792. * @p: the task in question.
  793. */
  794. inline int task_curr(const task_t *p)
  795. {
  796. return cpu_curr(task_cpu(p)) == p;
  797. }
  798. #ifdef CONFIG_SMP
  799. typedef struct {
  800. struct list_head list;
  801. task_t *task;
  802. int dest_cpu;
  803. struct completion done;
  804. } migration_req_t;
  805. /*
  806. * The task's runqueue lock must be held.
  807. * Returns true if you have to wait for migration thread.
  808. */
  809. static int migrate_task(task_t *p, int dest_cpu, migration_req_t *req)
  810. {
  811. runqueue_t *rq = task_rq(p);
  812. /*
  813. * If the task is not on a runqueue (and not running), then
  814. * it is sufficient to simply update the task's cpu field.
  815. */
  816. if (!p->array && !task_running(rq, p)) {
  817. set_task_cpu(p, dest_cpu);
  818. return 0;
  819. }
  820. init_completion(&req->done);
  821. req->task = p;
  822. req->dest_cpu = dest_cpu;
  823. list_add(&req->list, &rq->migration_queue);
  824. return 1;
  825. }
  826. /*
  827. * wait_task_inactive - wait for a thread to unschedule.
  828. *
  829. * The caller must ensure that the task *will* unschedule sometime soon,
  830. * else this function might spin for a *long* time. This function can't
  831. * be called with interrupts off, or it may introduce deadlock with
  832. * smp_call_function() if an IPI is sent by the same process we are
  833. * waiting to become inactive.
  834. */
  835. void wait_task_inactive(task_t *p)
  836. {
  837. unsigned long flags;
  838. runqueue_t *rq;
  839. int preempted;
  840. repeat:
  841. rq = task_rq_lock(p, &flags);
  842. /* Must be off runqueue entirely, not preempted. */
  843. if (unlikely(p->array || task_running(rq, p))) {
  844. /* If it's preempted, we yield. It could be a while. */
  845. preempted = !task_running(rq, p);
  846. task_rq_unlock(rq, &flags);
  847. cpu_relax();
  848. if (preempted)
  849. yield();
  850. goto repeat;
  851. }
  852. task_rq_unlock(rq, &flags);
  853. }
  854. /***
  855. * kick_process - kick a running thread to enter/exit the kernel
  856. * @p: the to-be-kicked thread
  857. *
  858. * Cause a process which is running on another CPU to enter
  859. * kernel-mode, without any delay. (to get signals handled.)
  860. *
  861. * NOTE: this function doesnt have to take the runqueue lock,
  862. * because all it wants to ensure is that the remote task enters
  863. * the kernel. If the IPI races and the task has been migrated
  864. * to another CPU then no harm is done and the purpose has been
  865. * achieved as well.
  866. */
  867. void kick_process(task_t *p)
  868. {
  869. int cpu;
  870. preempt_disable();
  871. cpu = task_cpu(p);
  872. if ((cpu != smp_processor_id()) && task_curr(p))
  873. smp_send_reschedule(cpu);
  874. preempt_enable();
  875. }
  876. /*
  877. * Return a low guess at the load of a migration-source cpu.
  878. *
  879. * We want to under-estimate the load of migration sources, to
  880. * balance conservatively.
  881. */
  882. static inline unsigned long __source_load(int cpu, int type, enum idle_type idle)
  883. {
  884. runqueue_t *rq = cpu_rq(cpu);
  885. unsigned long running = rq->nr_running;
  886. unsigned long source_load, cpu_load = rq->cpu_load[type-1],
  887. load_now = running * SCHED_LOAD_SCALE;
  888. if (type == 0)
  889. source_load = load_now;
  890. else
  891. source_load = min(cpu_load, load_now);
  892. if (running > 1 || (idle == NOT_IDLE && running))
  893. /*
  894. * If we are busy rebalancing the load is biased by
  895. * priority to create 'nice' support across cpus. When
  896. * idle rebalancing we should only bias the source_load if
  897. * there is more than one task running on that queue to
  898. * prevent idle rebalance from trying to pull tasks from a
  899. * queue with only one running task.
  900. */
  901. source_load = source_load * rq->prio_bias / running;
  902. return source_load;
  903. }
  904. static inline unsigned long source_load(int cpu, int type)
  905. {
  906. return __source_load(cpu, type, NOT_IDLE);
  907. }
  908. /*
  909. * Return a high guess at the load of a migration-target cpu
  910. */
  911. static inline unsigned long __target_load(int cpu, int type, enum idle_type idle)
  912. {
  913. runqueue_t *rq = cpu_rq(cpu);
  914. unsigned long running = rq->nr_running;
  915. unsigned long target_load, cpu_load = rq->cpu_load[type-1],
  916. load_now = running * SCHED_LOAD_SCALE;
  917. if (type == 0)
  918. target_load = load_now;
  919. else
  920. target_load = max(cpu_load, load_now);
  921. if (running > 1 || (idle == NOT_IDLE && running))
  922. target_load = target_load * rq->prio_bias / running;
  923. return target_load;
  924. }
  925. static inline unsigned long target_load(int cpu, int type)
  926. {
  927. return __target_load(cpu, type, NOT_IDLE);
  928. }
  929. /*
  930. * find_idlest_group finds and returns the least busy CPU group within the
  931. * domain.
  932. */
  933. static struct sched_group *
  934. find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
  935. {
  936. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  937. unsigned long min_load = ULONG_MAX, this_load = 0;
  938. int load_idx = sd->forkexec_idx;
  939. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  940. do {
  941. unsigned long load, avg_load;
  942. int local_group;
  943. int i;
  944. /* Skip over this group if it has no CPUs allowed */
  945. if (!cpus_intersects(group->cpumask, p->cpus_allowed))
  946. goto nextgroup;
  947. local_group = cpu_isset(this_cpu, group->cpumask);
  948. /* Tally up the load of all CPUs in the group */
  949. avg_load = 0;
  950. for_each_cpu_mask(i, group->cpumask) {
  951. /* Bias balancing toward cpus of our domain */
  952. if (local_group)
  953. load = source_load(i, load_idx);
  954. else
  955. load = target_load(i, load_idx);
  956. avg_load += load;
  957. }
  958. /* Adjust by relative CPU power of the group */
  959. avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
  960. if (local_group) {
  961. this_load = avg_load;
  962. this = group;
  963. } else if (avg_load < min_load) {
  964. min_load = avg_load;
  965. idlest = group;
  966. }
  967. nextgroup:
  968. group = group->next;
  969. } while (group != sd->groups);
  970. if (!idlest || 100*this_load < imbalance*min_load)
  971. return NULL;
  972. return idlest;
  973. }
  974. /*
  975. * find_idlest_queue - find the idlest runqueue among the cpus in group.
  976. */
  977. static int
  978. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  979. {
  980. cpumask_t tmp;
  981. unsigned long load, min_load = ULONG_MAX;
  982. int idlest = -1;
  983. int i;
  984. /* Traverse only the allowed CPUs */
  985. cpus_and(tmp, group->cpumask, p->cpus_allowed);
  986. for_each_cpu_mask(i, tmp) {
  987. load = source_load(i, 0);
  988. if (load < min_load || (load == min_load && i == this_cpu)) {
  989. min_load = load;
  990. idlest = i;
  991. }
  992. }
  993. return idlest;
  994. }
  995. /*
  996. * sched_balance_self: balance the current task (running on cpu) in domains
  997. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  998. * SD_BALANCE_EXEC.
  999. *
  1000. * Balance, ie. select the least loaded group.
  1001. *
  1002. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1003. *
  1004. * preempt must be disabled.
  1005. */
  1006. static int sched_balance_self(int cpu, int flag)
  1007. {
  1008. struct task_struct *t = current;
  1009. struct sched_domain *tmp, *sd = NULL;
  1010. for_each_domain(cpu, tmp)
  1011. if (tmp->flags & flag)
  1012. sd = tmp;
  1013. while (sd) {
  1014. cpumask_t span;
  1015. struct sched_group *group;
  1016. int new_cpu;
  1017. int weight;
  1018. span = sd->span;
  1019. group = find_idlest_group(sd, t, cpu);
  1020. if (!group)
  1021. goto nextlevel;
  1022. new_cpu = find_idlest_cpu(group, t, cpu);
  1023. if (new_cpu == -1 || new_cpu == cpu)
  1024. goto nextlevel;
  1025. /* Now try balancing at a lower domain level */
  1026. cpu = new_cpu;
  1027. nextlevel:
  1028. sd = NULL;
  1029. weight = cpus_weight(span);
  1030. for_each_domain(cpu, tmp) {
  1031. if (weight <= cpus_weight(tmp->span))
  1032. break;
  1033. if (tmp->flags & flag)
  1034. sd = tmp;
  1035. }
  1036. /* while loop will break here if sd == NULL */
  1037. }
  1038. return cpu;
  1039. }
  1040. #endif /* CONFIG_SMP */
  1041. /*
  1042. * wake_idle() will wake a task on an idle cpu if task->cpu is
  1043. * not idle and an idle cpu is available. The span of cpus to
  1044. * search starts with cpus closest then further out as needed,
  1045. * so we always favor a closer, idle cpu.
  1046. *
  1047. * Returns the CPU we should wake onto.
  1048. */
  1049. #if defined(ARCH_HAS_SCHED_WAKE_IDLE)
  1050. static int wake_idle(int cpu, task_t *p)
  1051. {
  1052. cpumask_t tmp;
  1053. struct sched_domain *sd;
  1054. int i;
  1055. if (idle_cpu(cpu))
  1056. return cpu;
  1057. for_each_domain(cpu, sd) {
  1058. if (sd->flags & SD_WAKE_IDLE) {
  1059. cpus_and(tmp, sd->span, p->cpus_allowed);
  1060. for_each_cpu_mask(i, tmp) {
  1061. if (idle_cpu(i))
  1062. return i;
  1063. }
  1064. }
  1065. else
  1066. break;
  1067. }
  1068. return cpu;
  1069. }
  1070. #else
  1071. static inline int wake_idle(int cpu, task_t *p)
  1072. {
  1073. return cpu;
  1074. }
  1075. #endif
  1076. /***
  1077. * try_to_wake_up - wake up a thread
  1078. * @p: the to-be-woken-up thread
  1079. * @state: the mask of task states that can be woken
  1080. * @sync: do a synchronous wakeup?
  1081. *
  1082. * Put it on the run-queue if it's not already there. The "current"
  1083. * thread is always on the run-queue (except when the actual
  1084. * re-schedule is in progress), and as such you're allowed to do
  1085. * the simpler "current->state = TASK_RUNNING" to mark yourself
  1086. * runnable without the overhead of this.
  1087. *
  1088. * returns failure only if the task is already active.
  1089. */
  1090. static int try_to_wake_up(task_t *p, unsigned int state, int sync)
  1091. {
  1092. int cpu, this_cpu, success = 0;
  1093. unsigned long flags;
  1094. long old_state;
  1095. runqueue_t *rq;
  1096. #ifdef CONFIG_SMP
  1097. unsigned long load, this_load;
  1098. struct sched_domain *sd, *this_sd = NULL;
  1099. int new_cpu;
  1100. #endif
  1101. rq = task_rq_lock(p, &flags);
  1102. old_state = p->state;
  1103. if (!(old_state & state))
  1104. goto out;
  1105. if (p->array)
  1106. goto out_running;
  1107. cpu = task_cpu(p);
  1108. this_cpu = smp_processor_id();
  1109. #ifdef CONFIG_SMP
  1110. if (unlikely(task_running(rq, p)))
  1111. goto out_activate;
  1112. new_cpu = cpu;
  1113. schedstat_inc(rq, ttwu_cnt);
  1114. if (cpu == this_cpu) {
  1115. schedstat_inc(rq, ttwu_local);
  1116. goto out_set_cpu;
  1117. }
  1118. for_each_domain(this_cpu, sd) {
  1119. if (cpu_isset(cpu, sd->span)) {
  1120. schedstat_inc(sd, ttwu_wake_remote);
  1121. this_sd = sd;
  1122. break;
  1123. }
  1124. }
  1125. if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
  1126. goto out_set_cpu;
  1127. /*
  1128. * Check for affine wakeup and passive balancing possibilities.
  1129. */
  1130. if (this_sd) {
  1131. int idx = this_sd->wake_idx;
  1132. unsigned int imbalance;
  1133. imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
  1134. load = source_load(cpu, idx);
  1135. this_load = target_load(this_cpu, idx);
  1136. new_cpu = this_cpu; /* Wake to this CPU if we can */
  1137. if (this_sd->flags & SD_WAKE_AFFINE) {
  1138. unsigned long tl = this_load;
  1139. /*
  1140. * If sync wakeup then subtract the (maximum possible)
  1141. * effect of the currently running task from the load
  1142. * of the current CPU:
  1143. */
  1144. if (sync)
  1145. tl -= SCHED_LOAD_SCALE;
  1146. if ((tl <= load &&
  1147. tl + target_load(cpu, idx) <= SCHED_LOAD_SCALE) ||
  1148. 100*(tl + SCHED_LOAD_SCALE) <= imbalance*load) {
  1149. /*
  1150. * This domain has SD_WAKE_AFFINE and
  1151. * p is cache cold in this domain, and
  1152. * there is no bad imbalance.
  1153. */
  1154. schedstat_inc(this_sd, ttwu_move_affine);
  1155. goto out_set_cpu;
  1156. }
  1157. }
  1158. /*
  1159. * Start passive balancing when half the imbalance_pct
  1160. * limit is reached.
  1161. */
  1162. if (this_sd->flags & SD_WAKE_BALANCE) {
  1163. if (imbalance*this_load <= 100*load) {
  1164. schedstat_inc(this_sd, ttwu_move_balance);
  1165. goto out_set_cpu;
  1166. }
  1167. }
  1168. }
  1169. new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
  1170. out_set_cpu:
  1171. new_cpu = wake_idle(new_cpu, p);
  1172. if (new_cpu != cpu) {
  1173. set_task_cpu(p, new_cpu);
  1174. task_rq_unlock(rq, &flags);
  1175. /* might preempt at this point */
  1176. rq = task_rq_lock(p, &flags);
  1177. old_state = p->state;
  1178. if (!(old_state & state))
  1179. goto out;
  1180. if (p->array)
  1181. goto out_running;
  1182. this_cpu = smp_processor_id();
  1183. cpu = task_cpu(p);
  1184. }
  1185. out_activate:
  1186. #endif /* CONFIG_SMP */
  1187. if (old_state == TASK_UNINTERRUPTIBLE) {
  1188. rq->nr_uninterruptible--;
  1189. /*
  1190. * Tasks on involuntary sleep don't earn
  1191. * sleep_avg beyond just interactive state.
  1192. */
  1193. p->activated = -1;
  1194. }
  1195. /*
  1196. * Tasks that have marked their sleep as noninteractive get
  1197. * woken up without updating their sleep average. (i.e. their
  1198. * sleep is handled in a priority-neutral manner, no priority
  1199. * boost and no penalty.)
  1200. */
  1201. if (old_state & TASK_NONINTERACTIVE)
  1202. __activate_task(p, rq);
  1203. else
  1204. activate_task(p, rq, cpu == this_cpu);
  1205. /*
  1206. * Sync wakeups (i.e. those types of wakeups where the waker
  1207. * has indicated that it will leave the CPU in short order)
  1208. * don't trigger a preemption, if the woken up task will run on
  1209. * this cpu. (in this case the 'I will reschedule' promise of
  1210. * the waker guarantees that the freshly woken up task is going
  1211. * to be considered on this CPU.)
  1212. */
  1213. if (!sync || cpu != this_cpu) {
  1214. if (TASK_PREEMPTS_CURR(p, rq))
  1215. resched_task(rq->curr);
  1216. }
  1217. success = 1;
  1218. out_running:
  1219. p->state = TASK_RUNNING;
  1220. out:
  1221. task_rq_unlock(rq, &flags);
  1222. return success;
  1223. }
  1224. int fastcall wake_up_process(task_t *p)
  1225. {
  1226. return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED |
  1227. TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0);
  1228. }
  1229. EXPORT_SYMBOL(wake_up_process);
  1230. int fastcall wake_up_state(task_t *p, unsigned int state)
  1231. {
  1232. return try_to_wake_up(p, state, 0);
  1233. }
  1234. /*
  1235. * Perform scheduler related setup for a newly forked process p.
  1236. * p is forked by current.
  1237. */
  1238. void fastcall sched_fork(task_t *p, int clone_flags)
  1239. {
  1240. int cpu = get_cpu();
  1241. #ifdef CONFIG_SMP
  1242. cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
  1243. #endif
  1244. set_task_cpu(p, cpu);
  1245. /*
  1246. * We mark the process as running here, but have not actually
  1247. * inserted it onto the runqueue yet. This guarantees that
  1248. * nobody will actually run it, and a signal or other external
  1249. * event cannot wake it up and insert it on the runqueue either.
  1250. */
  1251. p->state = TASK_RUNNING;
  1252. INIT_LIST_HEAD(&p->run_list);
  1253. p->array = NULL;
  1254. #ifdef CONFIG_SCHEDSTATS
  1255. memset(&p->sched_info, 0, sizeof(p->sched_info));
  1256. #endif
  1257. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  1258. p->oncpu = 0;
  1259. #endif
  1260. #ifdef CONFIG_PREEMPT
  1261. /* Want to start with kernel preemption disabled. */
  1262. task_thread_info(p)->preempt_count = 1;
  1263. #endif
  1264. /*
  1265. * Share the timeslice between parent and child, thus the
  1266. * total amount of pending timeslices in the system doesn't change,
  1267. * resulting in more scheduling fairness.
  1268. */
  1269. local_irq_disable();
  1270. p->time_slice = (current->time_slice + 1) >> 1;
  1271. /*
  1272. * The remainder of the first timeslice might be recovered by
  1273. * the parent if the child exits early enough.
  1274. */
  1275. p->first_time_slice = 1;
  1276. current->time_slice >>= 1;
  1277. p->timestamp = sched_clock();
  1278. if (unlikely(!current->time_slice)) {
  1279. /*
  1280. * This case is rare, it happens when the parent has only
  1281. * a single jiffy left from its timeslice. Taking the
  1282. * runqueue lock is not a problem.
  1283. */
  1284. current->time_slice = 1;
  1285. scheduler_tick();
  1286. }
  1287. local_irq_enable();
  1288. put_cpu();
  1289. }
  1290. /*
  1291. * wake_up_new_task - wake up a newly created task for the first time.
  1292. *
  1293. * This function will do some initial scheduler statistics housekeeping
  1294. * that must be done for every newly created context, then puts the task
  1295. * on the runqueue and wakes it.
  1296. */
  1297. void fastcall wake_up_new_task(task_t *p, unsigned long clone_flags)
  1298. {
  1299. unsigned long flags;
  1300. int this_cpu, cpu;
  1301. runqueue_t *rq, *this_rq;
  1302. rq = task_rq_lock(p, &flags);
  1303. BUG_ON(p->state != TASK_RUNNING);
  1304. this_cpu = smp_processor_id();
  1305. cpu = task_cpu(p);
  1306. /*
  1307. * We decrease the sleep average of forking parents
  1308. * and children as well, to keep max-interactive tasks
  1309. * from forking tasks that are max-interactive. The parent
  1310. * (current) is done further down, under its lock.
  1311. */
  1312. p->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(p) *
  1313. CHILD_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS);
  1314. p->prio = effective_prio(p);
  1315. if (likely(cpu == this_cpu)) {
  1316. if (!(clone_flags & CLONE_VM)) {
  1317. /*
  1318. * The VM isn't cloned, so we're in a good position to
  1319. * do child-runs-first in anticipation of an exec. This
  1320. * usually avoids a lot of COW overhead.
  1321. */
  1322. if (unlikely(!current->array))
  1323. __activate_task(p, rq);
  1324. else {
  1325. p->prio = current->prio;
  1326. list_add_tail(&p->run_list, &current->run_list);
  1327. p->array = current->array;
  1328. p->array->nr_active++;
  1329. inc_nr_running(p, rq);
  1330. }
  1331. set_need_resched();
  1332. } else
  1333. /* Run child last */
  1334. __activate_task(p, rq);
  1335. /*
  1336. * We skip the following code due to cpu == this_cpu
  1337. *
  1338. * task_rq_unlock(rq, &flags);
  1339. * this_rq = task_rq_lock(current, &flags);
  1340. */
  1341. this_rq = rq;
  1342. } else {
  1343. this_rq = cpu_rq(this_cpu);
  1344. /*
  1345. * Not the local CPU - must adjust timestamp. This should
  1346. * get optimised away in the !CONFIG_SMP case.
  1347. */
  1348. p->timestamp = (p->timestamp - this_rq->timestamp_last_tick)
  1349. + rq->timestamp_last_tick;
  1350. __activate_task(p, rq);
  1351. if (TASK_PREEMPTS_CURR(p, rq))
  1352. resched_task(rq->curr);
  1353. /*
  1354. * Parent and child are on different CPUs, now get the
  1355. * parent runqueue to update the parent's ->sleep_avg:
  1356. */
  1357. task_rq_unlock(rq, &flags);
  1358. this_rq = task_rq_lock(current, &flags);
  1359. }
  1360. current->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(current) *
  1361. PARENT_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS);
  1362. task_rq_unlock(this_rq, &flags);
  1363. }
  1364. /*
  1365. * Potentially available exiting-child timeslices are
  1366. * retrieved here - this way the parent does not get
  1367. * penalized for creating too many threads.
  1368. *
  1369. * (this cannot be used to 'generate' timeslices
  1370. * artificially, because any timeslice recovered here
  1371. * was given away by the parent in the first place.)
  1372. */
  1373. void fastcall sched_exit(task_t *p)
  1374. {
  1375. unsigned long flags;
  1376. runqueue_t *rq;
  1377. /*
  1378. * If the child was a (relative-) CPU hog then decrease
  1379. * the sleep_avg of the parent as well.
  1380. */
  1381. rq = task_rq_lock(p->parent, &flags);
  1382. if (p->first_time_slice && task_cpu(p) == task_cpu(p->parent)) {
  1383. p->parent->time_slice += p->time_slice;
  1384. if (unlikely(p->parent->time_slice > task_timeslice(p)))
  1385. p->parent->time_slice = task_timeslice(p);
  1386. }
  1387. if (p->sleep_avg < p->parent->sleep_avg)
  1388. p->parent->sleep_avg = p->parent->sleep_avg /
  1389. (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg /
  1390. (EXIT_WEIGHT + 1);
  1391. task_rq_unlock(rq, &flags);
  1392. }
  1393. /**
  1394. * prepare_task_switch - prepare to switch tasks
  1395. * @rq: the runqueue preparing to switch
  1396. * @next: the task we are going to switch to.
  1397. *
  1398. * This is called with the rq lock held and interrupts off. It must
  1399. * be paired with a subsequent finish_task_switch after the context
  1400. * switch.
  1401. *
  1402. * prepare_task_switch sets up locking and calls architecture specific
  1403. * hooks.
  1404. */
  1405. static inline void prepare_task_switch(runqueue_t *rq, task_t *next)
  1406. {
  1407. prepare_lock_switch(rq, next);
  1408. prepare_arch_switch(next);
  1409. }
  1410. /**
  1411. * finish_task_switch - clean up after a task-switch
  1412. * @rq: runqueue associated with task-switch
  1413. * @prev: the thread we just switched away from.
  1414. *
  1415. * finish_task_switch must be called after the context switch, paired
  1416. * with a prepare_task_switch call before the context switch.
  1417. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  1418. * and do any other architecture-specific cleanup actions.
  1419. *
  1420. * Note that we may have delayed dropping an mm in context_switch(). If
  1421. * so, we finish that here outside of the runqueue lock. (Doing it
  1422. * with the lock held can cause deadlocks; see schedule() for
  1423. * details.)
  1424. */
  1425. static inline void finish_task_switch(runqueue_t *rq, task_t *prev)
  1426. __releases(rq->lock)
  1427. {
  1428. struct mm_struct *mm = rq->prev_mm;
  1429. unsigned long prev_task_flags;
  1430. rq->prev_mm = NULL;
  1431. /*
  1432. * A task struct has one reference for the use as "current".
  1433. * If a task dies, then it sets EXIT_ZOMBIE in tsk->exit_state and
  1434. * calls schedule one last time. The schedule call will never return,
  1435. * and the scheduled task must drop that reference.
  1436. * The test for EXIT_ZOMBIE must occur while the runqueue locks are
  1437. * still held, otherwise prev could be scheduled on another cpu, die
  1438. * there before we look at prev->state, and then the reference would
  1439. * be dropped twice.
  1440. * Manfred Spraul <manfred@colorfullife.com>
  1441. */
  1442. prev_task_flags = prev->flags;
  1443. finish_arch_switch(prev);
  1444. finish_lock_switch(rq, prev);
  1445. if (mm)
  1446. mmdrop(mm);
  1447. if (unlikely(prev_task_flags & PF_DEAD))
  1448. put_task_struct(prev);
  1449. }
  1450. /**
  1451. * schedule_tail - first thing a freshly forked thread must call.
  1452. * @prev: the thread we just switched away from.
  1453. */
  1454. asmlinkage void schedule_tail(task_t *prev)
  1455. __releases(rq->lock)
  1456. {
  1457. runqueue_t *rq = this_rq();
  1458. finish_task_switch(rq, prev);
  1459. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  1460. /* In this case, finish_task_switch does not reenable preemption */
  1461. preempt_enable();
  1462. #endif
  1463. if (current->set_child_tid)
  1464. put_user(current->pid, current->set_child_tid);
  1465. }
  1466. /*
  1467. * context_switch - switch to the new MM and the new
  1468. * thread's register state.
  1469. */
  1470. static inline
  1471. task_t * context_switch(runqueue_t *rq, task_t *prev, task_t *next)
  1472. {
  1473. struct mm_struct *mm = next->mm;
  1474. struct mm_struct *oldmm = prev->active_mm;
  1475. if (unlikely(!mm)) {
  1476. next->active_mm = oldmm;
  1477. atomic_inc(&oldmm->mm_count);
  1478. enter_lazy_tlb(oldmm, next);
  1479. } else
  1480. switch_mm(oldmm, mm, next);
  1481. if (unlikely(!prev->mm)) {
  1482. prev->active_mm = NULL;
  1483. WARN_ON(rq->prev_mm);
  1484. rq->prev_mm = oldmm;
  1485. }
  1486. /* Here we just switch the register state and the stack. */
  1487. switch_to(prev, next, prev);
  1488. return prev;
  1489. }
  1490. /*
  1491. * nr_running, nr_uninterruptible and nr_context_switches:
  1492. *
  1493. * externally visible scheduler statistics: current number of runnable
  1494. * threads, current number of uninterruptible-sleeping threads, total
  1495. * number of context switches performed since bootup.
  1496. */
  1497. unsigned long nr_running(void)
  1498. {
  1499. unsigned long i, sum = 0;
  1500. for_each_online_cpu(i)
  1501. sum += cpu_rq(i)->nr_running;
  1502. return sum;
  1503. }
  1504. unsigned long nr_uninterruptible(void)
  1505. {
  1506. unsigned long i, sum = 0;
  1507. for_each_cpu(i)
  1508. sum += cpu_rq(i)->nr_uninterruptible;
  1509. /*
  1510. * Since we read the counters lockless, it might be slightly
  1511. * inaccurate. Do not allow it to go below zero though:
  1512. */
  1513. if (unlikely((long)sum < 0))
  1514. sum = 0;
  1515. return sum;
  1516. }
  1517. unsigned long long nr_context_switches(void)
  1518. {
  1519. unsigned long long i, sum = 0;
  1520. for_each_cpu(i)
  1521. sum += cpu_rq(i)->nr_switches;
  1522. return sum;
  1523. }
  1524. unsigned long nr_iowait(void)
  1525. {
  1526. unsigned long i, sum = 0;
  1527. for_each_cpu(i)
  1528. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  1529. return sum;
  1530. }
  1531. #ifdef CONFIG_SMP
  1532. /*
  1533. * double_rq_lock - safely lock two runqueues
  1534. *
  1535. * Note this does not disable interrupts like task_rq_lock,
  1536. * you need to do so manually before calling.
  1537. */
  1538. static void double_rq_lock(runqueue_t *rq1, runqueue_t *rq2)
  1539. __acquires(rq1->lock)
  1540. __acquires(rq2->lock)
  1541. {
  1542. if (rq1 == rq2) {
  1543. spin_lock(&rq1->lock);
  1544. __acquire(rq2->lock); /* Fake it out ;) */
  1545. } else {
  1546. if (rq1 < rq2) {
  1547. spin_lock(&rq1->lock);
  1548. spin_lock(&rq2->lock);
  1549. } else {
  1550. spin_lock(&rq2->lock);
  1551. spin_lock(&rq1->lock);
  1552. }
  1553. }
  1554. }
  1555. /*
  1556. * double_rq_unlock - safely unlock two runqueues
  1557. *
  1558. * Note this does not restore interrupts like task_rq_unlock,
  1559. * you need to do so manually after calling.
  1560. */
  1561. static void double_rq_unlock(runqueue_t *rq1, runqueue_t *rq2)
  1562. __releases(rq1->lock)
  1563. __releases(rq2->lock)
  1564. {
  1565. spin_unlock(&rq1->lock);
  1566. if (rq1 != rq2)
  1567. spin_unlock(&rq2->lock);
  1568. else
  1569. __release(rq2->lock);
  1570. }
  1571. /*
  1572. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1573. */
  1574. static void double_lock_balance(runqueue_t *this_rq, runqueue_t *busiest)
  1575. __releases(this_rq->lock)
  1576. __acquires(busiest->lock)
  1577. __acquires(this_rq->lock)
  1578. {
  1579. if (unlikely(!spin_trylock(&busiest->lock))) {
  1580. if (busiest < this_rq) {
  1581. spin_unlock(&this_rq->lock);
  1582. spin_lock(&busiest->lock);
  1583. spin_lock(&this_rq->lock);
  1584. } else
  1585. spin_lock(&busiest->lock);
  1586. }
  1587. }
  1588. /*
  1589. * If dest_cpu is allowed for this process, migrate the task to it.
  1590. * This is accomplished by forcing the cpu_allowed mask to only
  1591. * allow dest_cpu, which will force the cpu onto dest_cpu. Then
  1592. * the cpu_allowed mask is restored.
  1593. */
  1594. static void sched_migrate_task(task_t *p, int dest_cpu)
  1595. {
  1596. migration_req_t req;
  1597. runqueue_t *rq;
  1598. unsigned long flags;
  1599. rq = task_rq_lock(p, &flags);
  1600. if (!cpu_isset(dest_cpu, p->cpus_allowed)
  1601. || unlikely(cpu_is_offline(dest_cpu)))
  1602. goto out;
  1603. /* force the process onto the specified CPU */
  1604. if (migrate_task(p, dest_cpu, &req)) {
  1605. /* Need to wait for migration thread (might exit: take ref). */
  1606. struct task_struct *mt = rq->migration_thread;
  1607. get_task_struct(mt);
  1608. task_rq_unlock(rq, &flags);
  1609. wake_up_process(mt);
  1610. put_task_struct(mt);
  1611. wait_for_completion(&req.done);
  1612. return;
  1613. }
  1614. out:
  1615. task_rq_unlock(rq, &flags);
  1616. }
  1617. /*
  1618. * sched_exec - execve() is a valuable balancing opportunity, because at
  1619. * this point the task has the smallest effective memory and cache footprint.
  1620. */
  1621. void sched_exec(void)
  1622. {
  1623. int new_cpu, this_cpu = get_cpu();
  1624. new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
  1625. put_cpu();
  1626. if (new_cpu != this_cpu)
  1627. sched_migrate_task(current, new_cpu);
  1628. }
  1629. /*
  1630. * pull_task - move a task from a remote runqueue to the local runqueue.
  1631. * Both runqueues must be locked.
  1632. */
  1633. static inline
  1634. void pull_task(runqueue_t *src_rq, prio_array_t *src_array, task_t *p,
  1635. runqueue_t *this_rq, prio_array_t *this_array, int this_cpu)
  1636. {
  1637. dequeue_task(p, src_array);
  1638. dec_nr_running(p, src_rq);
  1639. set_task_cpu(p, this_cpu);
  1640. inc_nr_running(p, this_rq);
  1641. enqueue_task(p, this_array);
  1642. p->timestamp = (p->timestamp - src_rq->timestamp_last_tick)
  1643. + this_rq->timestamp_last_tick;
  1644. /*
  1645. * Note that idle threads have a prio of MAX_PRIO, for this test
  1646. * to be always true for them.
  1647. */
  1648. if (TASK_PREEMPTS_CURR(p, this_rq))
  1649. resched_task(this_rq->curr);
  1650. }
  1651. /*
  1652. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  1653. */
  1654. static inline
  1655. int can_migrate_task(task_t *p, runqueue_t *rq, int this_cpu,
  1656. struct sched_domain *sd, enum idle_type idle,
  1657. int *all_pinned)
  1658. {
  1659. /*
  1660. * We do not migrate tasks that are:
  1661. * 1) running (obviously), or
  1662. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  1663. * 3) are cache-hot on their current CPU.
  1664. */
  1665. if (!cpu_isset(this_cpu, p->cpus_allowed))
  1666. return 0;
  1667. *all_pinned = 0;
  1668. if (task_running(rq, p))
  1669. return 0;
  1670. /*
  1671. * Aggressive migration if:
  1672. * 1) task is cache cold, or
  1673. * 2) too many balance attempts have failed.
  1674. */
  1675. if (sd->nr_balance_failed > sd->cache_nice_tries)
  1676. return 1;
  1677. if (task_hot(p, rq->timestamp_last_tick, sd))
  1678. return 0;
  1679. return 1;
  1680. }
  1681. /*
  1682. * move_tasks tries to move up to max_nr_move tasks from busiest to this_rq,
  1683. * as part of a balancing operation within "domain". Returns the number of
  1684. * tasks moved.
  1685. *
  1686. * Called with both runqueues locked.
  1687. */
  1688. static int move_tasks(runqueue_t *this_rq, int this_cpu, runqueue_t *busiest,
  1689. unsigned long max_nr_move, struct sched_domain *sd,
  1690. enum idle_type idle, int *all_pinned)
  1691. {
  1692. prio_array_t *array, *dst_array;
  1693. struct list_head *head, *curr;
  1694. int idx, pulled = 0, pinned = 0;
  1695. task_t *tmp;
  1696. if (max_nr_move == 0)
  1697. goto out;
  1698. pinned = 1;
  1699. /*
  1700. * We first consider expired tasks. Those will likely not be
  1701. * executed in the near future, and they are most likely to
  1702. * be cache-cold, thus switching CPUs has the least effect
  1703. * on them.
  1704. */
  1705. if (busiest->expired->nr_active) {
  1706. array = busiest->expired;
  1707. dst_array = this_rq->expired;
  1708. } else {
  1709. array = busiest->active;
  1710. dst_array = this_rq->active;
  1711. }
  1712. new_array:
  1713. /* Start searching at priority 0: */
  1714. idx = 0;
  1715. skip_bitmap:
  1716. if (!idx)
  1717. idx = sched_find_first_bit(array->bitmap);
  1718. else
  1719. idx = find_next_bit(array->bitmap, MAX_PRIO, idx);
  1720. if (idx >= MAX_PRIO) {
  1721. if (array == busiest->expired && busiest->active->nr_active) {
  1722. array = busiest->active;
  1723. dst_array = this_rq->active;
  1724. goto new_array;
  1725. }
  1726. goto out;
  1727. }
  1728. head = array->queue + idx;
  1729. curr = head->prev;
  1730. skip_queue:
  1731. tmp = list_entry(curr, task_t, run_list);
  1732. curr = curr->prev;
  1733. if (!can_migrate_task(tmp, busiest, this_cpu, sd, idle, &pinned)) {
  1734. if (curr != head)
  1735. goto skip_queue;
  1736. idx++;
  1737. goto skip_bitmap;
  1738. }
  1739. #ifdef CONFIG_SCHEDSTATS
  1740. if (task_hot(tmp, busiest->timestamp_last_tick, sd))
  1741. schedstat_inc(sd, lb_hot_gained[idle]);
  1742. #endif
  1743. pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu);
  1744. pulled++;
  1745. /* We only want to steal up to the prescribed number of tasks. */
  1746. if (pulled < max_nr_move) {
  1747. if (curr != head)
  1748. goto skip_queue;
  1749. idx++;
  1750. goto skip_bitmap;
  1751. }
  1752. out:
  1753. /*
  1754. * Right now, this is the only place pull_task() is called,
  1755. * so we can safely collect pull_task() stats here rather than
  1756. * inside pull_task().
  1757. */
  1758. schedstat_add(sd, lb_gained[idle], pulled);
  1759. if (all_pinned)
  1760. *all_pinned = pinned;
  1761. return pulled;
  1762. }
  1763. /*
  1764. * find_busiest_group finds and returns the busiest CPU group within the
  1765. * domain. It calculates and returns the number of tasks which should be
  1766. * moved to restore balance via the imbalance parameter.
  1767. */
  1768. static struct sched_group *
  1769. find_busiest_group(struct sched_domain *sd, int this_cpu,
  1770. unsigned long *imbalance, enum idle_type idle, int *sd_idle)
  1771. {
  1772. struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
  1773. unsigned long max_load, avg_load, total_load, this_load, total_pwr;
  1774. unsigned long max_pull;
  1775. int load_idx;
  1776. max_load = this_load = total_load = total_pwr = 0;
  1777. if (idle == NOT_IDLE)
  1778. load_idx = sd->busy_idx;
  1779. else if (idle == NEWLY_IDLE)
  1780. load_idx = sd->newidle_idx;
  1781. else
  1782. load_idx = sd->idle_idx;
  1783. do {
  1784. unsigned long load;
  1785. int local_group;
  1786. int i;
  1787. local_group = cpu_isset(this_cpu, group->cpumask);
  1788. /* Tally up the load of all CPUs in the group */
  1789. avg_load = 0;
  1790. for_each_cpu_mask(i, group->cpumask) {
  1791. if (*sd_idle && !idle_cpu(i))
  1792. *sd_idle = 0;
  1793. /* Bias balancing toward cpus of our domain */
  1794. if (local_group)
  1795. load = __target_load(i, load_idx, idle);
  1796. else
  1797. load = __source_load(i, load_idx, idle);
  1798. avg_load += load;
  1799. }
  1800. total_load += avg_load;
  1801. total_pwr += group->cpu_power;
  1802. /* Adjust by relative CPU power of the group */
  1803. avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
  1804. if (local_group) {
  1805. this_load = avg_load;
  1806. this = group;
  1807. } else if (avg_load > max_load) {
  1808. max_load = avg_load;
  1809. busiest = group;
  1810. }
  1811. group = group->next;
  1812. } while (group != sd->groups);
  1813. if (!busiest || this_load >= max_load || max_load <= SCHED_LOAD_SCALE)
  1814. goto out_balanced;
  1815. avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
  1816. if (this_load >= avg_load ||
  1817. 100*max_load <= sd->imbalance_pct*this_load)
  1818. goto out_balanced;
  1819. /*
  1820. * We're trying to get all the cpus to the average_load, so we don't
  1821. * want to push ourselves above the average load, nor do we wish to
  1822. * reduce the max loaded cpu below the average load, as either of these
  1823. * actions would just result in more rebalancing later, and ping-pong
  1824. * tasks around. Thus we look for the minimum possible imbalance.
  1825. * Negative imbalances (*we* are more loaded than anyone else) will
  1826. * be counted as no imbalance for these purposes -- we can't fix that
  1827. * by pulling tasks to us. Be careful of negative numbers as they'll
  1828. * appear as very large values with unsigned longs.
  1829. */
  1830. /* Don't want to pull so many tasks that a group would go idle */
  1831. max_pull = min(max_load - avg_load, max_load - SCHED_LOAD_SCALE);
  1832. /* How much load to actually move to equalise the imbalance */
  1833. *imbalance = min(max_pull * busiest->cpu_power,
  1834. (avg_load - this_load) * this->cpu_power)
  1835. / SCHED_LOAD_SCALE;
  1836. if (*imbalance < SCHED_LOAD_SCALE) {
  1837. unsigned long pwr_now = 0, pwr_move = 0;
  1838. unsigned long tmp;
  1839. if (max_load - this_load >= SCHED_LOAD_SCALE*2) {
  1840. *imbalance = 1;
  1841. return busiest;
  1842. }
  1843. /*
  1844. * OK, we don't have enough imbalance to justify moving tasks,
  1845. * however we may be able to increase total CPU power used by
  1846. * moving them.
  1847. */
  1848. pwr_now += busiest->cpu_power*min(SCHED_LOAD_SCALE, max_load);
  1849. pwr_now += this->cpu_power*min(SCHED_LOAD_SCALE, this_load);
  1850. pwr_now /= SCHED_LOAD_SCALE;
  1851. /* Amount of load we'd subtract */
  1852. tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/busiest->cpu_power;
  1853. if (max_load > tmp)
  1854. pwr_move += busiest->cpu_power*min(SCHED_LOAD_SCALE,
  1855. max_load - tmp);
  1856. /* Amount of load we'd add */
  1857. if (max_load*busiest->cpu_power <
  1858. SCHED_LOAD_SCALE*SCHED_LOAD_SCALE)
  1859. tmp = max_load*busiest->cpu_power/this->cpu_power;
  1860. else
  1861. tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/this->cpu_power;
  1862. pwr_move += this->cpu_power*min(SCHED_LOAD_SCALE, this_load + tmp);
  1863. pwr_move /= SCHED_LOAD_SCALE;
  1864. /* Move if we gain throughput */
  1865. if (pwr_move <= pwr_now)
  1866. goto out_balanced;
  1867. *imbalance = 1;
  1868. return busiest;
  1869. }
  1870. /* Get rid of the scaling factor, rounding down as we divide */
  1871. *imbalance = *imbalance / SCHED_LOAD_SCALE;
  1872. return busiest;
  1873. out_balanced:
  1874. *imbalance = 0;
  1875. return NULL;
  1876. }
  1877. /*
  1878. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  1879. */
  1880. static runqueue_t *find_busiest_queue(struct sched_group *group,
  1881. enum idle_type idle)
  1882. {
  1883. unsigned long load, max_load = 0;
  1884. runqueue_t *busiest = NULL;
  1885. int i;
  1886. for_each_cpu_mask(i, group->cpumask) {
  1887. load = __source_load(i, 0, idle);
  1888. if (load > max_load) {
  1889. max_load = load;
  1890. busiest = cpu_rq(i);
  1891. }
  1892. }
  1893. return busiest;
  1894. }
  1895. /*
  1896. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  1897. * so long as it is large enough.
  1898. */
  1899. #define MAX_PINNED_INTERVAL 512
  1900. /*
  1901. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  1902. * tasks if there is an imbalance.
  1903. *
  1904. * Called with this_rq unlocked.
  1905. */
  1906. static int load_balance(int this_cpu, runqueue_t *this_rq,
  1907. struct sched_domain *sd, enum idle_type idle)
  1908. {
  1909. struct sched_group *group;
  1910. runqueue_t *busiest;
  1911. unsigned long imbalance;
  1912. int nr_moved, all_pinned = 0;
  1913. int active_balance = 0;
  1914. int sd_idle = 0;
  1915. if (idle != NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER)
  1916. sd_idle = 1;
  1917. schedstat_inc(sd, lb_cnt[idle]);
  1918. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle);
  1919. if (!group) {
  1920. schedstat_inc(sd, lb_nobusyg[idle]);
  1921. goto out_balanced;
  1922. }
  1923. busiest = find_busiest_queue(group, idle);
  1924. if (!busiest) {
  1925. schedstat_inc(sd, lb_nobusyq[idle]);
  1926. goto out_balanced;
  1927. }
  1928. BUG_ON(busiest == this_rq);
  1929. schedstat_add(sd, lb_imbalance[idle], imbalance);
  1930. nr_moved = 0;
  1931. if (busiest->nr_running > 1) {
  1932. /*
  1933. * Attempt to move tasks. If find_busiest_group has found
  1934. * an imbalance but busiest->nr_running <= 1, the group is
  1935. * still unbalanced. nr_moved simply stays zero, so it is
  1936. * correctly treated as an imbalance.
  1937. */
  1938. double_rq_lock(this_rq, busiest);
  1939. nr_moved = move_tasks(this_rq, this_cpu, busiest,
  1940. imbalance, sd, idle, &all_pinned);
  1941. double_rq_unlock(this_rq, busiest);
  1942. /* All tasks on this runqueue were pinned by CPU affinity */
  1943. if (unlikely(all_pinned))
  1944. goto out_balanced;
  1945. }
  1946. if (!nr_moved) {
  1947. schedstat_inc(sd, lb_failed[idle]);
  1948. sd->nr_balance_failed++;
  1949. if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
  1950. spin_lock(&busiest->lock);
  1951. /* don't kick the migration_thread, if the curr
  1952. * task on busiest cpu can't be moved to this_cpu
  1953. */
  1954. if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
  1955. spin_unlock(&busiest->lock);
  1956. all_pinned = 1;
  1957. goto out_one_pinned;
  1958. }
  1959. if (!busiest->active_balance) {
  1960. busiest->active_balance = 1;
  1961. busiest->push_cpu = this_cpu;
  1962. active_balance = 1;
  1963. }
  1964. spin_unlock(&busiest->lock);
  1965. if (active_balance)
  1966. wake_up_process(busiest->migration_thread);
  1967. /*
  1968. * We've kicked active balancing, reset the failure
  1969. * counter.
  1970. */
  1971. sd->nr_balance_failed = sd->cache_nice_tries+1;
  1972. }
  1973. } else
  1974. sd->nr_balance_failed = 0;
  1975. if (likely(!active_balance)) {
  1976. /* We were unbalanced, so reset the balancing interval */
  1977. sd->balance_interval = sd->min_interval;
  1978. } else {
  1979. /*
  1980. * If we've begun active balancing, start to back off. This
  1981. * case may not be covered by the all_pinned logic if there
  1982. * is only 1 task on the busy runqueue (because we don't call
  1983. * move_tasks).
  1984. */
  1985. if (sd->balance_interval < sd->max_interval)
  1986. sd->balance_interval *= 2;
  1987. }
  1988. if (!nr_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER)
  1989. return -1;
  1990. return nr_moved;
  1991. out_balanced:
  1992. schedstat_inc(sd, lb_balanced[idle]);
  1993. sd->nr_balance_failed = 0;
  1994. out_one_pinned:
  1995. /* tune up the balancing interval */
  1996. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  1997. (sd->balance_interval < sd->max_interval))
  1998. sd->balance_interval *= 2;
  1999. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER)
  2000. return -1;
  2001. return 0;
  2002. }
  2003. /*
  2004. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2005. * tasks if there is an imbalance.
  2006. *
  2007. * Called from schedule when this_rq is about to become idle (NEWLY_IDLE).
  2008. * this_rq is locked.
  2009. */
  2010. static int load_balance_newidle(int this_cpu, runqueue_t *this_rq,
  2011. struct sched_domain *sd)
  2012. {
  2013. struct sched_group *group;
  2014. runqueue_t *busiest = NULL;
  2015. unsigned long imbalance;
  2016. int nr_moved = 0;
  2017. int sd_idle = 0;
  2018. if (sd->flags & SD_SHARE_CPUPOWER)
  2019. sd_idle = 1;
  2020. schedstat_inc(sd, lb_cnt[NEWLY_IDLE]);
  2021. group = find_busiest_group(sd, this_cpu, &imbalance, NEWLY_IDLE, &sd_idle);
  2022. if (!group) {
  2023. schedstat_inc(sd, lb_nobusyg[NEWLY_IDLE]);
  2024. goto out_balanced;
  2025. }
  2026. busiest = find_busiest_queue(group, NEWLY_IDLE);
  2027. if (!busiest) {
  2028. schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]);
  2029. goto out_balanced;
  2030. }
  2031. BUG_ON(busiest == this_rq);
  2032. schedstat_add(sd, lb_imbalance[NEWLY_IDLE], imbalance);
  2033. nr_moved = 0;
  2034. if (busiest->nr_running > 1) {
  2035. /* Attempt to move tasks */
  2036. double_lock_balance(this_rq, busiest);
  2037. nr_moved = move_tasks(this_rq, this_cpu, busiest,
  2038. imbalance, sd, NEWLY_IDLE, NULL);
  2039. spin_unlock(&busiest->lock);
  2040. }
  2041. if (!nr_moved) {
  2042. schedstat_inc(sd, lb_failed[NEWLY_IDLE]);
  2043. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER)
  2044. return -1;
  2045. } else
  2046. sd->nr_balance_failed = 0;
  2047. return nr_moved;
  2048. out_balanced:
  2049. schedstat_inc(sd, lb_balanced[NEWLY_IDLE]);
  2050. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER)
  2051. return -1;
  2052. sd->nr_balance_failed = 0;
  2053. return 0;
  2054. }
  2055. /*
  2056. * idle_balance is called by schedule() if this_cpu is about to become
  2057. * idle. Attempts to pull tasks from other CPUs.
  2058. */
  2059. static inline void idle_balance(int this_cpu, runqueue_t *this_rq)
  2060. {
  2061. struct sched_domain *sd;
  2062. for_each_domain(this_cpu, sd) {
  2063. if (sd->flags & SD_BALANCE_NEWIDLE) {
  2064. if (load_balance_newidle(this_cpu, this_rq, sd)) {
  2065. /* We've pulled tasks over so stop searching */
  2066. break;
  2067. }
  2068. }
  2069. }
  2070. }
  2071. /*
  2072. * active_load_balance is run by migration threads. It pushes running tasks
  2073. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  2074. * running on each physical CPU where possible, and avoids physical /
  2075. * logical imbalances.
  2076. *
  2077. * Called with busiest_rq locked.
  2078. */
  2079. static void active_load_balance(runqueue_t *busiest_rq, int busiest_cpu)
  2080. {
  2081. struct sched_domain *sd;
  2082. runqueue_t *target_rq;
  2083. int target_cpu = busiest_rq->push_cpu;
  2084. if (busiest_rq->nr_running <= 1)
  2085. /* no task to move */
  2086. return;
  2087. target_rq = cpu_rq(target_cpu);
  2088. /*
  2089. * This condition is "impossible", if it occurs
  2090. * we need to fix it. Originally reported by
  2091. * Bjorn Helgaas on a 128-cpu setup.
  2092. */
  2093. BUG_ON(busiest_rq == target_rq);
  2094. /* move a task from busiest_rq to target_rq */
  2095. double_lock_balance(busiest_rq, target_rq);
  2096. /* Search for an sd spanning us and the target CPU. */
  2097. for_each_domain(target_cpu, sd)
  2098. if ((sd->flags & SD_LOAD_BALANCE) &&
  2099. cpu_isset(busiest_cpu, sd->span))
  2100. break;
  2101. if (unlikely(sd == NULL))
  2102. goto out;
  2103. schedstat_inc(sd, alb_cnt);
  2104. if (move_tasks(target_rq, target_cpu, busiest_rq, 1, sd, SCHED_IDLE, NULL))
  2105. schedstat_inc(sd, alb_pushed);
  2106. else
  2107. schedstat_inc(sd, alb_failed);
  2108. out:
  2109. spin_unlock(&target_rq->lock);
  2110. }
  2111. /*
  2112. * rebalance_tick will get called every timer tick, on every CPU.
  2113. *
  2114. * It checks each scheduling domain to see if it is due to be balanced,
  2115. * and initiates a balancing operation if so.
  2116. *
  2117. * Balancing parameters are set up in arch_init_sched_domains.
  2118. */
  2119. /* Don't have all balancing operations going off at once */
  2120. #define CPU_OFFSET(cpu) (HZ * cpu / NR_CPUS)
  2121. static void rebalance_tick(int this_cpu, runqueue_t *this_rq,
  2122. enum idle_type idle)
  2123. {
  2124. unsigned long old_load, this_load;
  2125. unsigned long j = jiffies + CPU_OFFSET(this_cpu);
  2126. struct sched_domain *sd;
  2127. int i;
  2128. this_load = this_rq->nr_running * SCHED_LOAD_SCALE;
  2129. /* Update our load */
  2130. for (i = 0; i < 3; i++) {
  2131. unsigned long new_load = this_load;
  2132. int scale = 1 << i;
  2133. old_load = this_rq->cpu_load[i];
  2134. /*
  2135. * Round up the averaging division if load is increasing. This
  2136. * prevents us from getting stuck on 9 if the load is 10, for
  2137. * example.
  2138. */
  2139. if (new_load > old_load)
  2140. new_load += scale-1;
  2141. this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) / scale;
  2142. }
  2143. for_each_domain(this_cpu, sd) {
  2144. unsigned long interval;
  2145. if (!(sd->flags & SD_LOAD_BALANCE))
  2146. continue;
  2147. interval = sd->balance_interval;
  2148. if (idle != SCHED_IDLE)
  2149. interval *= sd->busy_factor;
  2150. /* scale ms to jiffies */
  2151. interval = msecs_to_jiffies(interval);
  2152. if (unlikely(!interval))
  2153. interval = 1;
  2154. if (j - sd->last_balance >= interval) {
  2155. if (load_balance(this_cpu, this_rq, sd, idle)) {
  2156. /*
  2157. * We've pulled tasks over so either we're no
  2158. * longer idle, or one of our SMT siblings is
  2159. * not idle.
  2160. */
  2161. idle = NOT_IDLE;
  2162. }
  2163. sd->last_balance += interval;
  2164. }
  2165. }
  2166. }
  2167. #else
  2168. /*
  2169. * on UP we do not need to balance between CPUs:
  2170. */
  2171. static inline void rebalance_tick(int cpu, runqueue_t *rq, enum idle_type idle)
  2172. {
  2173. }
  2174. static inline void idle_balance(int cpu, runqueue_t *rq)
  2175. {
  2176. }
  2177. #endif
  2178. static inline int wake_priority_sleeper(runqueue_t *rq)
  2179. {
  2180. int ret = 0;
  2181. #ifdef CONFIG_SCHED_SMT
  2182. spin_lock(&rq->lock);
  2183. /*
  2184. * If an SMT sibling task has been put to sleep for priority
  2185. * reasons reschedule the idle task to see if it can now run.
  2186. */
  2187. if (rq->nr_running) {
  2188. resched_task(rq->idle);
  2189. ret = 1;
  2190. }
  2191. spin_unlock(&rq->lock);
  2192. #endif
  2193. return ret;
  2194. }
  2195. DEFINE_PER_CPU(struct kernel_stat, kstat);
  2196. EXPORT_PER_CPU_SYMBOL(kstat);
  2197. /*
  2198. * This is called on clock ticks and on context switches.
  2199. * Bank in p->sched_time the ns elapsed since the last tick or switch.
  2200. */
  2201. static inline void update_cpu_clock(task_t *p, runqueue_t *rq,
  2202. unsigned long long now)
  2203. {
  2204. unsigned long long last = max(p->timestamp, rq->timestamp_last_tick);
  2205. p->sched_time += now - last;
  2206. }
  2207. /*
  2208. * Return current->sched_time plus any more ns on the sched_clock
  2209. * that have not yet been banked.
  2210. */
  2211. unsigned long long current_sched_time(const task_t *tsk)
  2212. {
  2213. unsigned long long ns;
  2214. unsigned long flags;
  2215. local_irq_save(flags);
  2216. ns = max(tsk->timestamp, task_rq(tsk)->timestamp_last_tick);
  2217. ns = tsk->sched_time + (sched_clock() - ns);
  2218. local_irq_restore(flags);
  2219. return ns;
  2220. }
  2221. /*
  2222. * We place interactive tasks back into the active array, if possible.
  2223. *
  2224. * To guarantee that this does not starve expired tasks we ignore the
  2225. * interactivity of a task if the first expired task had to wait more
  2226. * than a 'reasonable' amount of time. This deadline timeout is
  2227. * load-dependent, as the frequency of array switched decreases with
  2228. * increasing number of running tasks. We also ignore the interactivity
  2229. * if a better static_prio task has expired:
  2230. */
  2231. #define EXPIRED_STARVING(rq) \
  2232. ((STARVATION_LIMIT && ((rq)->expired_timestamp && \
  2233. (jiffies - (rq)->expired_timestamp >= \
  2234. STARVATION_LIMIT * ((rq)->nr_running) + 1))) || \
  2235. ((rq)->curr->static_prio > (rq)->best_expired_prio))
  2236. /*
  2237. * Account user cpu time to a process.
  2238. * @p: the process that the cpu time gets accounted to
  2239. * @hardirq_offset: the offset to subtract from hardirq_count()
  2240. * @cputime: the cpu time spent in user space since the last update
  2241. */
  2242. void account_user_time(struct task_struct *p, cputime_t cputime)
  2243. {
  2244. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2245. cputime64_t tmp;
  2246. p->utime = cputime_add(p->utime, cputime);
  2247. /* Add user time to cpustat. */
  2248. tmp = cputime_to_cputime64(cputime);
  2249. if (TASK_NICE(p) > 0)
  2250. cpustat->nice = cputime64_add(cpustat->nice, tmp);
  2251. else
  2252. cpustat->user = cputime64_add(cpustat->user, tmp);
  2253. }
  2254. /*
  2255. * Account system cpu time to a process.
  2256. * @p: the process that the cpu time gets accounted to
  2257. * @hardirq_offset: the offset to subtract from hardirq_count()
  2258. * @cputime: the cpu time spent in kernel space since the last update
  2259. */
  2260. void account_system_time(struct task_struct *p, int hardirq_offset,
  2261. cputime_t cputime)
  2262. {
  2263. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2264. runqueue_t *rq = this_rq();
  2265. cputime64_t tmp;
  2266. p->stime = cputime_add(p->stime, cputime);
  2267. /* Add system time to cpustat. */
  2268. tmp = cputime_to_cputime64(cputime);
  2269. if (hardirq_count() - hardirq_offset)
  2270. cpustat->irq = cputime64_add(cpustat->irq, tmp);
  2271. else if (softirq_count())
  2272. cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
  2273. else if (p != rq->idle)
  2274. cpustat->system = cputime64_add(cpustat->system, tmp);
  2275. else if (atomic_read(&rq->nr_iowait) > 0)
  2276. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  2277. else
  2278. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  2279. /* Account for system time used */
  2280. acct_update_integrals(p);
  2281. }
  2282. /*
  2283. * Account for involuntary wait time.
  2284. * @p: the process from which the cpu time has been stolen
  2285. * @steal: the cpu time spent in involuntary wait
  2286. */
  2287. void account_steal_time(struct task_struct *p, cputime_t steal)
  2288. {
  2289. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2290. cputime64_t tmp = cputime_to_cputime64(steal);
  2291. runqueue_t *rq = this_rq();
  2292. if (p == rq->idle) {
  2293. p->stime = cputime_add(p->stime, steal);
  2294. if (atomic_read(&rq->nr_iowait) > 0)
  2295. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  2296. else
  2297. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  2298. } else
  2299. cpustat->steal = cputime64_add(cpustat->steal, tmp);
  2300. }
  2301. /*
  2302. * This function gets called by the timer code, with HZ frequency.
  2303. * We call it with interrupts disabled.
  2304. *
  2305. * It also gets called by the fork code, when changing the parent's
  2306. * timeslices.
  2307. */
  2308. void scheduler_tick(void)
  2309. {
  2310. int cpu = smp_processor_id();
  2311. runqueue_t *rq = this_rq();
  2312. task_t *p = current;
  2313. unsigned long long now = sched_clock();
  2314. update_cpu_clock(p, rq, now);
  2315. rq->timestamp_last_tick = now;
  2316. if (p == rq->idle) {
  2317. if (wake_priority_sleeper(rq))
  2318. goto out;
  2319. rebalance_tick(cpu, rq, SCHED_IDLE);
  2320. return;
  2321. }
  2322. /* Task might have expired already, but not scheduled off yet */
  2323. if (p->array != rq->active) {
  2324. set_tsk_need_resched(p);
  2325. goto out;
  2326. }
  2327. spin_lock(&rq->lock);
  2328. /*
  2329. * The task was running during this tick - update the
  2330. * time slice counter. Note: we do not update a thread's
  2331. * priority until it either goes to sleep or uses up its
  2332. * timeslice. This makes it possible for interactive tasks
  2333. * to use up their timeslices at their highest priority levels.
  2334. */
  2335. if (rt_task(p)) {
  2336. /*
  2337. * RR tasks need a special form of timeslice management.
  2338. * FIFO tasks have no timeslices.
  2339. */
  2340. if ((p->policy == SCHED_RR) && !--p->time_slice) {
  2341. p->time_slice = task_timeslice(p);
  2342. p->first_time_slice = 0;
  2343. set_tsk_need_resched(p);
  2344. /* put it at the end of the queue: */
  2345. requeue_task(p, rq->active);
  2346. }
  2347. goto out_unlock;
  2348. }
  2349. if (!--p->time_slice) {
  2350. dequeue_task(p, rq->active);
  2351. set_tsk_need_resched(p);
  2352. p->prio = effective_prio(p);
  2353. p->time_slice = task_timeslice(p);
  2354. p->first_time_slice = 0;
  2355. if (!rq->expired_timestamp)
  2356. rq->expired_timestamp = jiffies;
  2357. if (!TASK_INTERACTIVE(p) || EXPIRED_STARVING(rq)) {
  2358. enqueue_task(p, rq->expired);
  2359. if (p->static_prio < rq->best_expired_prio)
  2360. rq->best_expired_prio = p->static_prio;
  2361. } else
  2362. enqueue_task(p, rq->active);
  2363. } else {
  2364. /*
  2365. * Prevent a too long timeslice allowing a task to monopolize
  2366. * the CPU. We do this by splitting up the timeslice into
  2367. * smaller pieces.
  2368. *
  2369. * Note: this does not mean the task's timeslices expire or
  2370. * get lost in any way, they just might be preempted by
  2371. * another task of equal priority. (one with higher
  2372. * priority would have preempted this task already.) We
  2373. * requeue this task to the end of the list on this priority
  2374. * level, which is in essence a round-robin of tasks with
  2375. * equal priority.
  2376. *
  2377. * This only applies to tasks in the interactive
  2378. * delta range with at least TIMESLICE_GRANULARITY to requeue.
  2379. */
  2380. if (TASK_INTERACTIVE(p) && !((task_timeslice(p) -
  2381. p->time_slice) % TIMESLICE_GRANULARITY(p)) &&
  2382. (p->time_slice >= TIMESLICE_GRANULARITY(p)) &&
  2383. (p->array == rq->active)) {
  2384. requeue_task(p, rq->active);
  2385. set_tsk_need_resched(p);
  2386. }
  2387. }
  2388. out_unlock:
  2389. spin_unlock(&rq->lock);
  2390. out:
  2391. rebalance_tick(cpu, rq, NOT_IDLE);
  2392. }
  2393. #ifdef CONFIG_SCHED_SMT
  2394. static inline void wakeup_busy_runqueue(runqueue_t *rq)
  2395. {
  2396. /* If an SMT runqueue is sleeping due to priority reasons wake it up */
  2397. if (rq->curr == rq->idle && rq->nr_running)
  2398. resched_task(rq->idle);
  2399. }
  2400. static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq)
  2401. {
  2402. struct sched_domain *tmp, *sd = NULL;
  2403. cpumask_t sibling_map;
  2404. int i;
  2405. for_each_domain(this_cpu, tmp)
  2406. if (tmp->flags & SD_SHARE_CPUPOWER)
  2407. sd = tmp;
  2408. if (!sd)
  2409. return;
  2410. /*
  2411. * Unlock the current runqueue because we have to lock in
  2412. * CPU order to avoid deadlocks. Caller knows that we might
  2413. * unlock. We keep IRQs disabled.
  2414. */
  2415. spin_unlock(&this_rq->lock);
  2416. sibling_map = sd->span;
  2417. for_each_cpu_mask(i, sibling_map)
  2418. spin_lock(&cpu_rq(i)->lock);
  2419. /*
  2420. * We clear this CPU from the mask. This both simplifies the
  2421. * inner loop and keps this_rq locked when we exit:
  2422. */
  2423. cpu_clear(this_cpu, sibling_map);
  2424. for_each_cpu_mask(i, sibling_map) {
  2425. runqueue_t *smt_rq = cpu_rq(i);
  2426. wakeup_busy_runqueue(smt_rq);
  2427. }
  2428. for_each_cpu_mask(i, sibling_map)
  2429. spin_unlock(&cpu_rq(i)->lock);
  2430. /*
  2431. * We exit with this_cpu's rq still held and IRQs
  2432. * still disabled:
  2433. */
  2434. }
  2435. /*
  2436. * number of 'lost' timeslices this task wont be able to fully
  2437. * utilize, if another task runs on a sibling. This models the
  2438. * slowdown effect of other tasks running on siblings:
  2439. */
  2440. static inline unsigned long smt_slice(task_t *p, struct sched_domain *sd)
  2441. {
  2442. return p->time_slice * (100 - sd->per_cpu_gain) / 100;
  2443. }
  2444. static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq)
  2445. {
  2446. struct sched_domain *tmp, *sd = NULL;
  2447. cpumask_t sibling_map;
  2448. prio_array_t *array;
  2449. int ret = 0, i;
  2450. task_t *p;
  2451. for_each_domain(this_cpu, tmp)
  2452. if (tmp->flags & SD_SHARE_CPUPOWER)
  2453. sd = tmp;
  2454. if (!sd)
  2455. return 0;
  2456. /*
  2457. * The same locking rules and details apply as for
  2458. * wake_sleeping_dependent():
  2459. */
  2460. spin_unlock(&this_rq->lock);
  2461. sibling_map = sd->span;
  2462. for_each_cpu_mask(i, sibling_map)
  2463. spin_lock(&cpu_rq(i)->lock);
  2464. cpu_clear(this_cpu, sibling_map);
  2465. /*
  2466. * Establish next task to be run - it might have gone away because
  2467. * we released the runqueue lock above:
  2468. */
  2469. if (!this_rq->nr_running)
  2470. goto out_unlock;
  2471. array = this_rq->active;
  2472. if (!array->nr_active)
  2473. array = this_rq->expired;
  2474. BUG_ON(!array->nr_active);
  2475. p = list_entry(array->queue[sched_find_first_bit(array->bitmap)].next,
  2476. task_t, run_list);
  2477. for_each_cpu_mask(i, sibling_map) {
  2478. runqueue_t *smt_rq = cpu_rq(i);
  2479. task_t *smt_curr = smt_rq->curr;
  2480. /* Kernel threads do not participate in dependent sleeping */
  2481. if (!p->mm || !smt_curr->mm || rt_task(p))
  2482. goto check_smt_task;
  2483. /*
  2484. * If a user task with lower static priority than the
  2485. * running task on the SMT sibling is trying to schedule,
  2486. * delay it till there is proportionately less timeslice
  2487. * left of the sibling task to prevent a lower priority
  2488. * task from using an unfair proportion of the
  2489. * physical cpu's resources. -ck
  2490. */
  2491. if (rt_task(smt_curr)) {
  2492. /*
  2493. * With real time tasks we run non-rt tasks only
  2494. * per_cpu_gain% of the time.
  2495. */
  2496. if ((jiffies % DEF_TIMESLICE) >
  2497. (sd->per_cpu_gain * DEF_TIMESLICE / 100))
  2498. ret = 1;
  2499. } else
  2500. if (smt_curr->static_prio < p->static_prio &&
  2501. !TASK_PREEMPTS_CURR(p, smt_rq) &&
  2502. smt_slice(smt_curr, sd) > task_timeslice(p))
  2503. ret = 1;
  2504. check_smt_task:
  2505. if ((!smt_curr->mm && smt_curr != smt_rq->idle) ||
  2506. rt_task(smt_curr))
  2507. continue;
  2508. if (!p->mm) {
  2509. wakeup_busy_runqueue(smt_rq);
  2510. continue;
  2511. }
  2512. /*
  2513. * Reschedule a lower priority task on the SMT sibling for
  2514. * it to be put to sleep, or wake it up if it has been put to
  2515. * sleep for priority reasons to see if it should run now.
  2516. */
  2517. if (rt_task(p)) {
  2518. if ((jiffies % DEF_TIMESLICE) >
  2519. (sd->per_cpu_gain * DEF_TIMESLICE / 100))
  2520. resched_task(smt_curr);
  2521. } else {
  2522. if (TASK_PREEMPTS_CURR(p, smt_rq) &&
  2523. smt_slice(p, sd) > task_timeslice(smt_curr))
  2524. resched_task(smt_curr);
  2525. else
  2526. wakeup_busy_runqueue(smt_rq);
  2527. }
  2528. }
  2529. out_unlock:
  2530. for_each_cpu_mask(i, sibling_map)
  2531. spin_unlock(&cpu_rq(i)->lock);
  2532. return ret;
  2533. }
  2534. #else
  2535. static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq)
  2536. {
  2537. }
  2538. static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq)
  2539. {
  2540. return 0;
  2541. }
  2542. #endif
  2543. #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
  2544. void fastcall add_preempt_count(int val)
  2545. {
  2546. /*
  2547. * Underflow?
  2548. */
  2549. BUG_ON((preempt_count() < 0));
  2550. preempt_count() += val;
  2551. /*
  2552. * Spinlock count overflowing soon?
  2553. */
  2554. BUG_ON((preempt_count() & PREEMPT_MASK) >= PREEMPT_MASK-10);
  2555. }
  2556. EXPORT_SYMBOL(add_preempt_count);
  2557. void fastcall sub_preempt_count(int val)
  2558. {
  2559. /*
  2560. * Underflow?
  2561. */
  2562. BUG_ON(val > preempt_count());
  2563. /*
  2564. * Is the spinlock portion underflowing?
  2565. */
  2566. BUG_ON((val < PREEMPT_MASK) && !(preempt_count() & PREEMPT_MASK));
  2567. preempt_count() -= val;
  2568. }
  2569. EXPORT_SYMBOL(sub_preempt_count);
  2570. #endif
  2571. /*
  2572. * schedule() is the main scheduler function.
  2573. */
  2574. asmlinkage void __sched schedule(void)
  2575. {
  2576. long *switch_count;
  2577. task_t *prev, *next;
  2578. runqueue_t *rq;
  2579. prio_array_t *array;
  2580. struct list_head *queue;
  2581. unsigned long long now;
  2582. unsigned long run_time;
  2583. int cpu, idx, new_prio;
  2584. /*
  2585. * Test if we are atomic. Since do_exit() needs to call into
  2586. * schedule() atomically, we ignore that path for now.
  2587. * Otherwise, whine if we are scheduling when we should not be.
  2588. */
  2589. if (likely(!current->exit_state)) {
  2590. if (unlikely(in_atomic())) {
  2591. printk(KERN_ERR "scheduling while atomic: "
  2592. "%s/0x%08x/%d\n",
  2593. current->comm, preempt_count(), current->pid);
  2594. dump_stack();
  2595. }
  2596. }
  2597. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  2598. need_resched:
  2599. preempt_disable();
  2600. prev = current;
  2601. release_kernel_lock(prev);
  2602. need_resched_nonpreemptible:
  2603. rq = this_rq();
  2604. /*
  2605. * The idle thread is not allowed to schedule!
  2606. * Remove this check after it has been exercised a bit.
  2607. */
  2608. if (unlikely(prev == rq->idle) && prev->state != TASK_RUNNING) {
  2609. printk(KERN_ERR "bad: scheduling from the idle thread!\n");
  2610. dump_stack();
  2611. }
  2612. schedstat_inc(rq, sched_cnt);
  2613. now = sched_clock();
  2614. if (likely((long long)(now - prev->timestamp) < NS_MAX_SLEEP_AVG)) {
  2615. run_time = now - prev->timestamp;
  2616. if (unlikely((long long)(now - prev->timestamp) < 0))
  2617. run_time = 0;
  2618. } else
  2619. run_time = NS_MAX_SLEEP_AVG;
  2620. /*
  2621. * Tasks charged proportionately less run_time at high sleep_avg to
  2622. * delay them losing their interactive status
  2623. */
  2624. run_time /= (CURRENT_BONUS(prev) ? : 1);
  2625. spin_lock_irq(&rq->lock);
  2626. if (unlikely(prev->flags & PF_DEAD))
  2627. prev->state = EXIT_DEAD;
  2628. switch_count = &prev->nivcsw;
  2629. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  2630. switch_count = &prev->nvcsw;
  2631. if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
  2632. unlikely(signal_pending(prev))))
  2633. prev->state = TASK_RUNNING;
  2634. else {
  2635. if (prev->state == TASK_UNINTERRUPTIBLE)
  2636. rq->nr_uninterruptible++;
  2637. deactivate_task(prev, rq);
  2638. }
  2639. }
  2640. cpu = smp_processor_id();
  2641. if (unlikely(!rq->nr_running)) {
  2642. go_idle:
  2643. idle_balance(cpu, rq);
  2644. if (!rq->nr_running) {
  2645. next = rq->idle;
  2646. rq->expired_timestamp = 0;
  2647. wake_sleeping_dependent(cpu, rq);
  2648. /*
  2649. * wake_sleeping_dependent() might have released
  2650. * the runqueue, so break out if we got new
  2651. * tasks meanwhile:
  2652. */
  2653. if (!rq->nr_running)
  2654. goto switch_tasks;
  2655. }
  2656. } else {
  2657. if (dependent_sleeper(cpu, rq)) {
  2658. next = rq->idle;
  2659. goto switch_tasks;
  2660. }
  2661. /*
  2662. * dependent_sleeper() releases and reacquires the runqueue
  2663. * lock, hence go into the idle loop if the rq went
  2664. * empty meanwhile:
  2665. */
  2666. if (unlikely(!rq->nr_running))
  2667. goto go_idle;
  2668. }
  2669. array = rq->active;
  2670. if (unlikely(!array->nr_active)) {
  2671. /*
  2672. * Switch the active and expired arrays.
  2673. */
  2674. schedstat_inc(rq, sched_switch);
  2675. rq->active = rq->expired;
  2676. rq->expired = array;
  2677. array = rq->active;
  2678. rq->expired_timestamp = 0;
  2679. rq->best_expired_prio = MAX_PRIO;
  2680. }
  2681. idx = sched_find_first_bit(array->bitmap);
  2682. queue = array->queue + idx;
  2683. next = list_entry(queue->next, task_t, run_list);
  2684. if (!rt_task(next) && next->activated > 0) {
  2685. unsigned long long delta = now - next->timestamp;
  2686. if (unlikely((long long)(now - next->timestamp) < 0))
  2687. delta = 0;
  2688. if (next->activated == 1)
  2689. delta = delta * (ON_RUNQUEUE_WEIGHT * 128 / 100) / 128;
  2690. array = next->array;
  2691. new_prio = recalc_task_prio(next, next->timestamp + delta);
  2692. if (unlikely(next->prio != new_prio)) {
  2693. dequeue_task(next, array);
  2694. next->prio = new_prio;
  2695. enqueue_task(next, array);
  2696. } else
  2697. requeue_task(next, array);
  2698. }
  2699. next->activated = 0;
  2700. switch_tasks:
  2701. if (next == rq->idle)
  2702. schedstat_inc(rq, sched_goidle);
  2703. prefetch(next);
  2704. prefetch_stack(next);
  2705. clear_tsk_need_resched(prev);
  2706. rcu_qsctr_inc(task_cpu(prev));
  2707. update_cpu_clock(prev, rq, now);
  2708. prev->sleep_avg -= run_time;
  2709. if ((long)prev->sleep_avg <= 0)
  2710. prev->sleep_avg = 0;
  2711. prev->timestamp = prev->last_ran = now;
  2712. sched_info_switch(prev, next);
  2713. if (likely(prev != next)) {
  2714. next->timestamp = now;
  2715. rq->nr_switches++;
  2716. rq->curr = next;
  2717. ++*switch_count;
  2718. prepare_task_switch(rq, next);
  2719. prev = context_switch(rq, prev, next);
  2720. barrier();
  2721. /*
  2722. * this_rq must be evaluated again because prev may have moved
  2723. * CPUs since it called schedule(), thus the 'rq' on its stack
  2724. * frame will be invalid.
  2725. */
  2726. finish_task_switch(this_rq(), prev);
  2727. } else
  2728. spin_unlock_irq(&rq->lock);
  2729. prev = current;
  2730. if (unlikely(reacquire_kernel_lock(prev) < 0))
  2731. goto need_resched_nonpreemptible;
  2732. preempt_enable_no_resched();
  2733. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  2734. goto need_resched;
  2735. }
  2736. EXPORT_SYMBOL(schedule);
  2737. #ifdef CONFIG_PREEMPT
  2738. /*
  2739. * this is is the entry point to schedule() from in-kernel preemption
  2740. * off of preempt_enable. Kernel preemptions off return from interrupt
  2741. * occur there and call schedule directly.
  2742. */
  2743. asmlinkage void __sched preempt_schedule(void)
  2744. {
  2745. struct thread_info *ti = current_thread_info();
  2746. #ifdef CONFIG_PREEMPT_BKL
  2747. struct task_struct *task = current;
  2748. int saved_lock_depth;
  2749. #endif
  2750. /*
  2751. * If there is a non-zero preempt_count or interrupts are disabled,
  2752. * we do not want to preempt the current task. Just return..
  2753. */
  2754. if (unlikely(ti->preempt_count || irqs_disabled()))
  2755. return;
  2756. need_resched:
  2757. add_preempt_count(PREEMPT_ACTIVE);
  2758. /*
  2759. * We keep the big kernel semaphore locked, but we
  2760. * clear ->lock_depth so that schedule() doesnt
  2761. * auto-release the semaphore:
  2762. */
  2763. #ifdef CONFIG_PREEMPT_BKL
  2764. saved_lock_depth = task->lock_depth;
  2765. task->lock_depth = -1;
  2766. #endif
  2767. schedule();
  2768. #ifdef CONFIG_PREEMPT_BKL
  2769. task->lock_depth = saved_lock_depth;
  2770. #endif
  2771. sub_preempt_count(PREEMPT_ACTIVE);
  2772. /* we could miss a preemption opportunity between schedule and now */
  2773. barrier();
  2774. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  2775. goto need_resched;
  2776. }
  2777. EXPORT_SYMBOL(preempt_schedule);
  2778. /*
  2779. * this is is the entry point to schedule() from kernel preemption
  2780. * off of irq context.
  2781. * Note, that this is called and return with irqs disabled. This will
  2782. * protect us against recursive calling from irq.
  2783. */
  2784. asmlinkage void __sched preempt_schedule_irq(void)
  2785. {
  2786. struct thread_info *ti = current_thread_info();
  2787. #ifdef CONFIG_PREEMPT_BKL
  2788. struct task_struct *task = current;
  2789. int saved_lock_depth;
  2790. #endif
  2791. /* Catch callers which need to be fixed*/
  2792. BUG_ON(ti->preempt_count || !irqs_disabled());
  2793. need_resched:
  2794. add_preempt_count(PREEMPT_ACTIVE);
  2795. /*
  2796. * We keep the big kernel semaphore locked, but we
  2797. * clear ->lock_depth so that schedule() doesnt
  2798. * auto-release the semaphore:
  2799. */
  2800. #ifdef CONFIG_PREEMPT_BKL
  2801. saved_lock_depth = task->lock_depth;
  2802. task->lock_depth = -1;
  2803. #endif
  2804. local_irq_enable();
  2805. schedule();
  2806. local_irq_disable();
  2807. #ifdef CONFIG_PREEMPT_BKL
  2808. task->lock_depth = saved_lock_depth;
  2809. #endif
  2810. sub_preempt_count(PREEMPT_ACTIVE);
  2811. /* we could miss a preemption opportunity between schedule and now */
  2812. barrier();
  2813. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  2814. goto need_resched;
  2815. }
  2816. #endif /* CONFIG_PREEMPT */
  2817. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
  2818. void *key)
  2819. {
  2820. task_t *p = curr->private;
  2821. return try_to_wake_up(p, mode, sync);
  2822. }
  2823. EXPORT_SYMBOL(default_wake_function);
  2824. /*
  2825. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  2826. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  2827. * number) then we wake all the non-exclusive tasks and one exclusive task.
  2828. *
  2829. * There are circumstances in which we can try to wake a task which has already
  2830. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  2831. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  2832. */
  2833. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  2834. int nr_exclusive, int sync, void *key)
  2835. {
  2836. struct list_head *tmp, *next;
  2837. list_for_each_safe(tmp, next, &q->task_list) {
  2838. wait_queue_t *curr;
  2839. unsigned flags;
  2840. curr = list_entry(tmp, wait_queue_t, task_list);
  2841. flags = curr->flags;
  2842. if (curr->func(curr, mode, sync, key) &&
  2843. (flags & WQ_FLAG_EXCLUSIVE) &&
  2844. !--nr_exclusive)
  2845. break;
  2846. }
  2847. }
  2848. /**
  2849. * __wake_up - wake up threads blocked on a waitqueue.
  2850. * @q: the waitqueue
  2851. * @mode: which threads
  2852. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  2853. * @key: is directly passed to the wakeup function
  2854. */
  2855. void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
  2856. int nr_exclusive, void *key)
  2857. {
  2858. unsigned long flags;
  2859. spin_lock_irqsave(&q->lock, flags);
  2860. __wake_up_common(q, mode, nr_exclusive, 0, key);
  2861. spin_unlock_irqrestore(&q->lock, flags);
  2862. }
  2863. EXPORT_SYMBOL(__wake_up);
  2864. /*
  2865. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  2866. */
  2867. void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
  2868. {
  2869. __wake_up_common(q, mode, 1, 0, NULL);
  2870. }
  2871. /**
  2872. * __wake_up_sync - wake up threads blocked on a waitqueue.
  2873. * @q: the waitqueue
  2874. * @mode: which threads
  2875. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  2876. *
  2877. * The sync wakeup differs that the waker knows that it will schedule
  2878. * away soon, so while the target thread will be woken up, it will not
  2879. * be migrated to another CPU - ie. the two threads are 'synchronized'
  2880. * with each other. This can prevent needless bouncing between CPUs.
  2881. *
  2882. * On UP it can prevent extra preemption.
  2883. */
  2884. void fastcall
  2885. __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  2886. {
  2887. unsigned long flags;
  2888. int sync = 1;
  2889. if (unlikely(!q))
  2890. return;
  2891. if (unlikely(!nr_exclusive))
  2892. sync = 0;
  2893. spin_lock_irqsave(&q->lock, flags);
  2894. __wake_up_common(q, mode, nr_exclusive, sync, NULL);
  2895. spin_unlock_irqrestore(&q->lock, flags);
  2896. }
  2897. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  2898. void fastcall complete(struct completion *x)
  2899. {
  2900. unsigned long flags;
  2901. spin_lock_irqsave(&x->wait.lock, flags);
  2902. x->done++;
  2903. __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  2904. 1, 0, NULL);
  2905. spin_unlock_irqrestore(&x->wait.lock, flags);
  2906. }
  2907. EXPORT_SYMBOL(complete);
  2908. void fastcall complete_all(struct completion *x)
  2909. {
  2910. unsigned long flags;
  2911. spin_lock_irqsave(&x->wait.lock, flags);
  2912. x->done += UINT_MAX/2;
  2913. __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  2914. 0, 0, NULL);
  2915. spin_unlock_irqrestore(&x->wait.lock, flags);
  2916. }
  2917. EXPORT_SYMBOL(complete_all);
  2918. void fastcall __sched wait_for_completion(struct completion *x)
  2919. {
  2920. might_sleep();
  2921. spin_lock_irq(&x->wait.lock);
  2922. if (!x->done) {
  2923. DECLARE_WAITQUEUE(wait, current);
  2924. wait.flags |= WQ_FLAG_EXCLUSIVE;
  2925. __add_wait_queue_tail(&x->wait, &wait);
  2926. do {
  2927. __set_current_state(TASK_UNINTERRUPTIBLE);
  2928. spin_unlock_irq(&x->wait.lock);
  2929. schedule();
  2930. spin_lock_irq(&x->wait.lock);
  2931. } while (!x->done);
  2932. __remove_wait_queue(&x->wait, &wait);
  2933. }
  2934. x->done--;
  2935. spin_unlock_irq(&x->wait.lock);
  2936. }
  2937. EXPORT_SYMBOL(wait_for_completion);
  2938. unsigned long fastcall __sched
  2939. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  2940. {
  2941. might_sleep();
  2942. spin_lock_irq(&x->wait.lock);
  2943. if (!x->done) {
  2944. DECLARE_WAITQUEUE(wait, current);
  2945. wait.flags |= WQ_FLAG_EXCLUSIVE;
  2946. __add_wait_queue_tail(&x->wait, &wait);
  2947. do {
  2948. __set_current_state(TASK_UNINTERRUPTIBLE);
  2949. spin_unlock_irq(&x->wait.lock);
  2950. timeout = schedule_timeout(timeout);
  2951. spin_lock_irq(&x->wait.lock);
  2952. if (!timeout) {
  2953. __remove_wait_queue(&x->wait, &wait);
  2954. goto out;
  2955. }
  2956. } while (!x->done);
  2957. __remove_wait_queue(&x->wait, &wait);
  2958. }
  2959. x->done--;
  2960. out:
  2961. spin_unlock_irq(&x->wait.lock);
  2962. return timeout;
  2963. }
  2964. EXPORT_SYMBOL(wait_for_completion_timeout);
  2965. int fastcall __sched wait_for_completion_interruptible(struct completion *x)
  2966. {
  2967. int ret = 0;
  2968. might_sleep();
  2969. spin_lock_irq(&x->wait.lock);
  2970. if (!x->done) {
  2971. DECLARE_WAITQUEUE(wait, current);
  2972. wait.flags |= WQ_FLAG_EXCLUSIVE;
  2973. __add_wait_queue_tail(&x->wait, &wait);
  2974. do {
  2975. if (signal_pending(current)) {
  2976. ret = -ERESTARTSYS;
  2977. __remove_wait_queue(&x->wait, &wait);
  2978. goto out;
  2979. }
  2980. __set_current_state(TASK_INTERRUPTIBLE);
  2981. spin_unlock_irq(&x->wait.lock);
  2982. schedule();
  2983. spin_lock_irq(&x->wait.lock);
  2984. } while (!x->done);
  2985. __remove_wait_queue(&x->wait, &wait);
  2986. }
  2987. x->done--;
  2988. out:
  2989. spin_unlock_irq(&x->wait.lock);
  2990. return ret;
  2991. }
  2992. EXPORT_SYMBOL(wait_for_completion_interruptible);
  2993. unsigned long fastcall __sched
  2994. wait_for_completion_interruptible_timeout(struct completion *x,
  2995. unsigned long timeout)
  2996. {
  2997. might_sleep();
  2998. spin_lock_irq(&x->wait.lock);
  2999. if (!x->done) {
  3000. DECLARE_WAITQUEUE(wait, current);
  3001. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3002. __add_wait_queue_tail(&x->wait, &wait);
  3003. do {
  3004. if (signal_pending(current)) {
  3005. timeout = -ERESTARTSYS;
  3006. __remove_wait_queue(&x->wait, &wait);
  3007. goto out;
  3008. }
  3009. __set_current_state(TASK_INTERRUPTIBLE);
  3010. spin_unlock_irq(&x->wait.lock);
  3011. timeout = schedule_timeout(timeout);
  3012. spin_lock_irq(&x->wait.lock);
  3013. if (!timeout) {
  3014. __remove_wait_queue(&x->wait, &wait);
  3015. goto out;
  3016. }
  3017. } while (!x->done);
  3018. __remove_wait_queue(&x->wait, &wait);
  3019. }
  3020. x->done--;
  3021. out:
  3022. spin_unlock_irq(&x->wait.lock);
  3023. return timeout;
  3024. }
  3025. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  3026. #define SLEEP_ON_VAR \
  3027. unsigned long flags; \
  3028. wait_queue_t wait; \
  3029. init_waitqueue_entry(&wait, current);
  3030. #define SLEEP_ON_HEAD \
  3031. spin_lock_irqsave(&q->lock,flags); \
  3032. __add_wait_queue(q, &wait); \
  3033. spin_unlock(&q->lock);
  3034. #define SLEEP_ON_TAIL \
  3035. spin_lock_irq(&q->lock); \
  3036. __remove_wait_queue(q, &wait); \
  3037. spin_unlock_irqrestore(&q->lock, flags);
  3038. void fastcall __sched interruptible_sleep_on(wait_queue_head_t *q)
  3039. {
  3040. SLEEP_ON_VAR
  3041. current->state = TASK_INTERRUPTIBLE;
  3042. SLEEP_ON_HEAD
  3043. schedule();
  3044. SLEEP_ON_TAIL
  3045. }
  3046. EXPORT_SYMBOL(interruptible_sleep_on);
  3047. long fastcall __sched
  3048. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3049. {
  3050. SLEEP_ON_VAR
  3051. current->state = TASK_INTERRUPTIBLE;
  3052. SLEEP_ON_HEAD
  3053. timeout = schedule_timeout(timeout);
  3054. SLEEP_ON_TAIL
  3055. return timeout;
  3056. }
  3057. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  3058. void fastcall __sched sleep_on(wait_queue_head_t *q)
  3059. {
  3060. SLEEP_ON_VAR
  3061. current->state = TASK_UNINTERRUPTIBLE;
  3062. SLEEP_ON_HEAD
  3063. schedule();
  3064. SLEEP_ON_TAIL
  3065. }
  3066. EXPORT_SYMBOL(sleep_on);
  3067. long fastcall __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3068. {
  3069. SLEEP_ON_VAR
  3070. current->state = TASK_UNINTERRUPTIBLE;
  3071. SLEEP_ON_HEAD
  3072. timeout = schedule_timeout(timeout);
  3073. SLEEP_ON_TAIL
  3074. return timeout;
  3075. }
  3076. EXPORT_SYMBOL(sleep_on_timeout);
  3077. void set_user_nice(task_t *p, long nice)
  3078. {
  3079. unsigned long flags;
  3080. prio_array_t *array;
  3081. runqueue_t *rq;
  3082. int old_prio, new_prio, delta;
  3083. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  3084. return;
  3085. /*
  3086. * We have to be careful, if called from sys_setpriority(),
  3087. * the task might be in the middle of scheduling on another CPU.
  3088. */
  3089. rq = task_rq_lock(p, &flags);
  3090. /*
  3091. * The RT priorities are set via sched_setscheduler(), but we still
  3092. * allow the 'normal' nice value to be set - but as expected
  3093. * it wont have any effect on scheduling until the task is
  3094. * not SCHED_NORMAL:
  3095. */
  3096. if (rt_task(p)) {
  3097. p->static_prio = NICE_TO_PRIO(nice);
  3098. goto out_unlock;
  3099. }
  3100. array = p->array;
  3101. if (array) {
  3102. dequeue_task(p, array);
  3103. dec_prio_bias(rq, p->static_prio);
  3104. }
  3105. old_prio = p->prio;
  3106. new_prio = NICE_TO_PRIO(nice);
  3107. delta = new_prio - old_prio;
  3108. p->static_prio = NICE_TO_PRIO(nice);
  3109. p->prio += delta;
  3110. if (array) {
  3111. enqueue_task(p, array);
  3112. inc_prio_bias(rq, p->static_prio);
  3113. /*
  3114. * If the task increased its priority or is running and
  3115. * lowered its priority, then reschedule its CPU:
  3116. */
  3117. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  3118. resched_task(rq->curr);
  3119. }
  3120. out_unlock:
  3121. task_rq_unlock(rq, &flags);
  3122. }
  3123. EXPORT_SYMBOL(set_user_nice);
  3124. /*
  3125. * can_nice - check if a task can reduce its nice value
  3126. * @p: task
  3127. * @nice: nice value
  3128. */
  3129. int can_nice(const task_t *p, const int nice)
  3130. {
  3131. /* convert nice value [19,-20] to rlimit style value [1,40] */
  3132. int nice_rlim = 20 - nice;
  3133. return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
  3134. capable(CAP_SYS_NICE));
  3135. }
  3136. #ifdef __ARCH_WANT_SYS_NICE
  3137. /*
  3138. * sys_nice - change the priority of the current process.
  3139. * @increment: priority increment
  3140. *
  3141. * sys_setpriority is a more generic, but much slower function that
  3142. * does similar things.
  3143. */
  3144. asmlinkage long sys_nice(int increment)
  3145. {
  3146. int retval;
  3147. long nice;
  3148. /*
  3149. * Setpriority might change our priority at the same moment.
  3150. * We don't have to worry. Conceptually one call occurs first
  3151. * and we have a single winner.
  3152. */
  3153. if (increment < -40)
  3154. increment = -40;
  3155. if (increment > 40)
  3156. increment = 40;
  3157. nice = PRIO_TO_NICE(current->static_prio) + increment;
  3158. if (nice < -20)
  3159. nice = -20;
  3160. if (nice > 19)
  3161. nice = 19;
  3162. if (increment < 0 && !can_nice(current, nice))
  3163. return -EPERM;
  3164. retval = security_task_setnice(current, nice);
  3165. if (retval)
  3166. return retval;
  3167. set_user_nice(current, nice);
  3168. return 0;
  3169. }
  3170. #endif
  3171. /**
  3172. * task_prio - return the priority value of a given task.
  3173. * @p: the task in question.
  3174. *
  3175. * This is the priority value as seen by users in /proc.
  3176. * RT tasks are offset by -200. Normal tasks are centered
  3177. * around 0, value goes from -16 to +15.
  3178. */
  3179. int task_prio(const task_t *p)
  3180. {
  3181. return p->prio - MAX_RT_PRIO;
  3182. }
  3183. /**
  3184. * task_nice - return the nice value of a given task.
  3185. * @p: the task in question.
  3186. */
  3187. int task_nice(const task_t *p)
  3188. {
  3189. return TASK_NICE(p);
  3190. }
  3191. EXPORT_SYMBOL_GPL(task_nice);
  3192. /**
  3193. * idle_cpu - is a given cpu idle currently?
  3194. * @cpu: the processor in question.
  3195. */
  3196. int idle_cpu(int cpu)
  3197. {
  3198. return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  3199. }
  3200. /**
  3201. * idle_task - return the idle task for a given cpu.
  3202. * @cpu: the processor in question.
  3203. */
  3204. task_t *idle_task(int cpu)
  3205. {
  3206. return cpu_rq(cpu)->idle;
  3207. }
  3208. /**
  3209. * find_process_by_pid - find a process with a matching PID value.
  3210. * @pid: the pid in question.
  3211. */
  3212. static inline task_t *find_process_by_pid(pid_t pid)
  3213. {
  3214. return pid ? find_task_by_pid(pid) : current;
  3215. }
  3216. /* Actually do priority change: must hold rq lock. */
  3217. static void __setscheduler(struct task_struct *p, int policy, int prio)
  3218. {
  3219. BUG_ON(p->array);
  3220. p->policy = policy;
  3221. p->rt_priority = prio;
  3222. if (policy != SCHED_NORMAL)
  3223. p->prio = MAX_RT_PRIO-1 - p->rt_priority;
  3224. else
  3225. p->prio = p->static_prio;
  3226. }
  3227. /**
  3228. * sched_setscheduler - change the scheduling policy and/or RT priority of
  3229. * a thread.
  3230. * @p: the task in question.
  3231. * @policy: new policy.
  3232. * @param: structure containing the new RT priority.
  3233. */
  3234. int sched_setscheduler(struct task_struct *p, int policy,
  3235. struct sched_param *param)
  3236. {
  3237. int retval;
  3238. int oldprio, oldpolicy = -1;
  3239. prio_array_t *array;
  3240. unsigned long flags;
  3241. runqueue_t *rq;
  3242. recheck:
  3243. /* double check policy once rq lock held */
  3244. if (policy < 0)
  3245. policy = oldpolicy = p->policy;
  3246. else if (policy != SCHED_FIFO && policy != SCHED_RR &&
  3247. policy != SCHED_NORMAL)
  3248. return -EINVAL;
  3249. /*
  3250. * Valid priorities for SCHED_FIFO and SCHED_RR are
  3251. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL is 0.
  3252. */
  3253. if (param->sched_priority < 0 ||
  3254. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  3255. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  3256. return -EINVAL;
  3257. if ((policy == SCHED_NORMAL) != (param->sched_priority == 0))
  3258. return -EINVAL;
  3259. /*
  3260. * Allow unprivileged RT tasks to decrease priority:
  3261. */
  3262. if (!capable(CAP_SYS_NICE)) {
  3263. /* can't change policy */
  3264. if (policy != p->policy &&
  3265. !p->signal->rlim[RLIMIT_RTPRIO].rlim_cur)
  3266. return -EPERM;
  3267. /* can't increase priority */
  3268. if (policy != SCHED_NORMAL &&
  3269. param->sched_priority > p->rt_priority &&
  3270. param->sched_priority >
  3271. p->signal->rlim[RLIMIT_RTPRIO].rlim_cur)
  3272. return -EPERM;
  3273. /* can't change other user's priorities */
  3274. if ((current->euid != p->euid) &&
  3275. (current->euid != p->uid))
  3276. return -EPERM;
  3277. }
  3278. retval = security_task_setscheduler(p, policy, param);
  3279. if (retval)
  3280. return retval;
  3281. /*
  3282. * To be able to change p->policy safely, the apropriate
  3283. * runqueue lock must be held.
  3284. */
  3285. rq = task_rq_lock(p, &flags);
  3286. /* recheck policy now with rq lock held */
  3287. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  3288. policy = oldpolicy = -1;
  3289. task_rq_unlock(rq, &flags);
  3290. goto recheck;
  3291. }
  3292. array = p->array;
  3293. if (array)
  3294. deactivate_task(p, rq);
  3295. oldprio = p->prio;
  3296. __setscheduler(p, policy, param->sched_priority);
  3297. if (array) {
  3298. __activate_task(p, rq);
  3299. /*
  3300. * Reschedule if we are currently running on this runqueue and
  3301. * our priority decreased, or if we are not currently running on
  3302. * this runqueue and our priority is higher than the current's
  3303. */
  3304. if (task_running(rq, p)) {
  3305. if (p->prio > oldprio)
  3306. resched_task(rq->curr);
  3307. } else if (TASK_PREEMPTS_CURR(p, rq))
  3308. resched_task(rq->curr);
  3309. }
  3310. task_rq_unlock(rq, &flags);
  3311. return 0;
  3312. }
  3313. EXPORT_SYMBOL_GPL(sched_setscheduler);
  3314. static int
  3315. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  3316. {
  3317. int retval;
  3318. struct sched_param lparam;
  3319. struct task_struct *p;
  3320. if (!param || pid < 0)
  3321. return -EINVAL;
  3322. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  3323. return -EFAULT;
  3324. read_lock_irq(&tasklist_lock);
  3325. p = find_process_by_pid(pid);
  3326. if (!p) {
  3327. read_unlock_irq(&tasklist_lock);
  3328. return -ESRCH;
  3329. }
  3330. retval = sched_setscheduler(p, policy, &lparam);
  3331. read_unlock_irq(&tasklist_lock);
  3332. return retval;
  3333. }
  3334. /**
  3335. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  3336. * @pid: the pid in question.
  3337. * @policy: new policy.
  3338. * @param: structure containing the new RT priority.
  3339. */
  3340. asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
  3341. struct sched_param __user *param)
  3342. {
  3343. return do_sched_setscheduler(pid, policy, param);
  3344. }
  3345. /**
  3346. * sys_sched_setparam - set/change the RT priority of a thread
  3347. * @pid: the pid in question.
  3348. * @param: structure containing the new RT priority.
  3349. */
  3350. asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
  3351. {
  3352. return do_sched_setscheduler(pid, -1, param);
  3353. }
  3354. /**
  3355. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  3356. * @pid: the pid in question.
  3357. */
  3358. asmlinkage long sys_sched_getscheduler(pid_t pid)
  3359. {
  3360. int retval = -EINVAL;
  3361. task_t *p;
  3362. if (pid < 0)
  3363. goto out_nounlock;
  3364. retval = -ESRCH;
  3365. read_lock(&tasklist_lock);
  3366. p = find_process_by_pid(pid);
  3367. if (p) {
  3368. retval = security_task_getscheduler(p);
  3369. if (!retval)
  3370. retval = p->policy;
  3371. }
  3372. read_unlock(&tasklist_lock);
  3373. out_nounlock:
  3374. return retval;
  3375. }
  3376. /**
  3377. * sys_sched_getscheduler - get the RT priority of a thread
  3378. * @pid: the pid in question.
  3379. * @param: structure containing the RT priority.
  3380. */
  3381. asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
  3382. {
  3383. struct sched_param lp;
  3384. int retval = -EINVAL;
  3385. task_t *p;
  3386. if (!param || pid < 0)
  3387. goto out_nounlock;
  3388. read_lock(&tasklist_lock);
  3389. p = find_process_by_pid(pid);
  3390. retval = -ESRCH;
  3391. if (!p)
  3392. goto out_unlock;
  3393. retval = security_task_getscheduler(p);
  3394. if (retval)
  3395. goto out_unlock;
  3396. lp.sched_priority = p->rt_priority;
  3397. read_unlock(&tasklist_lock);
  3398. /*
  3399. * This one might sleep, we cannot do it with a spinlock held ...
  3400. */
  3401. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  3402. out_nounlock:
  3403. return retval;
  3404. out_unlock:
  3405. read_unlock(&tasklist_lock);
  3406. return retval;
  3407. }
  3408. long sched_setaffinity(pid_t pid, cpumask_t new_mask)
  3409. {
  3410. task_t *p;
  3411. int retval;
  3412. cpumask_t cpus_allowed;
  3413. lock_cpu_hotplug();
  3414. read_lock(&tasklist_lock);
  3415. p = find_process_by_pid(pid);
  3416. if (!p) {
  3417. read_unlock(&tasklist_lock);
  3418. unlock_cpu_hotplug();
  3419. return -ESRCH;
  3420. }
  3421. /*
  3422. * It is not safe to call set_cpus_allowed with the
  3423. * tasklist_lock held. We will bump the task_struct's
  3424. * usage count and then drop tasklist_lock.
  3425. */
  3426. get_task_struct(p);
  3427. read_unlock(&tasklist_lock);
  3428. retval = -EPERM;
  3429. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  3430. !capable(CAP_SYS_NICE))
  3431. goto out_unlock;
  3432. cpus_allowed = cpuset_cpus_allowed(p);
  3433. cpus_and(new_mask, new_mask, cpus_allowed);
  3434. retval = set_cpus_allowed(p, new_mask);
  3435. out_unlock:
  3436. put_task_struct(p);
  3437. unlock_cpu_hotplug();
  3438. return retval;
  3439. }
  3440. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  3441. cpumask_t *new_mask)
  3442. {
  3443. if (len < sizeof(cpumask_t)) {
  3444. memset(new_mask, 0, sizeof(cpumask_t));
  3445. } else if (len > sizeof(cpumask_t)) {
  3446. len = sizeof(cpumask_t);
  3447. }
  3448. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  3449. }
  3450. /**
  3451. * sys_sched_setaffinity - set the cpu affinity of a process
  3452. * @pid: pid of the process
  3453. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3454. * @user_mask_ptr: user-space pointer to the new cpu mask
  3455. */
  3456. asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
  3457. unsigned long __user *user_mask_ptr)
  3458. {
  3459. cpumask_t new_mask;
  3460. int retval;
  3461. retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
  3462. if (retval)
  3463. return retval;
  3464. return sched_setaffinity(pid, new_mask);
  3465. }
  3466. /*
  3467. * Represents all cpu's present in the system
  3468. * In systems capable of hotplug, this map could dynamically grow
  3469. * as new cpu's are detected in the system via any platform specific
  3470. * method, such as ACPI for e.g.
  3471. */
  3472. cpumask_t cpu_present_map;
  3473. EXPORT_SYMBOL(cpu_present_map);
  3474. #ifndef CONFIG_SMP
  3475. cpumask_t cpu_online_map = CPU_MASK_ALL;
  3476. cpumask_t cpu_possible_map = CPU_MASK_ALL;
  3477. #endif
  3478. long sched_getaffinity(pid_t pid, cpumask_t *mask)
  3479. {
  3480. int retval;
  3481. task_t *p;
  3482. lock_cpu_hotplug();
  3483. read_lock(&tasklist_lock);
  3484. retval = -ESRCH;
  3485. p = find_process_by_pid(pid);
  3486. if (!p)
  3487. goto out_unlock;
  3488. retval = 0;
  3489. cpus_and(*mask, p->cpus_allowed, cpu_possible_map);
  3490. out_unlock:
  3491. read_unlock(&tasklist_lock);
  3492. unlock_cpu_hotplug();
  3493. if (retval)
  3494. return retval;
  3495. return 0;
  3496. }
  3497. /**
  3498. * sys_sched_getaffinity - get the cpu affinity of a process
  3499. * @pid: pid of the process
  3500. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3501. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  3502. */
  3503. asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
  3504. unsigned long __user *user_mask_ptr)
  3505. {
  3506. int ret;
  3507. cpumask_t mask;
  3508. if (len < sizeof(cpumask_t))
  3509. return -EINVAL;
  3510. ret = sched_getaffinity(pid, &mask);
  3511. if (ret < 0)
  3512. return ret;
  3513. if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
  3514. return -EFAULT;
  3515. return sizeof(cpumask_t);
  3516. }
  3517. /**
  3518. * sys_sched_yield - yield the current processor to other threads.
  3519. *
  3520. * this function yields the current CPU by moving the calling thread
  3521. * to the expired array. If there are no other threads running on this
  3522. * CPU then this function will return.
  3523. */
  3524. asmlinkage long sys_sched_yield(void)
  3525. {
  3526. runqueue_t *rq = this_rq_lock();
  3527. prio_array_t *array = current->array;
  3528. prio_array_t *target = rq->expired;
  3529. schedstat_inc(rq, yld_cnt);
  3530. /*
  3531. * We implement yielding by moving the task into the expired
  3532. * queue.
  3533. *
  3534. * (special rule: RT tasks will just roundrobin in the active
  3535. * array.)
  3536. */
  3537. if (rt_task(current))
  3538. target = rq->active;
  3539. if (array->nr_active == 1) {
  3540. schedstat_inc(rq, yld_act_empty);
  3541. if (!rq->expired->nr_active)
  3542. schedstat_inc(rq, yld_both_empty);
  3543. } else if (!rq->expired->nr_active)
  3544. schedstat_inc(rq, yld_exp_empty);
  3545. if (array != target) {
  3546. dequeue_task(current, array);
  3547. enqueue_task(current, target);
  3548. } else
  3549. /*
  3550. * requeue_task is cheaper so perform that if possible.
  3551. */
  3552. requeue_task(current, array);
  3553. /*
  3554. * Since we are going to call schedule() anyway, there's
  3555. * no need to preempt or enable interrupts:
  3556. */
  3557. __release(rq->lock);
  3558. _raw_spin_unlock(&rq->lock);
  3559. preempt_enable_no_resched();
  3560. schedule();
  3561. return 0;
  3562. }
  3563. static inline void __cond_resched(void)
  3564. {
  3565. /*
  3566. * The BKS might be reacquired before we have dropped
  3567. * PREEMPT_ACTIVE, which could trigger a second
  3568. * cond_resched() call.
  3569. */
  3570. if (unlikely(preempt_count()))
  3571. return;
  3572. do {
  3573. add_preempt_count(PREEMPT_ACTIVE);
  3574. schedule();
  3575. sub_preempt_count(PREEMPT_ACTIVE);
  3576. } while (need_resched());
  3577. }
  3578. int __sched cond_resched(void)
  3579. {
  3580. if (need_resched()) {
  3581. __cond_resched();
  3582. return 1;
  3583. }
  3584. return 0;
  3585. }
  3586. EXPORT_SYMBOL(cond_resched);
  3587. /*
  3588. * cond_resched_lock() - if a reschedule is pending, drop the given lock,
  3589. * call schedule, and on return reacquire the lock.
  3590. *
  3591. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  3592. * operations here to prevent schedule() from being called twice (once via
  3593. * spin_unlock(), once by hand).
  3594. */
  3595. int cond_resched_lock(spinlock_t *lock)
  3596. {
  3597. int ret = 0;
  3598. if (need_lockbreak(lock)) {
  3599. spin_unlock(lock);
  3600. cpu_relax();
  3601. ret = 1;
  3602. spin_lock(lock);
  3603. }
  3604. if (need_resched()) {
  3605. _raw_spin_unlock(lock);
  3606. preempt_enable_no_resched();
  3607. __cond_resched();
  3608. ret = 1;
  3609. spin_lock(lock);
  3610. }
  3611. return ret;
  3612. }
  3613. EXPORT_SYMBOL(cond_resched_lock);
  3614. int __sched cond_resched_softirq(void)
  3615. {
  3616. BUG_ON(!in_softirq());
  3617. if (need_resched()) {
  3618. __local_bh_enable();
  3619. __cond_resched();
  3620. local_bh_disable();
  3621. return 1;
  3622. }
  3623. return 0;
  3624. }
  3625. EXPORT_SYMBOL(cond_resched_softirq);
  3626. /**
  3627. * yield - yield the current processor to other threads.
  3628. *
  3629. * this is a shortcut for kernel-space yielding - it marks the
  3630. * thread runnable and calls sys_sched_yield().
  3631. */
  3632. void __sched yield(void)
  3633. {
  3634. set_current_state(TASK_RUNNING);
  3635. sys_sched_yield();
  3636. }
  3637. EXPORT_SYMBOL(yield);
  3638. /*
  3639. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  3640. * that process accounting knows that this is a task in IO wait state.
  3641. *
  3642. * But don't do that if it is a deliberate, throttling IO wait (this task
  3643. * has set its backing_dev_info: the queue against which it should throttle)
  3644. */
  3645. void __sched io_schedule(void)
  3646. {
  3647. struct runqueue *rq = &per_cpu(runqueues, raw_smp_processor_id());
  3648. atomic_inc(&rq->nr_iowait);
  3649. schedule();
  3650. atomic_dec(&rq->nr_iowait);
  3651. }
  3652. EXPORT_SYMBOL(io_schedule);
  3653. long __sched io_schedule_timeout(long timeout)
  3654. {
  3655. struct runqueue *rq = &per_cpu(runqueues, raw_smp_processor_id());
  3656. long ret;
  3657. atomic_inc(&rq->nr_iowait);
  3658. ret = schedule_timeout(timeout);
  3659. atomic_dec(&rq->nr_iowait);
  3660. return ret;
  3661. }
  3662. /**
  3663. * sys_sched_get_priority_max - return maximum RT priority.
  3664. * @policy: scheduling class.
  3665. *
  3666. * this syscall returns the maximum rt_priority that can be used
  3667. * by a given scheduling class.
  3668. */
  3669. asmlinkage long sys_sched_get_priority_max(int policy)
  3670. {
  3671. int ret = -EINVAL;
  3672. switch (policy) {
  3673. case SCHED_FIFO:
  3674. case SCHED_RR:
  3675. ret = MAX_USER_RT_PRIO-1;
  3676. break;
  3677. case SCHED_NORMAL:
  3678. ret = 0;
  3679. break;
  3680. }
  3681. return ret;
  3682. }
  3683. /**
  3684. * sys_sched_get_priority_min - return minimum RT priority.
  3685. * @policy: scheduling class.
  3686. *
  3687. * this syscall returns the minimum rt_priority that can be used
  3688. * by a given scheduling class.
  3689. */
  3690. asmlinkage long sys_sched_get_priority_min(int policy)
  3691. {
  3692. int ret = -EINVAL;
  3693. switch (policy) {
  3694. case SCHED_FIFO:
  3695. case SCHED_RR:
  3696. ret = 1;
  3697. break;
  3698. case SCHED_NORMAL:
  3699. ret = 0;
  3700. }
  3701. return ret;
  3702. }
  3703. /**
  3704. * sys_sched_rr_get_interval - return the default timeslice of a process.
  3705. * @pid: pid of the process.
  3706. * @interval: userspace pointer to the timeslice value.
  3707. *
  3708. * this syscall writes the default timeslice value of a given process
  3709. * into the user-space timespec buffer. A value of '0' means infinity.
  3710. */
  3711. asmlinkage
  3712. long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
  3713. {
  3714. int retval = -EINVAL;
  3715. struct timespec t;
  3716. task_t *p;
  3717. if (pid < 0)
  3718. goto out_nounlock;
  3719. retval = -ESRCH;
  3720. read_lock(&tasklist_lock);
  3721. p = find_process_by_pid(pid);
  3722. if (!p)
  3723. goto out_unlock;
  3724. retval = security_task_getscheduler(p);
  3725. if (retval)
  3726. goto out_unlock;
  3727. jiffies_to_timespec(p->policy & SCHED_FIFO ?
  3728. 0 : task_timeslice(p), &t);
  3729. read_unlock(&tasklist_lock);
  3730. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  3731. out_nounlock:
  3732. return retval;
  3733. out_unlock:
  3734. read_unlock(&tasklist_lock);
  3735. return retval;
  3736. }
  3737. static inline struct task_struct *eldest_child(struct task_struct *p)
  3738. {
  3739. if (list_empty(&p->children)) return NULL;
  3740. return list_entry(p->children.next,struct task_struct,sibling);
  3741. }
  3742. static inline struct task_struct *older_sibling(struct task_struct *p)
  3743. {
  3744. if (p->sibling.prev==&p->parent->children) return NULL;
  3745. return list_entry(p->sibling.prev,struct task_struct,sibling);
  3746. }
  3747. static inline struct task_struct *younger_sibling(struct task_struct *p)
  3748. {
  3749. if (p->sibling.next==&p->parent->children) return NULL;
  3750. return list_entry(p->sibling.next,struct task_struct,sibling);
  3751. }
  3752. static void show_task(task_t *p)
  3753. {
  3754. task_t *relative;
  3755. unsigned state;
  3756. unsigned long free = 0;
  3757. static const char *stat_nam[] = { "R", "S", "D", "T", "t", "Z", "X" };
  3758. printk("%-13.13s ", p->comm);
  3759. state = p->state ? __ffs(p->state) + 1 : 0;
  3760. if (state < ARRAY_SIZE(stat_nam))
  3761. printk(stat_nam[state]);
  3762. else
  3763. printk("?");
  3764. #if (BITS_PER_LONG == 32)
  3765. if (state == TASK_RUNNING)
  3766. printk(" running ");
  3767. else
  3768. printk(" %08lX ", thread_saved_pc(p));
  3769. #else
  3770. if (state == TASK_RUNNING)
  3771. printk(" running task ");
  3772. else
  3773. printk(" %016lx ", thread_saved_pc(p));
  3774. #endif
  3775. #ifdef CONFIG_DEBUG_STACK_USAGE
  3776. {
  3777. unsigned long *n = end_of_stack(p);
  3778. while (!*n)
  3779. n++;
  3780. free = (unsigned long)n - (unsigned long)end_of_stack(p);
  3781. }
  3782. #endif
  3783. printk("%5lu %5d %6d ", free, p->pid, p->parent->pid);
  3784. if ((relative = eldest_child(p)))
  3785. printk("%5d ", relative->pid);
  3786. else
  3787. printk(" ");
  3788. if ((relative = younger_sibling(p)))
  3789. printk("%7d", relative->pid);
  3790. else
  3791. printk(" ");
  3792. if ((relative = older_sibling(p)))
  3793. printk(" %5d", relative->pid);
  3794. else
  3795. printk(" ");
  3796. if (!p->mm)
  3797. printk(" (L-TLB)\n");
  3798. else
  3799. printk(" (NOTLB)\n");
  3800. if (state != TASK_RUNNING)
  3801. show_stack(p, NULL);
  3802. }
  3803. void show_state(void)
  3804. {
  3805. task_t *g, *p;
  3806. #if (BITS_PER_LONG == 32)
  3807. printk("\n"
  3808. " sibling\n");
  3809. printk(" task PC pid father child younger older\n");
  3810. #else
  3811. printk("\n"
  3812. " sibling\n");
  3813. printk(" task PC pid father child younger older\n");
  3814. #endif
  3815. read_lock(&tasklist_lock);
  3816. do_each_thread(g, p) {
  3817. /*
  3818. * reset the NMI-timeout, listing all files on a slow
  3819. * console might take alot of time:
  3820. */
  3821. touch_nmi_watchdog();
  3822. show_task(p);
  3823. } while_each_thread(g, p);
  3824. read_unlock(&tasklist_lock);
  3825. mutex_debug_show_all_locks();
  3826. }
  3827. /**
  3828. * init_idle - set up an idle thread for a given CPU
  3829. * @idle: task in question
  3830. * @cpu: cpu the idle task belongs to
  3831. *
  3832. * NOTE: this function does not set the idle thread's NEED_RESCHED
  3833. * flag, to make booting more robust.
  3834. */
  3835. void __devinit init_idle(task_t *idle, int cpu)
  3836. {
  3837. runqueue_t *rq = cpu_rq(cpu);
  3838. unsigned long flags;
  3839. idle->sleep_avg = 0;
  3840. idle->array = NULL;
  3841. idle->prio = MAX_PRIO;
  3842. idle->state = TASK_RUNNING;
  3843. idle->cpus_allowed = cpumask_of_cpu(cpu);
  3844. set_task_cpu(idle, cpu);
  3845. spin_lock_irqsave(&rq->lock, flags);
  3846. rq->curr = rq->idle = idle;
  3847. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  3848. idle->oncpu = 1;
  3849. #endif
  3850. spin_unlock_irqrestore(&rq->lock, flags);
  3851. /* Set the preempt count _outside_ the spinlocks! */
  3852. #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
  3853. task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
  3854. #else
  3855. task_thread_info(idle)->preempt_count = 0;
  3856. #endif
  3857. }
  3858. /*
  3859. * In a system that switches off the HZ timer nohz_cpu_mask
  3860. * indicates which cpus entered this state. This is used
  3861. * in the rcu update to wait only for active cpus. For system
  3862. * which do not switch off the HZ timer nohz_cpu_mask should
  3863. * always be CPU_MASK_NONE.
  3864. */
  3865. cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
  3866. #ifdef CONFIG_SMP
  3867. /*
  3868. * This is how migration works:
  3869. *
  3870. * 1) we queue a migration_req_t structure in the source CPU's
  3871. * runqueue and wake up that CPU's migration thread.
  3872. * 2) we down() the locked semaphore => thread blocks.
  3873. * 3) migration thread wakes up (implicitly it forces the migrated
  3874. * thread off the CPU)
  3875. * 4) it gets the migration request and checks whether the migrated
  3876. * task is still in the wrong runqueue.
  3877. * 5) if it's in the wrong runqueue then the migration thread removes
  3878. * it and puts it into the right queue.
  3879. * 6) migration thread up()s the semaphore.
  3880. * 7) we wake up and the migration is done.
  3881. */
  3882. /*
  3883. * Change a given task's CPU affinity. Migrate the thread to a
  3884. * proper CPU and schedule it away if the CPU it's executing on
  3885. * is removed from the allowed bitmask.
  3886. *
  3887. * NOTE: the caller must have a valid reference to the task, the
  3888. * task must not exit() & deallocate itself prematurely. The
  3889. * call is not atomic; no spinlocks may be held.
  3890. */
  3891. int set_cpus_allowed(task_t *p, cpumask_t new_mask)
  3892. {
  3893. unsigned long flags;
  3894. int ret = 0;
  3895. migration_req_t req;
  3896. runqueue_t *rq;
  3897. rq = task_rq_lock(p, &flags);
  3898. if (!cpus_intersects(new_mask, cpu_online_map)) {
  3899. ret = -EINVAL;
  3900. goto out;
  3901. }
  3902. p->cpus_allowed = new_mask;
  3903. /* Can the task run on the task's current CPU? If so, we're done */
  3904. if (cpu_isset(task_cpu(p), new_mask))
  3905. goto out;
  3906. if (migrate_task(p, any_online_cpu(new_mask), &req)) {
  3907. /* Need help from migration thread: drop lock and wait. */
  3908. task_rq_unlock(rq, &flags);
  3909. wake_up_process(rq->migration_thread);
  3910. wait_for_completion(&req.done);
  3911. tlb_migrate_finish(p->mm);
  3912. return 0;
  3913. }
  3914. out:
  3915. task_rq_unlock(rq, &flags);
  3916. return ret;
  3917. }
  3918. EXPORT_SYMBOL_GPL(set_cpus_allowed);
  3919. /*
  3920. * Move (not current) task off this cpu, onto dest cpu. We're doing
  3921. * this because either it can't run here any more (set_cpus_allowed()
  3922. * away from this CPU, or CPU going down), or because we're
  3923. * attempting to rebalance this task on exec (sched_exec).
  3924. *
  3925. * So we race with normal scheduler movements, but that's OK, as long
  3926. * as the task is no longer on this CPU.
  3927. */
  3928. static void __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  3929. {
  3930. runqueue_t *rq_dest, *rq_src;
  3931. if (unlikely(cpu_is_offline(dest_cpu)))
  3932. return;
  3933. rq_src = cpu_rq(src_cpu);
  3934. rq_dest = cpu_rq(dest_cpu);
  3935. double_rq_lock(rq_src, rq_dest);
  3936. /* Already moved. */
  3937. if (task_cpu(p) != src_cpu)
  3938. goto out;
  3939. /* Affinity changed (again). */
  3940. if (!cpu_isset(dest_cpu, p->cpus_allowed))
  3941. goto out;
  3942. set_task_cpu(p, dest_cpu);
  3943. if (p->array) {
  3944. /*
  3945. * Sync timestamp with rq_dest's before activating.
  3946. * The same thing could be achieved by doing this step
  3947. * afterwards, and pretending it was a local activate.
  3948. * This way is cleaner and logically correct.
  3949. */
  3950. p->timestamp = p->timestamp - rq_src->timestamp_last_tick
  3951. + rq_dest->timestamp_last_tick;
  3952. deactivate_task(p, rq_src);
  3953. activate_task(p, rq_dest, 0);
  3954. if (TASK_PREEMPTS_CURR(p, rq_dest))
  3955. resched_task(rq_dest->curr);
  3956. }
  3957. out:
  3958. double_rq_unlock(rq_src, rq_dest);
  3959. }
  3960. /*
  3961. * migration_thread - this is a highprio system thread that performs
  3962. * thread migration by bumping thread off CPU then 'pushing' onto
  3963. * another runqueue.
  3964. */
  3965. static int migration_thread(void *data)
  3966. {
  3967. runqueue_t *rq;
  3968. int cpu = (long)data;
  3969. rq = cpu_rq(cpu);
  3970. BUG_ON(rq->migration_thread != current);
  3971. set_current_state(TASK_INTERRUPTIBLE);
  3972. while (!kthread_should_stop()) {
  3973. struct list_head *head;
  3974. migration_req_t *req;
  3975. try_to_freeze();
  3976. spin_lock_irq(&rq->lock);
  3977. if (cpu_is_offline(cpu)) {
  3978. spin_unlock_irq(&rq->lock);
  3979. goto wait_to_die;
  3980. }
  3981. if (rq->active_balance) {
  3982. active_load_balance(rq, cpu);
  3983. rq->active_balance = 0;
  3984. }
  3985. head = &rq->migration_queue;
  3986. if (list_empty(head)) {
  3987. spin_unlock_irq(&rq->lock);
  3988. schedule();
  3989. set_current_state(TASK_INTERRUPTIBLE);
  3990. continue;
  3991. }
  3992. req = list_entry(head->next, migration_req_t, list);
  3993. list_del_init(head->next);
  3994. spin_unlock(&rq->lock);
  3995. __migrate_task(req->task, cpu, req->dest_cpu);
  3996. local_irq_enable();
  3997. complete(&req->done);
  3998. }
  3999. __set_current_state(TASK_RUNNING);
  4000. return 0;
  4001. wait_to_die:
  4002. /* Wait for kthread_stop */
  4003. set_current_state(TASK_INTERRUPTIBLE);
  4004. while (!kthread_should_stop()) {
  4005. schedule();
  4006. set_current_state(TASK_INTERRUPTIBLE);
  4007. }
  4008. __set_current_state(TASK_RUNNING);
  4009. return 0;
  4010. }
  4011. #ifdef CONFIG_HOTPLUG_CPU
  4012. /* Figure out where task on dead CPU should go, use force if neccessary. */
  4013. static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk)
  4014. {
  4015. int dest_cpu;
  4016. cpumask_t mask;
  4017. /* On same node? */
  4018. mask = node_to_cpumask(cpu_to_node(dead_cpu));
  4019. cpus_and(mask, mask, tsk->cpus_allowed);
  4020. dest_cpu = any_online_cpu(mask);
  4021. /* On any allowed CPU? */
  4022. if (dest_cpu == NR_CPUS)
  4023. dest_cpu = any_online_cpu(tsk->cpus_allowed);
  4024. /* No more Mr. Nice Guy. */
  4025. if (dest_cpu == NR_CPUS) {
  4026. cpus_setall(tsk->cpus_allowed);
  4027. dest_cpu = any_online_cpu(tsk->cpus_allowed);
  4028. /*
  4029. * Don't tell them about moving exiting tasks or
  4030. * kernel threads (both mm NULL), since they never
  4031. * leave kernel.
  4032. */
  4033. if (tsk->mm && printk_ratelimit())
  4034. printk(KERN_INFO "process %d (%s) no "
  4035. "longer affine to cpu%d\n",
  4036. tsk->pid, tsk->comm, dead_cpu);
  4037. }
  4038. __migrate_task(tsk, dead_cpu, dest_cpu);
  4039. }
  4040. /*
  4041. * While a dead CPU has no uninterruptible tasks queued at this point,
  4042. * it might still have a nonzero ->nr_uninterruptible counter, because
  4043. * for performance reasons the counter is not stricly tracking tasks to
  4044. * their home CPUs. So we just add the counter to another CPU's counter,
  4045. * to keep the global sum constant after CPU-down:
  4046. */
  4047. static void migrate_nr_uninterruptible(runqueue_t *rq_src)
  4048. {
  4049. runqueue_t *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
  4050. unsigned long flags;
  4051. local_irq_save(flags);
  4052. double_rq_lock(rq_src, rq_dest);
  4053. rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
  4054. rq_src->nr_uninterruptible = 0;
  4055. double_rq_unlock(rq_src, rq_dest);
  4056. local_irq_restore(flags);
  4057. }
  4058. /* Run through task list and migrate tasks from the dead cpu. */
  4059. static void migrate_live_tasks(int src_cpu)
  4060. {
  4061. struct task_struct *tsk, *t;
  4062. write_lock_irq(&tasklist_lock);
  4063. do_each_thread(t, tsk) {
  4064. if (tsk == current)
  4065. continue;
  4066. if (task_cpu(tsk) == src_cpu)
  4067. move_task_off_dead_cpu(src_cpu, tsk);
  4068. } while_each_thread(t, tsk);
  4069. write_unlock_irq(&tasklist_lock);
  4070. }
  4071. /* Schedules idle task to be the next runnable task on current CPU.
  4072. * It does so by boosting its priority to highest possible and adding it to
  4073. * the _front_ of runqueue. Used by CPU offline code.
  4074. */
  4075. void sched_idle_next(void)
  4076. {
  4077. int cpu = smp_processor_id();
  4078. runqueue_t *rq = this_rq();
  4079. struct task_struct *p = rq->idle;
  4080. unsigned long flags;
  4081. /* cpu has to be offline */
  4082. BUG_ON(cpu_online(cpu));
  4083. /* Strictly not necessary since rest of the CPUs are stopped by now
  4084. * and interrupts disabled on current cpu.
  4085. */
  4086. spin_lock_irqsave(&rq->lock, flags);
  4087. __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1);
  4088. /* Add idle task to _front_ of it's priority queue */
  4089. __activate_idle_task(p, rq);
  4090. spin_unlock_irqrestore(&rq->lock, flags);
  4091. }
  4092. /* Ensures that the idle task is using init_mm right before its cpu goes
  4093. * offline.
  4094. */
  4095. void idle_task_exit(void)
  4096. {
  4097. struct mm_struct *mm = current->active_mm;
  4098. BUG_ON(cpu_online(smp_processor_id()));
  4099. if (mm != &init_mm)
  4100. switch_mm(mm, &init_mm, current);
  4101. mmdrop(mm);
  4102. }
  4103. static void migrate_dead(unsigned int dead_cpu, task_t *tsk)
  4104. {
  4105. struct runqueue *rq = cpu_rq(dead_cpu);
  4106. /* Must be exiting, otherwise would be on tasklist. */
  4107. BUG_ON(tsk->exit_state != EXIT_ZOMBIE && tsk->exit_state != EXIT_DEAD);
  4108. /* Cannot have done final schedule yet: would have vanished. */
  4109. BUG_ON(tsk->flags & PF_DEAD);
  4110. get_task_struct(tsk);
  4111. /*
  4112. * Drop lock around migration; if someone else moves it,
  4113. * that's OK. No task can be added to this CPU, so iteration is
  4114. * fine.
  4115. */
  4116. spin_unlock_irq(&rq->lock);
  4117. move_task_off_dead_cpu(dead_cpu, tsk);
  4118. spin_lock_irq(&rq->lock);
  4119. put_task_struct(tsk);
  4120. }
  4121. /* release_task() removes task from tasklist, so we won't find dead tasks. */
  4122. static void migrate_dead_tasks(unsigned int dead_cpu)
  4123. {
  4124. unsigned arr, i;
  4125. struct runqueue *rq = cpu_rq(dead_cpu);
  4126. for (arr = 0; arr < 2; arr++) {
  4127. for (i = 0; i < MAX_PRIO; i++) {
  4128. struct list_head *list = &rq->arrays[arr].queue[i];
  4129. while (!list_empty(list))
  4130. migrate_dead(dead_cpu,
  4131. list_entry(list->next, task_t,
  4132. run_list));
  4133. }
  4134. }
  4135. }
  4136. #endif /* CONFIG_HOTPLUG_CPU */
  4137. /*
  4138. * migration_call - callback that gets triggered when a CPU is added.
  4139. * Here we can start up the necessary migration thread for the new CPU.
  4140. */
  4141. static int migration_call(struct notifier_block *nfb, unsigned long action,
  4142. void *hcpu)
  4143. {
  4144. int cpu = (long)hcpu;
  4145. struct task_struct *p;
  4146. struct runqueue *rq;
  4147. unsigned long flags;
  4148. switch (action) {
  4149. case CPU_UP_PREPARE:
  4150. p = kthread_create(migration_thread, hcpu, "migration/%d",cpu);
  4151. if (IS_ERR(p))
  4152. return NOTIFY_BAD;
  4153. p->flags |= PF_NOFREEZE;
  4154. kthread_bind(p, cpu);
  4155. /* Must be high prio: stop_machine expects to yield to it. */
  4156. rq = task_rq_lock(p, &flags);
  4157. __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1);
  4158. task_rq_unlock(rq, &flags);
  4159. cpu_rq(cpu)->migration_thread = p;
  4160. break;
  4161. case CPU_ONLINE:
  4162. /* Strictly unneccessary, as first user will wake it. */
  4163. wake_up_process(cpu_rq(cpu)->migration_thread);
  4164. break;
  4165. #ifdef CONFIG_HOTPLUG_CPU
  4166. case CPU_UP_CANCELED:
  4167. /* Unbind it from offline cpu so it can run. Fall thru. */
  4168. kthread_bind(cpu_rq(cpu)->migration_thread,
  4169. any_online_cpu(cpu_online_map));
  4170. kthread_stop(cpu_rq(cpu)->migration_thread);
  4171. cpu_rq(cpu)->migration_thread = NULL;
  4172. break;
  4173. case CPU_DEAD:
  4174. migrate_live_tasks(cpu);
  4175. rq = cpu_rq(cpu);
  4176. kthread_stop(rq->migration_thread);
  4177. rq->migration_thread = NULL;
  4178. /* Idle task back to normal (off runqueue, low prio) */
  4179. rq = task_rq_lock(rq->idle, &flags);
  4180. deactivate_task(rq->idle, rq);
  4181. rq->idle->static_prio = MAX_PRIO;
  4182. __setscheduler(rq->idle, SCHED_NORMAL, 0);
  4183. migrate_dead_tasks(cpu);
  4184. task_rq_unlock(rq, &flags);
  4185. migrate_nr_uninterruptible(rq);
  4186. BUG_ON(rq->nr_running != 0);
  4187. /* No need to migrate the tasks: it was best-effort if
  4188. * they didn't do lock_cpu_hotplug(). Just wake up
  4189. * the requestors. */
  4190. spin_lock_irq(&rq->lock);
  4191. while (!list_empty(&rq->migration_queue)) {
  4192. migration_req_t *req;
  4193. req = list_entry(rq->migration_queue.next,
  4194. migration_req_t, list);
  4195. list_del_init(&req->list);
  4196. complete(&req->done);
  4197. }
  4198. spin_unlock_irq(&rq->lock);
  4199. break;
  4200. #endif
  4201. }
  4202. return NOTIFY_OK;
  4203. }
  4204. /* Register at highest priority so that task migration (migrate_all_tasks)
  4205. * happens before everything else.
  4206. */
  4207. static struct notifier_block __devinitdata migration_notifier = {
  4208. .notifier_call = migration_call,
  4209. .priority = 10
  4210. };
  4211. int __init migration_init(void)
  4212. {
  4213. void *cpu = (void *)(long)smp_processor_id();
  4214. /* Start one for boot CPU. */
  4215. migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  4216. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  4217. register_cpu_notifier(&migration_notifier);
  4218. return 0;
  4219. }
  4220. #endif
  4221. #ifdef CONFIG_SMP
  4222. #undef SCHED_DOMAIN_DEBUG
  4223. #ifdef SCHED_DOMAIN_DEBUG
  4224. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  4225. {
  4226. int level = 0;
  4227. if (!sd) {
  4228. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  4229. return;
  4230. }
  4231. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  4232. do {
  4233. int i;
  4234. char str[NR_CPUS];
  4235. struct sched_group *group = sd->groups;
  4236. cpumask_t groupmask;
  4237. cpumask_scnprintf(str, NR_CPUS, sd->span);
  4238. cpus_clear(groupmask);
  4239. printk(KERN_DEBUG);
  4240. for (i = 0; i < level + 1; i++)
  4241. printk(" ");
  4242. printk("domain %d: ", level);
  4243. if (!(sd->flags & SD_LOAD_BALANCE)) {
  4244. printk("does not load-balance\n");
  4245. if (sd->parent)
  4246. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain has parent");
  4247. break;
  4248. }
  4249. printk("span %s\n", str);
  4250. if (!cpu_isset(cpu, sd->span))
  4251. printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu);
  4252. if (!cpu_isset(cpu, group->cpumask))
  4253. printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu);
  4254. printk(KERN_DEBUG);
  4255. for (i = 0; i < level + 2; i++)
  4256. printk(" ");
  4257. printk("groups:");
  4258. do {
  4259. if (!group) {
  4260. printk("\n");
  4261. printk(KERN_ERR "ERROR: group is NULL\n");
  4262. break;
  4263. }
  4264. if (!group->cpu_power) {
  4265. printk("\n");
  4266. printk(KERN_ERR "ERROR: domain->cpu_power not set\n");
  4267. }
  4268. if (!cpus_weight(group->cpumask)) {
  4269. printk("\n");
  4270. printk(KERN_ERR "ERROR: empty group\n");
  4271. }
  4272. if (cpus_intersects(groupmask, group->cpumask)) {
  4273. printk("\n");
  4274. printk(KERN_ERR "ERROR: repeated CPUs\n");
  4275. }
  4276. cpus_or(groupmask, groupmask, group->cpumask);
  4277. cpumask_scnprintf(str, NR_CPUS, group->cpumask);
  4278. printk(" %s", str);
  4279. group = group->next;
  4280. } while (group != sd->groups);
  4281. printk("\n");
  4282. if (!cpus_equal(sd->span, groupmask))
  4283. printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  4284. level++;
  4285. sd = sd->parent;
  4286. if (sd) {
  4287. if (!cpus_subset(groupmask, sd->span))
  4288. printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n");
  4289. }
  4290. } while (sd);
  4291. }
  4292. #else
  4293. #define sched_domain_debug(sd, cpu) {}
  4294. #endif
  4295. static int sd_degenerate(struct sched_domain *sd)
  4296. {
  4297. if (cpus_weight(sd->span) == 1)
  4298. return 1;
  4299. /* Following flags need at least 2 groups */
  4300. if (sd->flags & (SD_LOAD_BALANCE |
  4301. SD_BALANCE_NEWIDLE |
  4302. SD_BALANCE_FORK |
  4303. SD_BALANCE_EXEC)) {
  4304. if (sd->groups != sd->groups->next)
  4305. return 0;
  4306. }
  4307. /* Following flags don't use groups */
  4308. if (sd->flags & (SD_WAKE_IDLE |
  4309. SD_WAKE_AFFINE |
  4310. SD_WAKE_BALANCE))
  4311. return 0;
  4312. return 1;
  4313. }
  4314. static int sd_parent_degenerate(struct sched_domain *sd,
  4315. struct sched_domain *parent)
  4316. {
  4317. unsigned long cflags = sd->flags, pflags = parent->flags;
  4318. if (sd_degenerate(parent))
  4319. return 1;
  4320. if (!cpus_equal(sd->span, parent->span))
  4321. return 0;
  4322. /* Does parent contain flags not in child? */
  4323. /* WAKE_BALANCE is a subset of WAKE_AFFINE */
  4324. if (cflags & SD_WAKE_AFFINE)
  4325. pflags &= ~SD_WAKE_BALANCE;
  4326. /* Flags needing groups don't count if only 1 group in parent */
  4327. if (parent->groups == parent->groups->next) {
  4328. pflags &= ~(SD_LOAD_BALANCE |
  4329. SD_BALANCE_NEWIDLE |
  4330. SD_BALANCE_FORK |
  4331. SD_BALANCE_EXEC);
  4332. }
  4333. if (~cflags & pflags)
  4334. return 0;
  4335. return 1;
  4336. }
  4337. /*
  4338. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  4339. * hold the hotplug lock.
  4340. */
  4341. static void cpu_attach_domain(struct sched_domain *sd, int cpu)
  4342. {
  4343. runqueue_t *rq = cpu_rq(cpu);
  4344. struct sched_domain *tmp;
  4345. /* Remove the sched domains which do not contribute to scheduling. */
  4346. for (tmp = sd; tmp; tmp = tmp->parent) {
  4347. struct sched_domain *parent = tmp->parent;
  4348. if (!parent)
  4349. break;
  4350. if (sd_parent_degenerate(tmp, parent))
  4351. tmp->parent = parent->parent;
  4352. }
  4353. if (sd && sd_degenerate(sd))
  4354. sd = sd->parent;
  4355. sched_domain_debug(sd, cpu);
  4356. rcu_assign_pointer(rq->sd, sd);
  4357. }
  4358. /* cpus with isolated domains */
  4359. static cpumask_t __devinitdata cpu_isolated_map = CPU_MASK_NONE;
  4360. /* Setup the mask of cpus configured for isolated domains */
  4361. static int __init isolated_cpu_setup(char *str)
  4362. {
  4363. int ints[NR_CPUS], i;
  4364. str = get_options(str, ARRAY_SIZE(ints), ints);
  4365. cpus_clear(cpu_isolated_map);
  4366. for (i = 1; i <= ints[0]; i++)
  4367. if (ints[i] < NR_CPUS)
  4368. cpu_set(ints[i], cpu_isolated_map);
  4369. return 1;
  4370. }
  4371. __setup ("isolcpus=", isolated_cpu_setup);
  4372. /*
  4373. * init_sched_build_groups takes an array of groups, the cpumask we wish
  4374. * to span, and a pointer to a function which identifies what group a CPU
  4375. * belongs to. The return value of group_fn must be a valid index into the
  4376. * groups[] array, and must be >= 0 and < NR_CPUS (due to the fact that we
  4377. * keep track of groups covered with a cpumask_t).
  4378. *
  4379. * init_sched_build_groups will build a circular linked list of the groups
  4380. * covered by the given span, and will set each group's ->cpumask correctly,
  4381. * and ->cpu_power to 0.
  4382. */
  4383. static void init_sched_build_groups(struct sched_group groups[], cpumask_t span,
  4384. int (*group_fn)(int cpu))
  4385. {
  4386. struct sched_group *first = NULL, *last = NULL;
  4387. cpumask_t covered = CPU_MASK_NONE;
  4388. int i;
  4389. for_each_cpu_mask(i, span) {
  4390. int group = group_fn(i);
  4391. struct sched_group *sg = &groups[group];
  4392. int j;
  4393. if (cpu_isset(i, covered))
  4394. continue;
  4395. sg->cpumask = CPU_MASK_NONE;
  4396. sg->cpu_power = 0;
  4397. for_each_cpu_mask(j, span) {
  4398. if (group_fn(j) != group)
  4399. continue;
  4400. cpu_set(j, covered);
  4401. cpu_set(j, sg->cpumask);
  4402. }
  4403. if (!first)
  4404. first = sg;
  4405. if (last)
  4406. last->next = sg;
  4407. last = sg;
  4408. }
  4409. last->next = first;
  4410. }
  4411. #define SD_NODES_PER_DOMAIN 16
  4412. #ifdef CONFIG_NUMA
  4413. /**
  4414. * find_next_best_node - find the next node to include in a sched_domain
  4415. * @node: node whose sched_domain we're building
  4416. * @used_nodes: nodes already in the sched_domain
  4417. *
  4418. * Find the next node to include in a given scheduling domain. Simply
  4419. * finds the closest node not already in the @used_nodes map.
  4420. *
  4421. * Should use nodemask_t.
  4422. */
  4423. static int find_next_best_node(int node, unsigned long *used_nodes)
  4424. {
  4425. int i, n, val, min_val, best_node = 0;
  4426. min_val = INT_MAX;
  4427. for (i = 0; i < MAX_NUMNODES; i++) {
  4428. /* Start at @node */
  4429. n = (node + i) % MAX_NUMNODES;
  4430. if (!nr_cpus_node(n))
  4431. continue;
  4432. /* Skip already used nodes */
  4433. if (test_bit(n, used_nodes))
  4434. continue;
  4435. /* Simple min distance search */
  4436. val = node_distance(node, n);
  4437. if (val < min_val) {
  4438. min_val = val;
  4439. best_node = n;
  4440. }
  4441. }
  4442. set_bit(best_node, used_nodes);
  4443. return best_node;
  4444. }
  4445. /**
  4446. * sched_domain_node_span - get a cpumask for a node's sched_domain
  4447. * @node: node whose cpumask we're constructing
  4448. * @size: number of nodes to include in this span
  4449. *
  4450. * Given a node, construct a good cpumask for its sched_domain to span. It
  4451. * should be one that prevents unnecessary balancing, but also spreads tasks
  4452. * out optimally.
  4453. */
  4454. static cpumask_t sched_domain_node_span(int node)
  4455. {
  4456. int i;
  4457. cpumask_t span, nodemask;
  4458. DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
  4459. cpus_clear(span);
  4460. bitmap_zero(used_nodes, MAX_NUMNODES);
  4461. nodemask = node_to_cpumask(node);
  4462. cpus_or(span, span, nodemask);
  4463. set_bit(node, used_nodes);
  4464. for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  4465. int next_node = find_next_best_node(node, used_nodes);
  4466. nodemask = node_to_cpumask(next_node);
  4467. cpus_or(span, span, nodemask);
  4468. }
  4469. return span;
  4470. }
  4471. #endif
  4472. /*
  4473. * At the moment, CONFIG_SCHED_SMT is never defined, but leave it in so we
  4474. * can switch it on easily if needed.
  4475. */
  4476. #ifdef CONFIG_SCHED_SMT
  4477. static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
  4478. static struct sched_group sched_group_cpus[NR_CPUS];
  4479. static int cpu_to_cpu_group(int cpu)
  4480. {
  4481. return cpu;
  4482. }
  4483. #endif
  4484. static DEFINE_PER_CPU(struct sched_domain, phys_domains);
  4485. static struct sched_group sched_group_phys[NR_CPUS];
  4486. static int cpu_to_phys_group(int cpu)
  4487. {
  4488. #ifdef CONFIG_SCHED_SMT
  4489. return first_cpu(cpu_sibling_map[cpu]);
  4490. #else
  4491. return cpu;
  4492. #endif
  4493. }
  4494. #ifdef CONFIG_NUMA
  4495. /*
  4496. * The init_sched_build_groups can't handle what we want to do with node
  4497. * groups, so roll our own. Now each node has its own list of groups which
  4498. * gets dynamically allocated.
  4499. */
  4500. static DEFINE_PER_CPU(struct sched_domain, node_domains);
  4501. static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
  4502. static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
  4503. static struct sched_group *sched_group_allnodes_bycpu[NR_CPUS];
  4504. static int cpu_to_allnodes_group(int cpu)
  4505. {
  4506. return cpu_to_node(cpu);
  4507. }
  4508. #endif
  4509. /*
  4510. * Build sched domains for a given set of cpus and attach the sched domains
  4511. * to the individual cpus
  4512. */
  4513. void build_sched_domains(const cpumask_t *cpu_map)
  4514. {
  4515. int i;
  4516. #ifdef CONFIG_NUMA
  4517. struct sched_group **sched_group_nodes = NULL;
  4518. struct sched_group *sched_group_allnodes = NULL;
  4519. /*
  4520. * Allocate the per-node list of sched groups
  4521. */
  4522. sched_group_nodes = kmalloc(sizeof(struct sched_group*)*MAX_NUMNODES,
  4523. GFP_ATOMIC);
  4524. if (!sched_group_nodes) {
  4525. printk(KERN_WARNING "Can not alloc sched group node list\n");
  4526. return;
  4527. }
  4528. sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
  4529. #endif
  4530. /*
  4531. * Set up domains for cpus specified by the cpu_map.
  4532. */
  4533. for_each_cpu_mask(i, *cpu_map) {
  4534. int group;
  4535. struct sched_domain *sd = NULL, *p;
  4536. cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
  4537. cpus_and(nodemask, nodemask, *cpu_map);
  4538. #ifdef CONFIG_NUMA
  4539. if (cpus_weight(*cpu_map)
  4540. > SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
  4541. if (!sched_group_allnodes) {
  4542. sched_group_allnodes
  4543. = kmalloc(sizeof(struct sched_group)
  4544. * MAX_NUMNODES,
  4545. GFP_KERNEL);
  4546. if (!sched_group_allnodes) {
  4547. printk(KERN_WARNING
  4548. "Can not alloc allnodes sched group\n");
  4549. break;
  4550. }
  4551. sched_group_allnodes_bycpu[i]
  4552. = sched_group_allnodes;
  4553. }
  4554. sd = &per_cpu(allnodes_domains, i);
  4555. *sd = SD_ALLNODES_INIT;
  4556. sd->span = *cpu_map;
  4557. group = cpu_to_allnodes_group(i);
  4558. sd->groups = &sched_group_allnodes[group];
  4559. p = sd;
  4560. } else
  4561. p = NULL;
  4562. sd = &per_cpu(node_domains, i);
  4563. *sd = SD_NODE_INIT;
  4564. sd->span = sched_domain_node_span(cpu_to_node(i));
  4565. sd->parent = p;
  4566. cpus_and(sd->span, sd->span, *cpu_map);
  4567. #endif
  4568. p = sd;
  4569. sd = &per_cpu(phys_domains, i);
  4570. group = cpu_to_phys_group(i);
  4571. *sd = SD_CPU_INIT;
  4572. sd->span = nodemask;
  4573. sd->parent = p;
  4574. sd->groups = &sched_group_phys[group];
  4575. #ifdef CONFIG_SCHED_SMT
  4576. p = sd;
  4577. sd = &per_cpu(cpu_domains, i);
  4578. group = cpu_to_cpu_group(i);
  4579. *sd = SD_SIBLING_INIT;
  4580. sd->span = cpu_sibling_map[i];
  4581. cpus_and(sd->span, sd->span, *cpu_map);
  4582. sd->parent = p;
  4583. sd->groups = &sched_group_cpus[group];
  4584. #endif
  4585. }
  4586. #ifdef CONFIG_SCHED_SMT
  4587. /* Set up CPU (sibling) groups */
  4588. for_each_cpu_mask(i, *cpu_map) {
  4589. cpumask_t this_sibling_map = cpu_sibling_map[i];
  4590. cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
  4591. if (i != first_cpu(this_sibling_map))
  4592. continue;
  4593. init_sched_build_groups(sched_group_cpus, this_sibling_map,
  4594. &cpu_to_cpu_group);
  4595. }
  4596. #endif
  4597. /* Set up physical groups */
  4598. for (i = 0; i < MAX_NUMNODES; i++) {
  4599. cpumask_t nodemask = node_to_cpumask(i);
  4600. cpus_and(nodemask, nodemask, *cpu_map);
  4601. if (cpus_empty(nodemask))
  4602. continue;
  4603. init_sched_build_groups(sched_group_phys, nodemask,
  4604. &cpu_to_phys_group);
  4605. }
  4606. #ifdef CONFIG_NUMA
  4607. /* Set up node groups */
  4608. if (sched_group_allnodes)
  4609. init_sched_build_groups(sched_group_allnodes, *cpu_map,
  4610. &cpu_to_allnodes_group);
  4611. for (i = 0; i < MAX_NUMNODES; i++) {
  4612. /* Set up node groups */
  4613. struct sched_group *sg, *prev;
  4614. cpumask_t nodemask = node_to_cpumask(i);
  4615. cpumask_t domainspan;
  4616. cpumask_t covered = CPU_MASK_NONE;
  4617. int j;
  4618. cpus_and(nodemask, nodemask, *cpu_map);
  4619. if (cpus_empty(nodemask)) {
  4620. sched_group_nodes[i] = NULL;
  4621. continue;
  4622. }
  4623. domainspan = sched_domain_node_span(i);
  4624. cpus_and(domainspan, domainspan, *cpu_map);
  4625. sg = kmalloc(sizeof(struct sched_group), GFP_KERNEL);
  4626. sched_group_nodes[i] = sg;
  4627. for_each_cpu_mask(j, nodemask) {
  4628. struct sched_domain *sd;
  4629. sd = &per_cpu(node_domains, j);
  4630. sd->groups = sg;
  4631. if (sd->groups == NULL) {
  4632. /* Turn off balancing if we have no groups */
  4633. sd->flags = 0;
  4634. }
  4635. }
  4636. if (!sg) {
  4637. printk(KERN_WARNING
  4638. "Can not alloc domain group for node %d\n", i);
  4639. continue;
  4640. }
  4641. sg->cpu_power = 0;
  4642. sg->cpumask = nodemask;
  4643. cpus_or(covered, covered, nodemask);
  4644. prev = sg;
  4645. for (j = 0; j < MAX_NUMNODES; j++) {
  4646. cpumask_t tmp, notcovered;
  4647. int n = (i + j) % MAX_NUMNODES;
  4648. cpus_complement(notcovered, covered);
  4649. cpus_and(tmp, notcovered, *cpu_map);
  4650. cpus_and(tmp, tmp, domainspan);
  4651. if (cpus_empty(tmp))
  4652. break;
  4653. nodemask = node_to_cpumask(n);
  4654. cpus_and(tmp, tmp, nodemask);
  4655. if (cpus_empty(tmp))
  4656. continue;
  4657. sg = kmalloc(sizeof(struct sched_group), GFP_KERNEL);
  4658. if (!sg) {
  4659. printk(KERN_WARNING
  4660. "Can not alloc domain group for node %d\n", j);
  4661. break;
  4662. }
  4663. sg->cpu_power = 0;
  4664. sg->cpumask = tmp;
  4665. cpus_or(covered, covered, tmp);
  4666. prev->next = sg;
  4667. prev = sg;
  4668. }
  4669. prev->next = sched_group_nodes[i];
  4670. }
  4671. #endif
  4672. /* Calculate CPU power for physical packages and nodes */
  4673. for_each_cpu_mask(i, *cpu_map) {
  4674. int power;
  4675. struct sched_domain *sd;
  4676. #ifdef CONFIG_SCHED_SMT
  4677. sd = &per_cpu(cpu_domains, i);
  4678. power = SCHED_LOAD_SCALE;
  4679. sd->groups->cpu_power = power;
  4680. #endif
  4681. sd = &per_cpu(phys_domains, i);
  4682. power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE *
  4683. (cpus_weight(sd->groups->cpumask)-1) / 10;
  4684. sd->groups->cpu_power = power;
  4685. #ifdef CONFIG_NUMA
  4686. sd = &per_cpu(allnodes_domains, i);
  4687. if (sd->groups) {
  4688. power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE *
  4689. (cpus_weight(sd->groups->cpumask)-1) / 10;
  4690. sd->groups->cpu_power = power;
  4691. }
  4692. #endif
  4693. }
  4694. #ifdef CONFIG_NUMA
  4695. for (i = 0; i < MAX_NUMNODES; i++) {
  4696. struct sched_group *sg = sched_group_nodes[i];
  4697. int j;
  4698. if (sg == NULL)
  4699. continue;
  4700. next_sg:
  4701. for_each_cpu_mask(j, sg->cpumask) {
  4702. struct sched_domain *sd;
  4703. int power;
  4704. sd = &per_cpu(phys_domains, j);
  4705. if (j != first_cpu(sd->groups->cpumask)) {
  4706. /*
  4707. * Only add "power" once for each
  4708. * physical package.
  4709. */
  4710. continue;
  4711. }
  4712. power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE *
  4713. (cpus_weight(sd->groups->cpumask)-1) / 10;
  4714. sg->cpu_power += power;
  4715. }
  4716. sg = sg->next;
  4717. if (sg != sched_group_nodes[i])
  4718. goto next_sg;
  4719. }
  4720. #endif
  4721. /* Attach the domains */
  4722. for_each_cpu_mask(i, *cpu_map) {
  4723. struct sched_domain *sd;
  4724. #ifdef CONFIG_SCHED_SMT
  4725. sd = &per_cpu(cpu_domains, i);
  4726. #else
  4727. sd = &per_cpu(phys_domains, i);
  4728. #endif
  4729. cpu_attach_domain(sd, i);
  4730. }
  4731. }
  4732. /*
  4733. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  4734. */
  4735. static void arch_init_sched_domains(const cpumask_t *cpu_map)
  4736. {
  4737. cpumask_t cpu_default_map;
  4738. /*
  4739. * Setup mask for cpus without special case scheduling requirements.
  4740. * For now this just excludes isolated cpus, but could be used to
  4741. * exclude other special cases in the future.
  4742. */
  4743. cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map);
  4744. build_sched_domains(&cpu_default_map);
  4745. }
  4746. static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
  4747. {
  4748. #ifdef CONFIG_NUMA
  4749. int i;
  4750. int cpu;
  4751. for_each_cpu_mask(cpu, *cpu_map) {
  4752. struct sched_group *sched_group_allnodes
  4753. = sched_group_allnodes_bycpu[cpu];
  4754. struct sched_group **sched_group_nodes
  4755. = sched_group_nodes_bycpu[cpu];
  4756. if (sched_group_allnodes) {
  4757. kfree(sched_group_allnodes);
  4758. sched_group_allnodes_bycpu[cpu] = NULL;
  4759. }
  4760. if (!sched_group_nodes)
  4761. continue;
  4762. for (i = 0; i < MAX_NUMNODES; i++) {
  4763. cpumask_t nodemask = node_to_cpumask(i);
  4764. struct sched_group *oldsg, *sg = sched_group_nodes[i];
  4765. cpus_and(nodemask, nodemask, *cpu_map);
  4766. if (cpus_empty(nodemask))
  4767. continue;
  4768. if (sg == NULL)
  4769. continue;
  4770. sg = sg->next;
  4771. next_sg:
  4772. oldsg = sg;
  4773. sg = sg->next;
  4774. kfree(oldsg);
  4775. if (oldsg != sched_group_nodes[i])
  4776. goto next_sg;
  4777. }
  4778. kfree(sched_group_nodes);
  4779. sched_group_nodes_bycpu[cpu] = NULL;
  4780. }
  4781. #endif
  4782. }
  4783. /*
  4784. * Detach sched domains from a group of cpus specified in cpu_map
  4785. * These cpus will now be attached to the NULL domain
  4786. */
  4787. static inline void detach_destroy_domains(const cpumask_t *cpu_map)
  4788. {
  4789. int i;
  4790. for_each_cpu_mask(i, *cpu_map)
  4791. cpu_attach_domain(NULL, i);
  4792. synchronize_sched();
  4793. arch_destroy_sched_domains(cpu_map);
  4794. }
  4795. /*
  4796. * Partition sched domains as specified by the cpumasks below.
  4797. * This attaches all cpus from the cpumasks to the NULL domain,
  4798. * waits for a RCU quiescent period, recalculates sched
  4799. * domain information and then attaches them back to the
  4800. * correct sched domains
  4801. * Call with hotplug lock held
  4802. */
  4803. void partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
  4804. {
  4805. cpumask_t change_map;
  4806. cpus_and(*partition1, *partition1, cpu_online_map);
  4807. cpus_and(*partition2, *partition2, cpu_online_map);
  4808. cpus_or(change_map, *partition1, *partition2);
  4809. /* Detach sched domains from all of the affected cpus */
  4810. detach_destroy_domains(&change_map);
  4811. if (!cpus_empty(*partition1))
  4812. build_sched_domains(partition1);
  4813. if (!cpus_empty(*partition2))
  4814. build_sched_domains(partition2);
  4815. }
  4816. #ifdef CONFIG_HOTPLUG_CPU
  4817. /*
  4818. * Force a reinitialization of the sched domains hierarchy. The domains
  4819. * and groups cannot be updated in place without racing with the balancing
  4820. * code, so we temporarily attach all running cpus to the NULL domain
  4821. * which will prevent rebalancing while the sched domains are recalculated.
  4822. */
  4823. static int update_sched_domains(struct notifier_block *nfb,
  4824. unsigned long action, void *hcpu)
  4825. {
  4826. switch (action) {
  4827. case CPU_UP_PREPARE:
  4828. case CPU_DOWN_PREPARE:
  4829. detach_destroy_domains(&cpu_online_map);
  4830. return NOTIFY_OK;
  4831. case CPU_UP_CANCELED:
  4832. case CPU_DOWN_FAILED:
  4833. case CPU_ONLINE:
  4834. case CPU_DEAD:
  4835. /*
  4836. * Fall through and re-initialise the domains.
  4837. */
  4838. break;
  4839. default:
  4840. return NOTIFY_DONE;
  4841. }
  4842. /* The hotplug lock is already held by cpu_up/cpu_down */
  4843. arch_init_sched_domains(&cpu_online_map);
  4844. return NOTIFY_OK;
  4845. }
  4846. #endif
  4847. void __init sched_init_smp(void)
  4848. {
  4849. lock_cpu_hotplug();
  4850. arch_init_sched_domains(&cpu_online_map);
  4851. unlock_cpu_hotplug();
  4852. /* XXX: Theoretical race here - CPU may be hotplugged now */
  4853. hotcpu_notifier(update_sched_domains, 0);
  4854. }
  4855. #else
  4856. void __init sched_init_smp(void)
  4857. {
  4858. }
  4859. #endif /* CONFIG_SMP */
  4860. int in_sched_functions(unsigned long addr)
  4861. {
  4862. /* Linker adds these: start and end of __sched functions */
  4863. extern char __sched_text_start[], __sched_text_end[];
  4864. return in_lock_functions(addr) ||
  4865. (addr >= (unsigned long)__sched_text_start
  4866. && addr < (unsigned long)__sched_text_end);
  4867. }
  4868. void __init sched_init(void)
  4869. {
  4870. runqueue_t *rq;
  4871. int i, j, k;
  4872. for (i = 0; i < NR_CPUS; i++) {
  4873. prio_array_t *array;
  4874. rq = cpu_rq(i);
  4875. spin_lock_init(&rq->lock);
  4876. rq->nr_running = 0;
  4877. rq->active = rq->arrays;
  4878. rq->expired = rq->arrays + 1;
  4879. rq->best_expired_prio = MAX_PRIO;
  4880. #ifdef CONFIG_SMP
  4881. rq->sd = NULL;
  4882. for (j = 1; j < 3; j++)
  4883. rq->cpu_load[j] = 0;
  4884. rq->active_balance = 0;
  4885. rq->push_cpu = 0;
  4886. rq->migration_thread = NULL;
  4887. INIT_LIST_HEAD(&rq->migration_queue);
  4888. #endif
  4889. atomic_set(&rq->nr_iowait, 0);
  4890. for (j = 0; j < 2; j++) {
  4891. array = rq->arrays + j;
  4892. for (k = 0; k < MAX_PRIO; k++) {
  4893. INIT_LIST_HEAD(array->queue + k);
  4894. __clear_bit(k, array->bitmap);
  4895. }
  4896. // delimiter for bitsearch
  4897. __set_bit(MAX_PRIO, array->bitmap);
  4898. }
  4899. }
  4900. /*
  4901. * The boot idle thread does lazy MMU switching as well:
  4902. */
  4903. atomic_inc(&init_mm.mm_count);
  4904. enter_lazy_tlb(&init_mm, current);
  4905. /*
  4906. * Make us the idle thread. Technically, schedule() should not be
  4907. * called from this thread, however somewhere below it might be,
  4908. * but because we are the idle thread, we just pick up running again
  4909. * when this runqueue becomes "idle".
  4910. */
  4911. init_idle(current, smp_processor_id());
  4912. }
  4913. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  4914. void __might_sleep(char *file, int line)
  4915. {
  4916. #if defined(in_atomic)
  4917. static unsigned long prev_jiffy; /* ratelimiting */
  4918. if ((in_atomic() || irqs_disabled()) &&
  4919. system_state == SYSTEM_RUNNING && !oops_in_progress) {
  4920. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  4921. return;
  4922. prev_jiffy = jiffies;
  4923. printk(KERN_ERR "Debug: sleeping function called from invalid"
  4924. " context at %s:%d\n", file, line);
  4925. printk("in_atomic():%d, irqs_disabled():%d\n",
  4926. in_atomic(), irqs_disabled());
  4927. dump_stack();
  4928. }
  4929. #endif
  4930. }
  4931. EXPORT_SYMBOL(__might_sleep);
  4932. #endif
  4933. #ifdef CONFIG_MAGIC_SYSRQ
  4934. void normalize_rt_tasks(void)
  4935. {
  4936. struct task_struct *p;
  4937. prio_array_t *array;
  4938. unsigned long flags;
  4939. runqueue_t *rq;
  4940. read_lock_irq(&tasklist_lock);
  4941. for_each_process (p) {
  4942. if (!rt_task(p))
  4943. continue;
  4944. rq = task_rq_lock(p, &flags);
  4945. array = p->array;
  4946. if (array)
  4947. deactivate_task(p, task_rq(p));
  4948. __setscheduler(p, SCHED_NORMAL, 0);
  4949. if (array) {
  4950. __activate_task(p, task_rq(p));
  4951. resched_task(rq->curr);
  4952. }
  4953. task_rq_unlock(rq, &flags);
  4954. }
  4955. read_unlock_irq(&tasklist_lock);
  4956. }
  4957. #endif /* CONFIG_MAGIC_SYSRQ */
  4958. #ifdef CONFIG_IA64
  4959. /*
  4960. * These functions are only useful for the IA64 MCA handling.
  4961. *
  4962. * They can only be called when the whole system has been
  4963. * stopped - every CPU needs to be quiescent, and no scheduling
  4964. * activity can take place. Using them for anything else would
  4965. * be a serious bug, and as a result, they aren't even visible
  4966. * under any other configuration.
  4967. */
  4968. /**
  4969. * curr_task - return the current task for a given cpu.
  4970. * @cpu: the processor in question.
  4971. *
  4972. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  4973. */
  4974. task_t *curr_task(int cpu)
  4975. {
  4976. return cpu_curr(cpu);
  4977. }
  4978. /**
  4979. * set_curr_task - set the current task for a given cpu.
  4980. * @cpu: the processor in question.
  4981. * @p: the task pointer to set.
  4982. *
  4983. * Description: This function must only be used when non-maskable interrupts
  4984. * are serviced on a separate stack. It allows the architecture to switch the
  4985. * notion of the current task on a cpu in a non-blocking manner. This function
  4986. * must be called with all CPU's synchronized, and interrupts disabled, the
  4987. * and caller must save the original value of the current task (see
  4988. * curr_task() above) and restore that value before reenabling interrupts and
  4989. * re-starting the system.
  4990. *
  4991. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  4992. */
  4993. void set_curr_task(int cpu, task_t *p)
  4994. {
  4995. cpu_curr(cpu) = p;
  4996. }
  4997. #endif