This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathxact.c
4079 lines (3478 loc) · 108 KB
/
xact.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Distributed Shared Non-Volatile Memory
*
* Copyright (C) 2016-2017 Wuklab, Purdue. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
/*
* KISS and Murphy's law, we keep that in mind.
*/
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/log2.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/sort.h>
#include <linux/sched.h>
#include <linux/random.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/random.h>
#include <linux/bitops.h>
#include <linux/memory.h>
#include <linux/kthread.h>
#include <linux/pagemap.h>
#include <asm/tlbflush.h>
#include <linux/dsnvm-interface.h>
#include "dsnvm.h"
#if 0
#define DSNVM_PROFILE
#endif
#include "dsnvm-profile.h"
/**
* xact_free_log_data
*
* Free data_area buffers that were allocated by IB layer and
* then free this log slot within NVM area.
*/
static void xact_free_log_data(struct dsnvm_log_record *log)
{
int i, nr_areas;
if (unlikely(!log)) {
DSNVM_BUG();
return;
}
nr_areas = log->nr_areas;
/* Free buffers allocated by IB: */
for (i = 0; i < nr_areas; i++) {
void *vaddr = log->data_areas[i].vaddr;
DSNVM_PRINTK("Free buffer of log_id %d xact_id %d, vaddr: %p",
log->log_id, log->xact_id, vaddr);
if (vaddr) {
ibapi_free_recv_buf(vaddr);
log->data_areas[i].vaddr = NULL;
}
}
/* Free log slot */
free_dsnvm_log(log);
}
static DECLARE_BITMAP(xact_ids_free_map, DSNVM_MAX_XACT);
static DEFINE_SPINLOCK(xact_id_lock);
#define xact_id_offset (DSNVM_LOCAL_ID * DSNVM_MAX_XACT)
static void dsnvm_init_xact_ids(void)
{
bitmap_clear(xact_ids_free_map, 0, DSNVM_MAX_XACT);
}
static int get_next_xact_id(void)
{
int local_id;
spin_lock(&xact_id_lock);
local_id = find_first_zero_bit(xact_ids_free_map, DSNVM_MAX_XACT);
if (unlikely(local_id == DSNVM_MAX_XACT)) {
DSNVM_BUG("Running out of xact id");
spin_unlock(&xact_id_lock);
return -1;
}
set_bit(local_id, xact_ids_free_map);
spin_unlock(&xact_id_lock);
DSNVM_PRINTK("Alloc xact id of bitmap: %d xact id %d\n",
local_id, local_id + xact_id_offset);
return local_id + xact_id_offset;
}
static void free_xact_id(int id)
{
int local_id = id - xact_id_offset;
DSNVM_PRINTK("Freeing xact_id: %d, local_id: %d", id, local_id);
if (unlikely(local_id < 0 || local_id >= DSNVM_MAX_XACT)) {
DSNVM_WARN();
return;
}
spin_lock(&xact_id_lock);
clear_bit(local_id, xact_ids_free_map);
spin_unlock(&xact_id_lock);
}
/*
* This function describes:
* DN's handler for coherence updates from ON
*
* Related IB APIs:
* ibapi_multi_atomic_send_yy
* ibapi_multi_atomic_send
*
* ON pushes coherent updates to DN in phase 2 of commit protocol.
* DN that receives this update will update if its local DN_REGION pages if any,
* or update its local REPLICA_REGION pages if any. If none of DN_REGION or
* REPLICA_REGION page exist, just ignore this page and report this to ON.
*
* Note that: No page or region will be created in this handler.
*
* TODO:
* What if both REPLICA_REGION and DN_REGION have page, update both of them?
*/
int dsnvm_handle_receive_coherence(int node_id, int nr_reqs,
struct atomic_struct *reqs,
char *output_buf, unsigned int *output_size)
{
int i, nr_areas;
struct dsnvm_commit_request_header *request_header;
struct dr_no_dro_page_offset *meta_for_areas;
unsigned long *reply_bitmap;
count_dsnvm_event(DSNVM_COHERENCE_RX);
/* Get request metadata */
meta_for_areas = reqs[1].vaddr;
request_header = reqs[0].vaddr;
nr_areas = request_header->nr_reqs;
/* Indicating which area is succefully updated */
reply_bitmap = (unsigned long *)output_buf;
*output_size = BITS_TO_LONGS(nr_areas) * sizeof(unsigned long);
bitmap_clear(reply_bitmap, 0, nr_areas);
/* We have 2 metadata requests */
if (unlikely(nr_areas != nr_reqs - 2)) {
DSNVM_BUG("nr_areas %d nr_reqs %d", nr_areas, nr_reqs);
return 0;
}
DSNVM_PRINTK("Sender-ID: %d nr_areas %d", node_id, nr_areas);
for (i = 0; i < nr_areas; i++) {
unsigned long dr_no = meta_for_areas[i].dr_no;
unsigned int dro = meta_for_areas[i].dro;
unsigned long pgoft = meta_for_areas[i].page_offset;
unsigned long pfn = 0;
size_t len;
struct dn_region_info *dr;
struct dsnvm_page *page;
void *dst, *src;
bool coherent_page = false;
/*
* Find DN region from hashtable
*
* Note that: we can find this DN_REGION if and only if the
* application is still using dsnvm_client_file. All DN_REGIONs
* will be removed from the hashtable once the app close dsnvm.
*
* Hence it should be normal and okay that dr == NULL happens
* a lot. Since, for another reason, DN will NOT tell remote ON
* that local DNs are no longer valid, however remote ON will
* search its dn_list for coherent DNs (check make_coherence_and_replication())
*/
dr = ht_get_dn_region(dr_no);
if (dr) {
spin_lock(&dr->page_lock[dro]);
pfn = dr->coherent_mapping[dro];
if (pfn_is_dsnvm(pfn))
coherent_page = true;
else
/* A partial DN region with this page missing */
coherent_page = false;
spin_unlock(&dr->page_lock[dro]);
}
/* Either no DN or partial DN region, then */
/* Try to find it out from REPLICA_REGION hashtable: */
if (!coherent_page) {
struct replica_region_info *rr;
rr = ht_get_replica_region(dr_no);
if (!rr) {
/* No RR, then skip this page */
DSNVM_PRINTK("No action (1) for dr_no: %lu, dro: %u",
dr_no, dro);
continue;
}
spin_lock(&rr->page_lock[dro]);
pfn = rr->mapping[dro];
if (!pfn_is_dsnvm(pfn)) {
/*
* If we reach here, it means:
* a. No DN or partial DN region with this page missing
* b. partial RN region with this page missing
*
* Thus skip this page
*/
spin_unlock(&rr->page_lock[dro]);
DSNVM_PRINTK("No action (2) for dr_no: %lu, dro: %u",
dr_no, dro);
continue;
}
spin_unlock(&rr->page_lock[dro]);
}
/* Either a coherent DN page or replica page */
page = pfn_to_dsnvm_page(pfn);
/*
* Only update if the page is
* in committed state, AND
* not in a xact
*/
lock_dsnvm_page(page);
if (likely(!DSNVM_PageInxact(page) && DSNVM_PageCommitted(page))) {
/* The source address (buffer allocated by IB): */
src = reqs[i + 2].vaddr;
len = reqs[i + 2].len;
/* Well, for safety.. */
WARN_ON((pgoft + len) > PAGE_SIZE);
/* The destination address (within DN or RN): */
dst = (void *)(pfn_to_dsnvm_virt(pfn) + pgoft);
/* Do the real update: */
memcpy(dst, src, len);
dsnvm_flush_buffer(dst, len);
set_bit(i, reply_bitmap);
count_dsnvm_event(DSNVM_COHERENCE_NR_UPDATED_PAGES);
DSNVM_PRINTK("Actual update to dr_no: %lu dro: %u, "
"cp data: %p -> %p, len: %zu", dr_no, dro, src, dst, len);
} else {
DSNVM_PRINTK("Trying to commit to a page currently "
"in another xact dr_no %lu dro %u", dr_no, dro);
unlock_dsnvm_page(page);
break;
}
unlock_dsnvm_page(page);
}
for (i = 0; i < nr_reqs; i++) {
ibapi_free_recv_buf(reqs[i].vaddr);
}
return 0;
}
/* util struct used only in this function */
struct dsnvm_page_char {
char data[DSNVM_PAGE_SIZE];
};
/*
* This function describes:
* Make more replicas to meet rep_degree
*
* Related IB API:
* ibapi_multi_atomic_send_yy
* ibapi_multi_atomic_send
*
* RETURN:
* 0 on success
* nagative value on failure
*/
static int make_replication(int commit_node_id, int nr_areas,
struct dr_no_dro_page_offset *meta_for_areas, struct atomic_struct *reqs,
int *nr_reps_per_area, int if_do_send_yy, struct atomic_struct **xact_reqs,
struct dr_no_dro_page_offset **meta_for_replica_areas, int **coherence_succeed_node,
struct max_reply_msg *reply_msg)
{
int i, j, ret = 0;
int nr_redundant_pages, nr_redundant_dns;
struct dr_no_dro *redundant_page_info;
struct dsnvm_page_char *redundant_page_data;
int *redundant_page_area_index;
int pos[DSNVM_MAX_NODE];
int send_dn_list[DSNVM_MAX_NODE];
int nr_areas_per_dn[DSNVM_MAX_NODE];
struct dsnvm_commit_request_header req_header[DSNVM_MAX_NODE];
redundant_page_info = kmalloc(sizeof(*redundant_page_info) * nr_areas, GFP_KERNEL);
if (!redundant_page_info)
return -ENOMEM;
redundant_page_area_index = kmalloc(sizeof(int) * nr_areas, GFP_KERNEL);
if (!redundant_page_area_index) {
kfree(redundant_page_info);
return -ENOMEM;
}
redundant_page_data = kmalloc(sizeof(*redundant_page_data) * nr_areas, GFP_KERNEL);
if (!redundant_page_data) {
kfree(redundant_page_area_index);
kfree(redundant_page_info);
return -ENOMEM;
}
/*
* Reset Arrays. Note that some arrays are already allocated by
* the make_coherence_and_replication():
*/
for (i = 0; i < DSNVM_MAX_NODE; i++) {
nr_areas_per_dn[i] = 0;
pos[i] = -1;
/* Will be updated later */
req_header[i].nr_reqs = 0;
req_header[i].op = DSNVM_OP_SEND_REPLICA_XACT;
/* The first metadata request */
xact_reqs[i][0].vaddr = &req_header[i];
xact_reqs[i][0].len = sizeof(struct dsnvm_commit_repdegree_request_header);
/* The second metadata request */
xact_reqs[i][1].vaddr = meta_for_replica_areas[i];
}
/*
* Make a copy of the page that need replication,
* then copy the new data from reqs[] to this copy.
* Send the combined page to redundant DN.
*/
nr_redundant_pages = 0;
for (i = 0; i < nr_areas; i++) {
unsigned long dr_no = meta_for_areas[i].dr_no;
unsigned int dro = meta_for_areas[i].dro;
unsigned long pgoft = meta_for_areas[i].page_offset;
struct on_region_info *on;
struct on_page_info *on_page;
void *src, *dst;
size_t len;
/* Already meet rep_degree */
if (nr_reps_per_area[i] < 1)
continue;
for (j = 0; j < nr_redundant_pages; j ++) {
if (dr_no == redundant_page_info[j].dr_no &&
dro == redundant_page_info[j].dro) {
break;
}
}
/* First time see this page */
if (j == nr_redundant_pages) {
/* reverse index into meta_for_areas array */
redundant_page_area_index[nr_redundant_pages] = i;
redundant_page_info[nr_redundant_pages].dr_no = dr_no;
redundant_page_info[nr_redundant_pages].dro = dro;
nr_redundant_pages++;
/* Self-ON case, use physical address */
if (if_do_send_yy)
continue;
on = ht_get_on_region(dr_no);
if (unlikely(!on)) {
DSNVM_BUG();
ret = -EFAULT;
goto out;
}
/* Make a whole copy of the page */
on_page = &on->mapping[dro];
src = (void *)(pfn_to_dsnvm_virt(on_page->local_pfn));
dst = redundant_page_data[j].data;
memcpy(dst, src, PAGE_SIZE);
}
/* Self-ON case, use physical address */
if (if_do_send_yy)
continue;
/* Update the xact required portion inside this copy: */
len = reqs[i + 2].len;
src = reqs[i + 2].vaddr;
dst = redundant_page_data[j].data + pgoft;
/* FIXME */
#if 0
WARN_ON((len + pgoft) > PAGE_SIZE);
memcpy(dst, src, len);
#endif
}
DSNVM_PRINTK("nr_redundant_pages = %d", nr_redundant_pages);
/*
* Now construct send_dn_list array for redundant_pages
* We iterate all redundant_pages, find suitable DN for each page.
*/
nr_redundant_dns = 0;
for (i = 0; i < nr_redundant_pages; i++) {
unsigned long dr_no = redundant_page_info[i].dr_no;
unsigned int dro = redundant_page_info[i].dro;
int area_index = redundant_page_area_index[i];
int node, curr_pos, curr_req;
int nr_remain_replica;
nr_remain_replica = nr_reps_per_area[area_index];
for (node = 0; node < DSNVM_MAX_NODE; node++) {
if (node == commit_node_id || node == DSNVM_LOCAL_ID)
continue;
/* Off-line */
if (unlikely(!test_bit(node, DSNVM_CLIENT_MACHINES)))
continue;
/*
* This node already made a coherent copy.
*
* Note that it is okay to use just one area to see
* if we need to send redundant page to this node.
* Since if a node reports failure for one area, it
* means this node does not have this page.
*/
if (coherence_succeed_node[node][area_index]) {
DSNVM_PRINTK("Node: %d area_index: %d dr_no: %lu dro: %u",
node, area_index, dr_no, dro);
continue;
}
/* Check if we found enough ON alreay: */
if (nr_remain_replica <= 0)
break;
nr_remain_replica--;
/* First time see this node */
if (pos[node] == -1) {
pos[node] = nr_redundant_dns;
send_dn_list[nr_redundant_dns++] = node;
DSNVM_PRINTK("Add node: %d to replica_dn list", node);
}
curr_pos = pos[node];
curr_req = nr_areas_per_dn[curr_pos];
/* Self-ON case, use physical address */
if (if_do_send_yy) {
struct dn_region_info *dn;
void *kern_paddr;
dn = ht_get_dn_region(dr_no);
if (unlikely(!dn)) {
DSNVM_BUG("dr_no: %lu", dr_no);
ret = -EFAULT;
goto out;
}
kern_paddr = (void *)(dn->coherent_mapping[dro] << PAGE_SHIFT);
xact_reqs[curr_pos][curr_req + 2].vaddr = kern_paddr;
} else {
/* Use virtual address of the copied page */
xact_reqs[curr_pos][curr_req + 2].vaddr = redundant_page_data[i].data;
}
/* Always send the whole page */
xact_reqs[curr_pos][curr_req + 2].len = PAGE_SIZE;
meta_for_replica_areas[curr_pos][curr_req].dr_no = dr_no;
meta_for_replica_areas[curr_pos][curr_req].dro = dro;
meta_for_replica_areas[curr_pos][curr_req].page_offset = 0;
nr_areas_per_dn[curr_pos]++;
if (unlikely(nr_areas_per_dn[curr_pos] >= MAX_ATOMIC_SEND_NUM)) {
DSNVM_BUG("Too many requests in one atomic-send");
ret = -EFAULT;
goto out;
}
}
}
/* Last step, fill the first 2 metadata requests: */
for (i = 0; i < nr_redundant_dns; i++) {
if (unlikely(nr_areas_per_dn[i] == 0)) {
ret = DSNVM_REPLY_CANNOT_MAKE_ENOUGH_REPLICA;
goto out;
}
req_header[i].nr_reqs = nr_areas_per_dn[i];
xact_reqs[i][1].len = nr_areas_per_dn[i] * sizeof(struct dr_no_dro_page_offset);
nr_areas_per_dn[i] += 2;
}
if (likely(nr_redundant_dns > 0)) {
DSNVM_PRINTK("Before sending redundant data to total %d DNs",
nr_redundant_dns);
/*
* ibapi_multi_atomic_send_yy: xact_reqs use Physical Address
* ibapi_multi_atomic_send: xact_reqs use Virtual Kernel Address
*/
if (if_do_send_yy) {
ibapi_multi_atomic_send_yy(nr_redundant_dns, send_dn_list,
xact_reqs, nr_areas_per_dn, reply_msg);
} else {
ibapi_multi_atomic_send(nr_redundant_dns, send_dn_list,
xact_reqs, nr_areas_per_dn, reply_msg);
}
DSNVM_PRINTK("After sending redundant data to total %d DNs",
nr_redundant_dns);
} else {
DSNVM_PRINTK("Can not find any DN to replicate");
}
count_dsnvm_events(DSNVM_REPLICATION_TX, nr_redundant_dns);
ret = 0;
out:
kfree(redundant_page_info);
kfree(redundant_page_data);
kfree(redundant_page_area_index);
return ret;
}
/*
* This function describes:
* Push coherence updates and
* meet replica degree
*
* Related IB API:
* ibapi_multi_atomic_send_yy
* ibapi_multi_atomic_send
*
* RETURN:
* 0 on success
* nagative value on failure
*/
static int make_coherence_and_replication(int commit_node_id, int nr_areas,
struct dr_no_dro_page_offset *meta_for_areas, struct atomic_struct *reqs,
int *nr_reps_per_area, int if_from_single_on_handler, int if_do_send_yy)
{
int i, j, ret = 0;
int **dn_area_map_to_area = NULL;
int **coherence_succeed_node = NULL;
int nr_coherence_dns = 0;
int pos[DSNVM_MAX_NODE];
int send_dn_list[DSNVM_MAX_NODE];
int nr_areas_per_dn[DSNVM_MAX_NODE];
struct max_reply_msg *reply_msg = NULL;
struct atomic_struct **xact_reqs = NULL;
struct dr_no_dro_page_offset **meta_for_replica_areas = NULL;
struct dsnvm_commit_request_header req_header[DSNVM_MAX_NODE];
ret = -ENOMEM;
reply_msg = kmalloc(sizeof(*reply_msg) * DSNVM_MAX_NODE, GFP_KERNEL);
if (!reply_msg)
goto out;
xact_reqs = kmalloc(sizeof(*xact_reqs) * DSNVM_MAX_NODE, GFP_KERNEL);
if (!xact_reqs)
goto out;
meta_for_replica_areas = kmalloc(sizeof(*meta_for_replica_areas) * DSNVM_MAX_NODE, GFP_KERNEL);
if (!meta_for_replica_areas)
goto out;
dn_area_map_to_area = kmalloc(sizeof(int *) * DSNVM_MAX_NODE, GFP_KERNEL);
if (!dn_area_map_to_area)
goto out;
coherence_succeed_node = kmalloc(sizeof(int *) * DSNVM_MAX_NODE, GFP_KERNEL);
if (!coherence_succeed_node)
goto out;
/* Allocate array for per NODE */
for (i = 0; i < DSNVM_MAX_NODE; i++) {
dn_area_map_to_area[i] = kzalloc(sizeof(int) * nr_areas, GFP_KERNEL);
if (!dn_area_map_to_area[i])
goto out;
coherence_succeed_node[i] = kzalloc(sizeof(int) * nr_areas, GFP_KERNEL);
if (!coherence_succeed_node[i])
goto out;
/* Need 2 more requestes for metadata */
xact_reqs[i] = kmalloc(sizeof(struct atomic_struct) * (nr_areas + 2), GFP_KERNEL);
if (!xact_reqs[i])
goto out;
meta_for_replica_areas[i] = kzalloc(sizeof(struct dr_no_dro_page_offset) * nr_areas, GFP_KERNEL);
if (!meta_for_replica_areas[i])
goto out;
nr_areas_per_dn[i] = 0;
pos[i] = -1;
/* Will be updated later */
req_header[i].nr_reqs = 0;
req_header[i].op = DSNVM_OP_SEND_COHERENCE_XACT;
/* The first metadata request */
xact_reqs[i][0].vaddr = &req_header[i];
xact_reqs[i][0].len = sizeof(struct dsnvm_commit_repdegree_request_header);
/* The second metadata request */
xact_reqs[i][1].vaddr = meta_for_replica_areas[i];
}
DSNVM_PRINTK("Commit-NodeID: %d, nr_areas: %d "
"from_single_on_handler %d do_send_yy %d",
commit_node_id, nr_areas, if_from_single_on_handler, if_do_send_yy);
/*
* Get all the DNs that have at least one data page in the xact
* these are the coherence nodes that we need to send the xact to
*/
for (i = 0; i < nr_areas; i++) {
unsigned long dr_no = meta_for_areas[i].dr_no;
unsigned int dro = meta_for_areas[i].dro;
unsigned long pgoft = meta_for_areas[i].page_offset;
struct on_region_info *on_dr;
struct on_page_info *on_page;
int node, curr_pos, curr_area;
on_dr = ht_get_on_region(dr_no);
if (unlikely(!on_dr)) {
DSNVM_BUG("commit_node_id: %d, local_node_id: %d "
"dr_no: %lu, dro: %u", commit_node_id,
DSNVM_LOCAL_ID, dr_no, dro);
continue;
}
BUG_ON(is_on_region_migrating_out(on_dr));
spin_lock(&on_dr->page_lock[dro]);
on_page = &on_dr->mapping[dro];
/* Find out what DN nodes need coherence update */
for_each_set_bit(node, on_page->dn_list, DSNVM_MAX_NODE) {
if (node == DSNVM_LOCAL_ID)
continue;
/* Off-line */
if (unlikely(!test_bit(node, DSNVM_CLIENT_MACHINES)))
continue;
/* Do NOT send back to the commiting node */
if (node == commit_node_id)
continue;
/* First time see this DN node */
if (pos[node] == -1) {
/* record the index of this node within send list */
pos[node] = nr_coherence_dns;
send_dn_list[nr_coherence_dns++] = node;
DSNVM_PRINTK("Add node: %d to coherence_dn list", node);
}
/* Alright, then update those arrays... */
curr_pos = pos[node];
curr_area = nr_areas_per_dn[curr_pos];
/* Store kernel vaddr and len for this area */
/* The first 2 are metadata, so we have a 2 shift */
xact_reqs[curr_pos][curr_area + 2].vaddr = reqs[i].vaddr;
xact_reqs[curr_pos][curr_area + 2].len = reqs[i].len;
/* Update the second metadata request */
meta_for_replica_areas[curr_pos][curr_area].dr_no = dr_no;
meta_for_replica_areas[curr_pos][curr_area].dro = dro;
meta_for_replica_areas[curr_pos][curr_area].page_offset = pgoft;
/* Save area_idx used later to calculate nr_rep_per_area */
dn_area_map_to_area[curr_pos][curr_area] = i;
nr_areas_per_dn[curr_pos]++;
if (unlikely(nr_areas_per_dn[curr_pos] >= MAX_ATOMIC_SEND_NUM)) {
DSNVM_BUG("Too many areas in one xact");
ret = -EFAULT;
goto out;
}
}
spin_unlock(&on_dr->page_lock[dro]);
}
/* Update the first 2 metadata requests */
for (i = 0; i < nr_coherence_dns; i++) {
if (unlikely(nr_areas_per_dn[i] == 0)) {
ret = DSNVM_REPLY_CANNOT_MAKE_ENOUGH_REPLICA;
goto out;
}
/* 1st req */
req_header[i].nr_reqs = nr_areas_per_dn[i];
/* 2st req */
xact_reqs[i][1].len = nr_areas_per_dn[i] * sizeof(struct dr_no_dro_page_offset);
nr_areas_per_dn[i] += 2;
}
/* Now send to remote DNs */
if (likely(nr_coherence_dns > 0)) {
DSNVM_PRINTK("Before sending coherence data to total %d DNs", nr_coherence_dns);
/*
* ibapi_multi_atomic_send_yy: xact_reqs use Physical Address
* ibapi_multi_atomic_send: xact_reqs use Virtual Kernel Address
*/
if (if_do_send_yy == 1) {
ibapi_multi_atomic_send_yy(nr_coherence_dns, send_dn_list,
xact_reqs, nr_areas_per_dn, reply_msg);
} else {
ibapi_multi_atomic_send(nr_coherence_dns, send_dn_list,
xact_reqs, nr_areas_per_dn, reply_msg);
}
DSNVM_PRINTK("After sending coherence data to total %d DNs", nr_coherence_dns);
} else {
/* no one has ever send us a page-fetch, how sad it is */
DSNVM_PRINTK("No remote coherent DN involved");
}
count_dsnvm_events(DSNVM_COHERENCE_TX, nr_coherence_dns);
/* Collect status reported by remote ONs: */
for (i = 0; i < nr_coherence_dns; i++) {
unsigned long *reply_bitmap = NULL;
reply_bitmap = (unsigned long *)(&reply_msg[i]);
for (j = 0; j < nr_areas_per_dn[i] - 2; j++) {
int area_idx = dn_area_map_to_area[i][j];
int node_id = send_dn_list[i];
if (test_bit(j, reply_bitmap)) {
/* Cool, this DN counts a valid replica copy */
nr_reps_per_area[area_idx]--;
coherence_succeed_node[node_id][area_idx] = 1;
} else {
coherence_succeed_node[node_id][area_idx] = 0;
}
}
}
/* Check if we meet the rep_degree: */
for (i = 0; i < nr_areas; i++) {
if (nr_reps_per_area[i] > 0)
break;
}
/*
* If these ONs alone can NOT meet the replication degree, ON will
* choose new DNs that do not have a copy of the data and send the
* data to them
*/
if (i != nr_areas) {
DSNVM_PRINTK("Need to make more replicas");
count_dsnvm_event(DSNVM_REPLICATION_NEED_EXTRA);
ret = make_replication(commit_node_id, nr_areas, meta_for_areas,
reqs, nr_reps_per_area, if_do_send_yy, xact_reqs, meta_for_replica_areas,
coherence_succeed_node, reply_msg);
} else {
ret = 0;
DSNVM_PRINTK("NO need to make more replicas");
}
out:
if (reply_msg)
kfree(reply_msg);
if (xact_reqs) {
for (i = 0; i < DSNVM_MAX_NODE; i++) {
if (xact_reqs[i])
kfree(xact_reqs[i]);
}
kfree(xact_reqs);
}
if (dn_area_map_to_area) {
for (i = 0; i < DSNVM_MAX_NODE; i++) {
if (dn_area_map_to_area[i])
kfree(dn_area_map_to_area[i]);
}
kfree(dn_area_map_to_area);
}
if (coherence_succeed_node) {
for (i = 0; i < DSNVM_MAX_NODE; i++) {
if (coherence_succeed_node[i])
kfree(coherence_succeed_node[i]);
}
kfree(coherence_succeed_node);
}
if (meta_for_replica_areas) {
for (i = 0; i < DSNVM_MAX_NODE; i++) {
if (meta_for_replica_areas[i])
kfree(meta_for_replica_areas[i]);
}
kfree(meta_for_replica_areas);
}
return ret;
}
/*
* This functions describes:
* ON handler for phase 1 of commit protocol
*
* Related IB API:
* ibapi_multi_atomic_send_yy
*
* It will record (pointers of) data and metadata in a redo-log and try to
* lock requested local ON pages by set if_blocked_by_commit_xact to 1. It will
* only succeed if and only if all requested local ON pages are locked.
*/
int dsnvm_handle_request_commit_xact(int sender_id, int nr_reqs,
struct atomic_struct *reqs,
char *reply_addr, unsigned int *reply_len)
{
struct dsnvm_commit_repdegree_request_header *meta_msg;
struct dr_no_dro_page_offset *meta_for_areas;
struct status_reply_msg *reply;
struct dsnvm_log_record *log_rec;
int nr_areas, log_id, rep_degree, xact_id;
int i, j;
int failed_area = 0;
count_dsnvm_event(DSNVM_MRMW_REMOTE_ON_N_RX);
transaction_enter();
if (unlikely(nr_reqs <= 1)) {
DSNVM_BUG("no data req in commit xact");
reply->status = DSNVM_NO_DATA_IN_REQUEST;
transaction_exit();
return 0;
}
/* Fill reply message first */
reply = (struct status_reply_msg *)reply_addr;
reply->status = DSNVM_REPLY_SUCCESS;
*reply_len = sizeof(struct status_reply_msg);
/* Get metadata from header requests */
meta_for_areas = reqs[1].vaddr;
meta_msg = reqs[0].vaddr;
nr_areas = meta_msg->nr_reqs;
rep_degree = meta_msg->rep_degree;
xact_id = meta_msg->xact_id;
/* First two requests are metadata */
/* Check dsnvm_request_commit_xact_to_ons for details */
if (unlikely(nr_areas != nr_reqs - 2)) {
DSNVM_BUG("nr_areas: %d, nr_reqs: %d",nr_areas, nr_reqs);
reply->status = DSNVM_REQ_AREA_DONT_MACTH;
transaction_exit();
return 0;
}
/* Allocate redo log in this ON */
log_rec = alloc_dsnvm_log(xact_id, &log_id);
if (unlikely(!log_rec)) {
DSNVM_WARN("No more logs");
reply->status = DSNVM_REPLY_LOG_FULL;
transaction_exit();
return 0;
}
/* Save metadata into redo-log */
log_rec->xact_id = xact_id;
log_rec->sender_id = sender_id;
log_rec->state = DSNVM_LOG_NOT_TO_REPLAY | DSNVM_LOG_PHASE_1_MIDDLE;
log_rec->nr_areas = nr_areas;
log_rec->rep_degree = rep_degree;
log_rec->meta_for_areas = meta_for_areas;
for (i = 0; i < nr_areas; i++) {
unsigned long dr_no = meta_for_areas[i].dr_no;
unsigned int dro = meta_for_areas[i].dro;
/*
* Save area info into redo-log
*
* Note that: reqs buffers are allocated by IB layer, we are
* just saving the virtual kernel address of these buffers into
* our log_rec. Those buffers are freed by Hotpot when we call
* xact_free_log_data().
*/
log_rec->data_areas[i].vaddr = reqs[i+2].vaddr;
log_rec->data_areas[i].len = reqs[i+2].len;
DSNVM_PRINTK("Logging area[%d]: dr_no %lu dro %u, VA: %p len: %zu",
i, dr_no, dro, reqs[i+2].vaddr, reqs[i+2].len);
}
/* Flush back all log except log_id first */
dsnvm_flush_buffer(log_rec, sizeof(*log_rec));
/* Now set log_id and flush back, make this log valid persistent */
log_rec->log_id = log_id;
dsnvm_flush_buffer(&log_rec->log_id, sizeof(log_rec->log_id));
DSNVM_PRINTK("Sender-ID: %d, log_id: %d, xact_id: %d, "
"nr_reqs: %d, nr_area: %d, rep_degree: %d",
sender_id, log_id, xact_id, nr_reqs, nr_areas, rep_degree);
/* Redo-log saved, now block ON_REGION pages */
for (i = 0; i < nr_areas; i++) {
struct on_region_info *on_dr;
struct on_page_info *on_page;
unsigned long dr_no = meta_for_areas[i].dr_no;
unsigned int dro = meta_for_areas[i].dro;
on_dr = ht_get_on_region(dr_no);
if (unlikely(!on_dr)) {
if (likely(proxy_find_new_owner(dr_no) > 0)) {
/*
* The ON_REGION was migrated out in short
* period before this.
*/
reply->status = DSNVM_REPLY_ON_REGION_MIGRATING_OUT;
} else {
reply->status = DSNVM_ENOREGION;
DSNVM_BUG("Receiving area that's not owned by myself: "
"Sender-ID: %d, dr_no %lu, dro %u",
sender_id, dr_no, dro);
}
failed_area = i;
goto error;
}
/*
* Locks:
* @on->region_lock
* @on->page_lock[dro]
*/
spin_lock(&on_dr->region_lock);
if (unlikely(is_on_region_migrating_out(on_dr))) {
spin_unlock(&on_dr->region_lock);
DSNVM_PRINTK("dr_no: %lu is migrating out", dr_no);
count_dsnvm_event(DSNVM_XACT_REJECT_DUE_TO_MIGRATION);
reply->status = DSNVM_REPLY_ON_REGION_MIGRATING_OUT;
failed_area = i;
goto error;
}
spin_lock(&on_dr->page_lock[dro]);
on_page = &on_dr->mapping[dro];
for (j = 0; j < i; j++) {
if (dr_no == meta_for_areas[j].dr_no &&
dro == meta_for_areas[j].dro) {
break;
}
}
/* First time see this page */
if (likely(j == i)) {
if (unlikely(on_page->if_blocked_by_commit_xact == 1)) {
/*
* Blocked by another XACT
*/
DSNVM_PRINTK_BLOCK("Concurrent block dr_no %lu dro %u",
dr_no, dro);
reply->status = DSNVM_REPLY_PAGE_IN_OTHER_XACT;
spin_unlock(&on_dr->page_lock[dro]);
spin_unlock(&on_dr->region_lock);
count_dsnvm_event(DSNVM_XACT_REJECT_DUE_TO_BLOCKED_PAGES);
failed_area = i;
goto error;
} else {