1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331 |
- #include <linux/slab.h>
- #include <linux/poll.h>
- #include <linux/fs.h>
- #include <linux/file.h>
- #include <linux/jhash.h>
- #include <linux/init.h>
- #include <linux/futex.h>
- #include <linux/mount.h>
- #include <linux/pagemap.h>
- #include <linux/syscalls.h>
- #include <linux/signal.h>
- #include <linux/export.h>
- #include <linux/magic.h>
- #include <linux/pid.h>
- #include <linux/nsproxy.h>
- #include <linux/ptrace.h>
- #include <linux/sched/rt.h>
- #include <linux/hugetlb.h>
- #include <linux/freezer.h>
- #include <linux/bootmem.h>
- #include <linux/fault-inject.h>
- #include <asm/futex.h>
- #include "locking/rtmutex_common.h"
- #ifndef CONFIG_HAVE_FUTEX_CMPXCHG
- int __read_mostly futex_cmpxchg_enabled;
- #endif
- #ifdef CONFIG_MMU
- # define FLAGS_SHARED 0x01
- #else
- # define FLAGS_SHARED 0x00
- #endif
- #define FLAGS_CLOCKRT 0x02
- #define FLAGS_HAS_TIMEOUT 0x04
- struct futex_pi_state {
-
- struct list_head list;
-
- struct rt_mutex pi_mutex;
- struct task_struct *owner;
- atomic_t refcount;
- union futex_key key;
- };
- struct futex_q {
- struct plist_node list;
- struct task_struct *task;
- spinlock_t *lock_ptr;
- union futex_key key;
- struct futex_pi_state *pi_state;
- struct rt_mutex_waiter *rt_waiter;
- union futex_key *requeue_pi_key;
- u32 bitset;
- };
- static const struct futex_q futex_q_init = {
-
- .key = FUTEX_KEY_INIT,
- .bitset = FUTEX_BITSET_MATCH_ANY
- };
- struct futex_hash_bucket {
- atomic_t waiters;
- spinlock_t lock;
- struct plist_head chain;
- } ____cacheline_aligned_in_smp;
- static struct {
- struct futex_hash_bucket *queues;
- unsigned long hashsize;
- } __futex_data __read_mostly __aligned(2*sizeof(long));
- #define futex_queues (__futex_data.queues)
- #define futex_hashsize (__futex_data.hashsize)
- #ifdef CONFIG_FAIL_FUTEX
- static struct {
- struct fault_attr attr;
- bool ignore_private;
- } fail_futex = {
- .attr = FAULT_ATTR_INITIALIZER,
- .ignore_private = false,
- };
- static int __init setup_fail_futex(char *str)
- {
- return setup_fault_attr(&fail_futex.attr, str);
- }
- __setup("fail_futex=", setup_fail_futex);
- static bool should_fail_futex(bool fshared)
- {
- if (fail_futex.ignore_private && !fshared)
- return false;
- return should_fail(&fail_futex.attr, 1);
- }
- #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
- static int __init fail_futex_debugfs(void)
- {
- umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
- struct dentry *dir;
- dir = fault_create_debugfs_attr("fail_futex", NULL,
- &fail_futex.attr);
- if (IS_ERR(dir))
- return PTR_ERR(dir);
- if (!debugfs_create_bool("ignore-private", mode, dir,
- &fail_futex.ignore_private)) {
- debugfs_remove_recursive(dir);
- return -ENOMEM;
- }
- return 0;
- }
- late_initcall(fail_futex_debugfs);
- #endif
- #else
- static inline bool should_fail_futex(bool fshared)
- {
- return false;
- }
- #endif
- static inline void futex_get_mm(union futex_key *key)
- {
- atomic_inc(&key->private.mm->mm_count);
-
- smp_mb__after_atomic();
- }
- static inline void hb_waiters_inc(struct futex_hash_bucket *hb)
- {
- #ifdef CONFIG_SMP
- atomic_inc(&hb->waiters);
-
- smp_mb__after_atomic();
- #endif
- }
- static inline void hb_waiters_dec(struct futex_hash_bucket *hb)
- {
- #ifdef CONFIG_SMP
- atomic_dec(&hb->waiters);
- #endif
- }
- static inline int hb_waiters_pending(struct futex_hash_bucket *hb)
- {
- #ifdef CONFIG_SMP
- return atomic_read(&hb->waiters);
- #else
- return 1;
- #endif
- }
- static struct futex_hash_bucket *hash_futex(union futex_key *key)
- {
- u32 hash = jhash2((u32*)&key->both.word,
- (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
- key->both.offset);
- return &futex_queues[hash & (futex_hashsize - 1)];
- }
- static inline int match_futex(union futex_key *key1, union futex_key *key2)
- {
- return (key1 && key2
- && key1->both.word == key2->both.word
- && key1->both.ptr == key2->both.ptr
- && key1->both.offset == key2->both.offset);
- }
- static void get_futex_key_refs(union futex_key *key)
- {
- if (!key->both.ptr)
- return;
-
- if (!IS_ENABLED(CONFIG_MMU)) {
- smp_mb();
- return;
- }
- switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
- case FUT_OFF_INODE:
- ihold(key->shared.inode);
- break;
- case FUT_OFF_MMSHARED:
- futex_get_mm(key);
- break;
- default:
-
- smp_mb();
- }
- }
- static void drop_futex_key_refs(union futex_key *key)
- {
- if (!key->both.ptr) {
-
- WARN_ON_ONCE(1);
- return;
- }
- if (!IS_ENABLED(CONFIG_MMU))
- return;
- switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
- case FUT_OFF_INODE:
- iput(key->shared.inode);
- break;
- case FUT_OFF_MMSHARED:
- mmdrop(key->private.mm);
- break;
- }
- }
- static int
- get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
- {
- unsigned long address = (unsigned long)uaddr;
- struct mm_struct *mm = current->mm;
- struct page *page, *tail;
- struct address_space *mapping;
- int err, ro = 0;
-
- key->both.offset = address % PAGE_SIZE;
- if (unlikely((address % sizeof(u32)) != 0))
- return -EINVAL;
- address -= key->both.offset;
- if (unlikely(!access_ok(rw, uaddr, sizeof(u32))))
- return -EFAULT;
- if (unlikely(should_fail_futex(fshared)))
- return -EFAULT;
-
- if (!fshared) {
- key->private.mm = mm;
- key->private.address = address;
- get_futex_key_refs(key);
- return 0;
- }
- again:
-
- if (unlikely(should_fail_futex(fshared)))
- return -EFAULT;
- err = get_user_pages_fast(address, 1, 1, &page);
-
- if (err == -EFAULT && rw == VERIFY_READ) {
- err = get_user_pages_fast(address, 1, 0, &page);
- ro = 1;
- }
- if (err < 0)
- return err;
- else
- err = 0;
-
- tail = page;
- page = compound_head(page);
- mapping = READ_ONCE(page->mapping);
-
- if (unlikely(!mapping)) {
- int shmem_swizzled;
-
- lock_page(page);
- shmem_swizzled = PageSwapCache(page) || page->mapping;
- unlock_page(page);
- put_page(page);
- if (shmem_swizzled)
- goto again;
- return -EFAULT;
- }
-
- if (PageAnon(page)) {
-
- if (unlikely(should_fail_futex(fshared)) || ro) {
- err = -EFAULT;
- goto out;
- }
- key->both.offset |= FUT_OFF_MMSHARED;
- key->private.mm = mm;
- key->private.address = address;
- get_futex_key_refs(key);
- } else {
- struct inode *inode;
-
- rcu_read_lock();
- if (READ_ONCE(page->mapping) != mapping) {
- rcu_read_unlock();
- put_page(page);
- goto again;
- }
- inode = READ_ONCE(mapping->host);
- if (!inode) {
- rcu_read_unlock();
- put_page(page);
- goto again;
- }
-
- if (!atomic_inc_not_zero(&inode->i_count)) {
- rcu_read_unlock();
- put_page(page);
- goto again;
- }
-
- if (WARN_ON_ONCE(inode->i_mapping != mapping)) {
- err = -EFAULT;
- rcu_read_unlock();
- iput(inode);
- goto out;
- }
- key->both.offset |= FUT_OFF_INODE;
- key->shared.inode = inode;
- key->shared.pgoff = basepage_index(tail);
- rcu_read_unlock();
- }
- out:
- put_page(page);
- return err;
- }
- static inline void put_futex_key(union futex_key *key)
- {
- drop_futex_key_refs(key);
- }
- static int fault_in_user_writeable(u32 __user *uaddr)
- {
- struct mm_struct *mm = current->mm;
- int ret;
- down_read(&mm->mmap_sem);
- ret = fixup_user_fault(current, mm, (unsigned long)uaddr,
- FAULT_FLAG_WRITE, NULL);
- up_read(&mm->mmap_sem);
- return ret < 0 ? ret : 0;
- }
- static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
- union futex_key *key)
- {
- struct futex_q *this;
- plist_for_each_entry(this, &hb->chain, list) {
- if (match_futex(&this->key, key))
- return this;
- }
- return NULL;
- }
- static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
- u32 uval, u32 newval)
- {
- int ret;
- pagefault_disable();
- ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
- pagefault_enable();
- return ret;
- }
- static int get_futex_value_locked(u32 *dest, u32 __user *from)
- {
- int ret;
- pagefault_disable();
- ret = __get_user(*dest, from);
- pagefault_enable();
- return ret ? -EFAULT : 0;
- }
- static int refill_pi_state_cache(void)
- {
- struct futex_pi_state *pi_state;
- if (likely(current->pi_state_cache))
- return 0;
- pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
- if (!pi_state)
- return -ENOMEM;
- INIT_LIST_HEAD(&pi_state->list);
-
- pi_state->owner = NULL;
- atomic_set(&pi_state->refcount, 1);
- pi_state->key = FUTEX_KEY_INIT;
- current->pi_state_cache = pi_state;
- return 0;
- }
- static struct futex_pi_state * alloc_pi_state(void)
- {
- struct futex_pi_state *pi_state = current->pi_state_cache;
- WARN_ON(!pi_state);
- current->pi_state_cache = NULL;
- return pi_state;
- }
- static void put_pi_state(struct futex_pi_state *pi_state)
- {
- if (!pi_state)
- return;
- if (!atomic_dec_and_test(&pi_state->refcount))
- return;
-
- if (pi_state->owner) {
- raw_spin_lock_irq(&pi_state->owner->pi_lock);
- list_del_init(&pi_state->list);
- raw_spin_unlock_irq(&pi_state->owner->pi_lock);
- rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
- }
- if (current->pi_state_cache)
- kfree(pi_state);
- else {
-
- pi_state->owner = NULL;
- atomic_set(&pi_state->refcount, 1);
- current->pi_state_cache = pi_state;
- }
- }
- static struct task_struct * futex_find_get_task(pid_t pid)
- {
- struct task_struct *p;
- rcu_read_lock();
- p = find_task_by_vpid(pid);
- if (p)
- get_task_struct(p);
- rcu_read_unlock();
- return p;
- }
- void exit_pi_state_list(struct task_struct *curr)
- {
- struct list_head *next, *head = &curr->pi_state_list;
- struct futex_pi_state *pi_state;
- struct futex_hash_bucket *hb;
- union futex_key key = FUTEX_KEY_INIT;
- if (!futex_cmpxchg_enabled)
- return;
-
- raw_spin_lock_irq(&curr->pi_lock);
- while (!list_empty(head)) {
- next = head->next;
- pi_state = list_entry(next, struct futex_pi_state, list);
- key = pi_state->key;
- hb = hash_futex(&key);
- raw_spin_unlock_irq(&curr->pi_lock);
- spin_lock(&hb->lock);
- raw_spin_lock_irq(&curr->pi_lock);
-
- if (head->next != next) {
- spin_unlock(&hb->lock);
- continue;
- }
- WARN_ON(pi_state->owner != curr);
- WARN_ON(list_empty(&pi_state->list));
- list_del_init(&pi_state->list);
- pi_state->owner = NULL;
- raw_spin_unlock_irq(&curr->pi_lock);
- rt_mutex_unlock(&pi_state->pi_mutex);
- spin_unlock(&hb->lock);
- raw_spin_lock_irq(&curr->pi_lock);
- }
- raw_spin_unlock_irq(&curr->pi_lock);
- }
- static int attach_to_pi_state(u32 uval, struct futex_pi_state *pi_state,
- struct futex_pi_state **ps)
- {
- pid_t pid = uval & FUTEX_TID_MASK;
-
- if (unlikely(!pi_state))
- return -EINVAL;
- WARN_ON(!atomic_read(&pi_state->refcount));
-
- if (uval & FUTEX_OWNER_DIED) {
-
- if (!pi_state->owner) {
-
- if (pid)
- return -EINVAL;
-
- goto out_state;
- }
-
- if (!pid)
- goto out_state;
- } else {
-
- if (!pi_state->owner)
- return -EINVAL;
- }
-
- if (pid != task_pid_vnr(pi_state->owner))
- return -EINVAL;
- out_state:
- atomic_inc(&pi_state->refcount);
- *ps = pi_state;
- return 0;
- }
- static int attach_to_pi_owner(u32 uval, union futex_key *key,
- struct futex_pi_state **ps)
- {
- pid_t pid = uval & FUTEX_TID_MASK;
- struct futex_pi_state *pi_state;
- struct task_struct *p;
-
- if (!pid)
- return -ESRCH;
- p = futex_find_get_task(pid);
- if (!p)
- return -ESRCH;
- if (unlikely(p->flags & PF_KTHREAD)) {
- put_task_struct(p);
- return -EPERM;
- }
-
- raw_spin_lock_irq(&p->pi_lock);
- if (unlikely(p->flags & PF_EXITING)) {
-
- int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
- raw_spin_unlock_irq(&p->pi_lock);
- put_task_struct(p);
- return ret;
- }
-
- pi_state = alloc_pi_state();
-
- rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
-
- pi_state->key = *key;
- WARN_ON(!list_empty(&pi_state->list));
- list_add(&pi_state->list, &p->pi_state_list);
- pi_state->owner = p;
- raw_spin_unlock_irq(&p->pi_lock);
- put_task_struct(p);
- *ps = pi_state;
- return 0;
- }
- static int lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
- union futex_key *key, struct futex_pi_state **ps)
- {
- struct futex_q *match = futex_top_waiter(hb, key);
-
- if (match)
- return attach_to_pi_state(uval, match->pi_state, ps);
-
- return attach_to_pi_owner(uval, key, ps);
- }
- static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
- {
- u32 uninitialized_var(curval);
- if (unlikely(should_fail_futex(true)))
- return -EFAULT;
- if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
- return -EFAULT;
-
- return curval != uval ? -EAGAIN : 0;
- }
- static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
- union futex_key *key,
- struct futex_pi_state **ps,
- struct task_struct *task, int set_waiters)
- {
- u32 uval, newval, vpid = task_pid_vnr(task);
- struct futex_q *match;
- int ret;
-
- if (get_futex_value_locked(&uval, uaddr))
- return -EFAULT;
- if (unlikely(should_fail_futex(true)))
- return -EFAULT;
-
- if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
- return -EDEADLK;
- if ((unlikely(should_fail_futex(true))))
- return -EDEADLK;
-
- match = futex_top_waiter(hb, key);
- if (match)
- return attach_to_pi_state(uval, match->pi_state, ps);
-
- if (!(uval & FUTEX_TID_MASK)) {
-
- newval = uval & FUTEX_OWNER_DIED;
- newval |= vpid;
-
- if (set_waiters)
- newval |= FUTEX_WAITERS;
- ret = lock_pi_update_atomic(uaddr, uval, newval);
-
- return ret < 0 ? ret : 1;
- }
-
- newval = uval | FUTEX_WAITERS;
- ret = lock_pi_update_atomic(uaddr, uval, newval);
- if (ret)
- return ret;
-
- return attach_to_pi_owner(uval, key, ps);
- }
- static void __unqueue_futex(struct futex_q *q)
- {
- struct futex_hash_bucket *hb;
- if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
- || WARN_ON(plist_node_empty(&q->list)))
- return;
- hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
- plist_del(&q->list, &hb->chain);
- hb_waiters_dec(hb);
- }
- static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
- {
- struct task_struct *p = q->task;
- if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
- return;
-
- wake_q_add(wake_q, p);
- __unqueue_futex(q);
-
- smp_wmb();
- q->lock_ptr = NULL;
- }
- static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
- struct futex_hash_bucket *hb)
- {
- struct task_struct *new_owner;
- struct futex_pi_state *pi_state = this->pi_state;
- u32 uninitialized_var(curval), newval;
- WAKE_Q(wake_q);
- bool deboost;
- int ret = 0;
- if (!pi_state)
- return -EINVAL;
-
- if (pi_state->owner != current)
- return -EINVAL;
- raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
- new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
-
- if (!new_owner)
- new_owner = this->task;
-
- newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
- if (unlikely(should_fail_futex(true)))
- ret = -EFAULT;
- if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) {
- ret = -EFAULT;
- } else if (curval != uval) {
-
- if ((FUTEX_TID_MASK & curval) == uval)
- ret = -EAGAIN;
- else
- ret = -EINVAL;
- }
- if (ret) {
- raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
- return ret;
- }
- raw_spin_lock(&pi_state->owner->pi_lock);
- WARN_ON(list_empty(&pi_state->list));
- list_del_init(&pi_state->list);
- raw_spin_unlock(&pi_state->owner->pi_lock);
- raw_spin_lock(&new_owner->pi_lock);
- WARN_ON(!list_empty(&pi_state->list));
- list_add(&pi_state->list, &new_owner->pi_state_list);
- pi_state->owner = new_owner;
- raw_spin_unlock(&new_owner->pi_lock);
- raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
- deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
-
- spin_unlock(&hb->lock);
- wake_up_q(&wake_q);
- if (deboost)
- rt_mutex_adjust_prio(current);
- return 0;
- }
- static inline void
- double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
- {
- if (hb1 <= hb2) {
- spin_lock(&hb1->lock);
- if (hb1 < hb2)
- spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
- } else {
- spin_lock(&hb2->lock);
- spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
- }
- }
- static inline void
- double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
- {
- spin_unlock(&hb1->lock);
- if (hb1 != hb2)
- spin_unlock(&hb2->lock);
- }
- static int
- futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
- {
- struct futex_hash_bucket *hb;
- struct futex_q *this, *next;
- union futex_key key = FUTEX_KEY_INIT;
- int ret;
- WAKE_Q(wake_q);
- if (!bitset)
- return -EINVAL;
- ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
- if (unlikely(ret != 0))
- goto out;
- hb = hash_futex(&key);
-
- if (!hb_waiters_pending(hb))
- goto out_put_key;
- spin_lock(&hb->lock);
- plist_for_each_entry_safe(this, next, &hb->chain, list) {
- if (match_futex (&this->key, &key)) {
- if (this->pi_state || this->rt_waiter) {
- ret = -EINVAL;
- break;
- }
-
- if (!(this->bitset & bitset))
- continue;
- mark_wake_futex(&wake_q, this);
- if (++ret >= nr_wake)
- break;
- }
- }
- spin_unlock(&hb->lock);
- wake_up_q(&wake_q);
- out_put_key:
- put_futex_key(&key);
- out:
- return ret;
- }
- static int
- futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
- int nr_wake, int nr_wake2, int op)
- {
- union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
- struct futex_hash_bucket *hb1, *hb2;
- struct futex_q *this, *next;
- int ret, op_ret;
- WAKE_Q(wake_q);
- retry:
- ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
- if (unlikely(ret != 0))
- goto out;
- ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
- if (unlikely(ret != 0))
- goto out_put_key1;
- hb1 = hash_futex(&key1);
- hb2 = hash_futex(&key2);
- retry_private:
- double_lock_hb(hb1, hb2);
- op_ret = futex_atomic_op_inuser(op, uaddr2);
- if (unlikely(op_ret < 0)) {
- double_unlock_hb(hb1, hb2);
- #ifndef CONFIG_MMU
-
- ret = op_ret;
- goto out_put_keys;
- #endif
- if (unlikely(op_ret != -EFAULT)) {
- ret = op_ret;
- goto out_put_keys;
- }
- ret = fault_in_user_writeable(uaddr2);
- if (ret)
- goto out_put_keys;
- if (!(flags & FLAGS_SHARED))
- goto retry_private;
- put_futex_key(&key2);
- put_futex_key(&key1);
- goto retry;
- }
- plist_for_each_entry_safe(this, next, &hb1->chain, list) {
- if (match_futex (&this->key, &key1)) {
- if (this->pi_state || this->rt_waiter) {
- ret = -EINVAL;
- goto out_unlock;
- }
- mark_wake_futex(&wake_q, this);
- if (++ret >= nr_wake)
- break;
- }
- }
- if (op_ret > 0) {
- op_ret = 0;
- plist_for_each_entry_safe(this, next, &hb2->chain, list) {
- if (match_futex (&this->key, &key2)) {
- if (this->pi_state || this->rt_waiter) {
- ret = -EINVAL;
- goto out_unlock;
- }
- mark_wake_futex(&wake_q, this);
- if (++op_ret >= nr_wake2)
- break;
- }
- }
- ret += op_ret;
- }
- out_unlock:
- double_unlock_hb(hb1, hb2);
- wake_up_q(&wake_q);
- out_put_keys:
- put_futex_key(&key2);
- out_put_key1:
- put_futex_key(&key1);
- out:
- return ret;
- }
- static inline
- void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
- struct futex_hash_bucket *hb2, union futex_key *key2)
- {
-
- if (likely(&hb1->chain != &hb2->chain)) {
- plist_del(&q->list, &hb1->chain);
- hb_waiters_dec(hb1);
- hb_waiters_inc(hb2);
- plist_add(&q->list, &hb2->chain);
- q->lock_ptr = &hb2->lock;
- }
- get_futex_key_refs(key2);
- q->key = *key2;
- }
- static inline
- void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
- struct futex_hash_bucket *hb)
- {
- get_futex_key_refs(key);
- q->key = *key;
- __unqueue_futex(q);
- WARN_ON(!q->rt_waiter);
- q->rt_waiter = NULL;
- q->lock_ptr = &hb->lock;
- wake_up_state(q->task, TASK_NORMAL);
- }
- static int futex_proxy_trylock_atomic(u32 __user *pifutex,
- struct futex_hash_bucket *hb1,
- struct futex_hash_bucket *hb2,
- union futex_key *key1, union futex_key *key2,
- struct futex_pi_state **ps, int set_waiters)
- {
- struct futex_q *top_waiter = NULL;
- u32 curval;
- int ret, vpid;
- if (get_futex_value_locked(&curval, pifutex))
- return -EFAULT;
- if (unlikely(should_fail_futex(true)))
- return -EFAULT;
-
- top_waiter = futex_top_waiter(hb1, key1);
-
- if (!top_waiter)
- return 0;
-
- if (!match_futex(top_waiter->requeue_pi_key, key2))
- return -EINVAL;
-
- vpid = task_pid_vnr(top_waiter->task);
- ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
- set_waiters);
- if (ret == 1) {
- requeue_pi_wake_futex(top_waiter, key2, hb2);
- return vpid;
- }
- return ret;
- }
- static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
- u32 __user *uaddr2, int nr_wake, int nr_requeue,
- u32 *cmpval, int requeue_pi)
- {
- union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
- int drop_count = 0, task_count = 0, ret;
- struct futex_pi_state *pi_state = NULL;
- struct futex_hash_bucket *hb1, *hb2;
- struct futex_q *this, *next;
- WAKE_Q(wake_q);
- if (requeue_pi) {
-
- if (uaddr1 == uaddr2)
- return -EINVAL;
-
- if (refill_pi_state_cache())
- return -ENOMEM;
-
- if (nr_wake != 1)
- return -EINVAL;
- }
- retry:
- ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
- if (unlikely(ret != 0))
- goto out;
- ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
- requeue_pi ? VERIFY_WRITE : VERIFY_READ);
- if (unlikely(ret != 0))
- goto out_put_key1;
-
- if (requeue_pi && match_futex(&key1, &key2)) {
- ret = -EINVAL;
- goto out_put_keys;
- }
- hb1 = hash_futex(&key1);
- hb2 = hash_futex(&key2);
- retry_private:
- hb_waiters_inc(hb2);
- double_lock_hb(hb1, hb2);
- if (likely(cmpval != NULL)) {
- u32 curval;
- ret = get_futex_value_locked(&curval, uaddr1);
- if (unlikely(ret)) {
- double_unlock_hb(hb1, hb2);
- hb_waiters_dec(hb2);
- ret = get_user(curval, uaddr1);
- if (ret)
- goto out_put_keys;
- if (!(flags & FLAGS_SHARED))
- goto retry_private;
- put_futex_key(&key2);
- put_futex_key(&key1);
- goto retry;
- }
- if (curval != *cmpval) {
- ret = -EAGAIN;
- goto out_unlock;
- }
- }
- if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
-
- ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
- &key2, &pi_state, nr_requeue);
-
- if (ret > 0) {
- WARN_ON(pi_state);
- drop_count++;
- task_count++;
-
- ret = lookup_pi_state(ret, hb2, &key2, &pi_state);
- }
- switch (ret) {
- case 0:
-
- break;
-
- case -EFAULT:
- double_unlock_hb(hb1, hb2);
- hb_waiters_dec(hb2);
- put_futex_key(&key2);
- put_futex_key(&key1);
- ret = fault_in_user_writeable(uaddr2);
- if (!ret)
- goto retry;
- goto out;
- case -EAGAIN:
-
- double_unlock_hb(hb1, hb2);
- hb_waiters_dec(hb2);
- put_futex_key(&key2);
- put_futex_key(&key1);
- cond_resched();
- goto retry;
- default:
- goto out_unlock;
- }
- }
- plist_for_each_entry_safe(this, next, &hb1->chain, list) {
- if (task_count - nr_wake >= nr_requeue)
- break;
- if (!match_futex(&this->key, &key1))
- continue;
-
- if ((requeue_pi && !this->rt_waiter) ||
- (!requeue_pi && this->rt_waiter) ||
- this->pi_state) {
- ret = -EINVAL;
- break;
- }
-
- if (++task_count <= nr_wake && !requeue_pi) {
- mark_wake_futex(&wake_q, this);
- continue;
- }
-
- if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
- ret = -EINVAL;
- break;
- }
-
- if (requeue_pi) {
-
- atomic_inc(&pi_state->refcount);
- this->pi_state = pi_state;
- ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
- this->rt_waiter,
- this->task);
- if (ret == 1) {
-
- requeue_pi_wake_futex(this, &key2, hb2);
- drop_count++;
- continue;
- } else if (ret) {
-
- this->pi_state = NULL;
- put_pi_state(pi_state);
-
- break;
- }
- }
- requeue_futex(this, hb1, hb2, &key2);
- drop_count++;
- }
-
- put_pi_state(pi_state);
- out_unlock:
- double_unlock_hb(hb1, hb2);
- wake_up_q(&wake_q);
- hb_waiters_dec(hb2);
-
- while (--drop_count >= 0)
- drop_futex_key_refs(&key1);
- out_put_keys:
- put_futex_key(&key2);
- out_put_key1:
- put_futex_key(&key1);
- out:
- return ret ? ret : task_count;
- }
- static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
- __acquires(&hb->lock)
- {
- struct futex_hash_bucket *hb;
- hb = hash_futex(&q->key);
-
- hb_waiters_inc(hb);
- q->lock_ptr = &hb->lock;
- spin_lock(&hb->lock);
- return hb;
- }
- static inline void
- queue_unlock(struct futex_hash_bucket *hb)
- __releases(&hb->lock)
- {
- spin_unlock(&hb->lock);
- hb_waiters_dec(hb);
- }
- static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
- __releases(&hb->lock)
- {
- int prio;
-
- prio = min(current->normal_prio, MAX_RT_PRIO);
- plist_node_init(&q->list, prio);
- plist_add(&q->list, &hb->chain);
- q->task = current;
- spin_unlock(&hb->lock);
- }
- static int unqueue_me(struct futex_q *q)
- {
- spinlock_t *lock_ptr;
- int ret = 0;
-
- retry:
-
- lock_ptr = READ_ONCE(q->lock_ptr);
- if (lock_ptr != NULL) {
- spin_lock(lock_ptr);
-
- if (unlikely(lock_ptr != q->lock_ptr)) {
- spin_unlock(lock_ptr);
- goto retry;
- }
- __unqueue_futex(q);
- BUG_ON(q->pi_state);
- spin_unlock(lock_ptr);
- ret = 1;
- }
- drop_futex_key_refs(&q->key);
- return ret;
- }
- static void unqueue_me_pi(struct futex_q *q)
- __releases(q->lock_ptr)
- {
- __unqueue_futex(q);
- BUG_ON(!q->pi_state);
- put_pi_state(q->pi_state);
- q->pi_state = NULL;
- spin_unlock(q->lock_ptr);
- }
- static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
- struct task_struct *newowner)
- {
- u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
- struct futex_pi_state *pi_state = q->pi_state;
- struct task_struct *oldowner = pi_state->owner;
- u32 uval, uninitialized_var(curval), newval;
- int ret;
-
- if (!pi_state->owner)
- newtid |= FUTEX_OWNER_DIED;
-
- retry:
- if (get_futex_value_locked(&uval, uaddr))
- goto handle_fault;
- while (1) {
- newval = (uval & FUTEX_OWNER_DIED) | newtid;
- if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
- goto handle_fault;
- if (curval == uval)
- break;
- uval = curval;
- }
-
- if (pi_state->owner != NULL) {
- raw_spin_lock_irq(&pi_state->owner->pi_lock);
- WARN_ON(list_empty(&pi_state->list));
- list_del_init(&pi_state->list);
- raw_spin_unlock_irq(&pi_state->owner->pi_lock);
- }
- pi_state->owner = newowner;
- raw_spin_lock_irq(&newowner->pi_lock);
- WARN_ON(!list_empty(&pi_state->list));
- list_add(&pi_state->list, &newowner->pi_state_list);
- raw_spin_unlock_irq(&newowner->pi_lock);
- return 0;
-
- handle_fault:
- spin_unlock(q->lock_ptr);
- ret = fault_in_user_writeable(uaddr);
- spin_lock(q->lock_ptr);
-
- if (pi_state->owner != oldowner)
- return 0;
- if (ret)
- return ret;
- goto retry;
- }
- static long futex_wait_restart(struct restart_block *restart);
- static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
- {
- struct task_struct *owner;
- int ret = 0;
- if (locked) {
-
- if (q->pi_state->owner != current)
- ret = fixup_pi_state_owner(uaddr, q, current);
- goto out;
- }
-
- if (q->pi_state->owner == current) {
-
- if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
- locked = 1;
- goto out;
- }
-
- raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
- owner = rt_mutex_owner(&q->pi_state->pi_mutex);
- if (!owner)
- owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
- raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
- ret = fixup_pi_state_owner(uaddr, q, owner);
- goto out;
- }
-
- if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
- printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
- "pi-state %p\n", ret,
- q->pi_state->pi_mutex.owner,
- q->pi_state->owner);
- out:
- return ret ? ret : locked;
- }
- static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
- struct hrtimer_sleeper *timeout)
- {
-
- set_current_state(TASK_INTERRUPTIBLE);
- queue_me(q, hb);
-
- if (timeout)
- hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
-
- if (likely(!plist_node_empty(&q->list))) {
-
- if (!timeout || timeout->task)
- freezable_schedule();
- }
- __set_current_state(TASK_RUNNING);
- }
- static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
- struct futex_q *q, struct futex_hash_bucket **hb)
- {
- u32 uval;
- int ret;
-
- retry:
- ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
- if (unlikely(ret != 0))
- return ret;
- retry_private:
- *hb = queue_lock(q);
- ret = get_futex_value_locked(&uval, uaddr);
- if (ret) {
- queue_unlock(*hb);
- ret = get_user(uval, uaddr);
- if (ret)
- goto out;
- if (!(flags & FLAGS_SHARED))
- goto retry_private;
- put_futex_key(&q->key);
- goto retry;
- }
- if (uval != val) {
- queue_unlock(*hb);
- ret = -EWOULDBLOCK;
- }
- out:
- if (ret)
- put_futex_key(&q->key);
- return ret;
- }
- static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
- ktime_t *abs_time, u32 bitset)
- {
- struct hrtimer_sleeper timeout, *to = NULL;
- struct restart_block *restart;
- struct futex_hash_bucket *hb;
- struct futex_q q = futex_q_init;
- int ret;
- if (!bitset)
- return -EINVAL;
- q.bitset = bitset;
- if (abs_time) {
- to = &timeout;
- hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
- CLOCK_REALTIME : CLOCK_MONOTONIC,
- HRTIMER_MODE_ABS);
- hrtimer_init_sleeper(to, current);
- hrtimer_set_expires_range_ns(&to->timer, *abs_time,
- current->timer_slack_ns);
- }
- retry:
-
- ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
- if (ret)
- goto out;
-
- futex_wait_queue_me(hb, &q, to);
-
- ret = 0;
-
- if (!unqueue_me(&q))
- goto out;
- ret = -ETIMEDOUT;
- if (to && !to->task)
- goto out;
-
- if (!signal_pending(current))
- goto retry;
- ret = -ERESTARTSYS;
- if (!abs_time)
- goto out;
- restart = ¤t->restart_block;
- restart->fn = futex_wait_restart;
- restart->futex.uaddr = uaddr;
- restart->futex.val = val;
- restart->futex.time = abs_time->tv64;
- restart->futex.bitset = bitset;
- restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
- ret = -ERESTART_RESTARTBLOCK;
- out:
- if (to) {
- hrtimer_cancel(&to->timer);
- destroy_hrtimer_on_stack(&to->timer);
- }
- return ret;
- }
- static long futex_wait_restart(struct restart_block *restart)
- {
- u32 __user *uaddr = restart->futex.uaddr;
- ktime_t t, *tp = NULL;
- if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
- t.tv64 = restart->futex.time;
- tp = &t;
- }
- restart->fn = do_no_restart_syscall;
- return (long)futex_wait(uaddr, restart->futex.flags,
- restart->futex.val, tp, restart->futex.bitset);
- }
- static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
- ktime_t *time, int trylock)
- {
- struct hrtimer_sleeper timeout, *to = NULL;
- struct futex_hash_bucket *hb;
- struct futex_q q = futex_q_init;
- int res, ret;
- if (refill_pi_state_cache())
- return -ENOMEM;
- if (time) {
- to = &timeout;
- hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
- HRTIMER_MODE_ABS);
- hrtimer_init_sleeper(to, current);
- hrtimer_set_expires(&to->timer, *time);
- }
- retry:
- ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
- if (unlikely(ret != 0))
- goto out;
- retry_private:
- hb = queue_lock(&q);
- ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
- if (unlikely(ret)) {
-
- switch (ret) {
- case 1:
-
- ret = 0;
- goto out_unlock_put_key;
- case -EFAULT:
- goto uaddr_faulted;
- case -EAGAIN:
-
- queue_unlock(hb);
- put_futex_key(&q.key);
- cond_resched();
- goto retry;
- default:
- goto out_unlock_put_key;
- }
- }
-
- queue_me(&q, hb);
- WARN_ON(!q.pi_state);
-
- if (!trylock) {
- ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
- } else {
- ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
-
- ret = ret ? 0 : -EWOULDBLOCK;
- }
- spin_lock(q.lock_ptr);
-
- res = fixup_owner(uaddr, &q, !ret);
-
- if (res)
- ret = (res < 0) ? res : 0;
-
- if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
- rt_mutex_unlock(&q.pi_state->pi_mutex);
-
- unqueue_me_pi(&q);
- goto out_put_key;
- out_unlock_put_key:
- queue_unlock(hb);
- out_put_key:
- put_futex_key(&q.key);
- out:
- if (to)
- destroy_hrtimer_on_stack(&to->timer);
- return ret != -EINTR ? ret : -ERESTARTNOINTR;
- uaddr_faulted:
- queue_unlock(hb);
- ret = fault_in_user_writeable(uaddr);
- if (ret)
- goto out_put_key;
- if (!(flags & FLAGS_SHARED))
- goto retry_private;
- put_futex_key(&q.key);
- goto retry;
- }
- static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
- {
- u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
- union futex_key key = FUTEX_KEY_INIT;
- struct futex_hash_bucket *hb;
- struct futex_q *match;
- int ret;
- retry:
- if (get_user(uval, uaddr))
- return -EFAULT;
-
- if ((uval & FUTEX_TID_MASK) != vpid)
- return -EPERM;
- ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
- if (ret)
- return ret;
- hb = hash_futex(&key);
- spin_lock(&hb->lock);
-
- match = futex_top_waiter(hb, &key);
- if (match) {
- ret = wake_futex_pi(uaddr, uval, match, hb);
-
- if (!ret)
- goto out_putkey;
-
- if (ret == -EFAULT)
- goto pi_faulted;
-
- if (ret == -EAGAIN) {
- spin_unlock(&hb->lock);
- put_futex_key(&key);
- goto retry;
- }
-
- goto out_unlock;
- }
-
- if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))
- goto pi_faulted;
-
- ret = (curval == uval) ? 0 : -EAGAIN;
- out_unlock:
- spin_unlock(&hb->lock);
- out_putkey:
- put_futex_key(&key);
- return ret;
- pi_faulted:
- spin_unlock(&hb->lock);
- put_futex_key(&key);
- ret = fault_in_user_writeable(uaddr);
- if (!ret)
- goto retry;
- return ret;
- }
- static inline
- int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
- struct futex_q *q, union futex_key *key2,
- struct hrtimer_sleeper *timeout)
- {
- int ret = 0;
-
- if (!match_futex(&q->key, key2)) {
- WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
-
- plist_del(&q->list, &hb->chain);
- hb_waiters_dec(hb);
-
- ret = -EWOULDBLOCK;
- if (timeout && !timeout->task)
- ret = -ETIMEDOUT;
- else if (signal_pending(current))
- ret = -ERESTARTNOINTR;
- }
- return ret;
- }
- static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
- u32 val, ktime_t *abs_time, u32 bitset,
- u32 __user *uaddr2)
- {
- struct hrtimer_sleeper timeout, *to = NULL;
- struct rt_mutex_waiter rt_waiter;
- struct futex_hash_bucket *hb;
- union futex_key key2 = FUTEX_KEY_INIT;
- struct futex_q q = futex_q_init;
- int res, ret;
- if (uaddr == uaddr2)
- return -EINVAL;
- if (!bitset)
- return -EINVAL;
- if (abs_time) {
- to = &timeout;
- hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
- CLOCK_REALTIME : CLOCK_MONOTONIC,
- HRTIMER_MODE_ABS);
- hrtimer_init_sleeper(to, current);
- hrtimer_set_expires_range_ns(&to->timer, *abs_time,
- current->timer_slack_ns);
- }
-
- debug_rt_mutex_init_waiter(&rt_waiter);
- RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
- RB_CLEAR_NODE(&rt_waiter.tree_entry);
- rt_waiter.task = NULL;
- ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
- if (unlikely(ret != 0))
- goto out;
- q.bitset = bitset;
- q.rt_waiter = &rt_waiter;
- q.requeue_pi_key = &key2;
-
- ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
- if (ret)
- goto out_key2;
-
- if (match_futex(&q.key, &key2)) {
- queue_unlock(hb);
- ret = -EINVAL;
- goto out_put_keys;
- }
-
- futex_wait_queue_me(hb, &q, to);
- spin_lock(&hb->lock);
- ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
- spin_unlock(&hb->lock);
- if (ret)
- goto out_put_keys;
-
-
- if (!q.rt_waiter) {
-
- if (q.pi_state && (q.pi_state->owner != current)) {
- spin_lock(q.lock_ptr);
- ret = fixup_pi_state_owner(uaddr2, &q, current);
- if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current)
- rt_mutex_unlock(&q.pi_state->pi_mutex);
-
- put_pi_state(q.pi_state);
- spin_unlock(q.lock_ptr);
- }
- } else {
- struct rt_mutex *pi_mutex;
-
- WARN_ON(!q.pi_state);
- pi_mutex = &q.pi_state->pi_mutex;
- ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
- debug_rt_mutex_free_waiter(&rt_waiter);
- spin_lock(q.lock_ptr);
-
- res = fixup_owner(uaddr2, &q, !ret);
-
- if (res)
- ret = (res < 0) ? res : 0;
-
- if (ret && rt_mutex_owner(pi_mutex) == current)
- rt_mutex_unlock(pi_mutex);
-
- unqueue_me_pi(&q);
- }
- if (ret == -EINTR) {
-
- ret = -EWOULDBLOCK;
- }
- out_put_keys:
- put_futex_key(&q.key);
- out_key2:
- put_futex_key(&key2);
- out:
- if (to) {
- hrtimer_cancel(&to->timer);
- destroy_hrtimer_on_stack(&to->timer);
- }
- return ret;
- }
- SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
- size_t, len)
- {
- if (!futex_cmpxchg_enabled)
- return -ENOSYS;
-
- if (unlikely(len != sizeof(*head)))
- return -EINVAL;
- current->robust_list = head;
- return 0;
- }
- SYSCALL_DEFINE3(get_robust_list, int, pid,
- struct robust_list_head __user * __user *, head_ptr,
- size_t __user *, len_ptr)
- {
- struct robust_list_head __user *head;
- unsigned long ret;
- struct task_struct *p;
- if (!futex_cmpxchg_enabled)
- return -ENOSYS;
- rcu_read_lock();
- ret = -ESRCH;
- if (!pid)
- p = current;
- else {
- p = find_task_by_vpid(pid);
- if (!p)
- goto err_unlock;
- }
- ret = -EPERM;
- if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
- goto err_unlock;
- head = p->robust_list;
- rcu_read_unlock();
- if (put_user(sizeof(*head), len_ptr))
- return -EFAULT;
- return put_user(head, head_ptr);
- err_unlock:
- rcu_read_unlock();
- return ret;
- }
- int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
- {
- u32 uval, uninitialized_var(nval), mval;
- retry:
- if (get_user(uval, uaddr))
- return -1;
- if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
-
- mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
-
- if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
- if (fault_in_user_writeable(uaddr))
- return -1;
- goto retry;
- }
- if (nval != uval)
- goto retry;
-
- if (!pi && (uval & FUTEX_WAITERS))
- futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
- }
- return 0;
- }
- static inline int fetch_robust_entry(struct robust_list __user **entry,
- struct robust_list __user * __user *head,
- unsigned int *pi)
- {
- unsigned long uentry;
- if (get_user(uentry, (unsigned long __user *)head))
- return -EFAULT;
- *entry = (void __user *)(uentry & ~1UL);
- *pi = uentry & 1;
- return 0;
- }
- void exit_robust_list(struct task_struct *curr)
- {
- struct robust_list_head __user *head = curr->robust_list;
- struct robust_list __user *entry, *next_entry, *pending;
- unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
- unsigned int uninitialized_var(next_pi);
- unsigned long futex_offset;
- int rc;
- if (!futex_cmpxchg_enabled)
- return;
-
- if (fetch_robust_entry(&entry, &head->list.next, &pi))
- return;
-
- if (get_user(futex_offset, &head->futex_offset))
- return;
-
- if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
- return;
- next_entry = NULL;
- while (entry != &head->list) {
-
- rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
-
- if (entry != pending)
- if (handle_futex_death((void __user *)entry + futex_offset,
- curr, pi))
- return;
- if (rc)
- return;
- entry = next_entry;
- pi = next_pi;
-
- if (!--limit)
- break;
- cond_resched();
- }
- if (pending)
- handle_futex_death((void __user *)pending + futex_offset,
- curr, pip);
- }
- long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
- u32 __user *uaddr2, u32 val2, u32 val3)
- {
- int cmd = op & FUTEX_CMD_MASK;
- unsigned int flags = 0;
- if (!(op & FUTEX_PRIVATE_FLAG))
- flags |= FLAGS_SHARED;
- if (op & FUTEX_CLOCK_REALTIME) {
- flags |= FLAGS_CLOCKRT;
- if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && \
- cmd != FUTEX_WAIT_REQUEUE_PI)
- return -ENOSYS;
- }
- switch (cmd) {
- case FUTEX_LOCK_PI:
- case FUTEX_UNLOCK_PI:
- case FUTEX_TRYLOCK_PI:
- case FUTEX_WAIT_REQUEUE_PI:
- case FUTEX_CMP_REQUEUE_PI:
- if (!futex_cmpxchg_enabled)
- return -ENOSYS;
- }
- switch (cmd) {
- case FUTEX_WAIT:
- val3 = FUTEX_BITSET_MATCH_ANY;
- case FUTEX_WAIT_BITSET:
- return futex_wait(uaddr, flags, val, timeout, val3);
- case FUTEX_WAKE:
- val3 = FUTEX_BITSET_MATCH_ANY;
- case FUTEX_WAKE_BITSET:
- return futex_wake(uaddr, flags, val, val3);
- case FUTEX_REQUEUE:
- return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
- case FUTEX_CMP_REQUEUE:
- return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
- case FUTEX_WAKE_OP:
- return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
- case FUTEX_LOCK_PI:
- return futex_lock_pi(uaddr, flags, timeout, 0);
- case FUTEX_UNLOCK_PI:
- return futex_unlock_pi(uaddr, flags);
- case FUTEX_TRYLOCK_PI:
- return futex_lock_pi(uaddr, flags, NULL, 1);
- case FUTEX_WAIT_REQUEUE_PI:
- val3 = FUTEX_BITSET_MATCH_ANY;
- return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
- uaddr2);
- case FUTEX_CMP_REQUEUE_PI:
- return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
- }
- return -ENOSYS;
- }
- SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
- struct timespec __user *, utime, u32 __user *, uaddr2,
- u32, val3)
- {
- struct timespec ts;
- ktime_t t, *tp = NULL;
- u32 val2 = 0;
- int cmd = op & FUTEX_CMD_MASK;
- if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
- cmd == FUTEX_WAIT_BITSET ||
- cmd == FUTEX_WAIT_REQUEUE_PI)) {
- if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
- return -EFAULT;
- if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
- return -EFAULT;
- if (!timespec_valid(&ts))
- return -EINVAL;
- t = timespec_to_ktime(ts);
- if (cmd == FUTEX_WAIT)
- t = ktime_add_safe(ktime_get(), t);
- tp = &t;
- }
-
- if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
- cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
- val2 = (u32) (unsigned long) utime;
- return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
- }
- static void __init futex_detect_cmpxchg(void)
- {
- #ifndef CONFIG_HAVE_FUTEX_CMPXCHG
- u32 curval;
-
- if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
- futex_cmpxchg_enabled = 1;
- #endif
- }
- static int __init futex_init(void)
- {
- unsigned int futex_shift;
- unsigned long i;
- #if CONFIG_BASE_SMALL
- futex_hashsize = 16;
- #else
- futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
- #endif
- futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
- futex_hashsize, 0,
- futex_hashsize < 256 ? HASH_SMALL : 0,
- &futex_shift, NULL,
- futex_hashsize, futex_hashsize);
- futex_hashsize = 1UL << futex_shift;
- futex_detect_cmpxchg();
- for (i = 0; i < futex_hashsize; i++) {
- atomic_set(&futex_queues[i].waiters, 0);
- plist_head_init(&futex_queues[i].chain);
- spin_lock_init(&futex_queues[i].lock);
- }
- return 0;
- }
- core_initcall(futex_init);
|