sched.c 142 KB

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